14#include "mitkImageReadAccessor.h"
22#include <usGetModuleContext.h>
24#include <usServiceProperties.h>
25#include <usModuleContext.h>
35 MITK_INFO <<
"Return Crop Area L:" << m_CropArea.
cropLeft
36 <<
" R:" << m_CropArea.cropRight <<
" T:" << m_CropArea.cropTop
37 <<
" B:" << m_CropArea.cropBottom;
43 return m_ImageVector.size();
47 :
mitk::ImageSource(),
50 m_IGTLServer(nullptr),
51 m_IGTLMessageProvider(nullptr),
52 m_ImageToIGTLMsgFilter(nullptr),
54 m_DeviceState(State_NoState),
56 m_ServiceProperties(),
57 m_ServiceRegistration(),
58 m_Manufacturer(manufacturer),
61 m_SpawnAcquireThread(true),
62 m_UnregisteringStarted(false)
75 mitk::Image::Pointer newOutput = mitk::Image::New();
76 this->SetNthOutput(0, newOutput);
80 :
mitk::ImageSource(),
83 m_IGTLServer(nullptr),
84 m_IGTLMessageProvider(nullptr),
85 m_ImageToIGTLMsgFilter(nullptr),
87 m_DeviceState(State_NoState),
89 m_ServiceProperties(),
90 m_ServiceRegistration(),
91 m_SpawnAcquireThread(true),
92 m_UnregisteringStarted(false)
94 m_Manufacturer = metadata->GetDeviceManufacturer();
95 m_Name = metadata->GetDeviceModel();
96 m_Comment = metadata->GetDeviceComment();
109 mitk::Image::Pointer newOutput = mitk::Image::New();
110 this->SetNthOutput(0, newOutput);
115 if (m_Thread.joinable())
120 this->UnregisterOnService();
123mitk::USAbstractControlInterface::Pointer
126 MITK_INFO <<
"Custom control interface does not exist for this object.";
130mitk::USControlInterfaceBMode::Pointer
133 MITK_INFO <<
"Control interface BMode does not exist for this object.";
137mitk::USControlInterfaceProbes::Pointer
140 MITK_INFO <<
"Control interface Probes does not exist for this object.";
144mitk::USControlInterfaceDoppler::Pointer
147 MITK_INFO <<
"Control interface Doppler does not exist for this object.";
153 m_Manufacturer = manufacturer;
154 if (m_DeviceState >= State_Initialized)
156 this->UpdateServiceProperty(
165 if (m_DeviceState >= State_Initialized)
167 this->UpdateServiceProperty(
175 if (m_DeviceState >= State_Initialized)
177 this->UpdateServiceProperty(
186 us::ServiceProperties props;
189 this->GetIsConnected() ?
"true" :
"false";
191 this->GetIsActive() ?
"true" :
"false";
196 mitk::USControlInterfaceProbes::Pointer probesControls =
197 this->GetControlInterfaceProbes();
198 if (probesControls.IsNotNull() && probesControls->GetIsActive())
200 mitk::USProbe::Pointer probe = probesControls->GetSelectedProbe();
201 if (probe.IsNotNull())
212 m_ServiceProperties = props;
220 if (m_ServiceRegistration && !m_UnregisteringStarted)
224 m_UnregisteringStarted =
true;
225 m_ServiceRegistration.Unregister();
226 m_ServiceRegistration = 0;
232 if (!this->OnInitialization())
237 m_DeviceState = State_Initialized;
240 us::ModuleContext* context = us::GetModuleContext();
241 us::ServiceProperties props = this->ConstructServiceProperties();
243 m_ServiceRegistration = context->RegisterService(
this, props);
250 MITK_DEBUG <<
"mitk::USDevice::Connect() called";
252 if (this->GetIsConnected())
254 MITK_INFO(
"mitkUSDevice") <<
"Tried to connect an ultrasound device that "
255 "was already connected. Ignoring call...";
259 if (!this->GetIsInitialized())
261 MITK_ERROR(
"mitkUSDevice")
262 <<
"Cannot connect device if it is not in initialized state.";
267 if (!this->OnConnection())
273 m_DeviceState = State_Connected;
275 this->UpdateServiceProperty(
287 if (!GetIsConnected())
289 MITK_WARN <<
"Tried to disconnect an ultrasound device that was not "
290 "connected. Ignoring call...";
294 if (!this->OnDisconnection())
298 m_DeviceState = State_Initialized;
300 this->UpdateServiceProperty(
308 if (!this->GetIsConnected())
310 MITK_INFO(
"mitkUSDevice")
311 <<
"Cannot activate device if it is not in connected state.";
317 m_DeviceState = State_Activated;
320 if (m_SpawnAcquireThread)
325 this->UpdateServiceProperty(
327 this->UpdateServiceProperty(
329 this->GetServicePropertyLabel());
331 mitk::USControlInterfaceBMode::Pointer bmodeControls =
332 this->GetControlInterfaceBMode();
333 if (bmodeControls.IsNotNull())
335 bmodeControls->Initialize();
339 this->ProvideViaOIGTL();
341 return m_DeviceState == State_Activated;
347 if (m_IGTLServer.IsNull())
348 m_IGTLServer = mitk::IGTLServer::New(
true);
350 m_IGTLServer->SetName(this->GetName());
353 if (m_IGTLMessageProvider.IsNull())
354 m_IGTLMessageProvider = mitk::IGTLMessageProvider::New();
357 m_IGTLMessageProvider->SetIGTLDevice(m_IGTLServer);
361 m_IGTLMessageProvider->RegisterAsMicroservice();
363 m_ImageToIGTLMsgFilter = mitk::ImageToIGTLMessageFilter::New();
364 m_ImageToIGTLMsgFilter->ConnectTo(
this);
367 m_ImageToIGTLMsgFilter->SetName(this->GetName());
373 m_ImageToIGTLMsgFilter->RegisterAsMicroservice();
378 if (!this->GetIsActive())
380 MITK_WARN(
"mitkUSDevice")
381 <<
"Cannot deactivate a device which is not activae.";
385 if (!OnDeactivation())
391 m_DeviceState = State_Connected;
393 this->UpdateServiceProperty(
395 this->UpdateServiceProperty(
397 this->GetServicePropertyLabel());
404 m_IGTLServer->CloseConnection();
405 m_IGTLMessageProvider->UnRegisterMicroservice();
406 m_ImageToIGTLMsgFilter->UnRegisterMicroservice();
411 if (!this->GetIsActive())
413 MITK_WARN(
"mitkUSDevice")
414 <<
"Cannot freeze or unfreeze if device is not active.";
418 this->OnFreeze(freeze);
422 std::lock_guard<std::mutex> lock(m_FreezeMutex);
428 std::lock_guard<std::mutex> lock(m_FreezeMutex);
433 m_FreezeBarrier.notify_one();
453 mitk::USImageSource::Pointer imageSource = this->GetUSImageSource();
454 if (imageSource.IsNull())
456 MITK_ERROR <<
"ImageSource must not be null when pushing a filter.";
457 mitkThrow() <<
"ImageSource must not be null when pushing a filter.";
460 imageSource->PushFilter(filter);
464 AbstractOpenCVImageFilter::Pointer filter)
466 mitk::USImageSource::Pointer imageSource = this->GetUSImageSource();
467 if (imageSource.IsNull())
469 MITK_ERROR <<
"ImageSource must not be null when pushing a filter.";
470 mitkThrow() <<
"ImageSource must not be null when pushing a filter.";
473 if (!imageSource->GetIsFilterInThePipeline(filter))
475 imageSource->PushFilter(filter);
481 mitk::USImageSource::Pointer imageSource = this->GetUSImageSource();
482 if (imageSource.IsNull())
484 MITK_ERROR <<
"ImageSource must not be null when pushing a filter.";
485 mitkThrow() <<
"ImageSource must not be null when removing a filter.";
488 return imageSource->RemoveFilter(filter);
493 m_ServiceProperties[key] = value;
494 m_ServiceRegistration.SetProperties(m_ServiceProperties);
497 m_PropertyChangedMessage(key, value);
502 std::stringstream stream;
504 this->UpdateServiceProperty(key, stream.str());
509 this->UpdateServiceProperty(
510 key, value ? std::string(
"true") : std::string(
"false"));
560 std::vector<mitk::Image::Pointer> image = this->GetUSImageSource()->GetNextImage();
562 this->SetImageVector(image);
563 m_ImageMutex.unlock();
570 return m_DeviceState == State_Initialized;
577 return m_DeviceState == State_Connected;
580std::string mitk::USDevice::GetDeviceManufacturer() {
return m_Manufacturer; }
582std::string mitk::USDevice::GetDeviceModel() {
return m_Name; }
584std::string mitk::USDevice::GetDeviceComment() {
return m_Comment; }
588 m_Spacing[0] = xSpacing;
589 m_Spacing[1] = ySpacing;
593 if( m_ImageVector.size() > 0 )
595 for(
size_t index = 0; index < m_ImageVector.size(); ++index )
597 auto& image = m_ImageVector[index];
598 if( image.IsNotNull() && image->IsInitialized() )
600 image->GetGeometry()->SetSpacing(m_Spacing);
605 MITK_INFO <<
"Spacing: " << m_Spacing;
612 for (
unsigned int i = 0; i < m_ImageVector.size() && i < this->GetNumberOfIndexedOutputs(); ++i)
614 auto& image = m_ImageVector[i];
615 if (image.IsNull() || !image->IsInitialized())
621 mitk::Image::Pointer output = this->GetOutput(i);
623 if (!output->IsInitialized() ||
624 output->GetDimension(0) != image->GetDimension(0) ||
625 output->GetDimension(1) != image->GetDimension(1) ||
626 output->GetDimension(2) != image->GetDimension(2) ||
627 output->GetPixelType() != image->GetPixelType())
629 output->Initialize(image->GetPixelType(), image->GetDimension(),
630 image->GetDimensions());
634 mitk::ImageReadAccessor inputReadAccessor(image);
635 output->SetImportVolume(inputReadAccessor.GetData());
636 output->SetGeometry(image->GetGeometry());
639 m_ImageMutex.unlock();
644 std::string isActive;
645 if (this->GetIsActive())
647 isActive =
" (Active)";
651 isActive =
" (Inactive)";
654 return m_Manufacturer +
" " + m_Name + isActive;
659 while (this->GetIsActive())
662 std::unique_lock<std::mutex> lock(m_FreezeMutex);
663 m_FreezeBarrier.wait(lock, [
this] {
return !m_IsFreezed; });
bool Disconnect()
Works analogously to mitk::USDevice::Connect(). Don't override this Method, but onDisconnection inste...
void GenerateData() override
Grabs the next frame from the Video input. This method is called internally, whenever Update() is inv...
virtual itk::SmartPointer< USControlInterfaceProbes > GetControlInterfaceProbes()
Default getter for the probes control interface. Has to be implemented in a subclass if a probes cont...
mitk::USDevice::USImageCropArea GetCropArea()
bool RemoveFilter(AbstractOpenCVImageFilter::Pointer filter)
void DisableOIGTL()
Deregisters the microservices for OpenIGTLink.
us::ServiceProperties ConstructServiceProperties()
This Method constructs the service properties which can later be used to register the object with the...
unsigned int GetSizeOfImageVector()
void ProvideViaOIGTL()
Registers an OpenIGTLink device as a microservice so that we can send the images of this device via t...
bool GetIsConnected()
True, if the device is currently ready to start transmitting image data or is already transmitting im...
USImageCropArea m_CropArea
virtual itk::SmartPointer< USAbstractControlInterface > GetControlInterfaceCustom()
Default getter for the custom control interface. Has to be implemented in a subclass if a custom cont...
void DepthChanged(double depth)
To be called when the scanning depth of the probe changed. Will update the service properties.
bool GetIsActive()
True, if the device is currently generating image data, false otherwise.
void PushFilter(AbstractOpenCVImageFilter::Pointer filter)
virtual void SetIsFreezed(bool freeze)
Can toggle if ultrasound image is currently updated or freezed.
void UnregisterOnService()
Remove this device from the micro service.
void SetManufacturer(std::string manufacturer)
bool Initialize()
Changes device state to mitk::USDevice::State_Initialized. During initialization the virtual method m...
void SetComment(std::string comment)
virtual void SetSpacing(double xSpacing, double ySpacing)
void UpdateServiceProperty(std::string key, std::string value)
Given property is updated in the device micro service. This method is mainly for being used by the co...
void SetName(std::string name)
bool Activate()
Activates this device. After the activation process, the device will start to produce images....
static mitk::USDevice::PropertyKeys GetPropertyKeys()
void PushFilterIfNotPushedBefore(AbstractOpenCVImageFilter::Pointer filter)
void Deactivate()
Deactivates this device. After the deactivation process, the device will no longer produce images,...
virtual itk::SmartPointer< USControlInterfaceDoppler > GetControlInterfaceDoppler()
Default getter for the doppler control interface. Has to be implemented in a subclass if a doppler co...
USDevice(std::string manufacturer, std::string model)
Enforces minimal Metadata to be set.
void ProbeChanged(std::string probename)
To be called when the used probe changed. Will update the service properties.
unsigned int m_NumberOfOutputs
virtual itk::SmartPointer< USControlInterfaceBMode > GetControlInterfaceBMode()
Default getter for the b mode control interface. Has to be implemented in a subclass if a b mode cont...
virtual bool GetIsFreezed()
std::string GetServicePropertyLabel()
bool Connect()
Connects this device. A connected device is ready to deliver images (i.e. be Activated)....
bool GetIsInitialized()
True, if the device object is created and initialized, false otherwise.
These constants are used in conjunction with Microservices. The constants aren't defined as static me...
const std::string US_PROPKEY_CLASS
const std::string US_PROPKEY_ISACTIVE
const std::string US_PROPKEY_COMMENT
const std::string US_PROPKEY_ISCONNECTED
const std::string US_PROPKEY_MANUFACTURER
const std::string US_PROPKEY_NAME
const std::string US_PROPKEY_PROBES_SELECTED
const std::string US_PROPKEY_LABEL