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   typedef typename Superclass::ParametersType            ParametersType;
00133   typedef typename Superclass::JacobianType              JacobianType;
00134   typedef typename Superclass::ScalarType                ScalarType;
00135   typedef typename Superclass::InputPointType            InputPointType;
00136   typedef typename Superclass::OutputPointType           OutputPointType;
00137   typedef typename Superclass::InputVectorType           InputVectorType;
00138   typedef typename Superclass::OutputVectorType          OutputVectorType;
00139   typedef typename Superclass::InputVnlVectorType        InputVnlVectorType;
00140   typedef typename Superclass::OutputVnlVectorType       OutputVnlVectorType;
00141   typedef typename Superclass::InputCovariantVectorType  InputCovariantVectorType;
00142   typedef typename Superclass::OutputCovariantVectorType OutputCovariantVectorType;
00143   typedef typename Superclass::MatrixType                MatrixType;
00144   typedef typename Superclass::InverseMatrixType         InverseMatrixType;
00145   typedef typename Superclass::CenterType                CenterType;
00146   typedef typename Superclass::OffsetType                OffsetType;
00147   typedef typename Superclass::TranslationType           TranslationType;
00148 
00151   typedef typename Superclass::InverseTransformBaseType InverseTransformBaseType;
00152   typedef typename InverseTransformBaseType::Pointer    InverseTransformBasePointer;
00153 
00160   void Translate(const OutputVectorType & offset, bool pre = 0);
00161 
00173   void Scale(const OutputVectorType & factor, bool pre = 0);
00174 
00175   void Scale(const TScalarType & factor, bool pre = 0);
00176 
00192   void Rotate(int axis1, int axis2, TScalarType angle, bool pre = 0);
00194 
00208   void Rotate2D(TScalarType angle, bool pre = 0);
00209 
00223   void Rotate3D(const OutputVectorType & axis, TScalarType angle, bool pre = 0);
00224 
00236   void Shear(int axis1, int axis2, TScalarType coef, bool pre = 0);
00237 
00239   bool GetInverse(Self *inverse) const;
00240 
00242   virtual InverseTransformBasePointer GetInverseTransform() const;
00243 
00252   inline InputPointType   BackTransform(const OutputPointType  & point) const;
00253 
00254   inline InputVectorType  BackTransform(const OutputVectorType & vector) const;
00255 
00256   inline InputVnlVectorType BackTransform(
00257     const OutputVnlVectorType & vector) const;
00258 
00259   inline InputCovariantVectorType BackTransform(
00260     const OutputCovariantVectorType & vector) const;
00261 
00271   inline InputPointType BackTransformPoint(const OutputPointType  & point) const;
00272 
00284   ScalarType Metric(const Self *other) const;
00285 
00289   ScalarType Metric(void) const;
00290 
00291 protected:
00299   AffineTransform(const MatrixType & matrix,
00300                   const OutputVectorType & offset);
00301   AffineTransform(unsigned int paramDims);
00302   AffineTransform();
00304 
00306   virtual ~AffineTransform();
00307 
00309   void PrintSelf(std::ostream & s, Indent indent) const;
00310 
00311 private:
00312 
00313   AffineTransform(const Self & other);
00314   const Self & operator=(const Self &);
00315 }; //class AffineTransform
00316 
00318 template< class TScalarType, unsigned int NDimensions >
00319 inline
00320 typename AffineTransform< TScalarType, NDimensions >::InputVectorType
00321 AffineTransform< TScalarType, NDimensions >::BackTransform(const OutputVectorType & vect) const
00322 {
00323   itkWarningMacro(
00324     << "BackTransform(): This method is slated to be removed "
00325     << "from ITK. Instead, please use GetInverse() to generate an inverse "
00326     << "transform and then perform the transform using that inverted transform.");
00327   return this->GetInverseMatrix() * vect;
00328 }
00330 
00332 template< class TScalarType, unsigned int NDimensions >
00333 inline
00334 typename AffineTransform< TScalarType, NDimensions >::InputVnlVectorType
00335 AffineTransform< TScalarType, NDimensions >::BackTransform(const OutputVnlVectorType & vect) const
00336 {
00337   itkWarningMacro(
00338     << "BackTransform(): This method is slated to be removed "
00339     << "from ITK. Instead, please use GetInverse() to generate an inverse "
00340     << "transform and then perform the transform using that inverted transform.");
00341   return this->GetInverseMatrix() * vect;
00342 }
00344 
00346 template< class TScalarType, unsigned int NDimensions >
00347 inline
00348 typename AffineTransform< TScalarType, NDimensions >::InputCovariantVectorType
00349 AffineTransform< TScalarType, NDimensions >::BackTransform(const OutputCovariantVectorType & vec) const
00350 {
00351   itkWarningMacro(
00352     << "BackTransform(): This method is slated to be removed "
00353     << "from ITK. Instead, please use GetInverse() to generate an inverse "
00354     << "transform and then perform the transform using that inverted transform.");
00355 
00356   InputCovariantVectorType result;    // Converted vector
00357 
00358   for ( unsigned int i = 0; i < NDimensions; i++ )
00359     {
00360     result[i] = NumericTraits< ScalarType >::Zero;
00361     for ( unsigned int j = 0; j < NDimensions; j++ )
00362       {
00363       result[i] += this->GetMatrix()[j][i] * vec[j]; // Direct matrix transposed
00364       }
00365     }
00366   return result;
00367 }
00368 
00370 template< class TScalarType, unsigned int NDimensions >
00371 inline
00372 typename AffineTransform< TScalarType, NDimensions >::InputPointType
00373 AffineTransform< TScalarType, NDimensions >::BackTransformPoint(const OutputPointType & point) const
00374 {
00375   return this->BackTransform(point);
00376 }
00377 
00379 template< class TScalarType, unsigned int NDimensions >
00380 inline
00381 typename AffineTransform< TScalarType, NDimensions >::InputPointType
00382 AffineTransform< TScalarType, NDimensions >::BackTransform(const OutputPointType & point) const
00383 {
00384   itkWarningMacro(
00385     << "BackTransform(): This method is slated to be removed "
00386     << "from ITK.  Instead, please use GetInverse() to generate an inverse "
00387     << "transform and then perform the transform using that inverted transform.");
00388   InputPointType result;       // Converted point
00389   ScalarType     temp[NDimensions];
00390   unsigned int   i, j;
00392 
00393   for ( j = 0; j < NDimensions; j++ )
00394     {
00395     temp[j] = point[j] - this->GetOffset()[j];
00396     }
00397 
00398   for ( i = 0; i < NDimensions; i++ )
00399     {
00400     result[i] = 0.0;
00401     for ( j = 0; j < NDimensions; j++ )
00402       {
00403       result[i] += this->GetInverseMatrix()[i][j] * temp[j];
00404       }
00405     }
00406   return result;
00407 }
00408 }  // namespace itk
00409 
00410 // Define instantiation macro for this template.
00411 #define ITK_TEMPLATE_AffineTransform(_, EXPORT, TypeX, TypeY)                \
00412   namespace itk                                                              \
00413   {                                                                          \
00414   _( 2 ( class EXPORT AffineTransform< ITK_TEMPLATE_2 TypeX > ) )            \
00415   namespace Templates                                                        \
00416   {                                                                          \
00417   typedef AffineTransform< ITK_TEMPLATE_2 TypeX >  AffineTransform##TypeY; \
00418   }                                                                          \
00419   }
00420 
00421 #if ITK_TEMPLATE_EXPLICIT
00422 #include "Templates/itkAffineTransform+-.h"
00423 #endif
00424 
00425 #if ITK_TEMPLATE_TXX
00426 #include "itkAffineTransform.hxx"
00427 #endif
00428 
00429 #endif /* __itkAffineTransform_h */
00430