CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxOpenIGTLinkTrackingSystemService.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 
34 
35 #include "cxLogger.h"
36 #include "cxNetworkConnection.h"
38 #include "cxOpenIGTLinkTool.h"
39 
40 namespace cx
41 {
42 
43 std::vector<ToolPtr> toVector(std::map<QString, OpenIGTLinkToolPtr> map)
44 {
45  std::vector<ToolPtr> retval;
46  std::map<QString, OpenIGTLinkToolPtr>::iterator it = map.begin();
47  for(; it!= map.end(); ++it)
48  {
49  retval.push_back(it->second);
50  }
51  return retval;
52 }
53 
55  mState(Tool::tsNONE),
56  mConnection(connection)
57 
58 {
59  if(mConnection == NULL)
60  return;
61 
62  NetworkConnection* client = mConnection->getNetworkConnection();
63 
66  connect(client, &NetworkConnection::connected, this, &OpenIGTLinkTrackingSystemService::serverIsConnected);
67  connect(client, &NetworkConnection::disconnected, this, &OpenIGTLinkTrackingSystemService::serverIsDisconnected);
68  connect(client, &NetworkConnection::transform, this, &OpenIGTLinkTrackingSystemService::receiveTransform);
69  connect(client, &NetworkConnection::calibration, this, &OpenIGTLinkTrackingSystemService::receiveCalibration);
70  connect(client, &NetworkConnection::probedefinition, this, &OpenIGTLinkTrackingSystemService::receiveProbedefinition);
71 }
72 
74 {
75  this->deconfigure();
76 }
77 
79 {
80  return "org.custusx.core.openigtlink";
81 }
82 
84 {
85  return mState;
86 }
87 
89 {
90  if (mState==val)
91  return;
92 
93  if (val > mState) // up
94  {
95  if (val == Tool::tsTRACKING)
96  this->startTracking();
97  else if (val == Tool::tsINITIALIZED)
98  this->initialize();
99  else if (val == Tool::tsCONFIGURED)
100  this->configure();
101  }
102  else // down
103  {
104  if (val == Tool::tsINITIALIZED)
105  this->stopTracking();
106  else if (val == Tool::tsCONFIGURED)
107  this->uninitialize();
108  else if (val == Tool::tsNONE)
109  this->deconfigure();
110  }
111 }
112 
114 {
115  return toVector(mTools);
116 }
117 
119 {
121  return retval;
122 }
123 
125 {
126  return mReference;
127 }
128 
130 {}
131 
132 void OpenIGTLinkTrackingSystemService::configure()
133 {
134  this->serverIsConfigured();
135 }
136 
137 void OpenIGTLinkTrackingSystemService::deconfigure()
138 {
139  mTools.clear();
140  mReference.reset();
141  this->serverIsDeconfigured();
142 }
143 
144 void OpenIGTLinkTrackingSystemService::initialize()
145 {
146  //emit connectToServer();
147 }
148 
149 void OpenIGTLinkTrackingSystemService::uninitialize()
150 {
151  emit disconnectFromServer();
152 }
153 
154 void OpenIGTLinkTrackingSystemService::startTracking()
155 {
156  //emit startListenToServer();
157  //emit connectToServer();
158 }
159 
160 void OpenIGTLinkTrackingSystemService::stopTracking()
161 {
162  //emit stopListenToServer();
163  emit disconnectFromServer();
164 }
165 
166 void OpenIGTLinkTrackingSystemService::serverIsConfigured()
167 {
168  this->internalSetState(Tool::tsCONFIGURED);
169 }
170 
171 void OpenIGTLinkTrackingSystemService::serverIsDeconfigured()
172 {
173  this->internalSetState(Tool::tsNONE);
174 }
175 
176 void OpenIGTLinkTrackingSystemService::serverIsConnected()
177 {
178  this->internalSetState(Tool::tsINITIALIZED);
179  this->internalSetState(Tool::tsTRACKING);
180 }
181 
182 void OpenIGTLinkTrackingSystemService::serverIsDisconnected()
183 {
184  this->internalSetState(Tool::tsCONFIGURED);
185  this->internalSetState(Tool::tsINITIALIZED);
186 }
187 
188 void OpenIGTLinkTrackingSystemService::receiveTransform(QString devicename, Transform3D transform, double timestamp)
189 {
190  CX_LOG_DEBUG() << "transform";
191  OpenIGTLinkToolPtr tool = this->getTool(devicename);
192  tool->toolTransformAndTimestampSlot(transform, timestamp);
193 }
194 
195 void OpenIGTLinkTrackingSystemService::receiveCalibration(QString devicename, Transform3D calibration)
196 {
197  OpenIGTLinkToolPtr tool = this->getTool(devicename);
198  tool->setCalibration_sMt(calibration);
199 }
200 
201 void OpenIGTLinkTrackingSystemService::receiveProbedefinition(QString devicename, ProbeDefinitionPtr definition)
202 {
203  OpenIGTLinkToolPtr tool = this->getTool(devicename);
204  ProbePtr probe = tool->getProbe();
205  ProbeDefinition old_def = probe->getProbeDefinition();
206  definition->setUid(old_def.getUid());
207  definition->applySoundSpeedCompensationFactor(old_def.getSoundSpeedCompensationFactor());
208 
209  probe->setProbeDefinition(*(definition.get()));
210 }
211 
212 void OpenIGTLinkTrackingSystemService::internalSetState(Tool::State state)
213 {
214  mState = state;
215  emit stateChanged();
216 }
217 
218 OpenIGTLinkToolPtr OpenIGTLinkTrackingSystemService::getTool(QString devicename)
219 {
220  OpenIGTLinkToolPtr retval;
221  std::map<QString, OpenIGTLinkToolPtr>::iterator it = mTools.find(devicename);
222  if(it == mTools.end())
223  {
224  retval = OpenIGTLinkToolPtr(new OpenIGTLinkTool(devicename));
225  mTools[devicename] = retval;
226  //todo: will this work?
227  emit stateChanged();
228  }
229  else
230  {
231  retval = it->second;
232  }
233 
234  return retval;
235 }
236 
237 
238 } /* namespace cx */
void probedefinition(QString devicename, ProbeDefinitionPtr definition)
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
void transform(QString devicename, Transform3D transform, double timestamp)
tsNONE
Not specified.
Interface to a tool, i.e. a pointer, US probe or similar.
Definition: cxTool.h:82
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
virtual void requestDisconnect()
not thread-safe: use invoke
configured with basic info
Definition: cxTool.h:96
virtual void requestConnect()
not thread-safe: use invoke
boost::shared_ptr< Probe > ProbePtr
Definition: cxProbe.h:93
boost::shared_ptr< class TrackerConfiguration > TrackerConfigurationPtr
std::vector< ToolPtr > toVector(std::map< QString, OpenIGTLinkToolPtr > map)
not available
Definition: cxTool.h:95
connected to hardware, if any, ready to use
Definition: cxTool.h:97
boost::shared_ptr< class NetworkConnectionHandle > NetworkConnectionHandlePtr
The NetworkConnection class handles incoming OpenIGTLink packages.
#define CX_LOG_DEBUG
Definition: cxLogger.h:110
void calibration(QString devicename, Transform3D calibration)
emitting tracking data
Definition: cxTool.h:98
boost::shared_ptr< class ProbeDefinition > ProbeDefinitionPtr
boost::shared_ptr< class Tool > ToolPtr