17 #ifndef __itkSimilarity2DTransform_txx
18 #define __itkSimilarity2DTransform_txx
21 #include "vnl/vnl_math.h"
27 template <
class TScalarType>
35 template<
class TScalarType>
38 unsigned int parametersDimension):
46 template <
class TScalarType>
51 itkDebugMacro( <<
"Setting parameters " << parameters );
54 const TScalarType scale = parameters[0];
55 this->SetVarScale( scale );
58 const TScalarType angle = parameters[1];
59 this->SetVarAngle( angle );
63 for(
unsigned int i=0; i < SpaceDimension; i++)
65 translation[i] = parameters[i+2];
67 this->SetVarTranslation( translation );
69 this->ComputeMatrix();
70 this->ComputeOffset();
76 itkDebugMacro(<<
"After setting parameters ");
81 template <
class TScalarType>
86 itkDebugMacro( <<
"Getting parameters ");
88 this->m_Parameters[0] = this->GetScale();
89 this->m_Parameters[1] = this->GetAngle();
92 OffsetType translation = this->GetTranslation();
93 for(
unsigned int i=0; i < SpaceDimension; i++)
95 this->m_Parameters[i+2] = translation[i];
98 itkDebugMacro(<<
"After getting parameters " << this->m_Parameters );
100 return this->m_Parameters;
104 template <
class TScalarType>
110 this->ComputeMatrix();
111 this->ComputeOffset();
116 template <
class TScalarType>
121 const double angle = this->GetAngle();
123 const double cc = vcl_cos(angle );
124 const double ss = vcl_sin(angle );
130 matrix[0][0]= ca; matrix[0][1]=-sa;
131 matrix[1][0]= sa; matrix[1][1]= ca;
133 this->SetVarMatrix( matrix );
138 template <
class TScalarType>
143 m_Scale = vcl_sqrt(vnl_math_sqr( this->GetMatrix()[0][0] ) +
144 vnl_math_sqr( this->GetMatrix()[0][1] ) );
146 this->SetVarAngle( vcl_acos(this->GetMatrix()[0][0] / m_Scale ) );
148 if(this->GetMatrix()[1][0]<0.0)
150 this->SetVarAngle( -this->GetAngle() );
153 if( ( this->GetMatrix()[1][0] / m_Scale ) - vcl_sin(this->GetAngle()) > 0.000001)
155 std::cout <<
"Bad Rotation Matrix" << std::endl;
161 template<
class TScalarType>
166 const double angle = this->GetAngle();
167 const double ca = vcl_cos(angle );
168 const double sa = vcl_sin(angle );
170 this->m_Jacobian.Fill(0.0);
173 const double cx = center[0];
174 const double cy = center[1];
179 this->m_Jacobian[0][0] = ca * ( p[0] - cx ) - sa * ( p[1] - cy );
180 this->m_Jacobian[1][0] = sa * ( p[0] - cx ) + ca * ( p[1] - cy );
183 this->m_Jacobian[0][1] = ( -sa * ( p[0] - cx ) - ca * ( p[1] - cy ) ) * m_Scale;
184 this->m_Jacobian[1][1] = ( ca * ( p[0] - cx ) - sa * ( p[1] - cy ) ) * m_Scale;
188 this->m_Jacobian[0][2] = 1.0;
189 this->m_Jacobian[1][2] = 0.0;
191 this->m_Jacobian[0][3] = 0.0;
192 this->m_Jacobian[1][3] = 1.0;
194 return this->m_Jacobian;
200 template <
class TScalarType>
205 this->Superclass::SetIdentity();
206 m_Scale =
static_cast< TScalarType
>( 1.0f );
210 template<
class TScalarType>
215 Superclass::PrintSelf(os,indent);
216 os << indent <<
"Scale =" << m_Scale << std::endl;
220 template<
class TScalarType>
230 template<
class TScalarType>
240 inverse->SetCenter( this->GetCenter() );
241 inverse->SetScale( NumericTraits<double>::One / this->GetScale() );
242 inverse->SetAngle( -this->GetAngle() );
243 inverse->SetTranslation( -( this->GetInverseMatrix() * this->GetTranslation() ) );
249 template<
class TScalarType>
255 if( this->GetInverse(inv) )
263 template<
class TScalarType>
269 result->SetCenter( this->GetCenter() );
270 result->SetScale( this->GetScale() );
271 result->SetAngle( this->GetAngle() );
272 result->SetTranslation( this->GetTranslation() );
276 template<
class TScalarType>
281 itkDebugMacro(
"setting m_Matrix to " << matrix );
283 typename MatrixType::InternalMatrixType test =
284 matrix.GetVnlMatrix() * matrix.GetTranspose();
288 const double tolerance = 1e-10;
289 if( !test.is_identity( tolerance ) )
291 itk::ExceptionObject ex(__FILE__,__LINE__,
"Attempt to set a Non-Orthogonal matrix",ITK_LOCATION);
295 this->SetVarMatrix( matrix );
296 this->ComputeOffset();
297 this->ComputeMatrixParameters();