7 #include "PlusConfigure.h" 12 #include "igsioTrackedFrame.h" 13 #include "vtkIGSIOAccurateTimer.h" 16 #include "vtkMatrix4x4.h" 17 #include "vtkObjectFactory.h" 20 #include "vtkIGSIOTrackedFrameList.h" 21 #include "vtkTransform.h" 22 #include "vtkXMLDataElement.h" 23 #include "vtksys/SystemTools.hxx" 44 vtkSmartPointer<vtkPlusDataSource> probeTool = vtkSmartPointer<vtkPlusDataSource>::New();
45 probeTool->SetId(
"Probe");
46 std::ostringstream probePortName;
48 probeTool->SetPortName(probePortName.str().c_str());
51 vtkSmartPointer<vtkPlusDataSource> templateTool = vtkSmartPointer<vtkPlusDataSource>::New();
52 templateTool->SetId(
"Template");
53 std::ostringstream templatePortName;
55 templateTool->SetPortName(templatePortName.str().c_str());
58 vtkSmartPointer<vtkPlusDataSource> encoderTool = vtkSmartPointer<vtkPlusDataSource>::New();
59 encoderTool->SetId(
"StepperEncoderValues");
60 std::ostringstream encoderPortName;
62 encoderTool->SetPortName(encoderPortName.str().c_str());
114 LOG_ERROR(
"Failed to connect to brachy tracker - BrachyStepperType not selected, device is NULL!");
138 LOG_ERROR(
"Unable to connect to stepper on port: " << this->
GetSerialPort());
157 LOG_ERROR(
"Couldn't initialize brachy stepper.");
173 std::ostringstream toolPortName;
174 toolPortName << tool;
178 LOG_ERROR(
"Failed to get tool source ID by port: " << toolPortName.str());
181 std::string sourceId = trackerTool->GetId();
188 ToolStatus status = TOOL_OK;
192 LOG_ERROR(
"called Update() when Brachy stepper was not tracking");
197 double dProbePosition(0), dTemplatePosition(0), dProbeRotation(0);
198 unsigned long frameNum(0);
201 LOG_DEBUG(
"Tracker request timeout...");
203 status = TOOL_REQ_TIMEOUT;
205 LOG_TRACE(
"Encoder values: " 206 <<
"(Probe position) " << dProbePosition <<
", " 207 <<
"(Probe rotation) " << dProbeRotation <<
", " 208 <<
"(Template position) " << dTemplatePosition <<
", " 209 <<
"(Frame number) " << frameNum);
211 const double unfilteredTimestamp = vtkIGSIOAccurateTimer::GetSystemTime();
216 vtkSmartPointer<vtkMatrix4x4> probePosition = vtkSmartPointer<vtkMatrix4x4>::New();
232 vtkSmartPointer<vtkTransform> tTemplateHomeToTemplate = vtkSmartPointer<vtkTransform>::New();
233 tTemplateHomeToTemplate->Translate(0, 0, dTemplatePosition);
241 vtkSmartPointer<vtkTransform> tProbeHomeToProbe = vtkSmartPointer<vtkTransform>::New();
242 tProbeHomeToProbe->Translate(0, 0, dProbePosition);
243 tProbeHomeToProbe->RotateZ(dProbeRotation);
253 vtkSmartPointer<vtkTransform> tTemplateHomeToTemplate = vtkSmartPointer<vtkTransform>::New();
254 double templateTranslationAxisVector[3];
256 vtkMath::MultiplyScalar(templateTranslationAxisVector, dTemplatePosition);
257 tTemplateHomeToTemplate->Translate(templateTranslationAxisVector);
266 vtkSmartPointer<vtkTransform> tProbeHomeToProbe = vtkSmartPointer<vtkTransform>::New();
268 double probeTranslationVector[3];
270 vtkMath::MultiplyScalar(probeTranslationVector, dProbePosition);
271 tProbeHomeToProbe->Translate(probeTranslationVector);
274 double probeRotationVector[3];
276 vtkMath::MultiplyScalar(probeRotationVector, dProbePosition);
277 tProbeHomeToProbe->Translate(probeRotationVector);
279 tProbeHomeToProbe->RotateZ(compensatedProbeRotation);
281 tProbeHomeToProbe->Translate(-probeRotationVector[0], -probeRotationVector[1], -probeRotationVector[2]);
298 LOG_ERROR(
"Unable to connect to stepper on port: " << this->
GetSerialPort());
307 LOG_ERROR(
"Couldn't get version info from stepper.");
323 XML_READ_SCALAR_ATTRIBUTE_OPTIONAL(
unsigned long,
SerialPort, deviceConfig);
324 XML_READ_SCALAR_ATTRIBUTE_OPTIONAL(
unsigned long,
BaudRate, deviceConfig);
328 const char* brachyStepperType = deviceConfig->GetAttribute(
"BrachyStepperType");
329 if (brachyStepperType == NULL)
331 LOG_ERROR(
"Unable to find BrachyStepperType attribute in configuration file");
368 LOG_ERROR(
"Unable to recognize brachy stepper type: " << brachyStepperType);
372 XML_READ_CSTRING_ATTRIBUTE_OPTIONAL(
ModelNumber, deviceConfig);
373 XML_READ_CSTRING_ATTRIBUTE_OPTIONAL(
ModelVersion, deviceConfig);
377 vtkXMLDataElement* calibration = deviceConfig->FindNestedElementWithName(
"StepperCalibrationResult");
378 if (calibration != NULL)
380 const char* calibrationAlgorithmVersion = calibration->GetAttribute(
"AlgorithmVersion");
381 if (calibrationAlgorithmVersion != NULL)
387 LOG_WARNING(
"Failed to read stepper calibration algorithm version from config file!");
391 const char* calibrationDate = calibration->GetAttribute(
"Date");
392 if (calibrationDate != NULL)
398 LOG_WARNING(
"Failed to read stepper calibration date from config file!");
421 trackerConfig->SetAttribute(
"BrachyStepperType", strStepperType.c_str());
424 trackerConfig->SetUnsignedLongAttribute(
"SerialPort", this->
GetSerialPort());
425 trackerConfig->SetDoubleAttribute(
"BaudRate", this->
GetBaudRate());
427 trackerConfig->SetAttribute(
"ModelNumber", this->
GetModelNumber());
431 vtkXMLDataElement* calibration = trackerConfig->FindNestedElementWithName(
"StepperCalibrationResult");
432 if (calibration == NULL)
435 vtkSmartPointer<vtkXMLDataElement> newCalibration = vtkSmartPointer<vtkXMLDataElement>::New();
436 newCalibration->SetName(
"StepperCalibrationResult");
437 newCalibration->SetParent(trackerConfig);
438 trackerConfig->AddNestedElement(newCalibration);
439 calibration = newCalibration;
474 ToolStatus probehome2probeStatus = TOOL_OK;
475 vtkSmartPointer<vtkMatrix4x4> probehome2probeMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
478 LOG_ERROR(
"Failed to get probe home to probe transform from buffer!");
483 if (!probeToReferenceTransformName.IsValid())
485 LOG_ERROR(
"Invalid probe to reference tranform name!");
489 if (aTrackedFrame->SetFrameTransform(probeToReferenceTransformName, probehome2probeMatrix) !=
PLUS_SUCCESS)
496 if (aTrackedFrame->SetFrameTransformStatus(probeToReferenceTransformName, probehome2probeStatus) !=
PLUS_SUCCESS)
504 ToolStatus templhome2templStatus = TOOL_OK;
505 vtkSmartPointer<vtkMatrix4x4> templhome2templMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
508 LOG_ERROR(
"Failed to get template home to template transform from buffer!");
513 if (!templateToReferenceTransformName.IsValid())
515 LOG_ERROR(
"Invalid template to reference tranform name!");
519 if (aTrackedFrame->SetFrameTransform(templateToReferenceTransformName, templhome2templMatrix) !=
PLUS_SUCCESS)
525 if (aTrackedFrame->SetFrameTransformStatus(templateToReferenceTransformName, templhome2templStatus) !=
PLUS_SUCCESS)
533 ToolStatus rawEncoderValuesStatus = TOOL_OK;
534 vtkSmartPointer<vtkMatrix4x4> rawEncoderValuesMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
537 LOG_ERROR(
"Failed to get raw encoder values from buffer!");
542 if (!encoderToReferenceTransformName.IsValid())
544 LOG_ERROR(
"Invalid encoder to reference tranform name!");
548 if (aTrackedFrame->SetFrameTransform(encoderToReferenceTransformName, rawEncoderValuesMatrix) !=
PLUS_SUCCESS)
554 if (aTrackedFrame->SetFrameTransformStatus(encoderToReferenceTransformName, rawEncoderValuesStatus) !=
PLUS_SUCCESS)
561 ToolStatus encoderStatus = TOOL_OK;
562 double probePos(0), probeRot(0), templatePos(0);
565 LOG_ERROR(
"Failed to get stepper encoder values!");
570 std::ostringstream strProbePos;
571 strProbePos << probePos;
572 aTrackedFrame->SetFrameField(
"ProbePosition", strProbePos.str());
575 std::ostringstream strProbeRot;
576 strProbeRot << probeRot;
577 aTrackedFrame->SetFrameField(
"ProbeRotation", strProbeRot.str());
580 std::ostringstream strTemplatePos;
581 strTemplatePos << templatePos;
582 aTrackedFrame->SetFrameField(
"TemplatePosition", strTemplatePos.str());
591 if (encoderToolSourceId.empty())
593 LOG_ERROR(
"Failed to get encoder values tool name!");
600 LOG_ERROR(
"Failed to get tool: " << encoderToolSourceId);
606 LOG_DEBUG(
"The buffer is empty");
609 templatePosition = 0.0;
610 status = TOOL_MISSING;
622 if (encoderToolSourceId.empty())
624 LOG_ERROR(
"Failed to get encoder values tool name!");
631 LOG_ERROR(
"Failed to get tool: " << encoderToolSourceId);
638 LOG_ERROR(
"Failed to get stepper encoder values from buffer by UID: " << uid);
642 vtkSmartPointer<vtkMatrix4x4> mx = vtkSmartPointer<vtkMatrix4x4>::New();
645 LOG_ERROR(
"Failed to get bufferitem matrix by UID: " << uid);
661 if (encoderToolSourceId.empty())
663 LOG_ERROR(
"Failed to get encoder values tool name!");
670 LOG_ERROR(
"Failed to get tool: " << encoderToolSourceId);
677 LOG_ERROR(
"Failed to get stepper encoder values from buffer by time: " << std::fixed <<
timestamp);
687 if (probeHomeToProbeMatrix == NULL)
689 LOG_ERROR(
"Failed to get probe home to probe transform - input transform is NULL!");
694 if (probeToolSourceId.empty())
696 LOG_ERROR(
"Failed to get probe tool name!");
703 LOG_ERROR(
"Failed to get tool: " << probeToolSourceId);
710 LOG_ERROR(
"Failed to get probe home to probe transform by UID: " << uid);
717 LOG_ERROR(
"Failed to get probeHomeToProbeMatrix");
728 if (probeToolSourceId.empty())
730 LOG_ERROR(
"Failed to get probe tool name!");
737 LOG_ERROR(
"Failed to get tool: " << probeToolSourceId);
744 LOG_ERROR(
"Failed to get probe home to probe transform by timestamp: " << std::fixed <<
timestamp);
754 if (templateHomeToTemplateMatrix == NULL)
756 LOG_ERROR(
"Failed to get template home to template transform - input transform is NULL!");
761 if (templateToolSourceId.empty())
763 LOG_ERROR(
"Failed to get template tool name!");
770 LOG_ERROR(
"Failed to get tool: " << templateToolSourceId);
777 LOG_ERROR(
"Failed to get template home to template transform by UID: " << uid);
784 LOG_ERROR(
"Failed to get templateHomeToTemplateMatrix");
795 if (templateToolSourceId.empty())
797 LOG_ERROR(
"Failed to get template tool name!");
804 LOG_ERROR(
"Failed to get tool: " << templateToolSourceId);
811 LOG_ERROR(
"Failed to get template home to template transform by timestamp: " << std::fixed <<
timestamp);
821 if (rawEncoderValuesTransform == NULL)
823 LOG_ERROR(
"Failed to get raw encoder values transform from buffer - input transform NULL!");
828 if (encoderToolSourceId.empty())
830 LOG_ERROR(
"Failed to get encoder values tool name!");
837 LOG_ERROR(
"Failed to get tool: " << encoderToolSourceId);
844 LOG_ERROR(
"Failed to get raw encoder values transform from buffer by UID: " << uid);
850 LOG_ERROR(
"Failed to get rawEncoderValuesTransform");
863 if (encoderToolSourceId.empty())
865 LOG_ERROR(
"Failed to get encoder values tool name!");
872 LOG_ERROR(
"Failed to get tool: " << encoderToolSourceId);
879 LOG_ERROR(
"Failed to get raw encoder values transform by timestamp: " << std::fixed <<
timestamp);
891 LOG_WARNING(
"vtkPlusBrachyTracker is expecting one output channel and there are " << this->
OutputChannels.size() <<
" channels. First output channel will be used.");
897 LOG_ERROR(
"No output channels defined for vtkPlusBrachyTracker. Cannot proceed.");
904 outputChannel->
Clear();
907 outputChannel->
AddTool(it->second);
virtual BufferItemUidType GetLatestItemUidInBuffer()
PlusStatus GetLatestStepperEncoderValues(double &probePosition, double &probeRotation, double &templatePosition, ToolStatus &status)
std::string ToolReferenceFrameName
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
ToolStatus GetStatus() const
PlusStatus InitializeStepper(std::string &calibMsg)
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *config)
PlusBrachyStepper::BRACHY_STEPPER_TYPE BrachyStepperType
virtual char * GetCalibrationDate()
virtual char * GetModelSerialNumber()
virtual ItemStatus GetStreamBufferItem(BufferItemUidType uid, StreamBufferItem *bufferItem)
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_WRITING(deviceConfig, rootConfigElement)
double ProbeRotationAxisOrientation[3]
virtual void SetProbeRotationEncoderScale(double)
virtual void CompensationEnabledOn()
virtual PlusStatus InternalDisconnect()
virtual unsigned long GetBaudRate()
PlusStatus InitBrachyTracker()
vtkStandardNewMacro(vtkPlusBrachyTracker)
PlusStatus AddTool(vtkPlusDataSource *tool, bool requireUniquePortName=true)
virtual PlusStatus ToolTimeStampedUpdate(const std::string &aToolSourceId, vtkMatrix4x4 *matrix, ToolStatus status, unsigned long frameNumber, double unfilteredtimestamp, const igsioFieldMapType *customFields=NULL)
bool RequirePortNameInDeviceSetConfiguration
virtual PlusStatus Clear()
virtual double GetProbeRotationEncoderScale()
virtual char * GetModelVersion()
BRACHY_STEPPER_TYPE GetBrachyStepperType() const
PlusStatus AddTool(vtkPlusDataSource *aTool)
DataSourceContainer Tools
virtual PlusStatus NotifyConfigured()
virtual void PrintSelf(ostream &os, vtkIndent indent) VTK_OVERRIDE
virtual void SetProbeRotationAxisOrientation(double, double, double)
PlusStatus GetTemplateHomeToTemplateTransform(BufferItemUidType uid, vtkMatrix4x4 *templateHomeToTemplateMatrix, ToolStatus &status)
double TemplateTranslationAxisOrientation[3]
virtual PlusStatus Disconnect()
virtual void SetCorrectlyConfigured(bool)
static std::string GetBrachyStepperTypeInString(BRACHY_STEPPER_TYPE stepperType)
virtual void SetCalibrationAlgorithmVersion(const char *)
PlusStatus InternalUpdate()
Interface to brachytherapy steppers.
PlusStatus GetToolByPortName(const char *aPortName, vtkPlusDataSource *&aSource)
double ProbeRotationEncoderScale
virtual PlusStatus Connect()
virtual void SetModelNumber(const char *)
PlusStatus InternalStartRecording()
PlusStatus InternalStopRecording()
virtual PlusStatus StopRecording()
PlusStatus GetMatrix(vtkMatrix4x4 *outputMatrix)
PlusStatus GetStepperEncoderValues(BufferItemUidType uid, double &probePosition, double &probeRotation, double &templatePosition, ToolStatus &status)
std::string GetBrachyToolSourceId(BRACHY_STEPPER_TOOL tool)
#define XML_FIND_DEVICE_ELEMENT_REQUIRED_FOR_READING(deviceConfig, rootConfigElement)
virtual void SetTemplateTranslationAxisOrientation(double, double, double)
DataSourceContainer::iterator DataSourceContainerIterator
virtual PlusStatus ResetStepper()=0
virtual PlusStatus GetTrackedFrame(double timestamp, igsioTrackedFrame *aTrackedFrame)
double ProbeTranslationAxisOrientation[3]
char * CalibrationAlgorithmVersion
virtual bool IsRecording() const
PlusStatus GetProbeHomeToProbeTransform(BufferItemUidType uid, vtkMatrix4x4 *probeHomeToProbeMatrix, ToolStatus &status)
virtual char * GetCalibrationAlgorithmVersion()
bool StartThreadForInternalUpdates
virtual PlusStatus Disconnect()=0
virtual char * GetModelNumber()
virtual double * GetProbeRotationAxisOrientation()
virtual PlusStatus GetDeviceModelInfo(std::string &version, std::string &model, std::string &serial)=0
virtual double * GetProbeTranslationAxisOrientation()
Contains an optional timestamped circular buffer containing the video images and a number of timestam...
virtual unsigned long GetSerialPort()
virtual PlusStatus InitializeStepper(std::string &CalibMsg)=0
PlusStatus ResetStepper()
ChannelContainer OutputChannels
virtual void SetCalibrationDate(const char *)
virtual void SetModelVersion(const char *)
PlusStatus InternalConnect()
virtual double * GetTemplateTranslationAxisOrientation()
virtual ItemStatus GetItemUidFromTime(double time, BufferItemUidType &uid)
void SetToolReferenceFrameName(const std::string &frameName)
PlusBrachyStepper * Device
virtual PlusStatus Connect()=0
PlusStatus GetTool(const char *aToolSourceId, vtkPlusDataSource *&aTool) const
unsigned long long BufferItemUidType
virtual void SetModelSerialNumber(const char *)
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *config)
virtual void SetSerialPort(unsigned long)
virtual void SetProbeTranslationAxisOrientation(double, double, double)
void SetBrachyStepperType(BRACHY_STEPPER_TYPE type)
virtual int GetNumberOfItems()
PlusStatus GetRawEncoderValuesTransform(BufferItemUidType uid, vtkMatrix4x4 *rawEncoderValuesTransform, ToolStatus &status)
virtual PlusStatus GetEncoderValues(double &PPosition, double &GPosition, double &RPosition, unsigned long &PositionRequestNumber)=0
Interface to a 3D positioning tool, video source, or generalized data stream.