Fraxinus  16.5.0-fx-rc6
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxAirwaysFilterService.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 #include "cxAirwaysFilterService.h"
34 
35 #include <QTimer>
36 
37 #include <vtkImageImport.h>
38 #include <vtkImageData.h>
39 #include <vtkImageShiftScale.h>
40 #include <ctkPluginContext.h>
41 
42 #include "cxTime.h"
43 #include "cxTypeConversions.h"
44 #include "cxLogger.h"
45 #include "cxDataReaderWriter.h"
47 #include "cxDoubleProperty.h"
48 #include "cxContourFilter.h"
49 #include "cxDataLocations.h"
51 #include "vtkForwardDeclarations.h"
53 #include "cxVisServices.h"
54 #include "cxUtilHelpers.h"
55 // Test
56 #include "FAST/Algorithms/AirwaySegmentation/AirwaySegmentation.hpp"
57 #include "FAST/Algorithms/CenterlineExtraction/CenterlineExtraction.hpp"
58 #include "FAST/Importers/ImageFileImporter.hpp"
59 #include "FAST/Exporters/VTKImageExporter.hpp"
60 #include "FAST/Exporters/VTKLineSetExporter.hpp"
61 #include "FAST/Data/Segmentation.hpp"
62 #include "FAST/SceneGraph.hpp"
63 
64 namespace cx {
65 
67  FilterImpl(services)
68 {
69 
70 }
71 
72 QString AirwaysFilter::getName() const
73 {
74  return "Airway Segmentation Filter";
75 }
76 
77 QString AirwaysFilter::getType() const
78 {
79  return "AirwaysFilter";
80 }
81 
82 QString AirwaysFilter::getHelp() const
83 {
84  return "<html>"
85  "<h3>Airway Segmentation.</h3>"
86  "<p><i>Extracts segmentation and centerline from a CT volume. If method fails, try to crop volume. </br>Algorithm written by Erik Smistad.</i></p>"
87  "</html>";
88 }
89 
91 {
92  CX_LOG_INFO() << "EXECUTING AIRWAYS FILTER";
93  ImagePtr input = this->getCopiedInputImage();
94  if (!input)
95  return false;
96  mInputImage = input;
97  std::string filename = (patientService()->getActivePatientFolder()+"/"+mInputImage->getFilename()).toStdString();
98 
99  try {
100  QString kernelDir = cx::DataLocations::findConfigFolder("/FAST", FAST_SOURCE_DIR);
101  fast::DeviceManager::getInstance().setKernelRootPath(kernelDir.toStdString());
102  // Import image data from disk
103  fast::ImageFileImporter::pointer importer = fast::ImageFileImporter::New();
104  importer->setFilename(filename);
105 
106  // Need to know the data type
107  importer->update();
108  fast::Image::pointer image = importer->getOutputData<fast::Image>();
109 
110  // Do segmentation
111  fast::AirwaySegmentation::pointer segmentation = fast::AirwaySegmentation::New();
112  segmentation->setInputConnection(importer->getOutputPort());
113 
114  // Convert fast segmentation data to VTK data which CX can use
115  vtkSmartPointer<fast::VTKImageExporter> vtkExporter = fast::VTKImageExporter::New();
116  vtkExporter->setInputConnection(segmentation->getOutputPort());
117  vtkExporter->Update();
118  mSegmentationOutput = vtkExporter->GetOutput();
119  CX_LOG_SUCCESS() << "FINISHED AIRWAY SEGMENTATION";
120 
121  // Get output segmentation data
122  fast::Segmentation::pointer segmentationData = segmentation->getOutputData<fast::Segmentation>(0);
123 
124  //HACK: there is some kind of race condition where data is not ready to be accessed, thus we need to wait a bit
125  //This only happens on mac release build (disapears if cout's are added for example.)
126  sleep_ms(500);
127  std::cout << segmentationData << std::endl;
128  std::cout << segmentationData->getTimestamp() << std::endl;
129 
130  // Get the transformation of the segmentation
131  Eigen::Affine3f T = fast::SceneGraph::getEigenAffineTransformationFromData(segmentationData);
132  mTransformation.matrix() = T.matrix().cast<double>(); // cast to double
133 
134  // Extract centerline
135  fast::CenterlineExtraction::pointer centerline = fast::CenterlineExtraction::New();
136  centerline->setInputConnection(segmentation->getOutputPort());
137 
138  // Get centerline
139  vtkSmartPointer<fast::VTKLineSetExporter> vtkCenterlineExporter = fast::VTKLineSetExporter::New();
140  vtkCenterlineExporter->setInputConnection(centerline->getOutputPort());
141  mCenterlineOutput = vtkCenterlineExporter->GetOutput();
142  vtkCenterlineExporter->Update();
143 
144  } catch(fast::Exception& e) {
145  std::string error = e.what();
146  reportError("fast::Exception: "+qstring_cast(error));
147 
148  return false;
149  } catch(cl::Error& e) {
150  reportError("cl::Error:"+qstring_cast(e.what()));
151 
152  return false;
153  } catch (std::exception& e){
154  reportError("std::exception:"+qstring_cast(e.what()));
155 
156  return false;
157  } catch (...){
158  reportError("Airway segmentation algorithm threw a unknown exception.");
159 
160  return false;
161  }
162  return true;
163 }
164 
166 {
167  if(!mSegmentationOutput)
168  return false;
169 
170  std::cout << "POST PROCESS" << std::endl;
171 
172  // Make contour of segmented volume
173  double threshold = 1;
175  mSegmentationOutput,
176  threshold,
177  false, // reduce resolution
178  true, // smoothing
179  true, // keep topology
180  0 // target decimation
181  );
182  //outputSegmentation->get_rMd_History()->setRegistration(rMd_i);
183  //patientService()->insertData(outputSegmentation);
184 
185  // Add contour internally to cx
187  patientService(),
188  rawContour,
189  mInputImage,
190  QColor("green")
191  );
192  contour->get_rMd_History()->setRegistration(mTransformation);
193 
194  // Set output
195  mOutputTypes[1]->setValue(contour->getUid());
196 
197  // TODO get centerline somehow
198  QString uid = mInputImage->getUid() + "_centerline%1";
199  QString name = mInputImage->getName() + " centerline%1";
200  MeshPtr centerline = patientService()->createSpecificData<Mesh>(uid, name);
201  centerline->setVtkPolyData(mCenterlineOutput);
202  centerline->get_rMd_History()->setParentSpace(mInputImage->getUid());
203  centerline->get_rMd_History()->setRegistration(mTransformation);
204  patientService()->insertData(centerline);
205  mOutputTypes[0]->setValue(centerline->getUid());
206 
207  return true;
208 }
209 
211 {
212  //mOptionsAdapters.push_back(getNoiseLevelOption(mOptions));
213 }
214 
216 {
218 
220  temp->setValueName("Input");
221  temp->setHelp("Select input to run airway segmentation on.");
222  mInputTypes.push_back(temp);
223 }
224 
226 {
227  StringPropertySelectMeshPtr tempMeshStringAdapter;
228 
229  //0
230  tempMeshStringAdapter = StringPropertySelectMesh::New(patientService());
231  tempMeshStringAdapter->setValueName("Centerline");
232  tempMeshStringAdapter->setHelp("Generated centerline mesh (vtk-format).");
233  mOutputTypes.push_back(tempMeshStringAdapter);
234 
235  //1
236  tempMeshStringAdapter = StringPropertySelectMesh::New(patientService());
237  tempMeshStringAdapter->setValueName("Segmentation");
238  tempMeshStringAdapter->setHelp("Generated surface of the segmented volume.");
239  mOutputTypes.push_back(tempMeshStringAdapter);
240 
241 }
242 
243 /*
244 DoublePropertyPtr AirwaysFilter::getNoiseLevelOption(QDomElement root)
245 {
246  DoublePropertyPtr retval = DoubleProperty::initialize("Noise level",
247  "", "Select the amount of noise present in the image", 0.5,
248  DoubleRange(0.0, 2, 0.5), 1, root);
249  retval->setGuiRepresentation(DoubleProperty::grSLIDER);
250  return retval;
251 }
252 */
253 
255 }
256 
257 } /* namespace cx */
258 
QString qstring_cast(const T &val)
std::vector< SelectDataStringPropertyBasePtr > mInputTypes
Definition: cxFilterImpl.h:94
void reportError(QString msg)
Definition: cxLogger.cpp:92
A mesh data set.
Definition: cxMesh.h:61
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:62
#define CX_LOG_INFO
Definition: cxLogger.h:111
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
virtual void createOutputTypes()
AirwaysFilter(VisServicesPtr services)
static QString findConfigFolder(QString pathRelativeToConfigRoot, QString alternativeAbsolutePath="")
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
boost::shared_ptr< class SelectDataStringPropertyBase > SelectDataStringPropertyBasePtr
PatientModelServicePtr patientService()
virtual bool execute()
void setVtkPolyData(const vtkPolyDataPtr &polyData)
Definition: cxMesh.cpp:100
ImagePtr getCopiedInputImage(int index=0)
#define CX_LOG_SUCCESS
Definition: cxLogger.h:112
virtual QString getType() const
std::vector< SelectDataStringPropertyBasePtr > mOutputTypes
Definition: cxFilterImpl.h:95
static StringPropertySelectMeshPtr New(PatientModelServicePtr patientModelService)
virtual bool postProcess()
static StringPropertySelectImagePtr New(PatientModelServicePtr patientModelService)
boost::shared_ptr< class Mesh > MeshPtr
void sleep_ms(int ms)
boost::shared_ptr< class StringPropertySelectMesh > StringPropertySelectMeshPtr
virtual QString getName() const
virtual QString getHelp() const