CustusX  2023.01.05-dev+develop.0da12
An IGT application
cxReconstructionExecuter.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 #include "cxTimedAlgorithm.h"
14 #include "cxReconstructThreads.h"
15 
16 #include "cxLogger.h"
17 
18 namespace cx
19 {
20 
22 {
23  return mPipeline;
24 }
25 
27 {
28  if (!fileData.isValid())
29  {
30  CX_LOG_WARNING() << "US reconstruction input data is invalid.";
31  return false;
32  }
33  cx::ReconstructPreprocessorPtr preprocessor = this->createPreprocessor(par, fileData);
34  mCores = this->createCores(algo, par, createBModeWhenAngio);
35 
36  std::vector<bool> angio;
37  for (unsigned i=0; i<mCores.size(); ++i)
38  angio.push_back(mCores[i]->getInputParams().mAngio);
39 
40  std::vector<cx::ProcessedUSInputDataPtr> processedInput = preprocessor->createProcessedInput(angio);
41 
42  for (unsigned i=0; i<mCores.size(); ++i)
43  {
44  mCores[i]->initialize(processedInput[i], preprocessor->getOutputVolumeParams());
45  }
46  for (unsigned i=0; i<mCores.size(); ++i)
47  {
48  mCores[i]->reconstruct();
49  }
50  return true;
51 }
52 
54 {
55  if (mPipeline)
56  {
57  reportError("Reconstruct Executer can only be run once. Ignoring start.");
58  return false;
59  }
60 
61  if (!fileData.isValid())
62  {
63  CX_LOG_WARNING() << "US reconstruction input data is invalid.";
64  return false;
65  }
66 
67  //Don't create an extra B-Mode volume if input data is 8 bit
68  if (fileData.is8bit())
69  createBModeWhenAngio = false;
70 
71  mCores = this->createCores(algo, par, createBModeWhenAngio);
72  if (mCores.empty())
73  reportWarning("Failed to start reconstruction");
74 
75  cx::CompositeTimedAlgorithmPtr algorithm = this->assembleReconstructionPipeline(mCores, par, fileData);
76  this->launch(algorithm);
77  return true;
78 }
79 
80 std::vector<cx::ImagePtr> ReconstructionExecuter::getResult()
81 {
82  std::vector<cx::ImagePtr> retval;
83  if (mPipeline && !mPipeline->isFinished())
84  return retval;
85 
86  for (unsigned i=0; i<mCores.size(); ++i)
87  retval.push_back(mCores[i]->getOutput());
88 
89  return retval;
90 }
91 
92 void ReconstructionExecuter::launch(cx::TimedAlgorithmPtr thread)
93 {
94  mPipeline = thread;
96  connect(thread.get(), SIGNAL(finished()), this, SIGNAL(reconstructFinished()));
97  thread->execute();
98  emit reconstructStarted();
99 }
100 
101 cx::CompositeTimedAlgorithmPtr ReconstructionExecuter::assembleReconstructionPipeline(std::vector<ReconstructCorePtr> cores, ReconstructCore::InputParams par, USReconstructInputData fileData)
102 {
104 
105  ReconstructPreprocessorPtr preprocessor = this->createPreprocessor(par, fileData);
106  pipeline->append(ThreadedTimedReconstructPreprocessor::create(mPatientModelService, preprocessor, cores));
107 
108  cx::CompositeTimedAlgorithmPtr temp = pipeline;
109  if(this->canCoresRunInParallel(cores) && cores.size()>1)
110  {
112  pipeline->append(parallel);
113  temp = parallel;
114  reportDebug("Running reconstruction cores in parallel.");
115  }
116 
117  for (unsigned i=0; i<cores.size(); ++i)
118  temp->append(ThreadedTimedReconstructCore::create(mPatientModelService, mViewService, cores[i]));
119 
120  return pipeline;
121 }
122 
123 bool ReconstructionExecuter::canCoresRunInParallel(std::vector<ReconstructCorePtr> cores)
124 {
125  bool parallelizable = true;
126 
127  std::vector<ReconstructCorePtr>::iterator it;
128  for(it = cores.begin(); it != cores.end(); ++it)
129  parallelizable = parallelizable && (it->get()->getInputParams().mAlgorithmUid == "pnn");
130 
131  return parallelizable;
132 }
133 
134 ReconstructPreprocessorPtr ReconstructionExecuter::createPreprocessor(ReconstructCore::InputParams par, USReconstructInputData fileData)
135 {
136  ReconstructPreprocessorPtr retval(new ReconstructPreprocessor(mPatientModelService));
137  if(fileData.isValid())
138  retval->initialize(par, fileData);
139 
140  return retval;
141 }
142 
143 std::vector<ReconstructCorePtr> ReconstructionExecuter::createCores(ReconstructionMethodService* algo, ReconstructCore::InputParams par, bool createBModeWhenAngio)
144 {
145  std::vector<ReconstructCorePtr> retval;
146 
147  if (createBModeWhenAngio && par.mAngio)
148  {
149  ReconstructCorePtr core = this->createBModeCore(par, algo);
150  if (core)
151  retval.push_back(core);
152  core = this->createCore(par, algo);
153  if (core)
154  retval.push_back(core);
155  }
156  // only one thread
157  else
158  {
159  ReconstructCorePtr core = this->createCore(par, algo);
160  if (core)
161  retval.push_back(core);
162  }
163 
164  return retval;
165 }
166 
167 ReconstructCorePtr ReconstructionExecuter::createCore(ReconstructCore::InputParams par, ReconstructionMethodService* algo)
168 {
169  ReconstructCorePtr retval(new ReconstructCore(mPatientModelService));
170  retval->initialize(par, algo);
171  return retval;
172 }
173 
174 ReconstructCorePtr ReconstructionExecuter::createBModeCore(ReconstructCore::InputParams par, ReconstructionMethodService* algo)
175 {
176  ReconstructCorePtr retval(new ReconstructCore(mPatientModelService));
177  par.mAngio = false;
178  par.mTransferFunctionPreset = "US B-Mode";
179  retval->initialize(par, algo);
180  return retval;
181 }
182 
183 
184 
185 } /* namespace cx */
boost::shared_ptr< class CompositeTimedAlgorithm > CompositeTimedAlgorithmPtr
void reportError(QString msg)
Definition: cxLogger.cpp:71
Abstract interface for reconstruction algorithm.
bool startNonThreadedReconstruction(ReconstructionMethodService *algo, ReconstructCore::InputParams par, USReconstructInputData fileData, bool createBModeWhenAngio)
void reconstructAboutToStart()
emitted before reconstruction threads are fired
static ThreadedTimedReconstructCorePtr create(PatientModelServicePtr patientModelService, ViewServicePtr viewService, ReconstructCorePtr reconstructer)
boost::shared_ptr< class TimedBaseAlgorithm > TimedAlgorithmPtr
static ThreadedTimedReconstructPreprocessorPtr create(PatientModelServicePtr patientModelService, ReconstructPreprocessorPtr input, std::vector< ReconstructCorePtr > cores)
std::vector< cx::ImagePtr > getResult()
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
boost::shared_ptr< class ReconstructPreprocessor > ReconstructPreprocessorPtr
Algorithm part of reconstruction - no dependencies on parameter classes.
boost::shared_ptr< CompositeParallelTimedAlgorithm > CompositeParallelTimedAlgorithmPtr
#define CX_LOG_WARNING
Definition: cxLogger.h:98
bool mAngio
true for angio data, false is B-mode.
cx::TimedAlgorithmPtr getThread()
Return the currently reconstructing thread object.
bool startReconstruction(ReconstructionMethodService *algo, ReconstructCore::InputParams par, USReconstructInputData fileData, bool createBModeWhenAngio)
Algorithm part of reconstruction - no dependencies on parameter classes.
boost::shared_ptr< CompositeSerialTimedAlgorithm > CompositeSerialTimedAlgorithmPtr
boost::shared_ptr< class ReconstructCore > ReconstructCorePtr
void reportDebug(QString msg)
Definition: cxLogger.cpp:68
Namespace for all CustusX production code.