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