MeshlessMap.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2015-2016 Imperial College London
5  * Copyright 2015-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_MeshlessMap_H
21 #define MIRTK_MeshlessMap_H
22 
23 #include "mirtk/Mapping.h"
24 
25 #include "mirtk/Math.h"
26 #include "mirtk/Matrix.h"
27 #include "mirtk/PointSet.h"
28 
29 
30 namespace mirtk {
31 
32 
33 // Forward declarations
34 class Cifstream;
35 class Cofstream;
36 
37 
38 /**
39  * Meshless map defined as superposition of kernel functions
40  */
41 class MeshlessMap : public Mapping
42 {
43  mirtkAbstractMacro(MeshlessMap);
44 
45  // ---------------------------------------------------------------------------
46  // Attributes
47 
48 private:
49 
50  /// Source points, i.e., centers of kernel functions
51  mirtkPublicAttributeMacro(PointSet, SourcePoints);
52 
53  /// Coefficients of the map
54  ///
55  /// The columns contain the source point weights for each scalar map.
56  mirtkPublicAttributeMacro(Matrix, Coefficients);
57 
58  /// Copy attributes of this class from another instance
59  void CopyAttributes(const MeshlessMap &);
60 
61  // ---------------------------------------------------------------------------
62  // Construction/Destruction
63 
64 protected:
65 
66  /// Default constructor
67  MeshlessMap();
68 
69  /// Copy constructor
70  MeshlessMap(const MeshlessMap &);
71 
72  /// Assignment operator
74 
75  /// Initialize map after inputs and parameters are set
76  virtual void Initialize();
77 
78 public:
79 
80  /// Destructor
81  virtual ~MeshlessMap();
82 
83  // ---------------------------------------------------------------------------
84  // Map domain
85 
86  // Import other overloads
88 
89  /// Get minimum axes-aligned bounding box of map domain
90  ///
91  /// \param[out] x1 Lower bound of map domain along x axis.
92  /// \param[out] y1 Lower bound of map domain along y axis.
93  /// \param[out] z1 Lower bound of map domain along z axis.
94  /// \param[out] x2 Upper bound of map domain along x axis.
95  /// \param[out] y2 Upper bound of map domain along y axis.
96  /// \param[out] z2 Upper bound of map domain along z axis.
97  virtual void BoundingBox(double &x1, double &y1, double &z1,
98  double &x2, double &y2, double &z2) const;
99 
100  // ---------------------------------------------------------------------------
101  // Source points
102 
103  /// Add source point with zero coefficient
104  ///
105  /// \returns Whether source point was added or too close to existing point.
106  bool AddSourcePoint(double p[3], double tol = .0);
107 
108  /// Get number of source points
109  int NumberOfSourcePoints() const;
110 
111  // ---------------------------------------------------------------------------
112  // Evaluation
113 
114  /// Dimension of codomain, i.e., number of output values
115  virtual int NumberOfComponents() const;
116 
117  // ---------------------------------------------------------------------------
118  // I/O
119 
120 protected:
121 
122  /// Read map attributes and parameters from file stream
123  virtual void ReadMap(Cifstream &);
124 
125  /// Write map attributes and parameters to file stream
126  virtual void WriteMap(Cofstream &) const;
127 
128 };
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 // Inline definitions
132 ////////////////////////////////////////////////////////////////////////////////
133 
134 // -----------------------------------------------------------------------------
135 inline bool MeshlessMap::AddSourcePoint(double p[3], double tol)
136 {
137  if (tol > .0) {
138  for (int i = 0; i < _SourcePoints.Size(); ++i) {
139  const Point &q = _SourcePoints(i);
140  if (fequal(p[0], q._x, tol) &&
141  fequal(p[1], q._y, tol) &&
142  fequal(p[2], q._z, tol)) {
143  return false;
144  }
145  }
146  }
147  _SourcePoints.Add(p);
148  _Coefficients.Resize(_SourcePoints.Size(), _Coefficients.Cols());
149  return true;
150 }
151 
152 // -----------------------------------------------------------------------------
154 {
155  return _SourcePoints.Size();
156 }
157 
158 // -----------------------------------------------------------------------------
160 {
161  return _Coefficients.Cols();
162 }
163 
164 
165 } // namespace mirtk
166 
167 #endif // MIRTK_MeshlessMap_H
virtual void ReadMap(Cifstream &)
Read map attributes and parameters from file stream.
void BoundingBox(double &x1, double &y1, double &x2, double &y2) const
Definition: Mapping.h:273
bool AddSourcePoint(double p[3], double tol=.0)
Definition: MeshlessMap.h:135
virtual void Initialize()
Initialize map after inputs and parameters are set.
int NumberOfSourcePoints() const
Get number of source points.
Definition: MeshlessMap.h:153
double _x
x coordinate of Point
Definition: Point.h:46
MIRTKCU_API bool fequal(double a, double b, double tol=1e-12)
Definition: Math.h:138
virtual int NumberOfComponents() const
Dimension of codomain, i.e., number of output values.
Definition: MeshlessMap.h:159
Definition: IOConfig.h:41
virtual void BoundingBox(double &x1, double &y1, double &z1, double &x2, double &y2, double &z2) const
MeshlessMap()
Default constructor.
virtual void WriteMap(Cofstream &) const
Write map attributes and parameters to file stream.
double _z
z coordinate of Point
Definition: Point.h:48
MeshlessMap & operator=(const MeshlessMap &)
Assignment operator.
double _y
y coordinate of Point
Definition: Point.h:47
virtual ~MeshlessMap()
Destructor.