12 #define _USE_MATH_DEFINES 16 #include "boost/bind.hpp" 23 #include <vtkDoubleArray.h> 24 #include <QCoreApplication> 54 mLastLoadPositionHistory(0),
66 this->initializeManualTool();
68 connect(
settings(), SIGNAL(valueChangedFor(QString)),
this, SLOT(globalConfigurationFileChangedSlot(QString)));
70 this->listenForTrackingSystemServices(context);
75 while (!mTrackingSystems.empty())
85 void TrackingImplService::onSystemStateChanged()
87 this->rebuildCachedTools();
104 std::vector<TrackingSystemServicePtr> old = mPlaybackSystem->getBase();
105 for (
unsigned i=0; i<old.size(); ++i)
107 mPlaybackSystem.reset();
114 std::vector<TrackingSystemServicePtr> old = mPlaybackSystem->getBase();
115 for (
unsigned i=0; i<old.size(); ++i)
140 mTrackingSystems.push_back(system);
141 report(
"Installing tracking system: " + system->getUid());
144 this->onSystemStateChanged();
149 report(
"Uninstalling tracking system: " + system->getUid());
153 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
155 if (mTrackingSystems[i]!=system)
157 mTrackingSystems.erase(mTrackingSystems.begin()+i);
161 this->onSystemStateChanged();
166 return mTrackingSystems;
169 void TrackingImplService::initializeManualTool()
175 mTools[
"ManualTool"] = mManualTool;
176 mManualTool->setVisible(
true);
177 connect(mManualTool.get(), &
Tool::toolVisible,
this, &TrackingImplService::activeCheckSlot);
179 connect(mManualTool.get(), &
Tool::tooltipOffset,
this, &TrackingImplService::onTooltipOffset);
184 mManualTool->set_prMt(prMt);
185 this->activeCheckSlot();
191 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
192 state = std::max(state, mTrackingSystems[i]->
getState());
198 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
202 void TrackingImplService::listenForTrackingSystemServices(ctkPluginContext *context)
206 boost::bind(&TrackingImplService::onTrackingSystemAdded,
this, _1),
207 boost::bind(&TrackingImplService::onTrackingSystemModified,
this, _1),
208 boost::bind(&TrackingImplService::onTrackingSystemRemoved,
this, _1)
210 mServiceListener->open();
228 void TrackingImplService::rebuildCachedTools()
231 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
233 this->addToolsFrom(mTrackingSystems[i]);
235 mTools[mManualTool->getUid()] = mManualTool;
236 this->imbueManualToolWithRealProperties();
237 this->loadPositionHistory();
238 this->resetTrackingPositionFilters();
239 this->onTooltipOffset(mToolTipOffset);
243 void TrackingImplService::imbueManualToolWithRealProperties()
246 if (!
settings()->value(
"giveManualToolPhysicalProperties").toBool())
249 for (ToolMap::iterator iter = mTools.begin(); iter != mTools.end(); ++iter)
251 if (iter->second == mManualTool)
255 mManualTool->setBase(iter->second);
256 mManualTool->startEmittingContinuousPositions(100);
258 report(
"Manual tool imbued with properties from " + iter->first);
265 std::vector<ToolPtr> tools = system->getTools();
266 for (
unsigned i=0; i<tools.size(); ++i)
269 mTools[tool->getUid()] = tool;
270 connect(tool.get(), SIGNAL(toolVisible(
bool)),
this, SLOT(activeCheckSlot()));
275 mReferenceTool = tool;
279 void TrackingImplService::onTooltipOffset(
double val)
281 mToolTipOffset = val;
282 for (ToolMap::iterator iter = mTools.begin(); iter != mTools.end(); ++iter)
284 iter->second->setTooltipOffset(val);
293 ToolMap::iterator it = tools.begin();
294 for (; it != tools.end(); ++it)
299 retval[it->second] = toolMap;
315 ToolMap::iterator it = mTools.find(uid);
316 if (it != mTools.end())
329 if (mActiveTool && mActiveTool->getUid() == uid)
336 mActiveTool = newTool;
341 if (oldTool && (oldTool!=mManualTool))
342 mManualTool->set_prMt(oldTool->get_prMt(), oldTool->getTimestamp() -1);
343 mManualTool->setVisible(
true);
347 mManualTool->setVisible(
false);
355 mActiveTool = mManualTool;
361 return mReferenceTool;
364 void TrackingImplService::savePositionHistory()
366 QString filename = this->getLoggingFolder() +
"/toolpositions.snwpos";
370 ToolMap::iterator it = mTools.begin();
371 for (; it != mTools.end(); ++it)
380 TimedTransformMap::iterator iter = data->lower_bound(mLastLoadPositionHistory);
381 for (; iter != data->end(); ++iter)
382 writer.
write(iter->second, (iter->first), current->getUid());
388 void TrackingImplService::loadPositionHistory()
393 this->savePositionHistory();
395 QString filename = this->getLoggingFolder()+
"/toolpositions.snwpos";
403 QStringList missingTools;
405 while (!reader.
atEnd())
407 if (!reader.
read(&matrix, ×tamp, &toolUid))
413 (*current->getPositionHistory())[timestamp] = matrix;
417 missingTools << toolUid;
421 missingTools.removeDuplicates();
422 missingTools.removeAll(
"");
424 if (!missingTools.empty())
426 reportWarning(QString(
"Loaded position history, but some of the tools " 427 "are not present in the configuration:" 428 "\n \t%1").arg(missingTools.join(
"\n \t")));
444 void TrackingImplService::globalConfigurationFileChangedSlot(QString key)
446 if (key.contains(
"TrackingPositionFilter"))
448 this->resetTrackingPositionFilters();
452 void TrackingImplService::resetTrackingPositionFilters()
454 bool enabled =
settings()->
value(
"TrackingPositionFilter/enabled",
false).toBool();
455 double cutoff =
settings()->
value(
"TrackingPositionFilter/cutoffFrequency", 0).toDouble();
457 for (ToolMap::iterator iter=mTools.begin(); iter!=mTools.end(); ++iter)
464 filter->setCutOffFrequency(cutoff);
466 iter->second->resetTrackingPositionFilter(filter);
470 void TrackingImplService::activeCheckSlot()
472 if (this->manualToolHasMostRecentTimestamp() && mManualTool->getVisible())
478 bool use =
settings()->
value(
"Automation/autoSelectActiveTool").toBool();
483 std::vector<ToolPtr> tools = this->getVisibleTools();
484 tools.push_back(mManualTool);
490 const QString uid = tools[0]->getUid();
495 bool TrackingImplService::manualToolHasMostRecentTimestamp()
508 for (ToolMap::iterator it = mTools.begin(); it != mTools.end(); ++it)
514 bestTime = std::max(bestTime, it->second->getTimestamp());
520 return (mts > bestTime);
523 std::vector<ToolPtr> TrackingImplService::getVisibleTools()
525 std::vector<ToolPtr> retval;
526 for (ToolMap::iterator it = mTools.begin(); it != mTools.end(); ++it)
527 if (it->second->getVisible())
528 retval.push_back(it->second);
565 QString TrackingImplService::getLoggingFolder()
567 return mSession->getSubFolder(
"Logs/");
570 void TrackingImplService::onSessionChanged()
572 QString loggingFolder = this->getLoggingFolder();
574 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
575 mTrackingSystems[i]->setLoggingFolder(loggingFolder);
578 void TrackingImplService::onSessionCleared()
580 mManualTool->set_prMt(Transform3D::Identity());
583 void TrackingImplService::onSessionLoad(QDomElement& node)
586 QDomElement toolManagerNode = root.
descend(
"managers/toolManager").
node().toElement();
587 if (!toolManagerNode.isNull())
588 this->parseXml(toolManagerNode);
592 void TrackingImplService::onSessionSave(QDomElement& node)
595 QDomElement managerNode = root.
descend(
"managers").
node().toElement();
596 this->addXml(managerNode);
598 this->savePositionHistory();
601 void TrackingImplService::addXml(QDomNode& parentNode)
607 base.addTextToElement(
"manualTool",
"\n" +
qstring_cast(mManualTool->get_prMt()));
612 ToolMap::iterator toolIt = tools.begin();
613 for (; toolIt != tools.end(); ++toolIt)
618 toolsNode.addObjectToElement(
"tool", tool);
624 void TrackingImplService::parseXml(QDomNode& dataNode)
626 if (dataNode.isNull())
631 QString manualToolText = dataNode.namedItem(
"manualTool").toElement().text();
632 mManualTool->set_prMt(Transform3D::fromString(manualToolText));
635 this->onTooltipOffset(mToolTipOffset);
642 for (
unsigned i=0; i<toolNodes.size(); ++i)
644 QDomElement toolNode = toolNodes[i];
645 QString tool_uid = toolNode.attribute(
"uid");
646 if (tools.find(tool_uid) != tools.end())
648 cxToolPtr tool = boost::dynamic_pointer_cast<
ToolImpl>(tools.find(tool_uid)->second);
674 if (active && active->getProbe())
680 for (ToolMap::iterator iter = tools.begin(); iter != tools.end(); ++iter)
681 if (iter->second->getProbe() && iter->second->getProbe()->isValid() && iter->second->getVisible())
685 for (ToolMap::iterator iter = tools.begin(); iter != tools.end(); ++iter)
686 if (iter->second->getProbe() && iter->second->getProbe()->isValid())
694 std::vector<TrackerConfigurationPtr> retval;
695 bool gotConfig =
false;
696 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
701 retval.push_back(config);
715 for(
unsigned i = 0; i < configs.size(); ++i)
717 if(configs[i]->getTrackingSystemImplementation() == trackingSystemImplementation)
730 if(configs.size() > 0)
731 retval = configs.at(0);
738 mTrackingSystemImplementation = trackingSystemImplementation;
743 return mTrackingSystemImplementation;
748 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
750 mTrackingSystems[i]->resetTimeSynchronization();
int getPriority(DataPtr data)
virtual void clearActiveTool()
Deselect active tool.
QString qstring_cast(const T &val)
double parseDoubleFromElementWithDefault(QString name, double defaultValue)
Transform3D createTransformRotateY(const double angle)
virtual void runDummyTool(DummyToolPtr tool)
void isLoading(QDomElement &root)
emitted while loading a session. Xml storage is available, getRootFolder() is set to loaded value...
virtual void resetTimeSynchronization()
Reset time synchronization. Used for resetting time synchronization of incoming timestamps in OpenIGT...
virtual void setCurrentTrackingSystemImplementation(QString trackingSystemImplementation)
XMLNodeAdder descend(QString path)
virtual ToolPtr getFirstProbe()
Find a probe that can be connected to a rt source.
std::map< ToolPtr, TimedTransformMap > SessionToolHistoryMap
virtual ToolPtr getTool(const QString &uid)
get a specific tool
QDomElement addElement(QString name)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< ToolImpl > cxToolPtr
std::vector< QDomElement > getDuplicateElements(QString name)
double getMilliSecondsSinceEpoch()
static SessionStorageServicePtr create(ctkPluginContext *pluginContext)
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
virtual Tool::State getState() const
virtual void setState(const Tool::State val)
virtual void setPlaybackMode(PlaybackTimePtr controller)
void sessionChanged()
emitted after change to a new session (new or loaded or cleared)
void stateChanged()
Reset time synchronization. Used for resetting time synchronization of incoming timestamps in OpenIGT...
bool toolTypeSort(const ToolPtr tool1, const ToolPtr tool2)
function for sorting tools by type
boost::shared_ptr< class PlaybackTime > PlaybackTimePtr
Reader class for the position file.
virtual void setActiveTool(const QString &uid)
can be set to either a connected or configured tool
boost::shared_ptr< class DummyTool > DummyToolPtr
boost::shared_ptr< class TrackerConfiguration > TrackerConfigurationPtr
void activeToolChanged(const QString &uId)
void updateTrackingSystemImplementation(QString trackingSystemImplementation)
void reportWarning(QString msg)
boost::shared_ptr< TimedTransformMap > TimedTransformMapPtr
bool read(Transform3D *matrix, double *timestamp, int *toolIndex)
std::map< QString, ToolPtr > ToolMap
virtual ToolMap getTools()
get all configured and initialized tools
virtual TrackerConfigurationPtr getConfiguration()
QDomElement addTextToElement(QString name, QString text)
virtual ToolPtr getManualTool()
a mouse-controllable virtual tool that is available even when not tracking.
virtual void unInstallTrackingSystem(TrackingSystemServicePtr system)
XMLNodeParser descend(QString path)
virtual std::vector< TrackerConfigurationPtr > getConfigurations()
Settings * settings()
Shortcut for accessing the settings instance.
Writer class for the position file.
void cleared()
emitted when session is cleared, before isLoading is called
Helper class for listening to services being added, modified and removed.
std::map< QString, ToolPtr > ToolMap
virtual std::vector< TrackingSystemServicePtr > getTrackingSystems()
virtual ToolPtr getReferenceTool() const
get the tool that is used as a reference, if any
void onActiveToolChanged(const QString &uId)
Interface towards a dummy tracking system.
virtual ~TrackingImplService()
TrackingImplService(ctkPluginContext *context)
virtual QString getCurrentTrackingSystemImplementation()
void write(Transform3D matrix, uint64_t timestamp, int toolIndex)
virtual ToolPtr getActiveTool()
get the tool that has higest priority when tracking
Transform3D createTransformRotateZ(const double angle)
virtual bool isPlaybackMode() const
Interface towards a playback tracking system.Wraps another tracking system, enabling playback of the ...
boost::shared_ptr< class TrackingPositionFilter > TrackingPositionFilterPtr
virtual SessionToolHistoryMap getSessionHistory(double startTime, double stopTime)
virtual void installTrackingSystem(TrackingSystemServicePtr system)
void isSaving(QDomElement &root)
xml storage is available
std::map< double, Transform3D > TimedTransformMap
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr
boost::shared_ptr< class TrackingSystemService > TrackingSystemServicePtr