12 #define _USE_MATH_DEFINES 16 #include "boost/bind.hpp" 23 #include <vtkDoubleArray.h> 24 #include <QCoreApplication> 55 mLastLoadPositionHistory(0),
65 this->initializeManualTool();
67 connect(
settings(), SIGNAL(valueChangedFor(QString)),
this, SLOT(globalConfigurationFileChangedSlot(QString)));
69 this->listenForTrackingSystemServices(context);
74 while (!mTrackingSystems.empty())
84 void TrackingImplService::onSystemStateChanged()
86 this->rebuildCachedTools();
103 std::vector<TrackingSystemServicePtr> old = mPlaybackSystem->getBase();
104 for (
unsigned i=0; i<old.size(); ++i)
106 mPlaybackSystem.reset();
113 std::vector<TrackingSystemServicePtr> old = mPlaybackSystem->getBase();
114 for (
unsigned i=0; i<old.size(); ++i)
138 mTrackingSystems.push_back(system);
139 report(
"Installing tracking system: " + system->getUid());
141 this->onSystemStateChanged();
146 report(
"Uninstalling tracking system: " + system->getUid());
149 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
151 if (mTrackingSystems[i]!=system)
153 mTrackingSystems.erase(mTrackingSystems.begin()+i);
157 this->onSystemStateChanged();
162 return mTrackingSystems;
165 void TrackingImplService::initializeManualTool()
171 mTools[
"ManualTool"] = mManualTool;
172 mManualTool->setVisible(
true);
173 connect(mManualTool.get(), &
Tool::toolVisible,
this, &TrackingImplService::activeCheckSlot);
175 connect(mManualTool.get(), &
Tool::tooltipOffset,
this, &TrackingImplService::onTooltipOffset);
180 mManualTool->set_prMt(prMt);
181 this->activeCheckSlot();
187 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
188 state = std::max(state, mTrackingSystems[i]->
getState());
194 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
198 void TrackingImplService::listenForTrackingSystemServices(ctkPluginContext *context)
202 boost::bind(&TrackingImplService::onTrackingSystemAdded,
this, _1),
203 boost::bind(&TrackingImplService::onTrackingSystemModified,
this, _1),
204 boost::bind(&TrackingImplService::onTrackingSystemRemoved,
this, _1)
206 mServiceListener->open();
224 void TrackingImplService::rebuildCachedTools()
227 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
229 this->addToolsFrom(mTrackingSystems[i]);
231 mTools[mManualTool->getUid()] = mManualTool;
232 this->imbueManualToolWithRealProperties();
233 this->loadPositionHistory();
234 this->resetTrackingPositionFilters();
235 this->onTooltipOffset(mToolTipOffset);
239 void TrackingImplService::imbueManualToolWithRealProperties()
242 if (!
settings()->value(
"giveManualToolPhysicalProperties").toBool())
245 for (ToolMap::iterator iter = mTools.begin(); iter != mTools.end(); ++iter)
247 if (iter->second == mManualTool)
251 mManualTool->setBase(iter->second);
252 mManualTool->startEmittingContinuousPositions(100);
254 report(
"Manual tool imbued with properties from " + iter->first);
261 std::vector<ToolPtr> tools = system->getTools();
262 for (
unsigned i=0; i<tools.size(); ++i)
265 mTools[tool->getUid()] = tool;
266 connect(tool.get(), SIGNAL(toolVisible(
bool)),
this, SLOT(activeCheckSlot()));
271 mReferenceTool = tool;
275 void TrackingImplService::onTooltipOffset(
double val)
277 mToolTipOffset = val;
278 for (ToolMap::iterator iter = mTools.begin(); iter != mTools.end(); ++iter)
280 iter->second->setTooltipOffset(val);
289 ToolMap::iterator it = tools.begin();
290 for (; it != tools.end(); ++it)
295 retval[it->second] = toolMap;
311 ToolMap::iterator it = mTools.find(uid);
312 if (it != mTools.end())
325 if (mActiveTool && mActiveTool->getUid() == uid)
332 mActiveTool = newTool;
337 if (oldTool && (oldTool!=mManualTool))
338 mManualTool->set_prMt(oldTool->get_prMt(), oldTool->getTimestamp() -1);
339 mManualTool->setVisible(
true);
343 mManualTool->setVisible(
false);
351 return mReferenceTool;
354 void TrackingImplService::savePositionHistory()
356 QString filename = this->getLoggingFolder() +
"/toolpositions.snwpos";
360 ToolMap::iterator it = mTools.begin();
361 for (; it != mTools.end(); ++it)
370 TimedTransformMap::iterator iter = data->lower_bound(mLastLoadPositionHistory);
371 for (; iter != data->end(); ++iter)
372 writer.
write(iter->second, (iter->first), current->getUid());
378 void TrackingImplService::loadPositionHistory()
383 this->savePositionHistory();
385 QString filename = this->getLoggingFolder()+
"/toolpositions.snwpos";
393 QStringList missingTools;
395 while (!reader.
atEnd())
397 if (!reader.
read(&matrix, ×tamp, &toolUid))
403 (*current->getPositionHistory())[timestamp] = matrix;
407 missingTools << toolUid;
411 missingTools.removeDuplicates();
412 missingTools.removeAll(
"");
414 if (!missingTools.empty())
416 reportWarning(QString(
"Loaded position history, but some of the tools " 417 "are not present in the configuration:" 418 "\n \t%1").arg(missingTools.join(
"\n \t")));
434 void TrackingImplService::globalConfigurationFileChangedSlot(QString key)
436 if (key.contains(
"TrackingPositionFilter"))
438 this->resetTrackingPositionFilters();
442 void TrackingImplService::resetTrackingPositionFilters()
444 bool enabled =
settings()->
value(
"TrackingPositionFilter/enabled",
false).toBool();
445 double cutoff =
settings()->
value(
"TrackingPositionFilter/cutoffFrequency", 0).toDouble();
447 for (ToolMap::iterator iter=mTools.begin(); iter!=mTools.end(); ++iter)
454 filter->setCutOffFrequency(cutoff);
456 iter->second->resetTrackingPositionFilter(filter);
460 void TrackingImplService::activeCheckSlot()
462 if (this->manualToolHasMostRecentTimestamp() && mManualTool->getVisible())
468 bool use =
settings()->
value(
"Automation/autoSelectActiveTool").toBool();
473 std::vector<ToolPtr> tools = this->getVisibleTools();
474 tools.push_back(mManualTool);
480 const QString uid = tools[0]->getUid();
485 bool TrackingImplService::manualToolHasMostRecentTimestamp()
498 for (ToolMap::iterator it = mTools.begin(); it != mTools.end(); ++it)
504 bestTime = std::max(bestTime, it->second->getTimestamp());
510 return (mts > bestTime);
513 std::vector<ToolPtr> TrackingImplService::getVisibleTools()
515 std::vector<ToolPtr> retval;
516 for (ToolMap::iterator it = mTools.begin(); it != mTools.end(); ++it)
517 if (it->second->getVisible())
518 retval.push_back(it->second);
555 QString TrackingImplService::getLoggingFolder()
557 return mSession->getSubFolder(
"Logs/");
560 void TrackingImplService::onSessionChanged()
562 QString loggingFolder = this->getLoggingFolder();
564 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
565 mTrackingSystems[i]->setLoggingFolder(loggingFolder);
568 void TrackingImplService::onSessionCleared()
570 mManualTool->set_prMt(Transform3D::Identity());
573 void TrackingImplService::onSessionLoad(QDomElement& node)
576 QDomElement toolManagerNode = root.
descend(
"managers/toolManager").
node().toElement();
577 if (!toolManagerNode.isNull())
578 this->parseXml(toolManagerNode);
582 void TrackingImplService::onSessionSave(QDomElement& node)
585 QDomElement managerNode = root.
descend(
"managers").
node().toElement();
586 this->addXml(managerNode);
588 this->savePositionHistory();
591 void TrackingImplService::addXml(QDomNode& parentNode)
597 base.addTextToElement(
"manualTool",
"\n" +
qstring_cast(mManualTool->get_prMt()));
602 ToolMap::iterator toolIt = tools.begin();
603 for (; toolIt != tools.end(); ++toolIt)
608 toolsNode.addObjectToElement(
"tool", tool);
614 void TrackingImplService::parseXml(QDomNode& dataNode)
616 if (dataNode.isNull())
621 QString manualToolText = dataNode.namedItem(
"manualTool").toElement().text();
622 mManualTool->set_prMt(Transform3D::fromString(manualToolText));
625 this->onTooltipOffset(mToolTipOffset);
632 for (
unsigned i=0; i<toolNodes.size(); ++i)
634 QDomElement toolNode = toolNodes[i];
635 QString tool_uid = toolNode.attribute(
"uid");
636 if (tools.find(tool_uid) != tools.end())
638 cxToolPtr tool = boost::dynamic_pointer_cast<
ToolImpl>(tools.find(tool_uid)->second);
664 if (active && active->getProbe())
670 for (ToolMap::iterator iter = tools.begin(); iter != tools.end(); ++iter)
671 if (iter->second->getProbe() && iter->second->getProbe()->isValid() && iter->second->getVisible())
675 for (ToolMap::iterator iter = tools.begin(); iter != tools.end(); ++iter)
676 if (iter->second->getProbe() && iter->second->getProbe()->isValid())
686 for (
unsigned i=0; i<mTrackingSystems.size(); ++i)
int getPriority(DataPtr data)
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...
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)
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 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)
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)
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