CustusX  20.03-rc1
An IGT application
cxUsReconstructionImplService.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 
15 #include <boost/bind.hpp>
16 #include <ctkPluginContext.h>
17 #include "cxLogger.h"
18 #include "cxStringProperty.h"
19 #include "cxDoubleProperty.h"
20 #include "cxBoolProperty.h"
22 #include "cxReconstructThreads.h"
23 #include "cxUSFrameData.h"
26 #include "cxReconstructParams.h"
30 #include "cxPatientModelService.h"
31 #include "cxFileManagerService.h"
32 
33 //Windows fix
34 #ifndef M_PI
35 #define M_PI 3.14159265358979323846
36 #endif
37 
38 namespace cx
39 {
40 
41 
42 UsReconstructionImplService::UsReconstructionImplService(ctkPluginContext *pluginContext, PatientModelServicePtr patientModelService, ViewServicePtr viewService, FileManagerServicePtr filemanagerservice, XmlOptionFile settings) :
43  mPatientModelService(patientModelService),
44  mViewService(viewService),
45  mFileManagerService(filemanagerservice)
46 {
47  mSettings = settings;
48  mSettings.getElement("algorithms");
49 
50  mParams.reset(new ReconstructParams(patientModelService, settings));
51  connect(mParams.get(), SIGNAL(changedInputSettings()), this, SLOT(setSettings()));
52  connect(patientModelService.get(), &PatientModelService::patientChanged, this, &UsReconstructionImplService::patientChangedSlot);
53 
54  mServiceListener = boost::shared_ptr<ServiceTrackerListener<ReconstructionMethodService> >(new ServiceTrackerListener<ReconstructionMethodService>(
55  pluginContext,
56  boost::bind(&UsReconstructionImplService::onServiceAdded, this, _1),
57  boost::bind(&UsReconstructionImplService::onServiceModified, this, _1),
58  boost::bind(&UsReconstructionImplService::onServiceRemoved, this, _1)
59  ));
60 
61  mServiceListener->open();
62 }
63 
65 {
66 }
67 
69 {
70  return false;
71 }
72 
73 void UsReconstructionImplService::patientChangedSlot()
74 {
75  this->selectData(mPatientModelService->getActivePatientFolder() + "/US_Acq/");
76  emit newInputDataPath(this->getSelectedFileData().mFilename);
77 }
78 
80 {
81  QString name = mParams->getParameter("Algorithm")->getValueAsVariant().toString();
82  if(name.isEmpty())
83  return NULL;
84  return mServiceListener->getServiceFromName(name);
85 }
86 
87 void UsReconstructionImplService::setSettings()
88 {
89  mAlgoOptions.clear();
90  this->updateFromOriginalFileData();
91  emit paramsChanged();
92  emit algorithmChanged();
93 }
94 
96 {
97  if(!mOutputVolumeParams.isValid())
98  {
99  reportError("Cannot reconstruct from invalid ultrasound data");
100  return;
101  }
104  USReconstructInputData fileData = mOriginalFileData;
105  fileData.mUsRaw = mOriginalFileData.mUsRaw->copy();
106 
107  ReconstructionExecuterPtr executer(new ReconstructionExecuter(mPatientModelService, mViewService));
108  connect(executer.get(), SIGNAL(reconstructAboutToStart()), this, SIGNAL(reconstructAboutToStart()));
109  connect(executer.get(), SIGNAL(reconstructStarted()), this, SIGNAL(reconstructStarted()));
110  connect(executer.get(), SIGNAL(reconstructFinished()), this, SIGNAL(reconstructFinished()));
111  connect(executer.get(), SIGNAL(reconstructFinished()), this, SLOT(reconstructFinishedSlot()));
112  mExecuters.push_back(executer);
113 
114  executer->startReconstruction(algo, par, fileData, mParams->getCreateBModeWhenAngio()->getValue());
115 }
116 
118 {
119  std::set<cx::TimedAlgorithmPtr> retval;
120  for (unsigned i=0; i<mExecuters.size(); ++i)
121  retval.insert(mExecuters[i]->getThread());
122  return retval;
123 }
124 
125 void UsReconstructionImplService::reconstructFinishedSlot()
126 {
127  mOriginalFileData.mUsRaw->purgeAll();
128 
129  for (unsigned i=0; i<mExecuters.size(); ++i)
130  {
131  if (mExecuters[i]->getThread()->isFinished())
132  {
133  ReconstructionExecuterPtr executer = mExecuters[i];
134  disconnect(executer.get(), SIGNAL(reconstructAboutToStart()), this, SIGNAL(reconstructAboutToStart()));
135  disconnect(executer.get(), SIGNAL(reconstructStarted()), this, SIGNAL(reconstructStarted()));
136  disconnect(executer.get(), SIGNAL(reconstructFinished()), this, SIGNAL(reconstructFinished()));
137  disconnect(executer.get(), SIGNAL(reconstructFinished()), this, SLOT(reconstructFinishedSlot()));
138 
139  mExecuters.erase(mExecuters.begin()+i);
140  i=0;
141  }
142  }
143 }
144 
145 void UsReconstructionImplService::clearAll()
146 {
147  mOriginalFileData = USReconstructInputData();
148  mOutputVolumeParams = OutputVolumeParams();
149 }
150 
152 {
153  return mOutputVolumeParams;
154 }
155 
157 {
158  mOutputVolumeParams = par;
159  this->setSettings();
160 }
161 
163 {
164  if (mAlgoOptions.empty())
165  {
167  if (algo)
168  {
169  QDomElement element = mSettings.getElement("algorithms", algo->getName());
170  mAlgoOptions = algo->getSettings(element);
171  }
172  }
173 
174  return mAlgoOptions;
175 }
176 
178 {
179  return mSettings;
180 }
181 
183 {
184  return mOriginalFileData.mFilename;
185 }
186 
188 {
189  return mOriginalFileData;
190 }
191 
193 {
194  return mParams->getParameter(uid);
195 }
196 
197 void UsReconstructionImplService::selectData(QString filename, QString calFilesPath)
198 {
199  if (filename.isEmpty())
200  {
201  reportWarning("no file selected");
202  return;
203  }
204 
205  cx::UsReconstructionFileReaderPtr fileReader(new cx::UsReconstructionFileReader(mFileManagerService));
206  USReconstructInputData fileData = fileReader->readAllFiles(filename, calFilesPath);
207  fileData.mFilename = filename;
208  this->selectData(fileData);
209 }
210 
212 {
213  this->clearAll();
214  mOriginalFileData = fileData;
215  this->updateFromOriginalFileData();
216  emit inputDataSelected(fileData.mFilename);
217 }
218 
219 void UsReconstructionImplService::updateFromOriginalFileData()
220 {
221  if (mFileManagerService->isNull() || !mOriginalFileData.isValid())
222  return;
223 
224  ReconstructPreprocessorPtr preprocessor(new ReconstructPreprocessor(mPatientModelService));
225  preprocessor->initialize(this->createCoreParameters(), mOriginalFileData);
226 
227  if (preprocessor->getOutputVolumeParams().isValid())
228  mOutputVolumeParams = preprocessor->getOutputVolumeParams();
229  else
230  {
231  reportError("Input ultrasound data not valid for reconstruction");
232  return;
233  }
234 
235  emit paramsChanged();
236 }
237 
239 {
241  par.mAlgorithmUid = mParams->getAlgorithmAdapter()->getValue();
242  par.mAlgoSettings = mSettings.getElement("algorithms", par.mAlgorithmUid).cloneNode(true).toElement();
243  par.mShaderPath = mShaderPath;
244  par.mAngio = mParams->getAngioAdapter()->getValue();
245  par.mTransferFunctionPreset = mParams->getPresetTFAdapter()->getValue();
246  par.mMaxOutputVolumeSize = mParams->getMaxVolumeSize()->getValue();
247  par.mExtraTimeCalibration = mParams->getTimeCalibration()->getValue();
248  par.mAlignTimestamps = mParams->getAlignTimestamps()->getValue();
249  par.mPositionThinning = mParams->getPositionThinning()->getValue();
250  par.mPosFilterStrength = mParams->getPosFilterStrength()->getValue().toDouble();;
251  par.mMaskReduce = mParams->getMaskReduce()->getValue().toDouble();
252  par.mOrientation = mParams->getOrientationAdapter()->getValue();
253  return par;
254 }
255 
256 void UsReconstructionImplService::onServiceAdded(ReconstructionMethodService* service)
257 {
258  QStringList range = mParams->getAlgorithmAdapter()->getValueRange();
259  range << service->getName();
260  mParams->getAlgorithmAdapter()->setValueRange(range);
261 
262  // select algo if none selected
263  PropertyPtr algoName = mParams->getParameter("Algorithm");
264  if (algoName->getValueAsVariant().value<QString>().isEmpty())
265  algoName->setValueFromVariant(service->getName());
266 }
267 
268 void UsReconstructionImplService::onServiceModified(ReconstructionMethodService* service)
269 {
270  reportWarning("ReconstructionMethodService modified... Do not know what to do. Contact developer.");
271 }
272 
273 void UsReconstructionImplService::onServiceRemoved(ReconstructionMethodService* service)
274 {
275  QStringList range = mParams->getAlgorithmAdapter()->getValueRange();
276  range.removeAll(service->getName());
277  mParams->getAlgorithmAdapter()->setValueRange(range);
278 
279  QString algoname = mParams->getParameter("Algorithm")->getValueAsVariant().toString();
280  if (algoname==service->getName())
281  this->setSettings();
282 }
283 
285 {
286  emit newInputDataAvailable(mhdFilename);
287 }
288 
289 } //cx
void newInputDataAvailable(QString mhdFileName)
boost::shared_ptr< class FileManagerService > FileManagerServicePtr
Reader class for the US Acquisition files.
void inputDataSelected(QString mhdFileName)
boost::shared_ptr< class ReconstructionExecuter > ReconstructionExecuterPtr
UsReconstructionImplService(ctkPluginContext *pluginContext, PatientModelServicePtr patientModelService, ViewServicePtr viewService, FileManagerServicePtr filemanagerservice, XmlOptionFile settings)
void reportError(QString msg)
Definition: cxLogger.cpp:71
virtual PropertyPtr getParam(QString uid)
Return one of the standard parameters.
Abstract interface for reconstruction algorithm.
virtual QString getSelectedFilename() const
Get the currently selected filename.
virtual std::set< cx::TimedAlgorithmPtr > getThreadedReconstruction()
Return the currently reconstructing thread object(s).
virtual void selectData(QString filename, QString calFilesPath="")
Set input data for reconstruction.
QString mShaderPath
name of shader folder
virtual void newDataOnDisk(QString mhdFilename)
boost::shared_ptr< class ViewService > ViewServicePtr
Helper struct for sending and controlling output volume properties.
QDomElement getElement()
return the current element
virtual std::vector< PropertyPtr > getSettings(QDomElement root)=0
virtual std::vector< PropertyPtr > getAlgoOptions()
Return control parameters for the currently selected algorithm, adjustable like getParams() ...
boost::shared_ptr< class UsReconstructionFileReader > UsReconstructionFileReaderPtr
boost::shared_ptr< class Property > PropertyPtr
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
Collection of reconstruction parameters.
boost::shared_ptr< class ReconstructPreprocessor > ReconstructPreprocessorPtr
virtual QString getName() const =0
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
Algorithm part of reconstruction - no dependencies on parameter classes.
virtual void setOutputVolumeParams(const OutputVolumeParams &par)
Control the output volume.
Helper class for listening to services being added, modified and removed.
virtual OutputVolumeParams getOutputVolumeParams() const
Return params controlling the output data. These are data-dependent.
virtual XmlOptionFile getSettings()
Return the settings xml file where parameters are stored.
virtual USReconstructInputData getSelectedFileData()
Return the currently selected input data.
void newInputDataPath(QString path)
bool mAngio
true for angio data, false is B-mode.
QString mFilename
filename used for current data read
virtual ReconstructionMethodService * createAlgorithm()
USFrameDataPtr mUsRaw
All imported US data frames with pointers to each frame.
virtual ReconstructCore::InputParams createCoreParameters()
Helper class for xml files used to store ssc/cx data.
Namespace for all CustusX production code.