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