| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkScaleSkewVersor3DTransform.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 _itkScaleSkewVersor3DTransform_txx |
| 18 |
DEF |
#define _itkScaleSkewVersor3DTransform_txx |
| 19 |
|
|
| 20 |
|
#include "itkScaleSkewVersor3DTransform.h" |
| 21 |
|
|
| 22 |
|
|
| 23 |
|
namespace itk |
| 24 |
IND |
**{ |
| 25 |
|
|
| 26 |
|
// Constructor with default arguments |
| 27 |
|
template <class TScalarType> |
| 28 |
|
ScaleSkewVersor3DTransform<TScalarType> |
| 29 |
|
::ScaleSkewVersor3DTransform() : |
| 30 |
IND |
**Superclass(OutputSpaceDimension, ParametersDimension) |
| 31 |
|
{ |
| 32 |
|
m_Scale.Fill( 1.0 ); |
| 33 |
|
m_Skew.Fill( 0.0 ); |
| 34 |
|
} |
| 35 |
|
|
| 36 |
|
|
| 37 |
|
// Constructor with arguments |
| 38 |
|
template<class TScalarType> |
| 39 |
|
ScaleSkewVersor3DTransform<TScalarType>:: |
| 40 |
|
ScaleSkewVersor3DTransform( unsigned int spaceDimension, |
| 41 |
|
unsigned int parametersDimension): |
| 42 |
IND |
**Superclass(spaceDimension, parametersDimension) |
| 43 |
|
{ |
| 44 |
|
m_Scale.Fill( 1.0 ); |
| 45 |
|
m_Skew.Fill( 0.0 ); |
| 46 |
|
} |
| 47 |
|
|
| 48 |
|
// Constructor with arguments |
| 49 |
|
template<class TScalarType> |
| 50 |
|
ScaleSkewVersor3DTransform<TScalarType>:: |
| 51 |
|
ScaleSkewVersor3DTransform( const MatrixType & matrix, |
| 52 |
|
const OutputVectorType & offset): |
| 53 |
IND |
**Superclass(matrix, offset) |
| 54 |
|
{ |
| 55 |
|
this->ComputeMatrixParameters(); |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
// Set Parameters |
| 60 |
|
template <class TScalarType> |
| 61 |
|
void |
| 62 |
|
ScaleSkewVersor3DTransform<TScalarType> |
| 63 |
|
::SetParameters( const ParametersType & parameters ) |
| 64 |
IND |
**{ |
| 65 |
|
|
| 66 |
|
itkDebugMacro( << "Setting paramaters " << parameters ); |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
// Transfer the versor part |
| 70 |
|
|
| 71 |
|
AxisType axis; |
| 72 |
|
|
| 73 |
|
double norm = parameters[0]*parameters[0]; |
| 74 |
|
axis[0] = parameters[0]; |
| 75 |
|
norm += parameters[1]*parameters[1]; |
| 76 |
|
axis[1] = parameters[1]; |
| 77 |
|
norm += parameters[2]*parameters[2]; |
| 78 |
|
axis[2] = parameters[2]; |
| 79 |
|
if( norm > 0) |
| 80 |
|
{ |
| 81 |
|
norm = sqrt(norm); |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
double epsilon = 1e-10; |
| 85 |
|
if(norm >= 1.0-epsilon) |
| 86 |
|
{ |
| 87 |
|
axis = axis / (norm+epsilon*norm); |
| 88 |
|
} |
| 89 |
|
VersorType newVersor; |
| 90 |
|
newVersor.Set(axis); |
| 91 |
|
this->SetVarVersor( newVersor ); |
| 92 |
|
|
| 93 |
|
itkDebugMacro( <<"Versor is now " << newVersor ); |
| 94 |
|
|
| 95 |
|
// Matrix must be defined before translation so that offset can be computed |
| 96 |
|
// from translation |
| 97 |
|
m_Scale[0] = parameters[6]; |
| 98 |
|
m_Scale[1] = parameters[7]; |
| 99 |
|
m_Scale[2] = parameters[8]; |
| 100 |
|
|
| 101 |
|
m_Skew[0] = parameters[9]; |
| 102 |
|
m_Skew[1] = parameters[10]; |
| 103 |
|
m_Skew[2] = parameters[11]; |
| 104 |
|
m_Skew[3] = parameters[12]; |
| 105 |
|
m_Skew[4] = parameters[13]; |
| 106 |
|
m_Skew[5] = parameters[14]; |
| 107 |
|
|
| 108 |
|
// Transfer the translation part |
| 109 |
|
TranslationType newTranslation; |
| 110 |
|
newTranslation[0] = parameters[3]; |
| 111 |
|
newTranslation[1] = parameters[4]; |
| 112 |
|
newTranslation[2] = parameters[5]; |
| 113 |
|
this->SetVarTranslation(newTranslation); |
| 114 |
|
this->ComputeMatrix(); |
| 115 |
|
this->ComputeOffset(); |
| 116 |
|
|
| 117 |
|
itkDebugMacro(<<"After setting paramaters "); |
| 118 |
IND |
**} |
| 119 |
|
|
| 120 |
|
|
| 121 |
EML |
|
| 122 |
|
// |
| 123 |
|
// Get Parameters |
| 124 |
|
// |
| 125 |
|
// Parameters are ordered as: |
| 126 |
|
// |
| 127 |
|
// p[0:2] = right part of the versor (axis times sin(t/2)) |
| 128 |
|
// p[3:5] = translation components |
| 129 |
|
// p[6:8] = Scale |
| 130 |
|
// p[9:14] = Skew {xy, xz, yx, yz, zx, zy} |
| 131 |
|
// |
| 132 |
|
|
| 133 |
|
template <class TScalarType> |
| 134 |
|
const typename ScaleSkewVersor3DTransform<TScalarType>::ParametersType & |
| 135 |
|
ScaleSkewVersor3DTransform<TScalarType> |
| 136 |
|
::GetParameters( void ) const |
| 137 |
IND |
**{ |
| 138 |
|
itkDebugMacro( << "Getting parameters "); |
| 139 |
|
|
| 140 |
|
this->m_Parameters[0] = this->GetVersor().GetX(); |
| 141 |
|
this->m_Parameters[1] = this->GetVersor().GetY(); |
| 142 |
|
this->m_Parameters[2] = this->GetVersor().GetZ(); |
| 143 |
|
|
| 144 |
|
this->m_Parameters[3] = this->GetTranslation()[0]; |
| 145 |
|
this->m_Parameters[4] = this->GetTranslation()[1]; |
| 146 |
|
this->m_Parameters[5] = this->GetTranslation()[2]; |
| 147 |
|
|
| 148 |
|
this->m_Parameters[6] = this->GetScale()[0]; |
| 149 |
|
this->m_Parameters[7] = this->GetScale()[1]; |
| 150 |
|
this->m_Parameters[8] = this->GetScale()[2]; |
| 151 |
|
|
| 152 |
|
this->m_Parameters[9] = this->GetSkew()[0]; |
| 153 |
|
this->m_Parameters[10] = this->GetSkew()[1]; |
| 154 |
|
this->m_Parameters[11] = this->GetSkew()[2]; |
| 155 |
|
this->m_Parameters[12] = this->GetSkew()[3]; |
| 156 |
|
this->m_Parameters[13] = this->GetSkew()[4]; |
| 157 |
|
this->m_Parameters[14] = this->GetSkew()[5]; |
| 158 |
|
|
| 159 |
|
itkDebugMacro(<<"After getting parameters " << this->m_Parameters ); |
| 160 |
|
|
| 161 |
|
return this->m_Parameters; |
| 162 |
IND |
**} |
| 163 |
|
|
| 164 |
|
template <class TScalarType> |
| 165 |
|
void |
| 166 |
|
ScaleSkewVersor3DTransform<TScalarType> |
| 167 |
|
::SetIdentity() |
| 168 |
IND |
**{ |
| 169 |
|
m_Scale.Fill( 1.0 ); |
| 170 |
|
m_Skew.Fill( 0.0 ); |
| 171 |
|
Superclass::SetIdentity(); |
| 172 |
IND |
**} |
| 173 |
|
|
| 174 |
|
|
| 175 |
|
template <class TScalarType> |
| 176 |
|
void |
| 177 |
|
ScaleSkewVersor3DTransform<TScalarType> |
| 178 |
|
::SetScale( const ScaleVectorType & scale ) |
| 179 |
|
{ |
| 180 |
|
m_Scale = scale; |
| 181 |
|
this->ComputeMatrix(); |
| 182 |
|
} |
| 183 |
|
|
| 184 |
|
template <class TScalarType> |
| 185 |
|
void |
| 186 |
|
ScaleSkewVersor3DTransform<TScalarType> |
| 187 |
|
::SetSkew( const SkewVectorType & skew ) |
| 188 |
|
{ |
| 189 |
|
m_Skew = skew; |
| 190 |
|
this->ComputeMatrix(); |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
// Compute the matrix |
| 194 |
|
template <class TScalarType> |
| 195 |
|
void |
| 196 |
|
ScaleSkewVersor3DTransform<TScalarType> |
| 197 |
|
::ComputeMatrix( void ) |
| 198 |
|
{ |
| 199 |
|
|
| 200 |
|
VersorType versor = Superclass::GetVersor(); |
| 201 |
|
|
| 202 |
|
const TScalarType vx = versor.GetX(); |
| 203 |
|
const TScalarType vy = versor.GetY(); |
| 204 |
|
const TScalarType vz = versor.GetZ(); |
| 205 |
|
const TScalarType vw = versor.GetW(); |
| 206 |
|
|
| 207 |
|
const TScalarType xx = vx * vx; |
| 208 |
|
const TScalarType yy = vy * vy; |
| 209 |
|
const TScalarType zz = vz * vz; |
| 210 |
|
const TScalarType xy = vx * vy; |
| 211 |
|
const TScalarType xz = vx * vz; |
| 212 |
|
const TScalarType xw = vx * vw; |
| 213 |
|
const TScalarType yz = vy * vz; |
| 214 |
|
const TScalarType yw = vy * vw; |
| 215 |
|
const TScalarType zw = vz * vw; |
| 216 |
|
|
| 217 |
|
MatrixType newMatrix; |
| 218 |
|
newMatrix[0][0] = m_Scale[0] - 2.0 * ( yy + zz ); |
| 219 |
|
newMatrix[1][1] = m_Scale[1] - 2.0 * ( xx + zz ); |
| 220 |
|
newMatrix[2][2] = m_Scale[2] - 2.0 * ( xx + yy ); |
| 221 |
|
newMatrix[0][1] = 2.0 * ( xy - zw ) + ( m_Skew[0] ); |
| 222 |
|
newMatrix[0][2] = 2.0 * ( xz + yw ) + ( m_Skew[1] ); |
| 223 |
|
newMatrix[1][0] = 2.0 * ( xy + zw ) + ( m_Skew[2] ); |
| 224 |
|
newMatrix[1][2] = 2.0 * ( yz - xw ) + ( m_Skew[3] ); |
| 225 |
|
newMatrix[2][0] = 2.0 * ( xz - yw ) + ( m_Skew[4] ); |
| 226 |
|
newMatrix[2][1] = 2.0 * ( yz + xw ) + ( m_Skew[5] ); |
| 227 |
|
this->SetVarMatrix ( newMatrix ); |
| 228 |
|
} |
| 229 |
|
|
| 230 |
|
template <class TScalarType> |
| 231 |
|
void |
| 232 |
|
ScaleSkewVersor3DTransform<TScalarType> |
| 233 |
|
::ComputeMatrixParameters( void ) |
| 234 |
|
{ |
| 235 |
LEN,IND |
****itkExceptionMacro( << "Setting the matrix of a ScaleSkewVersor3D transform is not supported at this time." ); |
| 236 |
|
} |
| 237 |
|
|
| 238 |
|
|
| 239 |
|
// Print self |
| 240 |
|
template<class TScalarType> |
| 241 |
|
void |
| 242 |
|
ScaleSkewVersor3DTransform<TScalarType>:: |
| 243 |
|
PrintSelf(std::ostream &os, Indent indent) const |
| 244 |
|
{ |
| 245 |
|
|
| 246 |
|
Superclass::PrintSelf(os,indent); |
| 247 |
|
|
| 248 |
|
os << indent << "Scale: " << m_Scale << std::endl; |
| 249 |
|
os << indent << "Skew: " << m_Skew << std::endl; |
| 250 |
|
} |
| 251 |
|
|
| 252 |
|
// Set parameters |
| 253 |
|
template<class TScalarType> |
| 254 |
|
const typename ScaleSkewVersor3DTransform<TScalarType>::JacobianType & |
| 255 |
|
ScaleSkewVersor3DTransform<TScalarType>:: |
| 256 |
|
GetJacobian( const InputPointType & p ) const |
| 257 |
|
{ |
| 258 |
|
typedef typename VersorType::ValueType ValueType; |
| 259 |
|
|
| 260 |
|
// compute derivatives with respect to rotation |
| 261 |
|
const ValueType vx = this->GetVersor().GetX(); |
| 262 |
|
const ValueType vy = this->GetVersor().GetY(); |
| 263 |
|
const ValueType vz = this->GetVersor().GetZ(); |
| 264 |
|
const ValueType vw = this->GetVersor().GetW(); |
| 265 |
|
|
| 266 |
|
this->m_Jacobian.Fill(0.0); |
| 267 |
|
|
| 268 |
|
const double px = p[0] - this->GetCenter()[0]; |
| 269 |
|
const double py = p[1] - this->GetCenter()[1]; |
| 270 |
|
const double pz = p[2] - this->GetCenter()[2]; |
| 271 |
|
|
| 272 |
|
const double vxx = vx * vx; |
| 273 |
|
const double vyy = vy * vy; |
| 274 |
|
const double vzz = vz * vz; |
| 275 |
|
const double vww = vw * vw; |
| 276 |
|
|
| 277 |
|
const double vxy = vx * vy; |
| 278 |
|
const double vxz = vx * vz; |
| 279 |
|
const double vxw = vx * vw; |
| 280 |
|
|
| 281 |
|
const double vyz = vy * vz; |
| 282 |
|
const double vyw = vy * vw; |
| 283 |
|
|
| 284 |
|
const double vzw = vz * vw; |
| 285 |
|
|
| 286 |
|
|
| 287 |
|
// compute Jacobian with respect to quaternion parameters |
| 288 |
|
this->m_Jacobian[0][0] = 2.0 * ( (vyw+vxz)*py + (vzw-vxy)*pz) |
| 289 |
IND |
*************************/ vw; |
| 290 |
|
this->m_Jacobian[1][0] = 2.0 * ((vyw-vxz)*px -2*vxw *py + (vxx-vww)*pz) |
| 291 |
IND |
*************************/ vw; |
| 292 |
|
this->m_Jacobian[2][0] = 2.0 * ((vzw+vxy)*px + (vww-vxx)*py -2*vxw *pz) |
| 293 |
IND |
*************************/ vw; |
| 294 |
|
|
| 295 |
|
this->m_Jacobian[0][1] = 2.0 * ( -2*vyw *px + (vxw+vyz)*py + (vww-vyy)*pz) |
| 296 |
IND |
*************************/ vw; |
| 297 |
|
this->m_Jacobian[1][1] = 2.0 * ((vxw-vyz)*px + (vzw+vxy)*pz) |
| 298 |
IND |
*************************/ vw; |
| 299 |
|
this->m_Jacobian[2][1] = 2.0 * ((vyy-vww)*px + (vzw-vxy)*py -2*vyw *pz) |
| 300 |
IND |
*************************/ vw; |
| 301 |
|
|
| 302 |
|
this->m_Jacobian[0][2] = 2.0 * ( -2*vzw *px + (vzz-vww)*py + (vxw-vyz)*pz) |
| 303 |
IND |
*************************/ vw; |
| 304 |
|
this->m_Jacobian[1][2] = 2.0 * ((vww-vzz)*px -2*vzw *py + (vyw+vxz)*pz) |
| 305 |
IND |
*************************/ vw; |
| 306 |
|
this->m_Jacobian[2][2] = 2.0 * ((vxw+vyz)*px + (vyw-vxz)*py ) |
| 307 |
IND |
*************************/ vw; |
| 308 |
|
|
| 309 |
|
this->m_Jacobian[0][3] = 1.0; |
| 310 |
|
this->m_Jacobian[1][4] = 1.0; |
| 311 |
|
this->m_Jacobian[2][5] = 1.0; |
| 312 |
|
|
| 313 |
|
this->m_Jacobian[0][6] = px; |
| 314 |
|
this->m_Jacobian[1][7] = py; |
| 315 |
|
this->m_Jacobian[2][8] = pz; |
| 316 |
|
|
| 317 |
|
this->m_Jacobian[0][9] = py; |
| 318 |
|
this->m_Jacobian[0][10] = pz; |
| 319 |
|
this->m_Jacobian[1][11] = px; |
| 320 |
|
this->m_Jacobian[1][12] = pz; |
| 321 |
|
this->m_Jacobian[2][13] = px; |
| 322 |
|
this->m_Jacobian[2][14] = py; |
| 323 |
|
|
| 324 |
|
return this->m_Jacobian; |
| 325 |
|
|
| 326 |
|
} |
| 327 |
|
|
| 328 |
|
|
| 329 |
EML |
|
| 330 |
|
} // namespace |
| 331 |
|
|
| 332 |
|
#endif |
| 333 |
|
|