17 #ifndef __itkSimilarity3DTransform_txx
18 #define __itkSimilarity3DTransform_txx
21 #include "vnl/vnl_math.h"
22 #include "vnl/vnl_det.h"
29 template <
class TScalarType>
32 Superclass(OutputSpaceDimension, ParametersDimension)
39 template<
class TScalarType>
42 unsigned int paramDim) :
49 template<
class TScalarType>
59 template <
class TScalarType>
65 this->ComputeMatrix();
70 template<
class TScalarType>
80 double det = vnl_det( matrix.GetVnlMatrix() );
84 itkExceptionMacro( <<
"Attempting to set a matrix with a zero determinant" );
92 double s = vnl_math_cuberoot( det );
100 itkExceptionMacro( <<
"Attempting to set a matrix with a negative trace" );
104 testForOrthogonal /= s;
106 const double tolerance = 1e-10;
107 if( !this->MatrixIsOrthogonal( testForOrthogonal, tolerance ) )
109 itkExceptionMacro( <<
"Attempting to set a non-orthogonal matrix (after removing scaling)" );
113 this->Baseclass::SetMatrix( matrix );
118 template <
class TScalarType>
124 itkDebugMacro( <<
"Setting parameters " << parameters );
130 double norm = parameters[0]*parameters[0];
131 axis[0] = parameters[0];
132 norm += parameters[1]*parameters[1];
133 axis[1] = parameters[1];
134 norm += parameters[2]*parameters[2];
135 axis[2] = parameters[2];
138 norm = vcl_sqrt(norm);
141 double epsilon = 1e-10;
142 if(norm >= 1.0-epsilon)
144 axis = axis / (norm+epsilon*norm);
148 this->SetVarVersor( newVersor );
149 m_Scale = parameters[6];
150 this->ComputeMatrix();
152 itkDebugMacro( <<
"Versor is now " << this->GetVersor() );
157 newTranslation[0] = parameters[3];
158 newTranslation[1] = parameters[4];
159 newTranslation[2] = parameters[5];
160 this->SetVarTranslation(newTranslation);
161 this->ComputeOffset();
167 itkDebugMacro(<<
"After setting parameters ");
181 template <
class TScalarType>
186 itkDebugMacro( <<
"Getting parameters ");
188 this->m_Parameters[0] = this->GetVersor().GetX();
189 this->m_Parameters[1] = this->GetVersor().GetY();
190 this->m_Parameters[2] = this->GetVersor().GetZ();
193 this->m_Parameters[3] = this->GetTranslation()[0];
194 this->m_Parameters[4] = this->GetTranslation()[1];
195 this->m_Parameters[5] = this->GetTranslation()[2];
197 this->m_Parameters[6] = this->GetScale();
199 itkDebugMacro(<<
"After getting parameters " << this->m_Parameters );
201 return this->m_Parameters;
205 template<
class TScalarType>
210 typedef typename VersorType::ValueType ValueType;
213 const ValueType vx = this->GetVersor().GetX();
214 const ValueType vy = this->GetVersor().GetY();
215 const ValueType vz = this->GetVersor().GetZ();
216 const ValueType vw = this->GetVersor().GetW();
218 this->m_Jacobian.Fill(0.0);
222 const double px = pp[0];
223 const double py = pp[1];
224 const double pz = pp[2];
226 const double vxx = vx * vx;
227 const double vyy = vy * vy;
228 const double vzz = vz * vz;
229 const double vww = vw * vw;
231 const double vxy = vx * vy;
232 const double vxz = vx * vz;
233 const double vxw = vx * vw;
235 const double vyz = vy * vz;
236 const double vyw = vy * vw;
238 const double vzw = vz * vw;
242 this->m_Jacobian[0][0] = 2.0 * ( (vyw+vxz)*py + (vzw-vxy)*pz)
244 this->m_Jacobian[1][0] = 2.0 * ((vyw-vxz)*px -2*vxw *py + (vxx-vww)*pz)
246 this->m_Jacobian[2][0] = 2.0 * ((vzw+vxy)*px + (vww-vxx)*py -2*vxw *pz)
249 this->m_Jacobian[0][1] = 2.0 * ( -2*vyw *px + (vxw+vyz)*py + (vww-vyy)*pz)
251 this->m_Jacobian[1][1] = 2.0 * ((vxw-vyz)*px + (vzw+vxy)*pz)
253 this->m_Jacobian[2][1] = 2.0 * ((vyy-vww)*px + (vzw-vxy)*py -2*vyw *pz)
256 this->m_Jacobian[0][2] = 2.0 * ( -2*vzw *px + (vzz-vww)*py + (vxw-vyz)*pz)
258 this->m_Jacobian[1][2] = 2.0 * ((vww-vzz)*px -2*vzw *py + (vyw+vxz)*pz)
260 this->m_Jacobian[2][2] = 2.0 * ((vxw+vyz)*px + (vyw-vxz)*py )
264 this->m_Jacobian[0][3] = 1.0;
265 this->m_Jacobian[1][4] = 1.0;
266 this->m_Jacobian[2][5] = 1.0;
269 const MatrixType & matrix = this->GetMatrix();
273 this->m_Jacobian[0][6] = mpp[0] / m_Scale;
274 this->m_Jacobian[1][6] = mpp[1] / m_Scale;
275 this->m_Jacobian[2][6] = mpp[2] / m_Scale;
277 return this->m_Jacobian;
283 template <
class TScalarType>
288 this->Superclass::ComputeMatrix();
290 newMatrix *= m_Scale;
291 this->SetVarMatrix(newMatrix);
296 template <
class TScalarType>
303 m_Scale = vnl_math_cuberoot( vnl_det( matrix.GetVnlMatrix() ) );
309 this->SetVarVersor( v );
314 template<
class TScalarType>
319 Superclass::PrintSelf(os,indent);
320 os << indent <<
"Scale = " << m_Scale << std::endl;