NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxSpaceProviderImpl.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 #include "cxSpaceProviderImpl.h"
33 
34 #include "cxPatientModelService.h"
35 #include "cxTrackingService.h"
36 #include "cxData.h"
37 #include "cxImage.h"
38 #include "vtkImageData.h"
39 #include "cxLogger.h"
40 #include "cxSpaceListenerImpl.h"
41 #include "cxTool.h"
42 #include "cxActiveData.h"
43 
44 
45 namespace cx
46 {
47 
49  mTrackingService(trackingService),
50  mDataManager(dataManager)
51 {
52 // connect(mTrackingService.get(), SIGNAL(stateChanged()), this, SIGNAL(spaceAddedOrRemoved()));
53  connect(mTrackingService.get(), &TrackingService::stateChanged, this, &SpaceProvider::spaceAddedOrRemoved);
55 }
56 
58 {
59  return SpaceListenerPtr(new SpaceListenerImpl(mTrackingService, mDataManager));
60 }
61 
62 std::vector<CoordinateSystem> SpaceProviderImpl::getSpacesToPresentInGUI()
63 {
64  std::vector<CoordinateSystem> retval;
65  retval.push_back(CoordinateSystem(csREF));
66  retval.push_back(CoordinateSystem(csPATIENTREF));
67 
68  // alias for the currently active tool:
69  retval.push_back(CoordinateSystem(csDATA, "active"));
70  retval.push_back(CoordinateSystem(csDATA_VOXEL, "active"));
71 
72  // alias for the currently active tool:
73  retval.push_back(CoordinateSystem(csTOOL, "active"));
74  retval.push_back(CoordinateSystem(csSENSOR, "active"));
75  retval.push_back(CoordinateSystem(csTOOL_OFFSET, "active"));
76 
77  std::map<QString, DataPtr> data = mDataManager->getData();
78  for (std::map<QString, DataPtr>::iterator i=data.begin(); i!=data.end(); ++i)
79  {
80  retval.push_back(CoordinateSystem(csDATA, i->second->getSpace()));
81  retval.push_back(CoordinateSystem(csDATA_VOXEL, i->second->getSpace()));
82  }
83 
84  std::map<QString, ToolPtr> tools = mTrackingService->getTools();
85  for (std::map<QString, ToolPtr>::iterator i=tools.begin(); i!=tools.end(); ++i)
86  {
87  retval.push_back(CoordinateSystem(csTOOL, i->first));
88  retval.push_back(CoordinateSystem(csSENSOR, i->first));
89  retval.push_back(CoordinateSystem(csTOOL_OFFSET, i->first));
90  }
91 
92  return retval;
93 }
94 
96 {
97  std::map<QString, QString> retval;
98 
99  retval["active"] = "active";
100 
101  std::map<QString, DataPtr> data = mDataManager->getData();
102  for (std::map<QString, DataPtr>::iterator i=data.begin(); i!=data.end(); ++i)
103  {
104  retval[i->second->getSpace()] = i->second->getName();
105  }
106 
107  std::map<QString, ToolPtr> tools = mTrackingService->getTools();
108  for (std::map<QString, ToolPtr>::iterator i=tools.begin(); i!=tools.end(); ++i)
109  {
110  retval[i->first] = i->second->getName();
111  }
112 
113  return retval;
114 }
115 
117 {
118  Transform3D toMfrom = this->getActiveToolTipTransform(to, useOffset);
119  return toMfrom.coord(Vector3D(0,0,0));
120 }
121 
122 CoordinateSystem SpaceProviderImpl::getToolCoordinateSystem(ToolPtr tool)
123 {
124  QString uid = tool->getUid();
125 
126  CoordinateSystem retval(csTOOL, uid);
127  return retval;
128 }
129 
133 {
134  ToolPtr tool = mTrackingService->getActiveTool();
135  if (!tool)
136  return Transform3D::Identity();
137 
138  COORDINATE_SYSTEM target;
139  if (useOffset)
140  target = csTOOL_OFFSET;
141  else
142  target = csTOOL;
143  CoordinateSystem from = CoordinateSystem(target, tool->getUid());
144 
145 // CoordinateSystem from = getToolCoordinateSystem(tool);
146  Transform3D retval = this->get_toMfrom(from, to);
147  return retval;
148 }
149 
151 {
152  Transform3D to_M_from = get_rMfrom(to).inv() * get_rMfrom(from);
153  return to_M_from;
154 }
155 
156 Transform3D SpaceProviderImpl::get_rMfrom(CoordinateSystem from)
157 {
158  Transform3D rMfrom = Transform3D::Identity();
159 
160  switch(from.mId)
161  {
162  case csREF:
163  rMfrom = get_rMr();
164  break;
165  case csDATA:
166  rMfrom = get_rMd(from.mRefObject);
167  break;
168  case csPATIENTREF:
169  rMfrom = get_rMpr();
170  break;
171  case csTOOL:
172  rMfrom = get_rMt(from.mRefObject);
173  break;
174  case csSENSOR:
175  rMfrom = get_rMs(from.mRefObject);
176  break;
177  case csTOOL_OFFSET:
178  rMfrom = get_rMto(from.mRefObject);
179  break;
180  case csDATA_VOXEL:
181  rMfrom = get_rMdv(from.mRefObject);
182  break;
183  default:
184 
185  break;
186  };
187 
188  return rMfrom;
189 }
190 
192 {
193  CoordinateSystem retval(csCOUNT);
194  if (!tool)
195  return retval;
196 
197  retval.mId = csSENSOR;
198  retval.mRefObject = tool->getUid();
199 
200  return retval;
201 }
202 
204 {
205  CoordinateSystem retval(csCOUNT);
206  if (!tool)
207  return retval;
208 
209  ToolPtr refTool = mTrackingService->getReferenceTool();
210  if (refTool && (tool == refTool))
211  {
212  retval.mId = csPATIENTREF;
213  }
214  else
215  retval.mId = csTOOL;
216 
217  retval.mRefObject = tool->getUid();
218 
219  return retval;
220 }
221 
223 {
224  CoordinateSystem retval(csCOUNT);
225  if (!tool)
226  return retval;
227 
228  ToolPtr refTool = mTrackingService->getReferenceTool();
229  if (refTool && (tool == refTool))
230  {
231  retval.mId = csPATIENTREF;
232  }
233  else
234  retval.mId = csTOOL_OFFSET;
235 
236  retval.mRefObject = tool->getUid();
237 
238  return retval;
239 }
240 
242 {
243  CoordinateSystem retval(csCOUNT);
244  if (!data)
245  return retval;
246 
247  retval.mId = csDATA;
248  retval.mRefObject = data->getUid();
249 
250  return retval;
251 }
252 
254 {
256  return pr;
257 }
258 
260 {
262  return r;
263 }
264 
266 {
267  if (space.mRefObject!="active")
268  return space;
269 
270  if (( space.mId==csDATA )||( space.mId==csDATA_VOXEL ))
271  {
272  DataPtr data = mDataManager->getActiveData()->getActiveUsingRegexp("image|trackedStream|mesh");
273 // DataPtr data = mDataManager->getActiveData()->getActive();
274  space.mRefObject = (data!=0) ? data->getUid() : "";
275  }
276  else if (( space.mId==csTOOL )||( space.mId==csSENSOR )||( space.mId==csTOOL_OFFSET))
277  {
278  ToolPtr tool = mTrackingService->getActiveTool();
279  space.mRefObject = (tool!=0) ? tool->getUid() : "";
280  }
281 
282  return space;
283 }
284 
285 Transform3D SpaceProviderImpl::get_rMr()
286 {
287  return Transform3D::Identity(); // ref_M_ref
288 }
289 
290 Transform3D SpaceProviderImpl::get_rMd(QString uid)
291 {
292  if (!mDataManager->isPatientValid())
293  return Transform3D::Identity();
294 
295  DataPtr data = mDataManager->getData(uid);
296 
297  if (!data && uid=="active")
298  {
299  ActiveDataPtr activeData = mDataManager->getActiveData();
300  data = activeData->getActive<Image>();
301  }
302 
303  if(!data)
304  {
305  reportWarning("Could not find data with uid: "+uid+". Can not find transform to unknown coordinate system, returning identity!");
306  return Transform3D::Identity();
307  }
308  return data->get_rMd(); // ref_M_d
309 }
310 
311 Transform3D SpaceProviderImpl::get_rMdv(QString uid)
312 {
313  if (!mDataManager->isPatientValid())
314  return Transform3D::Identity();
315 
316  DataPtr data = mDataManager->getData(uid);
317 
318  if (!data && uid=="active")
319  {
320  ActiveDataPtr activeData = mDataManager->getActiveData();
321  data = activeData->getActive<Image>();
322  }
323 
324  if(!data)
325  {
326  reportWarning("Could not find data with uid: "+uid+". Can not find transform to unknown coordinate system, returning identity!");
327  return Transform3D::Identity();
328  }
329 
330  ImagePtr image = boost::dynamic_pointer_cast<Image>(data);
331  if (!image)
332  return data->get_rMd();
333  return data->get_rMd()*createTransformScale(Vector3D(image->getBaseVtkImageData()->GetSpacing())); // ref_M_d
334 }
335 
337 {
338  Transform3D rMpr = mDataManager->get_rMpr();
339  return rMpr; //ref_M_pr
340 }
341 
342 Transform3D SpaceProviderImpl::get_rMt(QString uid)
343 {
344  ToolPtr tool = mTrackingService->getTool(uid);
345 
346  if (!tool && uid=="active")
347  tool = mTrackingService->getActiveTool();
348 
349  if(!tool)
350  {
351  reportWarning("Could not find tool with uid: "+uid+". Can not find transform to unknown coordinate system, returning identity!");
352  return Transform3D::Identity();
353  }
354  return get_rMpr() * tool->get_prMt(); // ref_M_t
355 }
356 
357 Transform3D SpaceProviderImpl::get_rMto(QString uid)
358 {
359  ToolPtr tool = mTrackingService->getTool(uid);
360 
361  if (!tool && uid=="active")
362  tool = mTrackingService->getActiveTool();
363 
364  if(!tool)
365  {
366  reportWarning("Could not find tool with uid: "+uid+". Can not find transform to unknown coordinate system, returning identity!");
367  return Transform3D::Identity();
368  }
369 
370  double offset = tool->getTooltipOffset();
371  Transform3D tMto = createTransformTranslate(Vector3D(0,0,offset));
372  return get_rMpr() * tool->get_prMt() * tMto; // ref_M_to
373 }
374 
375 Transform3D SpaceProviderImpl::get_rMs(QString uid)
376 {
377  ToolPtr tool = mTrackingService->getTool(uid);
378 
379  if (!tool && uid=="active")
380  tool = mTrackingService->getActiveTool();
381 
382  if(!tool)
383  {
384  reportWarning("Could not find tool with uid: "+uid+". Can not find transform to unknown coordinate system, returning identity!");
385  return Transform3D::Identity();
386  }
387 
388  Transform3D tMs = tool->getCalibration_sMt().inv();
389 
390  Transform3D rMpr = get_rMpr();
391  Transform3D prMt = tool->get_prMt();
392 
393  Transform3D rMs = rMpr * prMt * tMs;
394 
395  return rMs; //ref_M_s
396 }
397 
398 
399 } // namespace cx
400 
401 
virtual CoordinateSystem getTO(ToolPtr tool)
tool offset coordinate system
virtual Transform3D get_toMfrom(CoordinateSystem from, CoordinateSystem to)
to_M_from
virtual CoordinateSystem getS(ToolPtr tool)
tools sensor coordinate system
virtual CoordinateSystem getT(ToolPtr tool)
tools coordinate system
Transform3D createTransformScale(const Vector3D &scale_)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class TrackingService > TrackingServicePtr
csSENSOR
a tools sensor space (s)
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
csDATA_VOXEL
the data voxel space (dv)
void spaceAddedOrRemoved()
csREF
the data reference space (r) using LPS (left-posterior-superior) coordinates.
boost::shared_ptr< class ActiveData > ActiveDataPtr
Definition: cxColorWidget.h:42
Class that listens to changes in a coordinate system, and emits a signal if that system changes...
virtual CoordinateSystem getD(DataPtr data)
datas coordinate system static CoordinateSystem getPr(); ///<patient references coordinate system ...
virtual Transform3D get_rMpr()
csDATA
a datas space (d)
COORDINATE_SYSTEM mId
the type of coordinate system
csPATIENTREF
the patient/tool reference space (pr)
boost::shared_ptr< class Data > DataPtr
virtual CoordinateSystem getPr()
virtual Transform3D getActiveToolTipTransform(CoordinateSystem to, bool useOffset=false)
Get toMt, where t is active tool.
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
virtual std::map< QString, QString > getDisplayNamesForCoordRefObjects()
Transform3D createTransformTranslate(const Vector3D &translation)
Identification of a Coordinate system.
csTOOL_OFFSET
the tool space t with a virtual offset added along the z axis. (to)
QString mRefObject
for tool, sensor and data we need a object uid to define the coordinate system
virtual CoordinateSystem convertToSpecific(CoordinateSystem space)
convert "active" references to specific tool/data instances
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
virtual std::vector< CoordinateSystem > getSpacesToPresentInGUI()
virtual CoordinateSystem getR()
data references coordinate system
boost::shared_ptr< class SpaceListener > SpaceListenerPtr
cxLogicManager_EXPORT TrackingServicePtr trackingService()
virtual SpaceListenerPtr createListener()
csTOOL
a tools rspace (t)
SpaceProviderImpl(TrackingServicePtr trackingService, PatientModelServicePtr dataManager)
virtual Vector3D getActiveToolTipPoint(CoordinateSystem to, bool useOffset=false)
P_to, active tools current point in coord.
boost::shared_ptr< class Tool > ToolPtr