Fraxinus  18.10
An IGT application
cxPatientModelImplService.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 
13 
14 #include <ctkPluginContext.h>
15 #include <vtkImageData.h>
16 
17 #include "cxData.h"
18 #include "cxPatientData.h"
20 #include "cxDataFactory.h"
21 #include "cxDataManagerImpl.h"
22 #include "cxSharedPointerChecker.h"
23 #include "cxTrackingServiceProxy.h"
25 #include "cxSpaceProviderImpl.h"
27 #include "cxTrackedStream.h"
28 #include "cxReporter.h"
29 #include "cxVideoSource.h"
30 #include "cxActiveData.h"
31 #include "cxVideoServiceProxy.h"
32 
33 namespace cx
34 {
35 
37  mContext(context)
38 {
39  this->createInterconnectedDataAndSpace();
40 
41  connect(this->dataService().get(), &DataManager::dataAddedOrRemoved, this, &PatientModelService::dataAddedOrRemoved);
42  connect(this->dataService().get(), &DataManager::rMprChanged, this, &PatientModelService::rMprChanged);
43  connect(this->dataService().get(), &DataManager::streamLoaded, this, &PatientModelService::streamLoaded);
45 
46  connect(this->dataService().get(), &DataManager::centerChanged, this, &PatientModelService::centerChanged);
47  connect(this->dataService().get(), &DataManager::operatingTableChanged, this, &PatientModelService::operatingTableChanged);
49 
50  connect(this->patientData().get(), &PatientData::patientChanged, this, &PatientModelService::patientChanged);
51 
52  connect(mTrackingService.get(), &TrackingService::stateChanged, this, &PatientModelImplService::probesChanged);
53 
54  mUnavailableData.clear();
55 }
56 
57 void PatientModelImplService::createInterconnectedDataAndSpace()
58 {
59  // prerequisites:
60  mTrackingService = TrackingServiceProxy::create(mContext);
61 
62  mVideoService = VideoServiceProxy::create(mContext);
63 
64  // build object(s):
65  PatientModelServicePtr patientModelService = PatientModelServiceProxy::create(mContext);
67 
68  mActiveData.reset(new ActiveData(patientModelService, session));
69  mDataService = DataManagerImpl::create(mActiveData);
70 
71  SpaceProviderPtr spaceProvider(new cx::SpaceProviderImpl(mTrackingService, patientModelService));
72  mDataService->setSpaceProvider(spaceProvider);
73 
74  mDataFactory.reset(new DataFactory(patientModelService, spaceProvider));
75  mDataService->setDataFactory(mDataFactory);
76 
77  mPatientData.reset(new PatientData(mDataService, session));
78 }
79 
80 void PatientModelImplService::shutdownInterconnectedDataAndSpace()
81 {
82  requireUnique(mPatientData, "PatientData");
83  mPatientData.reset();
84 
85  // [HACK] break loop by removing connection to DataFactory and SpaceProvider
86  mDataService->setSpaceProvider(SpaceProviderPtr());
87  mDataService->setDataFactory(DataFactoryPtr());
88  mDataService->clear();
89 
90  requireUnique(mDataFactory, "DataFactory");
91  mDataFactory.reset();
92 
93  requireUnique(mDataService, "DataService");
94  mDataService.reset();
95 }
96 
98 {
99  if(dataService())
100  {
101  disconnect(this->dataService().get(), &DataManager::dataAddedOrRemoved, this, &PatientModelService::dataAddedOrRemoved);
102  disconnect(this->dataService().get(), &DataManager::rMprChanged, this, &PatientModelService::rMprChanged);
103  disconnect(this->dataService().get(), &DataManager::streamLoaded, this, &PatientModelService::streamLoaded);
105 
106  disconnect(this->patientData().get(), &PatientData::patientChanged, this, &PatientModelService::patientChanged);
107  }
108 
109  this->shutdownInterconnectedDataAndSpace();
110 }
111 
113 {
114  QString outputBasePath = this->patientData()->getActivePatientFolder();
115 
116  this->dataService()->loadData(data);
117  data->save(outputBasePath);
118 }
119 
120 DataPtr PatientModelImplService::createData(QString type, QString uid, QString name)
121 {
122  return dataService()->getDataFactory()->create(type, uid, name);
123 }
124 
125 std::map<QString, DataPtr> PatientModelImplService::getDatas(DataFilter filter) const
126 {
127  std::map<QString, DataPtr> retval = dataService()->getData();
128 
129  if (filter == HideUnavailable)
130  {
131  for(int i = 0; i < mUnavailableData.size(); ++i)
132  {
133  if (retval.count(mUnavailableData[i]))
134  retval.erase(mUnavailableData[i]);
135  }
136  }
137 
138  return retval;
139 }
140 
141 DataPtr PatientModelImplService::getData(const QString& uid) const
142 {
143  std::map<QString, DataPtr> dataMap = this->getDatas(AllData);
144  std::map<QString, DataPtr>::const_iterator iter = dataMap.find(uid);
145  if (iter == dataMap.end())
146  return DataPtr();
147  return iter->second;
148 }
149 
150 std::map<QString, DataPtr> PatientModelImplService::getChildren(QString parent_uid, QString of_type) const
151 {
152  std::map<QString, DataPtr> retval;
153  std::map<QString, DataPtr> dataMap = this->getDatas(AllData);
154  std::map<QString, DataPtr>::const_iterator iter = dataMap.begin();
155  for ( ; iter != dataMap.end(); ++iter)
156  {
157  if((iter->second->getParentSpace() == parent_uid))
158  if((of_type == "") || (iter->second->getType() == of_type))
159  retval[iter->first] = iter->second;
160  }
161 
162  return retval;
163 }
164 
166 {
167  return dataService()->getPatientLandmarks();
168 }
169 
170 std::map<QString, LandmarkProperty> PatientModelImplService::getLandmarkProperties() const
171 {
172  return dataService()->getLandmarkProperties();
173 }
174 
175 void PatientModelImplService::setLandmarkName(QString uid, QString name)
176 {
177  dataService()->setLandmarkName(uid, name);
178 }
179 
181 {
182  return dataService()->get_rMpr();
183 }
184 
186 {
187  this->patientData()->autoSave();
188 }
189 
191 {
192  return false;
193 }
194 
195 void PatientModelImplService::makeAvailable(const QString &uid, bool available)
196 {
197  if(!available)
198  mUnavailableData.push_back(uid);
199  else
200  {
201  std::vector<QString>::iterator iter = std::find(mUnavailableData.begin(), mUnavailableData.end(), uid);
202  if(iter != mUnavailableData.end())
203  mUnavailableData.erase(iter);
204  }
205 }
206 
208 {
209  return dataService()->getClinicalApplication();
210 }
211 
212 void PatientModelImplService::setClinicalApplication(CLINICAL_VIEW application)
213 {
214  dataService()->setClinicalApplication(application);
215 }
216 
217 std::map<QString, VideoSourcePtr> PatientModelImplService::getStreams() const
218 {
219  return dataService()->getStreams();
220 }
221 
223 {
224  return this->patientData()->getActivePatientFolder();
225 }
226 
228 {
229  return this->patientData()->isPatientValid();
230 }
231 
232 DataPtr PatientModelImplService::importData(QString fileName, QString &infoText)
233 {
234  return this->patientData()->importData(fileName, infoText);
235 }
236 
237 void PatientModelImplService::exportPatient(PATIENT_COORDINATE_SYSTEM externalSpace)
238 {
239  this->patientData()->exportPatient(externalSpace);
240 }
241 
243 {
244  this->patientData()->removeData(uid);
245 }
246 
248 {
249  return dataService()->getPresetTransferFunctions3D();
250 }
251 
253 {
254  this->dataService()->setCenter(center);
255 }
256 
258 {
259  return this->dataService()->getCenter();
260 }
261 
263 {
264  this->dataService()->setOperatingTable(ot);
265 }
266 
268 {
269  return this->dataService()->getOperatingTable();
270 }
271 
273 {
274  return dataService()->addLandmark();
275 }
276 
278 {
279  return dataService()->deleteLandmarks();
280 }
281 
282 void PatientModelImplService::setLandmarkActive(QString uid, bool active)
283 {
284  dataService()->setLandmarkActive(uid, active);
285 }
286 
288 {
289  return this->dataService()->get_rMpr_History();
290 }
291 
293 {
294  return mActiveData;
295 }
296 
297 DataServicePtr PatientModelImplService::dataService() const
298 {
299  return mDataService;
300 }
301 
302 PatientDataPtr PatientModelImplService::patientData() const
303 {
304  return mPatientData;
305 }
306 
307 void PatientModelImplService::probesChanged()
308 {
309  this->disconnectProbes();
310  mProbeTools.clear();
311  this->connectProbes();
312 }
313 
314 void PatientModelImplService::disconnectProbes()
315 {
316  for (std::map<QString, ToolPtr>::const_iterator iter = mProbeTools.begin(); iter != mProbeTools.end(); ++iter)
317  {
318  ProbePtr probe = iter->second->getProbe();
319  if(probe)
320  disconnect(probe.get(), &Probe::videoSourceAdded, this, &PatientModelImplService::videoSourceAdded);
321  else
322  CX_LOG_WARNING() << "PatientModelImplService::disconnectProbes: Tool is not a probe: " << iter->second->getUid();
323  }
324 }
325 
326 void PatientModelImplService::connectProbes()
327 {
328  ToolMap tools = mTrackingService->getTools();
329  for (ToolMap::const_iterator iter = tools.begin(); iter != tools.end(); ++iter)
330  {
331  ToolPtr tool = iter->second;
332  ProbePtr probe = tool->getProbe();
333  if(tool->getProbe())
334  {
335  mProbeTools[iter->first] = tool;
336  connect(probe.get(), &Probe::videoSourceAdded, this, &PatientModelImplService::videoSourceAdded);
337  }
338  }
339 }
340 
341 void PatientModelImplService::videoSourceAdded(VideoSourcePtr source)
342 {
343  ToolPtr tool = this->getProbeTool(source->getUid());
344  if(!tool)
345  return;
346 
347  QString uid = source->getUid() + tool->getUid();
348  QString name = source->getName() + " - " + tool->getName();
349  TrackedStreamPtr trackedStream = this->dataService()->getTrackedStream(uid);
350  if (!trackedStream)
351  trackedStream = this->createSpecificData<TrackedStream>(uid, name);
352  trackedStream->setProbeTool(tool);
353  trackedStream->setVideoSource(source);
354  trackedStream->setSpaceProvider(mDataService->getSpaceProvider());
355 
356  //Only load trackedStream, don't save it
357  this->dataService()->loadData(trackedStream);
359  this->reEmitActiveTrackedStream(trackedStream);
360 }
361 
362 void PatientModelImplService::reEmitActiveTrackedStream(TrackedStreamPtr trackedStream)
363 {
364  if(mActiveData->getActive<TrackedStream>() == trackedStream)
365  mActiveData->setActive(trackedStream);
366 }
367 
368 ToolPtr PatientModelImplService::getProbeTool(QString videoSourceUid)
369 {
370  for (std::map<QString, ToolPtr>::const_iterator iter = mProbeTools.begin(); iter != mProbeTools.end(); ++iter)
371  {
372  ToolPtr tool = iter->second;
373  ProbePtr probe = tool->getProbe();
374  if(probe && probe->getAvailableVideoSources().contains(videoSourceUid))
375  return tool;
376  }
377  reportWarning("Found no probe for stream" + videoSourceUid);
378  return ToolPtr();
379 }
380 
381 } /* namespace cx */
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
void rMprChanged()
emitted when the transformation between patient reference and (data) reference is set ...
virtual void setCenter(const Vector3D &center)
boost::shared_ptr< class RegistrationHistory > RegistrationHistoryPtr
Definition: cxDataManager.h:37
OperatingTable getOperatingTable() const
virtual PresetTransferFunctions3DPtr getPresetTransferFunctions3D() const
void operatingTableChanged()
void landmarkPropertiesChanged()
emitted when global info about a landmark changed
boost::shared_ptr< class TrackedStream > TrackedStreamPtr
virtual Transform3D get_rMpr() const
get the patient registration transform
Provides the last active data of warious types.
Definition: cxActiveData.h:35
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
virtual DataPtr importData(QString fileName, QString &infoText)
boost::shared_ptr< class TransferFunctions3DPresets > PresetTransferFunctions3DPtr
Definition: cxDataManager.h:36
virtual DataPtr createData(QString type, QString uid, QString name)
Functionality for storing patient data in a folder on the disk and access to these data...
Definition: cxPatientData.h:46
static SessionStorageServicePtr create(ctkPluginContext *pluginContext)
boost::shared_ptr< class ActiveData > ActiveDataPtr
Definition: cxColorWidget.h:21
void landmarkPropertiesChanged()
emitted when global info about a landmark changed
virtual ActiveDataPtr getActiveData() const
void centerChanged()
emitted when center is changed.
static VideoServicePtr create(ctkPluginContext *pluginContext)
A data set for video streams (2D/3D).
void centerChanged()
emitted when center is changed.
boost::shared_ptr< class DataManager > DataServicePtr
boost::shared_ptr< class Landmarks > LandmarksPtr
Definition: cxData.h:40
virtual void makeAvailable(const QString &uid, bool available)
Exclude this data from getDatas()
virtual void insertData(DataPtr data)
static TrackingServicePtr create(ctkPluginContext *pluginContext)
void setOperatingTable(const OperatingTable &ot)
boost::shared_ptr< Probe > ProbePtr
Definition: cxProbe.h:72
static PatientModelServicePtr create(ctkPluginContext *pluginContext)
boost::shared_ptr< class Data > DataPtr
void videoSourceAdded(VideoSourcePtr source)
static DataManagerImplPtr create(ActiveDataPtr activeData)
virtual void removeData(QString uid)
virtual std::map< QString, DataPtr > getChildren(QString parent_uid, QString of_type="") const
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
void dataAddedOrRemoved()
std::map< QString, ToolPtr > ToolMap
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
boost::shared_ptr< class VideoSource > VideoSourcePtr
virtual Vector3D getCenter() const
current common center point for user viewing.
virtual QString getActivePatientFolder() const
void patientChanged()
virtual LandmarksPtr getPatientLandmarks() const
landmark defined in patient space
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
virtual CLINICAL_VIEW getClinicalApplication() const
virtual void setLandmarkActive(QString uid, bool active)
virtual std::map< QString, VideoSourcePtr > getStreams() const
virtual void exportPatient(PATIENT_COORDINATE_SYSTEM externalSpace)
#define CX_LOG_WARNING
Definition: cxLogger.h:98
boost::shared_ptr< PatientData > PatientDataPtr
Definition: cxPatientData.h:79
virtual std::map< QString, DataPtr > getDatas(DataFilter filter) const
PatientModelImplService(ctkPluginContext *context)
virtual RegistrationHistoryPtr get_rMpr_History() const
The OperatingTable class.
boost::shared_ptr< class DataFactory > DataFactoryPtr
Definition: cxDataManager.h:39
virtual std::map< QString, LandmarkProperty > getLandmarkProperties() const
virtual void setLandmarkName(QString uid, QString name)
void requireUnique(int use_count, QString objectName)
void clinicalApplicationChanged()
virtual void setClinicalApplication(CLINICAL_VIEW application)
boost::shared_ptr< class SessionStorageService > SessionStorageServicePtr
virtual DataPtr getData(const QString &uid) const
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr