Chaste  Build::
ImageToSurface.cpp
1 /*
2  Copyright (c) 2005-2015, University of Oxford.
3  All rights reserved.
4 
5  University of Oxford means the Chancellor, Masters and Scholars of the
6  University of Oxford, having an administrative office at Wellington
7  Square, Oxford OX1 2JD, UK.
8 
9  This file is part of Chaste.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are met:
13  * Redistributions of source code must retain the above copyright notice,
14  this list of conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright notice,
16  this list of conditions and the following disclaimer in the documentation
17  and/or other materials provided with the distribution.
18  * Neither the name of the University of Oxford nor the names of its
19  contributors may be used to endorse or promote products derived from this
20  software without specific prior written permission.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33  */
34 
35 #include "Exception.hpp"
36 #include "ImageToSurface.hpp"
37 #include <vtkThreshold.h>
38 #include <vtkGeometryFilter.h>
39 #include <vtkMarchingCubes.h>
40 #include <vtkTriangleFilter.h>
41 #include <Debug.hpp>
42 
43 ImageToSurface::ImageToSurface()
44  : mpImage(vtkSmartPointer<vtkImageData>::New()),
45  mSegmentAboveThreshold(true),
46  mThreshold(1.0),
47  mpSurface(),
48  mUseMarchingCubes(false)
49 {
50 
51 }
52 
53 boost::shared_ptr<ImageToSurface> ImageToSurface::Create()
54 {
55  MAKE_PTR(ImageToSurface, pSelf);
56  return pSelf;
57 }
58 
59 ImageToSurface::~ImageToSurface()
60 {
61 
62 }
63 
64 vtkSmartPointer<vtkPolyData> ImageToSurface::GetOutput()
65 {
66  if(mpSurface)
67  {
68  return(mpSurface);
69  }
70  else
71  {
72  EXCEPTION("No output set. Did you run 'Update()' ?");
73  }
74 }
75 
76 void ImageToSurface::SetInput(vtkSmartPointer<vtkImageData> pImage)
77 {
78  mpImage = pImage;
79 }
80 
81 void ImageToSurface::SetInputRaw(vtkImageData* pImage)
82 {
83  mpImage.TakeReference(pImage);
84 // pImage->Delete();
85 }
86 
87 void ImageToSurface::SetThreshold(double threshold, bool segmentAboveThreshold)
88 {
89  mThreshold = threshold;
90  mSegmentAboveThreshold = segmentAboveThreshold;
91 }
92 
93 void ImageToSurface::SetUseMarchingCubes(bool useMarchingCubes)
94 {
95  mUseMarchingCubes = useMarchingCubes;
96 }
97 
98 void ImageToSurface::Update()
99 {
100  if(!mpImage)
101  {
102  EXCEPTION("No input set.");
103  }
104 
105  if(mUseMarchingCubes)
106  {
107  vtkSmartPointer<vtkMarchingCubes> p_marching_cubes = vtkSmartPointer<vtkMarchingCubes>::New();
108  #if VTK_MAJOR_VERSION <= 5
109  p_marching_cubes->SetInput(mpImage);
110  #else
111  p_marching_cubes->SetInputData(mpImage);
112  #endif
113 
114  p_marching_cubes->SetValue(0, mThreshold);
115  p_marching_cubes->Update();
116 
117  mpSurface = p_marching_cubes->GetOutput();
118  }
119  else
120  {
121  vtkSmartPointer<vtkThreshold> p_threshold = vtkSmartPointer<vtkThreshold>::New();
122  #if VTK_MAJOR_VERSION <= 5
123  p_threshold->SetInput(mpImage);
124  #else
125  p_threshold->SetInputData(mpImage);
126  #endif
127 
128  if(mSegmentAboveThreshold)
129  {
130  p_threshold->ThresholdByUpper(mThreshold);
131  }
132  else
133  {
134  p_threshold->ThresholdByLower(mThreshold);
135  }
136 
137  vtkSmartPointer<vtkGeometryFilter> p_geometry = vtkSmartPointer<vtkGeometryFilter>::New();
138  p_geometry->SetInputConnection(p_threshold->GetOutputPort());
139  p_geometry->Update();
140 
141  vtkSmartPointer<vtkTriangleFilter> p_triangle = vtkSmartPointer<vtkTriangleFilter>::New();
142  p_triangle->SetInputConnection(p_geometry->GetOutputPort());
143  p_triangle->Update();
144 
145  mpSurface = p_triangle->GetOutput();
146  }
147 }
vtkSmartPointer< vtkImageData > mpImage
Definition: ImageToMesh.hpp:55