Fraxinus  18.10
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 
32 //Windows fix
33 #ifndef M_PI
34 #define M_PI 3.14159265358979323846
35 #endif
36 
37 namespace cx
38 {
39 
40 
42  mPatientModelService(patientModelService),
43  mViewService(viewService)
44 {
45  mSettings = settings;
46  mSettings.getElement("algorithms");
47 
48  mParams.reset(new ReconstructParams(patientModelService, settings));
49  connect(mParams.get(), SIGNAL(changedInputSettings()), this, SLOT(setSettings()));
50  connect(patientModelService.get(), &PatientModelService::patientChanged, this, &UsReconstructionImplService::patientChangedSlot);
51 
52  mServiceListener = boost::shared_ptr<ServiceTrackerListener<ReconstructionMethodService> >(new ServiceTrackerListener<ReconstructionMethodService>(
53  pluginContext,
54  boost::bind(&UsReconstructionImplService::onServiceAdded, this, _1),
55  boost::bind(&UsReconstructionImplService::onServiceModified, this, _1),
56  boost::bind(&UsReconstructionImplService::onServiceRemoved, this, _1)
57  ));
58 
59  mServiceListener->open();
60 }
61 
63 {
64 }
65 
67 {
68  return false;
69 }
70 
71 void UsReconstructionImplService::patientChangedSlot()
72 {
73  this->selectData(mPatientModelService->getActivePatientFolder() + "/US_Acq/");
74  emit newInputDataPath(this->getSelectedFileData().mFilename);
75 }
76 
78 {
79  QString name = mParams->getParameter("Algorithm")->getValueAsVariant().toString();
80  if(name.isEmpty())
81  return NULL;
82  return mServiceListener->getServiceFromName(name);
83 }
84 
85 void UsReconstructionImplService::setSettings()
86 {
87  mAlgoOptions.clear();
88  this->updateFromOriginalFileData();
89  emit paramsChanged();
90  emit algorithmChanged();
91 }
92 
94 {
95  if(!mOutputVolumeParams.isValid())
96  {
97  reportError("Cannot reconstruct from invalid ultrasound data");
98  return;
99  }
102  USReconstructInputData fileData = mOriginalFileData;
103  fileData.mUsRaw = mOriginalFileData.mUsRaw->copy();
104 
105  ReconstructionExecuterPtr executer(new ReconstructionExecuter(mPatientModelService, mViewService));
106  connect(executer.get(), SIGNAL(reconstructAboutToStart()), this, SIGNAL(reconstructAboutToStart()));
107  connect(executer.get(), SIGNAL(reconstructStarted()), this, SIGNAL(reconstructStarted()));
108  connect(executer.get(), SIGNAL(reconstructFinished()), this, SIGNAL(reconstructFinished()));
109  connect(executer.get(), SIGNAL(reconstructFinished()), this, SLOT(reconstructFinishedSlot()));
110  mExecuters.push_back(executer);
111 
112  executer->startReconstruction(algo, par, fileData, mParams->getCreateBModeWhenAngio()->getValue());
113 }
114 
116 {
117  std::set<cx::TimedAlgorithmPtr> retval;
118  for (unsigned i=0; i<mExecuters.size(); ++i)
119  retval.insert(mExecuters[i]->getThread());
120  return retval;
121 }
122 
123 void UsReconstructionImplService::reconstructFinishedSlot()
124 {
125  mOriginalFileData.mUsRaw->purgeAll();
126 
127  for (unsigned i=0; i<mExecuters.size(); ++i)
128  {
129  if (mExecuters[i]->getThread()->isFinished())
130  {
131  ReconstructionExecuterPtr executer = mExecuters[i];
132  disconnect(executer.get(), SIGNAL(reconstructAboutToStart()), this, SIGNAL(reconstructAboutToStart()));
133  disconnect(executer.get(), SIGNAL(reconstructStarted()), this, SIGNAL(reconstructStarted()));
134  disconnect(executer.get(), SIGNAL(reconstructFinished()), this, SIGNAL(reconstructFinished()));
135  disconnect(executer.get(), SIGNAL(reconstructFinished()), this, SLOT(reconstructFinishedSlot()));
136 
137  mExecuters.erase(mExecuters.begin()+i);
138  i=0;
139  }
140  }
141 }
142 
143 void UsReconstructionImplService::clearAll()
144 {
145  mOriginalFileData = USReconstructInputData();
146  mOutputVolumeParams = OutputVolumeParams();
147 }
148 
150 {
151  return mOutputVolumeParams;
152 }
153 
155 {
156  mOutputVolumeParams = par;
157  this->setSettings();
158 }
159 
161 {
162  if (mAlgoOptions.empty())
163  {
165  if (algo)
166  {
167  QDomElement element = mSettings.getElement("algorithms", algo->getName());
168  mAlgoOptions = algo->getSettings(element);
169  }
170  }
171 
172  return mAlgoOptions;
173 }
174 
176 {
177  return mSettings;
178 }
179 
181 {
182  return mOriginalFileData.mFilename;
183 }
184 
186 {
187  return mOriginalFileData;
188 }
189 
191 {
192  return mParams->getParameter(uid);
193 }
194 
195 void UsReconstructionImplService::selectData(QString filename, QString calFilesPath)
196 {
197  if (filename.isEmpty())
198  {
199  reportWarning("no file selected");
200  return;
201  }
202 
204  USReconstructInputData fileData = fileReader->readAllFiles(filename, calFilesPath);
205  fileData.mFilename = filename;
206  this->selectData(fileData);
207 }
208 
210 {
211  this->clearAll();
212  mOriginalFileData = fileData;
213  this->updateFromOriginalFileData();
214  emit inputDataSelected(fileData.mFilename);
215 }
216 
217 void UsReconstructionImplService::updateFromOriginalFileData()
218 {
219  if (!mOriginalFileData.isValid())
220  return;
221 
222  ReconstructPreprocessorPtr preprocessor(new ReconstructPreprocessor(mPatientModelService));
223  preprocessor->initialize(this->createCoreParameters(), mOriginalFileData);
224 
225  if (preprocessor->getOutputVolumeParams().isValid())
226  mOutputVolumeParams = preprocessor->getOutputVolumeParams();
227  else
228  {
229  reportError("Input ultrasound data not valid for reconstruction");
230  return;
231  }
232 
233  emit paramsChanged();
234 }
235 
237 {
239  par.mAlgorithmUid = mParams->getAlgorithmAdapter()->getValue();
240  par.mAlgoSettings = mSettings.getElement("algorithms", par.mAlgorithmUid).cloneNode(true).toElement();
241  par.mShaderPath = mShaderPath;
242  par.mAngio = mParams->getAngioAdapter()->getValue();
243  par.mTransferFunctionPreset = mParams->getPresetTFAdapter()->getValue();
244  par.mMaxOutputVolumeSize = mParams->getMaxVolumeSize()->getValue();
245  par.mExtraTimeCalibration = mParams->getTimeCalibration()->getValue();
246  par.mAlignTimestamps = mParams->getAlignTimestamps()->getValue();
247  par.mPositionThinning = mParams->getPositionThinning()->getValue();
248  par.mPosFilterStrength = mParams->getPosFilterStrength()->getValue().toDouble();;
249  par.mMaskReduce = mParams->getMaskReduce()->getValue().toDouble();
250  par.mOrientation = mParams->getOrientationAdapter()->getValue();
251  return par;
252 }
253 
254 void UsReconstructionImplService::onServiceAdded(ReconstructionMethodService* service)
255 {
256  QStringList range = mParams->getAlgorithmAdapter()->getValueRange();
257  range << service->getName();
258  mParams->getAlgorithmAdapter()->setValueRange(range);
259 
260  // select algo if none selected
261  PropertyPtr algoName = mParams->getParameter("Algorithm");
262  if (algoName->getValueAsVariant().value<QString>().isEmpty())
263  algoName->setValueFromVariant(service->getName());
264 }
265 
266 void UsReconstructionImplService::onServiceModified(ReconstructionMethodService* service)
267 {
268  reportWarning("ReconstructionMethodService modified... Do not know what to do. Contact developer.");
269 }
270 
271 void UsReconstructionImplService::onServiceRemoved(ReconstructionMethodService* service)
272 {
273  QStringList range = mParams->getAlgorithmAdapter()->getValueRange();
274  range.removeAll(service->getName());
275  mParams->getAlgorithmAdapter()->setValueRange(range);
276 
277  QString algoname = mParams->getParameter("Algorithm")->getValueAsVariant().toString();
278  if (algoname==service->getName())
279  this->setSettings();
280 }
281 
283 {
284  emit newInputDataAvailable(mhdFilename);
285 }
286 
287 } //cx
void newInputDataAvailable(QString mhdFileName)
Reader class for the US Acquisition files.
void inputDataSelected(QString mhdFileName)
boost::shared_ptr< class ReconstructionExecuter > ReconstructionExecuterPtr
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)
UsReconstructionImplService(ctkPluginContext *pluginContext, PatientModelServicePtr patientModelService, ViewServicePtr viewService, XmlOptionFile settings)
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.