ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkAffineTransform.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkAffineTransform_h
00019 #define __itkAffineTransform_h
00020 
00021 #include <iostream>
00022 
00023 #include "itkMatrixOffsetTransformBase.h"
00024 
00025 namespace itk
00026 {
00098 
00099 template<
00100   class TScalarType = double,      // Data type for scalars
00101                                    //    (e.g. float or double)
00102   unsigned int NDimensions = 3 >
00103 // Number of dimensions in the input space
00104 class AffineTransform:
00105   public MatrixOffsetTransformBase< TScalarType, NDimensions, NDimensions >
00106 {
00107 public:
00109   typedef AffineTransform Self;
00110   typedef MatrixOffsetTransformBase< TScalarType,
00111                                      NDimensions,
00112                                      NDimensions >  Superclass;
00113 
00114   typedef SmartPointer< Self >       Pointer;
00115   typedef SmartPointer< const Self > ConstPointer;
00116 
00118   itkTypeMacro(AffineTransform, MatrixOffsetTransformBase);
00119 
00121   itkNewMacro(Self);
00122 
00124   itkStaticConstMacro(InputSpaceDimension, unsigned int, NDimensions);
00125   itkStaticConstMacro(OutputSpaceDimension, unsigned int, NDimensions);
00126   itkStaticConstMacro(SpaceDimension, unsigned int, NDimensions);
00127   itkStaticConstMacro( ParametersDimension, unsigned int,
00128                        NDimensions *( NDimensions + 1 ) );
00130 
00132   itkTransformCloneMacro();
00133 
00135   typedef typename Superclass::ParametersType            ParametersType;
00136   typedef typename Superclass::JacobianType              JacobianType;
00137   typedef typename Superclass::ScalarType                ScalarType;
00138   typedef typename Superclass::InputPointType            InputPointType;
00139   typedef typename Superclass::OutputPointType           OutputPointType;
00140   typedef typename Superclass::InputVectorType           InputVectorType;
00141   typedef typename Superclass::OutputVectorType          OutputVectorType;
00142   typedef typename Superclass::InputVnlVectorType        InputVnlVectorType;
00143   typedef typename Superclass::OutputVnlVectorType       OutputVnlVectorType;
00144   typedef typename Superclass::InputCovariantVectorType  InputCovariantVectorType;
00145   typedef typename Superclass::OutputCovariantVectorType OutputCovariantVectorType;
00146   typedef typename Superclass::MatrixType                MatrixType;
00147   typedef typename Superclass::InverseMatrixType         InverseMatrixType;
00148   typedef typename Superclass::CenterType                CenterType;
00149   typedef typename Superclass::OffsetType                OffsetType;
00150   typedef typename Superclass::TranslationType           TranslationType;
00151 
00154   typedef typename Superclass::InverseTransformBaseType InverseTransformBaseType;
00155   typedef typename InverseTransformBaseType::Pointer    InverseTransformBasePointer;
00156 
00163   void Translate(const OutputVectorType & offset, bool pre = 0);
00164 
00176   void Scale(const OutputVectorType & factor, bool pre = 0);
00177 
00178   void Scale(const TScalarType & factor, bool pre = 0);
00179 
00195   void Rotate(int axis1, int axis2, TScalarType angle, bool pre = 0);
00197 
00211   void Rotate2D(TScalarType angle, bool pre = 0);
00212 
00226   void Rotate3D(const OutputVectorType & axis, TScalarType angle, bool pre = 0);
00227 
00239   void Shear(int axis1, int axis2, TScalarType coef, bool pre = 0);
00240 
00242   bool GetInverse(Self *inverse) const;
00243 
00245   virtual InverseTransformBasePointer GetInverseTransform() const;
00246 
00255   inline InputPointType   BackTransform(const OutputPointType  & point) const;
00256 
00257   inline InputVectorType  BackTransform(const OutputVectorType & vector) const;
00258 
00259   inline InputVnlVectorType BackTransform(
00260     const OutputVnlVectorType & vector) const;
00261 
00262   inline InputCovariantVectorType BackTransform(
00263     const OutputCovariantVectorType & vector) const;
00264 
00274   inline InputPointType BackTransformPoint(const OutputPointType  & point) const;
00275 
00287   ScalarType Metric(const Self *other) const;
00288 
00292   ScalarType Metric(void) const;
00293 
00294 protected:
00302   AffineTransform(const MatrixType & matrix,
00303                   const OutputVectorType & offset);
00304   AffineTransform(unsigned int paramDims);
00305   AffineTransform();
00307 
00309   virtual ~AffineTransform();
00310 
00312   void PrintSelf(std::ostream & s, Indent indent) const;
00313 
00314 private:
00315 
00316   AffineTransform(const Self & other);
00317   const Self & operator=(const Self &);
00318 }; //class AffineTransform
00319 
00321 template< class TScalarType, unsigned int NDimensions >
00322 inline
00323 typename AffineTransform< TScalarType, NDimensions >::InputVectorType
00324 AffineTransform< TScalarType, NDimensions >::BackTransform(const OutputVectorType & vect) const
00325 {
00326   itkWarningMacro(
00327     << "BackTransform(): This method is slated to be removed "
00328     << "from ITK. Instead, please use GetInverse() to generate an inverse "
00329     << "transform and then perform the transform using that inverted transform.");
00330   return this->GetInverseMatrix() * vect;
00331 }
00333 
00335 template< class TScalarType, unsigned int NDimensions >
00336 inline
00337 typename AffineTransform< TScalarType, NDimensions >::InputVnlVectorType
00338 AffineTransform< TScalarType, NDimensions >::BackTransform(const OutputVnlVectorType & vect) const
00339 {
00340   itkWarningMacro(
00341     << "BackTransform(): This method is slated to be removed "
00342     << "from ITK. Instead, please use GetInverse() to generate an inverse "
00343     << "transform and then perform the transform using that inverted transform.");
00344   return this->GetInverseMatrix() * vect;
00345 }
00347 
00349 template< class TScalarType, unsigned int NDimensions >
00350 inline
00351 typename AffineTransform< TScalarType, NDimensions >::InputCovariantVectorType
00352 AffineTransform< TScalarType, NDimensions >::BackTransform(const OutputCovariantVectorType & vec) const
00353 {
00354   itkWarningMacro(
00355     << "BackTransform(): This method is slated to be removed "
00356     << "from ITK. Instead, please use GetInverse() to generate an inverse "
00357     << "transform and then perform the transform using that inverted transform.");
00358 
00359   InputCovariantVectorType result;    // Converted vector
00360 
00361   for ( unsigned int i = 0; i < NDimensions; i++ )
00362     {
00363     result[i] = NumericTraits< ScalarType >::Zero;
00364     for ( unsigned int j = 0; j < NDimensions; j++ )
00365       {
00366       result[i] += this->GetMatrix()[j][i] * vec[j]; // Direct matrix transposed
00367       }
00368     }
00369   return result;
00370 }
00371 
00373 template< class TScalarType, unsigned int NDimensions >
00374 inline
00375 typename AffineTransform< TScalarType, NDimensions >::InputPointType
00376 AffineTransform< TScalarType, NDimensions >::BackTransformPoint(const OutputPointType & point) const
00377 {
00378   return this->BackTransform(point);
00379 }
00380 
00382 template< class TScalarType, unsigned int NDimensions >
00383 inline
00384 typename AffineTransform< TScalarType, NDimensions >::InputPointType
00385 AffineTransform< TScalarType, NDimensions >::BackTransform(const OutputPointType & point) const
00386 {
00387   itkWarningMacro(
00388     << "BackTransform(): This method is slated to be removed "
00389     << "from ITK.  Instead, please use GetInverse() to generate an inverse "
00390     << "transform and then perform the transform using that inverted transform.");
00391   InputPointType result;       // Converted point
00392   ScalarType     temp[NDimensions];
00393   unsigned int   i, j;
00395 
00396   for ( j = 0; j < NDimensions; j++ )
00397     {
00398     temp[j] = point[j] - this->GetOffset()[j];
00399     }
00400 
00401   for ( i = 0; i < NDimensions; i++ )
00402     {
00403     result[i] = 0.0;
00404     for ( j = 0; j < NDimensions; j++ )
00405       {
00406       result[i] += this->GetInverseMatrix()[i][j] * temp[j];
00407       }
00408     }
00409   return result;
00410 }
00411 }  // namespace itk
00412 
00413 // Define instantiation macro for this template.
00414 #define ITK_TEMPLATE_AffineTransform(_, EXPORT, TypeX, TypeY)                \
00415   namespace itk                                                              \
00416   {                                                                          \
00417   _( 2 ( class EXPORT AffineTransform< ITK_TEMPLATE_2 TypeX > ) )            \
00418   namespace Templates                                                        \
00419   {                                                                          \
00420   typedef AffineTransform< ITK_TEMPLATE_2 TypeX >  AffineTransform##TypeY; \
00421   }                                                                          \
00422   }
00423 
00424 #if ITK_TEMPLATE_EXPLICIT
00425 #include "Templates/itkAffineTransform+-.h"
00426 #endif
00427 
00428 #if ITK_TEMPLATE_TXX
00429 #include "itkAffineTransform.hxx"
00430 #endif
00431 
00432 #endif /* __itkAffineTransform_h */
00433