CustusX  16.5.0-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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);
69 
70  connect(this->patientData().get(), &PatientData::patientChanged, this, &PatientModelService::patientChanged);
71 
72  connect(mTrackingService.get(), &TrackingService::stateChanged, this, &PatientModelImplService::probesChanged);
73 }
74 
75 void PatientModelImplService::createInterconnectedDataAndSpace()
76 {
77  // prerequisites:
78  mTrackingService = TrackingServiceProxy::create(mContext);
79 
80  mVideoService = VideoServiceProxy::create(mContext);
81 
82  // build object(s):
83  PatientModelServicePtr patientModelService = PatientModelServiceProxy::create(mContext);
85 
86  mActiveData.reset(new ActiveData(patientModelService, session));
87  mDataService = DataManagerImpl::create(mActiveData);
88 
89  SpaceProviderPtr spaceProvider(new cx::SpaceProviderImpl(mTrackingService, patientModelService));
90  mDataService->setSpaceProvider(spaceProvider);
91 
92  mDataFactory.reset(new DataFactory(patientModelService, spaceProvider));
93  mDataService->setDataFactory(mDataFactory);
94 
95  mPatientData.reset(new PatientData(mDataService, session));
96 }
97 
98 void PatientModelImplService::shutdownInterconnectedDataAndSpace()
99 {
100  requireUnique(mPatientData, "PatientData");
101  mPatientData.reset();
102 
103  // [HACK] break loop by removing connection to DataFactory and SpaceProvider
104  mDataService->setSpaceProvider(SpaceProviderPtr());
105  mDataService->setDataFactory(DataFactoryPtr());
106  mDataService->clear();
107 
108  requireUnique(mDataFactory, "DataFactory");
109  mDataFactory.reset();
110 
111  requireUnique(mDataService, "DataService");
112  mDataService.reset();
113 }
114 
116 {
117  if(dataService())
118  {
119  disconnect(this->dataService().get(), &DataManager::dataAddedOrRemoved, this, &PatientModelService::dataAddedOrRemoved);
120  disconnect(this->dataService().get(), &DataManager::rMprChanged, this, &PatientModelService::rMprChanged);
121  disconnect(this->dataService().get(), &DataManager::streamLoaded, this, &PatientModelService::streamLoaded);
123 
124  disconnect(this->patientData().get(), &PatientData::patientChanged, this, &PatientModelService::patientChanged);
125  }
126 
127  this->shutdownInterconnectedDataAndSpace();
128 }
129 
131 {
132  QString outputBasePath = this->patientData()->getActivePatientFolder();
133 
134  this->dataService()->loadData(data);
135  data->save(outputBasePath);
136 }
137 
138 DataPtr PatientModelImplService::createData(QString type, QString uid, QString name)
139 {
140  return dataService()->getDataFactory()->create(type, uid, name);
141 }
142 
143 std::map<QString, DataPtr> PatientModelImplService::getData() const
144 {
145  return dataService()->getData();
146 }
147 
148 DataPtr PatientModelImplService::getData(const QString& uid) const
149 {
150  std::map<QString, DataPtr> dataMap = this->getData();
151  std::map<QString, DataPtr>::const_iterator iter = dataMap.find(uid);
152  if (iter == dataMap.end())
153  return DataPtr();
154  return iter->second;
155 }
156 
158 {
159  return dataService()->getPatientLandmarks();
160 }
161 
162 std::map<QString, LandmarkProperty> PatientModelImplService::getLandmarkProperties() const
163 {
164  return dataService()->getLandmarkProperties();
165 }
166 
167 void PatientModelImplService::setLandmarkName(QString uid, QString name)
168 {
169  dataService()->setLandmarkName(uid, name);
170 }
171 
173 {
174  return dataService()->get_rMpr();
175 }
176 
178 {
179  this->patientData()->autoSave();
180 }
181 
183 {
184  return false;
185 }
186 
188 {
189  return dataService()->getClinicalApplication();
190 }
191 
192 void PatientModelImplService::setClinicalApplication(CLINICAL_VIEW application)
193 {
194  dataService()->setClinicalApplication(application);
195 }
196 
198 {
199  dataService()->loadData(data);
200 }
201 
202 std::map<QString, VideoSourcePtr> PatientModelImplService::getStreams() const
203 {
204  return dataService()->getStreams();
205 }
206 
208 {
209  return this->patientData()->getActivePatientFolder();
210 }
211 
213 {
214  return this->patientData()->isPatientValid();
215 }
216 
217 DataPtr PatientModelImplService::importData(QString fileName, QString &infoText)
218 {
219  return this->patientData()->importData(fileName, infoText);
220 }
221 
222 void PatientModelImplService::exportPatient(PATIENT_COORDINATE_SYSTEM externalSpace)
223 {
224  this->patientData()->exportPatient(externalSpace);
225 }
226 
228 {
229  this->patientData()->removeData(uid);
230 }
231 
233 {
234  return dataService()->getPresetTransferFunctions3D();
235 }
236 
238 {
239  this->dataService()->setCenter(center);
240 }
241 
243 {
244  return this->dataService()->getCenter();
245 }
246 
247 
249 {
250  return dataService()->addLandmark();
251 }
252 
253 void PatientModelImplService::setLandmarkActive(QString uid, bool active)
254 {
255  dataService()->setLandmarkActive(uid, active);
256 }
257 
259 {
260  return this->dataService()->get_rMpr_History();
261 }
262 
264 {
265  return mActiveData;
266 }
267 
268 DataServicePtr PatientModelImplService::dataService() const
269 {
270  return mDataService;
271 }
272 
273 PatientDataPtr PatientModelImplService::patientData() const
274 {
275  return mPatientData;
276 }
277 
278 void PatientModelImplService::probesChanged()
279 {
280  this->disconnectProbes();
281  mProbeTools.clear();
282  this->connectProbes();
283 }
284 
285 void PatientModelImplService::disconnectProbes()
286 {
287  for (std::map<QString, ToolPtr>::const_iterator iter = mProbeTools.begin(); iter != mProbeTools.end(); ++iter)
288  {
289  ProbePtr probe = iter->second->getProbe();
290  if(probe)
291  disconnect(probe.get(), &Probe::videoSourceAdded, this, &PatientModelImplService::videoSourceAdded);
292  else
293  CX_LOG_WARNING() << "PatientModelImplService::disconnectProbes: Tool is not a probe: " << iter->second->getUid();
294  }
295 }
296 
297 void PatientModelImplService::connectProbes()
298 {
299  ToolMap tools = mTrackingService->getTools();
300  for (ToolMap::const_iterator iter = tools.begin(); iter != tools.end(); ++iter)
301  {
302  ToolPtr tool = iter->second;
303  ProbePtr probe = tool->getProbe();
304  if(tool->getProbe())
305  {
306  mProbeTools[iter->first] = tool;
307  connect(probe.get(), &Probe::videoSourceAdded, this, &PatientModelImplService::videoSourceAdded);
308  }
309  }
310 }
311 
312 void PatientModelImplService::videoSourceAdded(VideoSourcePtr source)
313 {
314  ToolPtr tool = this->getProbeTool(source->getUid());
315  if(!tool)
316  return;
317 
318  QString uid = source->getUid() + tool->getUid();
319  QString name = source->getName() + " - " + tool->getName();
320  TrackedStreamPtr trackedStream = this->dataService()->getTrackedStream(uid);
321  if (!trackedStream)
322  trackedStream = this->createSpecificData<TrackedStream>(uid, name);
323  trackedStream->setProbeTool(tool);
324  trackedStream->setVideoSource(source);
325  trackedStream->setSpaceProvider(mDataService->getSpaceProvider());
326 
327  //Only load trackedStream, don't save it
328  this->dataService()->loadData(trackedStream);
330  this->reEmitActiveTrackedStream(trackedStream);
331 }
332 
333 void PatientModelImplService::reEmitActiveTrackedStream(TrackedStreamPtr trackedStream)
334 {
335  if(mActiveData->getActive<TrackedStream>() == trackedStream)
336  mActiveData->setActive(trackedStream);
337 }
338 
339 ToolPtr PatientModelImplService::getProbeTool(QString videoSourceUid)
340 {
341  for (std::map<QString, ToolPtr>::const_iterator iter = mProbeTools.begin(); iter != mProbeTools.end(); ++iter)
342  {
343  ToolPtr tool = iter->second;
344  ProbePtr probe = tool->getProbe();
345  if(probe && probe->getAvailableVideoSources().contains(videoSourceUid))
346  return tool;
347  }
348  reportWarning("Found no probe for stream" + videoSourceUid);
349  return ToolPtr();
350 }
351 
352 } /* 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:57
virtual PresetTransferFunctions3DPtr getPresetTransferFunctions3D() const
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:56
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)
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 insertData(DataPtr data)
static TrackingServicePtr create(ctkPluginContext *pluginContext)
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)
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 void loadData(DataPtr data)
virtual LandmarksPtr getPatientLandmarks() const
landmark defined in patient space
cxLogicManager_EXPORT SpaceProviderPtr spaceProvider()
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:113
boost::shared_ptr< PatientData > PatientDataPtr
PatientModelImplService(ctkPluginContext *context)
virtual RegistrationHistoryPtr get_rMpr_History() const
boost::shared_ptr< class DataFactory > DataFactoryPtr
virtual std::map< QString, DataPtr > getData() const
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
boost::shared_ptr< class Tool > ToolPtr