RigidTransformation.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2008-2015 Imperial College London
5  * Copyright 2008-2013 Daniel Rueckert, Julia Schnabel
6  * Copyright 2013-2015 Andreas Schuh
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef MIRTK_RigidTransformation_H
22 #define MIRTK_RigidTransformation_H
23 
24 #include "mirtk/HomogeneousTransformation.h"
25 
26 
27 namespace mirtk {
28 
29 
30 /**
31  * Class for rigid transformations.
32  *
33  * This class defines and implements rigid body transformations. The rigid
34  * body transformations are parameterized by three rotations around the axes
35  * of the coordinate system followed by three translations along the axes of
36  * the coordinate system. Note that the order of rotations is defined as a
37  * rotation around the z-axis, the y-axis and finally around the x-axis. In
38  * total, the transformation is parameterized by six degrees of freedom.
39  */
41 {
42  mirtkTransformationMacro(RigidTransformation);
43 
44  // ---------------------------------------------------------------------------
45  // Data members
46 
47 protected:
48 
49  /// Sine of rotation angle rx
50  double _sinrx;
51 
52  /// Sine of rotation angle ry
53  double _sinry;
54 
55  /// Sine of rotation angle rz
56  double _sinrz;
57 
58  /// Cosine of rotation angle rx
59  double _cosrx;
60 
61  /// Cosine of rotation angle ry
62  double _cosry;
63 
64  /// Cosine of rotation angle rz
65  double _cosrz;
66 
67  /// Update cached sine and cosine of rotation angles
69 
70  // ---------------------------------------------------------------------------
71  // Construction/Destruction
72 
73 protected:
74 
75  /// Default constructor with given number of parameters
77 
78  /// Copy constructor with given number of parameters
80 
81 public:
82 
83  /// Default constructor
85 
86  /// Copy constructor
88 
89  /// Destructor
90  virtual ~RigidTransformation();
91 
92  // ---------------------------------------------------------------------------
93  // Approximation
94 
95  /// Approximate displacements: This function takes a set of points and a set
96  /// of displacements and finds !new! parameters such that the resulting
97  /// transformation approximates the displacements as good as possible.
98  virtual void ApproximateDOFs(const double *, const double *, const double *, const double *,
99  const double *, const double *, const double *, int);
100 
101  // ---------------------------------------------------------------------------
102  // Transformation parameters
103 
104  /// Construct a matrix based on parameters passed in the array
105  static Matrix DOFs2Matrix(const double *);
106 
107  /// Return an array with parameters corresponding to a given matrix
108  static void Matrix2DOFs(const Matrix &, double *);
109 
110  /// Updates transformation matrix after change of parameter
111  virtual void UpdateMatrix();
112 
113  /// Updates transformation parameters after change of matrix
114  virtual void UpdateDOFs();
115 
116  /// Puts translation along the x-axis (transformation matrix is updated)
117  void PutTranslationX(double);
118 
119  /// Gets translation along the x-axis
120  double GetTranslationX() const;
121 
122  /// Puts translation along the y-axis (transformation matrix is updated)
123  void PutTranslationY(double);
124 
125  /// Gets translation along the y-axis
126  double GetTranslationY() const;
127 
128  /// Puts translation along the z-axis (transformation matrix is updated)
129  void PutTranslationZ(double);
130 
131  /// Gets translation along the z-axis
132  double GetTranslationZ() const;
133 
134  /// Puts rotation angle around the x-axis (transformation matrix is updated)
135  void PutRotationX(double);
136 
137  /// Gets rotation angle around the x-axis
138  double GetRotationX() const;
139 
140  /// Puts rotation angle around the y-axis (transformation matrix is updated)
141  void PutRotationY(double);
142 
143  /// Gets rotation angle around the y-axis
144  double GetRotationY() const;
145 
146  /// Puts rotation angle around the z-axis (transformation matrix is updated)
147  void PutRotationZ(double);
148 
149  /// Gets rotation angle around the z-axis
150  double GetRotationZ() const;
151 
152 public:
153 
154  // ---------------------------------------------------------------------------
155  // Point transformation
156 
157  /// Transforms a single point by the translation part of the rigid transformation
158  virtual void Translate(double& x, double& y, double& z) const;
159 
160  /// Transforms a single point by the rotation part of the rigid transformation
161  virtual void Rotate(double& x, double& y, double& z) const;
162 
163  // ---------------------------------------------------------------------------
164  // Derivatives
165 
166  // Do not overwrite other base class overloads
168 
169  /// Calculates the Jacobian of the transformation w.r.t the parameters
170  virtual void JacobianDOFs(double [3], int, double, double, double, double = 0, double = -1) const;
171 
172  /// Calculates the derivative of the Jacobian of the transformation (w.r.t. world coordinates) w.r.t. a transformation parameter
173  virtual void DeriveJacobianWrtDOF(Matrix &, int, double, double, double, double = 0, double = -1) const;
174 
175  // ---------------------------------------------------------------------------
176  // I/O
177 
178  // Do not hide methods of base class
180 
181  /// Prints the parameters of the transformation
182  virtual void Print(ostream &, Indent = 0) const;
183 
184 public:
185 
186  // ---------------------------------------------------------------------------
187  // Deprecated
188 
189  /// Set transformation parameters (DoFs)
190  /// \deprecated Use Put(params) instead.
191  void SetParameters(double *params);
192 
193 };
194 
195 ////////////////////////////////////////////////////////////////////////////////
196 // Inline definitions
197 ////////////////////////////////////////////////////////////////////////////////
198 
199 // =============================================================================
200 // Transformation parameters
201 // =============================================================================
202 
203 // -----------------------------------------------------------------------------
204 inline void RigidTransformation::PutRotationX(double rx)
205 {
206  Put(RX, rx);
207 }
208 
209 // -----------------------------------------------------------------------------
211 {
212  return Get(RX);
213 }
214 
215 // -----------------------------------------------------------------------------
216 inline void RigidTransformation::PutRotationY(double ry)
217 {
218  Put(RY, ry);
219 }
220 
221 // -----------------------------------------------------------------------------
223 {
224  return Get(RY);
225 }
226 
227 // -----------------------------------------------------------------------------
228 inline void RigidTransformation::PutRotationZ(double rz)
229 {
230  Put(RZ, rz);
231 }
232 
233 // -----------------------------------------------------------------------------
235 {
236  return Get(RZ);
237 }
238 
239 // -----------------------------------------------------------------------------
241 {
242  Put(TX, tx);
243 }
244 
245 // -----------------------------------------------------------------------------
247 {
248  return Get(TX);
249 }
250 
251 // -----------------------------------------------------------------------------
253 {
254  Put(TY, ty);
255 }
256 
257 // -----------------------------------------------------------------------------
259 {
260  return Get(TY);
261 }
262 
263 // -----------------------------------------------------------------------------
265 {
266  Put(TZ, tz);
267 }
268 
269 // -----------------------------------------------------------------------------
271 {
272  return Get(TZ);
273 }
274 
275 // =============================================================================
276 // Point transformation
277 // =============================================================================
278 
279 // -----------------------------------------------------------------------------
280 inline void RigidTransformation::Translate(double& x, double& y, double& z) const
281 {
282  x += _matrix(0, 3);
283  y += _matrix(1, 3);
284  z += _matrix(2, 3);
285 }
286 
287 // -----------------------------------------------------------------------------
288 inline void RigidTransformation::Rotate(double& x, double& y, double& z) const
289 {
290  double a = _matrix(0, 0) * x + _matrix(0, 1) * y + _matrix(0, 2) * z;
291  double b = _matrix(1, 0) * x + _matrix(1, 1) * y + _matrix(1, 2) * z;
292  double c = _matrix(2, 0) * x + _matrix(2, 1) * y + _matrix(2, 2) * z;
293 
294  x = a;
295  y = b;
296  z = c;
297 }
298 
299 // =============================================================================
300 // Deprecated
301 // =============================================================================
302 
303 // -----------------------------------------------------------------------------
304 inline void RigidTransformation::SetParameters(double *params)
305 {
306  Put(params);
307 }
308 
309 
310 } // namespace mirtk
311 
312 #endif // MIRTK_RigidTransformation_H
virtual void Print(ostream &, Indent=0) const
Prints the parameters of the transformation.
double _cosrx
Cosine of rotation angle rx.
double _sinrz
Sine of rotation angle rz.
void PutTranslationX(double)
Puts translation along the x-axis (transformation matrix is updated)
void PutRotationY(double)
Puts rotation angle around the y-axis (transformation matrix is updated)
virtual void JacobianDOFs(double [3], int, double, double, double, double=0, double=-1) const
Calculates the Jacobian of the transformation w.r.t the parameters.
double _sinrx
Sine of rotation angle rx.
double GetTranslationZ() const
Gets translation along the z-axis.
void PutRotationZ(double)
Puts rotation angle around the z-axis (transformation matrix is updated)
double GetTranslationY() const
Gets translation along the y-axis.
virtual void UpdateMatrix()
Updates transformation matrix after change of parameter.
virtual void JacobianDOFs(double [3], int, double, double, double, double=0, double=NaN) const
Calculates the Jacobian of the transformation w.r.t a transformation parameter.
virtual void ApproximateDOFs(const double *, const double *, const double *, const double *, const double *, const double *, const double *, int)
virtual void Translate(double &x, double &y, double &z) const
Transforms a single point by the translation part of the rigid transformation.
virtual void Put(int, DOFValue)
Puts a transformation parameter.
virtual void UpdateDOFs()
Updates transformation parameters after change of matrix.
virtual void Rotate(double &x, double &y, double &z) const
Transforms a single point by the rotation part of the rigid transformation.
void SetParameters(double *params)
Matrix _matrix
4x4 transformation matrix for homogeneous coordinates
double GetRotationX() const
Gets rotation angle around the x-axis.
virtual void DeriveJacobianWrtDOF(Matrix &, int, double, double, double, double=0, double=-1) const
Calculates the derivative of the Jacobian of the transformation (w.r.t. world coordinates) w...
void PutRotationX(double)
Puts rotation angle around the x-axis (transformation matrix is updated)
Definition: IOConfig.h:41
double GetTranslationX() const
Gets translation along the x-axis.
double _cosry
Cosine of rotation angle ry.
RigidTransformation()
Default constructor.
double GetRotationY() const
Gets rotation angle around the y-axis.
virtual ~RigidTransformation()
Destructor.
void UpdateRotationSineCosine()
Update cached sine and cosine of rotation angles.
double _cosrz
Cosine of rotation angle rz.
double GetRotationZ() const
Gets rotation angle around the z-axis.
virtual void Print(ostream &, Indent=0) const
Prints the parameters of the transformation.
static Matrix DOFs2Matrix(const double *)
Construct a matrix based on parameters passed in the array.
void PutTranslationY(double)
Puts translation along the y-axis (transformation matrix is updated)
double _sinry
Sine of rotation angle ry.
virtual double Get(int) const
Get value of transformation parameter.
void PutTranslationZ(double)
Puts translation along the z-axis (transformation matrix is updated)
static void Matrix2DOFs(const Matrix &, double *)
Return an array with parameters corresponding to a given matrix.