CustusX  15.8
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 
62 UsReconstructionImplService::UsReconstructionImplService(ctkPluginContext *pluginContext, PatientModelServicePtr patientModelService, VisualizationServicePtr visualizationService, XmlOptionFile settings) :
63  mPatientModelService(patientModelService),
64  mVisualizationService(visualizationService)
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, mVisualizationService));
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  std::set<cx::TimedAlgorithmPtr> retval;
149  for (unsigned i=0; i<mExecuters.size(); ++i)
150  {
151  if (mExecuters[i]->getThread()->isFinished())
152  {
153  ReconstructionExecuterPtr executer = mExecuters[i];
154  disconnect(executer.get(), SIGNAL(reconstructAboutToStart()), this, SIGNAL(reconstructAboutToStart()));
155  disconnect(executer.get(), SIGNAL(reconstructStarted()), this, SIGNAL(reconstructStarted()));
156  disconnect(executer.get(), SIGNAL(reconstructFinished()), this, SIGNAL(reconstructFinished()));
157  disconnect(executer.get(), SIGNAL(reconstructFinished()), this, SLOT(reconstructFinishedSlot()));
158 
159  mExecuters.erase(mExecuters.begin()+i);
160  i=0;
161  }
162  }
163 }
164 
165 void UsReconstructionImplService::clearAll()
166 {
167  mOriginalFileData = USReconstructInputData();
168  mOutputVolumeParams = OutputVolumeParams();
169 }
170 
172 {
173  return mOutputVolumeParams;
174 }
175 
177 {
178  mOutputVolumeParams = par;
179  this->setSettings();
180 }
181 
183 {
184  if (mAlgoOptions.empty())
185  {
187  if (algo)
188  {
189  QDomElement element = mSettings.getElement("algorithms", algo->getName());
190  mAlgoOptions = algo->getSettings(element);
191  }
192  }
193 
194  return mAlgoOptions;
195 }
196 
198 {
199  return mSettings;
200 }
201 
203 {
204  return mOriginalFileData.mFilename;
205 }
206 
208 {
209  return mOriginalFileData;
210 }
211 
213 {
214  return mParams->getParameter(uid);
215 }
216 
217 void UsReconstructionImplService::selectData(QString filename, QString calFilesPath)
218 {
219  if (filename.isEmpty())
220  {
221  reportWarning("no file selected");
222  return;
223  }
224 
226  USReconstructInputData fileData = fileReader->readAllFiles(filename, calFilesPath);
227  fileData.mFilename = filename;
228  this->selectData(fileData);
229 }
230 
232 {
233  this->clearAll();
234  mOriginalFileData = fileData;
235  this->updateFromOriginalFileData();
236  emit inputDataSelected(fileData.mFilename);
237 }
238 
239 void UsReconstructionImplService::updateFromOriginalFileData()
240 {
241  if (!mOriginalFileData.isValid())
242  return;
243 
244  ReconstructPreprocessorPtr preprocessor(new ReconstructPreprocessor(mPatientModelService));
245  preprocessor->initialize(this->createCoreParameters(), mOriginalFileData);
246 
247  if (preprocessor->getOutputVolumeParams().isValid())
248  mOutputVolumeParams = preprocessor->getOutputVolumeParams();
249  else
250  {
251  reportError("Input ultrasound data not valid for reconstruction");
252  return;
253  }
254 
255  emit paramsChanged();
256 }
257 
259 {
261  par.mAlgorithmUid = mParams->getAlgorithmAdapter()->getValue();
262  par.mAlgoSettings = mSettings.getElement("algorithms", par.mAlgorithmUid).cloneNode(true).toElement();
263  par.mShaderPath = mShaderPath;
264  par.mAngio = mParams->getAngioAdapter()->getValue();
265  par.mTransferFunctionPreset = mParams->getPresetTFAdapter()->getValue();
266  par.mMaxOutputVolumeSize = mParams->getMaxVolumeSize()->getValue();
267  par.mExtraTimeCalibration = mParams->getTimeCalibration()->getValue();
268  par.mAlignTimestamps = mParams->getAlignTimestamps()->getValue();
269  par.mMaskReduce = mParams->getMaskReduce()->getValue().toDouble();
270  par.mOrientation = mParams->getOrientationAdapter()->getValue();
271  return par;
272 }
273 
274 void UsReconstructionImplService::onServiceAdded(ReconstructionMethodService* service)
275 {
276  QStringList range = mParams->getAlgorithmAdapter()->getValueRange();
277  range << service->getName();
278  mParams->getAlgorithmAdapter()->setValueRange(range);
279 
280  // select algo if none selected
281  PropertyPtr algoName = mParams->getParameter("Algorithm");
282  if (algoName->getValueAsVariant().value<QString>().isEmpty())
283  algoName->setValueFromVariant(service->getName());
284 }
285 
286 void UsReconstructionImplService::onServiceModified(ReconstructionMethodService* service)
287 {
288  reportWarning("ReconstructionMethodService modified... Do not know what to do. Contact developer.");
289 }
290 
291 void UsReconstructionImplService::onServiceRemoved(ReconstructionMethodService* service)
292 {
293  QStringList range = mParams->getAlgorithmAdapter()->getValueRange();
294  range.removeAll(service->getName());
295  mParams->getAlgorithmAdapter()->setValueRange(range);
296 
297  QString algoname = mParams->getParameter("Algorithm")->getValueAsVariant().toString();
298  if (algoname==service->getName())
299  this->setSettings();
300 }
301 
303 {
304  emit newInputDataAvailable(mhdFilename);
305 }
306 
307 } //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).
UsReconstructionImplService(ctkPluginContext *pluginContext, PatientModelServicePtr patientModelService, VisualizationServicePtr visualizationService, XmlOptionFile settings)
virtual void selectData(QString filename, QString calFilesPath="")
Set input data for reconstruction.
QString mShaderPath
name of shader folder
virtual void newDataOnDisk(QString mhdFilename)
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 VisualizationService > VisualizationServicePtr
Definition: cxRegServices.h:43
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.
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.