MeshFilter.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2016 Imperial College London
5  * Copyright 2013-2016 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_MeshFilter_H
21 #define MIRTK_MeshFilter_H
22 
23 #include "mirtk/Object.h"
24 
25 #include "mirtk/EdgeTable.h"
26 #include "mirtk/Memory.h"
27 
28 #include "vtkType.h"
29 #include "vtkSmartPointer.h"
30 #include "vtkPolyData.h"
31 
32 
33 namespace mirtk {
34 
35 
36 /**
37  * Base class for filters which process discrete surface and/or volumetric meshes
38  */
39 class MeshFilter : public Object
40 {
41  mirtkAbstractMacro(MeshFilter);
42 
43  // ---------------------------------------------------------------------------
44  // Attributes
45 
46  /// Input mesh
47  mirtkPublicAttributeMacro(vtkSmartPointer<vtkPolyData>, Input);
48 
49  /// Precomputed edge table of input surface mesh
50  mirtkPublicAttributeMacro(SharedPtr<const class EdgeTable>, EdgeTable);
51 
52  /// Output surface mesh (NULL if filter does not produce polygonal output)
53  mirtkReadOnlyAttributeMacro(vtkSmartPointer<vtkPolyData>, Output);
54 
55  /// Whether to output floating point results in double precision
56  /// If \c true, output data arrays are of type vtkDoubleArray.
57  /// If \c false, output data arrays are of type vtkFloatArray.
58  mirtkPublicAttributeMacro(bool, DoublePrecision);
59 
60  /// Copy attributes of this filter from another instance
61  void CopyAttributes(const MeshFilter &);
62 
63  // ---------------------------------------------------------------------------
64  // Construction/Destruction
65 
66 protected:
67 
68  /// Default constructor
69  MeshFilter();
70 
71  /// Copy constructor
72  MeshFilter(const MeshFilter &);
73 
74  /// Assignment operator
76 
77  /// Destructor
78  virtual ~MeshFilter();
79 
80  // ---------------------------------------------------------------------------
81  // Execution
82 
83 public:
84 
85  /// Run filter
86  virtual void Run();
87 
88 protected:
89 
90  /// Initialize filter after input and parameters are set
91  virtual void Initialize();
92 
93  /// Execute filter
94  virtual void Execute() = 0;
95 
96  /// Finalize filter execution
97  virtual void Finalize();
98 
99  // ---------------------------------------------------------------------------
100  // Alternative VTK-like API
101 
102 public:
103 
104  /// Enable/disable double precision output
105  mirtkOnOffMacro(DoublePrecision);
106 
107  /// Set input surface mesh
108  void SetInputData(vtkPolyData *);
109 
110  /// Set input surface mesh
111  void SetInput(vtkPolyData *);
112 
113  /// Run filter
114  void Update();
115 
116  /// Get output surface mesh
117  vtkPolyData *GetOutput();
118 
119  // ---------------------------------------------------------------------------
120  // Auxiliaries
121 
122 protected:
123 
124  /// To be called by subclass in Initialize when _EdgeTable is needed
125  virtual void InitializeEdgeTable();
126 
127  /// Allocate new data set attributes array
128  ///
129  /// \param[in] name Name of data array.
130  /// \param[in] n Number of tuples.
131  /// \param[in] c Number of components.
132  /// \param[in] type Type of VTK array. When VTK_VOID, a floating point array
133  /// is allocated with either single or double precision depending
134  /// on the DoublePrecision flag of this mesh filter.
135  ///
136  /// \returns New floating point array
137  vtkSmartPointer<vtkDataArray> NewArray(const char *name, vtkIdType n, int c,
138  int type = VTK_VOID) const;
139 
140  /// Allocate new point data array
141  ///
142  /// \param[in] name Name of data array.
143  /// \param[in] c Number of components.
144  /// \param[in] type Type of VTK array. When VTK_VOID, a floating point array
145  /// is allocated with either single or double precision depending
146  /// on the DoublePrecision flag of this mesh filter.
147  ///
148  /// \returns New floating point array
149  ///
150  /// \deprecated Use NewPointArray instead.
151  vtkSmartPointer<vtkDataArray> NewArray(const char *name, int c = 1,
152  int type = VTK_VOID) const;
153 
154  /// Allocate new point data array
155  ///
156  /// \param[in] name Name of data array.
157  /// \param[in] c Number of components.
158  /// \param[in] type Type of VTK array. When VTK_VOID, a floating point array
159  /// is allocated with either single or double precision depending
160  /// on the DoublePrecision flag of this mesh filter.
161  ///
162  /// \returns New floating point array
163  vtkSmartPointer<vtkDataArray> NewPointArray(const char *name, int c = 1,
164  int type = VTK_VOID) const;
165 
166  /// Allocate new cell data array
167  ///
168  /// \param[in] name Name of data array.
169  /// \param[in] c Number of components.
170  /// \param[in] type Type of VTK array. When VTK_VOID, a floating point array
171  /// is allocated with either single or double precision depending
172  /// on the DoublePrecision flag of this mesh filter.
173  ///
174  /// \returns New floating point array
175  vtkSmartPointer<vtkDataArray> NewCellArray(const char *name, int c = 1,
176  int type = VTK_VOID) const;
177 };
178 
179 ////////////////////////////////////////////////////////////////////////////////
180 // Inline definitions
181 ////////////////////////////////////////////////////////////////////////////////
182 
183 // =============================================================================
184 // Alternative VTK-like API
185 // =============================================================================
186 
187 // -----------------------------------------------------------------------------
188 inline void MeshFilter::SetInputData(vtkPolyData *poly)
189 {
190  this->Input(poly);
191 }
192 
193 // -----------------------------------------------------------------------------
194 inline void MeshFilter::SetInput(vtkPolyData *poly)
195 {
196  this->Input(poly);
197 }
198 
199 // -----------------------------------------------------------------------------
200 inline void MeshFilter::Update()
201 {
202  this->Run();
203 }
204 
205 // -----------------------------------------------------------------------------
206 inline vtkPolyData *MeshFilter::GetOutput()
207 {
208  return this->Output();
209 }
210 
211 
212 } // namespace mirtk
213 
214 #endif // MIRTK_MeshFilter_H
vtkPolyData * GetOutput()
Get output surface mesh.
Definition: MeshFilter.h:206
MeshFilter()
Default constructor.
vtkSmartPointer< vtkDataArray > NewCellArray(const char *name, int c=1, int type=VTK_VOID) const
virtual void Execute()=0
Execute filter.
virtual ~MeshFilter()
Destructor.
vtkSmartPointer< vtkDataArray > NewPointArray(const char *name, int c=1, int type=VTK_VOID) const
Definition: IOConfig.h:41
void SetInputData(vtkPolyData *)
Set input surface mesh.
Definition: MeshFilter.h:188
void Update()
Run filter.
Definition: MeshFilter.h:200
virtual void InitializeEdgeTable()
To be called by subclass in Initialize when _EdgeTable is needed.
vtkSmartPointer< vtkDataArray > NewArray(const char *name, vtkIdType n, int c, int type=VTK_VOID) const
virtual void Finalize()
Finalize filter execution.
virtual void Run()
Run filter.
virtual void Initialize()
Initialize filter after input and parameters are set.
void SetInput(vtkPolyData *)
Set input surface mesh.
Definition: MeshFilter.h:194
MeshFilter & operator=(const MeshFilter &)
Assignment operator.
mirtkOnOffMacro(DoublePrecision)
Enable/disable double precision output.