17 #ifndef __itkAffineTransform_txx
18 #define __itkAffineTransform_txx
20 #include "itkNumericTraits.h"
22 #include "vnl/algo/vnl_matrix_inverse.h"
29 template<
class TScalarType,
unsigned int NDimensions>
37 template<
class TScalarType,
unsigned int NDimensions>
40 unsigned int parametersDimension ):
41 Superclass(outputSpaceDimension,parametersDimension)
47 template<
class TScalarType,
unsigned int NDimensions>
57 template<
class TScalarType,
unsigned int NDimensions>
66 template<
class TScalarType,
unsigned int NDimensions>
71 Superclass::PrintSelf(os,indent);
76 template<
class TScalarType,
unsigned int NDimensions>
84 newTranslation += this->GetMatrix() * trans;
88 newTranslation += trans;
90 this->SetVarTranslation(newTranslation);
91 this->ComputeOffset();
98 template<
class TScalarType,
unsigned int NDimensions>
107 this->SetVarMatrix(newMatrix);
113 this->SetVarMatrix(newMatrix);
116 newTranslation *= factor;
117 this->SetVarTranslation(newTranslation);
119 this->ComputeMatrixParameters();
120 this->ComputeOffset();
127 template<
class TScalarType,
unsigned int NDimensions>
135 for (i = 0; i < NDimensions; i++)
137 for (j = 0; j < NDimensions; j++)
141 trans[i][i] = factor[i];
145 this->SetVarMatrix( this->GetMatrix() * trans );
149 this->SetVarMatrix( trans * this->GetMatrix() );
150 this->SetVarTranslation( trans * this->GetTranslation() );
152 this->ComputeMatrixParameters();
153 this->ComputeOffset();
160 template<
class TScalarType,
unsigned int NDimensions>
163 ::Rotate(
int axis1,
int axis2, TScalarType angle,
bool pre)
168 for (i = 0; i < NDimensions; i++)
170 for (j = 0; j < NDimensions; j++)
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);
182 this->SetVarMatrix( this->GetMatrix() * trans );
186 this->SetVarMatrix( trans * this->GetMatrix() );
187 this->SetVarTranslation( trans * this->GetTranslation() );
189 this->ComputeMatrixParameters();
190 this->ComputeOffset();
199 template<
class TScalarType,
unsigned int NDimensions>
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);
212 this->SetVarMatrix( this->GetMatrix() * trans );
216 this->SetVarMatrix( trans * this->GetMatrix() );
217 this->SetVarTranslation( trans * this->GetTranslation() );
219 this->ComputeMatrixParameters();
220 this->ComputeOffset();
229 template<
class TScalarType,
unsigned int NDimensions>
239 r = vcl_sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]);
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);
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;
264 this->SetVarMatrix( this->GetMatrix() * trans );
268 this->SetVarMatrix( trans * this->GetMatrix() );
269 this->SetVarTranslation( trans * this->GetTranslation() );
271 this->ComputeMatrixParameters();
272 this->ComputeOffset();
279 template<
class TScalarType,
unsigned int NDimensions>
282 ::Shear(
int axis1,
int axis2, TScalarType coef,
bool pre)
287 for (i = 0; i < NDimensions; i++)
289 for (j = 0; j < NDimensions; j++)
295 trans[axis1][axis2] = coef;
298 this->SetVarMatrix( this->GetMatrix() * trans );
302 this->SetVarMatrix( trans * this->GetMatrix() );
303 this->SetVarTranslation( trans * this->GetTranslation() );
305 this->ComputeMatrixParameters();
306 this->ComputeOffset();
312 template<
class TScalarType,
unsigned int NDimensions>
317 return this->Superclass::GetInverse(inverse);
321 template<
class TScalarType,
unsigned int NDimensions>
332 template<
class TScalarType,
unsigned int NDimensions>
339 for (
unsigned int i = 0; i < NDimensions; i++)
341 for (
unsigned int j = 0; j < NDimensions; j++)
343 term = this->GetMatrix()[i][j] - other->
GetMatrix()[i][j];
344 result += term * term;
346 term = this->GetOffset()[i] - other->
GetOffset()[i];
347 result += term * term;
349 return vcl_sqrt(result);
354 template<
class TScalarType,
unsigned int NDimensions>
361 for (
unsigned int i = 0; i < NDimensions; i++)
363 for (
unsigned int j = 0; j < NDimensions; j++)
367 term = this->GetMatrix()[i][j] - 1.0;
371 term = this->GetMatrix()[i][j];
373 result += term * term;
375 term = this->GetOffset()[i];
376 result += term * term;
379 return vcl_sqrt(result);