CustusX  15.8
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 
52 namespace cx
53 {
54 
56  mContext(context )
57 {
58  this->createInterconnectedDataAndSpace();
59 
60  connect(this->dataService().get(), &DataManager::dataAddedOrRemoved, this, &PatientModelService::dataAddedOrRemoved);
61  connect(this->dataService().get(), &DataManager::activeImageChanged, this, &PatientModelService::activeImageChanged);
62  connect(this->dataService().get(), &DataManager::rMprChanged, this, &PatientModelService::rMprChanged);
63  connect(this->dataService().get(), &DataManager::streamLoaded, this, &PatientModelService::streamLoaded);
65 
66  connect(this->dataService().get(), &DataManager::centerChanged, this, &PatientModelService::centerChanged);
68 
69  connect(this->patientData().get(), &PatientData::patientChanged, this, &PatientModelService::patientChanged);
70 
71  connect(mTrackingService.get(), &TrackingService::newProbe, this, &PatientModelImplService::newProbe);
72 }
73 
74 void PatientModelImplService::createInterconnectedDataAndSpace()
75 {
76  // prerequisites:
77  mTrackingService = TrackingServiceProxy::create(mContext);
78 
79  // build object(s):
80  PatientModelServicePtr patientModelService = PatientModelServiceProxy::create(mContext);
81 
82  mDataService = DataManagerImpl::create();
83 
84  SpaceProviderPtr spaceProvider(new cx::SpaceProviderImpl(mTrackingService, patientModelService));
85  mDataService->setSpaceProvider(spaceProvider);
86 
87  mDataFactory.reset(new DataFactory(patientModelService, spaceProvider));
88  mDataService->setDataFactory(mDataFactory);
89 
91 
92  mPatientData.reset(new PatientData(mDataService, session));
93 }
94 
95 void PatientModelImplService::shutdownInterconnectedDataAndSpace()
96 {
97  requireUnique(mPatientData, "PatientData");
98  mPatientData.reset();
99 
100  // [HACK] break loop by removing connection to DataFactory and SpaceProvider
101  mDataService->setSpaceProvider(SpaceProviderPtr());
102  mDataService->setDataFactory(DataFactoryPtr());
103  mDataService->clear();
104 
105  requireUnique(mDataFactory, "DataFactory");
106  mDataFactory.reset();
107 
108  requireUnique(mDataService, "DataService");
109  mDataService.reset();
110 
111 
112 }
113 
114 
116 {
117  if(dataService())
118  {
119  disconnect(this->dataService().get(), &DataManager::dataAddedOrRemoved, this, &PatientModelService::dataAddedOrRemoved);
120  disconnect(this->dataService().get(), &DataManager::activeImageChanged, this, &PatientModelService::activeImageChanged);
121  disconnect(this->dataService().get(), &DataManager::rMprChanged, this, &PatientModelService::rMprChanged);
122  disconnect(this->dataService().get(), &DataManager::streamLoaded, this, &PatientModelService::streamLoaded);
124 
125  disconnect(this->patientData().get(), &PatientData::patientChanged, this, &PatientModelService::patientChanged);
126  }
127 
128  this->shutdownInterconnectedDataAndSpace();
129 }
130 
132 {
133  QString outputBasePath = this->patientData()->getActivePatientFolder();
134 
135  this->dataService()->loadData(data);
136  data->save(outputBasePath);
137 }
138 
139 DataPtr PatientModelImplService::createData(QString type, QString uid, QString name)
140 {
141  return dataService()->getDataFactory()->create(type, uid, name);
142 }
143 
144 std::map<QString, DataPtr> PatientModelImplService::getData() const
145 {
146  return dataService()->getData();
147 }
148 
149 DataPtr PatientModelImplService::getData(const QString& uid) const
150 {
151  std::map<QString, DataPtr> dataMap = this->getData();
152  std::map<QString, DataPtr>::const_iterator iter = dataMap.find(uid);
153  if (iter == dataMap.end())
154  return DataPtr();
155  return iter->second;
156 }
157 
159 {
160  return dataService()->getPatientLandmarks();
161 }
162 
163 std::map<QString, LandmarkProperty> PatientModelImplService::getLandmarkProperties() const
164 {
165  return dataService()->getLandmarkProperties();
166 }
167 
168 void PatientModelImplService::setLandmarkName(QString uid, QString name)
169 {
170  dataService()->setLandmarkName(uid, name);
171 }
172 
174 {
175  return dataService()->get_rMpr();
176 }
177 
179 {
180  this->patientData()->autoSave();
181 }
182 
184 {
185  return false;
186 }
187 
189 {
190  return dataService()->getActiveImage();
191 }
192 
194 {
195  dataService()->setActiveImage(activeImage);
196 }
197 
199 {
200  return dataService()->getClinicalApplication();
201 }
202 
203 void PatientModelImplService::setClinicalApplication(CLINICAL_VIEW application)
204 {
205  dataService()->setClinicalApplication(application);
206 }
207 
209 {
210  dataService()->loadData(data);
211 }
212 
213 std::map<QString, VideoSourcePtr> PatientModelImplService::getStreams() const
214 {
215  return dataService()->getStreams();
216 }
217 
219 {
220  return this->patientData()->getActivePatientFolder();
221 }
222 
224 {
225  return this->patientData()->isPatientValid();
226 }
227 
228 DataPtr PatientModelImplService::importData(QString fileName, QString &infoText)
229 {
230  return this->patientData()->importData(fileName, infoText);
231 }
232 
234 {
235  this->patientData()->exportPatient(niftiFormat);
236 }
237 
239 {
240  this->patientData()->removeData(uid);
241 }
242 
244 {
245  return dataService()->getPresetTransferFunctions3D();
246 }
247 
249 {
250  this->dataService()->setCenter(center);
251 }
252 
254 {
255  return this->dataService()->getCenter();
256 }
257 
258 
260 {
261  return dataService()->addLandmark();
262 }
263 
264 void PatientModelImplService::setLandmarkActive(QString uid, bool active)
265 {
266  dataService()->setLandmarkActive(uid, active);
267 }
268 
270 {
271  return this->dataService()->get_rMpr_History();
272 }
273 
274 
275 DataManagerImplPtr PatientModelImplService::dataService() const
276 {
277  return mDataService;
278 }
279 
280 PatientDataPtr PatientModelImplService::patientData() const
281 {
282  return mPatientData;
283 }
284 
285 void PatientModelImplService::newProbe(const ToolPtr tool)
286 {
287  ProbePtr probe = tool->getProbe();
288  if(!probe)
289  {
290  reportWarning("PatientModelImplService::newProbe: Tool is not a probe");
291  return;
292  }
293  //Move mProbeTools to DataManager?
294  mProbeTools[tool->getUid()] = tool;
295 
296  connect(probe.get(), &Probe::videoSourceAdded, this, &PatientModelImplService::videoSourceAdded);
297 }
298 
299 void PatientModelImplService::videoSourceAdded(VideoSourcePtr source)
300 {
301 
302  ToolPtr tool = this->getProbeTool(source->getUid());
303  if(!tool)
304  return;
305 
306  //Temporary code turning off generation of TrackedStream for video sources that are not 3D
307  if (!source || !source->getVtkImageData() || source->getVtkImageData()->GetDataDimension() != 3)
308  return;
309 
310  QString uid = source->getUid() + tool->getUid();
311  QString name = source->getName() + " - " + tool->getName();
312  TrackedStreamPtr trackedStream = this->dataService()->getTrackedStream(uid);
313  if (!trackedStream)
314  trackedStream = this->createSpecificData<TrackedStream>(uid, name);
315  trackedStream->setProbeTool(tool);
316  trackedStream->setVideoSource(source);
317  trackedStream->setSpaceProvider(mDataService->getSpaceProvider());
318 
319  //Only load trackedStream, don't save it
320  this->dataService()->loadData(trackedStream);
321 // this->insertData(trackedStream);
322 }
323 
324 ToolPtr PatientModelImplService::getProbeTool(QString videoSourceUid)
325 {
326  for (std::map<QString, ToolPtr>::const_iterator iter = mProbeTools.begin(); iter != mProbeTools.end(); ++iter)
327  {
328  ToolPtr tool = iter->second;
329  ProbePtr probe = tool->getProbe();
330  if(probe && probe->getAvailableVideoSources().contains(videoSourceUid))
331  return tool;
332  }
333  reportWarning("Found no probe for stream" + videoSourceUid);
334  return ToolPtr();
335 }
336 
337 } /* 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