GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
QGoMeshLevelSetAlgo.h
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  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  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
34 #ifndef __QGoMeshLevelSetAlgo_h
35 #define __QGoMeshLevelSetAlgo_h
36 
37 #include "QGoLevelSetAlgo.h"
38 #include "QGoFilterChanAndVese.h"
39 #include "QGoAlgorithmWidget.h"
40 #include "QGoAlgoParameter.h"
41 #include "QGoGUILibConfigure.h"
42 #include "vtkSmartPointer.h"
43 #include "vtkPolyData.h"
44 #include "vtkImageData.h"
45 #include "vtkTransform.h"
46 #include "vtkTransformPolyDataFilter.h"
47 
48 #include "GoImageProcessor.h"
49 
50 
57 {
58 public:
59  QGoMeshLevelSetAlgo(std::vector< vtkPoints* >* iSeeds, QWidget *iParent = 0);
61 
62  std::vector<vtkPolyData*> ApplyAlgo(
63  GoImageProcessor* iImages,
64  std::string iChannel,
65  bool iIsInvertedOn = false);
66 
67 protected:
68 
69  template < class TPixel >
70  // note this will work only in 3D, so we can remove the template
71  // parameter on the image dimension
72  //unsigned int VImageDimension >
73  vtkPolyData * ApplyLevelSetFilter(
74  const std::vector<double>& iCenter,
75  typename itk::Image< TPixel, 3 >::Pointer iImages)
76  {
77  assert( iCenter.size() == 3);
78 
79  const unsigned int ImageDimension = 3;
80 
81  typedef TPixel PixelType;
82 
83  typedef itk::Image< PixelType, ImageDimension > ImageType;
84  typedef typename ImageType::Pointer ImagePointer;
85 
86  // let's compute the bounds of the region of interest
87  double radius = this->m_Radius->GetValue();
88 
89  std::vector< double > bounds( 2 * ImageDimension, 0. );
90  unsigned int k = 0;
91  for( unsigned int dim = 0; dim < ImageDimension; dim++ )
92  {
93  bounds[k++] = iCenter[dim] - 2. * radius;
94  bounds[k++] = iCenter[dim] + 2. * radius;
95  }
96 
97  // then let's extract the Region of Interest
98  ImagePointer ITK_ROI_Image =
99  this->ITKExtractROI< PixelType, ImageDimension >( bounds, iImages );
100 
101  // Compute the segmentation in 3D
102  // why no call to the filter itself...?
103  QGoFilterChanAndVese Filter;
104  Filter.Apply3DFilter< PixelType >( ITK_ROI_Image,
105  iCenter,
106  0, // we dont want to extract ROI from input since we already did
107  this->m_Iterations->GetValue(),
108  this->m_Curvature->GetValue());
109 
111  ItkOutPut = Filter.GetOutput3D();
112 
113  // Here it would be better if the mesh extraction would be performed directly
114  // in ITK instead.
115  vtkImageData * FilterOutPutToVTK =
116  this->ConvertITK2VTK<
118  ImageDimension>( ItkOutPut );
119 
120  vtkPolyData* temp_output = this->ExtractPolyData(FilterOutPutToVTK, 0);
121  FilterOutPutToVTK->Delete();
122 
123  double temp_bounds[6];
124  temp_output->GetBounds( temp_bounds );
125 
126  double temp_center[3];
127  temp_center[0] = ( temp_bounds[0] + temp_bounds[1] ) * 0.5;
128  temp_center[1] = ( temp_bounds[2] + temp_bounds[3] ) * 0.5;
129  temp_center[2] = ( temp_bounds[4] + temp_bounds[5] ) * 0.5;
130 
131  vtkSmartPointer< vtkTransform > translation =
132  vtkSmartPointer< vtkTransform >::New();
133 
135  translation->Translate(iCenter[0] - temp_center[0],
136  iCenter[1] - temp_center[1],
137  iCenter[2] - temp_center[2] );
138 
139  vtkSmartPointer< vtkTransformPolyDataFilter > mesh_transform =
140  vtkSmartPointer< vtkTransformPolyDataFilter >::New();
141  mesh_transform->SetTransform(translation);
142  mesh_transform->SetInput( temp_output );
143  mesh_transform->Update();
144  temp_output->Delete();
145 
146  // MIGHT LEAK! CHECK IT IS DELETED!
147  vtkPolyData* mesh = vtkPolyData::New();
148  mesh->DeepCopy( mesh_transform->GetOutput() );
149 
150  return mesh;
151  }
152 };
153 
154 #endif
vtkPolyData * ApplyLevelSetFilter(const std::vector< double > &iCenter, typename itk::Image< TPixel, 3 >::Pointer iImages)
Levelset segmentation algorithm implementation. Can generate contours and meshes. Will generate 2D ob...
void Apply3DFilter(typename itk::Image< TPixel, 3 >::Pointer iITKInput, const std::vector< double > &iCenter, const double &iRadius, const int &iIterations, const int &iCurvature)
Output3DType::Pointer Output3DPointer
QGoMeshLevelSetAlgo(std::vector< vtkPoints * > *iSeeds, QWidget *iParent=0)
std::vector< vtkPolyData * > ExtractPolyData(std::vector< vtkImageData * > &iInputImage, const double &iThreshold)
QGoAlgoParameter< double > * m_Radius
QGoAlgoParameter< int > * m_Iterations
class to be the interface between the levelset algo for meshes, contours and set of contours and GoFi...
std::vector< vtkPolyData * > ApplyAlgo(GoImageProcessor *iImages, std::string iChannel, bool iIsInvertedOn=false)
return the vtkpolydata created by the algorithm
QGoAlgoParameter< int > * m_Curvature
Interface between image reader and vtkImageData.
vtkImageData * ConvertITK2VTK(typename itk::Image< PixelType, VImageDimension >::Pointer iInput)
class to be the interface between the levelset algo for meshes and GoFigure