15 #include <QStringList>
16 #include <QTextStream>
17 #include <vtkTransform.h>
29 igstk::Transform retval;
30 retval.ImportTransform(*
transform.getVtkMatrix());
38 transform.ExportTransform(*(matrix.GetPointer()));
39 retval = Transform3D::fromVtkMatrix(matrix);
46 mInternalStructure->mCalibration = cal;
49 Transform3D sMt = mInternalStructure->getCalibrationAsSSC();
50 report(
"Set " + mInternalStructure->mName +
"s calibration to \n" +
qstring_cast(sMt));
53 mInternalStructure->saveCalibrationToFile();
61 mToolObserver(
itk::ReceptorMemberCommand<
IgstkTool>::New()), mValid(false), mVisible(false), mAttachedToTracker(
64 qRegisterMetaType<Transform3D>(
"Transform3D");
65 qRegisterMetaType<ToolPositionMetadata>(
"ToolPositionMetadata");
67 mLatestEmittedTimestamp = 0;
69 mInternalStructure = internalStructure;
71 mToolObserver->SetCallbackFunction(
this, &IgstkTool::toolTransformCallback);
73 if (mInternalStructure->verify())
75 mTool = this->buildInternalTool();
81 reportError(mInternalStructure->mUid +
" was created with invalid internal structure.");
92 return mInternalStructure;
97 return mInternalStructure->mUid;
107 return mInternalStructure->mTrackerType;
117 return mAttachedToTracker;
132 mReferenceTool = refTool;
140 igstk::NDITracker::TrackingSampleInfo IgstkTool::getSampleInfo()
142 igstk::NDITracker::TrackingSampleInfo retval;
143 retval.m_FrameNumber = 0;
148 igstk::NDITracker* ndiTracker =
dynamic_cast<igstk::NDITracker*
>(tracker->getPointer());
153 std::map<std::string,igstk::NDITracker::TrackingSampleInfo> info = ndiTracker->GetTrackingSampleInfo();
154 return info[mTool->GetTrackerToolIdentifier()];
157 bool IgstkTool::validReferenceForResult(igstk::CoordinateSystemTransformToResult result)
159 const igstk::CoordinateSystem* destination = result.GetDestination();
164 if (!refTool->getPointer()->IsCoordinateSystem(destination))
170 if (!tracker || !tracker->getPointer()->IsCoordinateSystem(destination))
177 Transform3D IgstkTool::igstk2Transform3D(
const igstk::Transform& input)
const
180 input.ExportTransform(*vtkMatrix.GetPointer());
184 void IgstkTool::processReceivedTransformResult(igstk::CoordinateSystemTransformToResult result)
186 if (!this->validReferenceForResult(result))
191 igstk::NDITracker::TrackingSampleInfo sampleInfo = this->getSampleInfo();
194 if (
similar(mLatestEmittedTimestamp, sampleInfo.m_TimeStamp,1.0E-3))
198 mLatestEmittedTimestamp = sampleInfo.m_TimeStamp;
201 QDomElement root = doc.createElement(
"info");
202 doc.appendChild(root);
204 ToolPositionMetadata metadata;
205 metadata.mData = doc.toString();
207 igstk::Transform
transform = result.GetTransform();
209 double timestamp =
transform.GetStartTime();
214 void IgstkTool::toolTransformCallback(
const itk::EventObject &event)
216 if (igstk::CoordinateSystemTransformToEvent().CheckEvent(&event))
218 const igstk::CoordinateSystemTransformToEvent *transformEvent;
219 transformEvent =
dynamic_cast<const igstk::CoordinateSystemTransformToEvent*
>(&event);
222 this->processReceivedTransformResult(transformEvent->Get());
225 else if (igstk::TrackerToolConfigurationEvent().CheckEvent(&event))
227 report(QString(
"Configured [%1] with the tracking system").arg(mInternalStructure->mUid));
229 else if (igstk::TrackerToolAttachmentToTrackerEvent().CheckEvent(&event))
231 this->internalAttachedToTracker(
true);
233 else if (igstk::TrackerToolDetachmentFromTrackerEvent().CheckEvent(&event))
235 this->internalAttachedToTracker(
false);
237 else if (igstk::TrackerToolMadeTransitionToTrackedStateEvent().CheckEvent(&event))
239 this->internalVisible(
true);
241 else if (igstk::TrackerToolNotAvailableToBeTrackedEvent().CheckEvent(&event))
243 this->internalVisible(
false);
245 else if (igstk::ToolTrackingStartedEvent().CheckEvent(&event))
247 this->internalTracked(
true);
248 report(mInternalStructure->mUid +
" is tracked.");
250 else if (igstk::ToolTrackingStoppedEvent().CheckEvent(&event))
252 this->internalTracked(
false);
253 report(mInternalStructure->mUid +
" is not tracked anymore.");
256 else if (igstk::InvalidRequestErrorEvent().CheckEvent(&event))
259 mInternalStructure->mUid
260 +
" received an invalid request. This means that the internal igstk trackertool did not accept the request. Do not know which request.");
262 else if (igstk::TrackerToolConfigurationErrorEvent().CheckEvent(&event))
264 reportError(mInternalStructure->mUid +
" could not configure with the tracking system.");
266 else if (igstk::InvalidRequestToAttachTrackerToolErrorEvent().CheckEvent(&event))
268 reportError(mInternalStructure->mUid +
" could not request to attach to tracker.");
270 else if (igstk::InvalidRequestToDetachTrackerToolErrorEvent().CheckEvent(&event))
272 reportError(mInternalStructure->mUid +
" could not request to detach from tracker.");
274 else if (igstk::TrackerToolAttachmentToTrackerErrorEvent().CheckEvent(&event))
276 reportError(mInternalStructure->mUid +
" could not attach to tracker.");
278 else if (igstk::TrackerToolDetachmentFromTrackerErrorEvent().CheckEvent(&event))
280 reportError(mInternalStructure->mUid +
" could not detach from tracker.");
283 else if (igstk::InvalidPolarisPortNumberErrorEvent().CheckEvent(&event))
286 mInternalStructure->mUid +
" sendt invalid Polaris port number: "
289 else if (igstk::InvalidPolarisSROMFilenameErrorEvent().CheckEvent(&event))
292 mInternalStructure->mUid +
" sendt invalid ROM file: " + mInternalStructure->mSROMFilename);
294 else if (igstk::InvalidPolarisPartNumberErrorEvent().CheckEvent(&event))
296 reportError(mInternalStructure->mUid +
" has an invalid part number.");
299 else if (igstk::InvalidAuroraPortNumberErrorEvent().CheckEvent(&event))
302 mInternalStructure->mUid +
" has an invalid port number: "
305 else if (igstk::InvalidAuroraSROMFilenameErrorEvent().CheckEvent(&event))
308 mInternalStructure->mUid +
" sendt invalid ROM file: " + mInternalStructure->mSROMFilename);
310 else if (igstk::InvalidAuroraPartNumberErrorEvent().CheckEvent(&event))
312 reportError(mInternalStructure->mUid +
" has an invalid part number.");
314 else if (igstk::InvalidAuroraChannelNumberErrorEvent().CheckEvent(&event))
317 mInternalStructure->mUid +
" has an invalid channel number:"
318 +
qstring_cast(mInternalStructure->mChannelNumber) +
".");
322 igstk::TrackerTool::Pointer IgstkTool::buildInternalTool()
324 igstk::TrackerTool::Pointer tool;
326 igstk::PolarisTrackerTool::Pointer tempPolarisTool;
327 igstk::AuroraTrackerTool::Pointer tempAuroraTool;
329 igstk::Transform calibration =
toIgstkTransform(mInternalStructure->mCalibration);
330 switch (mInternalStructure->mTrackerType)
337 tempPolarisTool = igstk::PolarisTrackerTool::New();
338 tempPolarisTool->AddObserver(igstk::IGSTKEvent(), mToolObserver);
339 if (!mInternalStructure->mWireless)
340 return tool = tempPolarisTool.GetPointer();
341 tempPolarisTool->RequestSelectWirelessTrackerTool();
342 tempPolarisTool->RequestSetSROMFileName(
string_cast(mInternalStructure->mSROMFilename));
343 tempPolarisTool->RequestConfigure();
344 tempPolarisTool->SetCalibrationTransform(calibration);
345 tool = tempPolarisTool;
348 tempAuroraTool = igstk::AuroraTrackerTool::New();
349 tempAuroraTool->AddObserver(igstk::IGSTKEvent(), mToolObserver);
350 if (mInternalStructure->m5DOF)
352 tempAuroraTool->RequestSelect5DOFTrackerTool();
353 tempAuroraTool->RequestSetPortNumber(mInternalStructure->mPortNumber);
354 tempAuroraTool->RequestSetChannelNumber(mInternalStructure->mChannelNumber);
358 tempAuroraTool->RequestSelect6DOFTrackerTool();
359 tempAuroraTool->RequestSetPortNumber(mInternalStructure->mPortNumber);
361 tempAuroraTool->RequestConfigure();
362 tempAuroraTool->SetCalibrationTransform(calibration);
363 tool = tempAuroraTool;
376 mInternalStructure->mCalibration = calibration;
378 mTool->SetCalibrationTransform(
transform);
381 void IgstkTool::internalAttachedToTracker(
bool value)
383 if (mAttachedToTracker == value)
385 mAttachedToTracker = value;
387 mInternalStructure->mUid +
" is " + (value ?
"at" :
"de") +
"tached " + (value ?
"to" :
"from")
392 void IgstkTool::internalTracked(
bool value)
394 if (mTracked == value)
398 if (!mTracked && mVisible)
399 this->internalVisible(
false);
404 void IgstkTool::internalVisible(
bool value)
406 if (mVisible == value)
412 void IgstkTool::addLogging()
414 bool logging =
settings()->
value(
"IGSTKDebugLogging",
true).toBool();
417 std::ofstream* loggerFile =
new std::ofstream();
418 QString logFile = mInternalStructure->mLoggingFolderName +
"Tool_" + mInternalStructure->mName +
"_Logging.txt";
420 mLogger = igstk::Logger::New();
421 mLogOutput = itk::StdStreamLogOutput::New();
422 mLogOutput->SetStream(*loggerFile);
423 mLogger->AddLogOutput(mLogOutput);
424 mLogger->SetPriorityLevel(itk::Logger::DEBUG);
426 mTool->SetLogger(mLogger);
432 std::cout <<
"------------------------------------------------------------------" << std::endl;
433 std::cout <<
"mIsProbe: " << mInternalStructure->mIsProbe << std::endl;
434 std::cout <<
"mIsReference: " << mInternalStructure->mIsReference << std::endl;
435 std::cout <<
"mIsPointer: " << mInternalStructure->mIsPointer << std::endl;
436 std::cout <<
"mName: " << mInternalStructure->mName << std::endl;
437 std::cout <<
"mUid: " << mInternalStructure->mUid << std::endl;
438 std::cout <<
"mTrackerType: " << mInternalStructure->mTrackerType << std::endl;
439 std::cout <<
"mSROMFilename: " << mInternalStructure->mSROMFilename << std::endl;
440 std::cout <<
"mPortNumber: " << mInternalStructure->mPortNumber << std::endl;
441 std::cout <<
"mChannelNumber: " << mInternalStructure->mChannelNumber << std::endl;
442 std::cout <<
"mReferencePoints: " <<
string_cast(mInternalStructure->mReferencePoints.size()) << std::endl;
443 std::cout <<
"mWireless: " << mInternalStructure->mWireless << std::endl;
444 std::cout <<
"m5DOF: " << mInternalStructure->m5DOF << std::endl;
445 std::cout <<
"mCalibration: " << mInternalStructure->mCalibration << std::endl;
447 std::cout <<
"mCalibrationFilename: " << mInternalStructure->mCalibrationFilename << std::endl;
448 std::cout <<
"mGraphicsFileName: " << mInternalStructure->mGraphicsFileName << std::endl;
449 std::cout <<
"mTransformSaveFileName: " << mInternalStructure->mTransformSaveFileName << std::endl;
450 std::cout <<
"mLoggingFolderName: " << mInternalStructure->mLoggingFolderName << std::endl;
451 std::cout <<
"------------------------------------------------------------------" << std::endl;