CustusX  2023.01.05-dev+develop.0da12
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"
33 
34 namespace cx
35 {
36 
38  mContext(context)
39 {
40  this->createInterconnectedDataAndSpace();
41 
42  connect(this->dataService().get(), &DataManager::dataAddedOrRemoved, this, &PatientModelService::dataAddedOrRemoved);
43  connect(this->dataService().get(), &DataManager::rMprChanged, this, &PatientModelService::rMprChanged);
44  connect(this->dataService().get(), &DataManager::streamLoaded, this, &PatientModelService::streamLoaded);
46 
47  connect(this->dataService().get(), &DataManager::centerChanged, this, &PatientModelService::centerChanged);
48  connect(this->dataService().get(), &DataManager::operatingTableChanged, this, &PatientModelService::operatingTableChanged);
50 
51  connect(this->patientData().get(), &PatientData::patientChanged, this, &PatientModelService::patientChanged);
52 
53  connect(mTrackingService.get(), &TrackingService::stateChanged, this, &PatientModelImplService::probesChanged);
54 
55  mUnavailableData.clear();
56 }
57 
58 void PatientModelImplService::createInterconnectedDataAndSpace()
59 {
60  // prerequisites:
61  mTrackingService = TrackingServiceProxy::create(mContext);
62 
63  mVideoService = VideoServiceProxy::create(mContext);
64  mFileManagerService = FileManagerServiceProxy::create(mContext);
65 
66  // build object(s):
67  PatientModelServicePtr patientModelService = PatientModelServiceProxy::create(mContext);
69 
70  mActiveData.reset(new ActiveData(patientModelService, session));
71  mDataService = DataManagerImpl::create(mActiveData);
72 
73  SpaceProviderPtr spaceProvider(new cx::SpaceProviderImpl(mTrackingService, patientModelService));
74  mDataService->setServices(spaceProvider, mFileManagerService);
75 
76  mDataFactory.reset(new DataFactory(patientModelService, spaceProvider));
77  mDataService->setDataFactory(mDataFactory);
78 
79  mPatientData.reset(new PatientData(mDataService, session, mFileManagerService));
80 }
81 
82 void PatientModelImplService::shutdownInterconnectedDataAndSpace()
83 {
84  requireUnique(mPatientData, "PatientData");
85  mPatientData.reset();
86 
87  // [HACK] break loop by removing connection to DataFactory and SpaceProvider
88  mDataService->setServices(SpaceProviderPtr(), FileManagerServicePtr());
89  mDataService->setDataFactory(DataFactoryPtr());
90  mDataService->clear();
91 
92  requireUnique(mDataFactory, "DataFactory");
93  mDataFactory.reset();
94 
95  requireUnique(mDataService, "DataService");
96  mDataService.reset();
97 }
98 
100 {
101  if(dataService())
102  {
103  disconnect(this->dataService().get(), &DataManager::dataAddedOrRemoved, this, &PatientModelService::dataAddedOrRemoved);
104  disconnect(this->dataService().get(), &DataManager::rMprChanged, this, &PatientModelService::rMprChanged);
105  disconnect(this->dataService().get(), &DataManager::streamLoaded, this, &PatientModelService::streamLoaded);
107 
108  disconnect(this->patientData().get(), &PatientData::patientChanged, this, &PatientModelService::patientChanged);
109  }
110 
111  this->shutdownInterconnectedDataAndSpace();
112 }
113 
115 {
116  QString outputBasePath = this->patientData()->getActivePatientFolder();
117 
118  this->dataService()->loadData(data, overWrite);
119  data->save(outputBasePath, mFileManagerService);
120 }
121 
122 DataPtr PatientModelImplService::createData(QString type, QString uid, QString name)
123 {
124  return dataService()->getDataFactory()->create(type, uid, name);
125 }
126 
127 std::map<QString, DataPtr> PatientModelImplService::getDatas(DataFilter filter) const
128 {
129  std::map<QString, DataPtr> retval = dataService()->getData();
130 
131  if (filter == HideUnavailable)
132  {
133  for(int i = 0; i < mUnavailableData.size(); ++i)
134  {
135  if (retval.count(mUnavailableData[i]))
136  retval.erase(mUnavailableData[i]);
137  }
138  }
139 
140  return retval;
141 }
142 
143 DataPtr PatientModelImplService::getData(const QString& uid) const
144 {
145  std::map<QString, DataPtr> dataMap = this->getDatas(AllData);
146  std::map<QString, DataPtr>::const_iterator iter = dataMap.find(uid);
147  if (iter == dataMap.end())
148  return DataPtr();
149  return iter->second;
150 }
151 
152 std::map<QString, DataPtr> PatientModelImplService::getChildren(QString parent_uid, QString of_type) const
153 {
154  std::map<QString, DataPtr> retval;
155  std::map<QString, DataPtr> dataMap = this->getDatas(AllData);
156  std::map<QString, DataPtr>::const_iterator iter = dataMap.begin();
157  for ( ; iter != dataMap.end(); ++iter)
158  {
159  if((iter->second->getParentSpace() == parent_uid))
160  if((of_type == "") || (iter->second->getType() == of_type))
161  retval[iter->first] = iter->second;
162  }
163 
164  return retval;
165 }
166 
168 {
169  return dataService()->getPatientLandmarks();
170 }
171 
172 std::map<QString, LandmarkProperty> PatientModelImplService::getLandmarkProperties() const
173 {
174  return dataService()->getLandmarkProperties();
175 }
176 
177 void PatientModelImplService::setLandmarkName(QString uid, QString name)
178 {
179  dataService()->setLandmarkName(uid, name);
180 }
181 
183 {
184  return dataService()->get_rMpr();
185 }
186 
188 {
189  this->patientData()->autoSave();
190 }
191 
193 {
194  return false;
195 }
196 
197 void PatientModelImplService::makeAvailable(const QString &uid, bool available)
198 {
199  if(!available)
200  mUnavailableData.push_back(uid);
201  else
202  {
203  std::vector<QString>::iterator iter = std::find(mUnavailableData.begin(), mUnavailableData.end(), uid);
204  if(iter != mUnavailableData.end())
205  mUnavailableData.erase(iter);
206  }
207 }
208 
210 {
211  return dataService()->getClinicalApplication();
212 }
213 
214 void PatientModelImplService::setClinicalApplication(CLINICAL_VIEW application)
215 {
216  dataService()->setClinicalApplication(application);
217 }
218 
219 std::map<QString, VideoSourcePtr> PatientModelImplService::getStreams() const
220 {
221  return dataService()->getStreams();
222 }
223 
225 {
226  return this->patientData()->getActivePatientFolder();
227 }
228 
230 {
231  return this->patientData()->isPatientValid();
232 }
233 
234 DataPtr PatientModelImplService::importData(QString fileName, QString &infoText)
235 {
236  return this->patientData()->importData(fileName, infoText);
237 }
238 
239 void PatientModelImplService::exportPatient(PATIENT_COORDINATE_SYSTEM externalSpace)
240 {
241  this->patientData()->exportPatient(externalSpace);
242 }
243 
245 {
246  this->patientData()->removeData(uid);
247 }
248 
250 {
251  return dataService()->getPresetTransferFunctions3D();
252 }
253 
255 {
256  this->dataService()->setCenter(center);
257 }
258 
260 {
261  return this->dataService()->getCenter();
262 }
263 
265 {
266  this->dataService()->setOperatingTable(ot);
267 }
268 
270 {
271  return this->dataService()->getOperatingTable();
272 }
273 
275 {
276  return dataService()->addLandmark();
277 }
278 
280 {
281  return dataService()->deleteLandmarks();
282 }
283 
284 void PatientModelImplService::setLandmarkActive(QString uid, bool active)
285 {
286  dataService()->setLandmarkActive(uid, active);
287 }
288 
290 {
291  return this->dataService()->get_rMpr_History();
292 }
293 
295 {
296  return mActiveData;
297 }
298 
299 DataServicePtr PatientModelImplService::dataService() const
300 {
301  return mDataService;
302 }
303 
304 PatientDataPtr PatientModelImplService::patientData() const
305 {
306  return mPatientData;
307 }
308 
309 void PatientModelImplService::probesChanged()
310 {
311  this->disconnectProbes();
312  mProbeTools.clear();
313  this->connectProbes();
314 }
315 
316 void PatientModelImplService::disconnectProbes()
317 {
318  for (std::map<QString, ToolPtr>::const_iterator iter = mProbeTools.begin(); iter != mProbeTools.end(); ++iter)
319  {
320  ProbePtr probe = iter->second->getProbe();
321  if(probe)
322  disconnect(probe.get(), &Probe::videoSourceAdded, this, &PatientModelImplService::videoSourceAdded);
323  else
324  CX_LOG_WARNING() << "PatientModelImplService::disconnectProbes: Tool is not a probe: " << iter->second->getUid();
325  }
326 }
327 
328 void PatientModelImplService::connectProbes()
329 {
330  ToolMap tools = mTrackingService->getTools();
331  for (ToolMap::const_iterator iter = tools.begin(); iter != tools.end(); ++iter)
332  {
333  ToolPtr tool = iter->second;
334  ProbePtr probe = tool->getProbe();
335  if(tool->getProbe())
336  {
337  mProbeTools[iter->first] = tool;
338  connect(probe.get(), &Probe::videoSourceAdded, this, &PatientModelImplService::videoSourceAdded);
339  }
340  }
341 }
342 
343 void PatientModelImplService::videoSourceAdded(VideoSourcePtr source)
344 {
345  ToolPtr tool = this->getProbeTool(source->getUid());
346  if(!tool)
347  return;
348 
349  QString uid = source->getUid() + tool->getUid();
350  QString name = source->getName() + " - " + tool->getName();
351  TrackedStreamPtr trackedStream = this->dataService()->getTrackedStream(uid);
352  if (!trackedStream)
353  trackedStream = this->createSpecificData<TrackedStream>(uid, name);
354  trackedStream->setProbeTool(tool);
355  trackedStream->setVideoSource(source);
356  trackedStream->setSpaceProvider(mDataService->getSpaceProvider());
357 
358  //Only load trackedStream, don't save it
359  this->dataService()->loadData(trackedStream);
361  this->reEmitActiveTrackedStream(trackedStream);
362 }
363 
364 void PatientModelImplService::reEmitActiveTrackedStream(TrackedStreamPtr trackedStream)
365 {
366  if(mActiveData->getActive<TrackedStream>() == trackedStream)
367  mActiveData->setActive(trackedStream);
368 }
369 
370 ToolPtr PatientModelImplService::getProbeTool(QString videoSourceUid)
371 {
372  for (std::map<QString, ToolPtr>::const_iterator iter = mProbeTools.begin(); iter != mProbeTools.end(); ++iter)
373  {
374  ToolPtr tool = iter->second;
375  ProbePtr probe = tool->getProbe();
376  if(probe && probe->getAvailableVideoSources().contains(videoSourceUid))
377  return tool;
378  }
379  reportWarning("Found no probe for stream" + videoSourceUid);
380  return ToolPtr();
381 }
382 
383 } /* namespace cx */
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
boost::shared_ptr< class FileManagerService > FileManagerServicePtr
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)
virtual void insertData(DataPtr data, bool overWrite=false)
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:41
virtual void makeAvailable(const QString &uid, bool available)
Exclude this data from getDatas()
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
static FileManagerServicePtr create(ctkPluginContext *context)
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:80
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