ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkBSplineBaseTransform.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 __itkBSplineBaseTransform_h
00019 #define __itkBSplineBaseTransform_h
00020 
00021 #include <iostream>
00022 #include "itkTransform.h"
00023 #include "itkImage.h"
00024 #include "itkBSplineInterpolationWeightFunction.h"
00025 
00026 namespace itk
00027 {
00033 template <class TScalarType = double, unsigned int NDimensions = 3,
00034           unsigned int VSplineOrder = 3>
00035 class ITK_EXPORT BSplineBaseTransform :
00036   public Transform<TScalarType, NDimensions, NDimensions>
00037 {
00038 public:
00040   typedef BSplineBaseTransform                             Self;
00041   typedef Transform<TScalarType, NDimensions, NDimensions> Superclass;
00042   typedef SmartPointer<Self>                               Pointer;
00043   typedef SmartPointer<const Self>                         ConstPointer;
00044 
00046   itkTypeMacro( BSplineBaseTransform, Transform );
00047 
00049   itkStaticConstMacro( SpaceDimension, unsigned int, NDimensions );
00050 
00052   itkStaticConstMacro( SplineOrder, unsigned int, VSplineOrder );
00053 
00055   typedef typename Superclass::ScalarType ScalarType;
00056 
00058   typedef typename Superclass::ParametersType ParametersType;
00059 
00061   typedef typename Superclass::JacobianType JacobianType;
00062 
00064   typedef typename Superclass::NumberOfParametersType NumberOfParametersType;
00065 
00067   typedef Vector<TScalarType, itkGetStaticConstMacro( SpaceDimension )> InputVectorType;
00068   typedef Vector<TScalarType, itkGetStaticConstMacro( SpaceDimension )> OutputVectorType;
00070 
00072   typedef CovariantVector<TScalarType, itkGetStaticConstMacro( SpaceDimension )> InputCovariantVectorType;
00073   typedef CovariantVector<TScalarType, itkGetStaticConstMacro( SpaceDimension )> OutputCovariantVectorType;
00075 
00077   typedef vnl_vector_fixed<TScalarType, SpaceDimension> InputVnlVectorType;
00078   typedef vnl_vector_fixed<TScalarType, SpaceDimension> OutputVnlVectorType;
00079 
00081   typedef Point <TScalarType, itkGetStaticConstMacro( SpaceDimension )> InputPointType;
00082   typedef Point <TScalarType, itkGetStaticConstMacro( SpaceDimension )> OutputPointType;
00084 
00104   void SetParameters( const ParametersType & parameters );
00105 
00128   virtual void SetFixedParameters( const ParametersType & parameters ) = 0;
00130 
00147   void SetParametersByValue( const ParametersType & parameters );
00148 
00157   void SetIdentity();
00158 
00160   virtual const ParametersType & GetParameters() const;
00161 
00163   virtual const ParametersType & GetFixedParameters() const;
00164 
00166   typedef typename ParametersType::ValueType           ParametersValueType;
00167   typedef Image<ParametersValueType, itkGetStaticConstMacro( SpaceDimension )> ImageType;
00168   typedef typename ImageType::Pointer                  ImagePointer;
00169   typedef FixedArray<ImagePointer, NDimensions>        CoefficientImageArray;
00170 
00182   virtual void SetCoefficientImages( const CoefficientImageArray & images ) = 0;
00183 
00185   const CoefficientImageArray GetCoefficientImages() const
00186   {
00187     return this->m_CoefficientImages;
00188   }
00189 
00190   typedef typename Superclass::DerivativeType DerivativeType;
00191 
00202   virtual void UpdateTransformParameters( DerivativeType & update, TScalarType factor = 1.0 );
00203 
00205   typedef ImageRegion<itkGetStaticConstMacro( SpaceDimension )> RegionType;
00206 
00207   typedef typename RegionType::IndexType    IndexType;
00208   typedef typename RegionType::SizeType     SizeType;
00209   typedef typename ImageType::SpacingType   SpacingType;
00210   typedef typename ImageType::DirectionType DirectionType;
00211   typedef typename ImageType::PointType     OriginType;
00212 
00214   OutputPointType  TransformPoint( const InputPointType & point ) const;
00215 
00217   typedef BSplineInterpolationWeightFunction<ScalarType,
00218     itkGetStaticConstMacro( SpaceDimension ),
00219      itkGetStaticConstMacro( SplineOrder )> WeightsFunctionType;
00220 
00221   typedef typename WeightsFunctionType::WeightsType         WeightsType;
00222   typedef typename WeightsFunctionType::ContinuousIndexType ContinuousIndexType;
00223 
00225   typedef Array<unsigned long> ParameterIndexArrayType;
00226 
00235   virtual void TransformPoint( const InputPointType & inputPoint, OutputPointType & outputPoint,
00236     WeightsType & weights, ParameterIndexArrayType & indices, bool & inside ) const = 0;
00237 
00239   unsigned long GetNumberOfWeights() const
00240   {
00241     return m_WeightsFunction->GetNumberOfWeights();
00242   }
00243 
00246   using Superclass::TransformVector;
00247   virtual OutputVectorType TransformVector( const InputVectorType & ) const
00248   {
00249     itkExceptionMacro( << "Method not applicable for deformable transform." );
00250     return OutputVectorType();
00251   }
00253 
00256   virtual OutputVnlVectorType TransformVector( const InputVnlVectorType & ) const
00257   {
00258     itkExceptionMacro( << "Method not applicable for deformable transform. " );
00259     return OutputVnlVectorType();
00260   }
00262 
00265   using Superclass::TransformCovariantVector;
00266   virtual OutputCovariantVectorType TransformCovariantVector(
00267     const InputCovariantVectorType & ) const
00268   {
00269     itkExceptionMacro( << "Method not applicable for deformable transfrom. " );
00270     return OutputCovariantVectorType();
00271   }
00273 
00275   void ComputeJacobianFromBSplineWeightsWithRespectToPosition(
00276     const InputPointType &, WeightsType &, ParameterIndexArrayType & ) const;
00277 
00278   virtual void ComputeJacobianWithRespectToParameters( const InputPointType &, JacobianType & ) const = 0;
00279 
00280   virtual void ComputeJacobianWithRespectToPosition( const InputPointType &, JacobianType & ) const
00281   {
00282     itkExceptionMacro( << "ComputeJacobianWithRespectToPosition not yet implemented "
00283                        "for " << this->GetNameOfClass() );
00284   }
00285 
00287   virtual NumberOfParametersType GetNumberOfParameters() const = 0;
00288 
00290   virtual NumberOfParametersType GetNumberOfParametersPerDimension() const = 0;
00291 
00297   virtual bool IsLinear() const
00298   {
00299     return false;
00300   }
00301 
00302   unsigned int GetNumberOfAffectedWeights() const;
00303 
00304   typedef typename ImageType::SpacingType   PhysicalDimensionsType;
00305   typedef typename ImageType::PixelType     PixelType;
00306 
00307   typedef SizeType MeshSizeType;
00308 
00309 
00310 protected:
00312   void PrintSelf( std::ostream & os, Indent indent ) const;
00313 
00314   BSplineBaseTransform();
00315   virtual ~BSplineBaseTransform();
00316 
00318   itkSetObjectMacro( WeightsFunction, WeightsFunctionType );
00319 
00321   itkGetObjectMacro( WeightsFunction, WeightsFunctionType );
00322 
00324   void WrapAsImages();
00325 
00326 protected:
00328   void SetFixedParametersFromTransformDomainInformation() const;
00329 
00331   virtual void SetFixedParametersGridSizeFromTransformDomainInformation() const = 0;
00332 
00334   virtual void SetFixedParametersGridOriginFromTransformDomainInformation() const = 0;
00335 
00337   virtual void SetFixedParametersGridSpacingFromTransformDomainInformation() const = 0;
00338 
00340   virtual void SetFixedParametersGridDirectionFromTransformDomainInformation() const = 0;
00341 
00343   virtual void SetCoefficientImageInformationFromFixedParameters() =0;
00344 
00346   virtual bool InsideValidRegion( ContinuousIndexType & ) const = 0;
00347 
00348   // NOTE:  There is a natural duality between the
00349   //       two representations of of the coefficients
00350   //       whereby the m_InternalParametersBuffer is
00351   //       needed to fit into the optimization framework
00352   //       and the m_CoefficientImages is needed for
00353   //       the Jacobian computations.  This implementation
00354   //       is an attempt to remove as much redundancy as possible
00355   //       and share as much information between the two
00356   //       instances as possible.
00357   //
00362   CoefficientImageArray m_CoefficientImages;
00363 
00365   const ParametersType *m_InputParametersPointer;
00366 
00368   ParametersType m_InternalParametersBuffer;
00369 
00370 
00372   typename WeightsFunctionType::Pointer m_WeightsFunction;
00373 
00374 private:
00375   BSplineBaseTransform( const Self & ); // purposely not implemented
00376   void operator=( const Self & );   // purposely not implemented
00377 
00378   CoefficientImageArray ArrayOfImagePointerGeneratorHelper() const;
00379 }; // class BSplineBaseTransform
00380 }  // namespace itk
00381 
00382 // Define instantiation macro for this template.
00383 #define ITK_TEMPLATE_BSplineBaseTransform(_, EXPORT, TypeX, TypeY)     \
00384   namespace itk                                                              \
00385   {                                                                          \
00386   _( 3 ( class EXPORT BSplineBaseTransform<ITK_TEMPLATE_3 TypeX> ) ) \
00387   namespace Templates                                                        \
00388   {                                                                          \
00389   typedef BSplineBaseTransform<ITK_TEMPLATE_3 TypeX>                 \
00390   BSplineBaseTransform##TypeY;                                       \
00391   }                                                                          \
00392   }
00393 
00394 #if ITK_TEMPLATE_EXPLICIT
00395 // template < class TScalarType, unsigned int NDimensions, unsigned int
00396 // VSplineOrder >
00397 //   const unsigned int itk::BSplineBaseTransform<TScalarType,
00398 // NDimensions, VSplineOrder >::SpaceDimension;
00399 // template < class TScalarType, unsigned int NDimensions, unsigned int
00400 // VSplineOrder >
00401 //   const unsigned int itk::BSplineBaseTransform<TScalarType,
00402 // NDimensions, VSplineOrder >::SplineOrder;
00403 #include "Templates/itkBSplineBaseTransform+-.h"
00404 #endif
00405 
00406 #if ITK_TEMPLATE_TXX
00407 #include "itkBSplineBaseTransform.hxx"
00408 #endif
00409 
00410 #endif /* __itkBSplineBaseTransform_h */
00411