Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
48 {
49  mBase = 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 = mBase->getTools();
69 
70  std::pair<double,double> timeRange(getMilliSecondsSinceEpoch(), 0);
71 
72  for (unsigned i=0; i<original.size(); ++i)
73  {
74  cx::PlaybackToolPtr current(new PlaybackTool(original[i], mController));
75  connect(current.get(), &Tool::toolTransformAndTimestamp, this, &TrackingSystemPlaybackService::onToolPositionChanged);
76  mTools.push_back(current);
77 
78  TimedTransformMapPtr history = original[i]->getPositionHistory();
79  if (!history->empty())
80  {
81  timeRange.first = std::min(timeRange.first, history->begin()->first);
82  timeRange.second = std::max(timeRange.second, history->rbegin()->first);
83  }
84  }
85 
86  mController->initialize(QDateTime::fromMSecsSinceEpoch(timeRange.first), timeRange.second - timeRange.first);
87 
88  report("Opened Playback Mode");
89  mState = Tool::tsTRACKING;
90  emit stateChanged();
91 }
92 
93 void TrackingSystemPlaybackService::stop()
94 {
95  mTools.clear();
96  mState = Tool::tsNONE;
97  report("Closed Playback Mode");
98  emit stateChanged();
99 }
100 
101 bool TrackingSystemPlaybackService::forceBaseToConfiguredState()
102 {
103  // attempt to configure tracker if not configured,
104  // or deinit/stop tracking if on.
105  if (mBase->getState() != Tool::tsCONFIGURED)
106  {
107  mBase->setState(Tool::tsCONFIGURED);
108  this->waitForState(mBase, Tool::tsCONFIGURED, 200);
109  }
110 
111  return (mBase->getState() >= Tool::tsCONFIGURED);
112 }
113 
117 void TrackingSystemPlaybackService::waitForState(TrackingSystemServicePtr base, Tool::State state, int timeout)
118 {
119  int interval = 10;
120  for (unsigned i=0; i<timeout/interval; ++i)
121  {
122  if (base->getState() == state)
123  break;
124  qApp->processEvents();
125  sleep_ms(interval);
126  }
127 }
128 
130 {
131  std::vector<ToolPtr> retval;
132  std::copy(mTools.begin(), mTools.end(), back_inserter(retval));
133  return retval;
134 }
135 
137 {
138  return mState;
139 }
140 
141 bool TrackingSystemPlaybackService::isRunning() const
142 {
143  return !mTools.empty();
144 }
145 
147 {
148  mState = val;
149 
150  if (mState >= Tool::tsCONFIGURED)
151  {
152  if (!this->isRunning())
153  this->start();
154  }
155  else
156  {
157  if (this->isRunning())
158  this->stop();
159  }
160 }
161 
163 {
164 
165 }
166 
168 {
170 }
171 
172 void TrackingSystemPlaybackService::onToolPositionChanged(Transform3D matrix, double timestamp)
173 {
174  // Overwrite manual tool pos, set timestamp to 1ms previous.
175  // This makes sure manual tool is not picked as active.
176  mManual->set_prMt(matrix, timestamp-1);
177 // mManual->setVisible(false);
178 // mManual->setVisible(true);
179 }
180 
181 
182 
183 } // 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
TrackingSystemPlaybackService(PlaybackTimePtr controller, TrackingSystemServicePtr base, ManualToolPtr manual)
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
void report(QString msg)
Definition: cxLogger.cpp:90
emitting tracking data
Definition: cxTool.h:98
void sleep_ms(int ms)
boost::shared_ptr< class TrackingSystemService > TrackingSystemServicePtr