CustusX  15.3.4-beta
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  mCores = this->createCores(algo, par, createBModeWhenAngio);
80  if (mCores.empty())
81  reportWarning("Failed to start reconstruction");
82 
83  cx::CompositeTimedAlgorithmPtr algorithm = this->assembleReconstructionPipeline(mCores, par, fileData);
84  this->launch(algorithm);
85 }
86 
87 std::vector<cx::ImagePtr> ReconstructionExecuter::getResult()
88 {
89  std::vector<cx::ImagePtr> retval;
90  if (mPipeline && !mPipeline->isFinished())
91  return retval;
92 
93  for (unsigned i=0; i<mCores.size(); ++i)
94  retval.push_back(mCores[i]->getOutput());
95 
96  return retval;
97 }
98 
99 void ReconstructionExecuter::launch(cx::TimedAlgorithmPtr thread)
100 {
101  mPipeline = thread;
103  connect(thread.get(), SIGNAL(finished()), this, SIGNAL(reconstructFinished()));
104  thread->execute();
105  emit reconstructStarted();
106 }
107 
108 cx::CompositeTimedAlgorithmPtr ReconstructionExecuter::assembleReconstructionPipeline(std::vector<ReconstructCorePtr> cores, ReconstructCore::InputParams par, USReconstructInputData fileData)
109 {
111 
112  ReconstructPreprocessorPtr preprocessor = this->createPreprocessor(par, fileData);
113  pipeline->append(ThreadedTimedReconstructPreprocessor::create(mPatientModelService, preprocessor, cores));
114 
115  cx::CompositeTimedAlgorithmPtr temp = pipeline;
116  if(this->canCoresRunInParallel(cores) && cores.size()>1)
117  {
119  pipeline->append(parallel);
120  temp = parallel;
121  reportDebug("Running reconstruction cores in parallel.");
122  }
123 
124  for (unsigned i=0; i<cores.size(); ++i)
125  temp->append(ThreadedTimedReconstructCore::create(mPatientModelService, mVisualizationService, cores[i]));
126 
127  return pipeline;
128 }
129 
130 bool ReconstructionExecuter::canCoresRunInParallel(std::vector<ReconstructCorePtr> cores)
131 {
132  bool parallelizable = true;
133 
134  std::vector<ReconstructCorePtr>::iterator it;
135  for(it = cores.begin(); it != cores.end(); ++it)
136  parallelizable = parallelizable && (it->get()->getInputParams().mAlgorithmUid == "PNN");
137 
138  return parallelizable;
139 }
140 
141 ReconstructPreprocessorPtr ReconstructionExecuter::createPreprocessor(ReconstructCore::InputParams par, USReconstructInputData fileData)
142 {
143  ReconstructPreprocessorPtr retval(new ReconstructPreprocessor(mPatientModelService));
144  retval->initialize(par, fileData);
145 
146  return retval;
147 }
148 
149 std::vector<ReconstructCorePtr> ReconstructionExecuter::createCores(ReconstructionMethodService* algo, ReconstructCore::InputParams par, bool createBModeWhenAngio)
150 {
151  std::vector<ReconstructCorePtr> retval;
152 
153  if (createBModeWhenAngio && par.mAngio)
154  {
155  ReconstructCorePtr core = this->createBModeCore(par, algo);
156  if (core)
157  retval.push_back(core);
158  core = this->createCore(par, algo);
159  if (core)
160  retval.push_back(core);
161  }
162  // only one thread
163  else
164  {
165  ReconstructCorePtr core = this->createCore(par, algo);
166  if (core)
167  retval.push_back(core);
168  }
169 
170  return retval;
171 }
172 
173 ReconstructCorePtr ReconstructionExecuter::createCore(ReconstructCore::InputParams par, ReconstructionMethodService* algo)
174 {
175  ReconstructCorePtr retval(new ReconstructCore(mPatientModelService));
176  retval->initialize(par, algo);
177  return retval;
178 }
179 
180 ReconstructCorePtr ReconstructionExecuter::createBModeCore(ReconstructCore::InputParams par, ReconstructionMethodService* algo)
181 {
182  ReconstructCorePtr retval(new ReconstructCore(mPatientModelService));
183  par.mAngio = false;
184  par.mTransferFunctionPreset = "US B-Mode";
185  retval->initialize(par, algo);
186  return retval;
187 }
188 
189 
190 
191 } /* 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