| 1 |
HRD |
/* |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkMatrixOffsetTransformBase.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:41 $ |
| 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 |
|
|
| 18 |
|
#ifndef __itkMatrixOffsetTransformBase_h |
| 19 |
IND |
#define __itkMatrixOffsetTransformBase_h |
| 20 |
|
|
| 21 |
|
#include <iostream> |
| 22 |
|
|
| 23 |
|
#include "itkMatrix.h" |
| 24 |
|
#include "itkTransform.h" |
| 25 |
|
#include "itkExceptionObject.h" |
| 26 |
|
#include "itkMacro.h" |
| 27 |
|
|
| 28 |
|
namespace itk |
| 29 |
|
{ |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
/** |
| 33 |
|
* Matrix and Offset transformation of a vector space (e.g. space coordinates) |
| 34 |
|
* |
| 35 |
|
* This class serves as a base class for transforms that can be expressed |
| 36 |
|
* as a linear transformation plus a constant offset (e.g., affine, similarity |
| 37 |
|
* and rigid transforms). This base class also provides the concept of |
| 38 |
|
* using a center of rotation and a translation instead of an offset. |
| 39 |
|
* |
| 40 |
|
* As derived instances of this class are specializations of an affine |
| 41 |
|
* transform, any two of these transformations may be composed and the result |
| 42 |
|
* is an affine transformation. However, the order is important. |
| 43 |
|
* Given two affine transformations T1 and T2, we will say that |
| 44 |
|
* "precomposing T1 with T2" yields the transformation which applies |
| 45 |
|
* T1 to the source, and then applies T2 to that result to obtain the |
| 46 |
|
* target. Conversely, we will say that "postcomposing T1 with T2" |
| 47 |
|
* yields the transformation which applies T2 to the source, and then |
| 48 |
|
* applies T1 to that result to obtain the target. (Whether T1 or T2 |
| 49 |
|
* comes first lexicographically depends on whether you choose to |
| 50 |
|
* write mappings from right-to-left or vice versa; we avoid the whole |
| 51 |
|
* problem by referring to the order of application rather than the |
| 52 |
|
* textual order.) |
| 53 |
|
* |
| 54 |
|
* There are three template parameters for this class: |
| 55 |
|
* |
| 56 |
|
* ScalarT The type to be used for scalar numeric values. Either |
| 57 |
|
* float or double. |
| 58 |
|
* |
| 59 |
|
* NInputDimensions The number of dimensions of the input vector space. |
| 60 |
|
* |
| 61 |
|
* NOutputDimensions The number of dimensions of the output vector space. |
| 62 |
|
* |
| 63 |
|
* This class provides several methods for setting the matrix and offset |
| 64 |
|
* defining the transform. To support the registration framework, the |
| 65 |
|
* transform parameters can also be set as an Array<double> of size |
| 66 |
|
* (NInputDimension + 1) * NOutputDimension using method SetParameters(). |
| 67 |
|
* The first (NOutputDimension x NInputDimension) parameters defines the |
| 68 |
|
* matrix in column-major order (where the column index varies the fastest). |
| 69 |
|
* The last NOutputDimension parameters defines the translation |
| 70 |
|
* in each dimensions. |
| 71 |
|
* |
| 72 |
|
* \ingroup Transforms |
| 73 |
|
* |
| 74 |
|
*/ |
| 75 |
|
|
| 76 |
|
template < |
| 77 |
|
class TScalarType=double, // Data type for scalars |
| 78 |
|
unsigned int NInputDimensions=3, // Number of dimensions in the input space |
| 79 |
|
unsigned int NOutputDimensions=3> // Number of dimensions in the output space |
| 80 |
|
class MatrixOffsetTransformBase |
| 81 |
IND |
**: public Transform< TScalarType, NInputDimensions, NOutputDimensions > |
| 82 |
|
{ |
| 83 |
|
public: |
| 84 |
|
/** Standard typedefs */ |
| 85 |
|
typedef MatrixOffsetTransformBase Self; |
| 86 |
|
typedef Transform< TScalarType, |
| 87 |
|
NInputDimensions, |
| 88 |
|
NOutputDimensions > Superclass; |
| 89 |
|
typedef SmartPointer<Self> Pointer; |
| 90 |
|
typedef SmartPointer<const Self> ConstPointer; |
| 91 |
|
|
| 92 |
|
/** Run-time type information (and related methods). */ |
| 93 |
|
itkTypeMacro( MatrixOffsetTransformBase, Transform ); |
| 94 |
|
|
| 95 |
|
/** New macro for creation of through a Smart Pointer */ |
| 96 |
|
itkNewMacro( Self ); |
| 97 |
|
|
| 98 |
|
/** Dimension of the domain space. */ |
| 99 |
|
itkStaticConstMacro(InputSpaceDimension, unsigned int, NInputDimensions); |
| 100 |
|
itkStaticConstMacro(OutputSpaceDimension, unsigned int, NOutputDimensions); |
| 101 |
|
itkStaticConstMacro(ParametersDimension, unsigned int, |
| 102 |
|
NOutputDimensions*(NInputDimensions+1)); |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
/** Parameters Type */ |
| 106 |
|
typedef typename Superclass::ParametersType ParametersType; |
| 107 |
|
|
| 108 |
|
/** Jacobian Type */ |
| 109 |
|
typedef typename Superclass::JacobianType JacobianType; |
| 110 |
|
|
| 111 |
|
/** Standard scalar type for this class */ |
| 112 |
|
typedef typename Superclass::ScalarType ScalarType; |
| 113 |
|
|
| 114 |
|
/** Standard vector type for this class */ |
| 115 |
|
typedef Vector<TScalarType, |
| 116 |
|
itkGetStaticConstMacro(InputSpaceDimension)> InputVectorType; |
| 117 |
|
typedef Vector<TScalarType, |
| 118 |
|
itkGetStaticConstMacro(OutputSpaceDimension)> OutputVectorType; |
| 119 |
|
|
| 120 |
|
/** Standard covariant vector type for this class */ |
| 121 |
|
typedef CovariantVector<TScalarType, |
| 122 |
|
itkGetStaticConstMacro(InputSpaceDimension)> |
| 123 |
IND |
****************************************************InputCovariantVectorType; |
| 124 |
|
typedef CovariantVector<TScalarType, |
| 125 |
|
itkGetStaticConstMacro(OutputSpaceDimension)> |
| 126 |
IND |
****************************************************OutputCovariantVectorType; |
| 127 |
|
|
| 128 |
|
/** Standard vnl_vector type for this class */ |
| 129 |
|
typedef vnl_vector_fixed<TScalarType, |
| 130 |
|
itkGetStaticConstMacro(InputSpaceDimension)> |
| 131 |
IND |
****************************************************InputVnlVectorType; |
| 132 |
|
typedef vnl_vector_fixed<TScalarType, |
| 133 |
|
itkGetStaticConstMacro(OutputSpaceDimension)> |
| 134 |
IND |
****************************************************OutputVnlVectorType; |
| 135 |
|
|
| 136 |
|
/** Standard coordinate point type for this class */ |
| 137 |
|
typedef Point<TScalarType, |
| 138 |
|
itkGetStaticConstMacro(InputSpaceDimension)> |
| 139 |
IND |
****************************************************InputPointType; |
| 140 |
|
typedef Point<TScalarType, |
| 141 |
|
itkGetStaticConstMacro(OutputSpaceDimension)> |
| 142 |
IND |
****************************************************OutputPointType; |
| 143 |
|
|
| 144 |
|
/** Standard matrix type for this class */ |
| 145 |
|
typedef Matrix<TScalarType, itkGetStaticConstMacro(OutputSpaceDimension), |
| 146 |
|
itkGetStaticConstMacro(InputSpaceDimension)> |
| 147 |
IND |
****************************************************MatrixType; |
| 148 |
|
|
| 149 |
|
/** Standard inverse matrix type for this class */ |
| 150 |
|
typedef Matrix<TScalarType, itkGetStaticConstMacro(InputSpaceDimension), |
| 151 |
|
itkGetStaticConstMacro(OutputSpaceDimension)> |
| 152 |
IND |
****************************************************InverseMatrixType; |
| 153 |
|
|
| 154 |
|
typedef InputPointType CenterType; |
| 155 |
|
|
| 156 |
|
typedef OutputVectorType OffsetType; |
| 157 |
|
|
| 158 |
|
typedef OutputVectorType TranslationType; |
| 159 |
|
|
| 160 |
|
/** Set the transformation to an Identity |
| 161 |
|
* |
| 162 |
|
* This sets the matrix to identity and the Offset to null. */ |
| 163 |
|
virtual void SetIdentity( void ); |
| 164 |
|
|
| 165 |
|
/** Set matrix of an MatrixOffsetTransformBase |
| 166 |
|
* |
| 167 |
|
* This method sets the matrix of an MatrixOffsetTransformBase to a |
| 168 |
|
* value specified by the user. |
| 169 |
|
* |
| 170 |
|
* This updates the Offset wrt to current translation |
| 171 |
|
* and center. See the warning regarding offset-versus-translation |
| 172 |
|
* in the documentation for SetCenter. |
| 173 |
|
* |
| 174 |
|
* To define an affine transform, you must set the matrix, |
| 175 |
|
* center, and translation OR the matrix and offset */ |
| 176 |
|
virtual void SetMatrix(const MatrixType &matrix) |
| 177 |
|
{ m_Matrix = matrix; this->ComputeOffset(); |
| 178 |
IND |
******this->ComputeMatrixParameters(); |
| 179 |
IND |
******m_MatrixMTime.Modified(); this->Modified(); return; } |
| 180 |
|
|
| 181 |
|
/** Get matrix of an MatrixOffsetTransformBase |
| 182 |
|
* |
| 183 |
|
* This method returns the value of the matrix of the |
| 184 |
|
* MatrixOffsetTransformBase. |
| 185 |
|
* To define an affine transform, you must set the matrix, |
| 186 |
|
* center, and translation OR the matrix and offset */ |
| 187 |
|
const MatrixType & GetMatrix() const |
| 188 |
IND |
******{ return m_Matrix; } |
| 189 |
|
|
| 190 |
|
/** Set offset (origin) of an MatrixOffset TransformBase. |
| 191 |
|
* |
| 192 |
|
* This method sets the offset of an MatrixOffsetTransformBase to a |
| 193 |
|
* value specified by the user. |
| 194 |
|
* This updates Translation wrt current center. See the warning regarding |
| 195 |
|
* offset-versus-translation in the documentation for SetCenter. |
| 196 |
|
* To define an affine transform, you must set the matrix, |
| 197 |
|
* center, and translation OR the matrix and offset */ |
| 198 |
|
void SetOffset(const OutputVectorType &offset) |
| 199 |
IND |
******{ m_Offset = offset; this->ComputeTranslation(); |
| 200 |
IND |
********this->Modified(); return; } |
| 201 |
|
|
| 202 |
|
/** Get offset of an MatrixOffsetTransformBase |
| 203 |
|
* |
| 204 |
|
* This method returns the offset value of the MatrixOffsetTransformBase. |
| 205 |
|
* To define an affine transform, you must set the matrix, |
| 206 |
|
* center, and translation OR the matrix and offset */ |
| 207 |
|
const OutputVectorType & GetOffset(void) const |
| 208 |
IND |
******{ return m_Offset; } |
| 209 |
|
|
| 210 |
|
/** Set center of rotation of an MatrixOffsetTransformBase |
| 211 |
|
* |
| 212 |
|
* This method sets the center of rotation of an MatrixOffsetTransformBase |
| 213 |
|
* to a fixed point - for most transforms derived from this class, |
| 214 |
|
* this point is not a "parameter" of the transform - the exception is that |
| 215 |
|
* "centered" transforms have center as a parameter during optimization. |
| 216 |
|
* |
| 217 |
|
* This method updates offset wrt to current translation and matrix. |
| 218 |
|
* That is, changing the center changes the transform! |
| 219 |
|
* |
| 220 |
|
* WARNING: When using the Center, we strongly recommend only changing the |
| 221 |
|
* matrix and translation to define a transform. Changing a transform's |
| 222 |
|
* center, changes the mapping between spaces - specifically, translation is |
| 223 |
|
* not changed with respect to that new center, and so the offset is updated |
| 224 |
|
* to * maintain the consistency with translation. If a center is not used, |
| 225 |
|
* or is set before the matrix and the offset, then it is safe to change the |
| 226 |
|
* offset directly. |
| 227 |
|
* As a rule of thumb, if you wish to set the center explicitly, set |
| 228 |
|
* before Offset computations are done. |
| 229 |
|
* |
| 230 |
|
* To define an affine transform, you must set the matrix, |
| 231 |
|
* center, and translation OR the matrix and offset */ |
| 232 |
|
void SetCenter(const InputPointType & center) |
| 233 |
IND |
******{ m_Center = center; this->ComputeOffset(); |
| 234 |
IND |
********this->Modified(); return; } |
| 235 |
|
|
| 236 |
|
/** Get center of rotation of the MatrixOffsetTransformBase |
| 237 |
|
* |
| 238 |
|
* This method returns the point used as the fixed |
| 239 |
|
* center of rotation for the MatrixOffsetTransformBase. |
| 240 |
|
* To define an affine transform, you must set the matrix, |
| 241 |
|
* center, and translation OR the matrix and offset */ |
| 242 |
|
const InputPointType & GetCenter() const |
| 243 |
IND |
******{ return m_Center; } |
| 244 |
|
|
| 245 |
|
/** Set translation of an MatrixOffsetTransformBase |
| 246 |
|
* |
| 247 |
|
* This method sets the translation of an MatrixOffsetTransformBase. |
| 248 |
|
* This updates Offset to reflect current translation. |
| 249 |
|
* To define an affine transform, you must set the matrix, |
| 250 |
|
* center, and translation OR the matrix and offset */ |
| 251 |
|
void SetTranslation(const OutputVectorType & translation) |
| 252 |
IND |
******{ m_Translation = translation; this->ComputeOffset(); |
| 253 |
IND |
********this->Modified(); return; } |
| 254 |
|
|
| 255 |
|
/** Get translation component of the MatrixOffsetTransformBase |
| 256 |
|
* |
| 257 |
|
* This method returns the translation used after rotation |
| 258 |
|
* about the center point. |
| 259 |
|
* To define an affine transform, you must set the matrix, |
| 260 |
|
* center, and translation OR the matrix and offset */ |
| 261 |
|
const OutputVectorType & GetTranslation(void) const |
| 262 |
IND |
******{ return m_Translation; } |
| 263 |
|
|
| 264 |
|
|
| 265 |
|
/** Set the transformation from a container of parameters. |
| 266 |
|
* The first (NOutputDimension x NInputDimension) parameters define the |
| 267 |
|
* matrix and the last NOutputDimension parameters the translation. |
| 268 |
|
* Offset is updated based on current center. */ |
| 269 |
|
void SetParameters( const ParametersType & parameters ); |
| 270 |
|
|
| 271 |
|
/** Get the Transformation Parameters. */ |
| 272 |
|
const ParametersType& GetParameters(void) const; |
| 273 |
|
|
| 274 |
|
/** Set the fixed parameters and update internal transformation. */ |
| 275 |
|
virtual void SetFixedParameters( const ParametersType & ); |
| 276 |
|
|
| 277 |
|
/** Get the Fixed Parameters. */ |
| 278 |
|
virtual const ParametersType& GetFixedParameters(void) const; |
| 279 |
|
|
| 280 |
|
|
| 281 |
|
/** Compose with another MatrixOffsetTransformBase |
| 282 |
|
* |
| 283 |
|
* This method composes self with another MatrixOffsetTransformBase of the |
| 284 |
|
* same dimension, modifying self to be the composition of self |
| 285 |
|
* and other. If the argument pre is true, then other is |
| 286 |
|
* precomposed with self; that is, the resulting transformation |
| 287 |
|
* consists of first applying other to the source, followed by |
| 288 |
|
* self. If pre is false or omitted, then other is post-composed |
| 289 |
|
* with self; that is the resulting transformation consists of |
| 290 |
|
* first applying self to the source, followed by other. |
| 291 |
|
* This updates the Translation based on current center. */ |
| 292 |
|
void Compose(const Self * other, bool pre=0); |
| 293 |
|
|
| 294 |
|
/** Transform by an affine transformation |
| 295 |
|
* |
| 296 |
|
* This method applies the affine transform given by self to a |
| 297 |
|
* given point or vector, returning the transformed point or |
| 298 |
|
* vector. The TransformPoint method transforms its argument as |
| 299 |
|
* an affine point, whereas the TransformVector method transforms |
| 300 |
|
* its argument as a vector. */ |
| 301 |
|
OutputPointType TransformPoint(const InputPointType & point) const; |
| 302 |
|
OutputVectorType TransformVector(const InputVectorType & vector) const; |
| 303 |
|
OutputVnlVectorType TransformVector(const InputVnlVectorType & vector) const; |
| 304 |
|
OutputCovariantVectorType TransformCovariantVector( |
| 305 |
|
const InputCovariantVectorType &vector) const; |
| 306 |
|
|
| 307 |
|
/** Compute the Jacobian of the transformation |
| 308 |
|
* |
| 309 |
|
* This method computes the Jacobian matrix of the transformation. |
| 310 |
|
* given point or vector, returning the transformed point or |
| 311 |
|
* vector. The rank of the Jacobian will also indicate if the transform |
| 312 |
|
* is invertible at this point. */ |
| 313 |
|
const JacobianType & GetJacobian(const InputPointType & point ) const; |
| 314 |
|
|
| 315 |
|
/** Create inverse of an affine transformation |
| 316 |
IND |
***** |
| 317 |
IND |
***** This populates the parameters an affine transform such that |
| 318 |
IND |
***** the transform is the inverse of self. If self is not invertible, |
| 319 |
IND |
***** an exception is thrown. |
| 320 |
IND |
***** Note that by default the inverese transform is centered at |
| 321 |
IND |
***** the origin. If you need to compute the inverse centered at a point, p, |
| 322 |
IND |
***** |
| 323 |
IND |
***** \code |
| 324 |
IND |
***** transform2->SetCenter( p ); |
| 325 |
IND |
***** transform1->GetInverse( transform2 ); |
| 326 |
IND |
***** \endcode |
| 327 |
IND |
***** |
| 328 |
IND |
***** transform2 will now contain the inverse of transform1 and will |
| 329 |
IND |
***** with its center set to p. Flipping the two statements will produce an |
| 330 |
IND |
***** incorrect transform. */ |
| 331 |
|
bool GetInverse(Self * inverse) const; |
| 332 |
|
|
| 333 |
|
/** \deprecated Use GetInverse instead. |
| 334 |
|
* |
| 335 |
|
* Method will eventually be made a protected member function */ |
| 336 |
|
const InverseMatrixType & GetInverseMatrix( void ) const; |
| 337 |
|
|
| 338 |
|
protected: |
| 339 |
|
/** Construct an MatrixOffsetTransformBase object |
| 340 |
|
* |
| 341 |
|
* This method constructs a new MatrixOffsetTransformBase object and |
| 342 |
|
* initializes the matrix and offset parts of the transformation |
| 343 |
|
* to values specified by the caller. If the arguments are |
| 344 |
|
* omitted, then the MatrixOffsetTransformBase is initialized to an identity |
| 345 |
|
* transformation in the appropriate number of dimensions. **/ |
| 346 |
|
MatrixOffsetTransformBase(const MatrixType &matrix, |
| 347 |
|
const OutputVectorType &offset); |
| 348 |
|
MatrixOffsetTransformBase(unsigned int outputDims, |
| 349 |
|
unsigned int paramDims); |
| 350 |
|
MatrixOffsetTransformBase(); |
| 351 |
|
|
| 352 |
|
/** Destroy an MatrixOffsetTransformBase object **/ |
| 353 |
|
virtual ~MatrixOffsetTransformBase(); |
| 354 |
|
|
| 355 |
|
/** Print contents of an MatrixOffsetTransformBase */ |
| 356 |
|
void PrintSelf(std::ostream &s, Indent indent) const; |
| 357 |
|
|
| 358 |
|
InverseMatrixType GetVarInverseMatrix( void ) const |
| 359 |
|
{ return m_InverseMatrix; }; |
| 360 |
|
void SetVarInverseMatrix(const InverseMatrixType & matrix) const |
| 361 |
|
{ m_InverseMatrix = matrix; m_InverseMatrixMTime.Modified(); }; |
| 362 |
|
bool InverseMatrixIsOld(void) const |
| 363 |
|
{ if(m_MatrixMTime != m_InverseMatrixMTime) |
| 364 |
IND |
********{ return true; } else { return false; } }; |
| 365 |
|
|
| 366 |
|
virtual void ComputeMatrixParameters(void); |
| 367 |
|
|
| 368 |
|
virtual void ComputeMatrix(void); |
| 369 |
|
void SetVarMatrix(const MatrixType & matrix) |
| 370 |
|
{ m_Matrix = matrix; m_MatrixMTime.Modified(); }; |
| 371 |
|
|
| 372 |
|
virtual void ComputeTranslation(void); |
| 373 |
|
void SetVarTranslation(const OutputVectorType & translation) |
| 374 |
|
{ m_Translation = translation; }; |
| 375 |
|
|
| 376 |
|
virtual void ComputeOffset(void); |
| 377 |
|
void SetVarOffset(const OutputVectorType & offset) |
| 378 |
|
{ m_Offset = offset; }; |
| 379 |
|
|
| 380 |
|
void SetVarCenter(const InputPointType & center) |
| 381 |
|
{ m_Center = center; }; |
| 382 |
|
|
| 383 |
|
private: |
| 384 |
|
|
| 385 |
|
MatrixOffsetTransformBase(const Self & other); |
| 386 |
|
const Self & operator=( const Self & ); |
| 387 |
|
|
| 388 |
|
|
| 389 |
|
MatrixType m_Matrix; // Matrix of the transformation |
| 390 |
|
OutputVectorType m_Offset; // Offset of the transformation |
| 391 |
|
mutable InverseMatrixType m_InverseMatrix; // Inverse of the matrix |
| 392 |
|
mutable bool m_Singular; // Is m_Inverse singular? |
| 393 |
|
|
| 394 |
|
InputPointType m_Center; |
| 395 |
|
OutputVectorType m_Translation; |
| 396 |
|
|
| 397 |
|
/** To avoid recomputation of the inverse if not needed */ |
| 398 |
|
TimeStamp m_MatrixMTime; |
| 399 |
|
mutable TimeStamp m_InverseMatrixMTime; |
| 400 |
|
|
| 401 |
|
}; //class MatrixOffsetTransformBase |
| 402 |
|
|
| 403 |
|
} // namespace itk |
| 404 |
|
|
| 405 |
|
|
| 406 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 407 |
|
#include "itkMatrixOffsetTransformBase.txx" |
| 408 |
|
#endif |
| 409 |
|
|
| 410 |
|
#endif /* __itkMatrixOffsetTransformBase_h */ |
| 411 |
|
|
| 412 |
EOF |
|