NorMIT-nav  18.04-rc6
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  cx::ReconstructPreprocessorPtr preprocessor = this->createPreprocessor(par, fileData);
29  mCores = this->createCores(algo, par, createBModeWhenAngio);
30 
31  std::vector<bool> angio;
32  for (unsigned i=0; i<mCores.size(); ++i)
33  angio.push_back(mCores[i]->getInputParams().mAngio);
34 
35  std::vector<cx::ProcessedUSInputDataPtr> processedInput = preprocessor->createProcessedInput(angio);
36 
37  for (unsigned i=0; i<mCores.size(); ++i)
38  {
39  mCores[i]->initialize(processedInput[i], preprocessor->getOutputVolumeParams());
40  }
41  for (unsigned i=0; i<mCores.size(); ++i)
42  {
43  mCores[i]->reconstruct();
44  }
45 }
46 
48 {
49  if (mPipeline)
50  {
51  reportError("Reconstruct Executer can only be run once. Ignoring start.");
52  return;
53  }
54 
55  if (!fileData.isValid())
56  return;
57 
58  //Don't create an extra B-Mode volume if input data is 8 bit
59  if (fileData.is8bit())
60  createBModeWhenAngio = false;
61 
62  mCores = this->createCores(algo, par, createBModeWhenAngio);
63  if (mCores.empty())
64  reportWarning("Failed to start reconstruction");
65 
66  cx::CompositeTimedAlgorithmPtr algorithm = this->assembleReconstructionPipeline(mCores, par, fileData);
67  this->launch(algorithm);
68 }
69 
70 std::vector<cx::ImagePtr> ReconstructionExecuter::getResult()
71 {
72  std::vector<cx::ImagePtr> retval;
73  if (mPipeline && !mPipeline->isFinished())
74  return retval;
75 
76  for (unsigned i=0; i<mCores.size(); ++i)
77  retval.push_back(mCores[i]->getOutput());
78 
79  return retval;
80 }
81 
82 void ReconstructionExecuter::launch(cx::TimedAlgorithmPtr thread)
83 {
84  mPipeline = thread;
86  connect(thread.get(), SIGNAL(finished()), this, SIGNAL(reconstructFinished()));
87  thread->execute();
88  emit reconstructStarted();
89 }
90 
91 cx::CompositeTimedAlgorithmPtr ReconstructionExecuter::assembleReconstructionPipeline(std::vector<ReconstructCorePtr> cores, ReconstructCore::InputParams par, USReconstructInputData fileData)
92 {
94 
95  ReconstructPreprocessorPtr preprocessor = this->createPreprocessor(par, fileData);
96  pipeline->append(ThreadedTimedReconstructPreprocessor::create(mPatientModelService, preprocessor, cores));
97 
98  cx::CompositeTimedAlgorithmPtr temp = pipeline;
99  if(this->canCoresRunInParallel(cores) && cores.size()>1)
100  {
102  pipeline->append(parallel);
103  temp = parallel;
104  reportDebug("Running reconstruction cores in parallel.");
105  }
106 
107  for (unsigned i=0; i<cores.size(); ++i)
108  temp->append(ThreadedTimedReconstructCore::create(mPatientModelService, mViewService, cores[i]));
109 
110  return pipeline;
111 }
112 
113 bool ReconstructionExecuter::canCoresRunInParallel(std::vector<ReconstructCorePtr> cores)
114 {
115  bool parallelizable = true;
116 
117  std::vector<ReconstructCorePtr>::iterator it;
118  for(it = cores.begin(); it != cores.end(); ++it)
119  parallelizable = parallelizable && (it->get()->getInputParams().mAlgorithmUid == "pnn");
120 
121  return parallelizable;
122 }
123 
124 ReconstructPreprocessorPtr ReconstructionExecuter::createPreprocessor(ReconstructCore::InputParams par, USReconstructInputData fileData)
125 {
126  ReconstructPreprocessorPtr retval(new ReconstructPreprocessor(mPatientModelService));
127  retval->initialize(par, fileData);
128 
129  return retval;
130 }
131 
132 std::vector<ReconstructCorePtr> ReconstructionExecuter::createCores(ReconstructionMethodService* algo, ReconstructCore::InputParams par, bool createBModeWhenAngio)
133 {
134  std::vector<ReconstructCorePtr> retval;
135 
136  if (createBModeWhenAngio && par.mAngio)
137  {
138  ReconstructCorePtr core = this->createBModeCore(par, algo);
139  if (core)
140  retval.push_back(core);
141  core = this->createCore(par, algo);
142  if (core)
143  retval.push_back(core);
144  }
145  // only one thread
146  else
147  {
148  ReconstructCorePtr core = this->createCore(par, algo);
149  if (core)
150  retval.push_back(core);
151  }
152 
153  return retval;
154 }
155 
156 ReconstructCorePtr ReconstructionExecuter::createCore(ReconstructCore::InputParams par, ReconstructionMethodService* algo)
157 {
158  ReconstructCorePtr retval(new ReconstructCore(mPatientModelService));
159  retval->initialize(par, algo);
160  return retval;
161 }
162 
163 ReconstructCorePtr ReconstructionExecuter::createBModeCore(ReconstructCore::InputParams par, ReconstructionMethodService* algo)
164 {
165  ReconstructCorePtr retval(new ReconstructCore(mPatientModelService));
166  par.mAngio = false;
167  par.mTransferFunctionPreset = "US B-Mode";
168  retval->initialize(par, algo);
169  return retval;
170 }
171 
172 
173 
174 } /* namespace cx */
boost::shared_ptr< class CompositeTimedAlgorithm > CompositeTimedAlgorithmPtr
void reportError(QString msg)
Definition: cxLogger.cpp:71
Abstract interface for reconstruction algorithm.
void startReconstruction(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.
void startNonThreadedReconstruction(ReconstructionMethodService *algo, ReconstructCore::InputParams par, USReconstructInputData fileData, bool createBModeWhenAngio)
boost::shared_ptr< CompositeParallelTimedAlgorithm > CompositeParallelTimedAlgorithmPtr
bool mAngio
true for angio data, false is B-mode.
cx::TimedAlgorithmPtr getThread()
Return the currently reconstructing thread object.
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.