]> Creatis software - clitk.git/blob - itk/clitkVectorBSplineInterpolateImageFunction.h
Initial revision
[clitk.git] / itk / clitkVectorBSplineInterpolateImageFunction.h
1 #ifndef __clitkVectorBSplineInterpolateImageFunction_h
2 #define __clitkVectorBSplineInterpolateImageFunction_h
3
4 #include "clitkVectorBSplineDecompositionImageFilter.h"
5
6 // First make sure that the configuration is available.
7 // This line can be removed once the optimized versions
8 // gets integrated into the main directories.
9 #include "itkConfigure.h"
10
11 // #ifdef ITK_USE_OPTIMIZED_REGISTRATION_METHODS
12 // #include "itkOptBSplineInterpolateImageFunction.h"
13 // #else
14
15 #include <vector>
16
17 #include "itkImageLinearIteratorWithIndex.h"
18 #include "itkVectorInterpolateImageFunction.h"
19 #include "vnl/vnl_matrix.h"
20
21 #include "itkBSplineDecompositionImageFilter.h"
22 #include "itkConceptChecking.h"
23 #include "itkCovariantVector.h"
24
25
26 namespace clitk
27 {
28
29 template <
30   class TImageType, 
31   class TCoordRep = double,
32   class TCoefficientType = double >
33 class ITK_EXPORT VectorBSplineInterpolateImageFunction : 
34     public itk::VectorInterpolateImageFunction<TImageType,TCoordRep> 
35 {
36 public:
37   /** Standard class typedefs. */
38   typedef VectorBSplineInterpolateImageFunction       Self;
39   typedef itk::VectorInterpolateImageFunction<TImageType,TCoordRep>  Superclass;
40   typedef itk::SmartPointer<Self>                    Pointer;
41   typedef itk::SmartPointer<const Self>              ConstPointer;
42
43   /** Run-time type information (and related methods). */
44   itkTypeMacro(VectorBSplineInterpolateImageFunction, InterpolateImageFunction);
45
46
47   /** New macro for creation of through a Smart Pointer */
48   itkNewMacro( Self );
49
50   /** OutputType typedef support. */
51   typedef typename Superclass::OutputType OutputType;
52
53   /** InputImageType typedef support. */
54   typedef typename Superclass::InputImageType InputImageType;
55
56   /** Dimension underlying input image. */
57   itkStaticConstMacro(ImageDimension, unsigned int,Superclass::ImageDimension);
58
59   /** Index typedef support. */
60   typedef typename Superclass::IndexType IndexType;
61
62   /** ContinuousIndex typedef support. */
63   typedef typename Superclass::ContinuousIndexType ContinuousIndexType;
64
65   /** PointType typedef support */
66   typedef typename Superclass::PointType PointType;
67
68   //JV the vector dimension
69   itkStaticConstMacro(VectorDimension, unsigned int,Superclass::Dimension);
70
71
72   /** Iterator typedef support */
73   typedef itk::ImageLinearIteratorWithIndex<TImageType> Iterator;
74
75   /** Internal Coefficient typedef support */
76   typedef TCoefficientType CoefficientDataType;
77
78   //JV
79   typedef itk::Vector<CoefficientDataType, itkGetStaticConstMacro(VectorDimension)> CoefficientImagePixelType;
80   typedef  itk::Image<CoefficientImagePixelType , itkGetStaticConstMacro(ImageDimension) > CoefficientImageType;
81
82   /** Define filter for calculating the BSpline coefficients */
83   //JV make vectorial
84   typedef clitk::VectorBSplineDecompositionImageFilter<TImageType, CoefficientImageType> 
85   CoefficientFilter;
86   typedef typename CoefficientFilter::Pointer CoefficientFilterPointer;
87
88   /** Evaluate the function at a ContinuousIndex position.
89    *
90    * Returns the B-Spline interpolated image intensity at a 
91    * specified point position. No bounds checking is done.
92    * The point is assume to lie within the image buffer.
93    *
94    * ImageFunction::IsInsideBuffer() can be used to check bounds before
95    * calling the method. */
96   virtual OutputType EvaluateAtContinuousIndex( 
97     const ContinuousIndexType & index ) const; 
98
99   /** Derivative typedef support */
100   typedef itk::CovariantVector<OutputType,
101                           itkGetStaticConstMacro(ImageDimension)
102     > CovariantVectorType;
103
104   CovariantVectorType EvaluateDerivative( const PointType & point ) const
105   {    
106     ContinuousIndexType index;
107     this->GetInputImage()->TransformPhysicalPointToContinuousIndex( point, index );
108     return ( this->EvaluateDerivativeAtContinuousIndex( index ) );
109   } 
110
111   CovariantVectorType EvaluateDerivativeAtContinuousIndex( 
112     const ContinuousIndexType & x ) const;
113
114
115   /** Get/Sets the Spline Order, supports 0th - 5th order splines. The default
116    *  is a 3rd order spline. */
117   void SetSplineOrder(unsigned int SplineOrder);
118   itkGetMacro(SplineOrder, int);
119
120
121   /** Set the input image.  This must be set by the user. */
122   virtual void SetInputImage(const TImageType * inputData);
123
124
125   /** The UseImageDirection flag determines whether image derivatives are
126    * computed with respect to the image grid or with respect to the physical
127    * space. When this flag is ON the derivatives are computed with respect to
128    * the coodinate system of physical space. The difference is whether we take
129    * into account the image Direction or not. The flag ON will take into
130    * account the image direction and will result in an extra matrix
131    * multiplication compared to the amount of computation performed when the
132    * flag is OFF.  This flag is OFF by default.*/
133   itkSetMacro( UseImageDirection, bool );
134   itkGetMacro( UseImageDirection, bool );
135   itkBooleanMacro( UseImageDirection );
136
137
138 protected:
139   VectorBSplineInterpolateImageFunction();
140   virtual ~VectorBSplineInterpolateImageFunction() {};
141   void operator=( const Self& ); //purposely not implemented
142   void PrintSelf(std::ostream& os, itk::Indent indent) const;
143
144   // These are needed by the smoothing spline routine.
145   std::vector<CoefficientDataType>    m_Scratch;        // temp storage for processing of Coefficients
146   typename TImageType::SizeType       m_DataLength;  // Image size
147   unsigned int                        m_SplineOrder; // User specified spline order (3rd or cubic is the default)
148
149   typename CoefficientImageType::ConstPointer       m_Coefficients; // Spline coefficients  
150
151 private:
152   VectorBSplineInterpolateImageFunction( const Self& ); //purposely not implemented
153   /** Determines the weights for interpolation of the value x */
154   void SetInterpolationWeights( const ContinuousIndexType & x, 
155                                 const vnl_matrix<long> & EvaluateIndex, 
156                                 vnl_matrix<double> & weights, 
157                                 unsigned int splineOrder ) const;
158
159   /** Determines the weights for the derivative portion of the value x */
160   void SetDerivativeWeights( const ContinuousIndexType & x, 
161                              const vnl_matrix<long> & EvaluateIndex, 
162                              vnl_matrix<double> & weights, 
163                              unsigned int splineOrder ) const;
164
165   /** Precomputation for converting the 1D index of the interpolation neighborhood 
166     * to an N-dimensional index. */
167   void GeneratePointsToIndex(  );
168
169   /** Determines the indicies to use give the splines region of support */
170   void DetermineRegionOfSupport( vnl_matrix<long> & evaluateIndex, 
171                                  const ContinuousIndexType & x, 
172                                  unsigned int splineOrder ) const;
173
174   /** Set the indicies in evaluateIndex at the boundaries based on mirror 
175     * boundary conditions. */
176   void ApplyMirrorBoundaryConditions(vnl_matrix<long> & evaluateIndex, 
177                                      unsigned int splineOrder) const;
178
179
180   Iterator                  m_CIterator;    // Iterator for traversing spline coefficients.
181   unsigned long             m_MaxNumberInterpolationPoints; // number of neighborhood points used for interpolation
182   std::vector<IndexType>    m_PointsToIndex;  // Preallocation of interpolation neighborhood indicies
183
184   CoefficientFilterPointer     m_CoefficientFilter;
185   
186   // flag to take or not the image direction into account when computing the
187   // derivatives.
188   bool m_UseImageDirection;
189
190
191 };
192
193 } // namespace itk
194
195 #ifndef ITK_MANUAL_INSTANTIATION
196 #include "clitkVectorBSplineInterpolateImageFunction.txx"
197 #endif
198
199 #endif
200
201 //#endif