KWStyle - itkRigid2DTransform.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkRigid2DTransform.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 _itkRigid2DTransform_txx
18 DEF #define _itkRigid2DTransform_txx
19
20 #include "itkRigid2DTransform.h"
21
22
23 namespace itk
24 {
25
26 // Constructor with default arguments
27 template<class TScalarType>
28 Rigid2DTransform<TScalarType>::
29 Rigid2DTransform():
30 IND **Superclass(OutputSpaceDimension, ParametersDimension)
31 {
32   m_Angle = NumericTraits< TScalarType >::Zero;
33 }
34  
35
36 // Constructor with arguments
37 template<class TScalarType>
38 Rigid2DTransform<TScalarType>::
39 Rigid2DTransform( unsigned int spaceDimension, 
40                   unsigned int parametersDimension):
41 IND **Superclass(spaceDimension,parametersDimension)
42 {
43   m_Angle = NumericTraits< TScalarType >::Zero;
44 }
45
46
47 // Destructor
48 template<class TScalarType>
49 Rigid2DTransform<TScalarType>::
50 ~Rigid2DTransform()
51 {
52 }
53
54
55 // Print self
56 template<class TScalarType>
57 void
58 Rigid2DTransform<TScalarType>::
59 PrintSelf(std::ostream &os, Indent indent) const
60 {
61   Superclass::PrintSelf(os,indent);
62   os << indent << "Angle       = " << m_Angle        << std::endl;
63 }
64
65
66 // Set the rotation matrix
67 template<class TScalarType>
68 void
69 Rigid2DTransform<TScalarType>::
70 SetMatrix(const MatrixType & matrix )
71 {
72   itkDebugMacro("setting  m_Matrix  to " << matrix ); 
73   // The matrix must be orthogonal otherwise it is not
74   // representing a valid rotaion in 2D space
75   typename MatrixType::InternalMatrixType test = 
76     matrix.GetVnlMatrix() * matrix.GetTranspose();
77
78   const double tolerance = 1e-10;
79   if( !test.is_identity( tolerance ) ) 
80     {
81 LEN     itk::ExceptionObject ex(__FILE__,__LINE__,"Attempt to set a Non-Orthogonal matrix",ITK_LOCATION);
82     throw ex;
83     }
84
85   this->SetVarMatrix( matrix );
86   this->ComputeOffset();
87   this->ComputeMatrixParameters();
88   this->Modified();
89
90 }
91
92
93 /** Compute the Angle from the Rotation Matrix */
94 template <class TScalarType>
95 void
96 Rigid2DTransform<TScalarType>
97 ::ComputeMatrixParameters( void )
98 {
99   m_Angle = acos(this->GetMatrix()[0][0]); 
100
101   if(this->GetMatrix()[1][0]<0.0)
102     {
103     m_Angle = -m_Angle;
104     }
105
106   if(this->GetMatrix()[1][0]-sin(m_Angle) > 0.000001)
107     {
108     itkWarningMacro("Bad Rotation Matrix " << this->GetMatrix() ); 
109     }
110 }
111
112
113 // Compose with a translation
114 template<class TScalarType>
115 void
116 Rigid2DTransform<TScalarType>::
117 Translate(const OffsetType &offset, bool)
118 {
119   OutputVectorType newOffset = this->GetOffset();
120   newOffset += offset;
121   this->SetOffset(newOffset);
122 }
123
124
125 // Create and return an inverse transformation
126 template<class TScalarType>
127 void
128 Rigid2DTransform<TScalarType>::
129 CloneInverseTo( Pointer & result ) const
130 {
131   result = New();
132   result->SetCenter( this->GetCenter() );  // inverse have the same center
133   result->SetAngle( -this->GetAngle() );
134 LEN   result->SetTranslation( -( this->GetInverseMatrix() * this->GetTranslation() ) );
135 }
136
137 // Create and return a clone of the transformation
138 template<class TScalarType>
139 void
140 Rigid2DTransform<TScalarType>::
141 CloneTo( Pointer & result ) const
142 {
143   result = New();
144   result->SetCenter( this->GetCenter() );
145   result->SetAngle( this->GetAngle() );
146   result->SetTranslation( this->GetTranslation() );
147 }
148
149   
150 // Reset the transform to an identity transform 
151 template<class TScalarType >
152 void
153 Rigid2DTransform< TScalarType >::
154 SetIdentity( void ) 
155 {
156   this->Superclass::SetIdentity();
157   m_Angle = NumericTraits< TScalarType >::Zero;
158 }
159
160 // Set the angle of rotation
161 template <class TScalarType>
162 void
163 Rigid2DTransform<TScalarType>
164 ::SetAngle(TScalarType angle)
165 {
166   m_Angle = angle;
167   this->ComputeMatrix();
168   this->ComputeOffset();
169   this->Modified();
170 }
171
172
173 // Set the angle of rotation
174 template <class TScalarType>
175 void
176 Rigid2DTransform<TScalarType>
177 ::SetAngleInDegrees(TScalarType angle)
178 {
179   const TScalarType angleInRadians = angle * atan(1.0) / 45.0;
180   this->SetAngle( angleInRadians );
181 }
182
183 // Compute the matrix from the angle
184 template <class TScalarType>
185 void
186 Rigid2DTransform<TScalarType>
187 ::ComputeMatrix( void )
188 {
189   const double ca = cos( m_Angle );
190   const double sa = sin( m_Angle );
191
192   MatrixType rotationMatrix;
193   rotationMatrix[0][0]= ca; rotationMatrix[0][1]=-sa;
194   rotationMatrix[1][0]= sa; rotationMatrix[1][1]= ca;
195
196   this->SetVarMatrix( rotationMatrix );
197
198 }
199
200 // Set Parameters
201 template <class TScalarType>
202 void
203 Rigid2DTransform<TScalarType>::
204 SetParameters( const ParametersType & parameters )
205 {
206   itkDebugMacro( << "Setting paramaters " << parameters );
207
208   // Set angle
209   this->SetVarAngle( parameters[0] );
210  
211   // Set translation
212   OutputVectorType translation;
213   for(unsigned int i=0; i < OutputSpaceDimension; i++) 
214     {
215     translation[i] = parameters[i+1];
216     }
217   this->SetVarTranslation( translation );
218
219   // Update matrix and offset
220   this->ComputeMatrix();
221   this->ComputeOffset();
222
223   itkDebugMacro(<<"After setting parameters ");
224 }
225
226 // Get Parameters
227 template <class TScalarType>
228 const typename Rigid2DTransform<TScalarType>::ParametersType &
229 Rigid2DTransform<TScalarType>::
230 GetParameters( void ) const
231 {
232   itkDebugMacro( << "Getting parameters ");
233
234   // Get the angle
235   this->m_Parameters[0] = this->GetAngle();
236  
237   // Get the translation
238   for(unsigned int i=0; i < OutputSpaceDimension; i++) 
239     {
240     this->m_Parameters[i+1] = this->GetTranslation()[i];
241     }
242
243   itkDebugMacro(<<"After getting parameters " << this->m_Parameters );
244
245   return this->m_Parameters;
246 }
247
248 // Compute transformation Jacobian
249 template<class TScalarType>
250 const typename Rigid2DTransform<TScalarType>::JacobianType &
251 Rigid2DTransform<TScalarType>::
252 GetJacobian( const InputPointType & p ) const
253 {
254
255   const double ca = cos( this->GetAngle() );
256   const double sa = sin( this->GetAngle() );
257
258   this->m_Jacobian.Fill(0.0);
259
260   const double cx = this->GetCenter()[0];
261   const double cy = this->GetCenter()[1];
262
263   // derivatives with respect to the angle
264   this->m_Jacobian[0][0] = -sa * ( p[0] - cx ) - ca * ( p[1] - cy );
265   this->m_Jacobian[1][0] =  ca * ( p[0] - cx ) - sa * ( p[1] - cy ); 
266
267   // compute derivatives for the translation part
268   unsigned int blockOffset = 1;  
269   for(unsigned int dim=0; dim < OutputSpaceDimension; dim++ ) 
270     {
271     this->m_Jacobian[ dim ][ blockOffset + dim ] = 1.0;
272     }
273
274   return this->m_Jacobian;
275
276 }
277
278 // Back transform a point
279 template<class TScalarType>
280 typename Rigid2DTransform<TScalarType>::InputPointType
281 Rigid2DTransform<TScalarType>::
282 BackTransform(const OutputPointType &point) const 
283 {
284 LEN   itkWarningMacro(<<"BackTransform(): This method is slated to be removed from ITK.  Instead, please use GetInverse() to generate an inverse transform and then perform the transform using that inverted transform.");
285   return this->GetInverseMatrix() * (point - this->GetOffset());
286 }
287
288 // Back transform a vector
289 template<class TScalarType>
290 typename Rigid2DTransform<TScalarType>::InputVectorType
291 Rigid2DTransform<TScalarType>::
292 BackTransform(const OutputVectorType &vect ) const 
293 {
294 LEN   itkWarningMacro(<<"BackTransform(): This method is slated to be removed from ITK.  Instead, please use GetInverse() to generate an inverse transform and then perform the transform using that inverted transform.");
295   return this->GetInverseMatrix() * vect;
296 }
297
298 // Back transform a vnl_vector
299 template<class TScalarType>
300 typename Rigid2DTransform<TScalarType>::InputVnlVectorType
301 Rigid2DTransform<TScalarType>::
302 BackTransform(const OutputVnlVectorType &vect ) const 
303 {
304 LEN   itkWarningMacro(<<"BackTransform(): This method is slated to be removed from ITK.  Instead, please use GetInverse() to generate an inverse transform and then perform the transform using that inverted transform.");
305   return this->GetInverseMatrix() * vect;
306 }
307
308
309 // Back Transform a CovariantVector
310 template<class TScalarType>
311 typename Rigid2DTransform<TScalarType>::InputCovariantVectorType
312 Rigid2DTransform<TScalarType>::
313 BackTransform(const OutputCovariantVectorType &vect) const 
314 {
315 LEN   itkWarningMacro(<<"BackTransform(): This method is slated to be removed from ITK.  Instead, please use GetInverse() to generate an inverse transform and then perform the transform using that inverted transform.");
316   return this->GetMatrix() * vect;
317 }
318  
319 // namespace
320
321 #endif
322

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