Fraxinus  17.12
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) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
33 
34 
35 #include "cxLogger.h"
36 #include "cxTime.h"
37 #include "cxPlaybackTime.h"
38 #include "cxPlaybackTool.h"
39 #include <QApplication>
40 #include "cxUtilHelpers.h"
41 #include "cxManualTool.h"
42 #include "cxTrackerConfiguration.h"
43 
44 namespace cx
45 {
46 
47 TrackingSystemPlaybackService::TrackingSystemPlaybackService(PlaybackTimePtr controller, std::vector<TrackingSystemServicePtr> base, ManualToolPtr manual)
48 {
49  mBases = base;
50  mState = Tool::tsNONE;
51  mController = controller;
52  mManual = manual;
53 }
54 
56 {
57  this->setState(Tool::tsNONE);
58 }
59 
60 void TrackingSystemPlaybackService::start()
61 {
62  if (!this->forceBaseToConfiguredState())
63  {
64  reportWarning("ToolManager must be configured before setting playback");
65  return;
66  }
67 
68  std::vector<ToolPtr> original;
69  for (unsigned i=0; i<mBases.size(); ++i)
70  {
71  std::vector<ToolPtr> original_one = mBases[i]->getTools();
72  std::copy(original_one.begin(), original_one.end(), std::back_inserter(original));
73  }
74 
75  std::pair<double,double> timeRange(getMilliSecondsSinceEpoch(), 0);
76 
77  for (unsigned i=0; i<original.size(); ++i)
78  {
79  cx::PlaybackToolPtr current(new PlaybackTool(original[i], mController));
80  connect(current.get(), &Tool::toolTransformAndTimestamp, this, &TrackingSystemPlaybackService::onToolPositionChanged);
81  mTools.push_back(current);
82 
83  TimedTransformMapPtr history = original[i]->getPositionHistory();
84  if (!history->empty())
85  {
86  timeRange.first = std::min(timeRange.first, history->begin()->first);
87  timeRange.second = std::max(timeRange.second, history->rbegin()->first);
88  }
89  }
90 
91  mController->initialize(QDateTime::fromMSecsSinceEpoch(timeRange.first), timeRange.second - timeRange.first);
92 
93  report("Opened Playback Mode");
94  mState = Tool::tsTRACKING;
95  emit stateChanged();
96 }
97 
98 void TrackingSystemPlaybackService::stop()
99 {
100  mTools.clear();
101  mState = Tool::tsNONE;
102  report("Closed Playback Mode");
103  emit stateChanged();
104 }
105 
106 bool TrackingSystemPlaybackService::forceBaseToConfiguredState()
107 {
108  bool atLeastOneConfigured = false;
109 
110  for (unsigned i=0; i<mBases.size(); ++i)
111  {
112  // attempt to configure tracker if not configured,
113  // or deinit/stop tracking if on.
114  if (mBases[i]->getState() != Tool::tsCONFIGURED)
115  {
116  mBases[i]->setState(Tool::tsCONFIGURED);
117  this->waitForState(mBases[i], Tool::tsCONFIGURED, 200);
118  }
119 
120  bool configured = (mBases[i]->getState() >= Tool::tsCONFIGURED);
121  atLeastOneConfigured = atLeastOneConfigured || configured;
122  }
123 
124  return atLeastOneConfigured;
125 }
126 
130 void TrackingSystemPlaybackService::waitForState(TrackingSystemServicePtr base, Tool::State state, int timeout)
131 {
132  int interval = 10;
133  for (unsigned i=0; i<timeout/interval; ++i)
134  {
135  if (base->getState() == state)
136  break;
137  qApp->processEvents();
138  sleep_ms(interval);
139  }
140 }
141 
143 {
144  std::vector<ToolPtr> retval;
145  std::copy(mTools.begin(), mTools.end(), back_inserter(retval));
146  return retval;
147 }
148 
150 {
151  return mState;
152 }
153 
154 bool TrackingSystemPlaybackService::isRunning() const
155 {
156  return !mTools.empty();
157 }
158 
160 {
161  mState = val;
162 
163  if (mState >= Tool::tsCONFIGURED)
164  {
165  if (!this->isRunning())
166  this->start();
167  }
168  else
169  {
170  if (this->isRunning())
171  this->stop();
172  }
173 }
174 
176 {
177 
178 }
179 
181 {
183 }
184 
185 void TrackingSystemPlaybackService::onToolPositionChanged(Transform3D matrix, double timestamp)
186 {
187  // Overwrite manual tool pos, set timestamp to 1ms previous.
188  // This makes sure manual tool is not picked as active.
189  mManual->set_prMt(matrix, timestamp-1);
190 // mManual->setVisible(false);
191 // mManual->setVisible(true);
192 }
193 
194 
195 
196 } // 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:65
void toolTransformAndTimestamp(Transform3D matrix, double timestamp)
boost::shared_ptr< class ManualTool > ManualToolPtr
configured with basic info
Definition: cxTool.h:96
virtual TrackerConfigurationPtr getConfiguration()
boost::shared_ptr< class PlaybackTime > PlaybackTimePtr
boost::shared_ptr< class TrackerConfiguration > TrackerConfigurationPtr
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
static TrackerConfigurationPtr getNullObject()
boost::shared_ptr< TimedTransformMap > TimedTransformMapPtr
Definition: cxTool.h:57
not available
Definition: cxTool.h:95
A tool used during playback.
void report(QString msg)
Definition: cxLogger.cpp:90
TrackingSystemPlaybackService(PlaybackTimePtr controller, std::vector< TrackingSystemServicePtr > base, ManualToolPtr manual)
emitting tracking data
Definition: cxTool.h:98
void sleep_ms(int ms)
Namespace for all CustusX production code.
boost::shared_ptr< class TrackingSystemService > TrackingSystemServicePtr