HomogeneousTransformationIterator.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2008-2015 Imperial College London
5  * Copyright 2008-2015 Daniel Rueckert, Julia Schnabel
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_HomogeneousTransformationIterator_H
21 #define MIRTK_HomogeneousTransformationIterator_H
22 
23 #include "mirtk/Point.h"
24 #include "mirtk/BaseImage.h"
25 #include "mirtk/HomogeneousTransformation.h"
26 
27 #include <cstdlib>
28 #include <iostream>
29 
30 
31 namespace mirtk {
32 
33 
34 /**
35  * Class for iterator for homogeneous matrix transformations.
36  *
37  * This class implements a fast access iterator 3D for homogeneous
38  * matrix transformations.
39  *
40  * NOTE: This class has NO copy constructor
41  */
43 {
44  mirtkObjectMacro(HomogeneousTransformationIterator);
45 
46  // ---------------------------------------------------------------------------
47  // Attributes
48 
49  /// Pointer to transformation
50  mirtkPublicAggregateMacro(const HomogeneousTransformation, Transformation);
51 
52  /// Current x,y,z position in x-direction
53  double _xx, _xy, _xz;
54 
55  /// Current x,y,z position in y-direction
56  double _yx, _yy, _yz;
57 
58  /// Current x,y,z position in z-direction
59  double _zx, _zy, _zz;
60 
61  /// Current x,y,z offsets in x-direction
62  double _xdx, _xdy, _xdz;
63 
64  /// Current x,y,z offsets in y-direction
65  double _ydx, _ydy, _ydz;
66 
67  /// Current x,y,z offsets in z-direction
68  double _zdx, _zdy, _zdz;
69 
70  // ---------------------------------------------------------------------------
71  // Construction/Destruction
72 
73 public:
74 
75  /// Constructor
77 
78  // ---------------------------------------------------------------------------
79  // Iteration
80 
81  /// Initialize iterator. This function initializes the transformation
82  /// iterator at point x, y, z with voxel offsets 1, 1, 1
83  void Initialize(const BaseImage *target, const BaseImage *source,
84  double x = .0, double y = .0, double z = .0,
85  bool inv = false);
86 
87  /// Advance iterator in x-direction
88  void NextX();
89 
90  /// Advance iterator in x-direction by a certain amount
91  void NextX(double);
92 
93  /// Advance iterator in y-direction
94  void NextY();
95 
96  /// Advance iterator in y-direction by a certain amount
97  void NextY(double);
98 
99  /// Advance iterator in z-direction
100  void NextZ();
101 
102  /// Advance iterator in z-direction by a certain amount
103  void NextZ(double);
104 
105 };
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 // Inline definitions
109 ////////////////////////////////////////////////////////////////////////////////
110 
111 // -----------------------------------------------------------------------------
114 :
115  _Transformation(transformation)
116 {
117 }
118 
119 // -----------------------------------------------------------------------------
121 ::Initialize(const BaseImage *target, const BaseImage *source, double x, double y, double z, bool inv)
122 {
123  if (_Transformation == NULL) {
124  cout << "HomogeneousTransformationIterator::Initialize(): Transformation has not been set" << endl;
125  exit(1);
126  }
127 
128  // Image to image transformation matrix
129  Matrix matrix = _Transformation->GetMatrix();
130  if (inv) matrix.Invert();
131  matrix = source->GetWorldToImageMatrix() * matrix * target->GetImageToWorldMatrix();
132 
133  // Calculate starting point
134  _x = matrix(0, 0) * x + matrix(0, 1) * y + matrix(0, 2) * z + matrix(0, 3);
135  _y = matrix(1, 0) * x + matrix(1, 1) * y + matrix(1, 2) * z + matrix(1, 3);
136  _z = matrix(2, 0) * x + matrix(2, 1) * y + matrix(2, 2) * z + matrix(2, 3);
137 
138  _xx = _yx = _zx = _x;
139  _xy = _yy = _zy = _y;
140  _xz = _yz = _zz = _z;
141 
142  // Calculate offsets
143  _zdx = matrix(0, 2);
144  _zdy = matrix(1, 2);
145  _zdz = matrix(2, 2);
146  _ydx = matrix(0, 1);
147  _ydy = matrix(1, 1);
148  _ydz = matrix(2, 1);
149  _xdx = matrix(0, 0);
150  _xdy = matrix(1, 0);
151  _xdz = matrix(2, 0);
152 }
153 
154 // -----------------------------------------------------------------------------
156 {
157  _x = (_xx += _xdx);
158  _y = (_xy += _xdy);
159  _z = (_xz += _xdz);
160 }
161 
162 // -----------------------------------------------------------------------------
164 {
165  _x = (_xx += _xdx * offset);
166  _y = (_xy += _xdy * offset);
167  _z = (_xz += _xdz * offset);
168 }
169 
170 // -----------------------------------------------------------------------------
172 {
173  _x = _xx = (_yx += _ydx);
174  _y = _xy = (_yy += _ydy);
175  _z = _xz = (_yz += _ydz);
176 }
177 
178 // -----------------------------------------------------------------------------
180 {
181  _x = _xx = (_yx + _ydx * offset);
182  _y = _xy = (_yy + _ydy * offset);
183  _z = _xz = (_yz + _ydz * offset);
184 }
185 
186 // -----------------------------------------------------------------------------
188 {
189  _x = _xx = _yx = (_zx += _zdx);
190  _y = _xy = _yy = (_zy += _zdy);
191  _z = _xz = _yz = (_zz += _zdz);
192 }
193 
194 // -----------------------------------------------------------------------------
196 {
197  _x = _xx = _yx = (_zx += _zdx * offset);
198  _y = _xy = _yy = (_zy += _zdy * offset);
199  _z = _xz = _yz = (_zz += _zdz * offset);
200 }
201 
202 
203 } // namespace mirtk
204 
205 #endif // MIRTK_HomogeneousTransformationIterator_H
HomogeneousTransformationIterator(const HomogeneousTransformation *=NULL)
Constructor.
double _x
x coordinate of Point
Definition: Point.h:46
Matrix & Invert(bool use_svd_if_singular=true)
Invert matrix.
Definition: IOConfig.h:41
const Matrix & GetWorldToImageMatrix() const
Return transformation matrix for world to image coordinates.
Definition: BaseImage.h:1250
void Initialize(const BaseImage *target, const BaseImage *source, double x=.0, double y=.0, double z=.0, bool inv=false)
double _z
z coordinate of Point
Definition: Point.h:48
double _y
y coordinate of Point
Definition: Point.h:47
const Matrix & GetImageToWorldMatrix() const
Return transformation matrix for image to world coordinates.
Definition: BaseImage.h:1244