Fraxinus  17.12-rc1
An IGT application
cxReconstructCore.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 
33 
34 #include "cxReconstructCore.h"
35 #include <vtkImageData.h>
36 #include "cxTime.h"
37 #include "cxTypeConversions.h"
39 #include "cxVolumeHelpers.h"
41 #include "cxTimeKeeper.h"
42 #include "cxLogger.h"
43 #include "cxUSFrameData.h"
45 #include "vtkPointData.h"
46 #include "vtkDataArray.h"
47 #include "cxPatientModelService.h"
48 
49 namespace cx
50 {
51 
52 
54  mInput(InputParams()),
55  mPatientModelService(patientModelService)
56 {
57 // mMaxTimeDiff = 100; // TODO: Change default value for max allowed time difference between tracking and image time tags
58  mSuccess = false;
59 }
60 
62 {
63 }
64 
66 {
67  mInput = input;
68  mAlgorithm = algorithm;
69 }
70 
72 {
73  mOutputVolumeParams = outputVolumeParams;
74  mFileData = fileData;
75 }
76 
78 {
79  this->threadedPreReconstruct();
80 // this->threadablePreReconstruct();
81  this->threadedReconstruct();
83  return mOutput;
84 }
85 
90 {
91  if (!this->validInputData())
92  return;
93  mRawOutput = this->generateRawOutputVolume();
94 }
95 
100 {
101  if (!this->validInputData())
102  return;
103  CX_ASSERT(mRawOutput);
104 
105  TimeKeeper timer;
106 
107  mSuccess = mAlgorithm->reconstruct(mFileData, mRawOutput, mInput.mAlgoSettings);
108 
109  timer.printElapsedSeconds("Reconstruct core time");
110 }
111 
116 {
117  if (!this->validInputData())
118  return;
119 
120  if (mSuccess)
121  {
122  mOutput = this->generateOutputVolume(mRawOutput);
123 
124  Eigen::Array3i outputDims(mRawOutput->GetDimensions());
125  int total = outputDims[0] * outputDims[1] * outputDims[2];
126  report(QString("US Reconstruction complete: %1Mb, output=%2, algo=%3, preset=%4, angio=%5")
127  .arg(total/1024/1024)
128  .arg(mOutput->getName())
129  .arg(mAlgorithm->getName())
130  .arg(mInput.mTransferFunctionPreset)
131  .arg(mInput.mAngio));
132 
133  mPatientModelService->insertData(mOutput);
134  }
135  else
136  {
137  reportError("Reconstruction failed");
138  }
139 }
140 
145 ImagePtr ReconstructCore::generateOutputVolume(vtkImageDataPtr rawOutput)
146 {
147  setDeepModified(rawOutput);
148 
149 // //If no output path is selecetd, use the same path as the input
150 // QString filePath;
151 // if (mInput.mOutputBasePath.isEmpty() && mInput.mOutputRelativePath.isEmpty())
152 // filePath = qstring_cast(mFileData->getFilePath());
153 // else
154 // filePath = mInput.mOutputRelativePath;
155 
156  QString uid = this->generateOutputUid();
157  QString name = this->generateImageName(uid);
158 
159  ImagePtr image = mPatientModelService->createSpecificData<Image>(uid + "_%1", name + " %1");
160  image->setVtkImageData(rawOutput);
161 
162 // ImagePtr image = mPatientModelService->createImage(rawOutput, uid + "_%1", name + " %1", filePath);
163  image->get_rMd_History()->setRegistration(mOutputVolumeParams.get_rMd());
164  image->setModality("US");
165  if (mInput.mAngio)
166  image->setImageType("Angio");
167  else
168  image->setImageType("B-Mode");
169 
170  PresetTransferFunctions3DPtr presets = mPatientModelService->getPresetTransferFunctions3D();
171  presets->load(mInput.mTransferFunctionPreset, image, true, false);//Only apply to 2D, not 3D
172  presets->load("US B-Mode", image, false, true);//Only apply to 3D, not 2D
173 
174  return image;
175 }
176 
181 vtkImageDataPtr ReconstructCore::generateRawOutputVolume()
182 {
183  Eigen::Array3i dim = mOutputVolumeParams.getDim();
184  Vector3D spacing = Vector3D(1, 1, 1) * mOutputVolumeParams.getSpacing();
185  vtkImageDataPtr data = generateVtkImageData(dim, spacing, 0);
186  return data;
187 }
188 
194 QString ReconstructCore::generateOutputUid()
195 {
196  QString base = mFileData->getUid();
197  QString name = mFileData->getFilePath().split("/").back();
198  name = name.split(".").front();
199 
200  QStringList split = name.split("_");
201  QStringList prefix = split.front().split("-");
202  if (prefix.size() == 2)
203  {
204  split[0] = prefix[0];
205  }
206  else
207  {
208  split[0] += "_rec";
209  }
210 
211  return split.join("_");
212 }
213 
219 QString ReconstructCore::generateImageName(QString uid) const
220 {
221  QString name = uid.split("/").back();
222  name = name.split(".").front();
223  QString prefix = name.split("_").front(); // retrieve US-Acq part
224  prefix = prefix.split("-").front(); // retrieve US part.
225  if (prefix.isEmpty())
226  prefix = "US";
227 
228 // std::cout << "reconstruct input uid " << uid << std::endl;
229  if (mInput.mAngio) // tag angio images as such
230  prefix += "A";
231 
232  // retrieve index counter from _99_
233  QString counter = "";
234  QRegExp countReg("_[0-9]{1,2}_");
235  if (countReg.indexIn(name) > 0)
236  {
237  counter = countReg.cap(0).remove("_");
238  }
239 
240  // retrieve timestamp as HH:MM
241  QRegExp tsReg("[0-9]{8}T[0-9]{6}");
242  if (tsReg.indexIn(name) > 0)
243  {
244  QString postfix = name.split("_").back();
245  QDateTime datetime = QDateTime::fromString(tsReg.cap(0), timestampSecondsFormat());
246  QString timestamp = datetime.toString("hh:mm");
247  return prefix + " " + counter + " " + postfix + " " + timestamp;
248  }
249 
250  return name;
251 }
252 
254 {
255  return mOutput;
256 }
257 
258 bool ReconstructCore::validInputData() const
259 {
260  return mAlgorithm!=0;
261 }
262 
263 } /* namespace cx */
void reportError(QString msg)
Definition: cxLogger.cpp:92
Abstract interface for reconstruction algorithm.
#define CX_ASSERT(statement)
Definition: cxLogger.h:137
virtual void setVtkImageData(const vtkImageDataPtr &data, bool resetTransferFunctions=true)
Definition: cxImage.cpp:289
boost::shared_ptr< class TransferFunctions3DPresets > PresetTransferFunctions3DPtr
Definition: cxDataManager.h:57
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
Helper struct for sending and controlling output volume properties.
QString timestampSecondsFormat()
Definition: cxTime.cpp:39
A volumetric data set.
Definition: cxImage.h:66
virtual QString getName() const =0
void initialize(InputParams input, ReconstructionMethodService *algorithm)
boost::shared_ptr< class PatientModelService > PatientModelServicePtr
virtual bool reconstruct(ProcessedUSInputDataPtr input, vtkImageDataPtr outputData, QDomElement settings)=0
vtkImageDataPtr generateVtkImageData(Eigen::Array3i dim, Vector3D spacing, const unsigned char initValue, int components)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
void printElapsedSeconds(QString text="Elapsed time")
void report(QString msg)
Definition: cxLogger.cpp:90
void setDeepModified(vtkImageDataPtr image)
bool mAngio
true for angio data, false is B-mode.
ReconstructCore(PatientModelServicePtr patientModelService)
boost::shared_ptr< class ProcessedUSInputData > ProcessedUSInputDataPtr
vtkSmartPointer< class vtkImageData > vtkImageDataPtr
Namespace for all CustusX production code.