KWStyle - itkSimilarity2DTransform.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkSimilarity2DTransform.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:47 $
7   Version:   $Revision: 1.4 $
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 DEF #ifndef _itkSimilarity2DTransform_txx
18 DEF #define _itkSimilarity2DTransform_txx
19
20 #include "itkSimilarity2DTransform.h"
21
22
23 namespace itk
24 {
25
26 // Constructor with default arguments
27 template <class TScalarType>
28 Similarity2DTransform<TScalarType>
29 ::Similarity2DTransform():Superclass(OutputSpaceDimension, ParametersDimension)
30 {
31   m_Scale = 1.0f; 
32 }
33
34 // Constructor with arguments
35 template<class TScalarType>
36 Similarity2DTransform<TScalarType>::
37 Similarity2DTransform( unsigned int spaceDimension, 
38                   unsigned int parametersDimension):
39 IND **Superclass(spaceDimension,parametersDimension)
40 {
41   m_Scale = 1.0f; 
42
43 }
44  
45 // Set Parameters
46 template <class TScalarType>
47 void
48 Similarity2DTransform<TScalarType>
49 ::SetParameters( const ParametersType & parameters )
50 {
51   itkDebugMacro( << "Setting paramaters " << parameters );
52
53   // Set scale
54   this->SetVarScale( parameters[0] );
55  
56   // Set angle
57   this->SetVarAngle( parameters[1] );
58
59   // Set translation
60   OffsetType translation;
61   for(unsigned int i=0; i < SpaceDimension; i++) 
62     {
63     translation[i] = parameters[i+2];
64     }
65   this->SetVarTranslation( translation );
66
67   this->ComputeMatrix();
68   this->ComputeOffset();
69
70   itkDebugMacro(<<"After setting paramaters ");
71 }
72
73
74 // Get Parameters
75 template <class TScalarType>
76 const typename Similarity2DTransform<TScalarType>::ParametersType &
77 Similarity2DTransform<TScalarType>
78 ::GetParameters( void ) const
79 {
80   itkDebugMacro( << "Getting parameters ");
81
82   this->m_Parameters[0] = this->GetScale();
83   this->m_Parameters[1] = this->GetAngle();
84  
85   // Get the translation
86   OffsetType translation = this->GetTranslation();
87   for(unsigned int i=0; i < SpaceDimension; i++) 
88     {
89     this->m_Parameters[i+2] = translation[i];
90     }
91
92   itkDebugMacro(<<"After getting parameters " << this->m_Parameters );
93
94   return this->m_Parameters;
95 }
96
97
98 EML
99 // Set Scale Part
100 template <class TScalarType>
101 void
102 Similarity2DTransform<TScalarType>
103 ::SetScale( ScaleType scale )
104 {
105   m_Scale = scale;
106   this->ComputeMatrix();
107   this->ComputeOffset();
108 }
109
110
111 // Compute the matrix
112 template <class TScalarType>
113 void
114 Similarity2DTransform<TScalarType>
115 ::ComputeMatrix( void )
116 {
117   const double angle = this->GetAngle();
118
119   const double cc = cos( angle );
120   const double ss = sin( angle );
121
122   const double ca = cc * m_Scale;
123   const double sa = ss * m_Scale;
124
125   MatrixType matrix;
126   matrix[0][0]= ca; matrix[0][1]=-sa;
127   matrix[1][0]= sa; matrix[1][1]= ca;
128
129   this->SetVarMatrix( matrix );
130
131 }
132
133 /** Compute the Angle from the Rotation Matrix */
134 template <class TScalarType>
135 void
136 Similarity2DTransform<TScalarType>
137 ::ComputeMatrixParameters( void )
138 {
139   m_Scale = sqrt( vnl_math_sqr( this->GetMatrix()[0][0] ) +
140 IND ******************vnl_math_sqr( this->GetMatrix()[0][1] ) );
141
142   this->SetVarAngle( acos( this->GetMatrix()[0][0] / m_Scale ) ); 
143
144   if(this->GetMatrix()[1][0]<0.0)
145     {
146     this->SetVarAngle( this->GetAngle() );
147     }
148
149   if( ( this->GetMatrix()[1][0] / m_Scale ) - sin(this->GetAngle()) > 0.000001)
150     {
151     std::cout << "Bad Rotation Matrix" << std::endl;
152     }
153 }
154
155
156 // Compute the transformation Jacobian
157 template<class TScalarType>
158 const typename Similarity2DTransform<TScalarType>::JacobianType &
159 Similarity2DTransform<TScalarType>::
160 GetJacobian( const InputPointType & p ) const
161 {
162   const double angle = this->GetAngle();
163   const double ca = cos( angle );
164   const double sa = sin( angle );
165
166   this->m_Jacobian.Fill(0.0);
167
168   const InputPointType center = this->GetCenter();  
169   const double cx = center[0];
170   const double cy = center[1];
171
172   const OutputVectorType translation = this->GetTranslation();
173
174   // derivatives with respect to the scale
175   this->m_Jacobian[0][0] =    ca * ( p[0] - cx ) - sa * ( p[1] - cy );
176   this->m_Jacobian[1][0] =    sa * ( p[0] - cx ) + ca * ( p[1] - cy ); 
177
178   // derivatives with respect to the angle
179 LEN   this->m_Jacobian[0][1] = ( -sa * ( p[0] - cx ) - ca * ( p[1] - cy ) ) * m_Scale;
180 LEN   this->m_Jacobian[1][1] = (  ca * ( p[0] - cx ) - sa * ( p[1] - cy ) ) * m_Scale; 
181
182   // compute derivatives with respect to the translation part
183   // first with respect to tx
184   this->m_Jacobian[0][2] = 1.0;
185   this->m_Jacobian[1][2] = 0.0;
186   // first with respect to ty
187   this->m_Jacobian[0][3] = 0.0;
188   this->m_Jacobian[1][3] = 1.0;
189
190   return this->m_Jacobian;
191
192 }
193
194  
195 // Set Identity
196 template <class TScalarType>
197 void
198 Similarity2DTransform<TScalarType>
199 ::SetIdentity(void)
200 {
201   this->Superclass::SetIdentity();
202   m_Scale = static_cast< TScalarType >( 1.0f );
203 }
204  
205 // Print self
206 template<class TScalarType>
207 void
208 Similarity2DTransform<TScalarType>::
209 PrintSelf(std::ostream &os, Indent indent) const
210 {
211   Superclass::PrintSelf(os,indent);
212   os << indent << "Scale =" << m_Scale  << std::endl;
213 }
214
215 // Create and return an inverse transformation
216 template<class TScalarType>
217 void
218 Similarity2DTransform<TScalarType>::
219 CloneInverseTo( Pointer & result ) const
220 {
221   result = New();
222   result->SetCenter( this->GetCenter() );  // inverse have the same center
223   result->SetScale( 1.0 / this->GetScale() );
224   result->SetAngle( -this->GetAngle() );
225 LEN   result->SetTranslation( -( this->GetInverseMatrix() * this->GetTranslation() ) );
226 }
227
228 // Create and return a clone of the transformation
229 template<class TScalarType>
230 void
231 Similarity2DTransform<TScalarType>::
232 CloneTo( Pointer & result ) const
233 {
234   result = New();
235   result->SetCenter( this->GetCenter() );
236   result->SetScale( this->GetScale() );
237   result->SetAngle( this->GetAngle() );
238   result->SetTranslation( this->GetTranslation() );
239 }
240
241 // Set the similarity matrix
242 template<class TScalarType>
243 void
244 Similarity2DTransform<TScalarType>::
245 SetMatrix(const MatrixType & matrix )
246 {
247   itkDebugMacro("setting  m_Matrix  to " << matrix ); 
248  
249   typename MatrixType::InternalMatrixType test = 
250     matrix.GetVnlMatrix() * matrix.GetTranspose();
251
252   test /= test[0][0]; // factor out the scale
253
254   const double tolerance = 1e-10;
255   if( !test.is_identity( tolerance ) ) 
256     {
257 LEN     itk::ExceptionObject ex(__FILE__,__LINE__,"Attempt to set a Non-Orthogonal matrix",ITK_LOCATION);
258     throw ex;
259     }
260
261   this->SetVarMatrix( matrix );
262   this->ComputeOffset();
263   this->ComputeMatrixParameters();
264   this->Modified();
265
266 }
267
268 // namespace
269
270 #endif
271

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:46PM
© Kitware Inc.