Fraxinus  18.10
An IGT application
cxTrackingSystemPlaybackService.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
12 
13 
14 #include "cxLogger.h"
15 #include "cxTime.h"
16 #include "cxPlaybackTime.h"
17 #include "cxPlaybackTool.h"
18 #include <QApplication>
19 #include "cxUtilHelpers.h"
20 #include "cxManualTool.h"
21 #include "cxTrackerConfiguration.h"
22 
23 namespace cx
24 {
25 
26 TrackingSystemPlaybackService::TrackingSystemPlaybackService(PlaybackTimePtr controller, std::vector<TrackingSystemServicePtr> base, ManualToolPtr manual)
27 {
28  mBases = base;
29  mState = Tool::tsNONE;
30  mController = controller;
31  mManual = manual;
32 }
33 
35 {
36  this->setState(Tool::tsNONE);
37 }
38 
39 void TrackingSystemPlaybackService::start()
40 {
41  if (!this->forceBaseToConfiguredState())
42  {
43  reportWarning("ToolManager must be configured before setting playback");
44  return;
45  }
46 
47  std::vector<ToolPtr> original;
48  for (unsigned i=0; i<mBases.size(); ++i)
49  {
50  std::vector<ToolPtr> original_one = mBases[i]->getTools();
51  std::copy(original_one.begin(), original_one.end(), std::back_inserter(original));
52  }
53 
54  std::pair<double,double> timeRange(getMilliSecondsSinceEpoch(), 0);
55 
56  for (unsigned i=0; i<original.size(); ++i)
57  {
58  cx::PlaybackToolPtr current(new PlaybackTool(original[i], mController));
59  connect(current.get(), &Tool::toolTransformAndTimestamp, this, &TrackingSystemPlaybackService::onToolPositionChanged);
60  mTools.push_back(current);
61 
62  TimedTransformMapPtr history = original[i]->getPositionHistory();
63  if (!history->empty())
64  {
65  timeRange.first = std::min(timeRange.first, history->begin()->first);
66  timeRange.second = std::max(timeRange.second, history->rbegin()->first);
67  }
68  }
69 
70  mController->initialize(QDateTime::fromMSecsSinceEpoch(timeRange.first), timeRange.second - timeRange.first);
71 
72  report("Opened Playback Mode");
73  mState = Tool::tsTRACKING;
74  emit stateChanged();
75 }
76 
77 void TrackingSystemPlaybackService::stop()
78 {
79  mTools.clear();
80  mState = Tool::tsNONE;
81  report("Closed Playback Mode");
82  emit stateChanged();
83 }
84 
85 bool TrackingSystemPlaybackService::forceBaseToConfiguredState()
86 {
87  bool atLeastOneConfigured = false;
88 
89  for (unsigned i=0; i<mBases.size(); ++i)
90  {
91  // attempt to configure tracker if not configured,
92  // or deinit/stop tracking if on.
93  if (mBases[i]->getState() != Tool::tsCONFIGURED)
94  {
95  mBases[i]->setState(Tool::tsCONFIGURED);
96  this->waitForState(mBases[i], Tool::tsCONFIGURED, 200);
97  }
98 
99  bool configured = (mBases[i]->getState() >= Tool::tsCONFIGURED);
100  atLeastOneConfigured = atLeastOneConfigured || configured;
101  }
102 
103  return atLeastOneConfigured;
104 }
105 
109 void TrackingSystemPlaybackService::waitForState(TrackingSystemServicePtr base, Tool::State state, int timeout)
110 {
111  int interval = 10;
112  for (unsigned i=0; i<timeout/interval; ++i)
113  {
114  if (base->getState() == state)
115  break;
116  qApp->processEvents();
117  sleep_ms(interval);
118  }
119 }
120 
122 {
123  std::vector<ToolPtr> retval;
124  std::copy(mTools.begin(), mTools.end(), back_inserter(retval));
125  return retval;
126 }
127 
129 {
130  return mState;
131 }
132 
133 bool TrackingSystemPlaybackService::isRunning() const
134 {
135  return !mTools.empty();
136 }
137 
139 {
140  mState = val;
141 
142  if (mState >= Tool::tsCONFIGURED)
143  {
144  if (!this->isRunning())
145  this->start();
146  }
147  else
148  {
149  if (this->isRunning())
150  this->stop();
151  }
152 }
153 
155 {
156 
157 }
158 
160 {
162 }
163 
164 void TrackingSystemPlaybackService::onToolPositionChanged(Transform3D matrix, double timestamp)
165 {
166  // Overwrite manual tool pos, set timestamp to 1ms previous.
167  // This makes sure manual tool is not picked as active.
168  mManual->set_prMt(matrix, timestamp-1);
169 // mManual->setVisible(false);
170 // mManual->setVisible(true);
171 }
172 
173 
174 
175 } // namespace cx
virtual void setLoggingFolder(QString loggingFolder)
virtual void setState(const Tool::State val)
asynchronously request a state. Wait for signal stateChanged()
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class PlaybackTool > PlaybackToolPtr
double getMilliSecondsSinceEpoch()
Definition: cxTime.cpp:44
void toolTransformAndTimestamp(Transform3D matrix, double timestamp)
boost::shared_ptr< class ManualTool > ManualToolPtr
configured with basic info
Definition: cxTool.h:75
virtual TrackerConfigurationPtr getConfiguration()
boost::shared_ptr< class PlaybackTime > PlaybackTimePtr
boost::shared_ptr< class TrackerConfiguration > TrackerConfigurationPtr
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
static TrackerConfigurationPtr getNullObject()
boost::shared_ptr< TimedTransformMap > TimedTransformMapPtr
Definition: cxTool.h:36
not available
Definition: cxTool.h:74
A tool used during playback.
void report(QString msg)
Definition: cxLogger.cpp:69
TrackingSystemPlaybackService(PlaybackTimePtr controller, std::vector< TrackingSystemServicePtr > base, ManualToolPtr manual)
emitting tracking data
Definition: cxTool.h:77
void sleep_ms(int ms)
Namespace for all CustusX production code.
boost::shared_ptr< class TrackingSystemService > TrackingSystemServicePtr