LinearFixedBoundarySurfaceMapper.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2016 Imperial College London
5  * Copyright 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_LinearFixedBoundarySurfaceMapper_H
21 #define MIRTK_LinearFixedBoundarySurfaceMapper_H
22 
23 #include "mirtk/FixedBoundarySurfaceMapper.h"
24 
25 #include "mirtk/Array.h"
26 
27 #include "vtkSmartPointer.h"
28 #include "vtkDataArray.h"
29 
30 
31 namespace mirtk {
32 
33 
34 /**
35  * Base class of discrete piecewise linear fixed boundary surface mapping methods
36  *
37  * For a given choice of edge weights, a sparse system of linear equations is solved
38  * iteratively to obtain a surface parameterization (Marchandise et al., 2014).
39  * Many published surface parameterization/texture mapping methods can be seen
40  * as spring and mass model, where the mesh vertices are the masses and the mesh
41  * edges are linear springs. The only difference between the different methods
42  * are the spring constants used, which result from a discretization/modeling
43  * of different map energies/properties.
44  *
45  * \todo Implement interactive boundary map update as proposed in
46  * Desbrun et al. (2002). Intrinsic parameterizations of surface meshes.
47  *
48  * \todo Implement boundary map optimization as proposed in
49  * Desbrun et al. (2002). Intrinsic parameterizations of surface meshes.
50  */
52 {
53  mirtkAbstractMacro(LinearFixedBoundarySurfaceMapper);
54 
55  // ---------------------------------------------------------------------------
56  // Attributes
57 
58  /// Maximum number of iterations
59  ///
60  /// When the number of iterations is set to 1 a sparse direct solver is used.
61  mirtkPublicAttributeMacro(int, NumberOfIterations);
62 
63  /// Tolerance for sparse linear solver
64  mirtkPublicAttributeMacro(double, Tolerance);
65 
66  /// Index of point in set of points with free (i >= 0) or fixed (i < 0) values
67  mirtkAttributeMacro(Array<int>, PointIndex);
68 
69  /// IDs of surface points with free map values
70  mirtkAttributeMacro(Array<int>, FreePoints);
71 
72  /// IDs of surface points with fixed map values
73  mirtkAttributeMacro(Array<int>, FixedPoints);
74 
75  /// Computed map values at surface points
76  mirtkAttributeMacro(vtkSmartPointer<vtkDataArray>, Values);
77 
78  /// Copy attributes of this class from another instance
79  void CopyAttributes(const LinearFixedBoundarySurfaceMapper &);
80 
81  // ---------------------------------------------------------------------------
82  // Construction/Destruction
83 
84 protected:
85 
86  /// Default constructor
88 
89  /// Copy constructor
91 
92  /// Assignment operator
94 
95 public:
96 
97  /// Destructor
99 
100  // ---------------------------------------------------------------------------
101  // Execution
102 
103  /// Initialize filter after input and parameters are set
104  virtual void Initialize();
105 
106  /// Compute map values at interior points
107  virtual void ComputeMap() = 0;
108 
109  /// Assemble output surface map
110  virtual void Finalize();
111 
112  // ---------------------------------------------------------------------------
113  // Auxiliaries
114 
115 public:
116 
117  /// Number of surface points with free map value
118  int NumberOfFreePoints() const;
119 
120  /// Number of surface points with fixed map value
121  int NumberOfFixedPoints() const;
122 
123  /// Whether the map value of the specified surface point is free
124  bool IsFreePoint(int i) const;
125 
126  /// Whether the map value of the specified surface point is fixed
127  bool IsFixedPoint(int i) const;
128 
129  /// Get index of i-th point with free map value or -1 if map value of point is fixed
130  ///
131  /// \param[in] i Surface point index.
132  ///
133  /// \return Index of point in set of points with free map value.
134  int FreePointIndex(int i) const;
135 
136  /// Get surface point ID of i-th point with free map value
137  ///
138  /// \param[in] i Index of point in set of points with free map value.
139  ///
140  /// \return Surface point index.
141  int FreePointId(int i) const;
142 
143  /// Get index of i-th point with fixed map value or -1 if map value of point is free
144  ///
145  /// \param[in] i Surface point index.
146  ///
147  /// \return Index of point in set of points with fixed map value.
148  int FixedPointIndex(int i) const;
149 
150  /// Get surface point ID of i-th point with fixed map value
151  ///
152  /// \param[in] i Index of point in set of points with fixed map value.
153  ///
154  /// \return Surface point index.
155  int FixedPointId(int i) const;
156 
157  /// Get component of map value at surface vertex
158  ///
159  /// \param[in] i Surface point index.
160  /// \param[in] j Map value component index.
161  ///
162  /// \return The j-th component of the map value evaluated at the i-th surface point.
163  double GetValue(int i, int j = 0) const;
164 
165  /// Get component of map value at i-th free point
166  ///
167  /// \param[in] i Index of point in set of points with free map value.
168  /// \param[in] j Map value component index.
169  ///
170  /// \return The j-th component of the map value evaluated at the i-th free point.
171  double GetFreeValue(int i, int j = 0) const;
172 
173  /// Get component of map value at i-th fixed point
174  ///
175  /// \param[in] i Index of point in set of points with fixed map value.
176  /// \param[in] j Map value component index.
177  ///
178  /// \return The j-th component of the map value evaluated at the i-th fixed point.
179  double GetFixedValue(int i, int j = 0) const;
180 
181 protected:
182 
183  /// Set scalar map value at surface vertex
184  ///
185  /// \param[in] i Surface point index.
186  /// \param[in] v Map value.
187  void SetValue(int i, double v);
188 
189  /// Set component of map value at surface vertex
190  ///
191  /// \param[in] i Surface point index.
192  /// \param[in] j Map component index.
193  /// \param[in] v Map component value.
194  void SetValue(int i, int j, double v);
195 
196  /// Set scalar map value at i-th free point
197  ///
198  /// \param[in] i Index of point in set of points with free map value.
199  /// \param[in] v Map value.
200  void SetFreeValue(int i, double v);
201 
202  /// Set component of map value at i-th free point
203  ///
204  /// \param[in] i Index of point in set of points with free map value.
205  /// \param[in] j Map component index.
206  /// \param[in] v Map component value.
207  void SetFreeValue(int i, int j, double v);
208 };
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 // Inline definitions
212 ////////////////////////////////////////////////////////////////////////////////
213 
214 // =============================================================================
215 // Auxiliaries
216 // =============================================================================
217 
218 // -----------------------------------------------------------------------------
220 {
221  return static_cast<int>(_FreePoints.size());
222 }
223 
224 // -----------------------------------------------------------------------------
226 {
227  return static_cast<int>(_FixedPoints.size());
228 }
229 
230 // -----------------------------------------------------------------------------
232 {
233  const int i = _PointIndex[ptId];
234  return (i < 0 ? -1 : i);
235 }
236 
237 // -----------------------------------------------------------------------------
239 {
240  return _FreePoints[i];
241 }
242 
243 // -----------------------------------------------------------------------------
245 {
246  return FreePointIndex(ptId) != -1;
247 }
248 
249 // -----------------------------------------------------------------------------
251 {
252  const int i = _PointIndex[ptId];
253  return (i < 0 ? (-i) - 1 : -1);
254 }
255 
256 // -----------------------------------------------------------------------------
258 {
259  return _FixedPoints[i];
260 }
261 
262 // -----------------------------------------------------------------------------
264 {
265  return FixedPointIndex(ptId) != -1;
266 }
267 
268 // -----------------------------------------------------------------------------
269 inline double LinearFixedBoundarySurfaceMapper::GetValue(int i, int j) const
270 {
271  return _Values->GetComponent(static_cast<vtkIdType>(i), j);
272 }
273 
274 // -----------------------------------------------------------------------------
275 inline double LinearFixedBoundarySurfaceMapper::GetFreeValue(int i, int j) const
276 {
277  return GetValue(FreePointId(i), j);
278 }
279 
280 // -----------------------------------------------------------------------------
281 inline double LinearFixedBoundarySurfaceMapper::GetFixedValue(int i, int j) const
282 {
283  return GetValue(FixedPointId(i), j);
284 }
285 
286 // -----------------------------------------------------------------------------
287 inline void LinearFixedBoundarySurfaceMapper::SetValue(int i, double v)
288 {
289  _Values->SetComponent(static_cast<vtkIdType>(i), 0, v);
290 }
291 
292 // -----------------------------------------------------------------------------
293 inline void LinearFixedBoundarySurfaceMapper::SetValue(int i, int j, double v)
294 {
295  _Values->SetComponent(static_cast<vtkIdType>(i), j, v);
296 }
297 
298 // -----------------------------------------------------------------------------
300 {
301  SetValue(FreePointId(i), v);
302 }
303 
304 // -----------------------------------------------------------------------------
305 inline void LinearFixedBoundarySurfaceMapper::SetFreeValue(int i, int j, double v)
306 {
307  SetValue(FreePointId(i), j, v);
308 }
309 
310 
311 } // namespace mirtk
312 
313 #endif // MIRTK_LinearFixedBoundarySurfaceMapper_H
int NumberOfFreePoints() const
Number of surface points with free map value.
bool IsFixedPoint(int i) const
Whether the map value of the specified surface point is fixed.
virtual void Finalize()
Assemble output surface map.
LinearFixedBoundarySurfaceMapper & operator=(const LinearFixedBoundarySurfaceMapper &)
Assignment operator.
virtual void Initialize()
Initialize filter after input and parameters are set.
Definition: IOConfig.h:41
LinearFixedBoundarySurfaceMapper()
Default constructor.
bool IsFreePoint(int i) const
Whether the map value of the specified surface point is free.
int NumberOfFixedPoints() const
Number of surface points with fixed map value.
virtual ~LinearFixedBoundarySurfaceMapper()
Destructor.
virtual void ComputeMap()=0
Compute map values at interior points.