33 #define _USE_MATH_DEFINES
37 #include "boost/bind.hpp"
44 #include <vtkDoubleArray.h>
45 #include <QCoreApplication>
76 mLastLoadPositionHistory(0),
86 this->initializeManualTool();
88 connect(
settings(), SIGNAL(valueChangedFor(QString)),
this, SLOT(globalConfigurationFileChangedSlot(QString)));
90 this->listenForTrackingSystemServices(context);
95 while (!mTrackingSystems.empty())
105 void TrackingImplService::onSystemStateChanged()
107 this->rebuildCachedTools();
124 mPlaybackSystem.reset();
153 mTrackingSystems.push_back(system);
155 this->onSystemStateChanged();
162 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
164 if (mTrackingSystems[i]!=system)
166 mTrackingSystems.erase(mTrackingSystems.begin()+i);
170 this->onSystemStateChanged();
175 return mTrackingSystems;
178 void TrackingImplService::initializeManualTool()
184 mTools[
"ManualTool"] = mManualTool;
185 mManualTool->setVisible(
true);
186 connect(mManualTool.get(), &
Tool::toolVisible,
this, &TrackingImplService::activeCheckSlot);
188 connect(mManualTool.get(), &
Tool::tooltipOffset,
this, &TrackingImplService::onTooltipOffset);
193 mManualTool->set_prMt(prMt);
194 this->activeCheckSlot();
200 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
201 state = std::max(state, mTrackingSystems[i]->
getState());
207 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
211 void TrackingImplService::listenForTrackingSystemServices(ctkPluginContext *context)
215 boost::bind(&TrackingImplService::onTrackingSystemAdded,
this, _1),
216 boost::bind(&TrackingImplService::onTrackingSystemModified,
this, _1),
217 boost::bind(&TrackingImplService::onTrackingSystemRemoved,
this, _1)
219 mServiceListener->open();
222 void TrackingImplService::onTrackingSystemAdded(TrackingSystemService* service)
227 void TrackingImplService::onTrackingSystemRemoved(TrackingSystemService* service)
232 void TrackingImplService::onTrackingSystemModified(TrackingSystemService* service)
236 void TrackingImplService::rebuildCachedTools()
239 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
241 this->addToolsFrom(mTrackingSystems[i]);
243 mTools[mManualTool->getUid()] = mManualTool;
244 this->imbueManualToolWithRealProperties();
246 this->loadPositionHistory();
249 void TrackingImplService::imbueManualToolWithRealProperties()
252 if (!
settings()->value(
"giveManualToolPhysicalProperties").toBool())
255 for (ToolMap::iterator iter = mTools.begin(); iter != mTools.end(); ++iter)
257 if (iter->second == mManualTool)
261 mManualTool->setBase(iter->second);
262 mManualTool->startEmittingContinuousPositions(100);
264 report(
"Manual tool imbued with properties from " + iter->first);
271 std::vector<ToolPtr> tools = system->getTools();
272 for (
unsigned i=0; i<tools.size(); ++i)
275 mTools[tool->getUid()] = tool;
276 connect(tool.get(), SIGNAL(toolVisible(
bool)),
this, SLOT(activeCheckSlot()));
281 mReferenceTool = tool;
285 void TrackingImplService::onTooltipOffset(
double val)
287 mToolTipOffset = val;
288 for (ToolMap::iterator iter = mTools.begin(); iter != mTools.end(); ++iter)
290 iter->second->setTooltipOffset(val);
299 ToolMap::iterator it = tools.begin();
300 for (; it != tools.end(); ++it)
305 retval[it->second] = toolMap;
321 ToolMap::iterator it = mTools.find(uid);
322 if (it != mTools.end())
335 if (mActiveTool && mActiveTool->getUid() == uid)
342 mActiveTool = newTool;
347 if (oldTool && (oldTool!=mManualTool))
348 mManualTool->set_prMt(oldTool->get_prMt(), oldTool->getTimestamp() -1);
349 mManualTool->setVisible(
true);
353 mManualTool->setVisible(
false);
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";
397 PositionStorageReader reader(filename);
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).toInt();
456 for (ToolMap::iterator iter=mTools.begin(); iter!=mTools.end(); ++iter)
460 filter.reset(
new TrackingPositionFilter());
461 iter->second->resetTrackingPositionFilter(filter);
465 void TrackingImplService::activeCheckSlot()
467 if (this->manualToolHasMostRecentTimestamp() && mManualTool->getVisible())
473 bool use =
settings()->
value(
"Automation/autoSelectActiveTool").toBool();
478 std::vector<ToolPtr> tools = this->getVisibleTools();
479 tools.push_back(mManualTool);
485 const QString uid = tools[0]->getUid();
490 bool TrackingImplService::manualToolHasMostRecentTimestamp()
503 for (ToolMap::iterator it = mTools.begin(); it != mTools.end(); ++it)
509 bestTime = std::max(bestTime, it->second->getTimestamp());
515 return (mts > bestTime);
518 std::vector<ToolPtr> TrackingImplService::getVisibleTools()
520 std::vector<ToolPtr> retval;
521 for (ToolMap::iterator it = mTools.begin(); it != mTools.end(); ++it)
522 if (it->second->getVisible())
523 retval.push_back(it->second);
560 QString TrackingImplService::getLoggingFolder()
562 return mSession->getSubFolder(
"Logs/");
565 void TrackingImplService::onSessionChanged()
567 QString loggingFolder = this->getLoggingFolder();
569 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
570 mTrackingSystems[i]->setLoggingFolder(loggingFolder);
573 void TrackingImplService::onSessionCleared()
575 mManualTool->set_prMt(Transform3D::Identity());
578 void TrackingImplService::onSessionLoad(QDomElement& node)
580 XMLNodeParser root(node);
581 QDomElement toolManagerNode = root.descend(
"managers/toolManager").node().toElement();
582 if (!toolManagerNode.isNull())
583 this->parseXml(toolManagerNode);
587 void TrackingImplService::onSessionSave(QDomElement& node)
589 XMLNodeAdder root(node);
590 QDomElement managerNode = root.descend(
"managers").node().toElement();
591 this->addXml(managerNode);
593 this->savePositionHistory();
596 void TrackingImplService::addXml(QDomNode& parentNode)
598 XMLNodeAdder parent(parentNode);
599 XMLNodeAdder base(parent.addElement(
"toolManager"));
601 base.addTextToElement(
"toolTipOffset",
qstring_cast(mToolTipOffset));
602 base.addTextToElement(
"manualTool",
"\n" +
qstring_cast(mManualTool->get_prMt()));
605 XMLNodeAdder toolsNode(base.addElement(
"tools"));
607 ToolMap::iterator toolIt = tools.begin();
608 for (; toolIt != tools.end(); ++toolIt)
610 cxToolPtr tool = boost::dynamic_pointer_cast<ToolImpl>(toolIt->second);
613 toolsNode.addObjectToElement(
"tool", tool);
619 void TrackingImplService::parseXml(QDomNode& dataNode)
621 if (dataNode.isNull())
624 XMLNodeParser base(dataNode);
626 QString manualToolText = dataNode.namedItem(
"manualTool").toElement().text();
627 mManualTool->set_prMt(Transform3D::fromString(manualToolText));
629 mToolTipOffset = base.parseDoubleFromElementWithDefault(
"toolTipOffset", 0.0);
630 this->onTooltipOffset(mToolTipOffset);
634 XMLNodeParser toolssNode(dataNode.namedItem(
"tools"));
635 std::vector<QDomElement> toolNodes = toolssNode.getDuplicateElements(
"tool");
637 for (
unsigned i=0; i<toolNodes.size(); ++i)
639 QDomElement toolNode = toolNodes[i];
640 QString tool_uid = toolNode.attribute(
"uid");
641 if (tools.find(tool_uid) != tools.end())
643 cxToolPtr tool = boost::dynamic_pointer_cast<ToolImpl>(tools.find(tool_uid)->second);
644 tool->parseXml(toolNode);
669 if (active && active->getProbe())
675 for (ToolMap::iterator iter = tools.begin(); iter != tools.end(); ++iter)
676 if (iter->second->getProbe() && iter->second->getProbe()->isValid() && iter->second->getVisible())
680 for (ToolMap::iterator iter = tools.begin(); iter != tools.end(); ++iter)
681 if (iter->second->getProbe() && iter->second->getProbe()->isValid())
690 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
int getPriority(DataPtr data)
QString qstring_cast(const T &val)
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 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
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< ToolImpl > cxToolPtr
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)
bool toolTypeSort(const ToolPtr tool1, const ToolPtr tool2)
function for sorting tools by type
boost::shared_ptr< class PlaybackTime > PlaybackTimePtr
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 reportWarning(QString msg)
boost::shared_ptr< TimedTransformMap > TimedTransformMapPtr
virtual ToolMap getTools()
get all configured and initialized tools
virtual TrackerConfigurationPtr getConfiguration()
virtual ToolPtr getManualTool()
a mouse-controllable virtual tool that is available even when not tracking.
virtual void unInstallTrackingSystem(TrackingSystemServicePtr system)
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
Interface towards a dummy tracking system.
virtual ~TrackingImplService()
TrackingImplService(ctkPluginContext *context)
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
boost::shared_ptr< class Tool > ToolPtr
boost::shared_ptr< class TrackingSystemService > TrackingSystemServicePtr