CustusX  15.8
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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) 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 #include "cxTimedAlgorithm.h"
35 #include "cxReconstructThreads.h"
36 
37 #include "cxLogger.h"
38 
39 namespace cx
40 {
41 
43 {
44  return mPipeline;
45 }
46 
48 {
49  cx::ReconstructPreprocessorPtr preprocessor = this->createPreprocessor(par, fileData);
50  mCores = this->createCores(algo, par, createBModeWhenAngio);
51 
52  std::vector<bool> angio;
53  for (unsigned i=0; i<mCores.size(); ++i)
54  angio.push_back(mCores[i]->getInputParams().mAngio);
55 
56  std::vector<cx::ProcessedUSInputDataPtr> processedInput = preprocessor->createProcessedInput(angio);
57 
58  for (unsigned i=0; i<mCores.size(); ++i)
59  {
60  mCores[i]->initialize(processedInput[i], preprocessor->getOutputVolumeParams());
61  }
62  for (unsigned i=0; i<mCores.size(); ++i)
63  {
64  mCores[i]->reconstruct();
65  }
66 }
67 
69 {
70  if (mPipeline)
71  {
72  reportError("Reconstruct Executer can only be run once. Ignoring start.");
73  return;
74  }
75 
76  if (!fileData.isValid())
77  return;
78 
79  //Don't create an extra B-Mode volume if input data is 8 bit
80  if (fileData.is8bit())
81  createBModeWhenAngio = false;
82 
83  mCores = this->createCores(algo, par, createBModeWhenAngio);
84  if (mCores.empty())
85  reportWarning("Failed to start reconstruction");
86 
87  cx::CompositeTimedAlgorithmPtr algorithm = this->assembleReconstructionPipeline(mCores, par, fileData);
88  this->launch(algorithm);
89 }
90 
91 std::vector<cx::ImagePtr> ReconstructionExecuter::getResult()
92 {
93  std::vector<cx::ImagePtr> retval;
94  if (mPipeline && !mPipeline->isFinished())
95  return retval;
96 
97  for (unsigned i=0; i<mCores.size(); ++i)
98  retval.push_back(mCores[i]->getOutput());
99 
100  return retval;
101 }
102 
103 void ReconstructionExecuter::launch(cx::TimedAlgorithmPtr thread)
104 {
105  mPipeline = thread;
107  connect(thread.get(), SIGNAL(finished()), this, SIGNAL(reconstructFinished()));
108  thread->execute();
109  emit reconstructStarted();
110 }
111 
112 cx::CompositeTimedAlgorithmPtr ReconstructionExecuter::assembleReconstructionPipeline(std::vector<ReconstructCorePtr> cores, ReconstructCore::InputParams par, USReconstructInputData fileData)
113 {
115 
116  ReconstructPreprocessorPtr preprocessor = this->createPreprocessor(par, fileData);
117  pipeline->append(ThreadedTimedReconstructPreprocessor::create(mPatientModelService, preprocessor, cores));
118 
119  cx::CompositeTimedAlgorithmPtr temp = pipeline;
120  if(this->canCoresRunInParallel(cores) && cores.size()>1)
121  {
123  pipeline->append(parallel);
124  temp = parallel;
125  reportDebug("Running reconstruction cores in parallel.");
126  }
127 
128  for (unsigned i=0; i<cores.size(); ++i)
129  temp->append(ThreadedTimedReconstructCore::create(mPatientModelService, mVisualizationService, cores[i]));
130 
131  return pipeline;
132 }
133 
134 bool ReconstructionExecuter::canCoresRunInParallel(std::vector<ReconstructCorePtr> cores)
135 {
136  bool parallelizable = true;
137 
138  std::vector<ReconstructCorePtr>::iterator it;
139  for(it = cores.begin(); it != cores.end(); ++it)
140  parallelizable = parallelizable && (it->get()->getInputParams().mAlgorithmUid == "PNN");
141 
142  return parallelizable;
143 }
144 
145 ReconstructPreprocessorPtr ReconstructionExecuter::createPreprocessor(ReconstructCore::InputParams par, USReconstructInputData fileData)
146 {
147  ReconstructPreprocessorPtr retval(new ReconstructPreprocessor(mPatientModelService));
148  retval->initialize(par, fileData);
149 
150  return retval;
151 }
152 
153 std::vector<ReconstructCorePtr> ReconstructionExecuter::createCores(ReconstructionMethodService* algo, ReconstructCore::InputParams par, bool createBModeWhenAngio)
154 {
155  std::vector<ReconstructCorePtr> retval;
156 
157  if (createBModeWhenAngio && par.mAngio)
158  {
159  ReconstructCorePtr core = this->createBModeCore(par, algo);
160  if (core)
161  retval.push_back(core);
162  core = this->createCore(par, algo);
163  if (core)
164  retval.push_back(core);
165  }
166  // only one thread
167  else
168  {
169  ReconstructCorePtr core = this->createCore(par, algo);
170  if (core)
171  retval.push_back(core);
172  }
173 
174  return retval;
175 }
176 
177 ReconstructCorePtr ReconstructionExecuter::createCore(ReconstructCore::InputParams par, ReconstructionMethodService* algo)
178 {
179  ReconstructCorePtr retval(new ReconstructCore(mPatientModelService));
180  retval->initialize(par, algo);
181  return retval;
182 }
183 
184 ReconstructCorePtr ReconstructionExecuter::createBModeCore(ReconstructCore::InputParams par, ReconstructionMethodService* algo)
185 {
186  ReconstructCorePtr retval(new ReconstructCore(mPatientModelService));
187  par.mAngio = false;
188  par.mTransferFunctionPreset = "US B-Mode";
189  retval->initialize(par, algo);
190  return retval;
191 }
192 
193 
194 
195 } /* namespace cx */
boost::shared_ptr< class CompositeTimedAlgorithm > CompositeTimedAlgorithmPtr
void reportError(QString msg)
Definition: cxLogger.cpp:92
Abstract interface for reconstruction algorithm.
static ThreadedTimedReconstructCorePtr create(PatientModelServicePtr patientModelService, VisualizationServicePtr visualizationService, ReconstructCorePtr reconstructer)
void startReconstruction(ReconstructionMethodService *algo, ReconstructCore::InputParams par, USReconstructInputData fileData, bool createBModeWhenAngio)
void reconstructAboutToStart()
emitted before reconstruction threads are fired
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:91
boost::shared_ptr< class ReconstructPreprocessor > ReconstructPreprocessorPtr
void startNonThreadedReconstruction(ReconstructionMethodService *algo, ReconstructCore::InputParams par, USReconstructInputData fileData, bool createBModeWhenAngio)
boost::shared_ptr< CompositeParallelTimedAlgorithm > CompositeParallelTimedAlgorithmPtr
cx::TimedAlgorithmPtr getThread()
Return the currently reconstructing thread object.
boost::shared_ptr< CompositeSerialTimedAlgorithm > CompositeSerialTimedAlgorithmPtr
boost::shared_ptr< class ReconstructCore > ReconstructCorePtr
void reportDebug(QString msg)
Definition: cxLogger.cpp:89