KWStyle - itkQuaternionRigidTransform.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkQuaternionRigidTransform.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:46 $
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 _itkQuaternionRigidTransform_txx
18 DEF #define _itkQuaternionRigidTransform_txx
19
20 #include "itkQuaternionRigidTransform.h"
21
22
23 namespace itk
24 {
25
26 // Constructor with default arguments
27 template <class TScalarType>
28 QuaternionRigidTransform<TScalarType>
29 ::QuaternionRigidTransform() :
30 IND **Superclass(SpaceDimension, ParametersDimension) 
31 {
32   m_Rotation = VnlQuaternionType(0,0,0,1); // axis * sin(t/2), cos(t/2)
33 }
34
35 // Constructor with default arguments
36 template<class TScalarType>
37 QuaternionRigidTransform<TScalarType>::
38 QuaternionRigidTransform( unsigned int outputSpaceDimension, 
39                           unsigned int parametersDimension ) :
40 IND **Superclass(outputSpaceDimension, parametersDimension)
41 {
42   m_Rotation = VnlQuaternionType(0,0,0,1); // axis * sin(t/2), cos(t/2)
43 }
44
45
46 EML
47 // Constructor with explicit arguments
48 template<class TScalarType>
49 QuaternionRigidTransform<TScalarType>::
50 QuaternionRigidTransform( const MatrixType & matrix,
51                           const OutputVectorType & offset ) :
52 IND **Superclass(matrix, offset)
53 {
54   this->ComputeMatrixParameters();
55 }
56
57
58 // Print self
59 template<class TScalarType>
60 void
61 QuaternionRigidTransform<TScalarType>::
62 PrintSelf(std::ostream &os, Indent indent ) const
63 {
64   Superclass::PrintSelf(os,indent);
65   os << indent << "Rotation:    " << m_Rotation    << std::endl;
66 }
67
68
69 // Set rotation
70 template<class TScalarType>
71 void
72 QuaternionRigidTransform<TScalarType>::
73 SetRotation(const VnlQuaternionType &rotation )
74 {
75   m_Rotation        = rotation;
76
77   this->ComputeMatrix();
78
79   return;
80 }
81
82
83 // Set the parameters in order to fit an Identity transform
84 template<class TScalarType>
85 void
86 QuaternionRigidTransform<TScalarType>::
87 SetIdentity( void ) 
88
89   m_Rotation = VnlQuaternionType(0,0,0,1);
90   this->Superclass::SetIdentity();
91 }
92
93
94 // Set Parameters
95 template <class TScalarType>
96 void
97 QuaternionRigidTransform<TScalarType>
98 ::SetParameters( const ParametersType & parameters )
99 {
100   OutputVectorType   translation; 
101
102   // Transfer the quaternion part
103   unsigned int par = 0;
104
105   for(unsigned int j=0; j < 4; j++) 
106     {
107     m_Rotation[j] = parameters[par];
108     ++par;
109     }
110   this->ComputeMatrix();
111
112   // Transfer the constant part
113   for(unsigned int i=0; i < SpaceDimension; i++) 
114     {
115     translation[i] = parameters[par];
116     ++par;
117     }
118   this->SetVarTranslation( translation );
119   this->ComputeOffset();
120
121 }
122
123
124 EML
125 // Set Parameters
126 template <class TScalarType>
127 const 
128 typename QuaternionRigidTransform<TScalarType>::ParametersType & 
129 QuaternionRigidTransform<TScalarType>
130 ::GetParameters() const
131 {
132   VnlQuaternionType  quaternion  = this->GetRotation();
133   OutputVectorType   translation = this->GetTranslation(); 
134
135   // Transfer the quaternion part
136   unsigned int par = 0;
137
138   for(unsigned int j=0; j < 4; j++) 
139     {
140     this->m_Parameters[par] = quaternion[j];
141     ++par;
142     }
143
144   // Transfer the constant part
145   for(unsigned int i=0; i < SpaceDimension; i++) 
146     {
147     this->m_Parameters[par] = translation[i];
148     ++par;
149     }
150
151   return this->m_Parameters;
152
153 }
154
155
156 // Get parameters
157 template<class TScalarType>
158 const typename QuaternionRigidTransform<TScalarType>::JacobianType &
159 QuaternionRigidTransform<TScalarType>::
160 GetJacobian( const InputPointType & p ) const
161 {
162
163   // compute derivatives with respect to rotation
164   this->m_Jacobian.Fill(0.0);
165
166   const TScalarType x = p[0] - this->GetCenter()[0];
167   const TScalarType y = p[1] - this->GetCenter()[1];
168   const TScalarType z = p[2] - this->GetCenter()[2];
169
170   // compute Jacobian with respect to quaternion parameters
171   this->m_Jacobian[0][0] =   2.0 * (  m_Rotation.x() * x + m_Rotation.y() * y 
172 IND ******************************+ m_Rotation.z() * z );
173   this->m_Jacobian[0][1] =   2.0 * (- m_Rotation.y() * x + m_Rotation.x() * y 
174 IND ******************************+ m_Rotation.r() * z );
175   this->m_Jacobian[0][2] =   2.0 * (- m_Rotation.z() * x - m_Rotation.r() * y 
176 IND ******************************+ m_Rotation.x() * z );
177   this->m_Jacobian[0][3] = - 2.0 * (- m_Rotation.r() * x + m_Rotation.z() * y 
178 IND ******************************- m_Rotation.y() * z );
179
180   this->m_Jacobian[1][0] = - this->m_Jacobian[0][1];
181   this->m_Jacobian[1][1] =   this->m_Jacobian[0][0];
182   this->m_Jacobian[1][2] =   this->m_Jacobian[0][3];
183   this->m_Jacobian[1][3] = - this->m_Jacobian[0][2];
184
185   this->m_Jacobian[2][0] = - this->m_Jacobian[0][2];
186   this->m_Jacobian[2][1] = - this->m_Jacobian[0][3];
187   this->m_Jacobian[2][2] =   this->m_Jacobian[0][0];
188   this->m_Jacobian[2][3] =   this->m_Jacobian[0][1];
189
190
191   // compute derivatives for the translation part
192   unsigned int blockOffset = 4;  
193   for(unsigned int dim=0; dim < SpaceDimension; dim++ ) 
194     {
195     this->m_Jacobian[ dim ][ blockOffset + dim ] = 1.0;
196     }
197
198   return this->m_Jacobian;
199
200 }
201  
202 template<class TScalarType>
203 const typename QuaternionRigidTransform< TScalarType >::InverseMatrixType &
204 QuaternionRigidTransform<TScalarType>::
205 GetInverseMatrix() const
206 {
207   // If the transform has been modified we recompute the inverse
208   if(this->InverseMatrixIsOld())
209     {
210     InverseMatrixType newMatrix;
211     VnlQuaternionType conjugateRotation = m_Rotation.conjugate();
212     VnlQuaternionType inverseRotation = conjugateRotation.inverse();
213     newMatrix = inverseRotation.rotation_matrix_transpose();
214     this->SetVarInverseMatrix(newMatrix);
215     }
216   return this->GetVarInverseMatrix();
217 }
218
219 template<class TScalarType>
220 void
221 QuaternionRigidTransform<TScalarType>::
222 ComputeMatrix()
223 {
224   VnlQuaternionType conjugateRotation = m_Rotation.conjugate();
225   // this is done to compensate for the transposed representation
226   // between VNL and ITK
227   MatrixType newMatrix;
228   newMatrix = conjugateRotation.rotation_matrix_transpose();
229   this->SetVarMatrix(newMatrix);
230 }
231
232 template<class TScalarType>
233 void
234 QuaternionRigidTransform<TScalarType>::
235 ComputeMatrixParameters()
236 {
237   VnlQuaternionType quat(this->GetMatrix().GetVnlMatrix());
238   m_Rotation = quat;
239 }
240  
241 // namespace
242
243 #endif
244

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