CustusX  22.04-rc3
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 = mDataManager->getData(uid);
275 
276  if (!data && uid=="active")
277  {
278  ActiveDataPtr activeData = mDataManager->getActiveData();
279  data = activeData->getActive<Image>();
280  }
281 
282  if(!data)
283  {
284  reportWarning("Could not find data with uid: "+uid+". Can not find transform to unknown coordinate system, returning identity!");
285  return Transform3D::Identity();
286  }
287  return data->get_rMd(); // ref_M_d
288 }
289 
290 Transform3D SpaceProviderImpl::get_rMdv(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 
309  ImagePtr image = boost::dynamic_pointer_cast<Image>(data);
310  if (!image)
311  return data->get_rMd();
312  return data->get_rMd()*createTransformScale(Vector3D(image->getBaseVtkImageData()->GetSpacing())); // ref_M_d
313 }
314 
316 {
317  Transform3D rMpr = mDataManager->get_rMpr();
318  return rMpr; //ref_M_pr
319 }
320 
321 Transform3D SpaceProviderImpl::get_rMt(QString uid)
322 {
323  ToolPtr tool = mTrackingService->getTool(uid);
324 
325  if (!tool && uid=="active")
326  tool = mTrackingService->getActiveTool();
327 
328  if(!tool)
329  {
330  reportWarning("Could not find tool with uid: "+uid+". Can not find transform to unknown coordinate system, returning identity!");
331  return Transform3D::Identity();
332  }
333  return get_rMpr() * tool->get_prMt(); // ref_M_t
334 }
335 
336 Transform3D SpaceProviderImpl::get_rMto(QString uid)
337 {
338  ToolPtr tool = mTrackingService->getTool(uid);
339 
340  if (!tool && uid=="active")
341  tool = mTrackingService->getActiveTool();
342 
343  if(!tool)
344  {
345  reportWarning("Could not find tool with uid: "+uid+". Can not find transform to unknown coordinate system, returning identity!");
346  return Transform3D::Identity();
347  }
348 
349  double offset = tool->getTooltipOffset();
350  Transform3D tMto = createTransformTranslate(Vector3D(0,0,offset));
351  return get_rMpr() * tool->get_prMt() * tMto; // ref_M_to
352 }
353 
354 Transform3D SpaceProviderImpl::get_rMs(QString uid)
355 {
356  ToolPtr tool = mTrackingService->getTool(uid);
357 
358  if (!tool && uid=="active")
359  tool = mTrackingService->getActiveTool();
360 
361  if(!tool)
362  {
363  reportWarning("Could not find tool with uid: "+uid+". Can not find transform to unknown coordinate system, returning identity!");
364  return Transform3D::Identity();
365  }
366 
367  Transform3D tMs = tool->getCalibration_sMt().inv();
368 
369  Transform3D rMpr = get_rMpr();
370  Transform3D prMt = tool->get_prMt();
371 
372  Transform3D rMs = rMpr * prMt * tMs;
373 
374  return rMs; //ref_M_s
375 }
376 
377 
378 } // namespace cx
379 
380 
virtual CoordinateSystem getTO(ToolPtr tool)
tool offset coordinate system
virtual Transform3D get_toMfrom(CoordinateSystem from, CoordinateSystem to)
to_M_from
virtual Transform3D get_rMd() const
Definition: cxData.cpp:86
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)
Definition: cxDefinitions.h:90
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
csDATA_VOXEL
the data voxel space (dv)
Definition: cxDefinitions.h:90
void spaceAddedOrRemoved()
csREF
the data reference space (r) using LPS (left-posterior-superior) coordinates.
Definition: cxDefinitions.h:90
boost::shared_ptr< class ActiveData > ActiveDataPtr
Definition: cxColorWidget.h:21
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)
Definition: cxDefinitions.h:90
COORDINATE_SYSTEM mId
the type of coordinate system
csPATIENTREF
the patient/tool reference space (pr)
Definition: cxDefinitions.h:90
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:70
A volumetric data set.
Definition: cxImage.h:45
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)
Definition: cxDefinitions.h:90
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:42
virtual std::vector< CoordinateSystem > getSpacesToPresentInGUI()
virtual CoordinateSystem getR()
data references coordinate system
boost::shared_ptr< class SpaceListener > SpaceListenerPtr
virtual SpaceListenerPtr createListener()
csTOOL
a tools rspace (t)
Definition: cxDefinitions.h:90
SpaceProviderImpl(TrackingServicePtr trackingService, PatientModelServicePtr dataManager)
virtual Vector3D getActiveToolTipPoint(CoordinateSystem to, bool useOffset=false)
P_to, active tools current point in coord.
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr