NorMIT-nav  2023.01.05-dev+develop.0da12
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  bool success = executer->startReconstruction(algo, par, fileData, mParams->getCreateBModeWhenAngio()->getValue());
115  if(!success)
116  CX_LOG_WARNING() << "US reconstruction failed. Probably an error with input data.";
117 }
118 
120 {
121  std::set<cx::TimedAlgorithmPtr> retval;
122  for (unsigned i=0; i<mExecuters.size(); ++i)
123  retval.insert(mExecuters[i]->getThread());
124  return retval;
125 }
126 
127 void UsReconstructionImplService::reconstructFinishedSlot()
128 {
129  mOriginalFileData.mUsRaw->purgeAll();
130 
131  for (unsigned i=0; i<mExecuters.size(); ++i)
132  {
133  if (mExecuters[i]->getThread()->isFinished())
134  {
135  ReconstructionExecuterPtr executer = mExecuters[i];
136  disconnect(executer.get(), SIGNAL(reconstructAboutToStart()), this, SIGNAL(reconstructAboutToStart()));
137  disconnect(executer.get(), SIGNAL(reconstructStarted()), this, SIGNAL(reconstructStarted()));
138  disconnect(executer.get(), SIGNAL(reconstructFinished()), this, SIGNAL(reconstructFinished()));
139  disconnect(executer.get(), SIGNAL(reconstructFinished()), this, SLOT(reconstructFinishedSlot()));
140 
141  mExecuters.erase(mExecuters.begin()+i);
142  i=0;
143  }
144  }
145 }
146 
147 void UsReconstructionImplService::clearAll()
148 {
149  mOriginalFileData = USReconstructInputData();
150  mOutputVolumeParams = OutputVolumeParams();
151 }
152 
154 {
155  return mOutputVolumeParams;
156 }
157 
159 {
160  mOutputVolumeParams = par;
161  this->setSettings();
162 }
163 
165 {
166  if (mAlgoOptions.empty())
167  {
169  if (algo)
170  {
171  QDomElement element = mSettings.getElement("algorithms", algo->getName());
172  mAlgoOptions = algo->getSettings(element);
173  }
174  }
175 
176  return mAlgoOptions;
177 }
178 
180 {
181  return mSettings;
182 }
183 
185 {
186  return mOriginalFileData.mFilename;
187 }
188 
190 {
191  return mOriginalFileData;
192 }
193 
195 {
196  return mParams->getParameter(uid);
197 }
198 
199 void UsReconstructionImplService::selectData(QString filename, QString calFilesPath)
200 {
201  if (filename.isEmpty())
202  {
203  reportWarning("no file selected");
204  return;
205  }
206 
207  cx::UsReconstructionFileReaderPtr fileReader(new cx::UsReconstructionFileReader(mFileManagerService));
208  USReconstructInputData fileData = fileReader->readAllFiles(filename, calFilesPath);
209  fileData.mFilename = filename;
210  if(!fileData.isValid())
211  {
212  CX_LOG_WARNING() << "UsReconstructionImplService::selectData: Invalid input data";
213  return;
214  }
215  this->selectData(fileData);
216 }
217 
219 {
220  this->clearAll();
221  mOriginalFileData = fileData;
222  this->updateFromOriginalFileData();
223  emit inputDataSelected(fileData.mFilename);
224 }
225 
226 void UsReconstructionImplService::updateFromOriginalFileData()
227 {
228  if (mFileManagerService->isNull() || !mOriginalFileData.isValid())
229  return;
230 
231  ReconstructPreprocessorPtr preprocessor(new ReconstructPreprocessor(mPatientModelService));
232  preprocessor->initialize(this->createCoreParameters(), mOriginalFileData);
233 
234  if (preprocessor->getOutputVolumeParams().isValid())
235  mOutputVolumeParams = preprocessor->getOutputVolumeParams();
236  else
237  {
238  reportError("Input ultrasound data not valid for reconstruction");
239  return;
240  }
241 
242  emit paramsChanged();
243 }
244 
246 {
248  par.mAlgorithmUid = mParams->getAlgorithmAdapter()->getValue();
249  par.mAlgoSettings = mSettings.getElement("algorithms", par.mAlgorithmUid).cloneNode(true).toElement();
250  par.mShaderPath = mShaderPath;
251  par.mAngio = mParams->getAngioAdapter()->getValue();
252  par.mTransferFunctionPreset = mParams->getPresetTFAdapter()->getValue();
253  par.mMaxOutputVolumeSize = mParams->getMaxVolumeSize()->getValue();
254  par.mExtraTimeCalibration = mParams->getTimeCalibration()->getValue();
255  par.mAlignTimestamps = mParams->getAlignTimestamps()->getValue();
256  par.mPositionThinning = mParams->getPositionThinning()->getValue();
257  par.mPosFilterStrength = mParams->getPosFilterStrength()->getValue().toDouble();;
258  par.mMaskReduce = mParams->getMaskReduce()->getValue().toDouble();
259  par.mOrientation = mParams->getOrientationAdapter()->getValue();
260  return par;
261 }
262 
263 void UsReconstructionImplService::onServiceAdded(ReconstructionMethodService* service)
264 {
265  QStringList range = mParams->getAlgorithmAdapter()->getValueRange();
266  range << service->getName();
267  mParams->getAlgorithmAdapter()->setValueRange(range);
268 
269  // select algo if none selected
270  PropertyPtr algoName = mParams->getParameter("Algorithm");
271  if (algoName->getValueAsVariant().value<QString>().isEmpty())
272  algoName->setValueFromVariant(service->getName());
273 }
274 
275 void UsReconstructionImplService::onServiceModified(ReconstructionMethodService* service)
276 {
277  reportWarning("ReconstructionMethodService modified... Do not know what to do. Contact developer.");
278 }
279 
280 void UsReconstructionImplService::onServiceRemoved(ReconstructionMethodService* service)
281 {
282  QStringList range = mParams->getAlgorithmAdapter()->getValueRange();
283  range.removeAll(service->getName());
284  mParams->getAlgorithmAdapter()->setValueRange(range);
285 
286  QString algoname = mParams->getParameter("Algorithm")->getValueAsVariant().toString();
287  if (algoname==service->getName())
288  this->setSettings();
289 }
290 
292 {
293  emit newInputDataAvailable(mhdFilename);
294 }
295 
296 } //cx
cx::XmlOptionFile
Helper class for xml files used to store ssc/cx data.
Definition: cxXmlOptionItem.h:78
cx::UsReconstructionImplService::selectData
virtual void selectData(QString filename, QString calFilesPath="")
Set input data for reconstruction.
Definition: cxUsReconstructionImplService.cpp:199
cx::ReconstructParams
Collection of reconstruction parameters.
Definition: cxReconstructParams.h:34
cx::UsReconstructionImplService::createAlgorithm
virtual ReconstructionMethodService * createAlgorithm()
Definition: cxUsReconstructionImplService.cpp:79
cxServiceTrackerListener.h
cx::UsReconstructionService::reconstructAboutToStart
void reconstructAboutToStart()
cxLogger.h
cxFileManagerService.h
cx::USReconstructInputData::mUsRaw
USFrameDataPtr mUsRaw
All imported US data frames with pointers to each frame.
Definition: cxUSReconstructInputData.h:52
cx::ReconstructPreprocessorPtr
boost::shared_ptr< class ReconstructPreprocessor > ReconstructPreprocessorPtr
Definition: cxReconstructPreprocessor.h:24
cx::UsReconstructionService::inputDataSelected
void inputDataSelected(QString mhdFileName)
cxReconstructPreprocessor.h
cx::UsReconstructionImplService::UsReconstructionImplService
UsReconstructionImplService(ctkPluginContext *pluginContext, PatientModelServicePtr patientModelService, ViewServicePtr viewService, FileManagerServicePtr filemanagerservice, XmlOptionFile settings)
Definition: cxUsReconstructionImplService.cpp:42
cxUsReconstructionFileReader.h
cx::ReconstructCore::InputParams::mExtraTimeCalibration
double mExtraTimeCalibration
Definition: cxReconstructCore.h:52
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::UsReconstructionImplService::getAlgoOptions
virtual std::vector< PropertyPtr > getAlgoOptions()
Return control parameters for the currently selected algorithm, adjustable like getParams()
Definition: cxUsReconstructionImplService.cpp:164
cx::ReconstructionExecuter
Definition: cxReconstructionExecuter.h:33
cx::UsReconstructionImplService::getSelectedFilename
virtual QString getSelectedFilename() const
Get the currently selected filename.
Definition: cxUsReconstructionImplService.cpp:184
cx::USReconstructInputData::isValid
bool isValid() const
Definition: cxUSReconstructInputData.cpp:24
cx::ServiceTrackerListener
Helper class for listening to services being added, modified and removed.
Definition: cxServiceTrackerListener.h:53
cx::PatientModelService::patientChanged
void patientChanged()
cx::ReconstructCore::InputParams::mMaxOutputVolumeSize
double mMaxOutputVolumeSize
Definition: cxReconstructCore.h:66
cx::UsReconstructionImplService::createCoreParameters
virtual ReconstructCore::InputParams createCoreParameters()
Definition: cxUsReconstructionImplService.cpp:245
cx::UsReconstructionImplService::~UsReconstructionImplService
virtual ~UsReconstructionImplService()
Definition: cxUsReconstructionImplService.cpp:64
cx::UsReconstructionService::newInputDataPath
void newInputDataPath(QString path)
cx::ReconstructCore::InputParams::mAlgorithmUid
QString mAlgorithmUid
Definition: cxReconstructCore.h:59
cxDoubleProperty.h
cx::ReconstructCore::InputParams::mShaderPath
QString mShaderPath
name of shader folder
Definition: cxReconstructCore.h:63
cxReconstructionExecuter.h
cx::ReconstructionMethodService
Abstract interface for reconstruction algorithm.
Definition: cxReconstructionMethodService.h:54
cx::FileManagerServicePtr
boost::shared_ptr< class FileManagerService > FileManagerServicePtr
Definition: cxLogicManager.h:31
cx::PropertyPtr
boost::shared_ptr< class Property > PropertyPtr
Definition: cxLocalServerStreamerServer.h:25
cx::ReconstructCore::InputParams::mMaskReduce
double mMaskReduce
Definition: cxReconstructCore.h:56
cxCompositeTimedAlgorithm.h
cx::UsReconstructionImplService::setOutputVolumeParams
virtual void setOutputVolumeParams(const OutputVolumeParams &par)
Control the output volume.
Definition: cxUsReconstructionImplService.cpp:158
cx::ReconstructCore::InputParams::mAlignTimestamps
bool mAlignTimestamps
Definition: cxReconstructCore.h:53
cx::ReconstructCore::InputParams
Definition: cxReconstructCore.h:41
cxUSFrameData.h
cx::ReconstructCore::InputParams::mTransferFunctionPreset
QString mTransferFunctionPreset
Definition: cxReconstructCore.h:65
cx::ReconstructCore::InputParams::mOrientation
QString mOrientation
Definition: cxReconstructCore.h:57
cx::UsReconstructionService::newInputDataAvailable
void newInputDataAvailable(QString mhdFileName)
cx::UsReconstructionImplService::newDataOnDisk
virtual void newDataOnDisk(QString mhdFilename)
Definition: cxUsReconstructionImplService.cpp:291
cx::XmlOptionFile::getElement
QDomElement getElement()
return the current element
Definition: cxXmlOptionItem.cpp:365
cx::PatientModelServicePtr
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
Definition: cxLogicManager.h:25
cx::ReconstructionMethodService::getSettings
virtual std::vector< PropertyPtr > getSettings(QDomElement root)=0
cx::UsReconstructionService::paramsChanged
void paramsChanged()
cx::ViewServicePtr
boost::shared_ptr< class ViewService > ViewServicePtr
Definition: cxLogicManager.h:28
cx::UsReconstructionService::algorithmChanged
void algorithmChanged()
cx::ReconstructionExecuterPtr
boost::shared_ptr< class ReconstructionExecuter > ReconstructionExecuterPtr
Definition: cxReconstructionExecuter.h:28
cxPatientModelService.h
cx::UsReconstructionService::reconstructFinished
void reconstructFinished()
cx::UsReconstructionImplService::isNull
virtual bool isNull()
Definition: cxUsReconstructionImplService.cpp:68
cx::UsReconstructionImplService::getThreadedReconstruction
virtual std::set< cx::TimedAlgorithmPtr > getThreadedReconstruction()
Return the currently reconstructing thread object(s).
Definition: cxUsReconstructionImplService.cpp:119
cx::UsReconstructionService::reconstructStarted
void reconstructStarted()
cxReconstructionMethodService.h
cx::UsReconstructionFileReader
Reader class for the US Acquisition files.
Definition: cxUsReconstructionFileReader.h:126
cx::USReconstructInputData::mFilename
QString mFilename
filename used for current data read
Definition: cxUSReconstructInputData.h:50
cx::ReconstructPreprocessor
Algorithm part of reconstruction - no dependencies on parameter classes.
Definition: cxReconstructPreprocessor.h:33
cxUsReconstructionImplService.h
cx::OutputVolumeParams::isValid
bool isValid()
Definition: cxReconstructedOutputVolumeParams.h:45
cx::ReconstructCore::InputParams::mPosFilterStrength
unsigned mPosFilterStrength
Definition: cxReconstructCore.h:55
cx::OutputVolumeParams
Helper struct for sending and controlling output volume properties.
Definition: cxReconstructedOutputVolumeParams.h:39
cx::USReconstructInputData
Definition: cxUSReconstructInputData.h:48
cxStringProperty.h
CX_LOG_WARNING
#define CX_LOG_WARNING
Definition: cxLogger.h:98
cx::UsReconstructionImplService::startReconstruction
virtual void startReconstruction()
Definition: cxUsReconstructionImplService.cpp:95
cx::ReconstructCore::InputParams::mPositionThinning
bool mPositionThinning
Definition: cxReconstructCore.h:54
cxReconstructThreads.h
cx::UsReconstructionFileReaderPtr
boost::shared_ptr< class UsReconstructionFileReader > UsReconstructionFileReaderPtr
Definition: cxForwardDeclarations.h:167
cxBoolProperty.h
cx::reportError
void reportError(QString msg)
Definition: cxLogger.cpp:71
cxReconstructParams.h
cx::ReconstructCore::InputParams::mAngio
bool mAngio
true for angio data, false is B-mode.
Definition: cxReconstructCore.h:64
cx::UsReconstructionImplService::getOutputVolumeParams
virtual OutputVolumeParams getOutputVolumeParams() const
Return params controlling the output data. These are data-dependent.
Definition: cxUsReconstructionImplService.cpp:153
cx::reportWarning
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
cx::settings
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
cx::ReconstructionMethodService::getName
virtual QString getName() const =0
cx::ReconstructCore::InputParams::mAlgoSettings
QDomElement mAlgoSettings
Definition: cxReconstructCore.h:60
cx::UsReconstructionImplService::getSelectedFileData
virtual USReconstructInputData getSelectedFileData()
Return the currently selected input data.
Definition: cxUsReconstructionImplService.cpp:189
cx::UsReconstructionImplService::getSettings
virtual XmlOptionFile getSettings()
Return the settings xml file where parameters are stored.
Definition: cxUsReconstructionImplService.cpp:179
cx::UsReconstructionImplService::getParam
virtual PropertyPtr getParam(QString uid)
Return one of the standard parameters.
Definition: cxUsReconstructionImplService.cpp:194