Orfeo Toolbox  3.16
itkAffineTransform.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkAffineTransform.txx,v $
5  Language: C++
6  Date: $Date: 2009-04-09 09:23:20 $
7  Version: $Revision: 1.60 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkAffineTransform_txx
18 #define __itkAffineTransform_txx
19 
20 #include "itkNumericTraits.h"
21 #include "itkAffineTransform.h"
22 #include "vnl/algo/vnl_matrix_inverse.h"
23 
24 
25 namespace itk
26 {
27 
29 template<class TScalarType, unsigned int NDimensions>
31 AffineTransform(): Superclass(SpaceDimension,ParametersDimension)
32 {
33 }
34 
35 
37 template<class TScalarType, unsigned int NDimensions>
39 AffineTransform( unsigned int outputSpaceDimension,
40  unsigned int parametersDimension ):
41  Superclass(outputSpaceDimension,parametersDimension)
42 {
43 }
44 
45 
47 template<class TScalarType, unsigned int NDimensions>
49 AffineTransform(const MatrixType & matrix,
50  const OutputVectorType & offset):
51  Superclass(matrix, offset)
52 {
53 }
54 
55 
57 template<class TScalarType, unsigned int NDimensions>
60 {
61  return;
62 }
63 
64 
66 template<class TScalarType, unsigned int NDimensions>
67 void
69 PrintSelf(std::ostream &os, Indent indent) const
70 {
71  Superclass::PrintSelf(os,indent);
72 }
73 
74 
76 template<class TScalarType, unsigned int NDimensions>
77 void
79 Translate(const OutputVectorType &trans, bool pre)
80 {
81  OutputVectorType newTranslation = this->GetTranslation();
82  if (pre)
83  {
84  newTranslation += this->GetMatrix() * trans;
85  }
86  else
87  {
88  newTranslation += trans;
89  }
90  this->SetVarTranslation(newTranslation);
91  this->ComputeOffset();
92  this->Modified();
93  return;
94 }
95 
96 
98 template<class TScalarType, unsigned int NDimensions>
99 void
101 ::Scale(const TScalarType &factor, bool pre)
102 {
103  if (pre)
104  {
105  MatrixType newMatrix = this->GetMatrix();
106  newMatrix *= factor;
107  this->SetVarMatrix(newMatrix);
108  }
109  else
110  {
111  MatrixType newMatrix = this->GetMatrix();
112  newMatrix *= factor;
113  this->SetVarMatrix(newMatrix);
114 
115  OutputVectorType newTranslation = this->GetTranslation();
116  newTranslation *= factor;
117  this->SetVarTranslation(newTranslation);
118  }
119  this->ComputeMatrixParameters();
120  this->ComputeOffset();
121  this->Modified();
122  return;
123 }
124 
125 
127 template<class TScalarType, unsigned int NDimensions>
128 void
130 ::Scale(const OutputVectorType &factor, bool pre)
131 {
132  MatrixType trans;
133  unsigned int i, j;
134 
135  for (i = 0; i < NDimensions; i++)
136  {
137  for (j = 0; j < NDimensions; j++)
138  {
139  trans[i][j] = 0.0;
140  }
141  trans[i][i] = factor[i];
142  }
143  if (pre)
144  {
145  this->SetVarMatrix( this->GetMatrix() * trans );
146  }
147  else
148  {
149  this->SetVarMatrix( trans * this->GetMatrix() );
150  this->SetVarTranslation( trans * this->GetTranslation() );
151  }
152  this->ComputeMatrixParameters();
153  this->ComputeOffset();
154  this->Modified();
155  return;
156 }
157 
158 
160 template<class TScalarType, unsigned int NDimensions>
161 void
163 ::Rotate(int axis1, int axis2, TScalarType angle, bool pre)
164 {
165  MatrixType trans;
166  unsigned int i, j;
167 
168  for (i = 0; i < NDimensions; i++)
169  {
170  for (j = 0; j < NDimensions; j++)
171  {
172  trans[i][j] = 0.0;
173  }
174  trans[i][i] = 1.0;
175  }
176  trans[axis1][axis1] = vcl_cos(angle);
177  trans[axis1][axis2] = vcl_sin(angle);
178  trans[axis2][axis1] = -vcl_sin(angle);
179  trans[axis2][axis2] = vcl_cos(angle);
180  if (pre)
181  {
182  this->SetVarMatrix( this->GetMatrix() * trans );
183  }
184  else
185  {
186  this->SetVarMatrix( trans * this->GetMatrix() );
187  this->SetVarTranslation( trans * this->GetTranslation() );
188  }
189  this->ComputeMatrixParameters();
190  this->ComputeOffset();
191  this->Modified();
192  return;
193 }
194 
195 
199 template<class TScalarType, unsigned int NDimensions>
200 void
202 ::Rotate2D(TScalarType angle, bool pre)
203 {
204  MatrixType trans;
205 
206  trans[0][0] = vcl_cos(angle);
207  trans[0][1] = -vcl_sin(angle);
208  trans[1][0] = vcl_sin(angle);
209  trans[1][1] = vcl_cos(angle);
210  if (pre)
211  {
212  this->SetVarMatrix( this->GetMatrix() * trans );
213  }
214  else
215  {
216  this->SetVarMatrix( trans * this->GetMatrix() );
217  this->SetVarTranslation( trans * this->GetTranslation() );
218  }
219  this->ComputeMatrixParameters();
220  this->ComputeOffset();
221  this->Modified();
222  return;
223 }
224 
225 
229 template<class TScalarType, unsigned int NDimensions>
230 void
232 ::Rotate3D(const OutputVectorType &axis, TScalarType angle, bool pre)
233 {
234  MatrixType trans;
235  ScalarType r, x1, x2, x3;
236  ScalarType q0, q1, q2, q3;
237 
238  // Convert the axis to a unit vector
239  r = vcl_sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]);
240  x1 = axis[0] / r;
241  x2 = axis[1] / r;
242  x3 = axis[2] / r;
243 
244  // Compute quaternion elements
245  q0 = vcl_cos(angle/2.0);
246  q1 = x1 * vcl_sin(angle/2.0);
247  q2 = x2 * vcl_sin(angle/2.0);
248  q3 = x3 * vcl_sin(angle/2.0);
249 
250  // Compute elements of the rotation matrix
251  trans[0][0] = q0*q0 + q1*q1 - q2*q2 - q3*q3;
252  trans[0][1] = 2.0*(q1*q2 - q0*q3);
253  trans[0][2] = 2.0*(q1*q3 + q0*q2);
254  trans[1][0] = 2.0*(q1*q2 + q0*q3);
255  trans[1][1] = q0*q0 + q2*q2 - q1*q1 - q3*q3;
256  trans[1][2] = 2.0*(q2*q3 - q0*q1);
257  trans[2][0] = 2.0*(q1*q3 - q0*q2);
258  trans[2][1] = 2.0*(q2*q3 + q0*q1);
259  trans[2][2] = q0*q0 + q3*q3 - q1*q1 - q2*q2;
260 
261  // Compose rotation matrix with the existing matrix
262  if (pre)
263  {
264  this->SetVarMatrix( this->GetMatrix() * trans );
265  }
266  else
267  {
268  this->SetVarMatrix( trans * this->GetMatrix() );
269  this->SetVarTranslation( trans * this->GetTranslation() );
270  }
271  this->ComputeMatrixParameters();
272  this->ComputeOffset();
273  this->Modified();
274  return;
275 }
276 
277 
279 template<class TScalarType, unsigned int NDimensions>
280 void
282 ::Shear(int axis1, int axis2, TScalarType coef, bool pre)
283 {
284  MatrixType trans;
285  unsigned int i, j;
286 
287  for (i = 0; i < NDimensions; i++)
288  {
289  for (j = 0; j < NDimensions; j++)
290  {
291  trans[i][j] = 0.0;
292  }
293  trans[i][i] = 1.0;
294  }
295  trans[axis1][axis2] = coef;
296  if (pre)
297  {
298  this->SetVarMatrix( this->GetMatrix() * trans );
299  }
300  else
301  {
302  this->SetVarMatrix( trans * this->GetMatrix() );
303  this->SetVarTranslation( trans * this->GetTranslation() );
304  }
305  this->ComputeMatrixParameters();
306  this->ComputeOffset();
307  this->Modified();
308  return;
309 }
310 
312 template<class TScalarType, unsigned int NDimensions>
313 bool
315 ::GetInverse(Self* inverse) const
316 {
317  return this->Superclass::GetInverse(inverse);
318 }
319 
321 template<class TScalarType, unsigned int NDimensions>
325 {
326  Pointer inv = New();
327  return this->GetInverse(inv) ? inv.GetPointer() : NULL;
328 }
329 
330 
332 template<class TScalarType, unsigned int NDimensions>
335 ::Metric(const Self * other) const
336 {
337  ScalarType result = 0.0, term;
338 
339  for (unsigned int i = 0; i < NDimensions; i++)
340  {
341  for (unsigned int j = 0; j < NDimensions; j++)
342  {
343  term = this->GetMatrix()[i][j] - other->GetMatrix()[i][j];
344  result += term * term;
345  }
346  term = this->GetOffset()[i] - other->GetOffset()[i];
347  result += term * term;
348  }
349  return vcl_sqrt(result);
350 }
351 
352 
354 template<class TScalarType, unsigned int NDimensions>
357 ::Metric(void) const
358 {
359  ScalarType result = 0.0, term;
360 
361  for (unsigned int i = 0; i < NDimensions; i++)
362  {
363  for (unsigned int j = 0; j < NDimensions; j++)
364  {
365  if (i == j)
366  {
367  term = this->GetMatrix()[i][j] - 1.0;
368  }
369  else
370  {
371  term = this->GetMatrix()[i][j];
372  }
373  result += term * term;
374  }
375  term = this->GetOffset()[i];
376  result += term * term;
377  }
378 
379  return vcl_sqrt(result);
380 }
381 
382 
383 } // namespace
384 
385 #endif

Generated at Sat Feb 2 2013 23:23:29 for Orfeo Toolbox with doxygen 1.8.1.1