CustusX  15.3.3-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxIgstkToolManager.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 =========================================================================*/
32 
33 #include "cxIgstkToolManager.h"
34 
35 #include "cxLogger.h"
36 #include "cxTypeConversions.h"
37 
38 namespace cx
39 {
40 
42  std::vector<IgstkTool::InternalStructure> toolStructures,
43  IgstkTool::InternalStructure referenceToolStructure) :
44  mInitAnsweres(0), mInternalInitialized(false)
45 {
46  mTimer = 0;
47 
48  this->createTracker(trackerStructure);
49  this->createTools(toolStructures, referenceToolStructure);
50  this->setReferenceAndTrackerOnTools();
51 
52  connect(mTracker.get(), SIGNAL(tracking(bool)), this, SIGNAL(tracking(bool)));
53  connect(mTracker.get(), SIGNAL(error()), this, SIGNAL(error()));
54 
55  connect(mTracker.get(), SIGNAL(initialized(bool)), this, SLOT(deviceInitializedSlot(bool)));
56  connect(mTracker.get(), SIGNAL(tracking(bool)), this, SLOT(trackerTrackingSlot(bool)));
57 
58  mTimer = new QTimer();
59  connect(mTimer, SIGNAL(timeout()), this, SLOT(checkTimeoutsAndRequestTransformSlot()));
60 
61  igstk::RealTimeClock::Initialize();
62 
63  //WARNING will this work when newing several pulsegenerators in different threads????
64  mPulseGenerator = igstk::PulseGenerator::New();
65  mPulseGenerator->RequestSetFrequency(30.0);
66  mPulseGenerator->RequestStart();
67 }
68 
70 {
71  this->trackSlot(false);
72  this->initializeSlot(false);
73 
74  mPulseGenerator->RequestStop();
75 }
76 
77 std::map<QString, IgstkToolPtr> IgstkToolManager::getTools()
78 {
79  QMutexLocker sentry(&mToolMutex);
80  return mTools;
81 }
82 
84 {
85  QMutexLocker sentry(&mReferenceMutex);
86  return mReferenceTool;
87 }
88 
89 void IgstkToolManager::setReferenceAndTrackerOnTools()
90 {
91  if (!mReferenceTool)
92  {
93  reportWarning("Tracking is configured without a reference tool.");
94  }
95 
96  std::map<QString, IgstkToolPtr>::iterator it;
97  for (it = mTools.begin(); it != mTools.end(); ++it)
98  {
99  if (mReferenceTool)
100  it->second->setReference(mReferenceTool);
101  if (mTracker)
102  it->second->setTracker(mTracker);
103  }
104 }
105 
106 void IgstkToolManager::createTracker(IgstkTracker::InternalStructure trackerStructure)
107 {
108  TrackerPtr tracker(new IgstkTracker(trackerStructure));
109  if (tracker->isValid())
110  mTracker = tracker;
111  else
112  reportWarning("Invalid tracker.");
113 }
114 
115 void IgstkToolManager::createTools(std::vector<IgstkTool::InternalStructure> toolStructures,
116  IgstkTool::InternalStructure referenceToolStructure)
117 {
118  for (unsigned i = 0; i < toolStructures.size(); ++i)
119  {
120  this->addIgstkTools(toolStructures[i]);
121  }
122  if (!referenceToolStructure.mUid.isEmpty())
123  {
124  IgstkToolPtr refTool = this->addIgstkTools(referenceToolStructure);
125  if (refTool->isValid())
126  mReferenceTool = refTool;
127  }
128 }
129 
130 IgstkToolPtr IgstkToolManager::addIgstkTools(IgstkTool::InternalStructure& toolStructure)
131 {
132  IgstkToolPtr igstkTool(new IgstkTool(toolStructure));
133  if (igstkTool->isValid())
134  {
135  QMutexLocker sentry(&mToolMutex);
136  mTools[igstkTool->getUid()] = igstkTool;
137  connect(igstkTool.get(), SIGNAL(attachedToTracker(bool)), this, SLOT(deviceInitializedSlot(bool)));
138  }
139  else
140  {
141  reportWarning(toolStructure.mUid + " is not valid.");
142  }
143  return igstkTool;
144 }
145 
146 void IgstkToolManager::trackerTrackingSlot(bool isTracking)
147 {
148  if (isTracking)
149  mTimer->start(33);
150  else
151  mTimer->stop();
152 }
153 
155 {
156  if (on)
157  {
158  connect(mTracker.get(), SIGNAL(initialized(bool)), this, SLOT(attachToolsWhenTrackerIsInitializedSlot(bool)));
159  if (!mTracker->isOpen())
160  mTracker->open();
161  }
162  else
163  {
164  mTracker->detachTools(mTools); //not sure we have to detach all tools before we close, read NDI manual
165  if (mTracker->isOpen())
166  mTracker->close();
167  }
168 }
169 
171 {
172  if (on && !mTracker->isTracking())
173  mTracker->startTracking();
174  else if (!on && mTracker->isTracking())
175  mTracker->stopTracking();
176 }
177 
178 void IgstkToolManager::checkTimeoutsAndRequestTransformSlot()
179 {
180  mPulseGenerator->CheckTimeouts();
181 
182  std::map<QString, IgstkToolPtr>::iterator it = mTools.begin();
183  for (; it != mTools.end(); ++it)
184  {
185  if (!it->second)
186  continue;
187  if (mReferenceTool)
188  it->second->getPointer()->RequestComputeTransformTo(mReferenceTool->getPointer());
189  else
190  it->second->getPointer()->RequestComputeTransformTo(mTracker->getPointer());
191  }
192 }
193 
194 void IgstkToolManager::deviceInitializedSlot(bool deviceInit)
195 {
196  int numberOfDevices = mTools.size() + 1; //+1 is the tracker
197 
198  if (deviceInit)
199  {
200  mInitAnsweres++;
201 
202  if (mInitAnsweres == numberOfDevices)
203  {
204  mInternalInitialized = true;
205  emit initialized(true);
206  }
207  }
208  else
209  {
210  mInitAnsweres--;
211 
212  if (mInitAnsweres < numberOfDevices)
213  {
214  if (mInternalInitialized)
215  {
216  mInitAnsweres = 0;
217  mInternalInitialized = false;
218  emit initialized(false);
219  }
220  }
221  }
222 }
223 
224 void IgstkToolManager::attachToolsWhenTrackerIsInitializedSlot(bool open)
225 {
226  if (!open)
227  return;
228 
229  disconnect(mTracker.get(), SIGNAL(initialized(bool)), this, SLOT(attachToolsWhenTrackerIsInitializedSlot(bool)));
230  mTracker->attachTools(mTools);
231 }
232 
233 void IgstkToolManager::printStatus()
234 {
235  std::cout << "mInternalInitialized " << mInternalInitialized << std::endl;
236  std::cout << "mInitAnsweres " << mInitAnsweres << std::endl;
237  std::cout << "mTracker->isValid() " << mTracker->isValid() << std::endl;
238  std::cout << "mTracker->isOpen() " << mTracker->isOpen() << std::endl;
239  std::cout << "mTracker->isInitialized() " << mTracker->isInitialized() << std::endl;
240  std::cout << "mTracker->isTracking() " << mTracker->isTracking() << std::endl;
241  std::cout << "mTools.size() " << string_cast(mTools.size()) << std::endl;
242 }
243 }
boost::shared_ptr< IgstkTracker > TrackerPtr
std::map< QString, IgstkToolPtr > getTools()
ThreadSafe.
boost::shared_ptr< IgstkTool > IgstkToolPtr
Definition: cxIgstkTool.h:60
void trackSlot(bool on)
tracking on or off
std::string string_cast(const T &val)
void tracking(bool on)
void initializeSlot(bool on)
connects to the hardware
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
A tools internal structure.
Definition: cxIgstkTool.h:80
void initialized(bool on)
when all trackers and tools are initialized == true, else false
IgstkToolManager(IgstkTracker::InternalStructure trackerStructure, std::vector< IgstkTool::InternalStructure > toolStructures, IgstkTool::InternalStructure referenceToolStructure)
IgstkToolPtr getRefereceTool()
ThreadSafe.