36 #include <QStringList>
37 #include <QTextStream>
38 #include <vtkTransform.h>
50 igstk::Transform retval;
51 retval.ImportTransform(*transform.getVtkMatrix());
59 transform.ExportTransform(*(matrix.GetPointer()));
60 retval = Transform3D::fromVtkMatrix(matrix);
82 mToolObserver(itk::ReceptorMemberCommand<
IgstkTool>::New()), mValid(false), mVisible(false), mAttachedToTracker(
85 qRegisterMetaType<Transform3D>(
"Transform3D");
86 qRegisterMetaType<ToolPositionMetadata>(
"ToolPositionMetadata");
88 mLatestEmittedTimestamp = 0;
90 mInternalStructure = internalStructure;
92 mToolObserver->SetCallbackFunction(
this, &IgstkTool::toolTransformCallback);
94 if (mInternalStructure.
verify())
96 mTool = this->buildInternalTool();
102 reportError(mInternalStructure.
mUid +
" was created with invalid internal structure.");
113 return mInternalStructure;
118 return mInternalStructure.
mUid;
138 return mAttachedToTracker;
153 mReferenceTool = refTool;
161 igstk::NDITracker::TrackingSampleInfo IgstkTool::getSampleInfo()
163 igstk::NDITracker::TrackingSampleInfo retval;
164 retval.m_FrameNumber = 0;
169 igstk::NDITracker* ndiTracker =
dynamic_cast<igstk::NDITracker*
>(tracker->getPointer());
174 std::map<std::string,igstk::NDITracker::TrackingSampleInfo> info = ndiTracker->GetTrackingSampleInfo();
175 return info[mTool->GetTrackerToolIdentifier()];
178 bool IgstkTool::validReferenceForResult(igstk::CoordinateSystemTransformToResult result)
180 const igstk::CoordinateSystem* destination = result.GetDestination();
185 if (!refTool->getPointer()->IsCoordinateSystem(destination))
191 if (!tracker || !tracker->getPointer()->IsCoordinateSystem(destination))
198 Transform3D IgstkTool::igstk2Transform3D(
const igstk::Transform& input)
const
201 input.ExportTransform(*vtkMatrix.GetPointer());
205 void IgstkTool::processReceivedTransformResult(igstk::CoordinateSystemTransformToResult result)
207 if (!this->validReferenceForResult(result))
212 igstk::NDITracker::TrackingSampleInfo sampleInfo = this->getSampleInfo();
215 if (
similar(mLatestEmittedTimestamp, sampleInfo.m_TimeStamp,1.0E-3))
219 mLatestEmittedTimestamp = sampleInfo.m_TimeStamp;
222 QDomElement root = doc.createElement(
"info");
223 doc.appendChild(root);
225 ToolPositionMetadata metadata;
226 metadata.mData = doc.toString();
228 igstk::Transform
transform = result.GetTransform();
230 double timestamp = transform.GetStartTime();
235 void IgstkTool::toolTransformCallback(
const itk::EventObject &event)
237 if (igstk::CoordinateSystemTransformToEvent().CheckEvent(&event))
239 const igstk::CoordinateSystemTransformToEvent *transformEvent;
240 transformEvent =
dynamic_cast<const igstk::CoordinateSystemTransformToEvent*
>(&event);
243 this->processReceivedTransformResult(transformEvent->Get());
246 else if (igstk::TrackerToolConfigurationEvent().CheckEvent(&event))
248 report(QString(
"Configured [%1] with the tracking system").arg(mInternalStructure.
mUid));
250 else if (igstk::TrackerToolAttachmentToTrackerEvent().CheckEvent(&event))
252 this->internalAttachedToTracker(
true);
254 else if (igstk::TrackerToolDetachmentFromTrackerEvent().CheckEvent(&event))
256 this->internalAttachedToTracker(
false);
258 else if (igstk::TrackerToolMadeTransitionToTrackedStateEvent().CheckEvent(&event))
260 this->internalVisible(
true);
262 else if (igstk::TrackerToolNotAvailableToBeTrackedEvent().CheckEvent(&event))
264 this->internalVisible(
false);
266 else if (igstk::ToolTrackingStartedEvent().CheckEvent(&event))
268 this->internalTracked(
true);
269 report(mInternalStructure.
mUid +
" is tracked.");
271 else if (igstk::ToolTrackingStoppedEvent().CheckEvent(&event))
273 this->internalTracked(
false);
274 report(mInternalStructure.
mUid +
" is not tracked anymore.");
277 else if (igstk::InvalidRequestErrorEvent().CheckEvent(&event))
280 mInternalStructure.
mUid
281 +
" received an invalid request. This means that the internal igstk trackertool did not accept the request. Do not know which request.");
283 else if (igstk::TrackerToolConfigurationErrorEvent().CheckEvent(&event))
285 reportError(mInternalStructure.
mUid +
" could not configure with the tracking system.");
287 else if (igstk::InvalidRequestToAttachTrackerToolErrorEvent().CheckEvent(&event))
289 reportError(mInternalStructure.
mUid +
" could not request to attach to tracker.");
291 else if (igstk::InvalidRequestToDetachTrackerToolErrorEvent().CheckEvent(&event))
293 reportError(mInternalStructure.
mUid +
" could not request to detach from tracker.");
295 else if (igstk::TrackerToolAttachmentToTrackerErrorEvent().CheckEvent(&event))
297 reportError(mInternalStructure.
mUid +
" could not attach to tracker.");
299 else if (igstk::TrackerToolDetachmentFromTrackerErrorEvent().CheckEvent(&event))
301 reportError(mInternalStructure.
mUid +
" could not detach from tracker.");
304 else if (igstk::InvalidPolarisPortNumberErrorEvent().CheckEvent(&event))
307 mInternalStructure.
mUid +
" sendt invalid Polaris port number: "
310 else if (igstk::InvalidPolarisSROMFilenameErrorEvent().CheckEvent(&event))
313 mInternalStructure.
mUid +
" sendt invalid ROM file: " + mInternalStructure.
mSROMFilename);
315 else if (igstk::InvalidPolarisPartNumberErrorEvent().CheckEvent(&event))
320 else if (igstk::InvalidAuroraPortNumberErrorEvent().CheckEvent(&event))
323 mInternalStructure.
mUid +
" has an invalid port number: "
326 else if (igstk::InvalidAuroraSROMFilenameErrorEvent().CheckEvent(&event))
329 mInternalStructure.
mUid +
" sendt invalid ROM file: " + mInternalStructure.
mSROMFilename);
331 else if (igstk::InvalidAuroraPartNumberErrorEvent().CheckEvent(&event))
335 else if (igstk::InvalidAuroraChannelNumberErrorEvent().CheckEvent(&event))
338 mInternalStructure.
mUid +
" has an invalid channel number:"
343 igstk::TrackerTool::Pointer IgstkTool::buildInternalTool()
345 igstk::TrackerTool::Pointer tool;
347 igstk::PolarisTrackerTool::Pointer tempPolarisTool;
348 igstk::AuroraTrackerTool::Pointer tempAuroraTool;
358 tempPolarisTool = igstk::PolarisTrackerTool::New();
359 tempPolarisTool->AddObserver(igstk::IGSTKEvent(), mToolObserver);
361 return tool = tempPolarisTool.GetPointer();
362 tempPolarisTool->RequestSelectWirelessTrackerTool();
364 tempPolarisTool->RequestConfigure();
365 tempPolarisTool->SetCalibrationTransform(calibration);
366 tool = tempPolarisTool;
369 tempAuroraTool = igstk::AuroraTrackerTool::New();
370 tempAuroraTool->AddObserver(igstk::IGSTKEvent(), mToolObserver);
371 if (mInternalStructure.
m5DOF)
373 tempAuroraTool->RequestSelect5DOFTrackerTool();
374 tempAuroraTool->RequestSetPortNumber(mInternalStructure.
mPortNumber);
375 tempAuroraTool->RequestSetChannelNumber(mInternalStructure.
mChannelNumber);
379 tempAuroraTool->RequestSelect6DOFTrackerTool();
380 tempAuroraTool->RequestSetPortNumber(mInternalStructure.
mPortNumber);
382 tempAuroraTool->RequestConfigure();
383 tempAuroraTool->SetCalibrationTransform(calibration);
384 tool = tempAuroraTool;
399 mTool->SetCalibrationTransform(transform);
402 void IgstkTool::internalAttachedToTracker(
bool value)
404 if (mAttachedToTracker == value)
406 mAttachedToTracker = value;
408 mInternalStructure.
mUid +
" is " + (value ?
"at" :
"de") +
"tached " + (value ?
"to" :
"from")
413 void IgstkTool::internalTracked(
bool value)
415 if (mTracked == value)
419 if (!mTracked && mVisible)
420 this->internalVisible(
false);
425 void IgstkTool::internalVisible(
bool value)
427 if (mVisible == value)
433 void IgstkTool::addLogging()
435 bool logging =
settings()->
value(
"IGSTKDebugLogging",
true).toBool();
438 std::ofstream* loggerFile =
new std::ofstream();
441 mLogger = igstk::Logger::New();
442 mLogOutput = itk::StdStreamLogOutput::New();
443 mLogOutput->SetStream(*loggerFile);
444 mLogger->AddLogOutput(mLogOutput);
445 mLogger->SetPriorityLevel(itk::Logger::DEBUG);
447 mTool->SetLogger(mLogger);
453 std::cout <<
"------------------------------------------------------------------" << std::endl;
454 std::cout <<
"mIsProbe: " << mInternalStructure.
mIsProbe << std::endl;
455 std::cout <<
"mIsReference: " << mInternalStructure.
mIsReference << std::endl;
456 std::cout <<
"mIsPointer: " << mInternalStructure.
mIsPointer << std::endl;
457 std::cout <<
"mName: " << mInternalStructure.
mName << std::endl;
458 std::cout <<
"mUid: " << mInternalStructure.
mUid << std::endl;
459 std::cout <<
"mTrackerType: " << mInternalStructure.
mTrackerType << std::endl;
460 std::cout <<
"mSROMFilename: " << mInternalStructure.
mSROMFilename << std::endl;
461 std::cout <<
"mPortNumber: " << mInternalStructure.
mPortNumber << std::endl;
462 std::cout <<
"mChannelNumber: " << mInternalStructure.
mChannelNumber << std::endl;
464 std::cout <<
"mWireless: " << mInternalStructure.
mWireless << std::endl;
465 std::cout <<
"m5DOF: " << mInternalStructure.
m5DOF << std::endl;
466 std::cout <<
"mCalibration: " << mInternalStructure.
mCalibration << std::endl;
469 std::cout <<
"mGraphicsFileName: " << mInternalStructure.
mGraphicsFileName << std::endl;
471 std::cout <<
"mLoggingFolderName: " << mInternalStructure.
mLoggingFolderName << std::endl;
472 std::cout <<
"------------------------------------------------------------------" << std::endl;
QString qstring_cast(const T &val)
vtkSmartPointer< class vtkMatrix4x4 > vtkMatrix4x4Ptr
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
void reportError(QString msg)
tsPOLARIS
NDIs Polaris tracker.
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class IgstkTool > IgstkToolPtr
cstring_cast_Placeholder cstring_cast(const T &val)
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
std::string string_cast(const T &val)
bool similar(const DoubleBoundingBox3D &a, const DoubleBoundingBox3D &b, double tol)
tsPOLARIS_SPECTRA
NDIs Polaris Spectra tracker.
void reportWarning(QString msg)
void sampleInfo2xml(const igstk::NDITracker::TrackingSampleInfo &info, QDomElement &node)
Settings * settings()
Shortcut for accessing the settings instance.
tsAURORA
NDIs Aurora tracker.
tsMICRON
Claron Technologys Micron tracker.
boost::shared_ptr< IgstkTracker > TrackerPtr
tsPOLARIS_VICRA
NDIs Polaris Vicra tracker.