CustusX  15.3.4-beta
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 "cxData.h"
37 
38 #include "cxPatientData.h"
40 #include "cxDataFactory.h"
41 #include "cxDataManagerImpl.h"
42 #include "cxSharedPointerChecker.h"
43 #include "cxTrackingServiceProxy.h"
45 #include "cxSpaceProviderImpl.h"
47 #include "cxTrackedStream.h"
48 #include "cxReporter.h"
49 #include "cxVideoSource.h"
50 
51 namespace cx
52 {
53 
55  mContext(context )
56 {
57  this->createInterconnectedDataAndSpace();
58 
59  connect(this->dataService().get(), &DataManager::dataAddedOrRemoved, this, &PatientModelService::dataAddedOrRemoved);
60  connect(this->dataService().get(), &DataManager::activeImageChanged, this, &PatientModelService::activeImageChanged);
61  connect(this->dataService().get(), &DataManager::rMprChanged, this, &PatientModelService::rMprChanged);
62  connect(this->dataService().get(), &DataManager::streamLoaded, this, &PatientModelService::streamLoaded);
64 
65  connect(this->dataService().get(), &DataManager::centerChanged, this, &PatientModelService::centerChanged);
67 
68  connect(this->patientData().get(), &PatientData::patientChanged, this, &PatientModelService::patientChanged);
69 
70  connect(mTrackingService.get(), &TrackingService::newProbe, this, &PatientModelImplService::newProbe);
71 }
72 
73 void PatientModelImplService::createInterconnectedDataAndSpace()
74 {
75  // prerequisites:
76  mTrackingService = TrackingServiceProxy::create(mContext);
77 
78  // build object(s):
79  PatientModelServicePtr patientModelService = PatientModelServiceProxy::create(mContext);
80 
81  mDataService = DataManagerImpl::create();
82 
83  SpaceProviderPtr spaceProvider(new cx::SpaceProviderImpl(mTrackingService, patientModelService));
84  mDataService->setSpaceProvider(spaceProvider);
85 
86  mDataFactory.reset(new DataFactory(patientModelService, spaceProvider));
87  mDataService->setDataFactory(mDataFactory);
88 
90 
91  mPatientData.reset(new PatientData(mDataService, session));
92 }
93 
94 void PatientModelImplService::shutdownInterconnectedDataAndSpace()
95 {
96  requireUnique(mPatientData, "PatientData");
97  mPatientData.reset();
98 
99  // [HACK] break loop by removing connection to DataFactory and SpaceProvider
100  mDataService->setSpaceProvider(SpaceProviderPtr());
101  mDataService->setDataFactory(DataFactoryPtr());
102  mDataService->clear();
103 
104  requireUnique(mDataFactory, "DataFactory");
105  mDataFactory.reset();
106 
107  requireUnique(mDataService, "DataService");
108  mDataService.reset();
109 
110 
111 }
112 
113 
115 {
116  if(dataService())
117  {
118  disconnect(this->dataService().get(), &DataManager::dataAddedOrRemoved, this, &PatientModelService::dataAddedOrRemoved);
119  disconnect(this->dataService().get(), &DataManager::activeImageChanged, this, &PatientModelService::activeImageChanged);
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()->getActiveImage();
190 }
191 
193 {
194  dataService()->setActiveImage(activeImage);
195 }
196 
198 {
199  return dataService()->getClinicalApplication();
200 }
201 
202 void PatientModelImplService::setClinicalApplication(CLINICAL_VIEW application)
203 {
204  dataService()->setClinicalApplication(application);
205 }
206 
208 {
209  dataService()->loadData(data);
210 }
211 
212 std::map<QString, VideoSourcePtr> PatientModelImplService::getStreams() const
213 {
214  return dataService()->getStreams();
215 }
216 
218 {
219  return this->patientData()->getActivePatientFolder();
220 }
221 
223 {
224  return this->patientData()->isPatientValid();
225 }
226 
227 DataPtr PatientModelImplService::importData(QString fileName, QString &infoText)
228 {
229  return this->patientData()->importData(fileName, infoText);
230 }
231 
233 {
234  this->patientData()->exportPatient(niftiFormat);
235 }
236 
238 {
239  this->patientData()->removeData(uid);
240 }
241 
243 {
244  return dataService()->getPresetTransferFunctions3D();
245 }
246 
248 {
249  this->dataService()->setCenter(center);
250 }
251 
253 {
254  return this->dataService()->getCenter();
255 }
256 
257 
259 {
260  return dataService()->addLandmark();
261 }
262 
263 void PatientModelImplService::setLandmarkActive(QString uid, bool active)
264 {
265  dataService()->setLandmarkActive(uid, active);
266 }
267 
269 {
270  return this->dataService()->get_rMpr_History();
271 }
272 
273 
274 DataManagerImplPtr PatientModelImplService::dataService() const
275 {
276  return mDataService;
277 }
278 
279 PatientDataPtr PatientModelImplService::patientData() const
280 {
281  return mPatientData;
282 }
283 
284 void PatientModelImplService::newProbe(const ToolPtr tool)
285 {
286  ProbePtr probe = tool->getProbe();
287  if(!probe)
288  {
289  reportWarning("PatientModelImplService::newProbe: Tool is not a probe");
290  return;
291  }
292  //Move mProbeTools to DataManager?
293  mProbeTools[tool->getUid()] = tool;
294 
295  connect(probe.get(), &Probe::videoSourceAdded, this, &PatientModelImplService::videoSourceAdded);
296 }
297 
298 void PatientModelImplService::videoSourceAdded(VideoSourcePtr source)
299 {
300 
301  ToolPtr tool = this->getProbeTool(source->getUid());
302  if(!tool)
303  return;
304 
305  QString uid = source->getUid() + tool->getUid();
306  QString name = source->getName() + " - " + tool->getName();
307  TrackedStreamPtr trackedStream = this->dataService()->getTrackedStream(uid);
308  if (!trackedStream)
309  trackedStream = this->createSpecificData<TrackedStream>(uid, name);
310  trackedStream->setProbeTool(tool);
311  trackedStream->setVideoSource(source);
312  trackedStream->setSpaceProvider(mDataService->getSpaceProvider());
313 
314  //Only load trackedStream, don't save it
315  this->dataService()->loadData(trackedStream);
316 // this->insertData(trackedStream);
317 }
318 
319 ToolPtr PatientModelImplService::getProbeTool(QString videoSourceUid)
320 {
321  for (std::map<QString, ToolPtr>::const_iterator iter = mProbeTools.begin(); iter != mProbeTools.end(); ++iter)
322  {
323  ToolPtr tool = iter->second;
324  ProbePtr probe = tool->getProbe();
325  if(probe && probe->getAvailableVideoSources().contains(videoSourceUid))
326  return tool;
327  }
328  reportWarning("Found no probe for stream" + videoSourceUid);
329  return ToolPtr();
330 }
331 
332 } /* namespace cx */
void newProbe(const ToolPtr probe)
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)
void activeImageChanged(const QString &uId)
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
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
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
virtual DataPtr createData(QString type, QString uid, QString name)
boost::shared_ptr< class DataManagerImpl > DataManagerImplPtr
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)
static DataManagerImplPtr create()
void landmarkPropertiesChanged()
emitted when global info about a landmark changed
void centerChanged()
emitted when center is changed.
void centerChanged()
emitted when center is changed.
boost::shared_ptr< class Landmarks > LandmarksPtr
Definition: cxData.h:60
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)
virtual void exportPatient(bool niftiFormat)
virtual void removeData(QString uid)
virtual void setActiveImage(ImagePtr activeImage)
used for system state
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
void dataAddedOrRemoved()
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
void activeImageChanged(const QString &uId)
emitted when the active image is changed
boost::shared_ptr< PatientData > PatientDataPtr
virtual ImagePtr getActiveImage() const
used for system state
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