14#include "mitkImageReadAccessor.h"
19#include <mitkUIDGenerator.h>
27#include <usGetModuleContext.h>
29#include <usModuleContext.h>
30#include <usServiceProperties.h>
38 "org.mitk.modules.us.AbstractUltrasoundTrackerDevice";
43 "org.mitk.services.AbstractUltrasoundTrackerDevice";
50 NavigationDataSource::Pointer trackingDevice,
51 bool trackedUltrasoundActive)
52 : m_UltrasoundDevice(usDevice),
53 m_TrackingDeviceDataSource(trackingDevice),
57 m_LastFilterOfIGTPipeline(nullptr),
58 m_NumberOfSmoothingValues(0),
60 m_IsTrackedUltrasoundActive(trackedUltrasoundActive)
77 return mitk::AffineTransform3D::New();
82 if (m_UltrasoundDevice.IsNull() || m_TrackingDeviceDataSource.IsNull())
84 MITK_WARN <<
"Combined modality not correctly initialized, aborting!";
88 if (!m_UltrasoundDevice->GetIsActive())
90 MITK_WARN(
"mitkUSDevice")
91 <<
"Cannot freeze or unfreeze if device is not active.";
95 this->OnFreeze(freeze);
114 if (m_ServiceRegistration !=
nullptr)
116 m_ServiceRegistration.Unregister();
118 m_ServiceRegistration = 0;
123 return this->GetCalibration(this->GetCurrentDepthValue(), this->GetIdentifierForCurrentProbe());
128 return this->GetCalibration(depth, this->GetIdentifierForCurrentProbe());
135 std::replace(probe.begin(), probe.end(),
'/',
'-');
141 std::map<std::string, mitk::AffineTransform3D::Pointer>::iterator calibrationIterator =
142 m_Calibrations.find(calibrationKey);
144 if (calibrationIterator == m_Calibrations.end())
149 return calibrationIterator->second;
154 if (calibration.IsNull())
156 MITK_WARN <<
"Null pointer passed to SetCalibration of mitk::USDevice. Ignoring call.";
160 std::string calibrationKey = this->GetIdentifierForCurrentCalibration();
161 if (calibrationKey.empty())
163 MITK_WARN <<
"Could not get a key for the calibration -> Calibration cannot be set.";
167 m_Calibrations[calibrationKey] = calibration;
172 return this->RemoveCalibration(this->GetCurrentDepthValue(), this->GetIdentifierForCurrentProbe());
177 return this->RemoveCalibration(depth, this->GetIdentifierForCurrentProbe());
183 std::replace(probe.begin(), probe.end(),
'/',
'-');
188 return m_Calibrations.erase(calibrationKey) > 0;
193 return DeviceClassIdentifier;
198 if (m_UltrasoundDevice.IsNull())
200 MITK_ERROR(
"AbstractUltrasoundTrackerDevice")(
"USDevice") <<
"UltrasoundDevice must not be null.";
201 mitkThrow() <<
"UltrasoundDevice must not be null.";
204 return m_UltrasoundDevice->GetUSImageSource();
209 if (m_LastFilterOfIGTPipeline.IsNull())
211 this->RebuildFilterPipeline();
213 m_LastFilterOfIGTPipeline->SetToolMetaDataCollection(this->m_TrackingDeviceDataSource->GetToolMetaDataCollection());
214 return m_LastFilterOfIGTPipeline;
219 return m_Calibrations.find(this->GetIdentifierForCurrentCalibration()) != m_Calibrations.end();
224 return !m_Calibrations.empty();
229 tinyxml2::XMLDocument doc;
230 auto* calibrations = doc.NewElement(
"calibrations");
231 doc.InsertEndChild(calibrations);
234 for (std::map<std::string, mitk::AffineTransform3D::Pointer>::iterator it = m_Calibrations.begin();
235 it != m_Calibrations.end();
238 mitk::AffineTransform3D::MatrixType matrix = it->second->GetMatrix();
239 mitk::AffineTransform3D::TranslationType translation = it->second->GetTranslation();
240 auto *elem = doc.NewElement(it->first.c_str());
242 elem->SetAttribute(
"M00", matrix[0][0]);
243 elem->SetAttribute(
"M01", matrix[0][1]);
244 elem->SetAttribute(
"M02", matrix[0][2]);
245 elem->SetAttribute(
"M10", matrix[1][0]);
246 elem->SetAttribute(
"M11", matrix[1][1]);
247 elem->SetAttribute(
"M12", matrix[1][2]);
248 elem->SetAttribute(
"M20", matrix[2][0]);
249 elem->SetAttribute(
"M21", matrix[2][1]);
250 elem->SetAttribute(
"M22", matrix[2][2]);
252 elem->SetAttribute(
"T0", translation[0]);
253 elem->SetAttribute(
"T1", translation[1]);
254 elem->SetAttribute(
"T2", translation[2]);
256 calibrations->InsertEndChild(elem);
259 tinyxml2::XMLPrinter printer;
262 return printer.CStr();
266 bool clearPreviousCalibrations)
271 MITK_ERROR <<
"Empty string passed to Deserialize() method of CombinedModality. Aborting...";
272 mitkThrow() <<
"Empty string passed to Deserialize() method of CombinedModality. Aborting...";
276 if (clearPreviousCalibrations)
277 m_Calibrations.clear();
280 tinyxml2::XMLDocument doc;
281 if (tinyxml2::XML_SUCCESS != doc.Parse(xmlString.c_str()))
283 MITK_ERROR <<
"Unable to deserialize calibrations in CombinedModality. Error was: " << doc.ErrorStr();
284 mitkThrow() <<
"Unable to deserialize calibrations in CombinedModality. Error was: " << doc.ErrorStr();
287 auto *root = doc.FirstChildElement();
290 MITK_ERROR <<
"Unable to deserialize calibrations in CombinedModality. String contained no root element.";
291 mitkThrow() <<
"Unable to deserialize calibrations in CombinedModality. String contained no root element.";
295 for (
auto *elem = root->FirstChildElement(); elem !=
nullptr; elem = elem->NextSiblingElement())
297 mitk::AffineTransform3D::MatrixType matrix;
298 mitk::AffineTransform3D::OffsetType translation;
300 std::string calibName = elem->Value();
303 elem->QueryDoubleAttribute(
"M00", &matrix[0][0]);
304 elem->QueryDoubleAttribute(
"M01", &matrix[0][1]);
305 elem->QueryDoubleAttribute(
"M02", &matrix[0][2]);
306 elem->QueryDoubleAttribute(
"M10", &matrix[1][0]);
307 elem->QueryDoubleAttribute(
"M11", &matrix[1][1]);
308 elem->QueryDoubleAttribute(
"M12", &matrix[1][2]);
309 elem->QueryDoubleAttribute(
"M20", &matrix[2][0]);
310 elem->QueryDoubleAttribute(
"M21", &matrix[2][1]);
311 elem->QueryDoubleAttribute(
"M22", &matrix[2][2]);
314 elem->QueryDoubleAttribute(
"T0", &translation[0]);
315 elem->QueryDoubleAttribute(
"T1", &translation[1]);
316 elem->QueryDoubleAttribute(
"T2", &translation[2]);
318 mitk::AffineTransform3D::Pointer calibration = mitk::AffineTransform3D::New();
319 calibration->SetMatrix(matrix);
320 calibration->SetTranslation(translation);
321 m_Calibrations[calibName] = calibration;
327 unsigned int oldNumber = m_NumberOfSmoothingValues;
328 m_NumberOfSmoothingValues = numberOfSmoothingValues;
331 if ((oldNumber == 0 && numberOfSmoothingValues != 0) || (oldNumber != 0 && numberOfSmoothingValues == 0))
333 this->RebuildFilterPipeline();
335 m_SmoothingFilter->SetNumerOfValues(numberOfSmoothingValues);
340 unsigned int oldCount = m_DelayCount;
341 m_DelayCount = delayCount;
344 if ((oldCount == 0 && delayCount != 0) || (oldCount != 0 && delayCount == 0))
346 this->RebuildFilterPipeline();
348 m_DelayFilter->SetDelay(delayCount);
356 this->GetCurrentDepthValue();
361 us::ServiceProperties usdeviceProperties = m_UltrasoundDevice->GetServiceProperties();
363 us::ServiceProperties::const_iterator probeIt =
368 if (probeIt != usdeviceProperties.end())
370 probeName = (probeIt->second).ToString();
374 std::replace(probeName.begin(), probeName.end(),
'/',
'-');
381 us::ServiceProperties usdeviceProperties = m_UltrasoundDevice->GetServiceProperties();
385 us::ServiceProperties::iterator depthIterator =
388 if (depthIterator != usdeviceProperties.end())
390 depth = depthIterator->second.ToString();
402 m_LastFilterOfIGTPipeline = m_TrackingDeviceDataSource;
404 if (m_NumberOfSmoothingValues > 0)
406 m_SmoothingFilter->ConnectTo(m_LastFilterOfIGTPipeline.GetPointer());
407 m_LastFilterOfIGTPipeline = m_SmoothingFilter;
410 if (m_DelayCount > 0)
412 m_DelayFilter->ConnectTo(m_LastFilterOfIGTPipeline.GetPointer());
413 m_LastFilterOfIGTPipeline = m_DelayFilter;
416 if (m_IsTrackedUltrasoundActive)
418 m_DisplacementFilter->ConnectTo(m_LastFilterOfIGTPipeline.GetPointer());
419 m_LastFilterOfIGTPipeline = m_DisplacementFilter;
427 m_UltrasoundDevice->Deactivate();
431 m_UltrasoundDevice->Disconnect();
434 if (m_ServiceRegistration !=
nullptr)
435 m_ServiceRegistration.Unregister();
436 m_ServiceRegistration = 0;
442 us::ModuleContext *context = us::GetModuleContext();
446 mitk::UIDGenerator uidGen = mitk::UIDGenerator(
"org.mitk.services.AbstractUltrasoundTrackerDevice");
447 m_ServiceProperties[US_PROPKEY_ID] = uidGen.GetUID();
448 m_ServiceProperties[US_PROPKEY_DEVICENAME] = m_UltrasoundDevice->GetName();
451 m_ServiceRegistration = context->RegisterService(
this, m_ServiceProperties);
456 if (m_UltrasoundDevice.IsNull())
458 MITK_ERROR(
"USCombinedModality")(
"USDevice") <<
"UltrasoundDevice must not be null.";
459 mitkThrow() <<
"UltrasoundDevice must not be null.";
462 return m_UltrasoundDevice->GetControlInterfaceCustom();
467 if (m_UltrasoundDevice.IsNull())
469 MITK_ERROR(
"USCombinedModality")(
"USDevice") <<
"UltrasoundDevice must not be null.";
470 mitkThrow() <<
"UltrasoundDevice must not be null.";
473 return m_UltrasoundDevice->GetControlInterfaceBMode();
478 if (m_UltrasoundDevice.IsNull())
480 MITK_ERROR(
"USCombinedModality")(
"USDevice") <<
"UltrasoundDevice must not be null.";
481 mitkThrow() <<
"UltrasoundDevice must not be null.";
484 return m_UltrasoundDevice->GetControlInterfaceProbes();
489 if (m_UltrasoundDevice.IsNull())
491 MITK_ERROR(
"USCombinedModality")(
"USDevice") <<
"UltrasoundDevice must not be null.";
492 mitkThrow() <<
"UltrasoundDevice must not be null.";
495 return m_UltrasoundDevice->GetControlInterfaceDoppler();
itk::SmartPointer< mitk::NavigationDataSource > GetNavigationDataSource()
itk::SmartPointer< mitk::NavigationDataDisplacementFilter > m_DisplacementFilter
static const char * ProbeAndDepthSeperator
static const std::string US_PROPKEY_CLASS
static const std::string DeviceClassIdentifier
USImageSource::Pointer GetUSImageSource()
Wrapper for returning USImageSource of the UltrasoundDevice.
std::string GetIdentifierForCurrentProbe()
virtual itk::SmartPointer< USAbstractControlInterface > GetControlInterfaceCustom()
Wrapper for returning custom control interface of the UltrasoundDevice.
virtual void SetIsFreezed(bool freeze)
Can toggle if the combined modality is currently updated or freezed.
void UnregisterOnService()
Remove this device from the micro service. This method is public for mitk::USCombinedModality,...
void DeserializeCalibration(const std::string &xmlString, bool clearPreviousCalibrations=true)
Deserializes a string provided by a prior call to Serialize(). If the bool flag is true,...
bool GetIsCalibratedForCurrentStatus()
std::string GetIdentifierForCurrentCalibration()
std::string GetCurrentDepthValue()
~AbstractUltrasoundTrackerDevice() override
void GenerateData() override
Grabs the next frame from the input. Must be implemented by the derived class. This method is called ...
static const char * DefaultProbeIdentifier
void RebuildFilterPipeline()
virtual AffineTransform3D::Pointer GetUSPlaneTransform()
void SetCalibration(AffineTransform3D::Pointer calibration)
Sets a transformation as calibration data. Calibration data is set for the currently activated probe ...
virtual itk::SmartPointer< USControlInterfaceDoppler > GetControlInterfaceDoppler()
Wrapper for returning doppler control interface of the UltrasoundDevice.
AffineTransform3D::Pointer GetCalibration()
Getter for calibration data of the currently active depth and probe.
virtual bool GetIsFreezed()
AbstractUltrasoundTrackerDevice(USDevice::Pointer usDevice, itk::SmartPointer< NavigationDataSource > trackingDevice, bool trackedUltrasoundActive)
void SetDelayCount(unsigned int delayCount)
static const std::string US_PROPKEY_ID
std::string GetDeviceClass()
Returns the Class of the Device.
virtual void RegisterAsMicroservice()
void SetNumberOfSmoothingValues(unsigned int numberOfSmoothingValues)
static const std::string US_INTERFACE_NAME
virtual itk::SmartPointer< USControlInterfaceBMode > GetControlInterfaceBMode()
Wrapper for returning B mode control interface of the UltrasoundDevice.
static const std::string US_PROPKEY_DEVICENAME
bool GetContainsAtLeastOneCalibration()
virtual itk::SmartPointer< USControlInterfaceProbes > GetControlInterfaceProbes()
Wrapper for returning probes control interface of the UltrasoundDevice.
std::string SerializeCalibration()
Serializes all contained calibrations into an xml fragment.
bool RemoveCalibration()
Removes the calibration data of the currently active depth and probe.
NavigationDataDelayFilter.
NavigationDataDisplacementFilter adds an offset to navigation data objects.
This filter smoothes the navigation data by calculating the mean value of the last few input values a...
static mitk::USDevice::PropertyKeys GetPropertyKeys()