Fraxinus  16.5.0-fx-rc3
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 
36 #include <boost/bind.hpp>
37 #include <ctkPluginContext.h>
38 #include "cxLogger.h"
39 #include "cxStringProperty.h"
40 #include "cxDoubleProperty.h"
41 #include "cxBoolProperty.h"
43 #include "cxReconstructThreads.h"
44 #include "cxUSFrameData.h"
47 #include "cxReconstructParams.h"
51 #include "cxPatientModelService.h"
52 
53 //Windows fix
54 #ifndef M_PI
55 #define M_PI 3.14159265358979323846
56 #endif
57 
58 namespace cx
59 {
60 
61 
63  mPatientModelService(patientModelService),
64  mViewService(viewService)
65 {
66  mSettings = settings;
67  mSettings.getElement("algorithms");
68 
69  mParams.reset(new ReconstructParams(patientModelService, settings));
70  connect(mParams.get(), SIGNAL(changedInputSettings()), this, SLOT(setSettings()));
71  connect(patientModelService.get(), &PatientModelService::patientChanged, this, &UsReconstructionImplService::patientChangedSlot);
72 
73  mServiceListener = boost::shared_ptr<ServiceTrackerListener<ReconstructionMethodService> >(new ServiceTrackerListener<ReconstructionMethodService>(
74  pluginContext,
75  boost::bind(&UsReconstructionImplService::onServiceAdded, this, _1),
76  boost::bind(&UsReconstructionImplService::onServiceModified, this, _1),
77  boost::bind(&UsReconstructionImplService::onServiceRemoved, this, _1)
78  ));
79 
80  mServiceListener->open();
81 }
82 
84 {
85 }
86 
88 {
89  return false;
90 }
91 
92 void UsReconstructionImplService::patientChangedSlot()
93 {
94  this->selectData(mPatientModelService->getActivePatientFolder() + "/US_Acq/");
95  emit newInputDataPath(this->getSelectedFileData().mFilename);
96 }
97 
99 {
100  QString name = mParams->getParameter("Algorithm")->getValueAsVariant().toString();
101  if(name.isEmpty())
102  return NULL;
103  return mServiceListener->getServiceFromName(name);
104 }
105 
106 void UsReconstructionImplService::setSettings()
107 {
108  mAlgoOptions.clear();
109  this->updateFromOriginalFileData();
110  emit paramsChanged();
111  emit algorithmChanged();
112 }
113 
115 {
116  if(!mOutputVolumeParams.isValid())
117  {
118  reportError("Cannot reconstruct from invalid ultrasound data");
119  return;
120  }
123  USReconstructInputData fileData = mOriginalFileData;
124  fileData.mUsRaw = mOriginalFileData.mUsRaw->copy();
125 
126  ReconstructionExecuterPtr executer(new ReconstructionExecuter(mPatientModelService, mViewService));
127  connect(executer.get(), SIGNAL(reconstructAboutToStart()), this, SIGNAL(reconstructAboutToStart()));
128  connect(executer.get(), SIGNAL(reconstructStarted()), this, SIGNAL(reconstructStarted()));
129  connect(executer.get(), SIGNAL(reconstructFinished()), this, SIGNAL(reconstructFinished()));
130  connect(executer.get(), SIGNAL(reconstructFinished()), this, SLOT(reconstructFinishedSlot()));
131  mExecuters.push_back(executer);
132 
133  executer->startReconstruction(algo, par, fileData, mParams->getCreateBModeWhenAngio()->getValue());
134 }
135 
137 {
138  std::set<cx::TimedAlgorithmPtr> retval;
139  for (unsigned i=0; i<mExecuters.size(); ++i)
140  retval.insert(mExecuters[i]->getThread());
141  return retval;
142 }
143 
144 void UsReconstructionImplService::reconstructFinishedSlot()
145 {
146  mOriginalFileData.mUsRaw->purgeAll();
147 
148  for (unsigned i=0; i<mExecuters.size(); ++i)
149  {
150  if (mExecuters[i]->getThread()->isFinished())
151  {
152  ReconstructionExecuterPtr executer = mExecuters[i];
153  disconnect(executer.get(), SIGNAL(reconstructAboutToStart()), this, SIGNAL(reconstructAboutToStart()));
154  disconnect(executer.get(), SIGNAL(reconstructStarted()), this, SIGNAL(reconstructStarted()));
155  disconnect(executer.get(), SIGNAL(reconstructFinished()), this, SIGNAL(reconstructFinished()));
156  disconnect(executer.get(), SIGNAL(reconstructFinished()), this, SLOT(reconstructFinishedSlot()));
157 
158  mExecuters.erase(mExecuters.begin()+i);
159  i=0;
160  }
161  }
162 }
163 
164 void UsReconstructionImplService::clearAll()
165 {
166  mOriginalFileData = USReconstructInputData();
167  mOutputVolumeParams = OutputVolumeParams();
168 }
169 
171 {
172  return mOutputVolumeParams;
173 }
174 
176 {
177  mOutputVolumeParams = par;
178  this->setSettings();
179 }
180 
182 {
183  if (mAlgoOptions.empty())
184  {
186  if (algo)
187  {
188  QDomElement element = mSettings.getElement("algorithms", algo->getName());
189  mAlgoOptions = algo->getSettings(element);
190  }
191  }
192 
193  return mAlgoOptions;
194 }
195 
197 {
198  return mSettings;
199 }
200 
202 {
203  return mOriginalFileData.mFilename;
204 }
205 
207 {
208  return mOriginalFileData;
209 }
210 
212 {
213  return mParams->getParameter(uid);
214 }
215 
216 void UsReconstructionImplService::selectData(QString filename, QString calFilesPath)
217 {
218  if (filename.isEmpty())
219  {
220  reportWarning("no file selected");
221  return;
222  }
223 
225  USReconstructInputData fileData = fileReader->readAllFiles(filename, calFilesPath);
226  fileData.mFilename = filename;
227  this->selectData(fileData);
228 }
229 
231 {
232  this->clearAll();
233  mOriginalFileData = fileData;
234  this->updateFromOriginalFileData();
235  emit inputDataSelected(fileData.mFilename);
236 }
237 
238 void UsReconstructionImplService::updateFromOriginalFileData()
239 {
240  if (!mOriginalFileData.isValid())
241  return;
242 
243  ReconstructPreprocessorPtr preprocessor(new ReconstructPreprocessor(mPatientModelService));
244  preprocessor->initialize(this->createCoreParameters(), mOriginalFileData);
245 
246  if (preprocessor->getOutputVolumeParams().isValid())
247  mOutputVolumeParams = preprocessor->getOutputVolumeParams();
248  else
249  {
250  reportError("Input ultrasound data not valid for reconstruction");
251  return;
252  }
253 
254  emit paramsChanged();
255 }
256 
258 {
260  par.mAlgorithmUid = mParams->getAlgorithmAdapter()->getValue();
261  par.mAlgoSettings = mSettings.getElement("algorithms", par.mAlgorithmUid).cloneNode(true).toElement();
262  par.mShaderPath = mShaderPath;
263  par.mAngio = mParams->getAngioAdapter()->getValue();
264  par.mTransferFunctionPreset = mParams->getPresetTFAdapter()->getValue();
265  par.mMaxOutputVolumeSize = mParams->getMaxVolumeSize()->getValue();
266  par.mExtraTimeCalibration = mParams->getTimeCalibration()->getValue();
267  par.mAlignTimestamps = mParams->getAlignTimestamps()->getValue();
268  par.mMaskReduce = mParams->getMaskReduce()->getValue().toDouble();
269  par.mOrientation = mParams->getOrientationAdapter()->getValue();
270  return par;
271 }
272 
273 void UsReconstructionImplService::onServiceAdded(ReconstructionMethodService* service)
274 {
275  QStringList range = mParams->getAlgorithmAdapter()->getValueRange();
276  range << service->getName();
277  mParams->getAlgorithmAdapter()->setValueRange(range);
278 
279  // select algo if none selected
280  PropertyPtr algoName = mParams->getParameter("Algorithm");
281  if (algoName->getValueAsVariant().value<QString>().isEmpty())
282  algoName->setValueFromVariant(service->getName());
283 }
284 
285 void UsReconstructionImplService::onServiceModified(ReconstructionMethodService* service)
286 {
287  reportWarning("ReconstructionMethodService modified... Do not know what to do. Contact developer.");
288 }
289 
290 void UsReconstructionImplService::onServiceRemoved(ReconstructionMethodService* service)
291 {
292  QStringList range = mParams->getAlgorithmAdapter()->getValueRange();
293  range.removeAll(service->getName());
294  mParams->getAlgorithmAdapter()->setValueRange(range);
295 
296  QString algoname = mParams->getParameter("Algorithm")->getValueAsVariant().toString();
297  if (algoname==service->getName())
298  this->setSettings();
299 }
300 
302 {
303  emit newInputDataAvailable(mhdFilename);
304 }
305 
306 } //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:92
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:91
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:42
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.
cxLogicManager_EXPORT ViewServicePtr viewService()
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.