KWStyle - itkBSplineDeformableTransform.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkBSplineDeformableTransform.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:33 $
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 __itkBSplineDeformableTransform_h
19 #define __itkBSplineDeformableTransform_h
20
21 #include <iostream>
22 #include "itkTransform.h"
23 #include "itkImage.h"
24 #include "itkImageRegion.h"
25 #include "itkBSplineInterpolationWeightFunction.h"
26
27 namespace itk
28 {
29
30 /** \class BSplineDeformableTransform
31  * \brief Deformable transform using a BSpline representation
32  *
33 LEN  * This class encapsulates a deformable transform of points from one N-dimensional
34  * one space to another N-dimensional space.
35  * The deformation field is modeled using B-splines. 
36  * A deformation is defined on a sparse regular grid of control points
37  * \f$ \vec{\lambda}_j \f$ and is varied by defining a deformation 
38  * \f$ \vec{g}(\vec{\lambda}_j) \f$ of each control point. 
39  * The deformation \f$ D(\vec{x}) \f$ at any point \f$ \vec{x} \f$
40  * is obtained by using a B-spline interpolation kernel.
41  *
42  * The deformation field grid is defined by a user specified GridRegion, 
43  * GridSpacing and GridOrigin. Each grid/control point has associated with it
44 LEN  * N deformation coefficients \f$ \vec{\delta}_j \f$, representing the N directional 
45  * components of the deformation. Deformation outside the grid plus support
46  * region for the BSpline interpolation is assumed to be zero.
47  *
48  * Additionally, the user can specified an addition bulk transform \f$ B \f$
49  * such that the transformed point is given by:
50  * \f[ \vec{y} = B(\vec{x}) + D(\vec{x}) \f]
51  *
52  * The parameters for this transform is N x N-D grid of spline coefficients.
53  * The user specifies the parameters as one flat array: each N-D grid
54  * is represented by an array in the same way an N-D image is represented
55  * in the buffer; the N arrays are then concatentated together on form
56  * a single array.
57  *
58  * For efficiency, this transform does not make a copy of the parameters.
59  * It only keeps a pointer to the input parameters and assumes that the memory
60  * is managed by the caller.
61  *
62  * The following illustrates the typical usage of this class:
63  * \verbatim
64  * typedef BSplineDeformableTransform<double,2,3> TransformType;
65  * TransformType::Pointer transform = TransformType::New();
66  *
67  * transform->SetGridRegion( region );
68  * transform->SetGridSpacing( spacing );
69  * transform->SetGridOrigin( origin );
70  *
71  * // NB: the region must be set first before setting the parameters
72  *
73 LEN  * TransformType::ParametersType parameters( transform->GetNumberOfParameters() );
74  *
75  * // Fill the parameters with values
76  *
77  * transform->SetParameters( parameters )
78  *
79  * outputPoint = transform->TransformPoint( inputPoint );
80  *
81  * \endverbatim
82  *
83  * An alternative way to set the B-spline coefficients is via array of
84  * images. The grid region, spacing and origin information is taken
85  * directly from the first image. It is assumed that the subsequent images
86  * are the same buffered region. The following illustrates the API:
87  * \verbatim
88  *
89  * TransformType::ImageConstPointer images[2];
90  *
91  * // Fill the images up with values
92  *
93  * transform->SetCoefficientImages( images ); 
94  * outputPoint = transform->TransformPoint( inputPoint );
95  *
96  * \endverbatim
97  *
98  * Warning: use either the SetParameters() or SetCoefficientImage()
99  * API. Mixing the two modes may results in unexpected results.
100  *
101  * The class is templated coordinate representation type (float or double),
102  * the space dimension and the spline order.
103  *
104  * \ingroup Transforms
105  */
106 template <
107 LEN     class TScalarType = double,          // Data type for scalars (float or double)
108     unsigned int NDimensions = 3,        // Number of dimensions
109     unsigned int VSplineOrder = 3 >      // Spline order
110 class ITK_EXPORT BSplineDeformableTransform : 
111 IND **********public Transform< TScalarType, NDimensions, NDimensions >
112 {
113 public:
114   /** Standard class typedefs. */
115   typedef BSplineDeformableTransform Self;
116 TDA   typedef Transform< TScalarType, NDimensions, NDimensions > Superclass;
117 TDA   typedef SmartPointer<Self>        Pointer;
118 TDA   typedef SmartPointer<const Self>  ConstPointer;
119       
120   /** New macro for creation of through the object factory.*/
121   itkNewMacro( Self );
122
123   /** Run-time type information (and related methods). */
124   itkTypeMacro( BSplineDeformableTransform, Transform );
125
126   /** Dimension of the domain space. */
127   itkStaticConstMacro(SpaceDimension, unsigned int, NDimensions);
128
129   /** The BSpline order. */
130   itkStaticConstMacro(SplineOrder, unsigned int, VSplineOrder);
131
132   /** Standard scalar type for this class. */
133   typedef typename Superclass::ScalarType ScalarType;
134
135   /** Standard parameters container. */
136   typedef typename Superclass::ParametersType ParametersType;
137
138   /** Standard Jacobian container. */
139   typedef typename Superclass::JacobianType JacobianType;
140
141   /** Standard vector type for this class. */
142   typedef Vector<TScalarType,
143                  itkGetStaticConstMacro(SpaceDimension)> InputVectorType;
144   typedef Vector<TScalarType,
145                  itkGetStaticConstMacro(SpaceDimension)> OutputVectorType;
146
147   /** Standard covariant vector type for this class. */
148   typedef CovariantVector<TScalarType,
149 LEN                           itkGetStaticConstMacro(SpaceDimension)> InputCovariantVectorType;
150   typedef CovariantVector<TScalarType,
151 LEN                           itkGetStaticConstMacro(SpaceDimension)> OutputCovariantVectorType;
152   
153   /** Standard vnl_vector type for this class. */
154   typedef vnl_vector_fixed<TScalarType,
155 LEN                            itkGetStaticConstMacro(SpaceDimension)> InputVnlVectorType;
156   typedef vnl_vector_fixed<TScalarType,
157 LEN                            itkGetStaticConstMacro(SpaceDimension)> OutputVnlVectorType;
158   
159   /** Standard coordinate point type for this class. */
160   typedef Point<TScalarType,
161                 itkGetStaticConstMacro(SpaceDimension)> InputPointType;
162   typedef Point<TScalarType,
163                 itkGetStaticConstMacro(SpaceDimension)> OutputPointType;
164   
165   /** This method sets the parameters of the transform.
166    * For a BSpline deformation transform, the parameters are the BSpline 
167    * coefficients on a sparse grid. 
168    * 
169    * The parameters are N number of N-D grid of coefficients. Each N-D grid 
170    * is represented as a flat array of doubles 
171    * (in the same configuration as an itk::Image).
172    * The N arrays are then concatenated to form one parameter array.
173    *
174    * For efficiency, this transform does not make a copy of the parameters.
175    * It only keeps a pointer to the input parameters. It assumes that the memory
176    * is managed by the caller. Use SetParametersByValue to force the transform
177    * to call copy the parameters.
178    *
179    * This method wraps each grid as itk::Image's using the user specified
180    * grid region, spacing and origin.
181    * NOTE: The grid region, spacing and origin must be set first.
182    *
183    */
184   void SetParameters(const ParametersType & parameters);
185   
186   /** This method sets the fixed parameters of the transform.
187    * For a BSpline deformation transform, the parameters are the following:
188    *    Grid Size, Grid Origin, and Grid Spacing
189    * 
190 LEN    * The fixed parameters are the three times the size of the templated dimensions.
191    * This function has the effect of make the following calls:
192    *       transform->SetGridSpacing( spacing );
193    *       transform->SetGridOrigin( origin );
194    *       transform->SetGridRegion( bsplineRegion );
195    *
196    * This function was added to allow the transform to work with the 
197    * itkTransformReader/Writer I/O filters.
198    *
199    */
200   void SetFixedParameters(const ParametersType & parameters);
201
202   /** This method sets the parameters of the transform.
203    * For a BSpline deformation transform, the parameters are the BSpline 
204    * coefficients on a sparse grid. 
205    * 
206    * The parameters are N number of N-D grid of coefficients. Each N-D grid 
207    * is represented as a flat array of doubles 
208    * (in the same configuration as an itk::Image).
209    * The N arrays are then concatenated to form one parameter array.
210    *
211    * This methods makes a copy of the parameters while for
212    * efficiency the SetParameters method does not.
213    *
214    * This method wraps each grid as itk::Image's using the user specified
215    * grid region, spacing and origin.
216    * NOTE: The grid region, spacing and origin must be set first.
217    *
218    */
219   void SetParametersByValue(const ParametersType & parameters);
220
221   /** This method can ONLY be invoked AFTER calling SetParameters(). 
222    *  This restriction is due to the fact that the BSplineDeformableTransform
223    *  does not copy the array of paramters internally, instead it keeps a 
224    *  pointer to the user-provided array of parameters. This method is also
225    *  in violation of the const-correctness of the parameters since the 
226    *  parameter array has been passed to the transform on a 'const' basis but
227    *  the values get modified when the user invokes SetIdentity().
228    */
229   void SetIdentity();
230
231   /** Get the Transformation Parameters. */
232   virtual const ParametersType& GetParameters(void) const;
233
234   /** Get the Transformation Fixed Parameters. */
235   virtual const ParametersType& GetFixedParameters(void) const;
236   
237   /** Parameters as SpaceDimension number of images. */
238   typedef typename ParametersType::ValueType PixelType;
239 TDA   typedef Image<PixelType,itkGetStaticConstMacro(SpaceDimension)> ImageType;
240 TDA   typedef typename ImageType::Pointer ImagePointer;
241
242   /** Get the array of coefficient images. */
243   virtual ImagePointer * GetCoefficientImage()
244     { return m_CoefficientImage; }
245
246   /** Set the array of coefficient images.
247    *
248    * This is an alternative API for setting the BSpline coefficients
249    * as an array of SpaceDimension images. The grid region spacing 
250    * and origin is taken from the first image. It is assume that
251    * the buffered region of all the subsequent images are the same 
252    * as the first image. Note that no error checking is done.
253    *
254    * Warning: use either the SetParameters() or SetCoefficientImage()
255    * API. Mixing the two modes may results in unexpected results.
256    *
257    */
258   virtual void SetCoefficientImage( ImagePointer images[] );  
259
260   /** Typedefs for specifying the extend to the grid. */
261   typedef ImageRegion<itkGetStaticConstMacro(SpaceDimension)>    RegionType;
262 TDA   typedef typename RegionType::IndexType IndexType;
263 TDA   typedef typename RegionType::SizeType  SizeType;
264 TDA   typedef typename ImageType::SpacingType SpacingType;
265 TDA   typedef typename ImageType::PointType OriginType;
266
267   /** This method specifies the region over which the grid resides. */
268   virtual void SetGridRegion( const RegionType& region );
269   itkGetMacro( GridRegion, RegionType );
270   itkGetConstMacro( GridRegion, RegionType );
271
272   /** This method specifies the grid spacing or resolution. */
273   virtual void SetGridSpacing( const SpacingType& spacing );
274   itkGetMacro( GridSpacing, SpacingType );
275   itkGetConstMacro( GridSpacing, SpacingType );
276
277   /** This method specifies the grid origin. */
278   virtual void SetGridOrigin( const OriginType& origin );
279   itkGetMacro( GridOrigin, OriginType );
280   itkGetConstMacro( GridOrigin, OriginType );
281
282   /** Typedef of the bulk transform. */
283   typedef Transform<ScalarType,itkGetStaticConstMacro(SpaceDimension),
284                     itkGetStaticConstMacro(SpaceDimension)> BulkTransformType;
285 TDA   typedef typename BulkTransformType::ConstPointer  BulkTransformPointer;
286
287   /** This method specifies the bulk transform to be applied. 
288    * The default is the identity transform.
289    */
290   itkSetConstObjectMacro( BulkTransform, BulkTransformType );
291   itkGetConstObjectMacro( BulkTransform, BulkTransformType );
292
293   /** Transform points by a BSpline deformable transformation. */
294   OutputPointType  TransformPoint(const InputPointType  &point ) const;
295
296   /** Interpolation weights function type. */
297   typedef BSplineInterpolationWeightFunction<ScalarType,
298 LEN                                              itkGetStaticConstMacro(SpaceDimension),
299 LEN                                              itkGetStaticConstMacro(SplineOrder)> WeightsFunctionType;
300 TDA   typedef typename WeightsFunctionType::WeightsType WeightsType;
301 TDA   typedef typename WeightsFunctionType::ContinuousIndexType ContinuousIndexType;
302
303   /** Parameter index array type. */
304   typedef Array<unsigned long> ParameterIndexArrayType;
305
306   /** Transform points by a BSpline deformable transformation. 
307    * On return, weights contains the interpolation weights used to compute the 
308    * deformation and indices of the x (zeroth) dimension coefficient parameters
309    * in the support region used to compute the deformation.
310    * Parameter indices for the i-th dimension can be obtained by adding
311    * ( i * this->GetNumberOfParametersPerDimension() ) to the indices array.
312    */
313   virtual void TransformPoint( const InputPointType & inputPoint,
314                        OutputPointType & outputPoint,
315                        WeightsType & weights,
316                        ParameterIndexArrayType & indices, 
317                        bool & inside ) const;
318
319   /** Get number of weights. */
320   unsigned long GetNumberOfWeights() const
321     { return m_WeightsFunction->GetNumberOfWeights(); }
322
323 LEN   /**  Method to transform a vector - not applicable for this type of transform. */
324   virtual OutputVectorType TransformVector(const InputVectorType &) const
325     { 
326 IND ******itkExceptionMacro(<< "Method not applicable for deformable transform." );
327       return OutputVectorType(); 
328     }
329
330 LEN   /**  Method to transform a vnl_vector - not applicable for this type of transform */
331   virtual OutputVnlVectorType TransformVector(const InputVnlVectorType &) const
332     { 
333       itkExceptionMacro(<< "Method not applicable for deformable transform. ");
334       return OutputVnlVectorType(); 
335     }
336
337 LEN   /**  Method to transform a CovariantVector - not applicable for this type of transform */
338   virtual OutputCovariantVectorType TransformCovariantVector(
339     const InputCovariantVectorType &) const
340     { 
341       itkExceptionMacro(<< "Method not applicable for deformable transfrom. ");
342       return OutputCovariantVectorType(); 
343     } 
344     
345   /** Compute the Jacobian Matrix of the transformation at one point */
346   virtual const JacobianType& GetJacobian(const InputPointType  &point ) const;
347
348   /** Return the number of parameters that completely define the Transfom */
349   virtual unsigned int GetNumberOfParameters(void) const;
350
351   /** Return the number of parameters per dimension */
352   unsigned int GetNumberOfParametersPerDimension(void) const;
353
354   /** Return the region of the grid wholly within the support region */
355   itkGetConstReferenceMacro( ValidRegion, RegionType );
356
357 protected:
358   /** Print contents of an BSplineDeformableTransform. */
359   void PrintSelf(std::ostream &os, Indent indent) const;
360
361
362   BSplineDeformableTransform();
363   virtual ~BSplineDeformableTransform();
364
365   /** Allow subclasses to access and manipulate the weights function. */
366   itkSetObjectMacro( WeightsFunction, WeightsFunctionType );
367   itkGetObjectMacro( WeightsFunction, WeightsFunctionType );
368
369   /** Wrap flat array into images of coefficients. */
370   void WrapAsImages();
371
372 private:
373   BSplineDeformableTransform(const Self&); //purposely not implemented
374   void operator=(const Self&); //purposely not implemented
375
376   /** The bulk transform. */
377   BulkTransformPointer  m_BulkTransform;
378
379   /** Variables defining the coefficient grid extend. */
380   RegionType    m_GridRegion;
381   SpacingType   m_GridSpacing;
382   OriginType    m_GridOrigin;
383   
384   RegionType    m_ValidRegion;
385
386   /** Variables defining the interpolation support region. */
387   unsigned long m_Offset;
388   bool          m_SplineOrderOdd;
389   SizeType      m_SupportSize;
390   IndexType     m_ValidRegionLast;
391   
392   /** Array holding images wrapped from the flat parameters. */
393   ImagePointer   m_WrappedImage[NDimensions];
394
395 LEN   /** Array of images representing the B-spline coefficients in each dimension. */
396   ImagePointer   m_CoefficientImage[NDimensions];
397
398   /** Jacobian as SpaceDimension number of images. */
399   typedef typename JacobianType::ValueType JacobianPixelType;
400   typedef Image<JacobianPixelType,
401 TDA                 itkGetStaticConstMacro(SpaceDimension)> JacobianImageType;
402  
403   typename JacobianImageType::Pointer m_JacobianImage[NDimensions];
404
405   /** Keep track of last support region used in computing the Jacobian
406    * for fast resetting of Jacobian to zero.
407    */
408   mutable IndexType m_LastJacobianIndex;
409
410   /** Keep a pointer to the input parameters. */
411   const ParametersType *  m_InputParametersPointer;
412
413   /** Internal parameters buffer. */
414   ParametersType          m_InternalParametersBuffer;
415
416   /** Pointer to function used to compute Bspline interpolation weights. */
417   typename WeightsFunctionType::Pointer  m_WeightsFunction;
418
419   /** Check if a continuous index is inside the valid region. */
420   bool InsideValidRegion( const ContinuousIndexType& index ) const;
421
422
423 }; //class BSplineDeformableTransform
424
425
426 }  // namespace itk
427
428
429 #ifndef ITK_MANUAL_INSTANTIATION
430 #include "itkBSplineDeformableTransform.txx"
431 #endif
432
433 #endif /* __itkBSplineDeformableTransform_h */
434

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:00PM
© Kitware Inc.