]> Creatis software - clitk.git/blob - registration/clitkBSplineDeformableTransform.h
Debug RTStruct conversion with empty struc
[clitk.git] / registration / clitkBSplineDeformableTransform.h
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
5   - University of LYON              http://www.universite-lyon.fr/
6   - Léon Bérard cancer center       http://www.centreleonberard.fr
7   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
8
9   This software is distributed WITHOUT ANY WARRANTY; without even
10   the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   PURPOSE.  See the copyright notices for more information.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef __clitkBSplineDeformableTransform_h
19 #define __clitkBSplineDeformableTransform_h
20 #include "clitkVectorBSplineResampleImageFunctionWithLUT.h"
21
22 //itk include
23 #include "itkTransform.h"
24 #include "itkImage.h"
25 #include "itkImageRegion.h"
26 #include "itkSpatialObject.h"
27
28 namespace clitk
29 {
30   // Forward declaration needed for friendship
31   template <class TCoordRep, unsigned int NInputDimensions, unsigned int NOutputDimensions>
32   class ITK_EXPORT MultipleBSplineDeformableTransform;
33
34   template <
35     class TCoordRep = double,               // Data type for scalars, coordinate representation,vectors
36     unsigned int NInputDimensions = 3,        // Number of input dimensions
37     unsigned int NOutputDimensions = 3 >      // Number of output dimensions
38   class ITK_EXPORT BSplineDeformableTransform : 
39     public itk::Transform< TCoordRep, NInputDimensions, NOutputDimensions >
40   {
41   public:
42
43     //====================================================================
44     // Typedefs
45     //====================================================================
46     typedef BSplineDeformableTransform                         Self;
47     typedef itk::Transform< TCoordRep, NInputDimensions, NOutputDimensions > Superclass;
48     typedef itk::SmartPointer<Self>                                 Pointer;
49     typedef itk::SmartPointer<const Self>                           ConstPointer;
50       
51     /** New macro for creation of through the object factory.*/
52     itkNewMacro( Self );
53
54     /** Run-time type information (and related methods). */
55     itkTypeMacro( BSplineDeformableTransform, Transform );
56
57     /** Dimension of the domain space. */
58     itkStaticConstMacro(OutputDimension, unsigned int, NOutputDimensions);
59
60     /** Dimension of the input model. */
61     itkStaticConstMacro(InputDimension, unsigned int, NInputDimensions);
62
63     /** Standard scalar type for this class. */
64     typedef typename Superclass::ScalarType ScalarType;
65
66     /** Standard parameters container. */
67     typedef typename Superclass::ParametersType ParametersType;
68     typedef typename Superclass::NumberOfParametersType NumberOfParametersType;
69
70     /** Standard Jacobian container. */
71     typedef typename Superclass::JacobianType JacobianType;
72
73     /** Standard vector type for this class. */
74     typedef itk::Vector<TCoordRep,
75                         InputDimension> InputVectorType;
76     typedef itk::Vector<TCoordRep,
77                         OutputDimension> OutputVectorType;
78   
79     /** Standard covariant vector type for this class. */
80     typedef itk::CovariantVector<TCoordRep,
81                                  InputDimension> InputCovariantVectorType;
82     typedef itk::CovariantVector<TCoordRep,
83                                  OutputDimension> OutputCovariantVectorType;
84
85     /** Standard vnl_vector type for this class. */
86     typedef vnl_vector_fixed<TCoordRep,
87                              InputDimension> InputVnlVectorType;
88     typedef vnl_vector_fixed<TCoordRep,
89                              OutputDimension> OutputVnlVectorType;
90
91     /** Standard coordinate point type for this class. */
92     typedef itk::Point<TCoordRep,
93                        InputDimension> InputPointType;
94     typedef itk::Point<TCoordRep,
95                        OutputDimension> OutputPointType;
96
97     //JV Parameters as images with OutputDimension number of components per Pixel
98     typedef typename ParametersType::ValueType                          ParametersValueType;
99     typedef typename itk::Vector<ParametersValueType, OutputDimension>  PixelType;
100     typedef itk::Image<PixelType, InputDimension>                       CoefficientImageType;
101     typedef typename CoefficientImageType::Pointer                      CoefficientImagePointer;
102
103   
104     /** Typedefs for specifying the extend to the grid. */
105     typedef itk::ImageRegion<InputDimension>                RegionType;
106     typedef typename RegionType::IndexType                  IndexType;
107     typedef typename RegionType::SizeType                   SizeType;
108     typedef typename CoefficientImageType::SpacingType      SpacingType;
109     typedef typename CoefficientImageType::DirectionType    DirectionType;
110     typedef typename CoefficientImageType::PointType        OriginType;
111     typedef itk::ContinuousIndex<TCoordRep, InputDimension> ContinuousIndexType;
112   
113     //JV added for the BLUT interpolator
114     //typedef itk::Vector<TCoordRep, InputDimension> OutputSpacingType;
115
116     //JV m_VectorInterpolator
117     typedef VectorBSplineResampleImageFunctionWithLUT
118     <CoefficientImageType, TCoordRep> VectorInterpolatorType;
119     typedef  typename  VectorInterpolatorType::CoefficientDataType CoefficientDataType;
120     typedef  typename  VectorInterpolatorType::CoefficientDataType WeightsDataType;
121
122     /** Typedef of the bulk transform. */
123     typedef itk::Transform<ScalarType, InputDimension, OutputDimension> BulkTransformType;
124     typedef BulkTransformType*                  BulkTransformPointer;
125
126     /** Typedef of the mask */
127     typedef itk::SpatialObject<  InputDimension >   MaskType;
128     typedef MaskType*    MaskPointer;
129
130     //====================================================================
131     // Set et Gets
132     //====================================================================
133     //JV  added for the BLUT interpolator
134     void SetSplineOrder(const unsigned int &  splineOrder);
135     void SetSplineOrders(const SizeType &  splineOrders);
136     itkGetMacro( SplineOrders, SizeType );
137     itkGetConstMacro( SplineOrders, SizeType );
138     void SetLUTSamplingFactor(const int &  samplingFactor);
139     void SetLUTSamplingFactors(const SizeType &  samplingFactors);
140     itkGetMacro( LUTSamplingFactors, SizeType );
141     itkGetConstMacro( LUTSamplingFactors,SizeType );
142     //void SetOutputSpacing(const OutputSpacingType & outputSpacing);
143     //itkGetMacro( OutputSpacing, OutputSpacingType );
144     //itkGetConstMacro( OutputSpacing, OutputSpacingType );
145
146     void SetParameters(const ParametersType & parameters);
147   
148     void SetFixedParameters(const ParametersType & parameters);
149
150     void SetParametersByValue(const ParametersType & parameters);
151
152     void SetIdentity();
153
154     /** Get the Transformation Parameters. */
155     virtual const ParametersType& GetParameters(void) const;
156
157     /** Get the Transformation Fixed Parameters. */
158     virtual const ParametersType& GetFixedParameters(void) const;
159   
160     // The coefficientImage
161     virtual CoefficientImagePointer GetCoefficientImage()
162     { return m_CoefficientImage; }
163     virtual const CoefficientImagePointer  GetCoefficientImage() const
164     { return m_CoefficientImage; }
165     virtual void SetCoefficientImage(CoefficientImagePointer image);  
166
167     /** This method specifies the region over which the grid resides. */
168     virtual void SetGridRegion( const RegionType& region );
169     itkGetMacro( GridRegion, RegionType );
170     itkGetConstMacro( GridRegion, RegionType );
171
172     /** This method specifies the grid spacing or resolution. */
173     virtual void SetGridSpacing( const SpacingType& spacing );
174     itkGetMacro( GridSpacing, SpacingType );
175     itkGetConstMacro( GridSpacing, SpacingType );
176
177     /** This method specifies the grid directions . */
178     virtual void SetGridDirection( const DirectionType & spacing );
179     itkGetMacro( GridDirection, DirectionType );
180     itkGetConstMacro( GridDirection, DirectionType );
181
182     /** This method specifies the grid origin. */
183     virtual void SetGridOrigin( const OriginType& origin );
184     itkGetMacro( GridOrigin, OriginType );
185     itkGetConstMacro( GridOrigin, OriginType );
186     
187     // Set the bulk transform, real pointer
188     // itkSetConstObjectMacro( BulkTransform, BulkTransformType );
189     // itkGetConstObjectMacro( BulkTransform, BulkTransformType );
190     void SetBulkTransform(BulkTransformPointer b){m_BulkTransform=b;}
191     BulkTransformPointer GetBulkTransform(void) {return m_BulkTransform;}
192
193     // Set mask, inside transform applies, outside zero, real pointer
194     void SetMask(MaskPointer m){m_Mask=m;}
195     MaskPointer GetMask(void){return m_Mask;}
196     // itkSetConstObjectMacro( Mask, MaskType );
197     // itkGetConstObjectMacro( Mask, MaskType );
198
199     /** Transform points by a BSpline deformable transformation. */
200     OutputPointType  TransformPoint(const InputPointType  &point ) const;
201   
202     // JV added for just the deformable part, without bulk
203     OutputPointType  DeformablyTransformPoint(const InputPointType  &point ) const;
204   
205     /** Parameter index array type. */
206     typedef itk::Array<unsigned long> ParameterIndexArrayType;
207
208     /** Transform points by a BSpline deformable transformation. 
209      * On return, weights contains the interpolation weights used to compute the 
210      * deformation and indices of the x (zeroth) dimension coefficient parameters
211      * in the support region used to compute the deformation.
212      * Parameter indices for the i-th dimension can be obtained by adding
213      * ( i * this->GetNumberOfParametersPerDimension() ) to the indices array.
214      */
215
216     // JV not implemented
217     //  virtual void TransformPoint( const InputPointType & inputPoint,
218     //                           OutputPointType & outputPoint,
219     //                           WeightsType & weights,
220     //                           ParameterIndexArrayType & indices, 
221     //                           bool & inside ) const;
222     //     virtual void DeformablyTransformPoint( const InputPointType & inputPoint,
223     //                                     OutputPointType & outputPoint,
224     //                                     WeightsType & weights,
225     //                                     ParameterIndexArrayType & indices, 
226     //                                     bool & inside ) const;
227     //     virtual void GetJacobian( const InputPointType & inputPoint,
228     //                                WeightsType & weights,
229     //                                ParameterIndexArrayType & indices
230     //                                ) const;
231    
232     /** Method to transform a vector - 
233      *  not applicable for this type of transform. */
234     virtual OutputVectorType TransformVector(const InputVectorType &) const
235     { 
236       itkExceptionMacro(<< "Method not applicable for deformable transform." );
237       return OutputVectorType(); 
238     }
239
240     /** Method to transform a vnl_vector - 
241      *  not applicable for this type of transform */
242     virtual OutputVnlVectorType TransformVector(const InputVnlVectorType &) const
243     { 
244       itkExceptionMacro(<< "Method not applicable for deformable transform. ");
245       return OutputVnlVectorType(); 
246     }
247
248     /** Method to transform a CovariantVector - 
249      *  not applicable for this type of transform */
250     virtual OutputCovariantVectorType TransformCovariantVector(
251                                                                const InputCovariantVectorType &) const
252     { 
253       itkExceptionMacro(<< "Method not applicable for deformable transfrom. ");
254       return OutputCovariantVectorType(); 
255     } 
256     
257     /** Compute the Jacobian Matrix of the transformation at one point */
258     virtual void ComputeJacobianWithRespectToParameters (const InputPointType &p, JacobianType &jacobian) const;
259     virtual void ComputeJacobianWithRespectToPosition (const InputPointType &p, JacobianType &jacobian) const
260     {
261       itkExceptionMacro( "ComputeJacobianWithRespectToPosition not yet implemented for " << this->GetNameOfClass() );
262     }
263
264     /** Return the number of parameters that completely define the Transfom */
265     virtual NumberOfParametersType GetNumberOfParameters(void) const;
266
267     /** Return the number of parameters per dimension */
268     unsigned int GetNumberOfParametersPerDimension(void) const;
269
270     /** Return the region of the grid wholly within the support region */
271     itkGetConstReferenceMacro( ValidRegion, RegionType );
272
273     /** Indicates that this transform is linear. That is, given two
274      * points P and Q, and scalar coefficients a and b, then
275      *
276      *           T( a*P + b*Q ) = a * T(P) + b * T(Q)
277      */
278     virtual bool IsLinear() const { return false; }
279
280    //unsigned int GetNumberOfAffectedWeights() const;
281
282   protected:
283     /** Print contents of an BSplineDeformableTransform. */
284     void PrintSelf(std::ostream &os, itk::Indent indent) const;
285
286
287     BSplineDeformableTransform();
288     virtual ~BSplineDeformableTransform();
289
290     /** Wrap flat array into images of coefficients. */
291     void WrapAsImages();
292
293     /** Convert an input point to a continuous index inside the BSpline grid */
294     void TransformPointToContinuousIndex( const InputPointType & point, ContinuousIndexType & index ) const;
295
296   private:
297     BSplineDeformableTransform(const Self&); //purposely not implemented
298     void operator=(const Self&); //purposely not implemented
299
300     /** The bulk transform. */
301     BulkTransformPointer  m_BulkTransform;
302     MaskPointer m_Mask;
303
304     // JV added for BLUT interpolator
305     SizeType m_SplineOrders;
306     SizeType m_LUTSamplingFactors;
307
308     /** Variables defining the coefficient grid extend. */
309     RegionType    m_GridRegion;
310     SpacingType   m_GridSpacing;
311     DirectionType m_GridDirection;
312     OriginType    m_GridOrigin;
313
314     DirectionType m_PointToIndex;
315     DirectionType m_IndexToPoint;
316   
317     RegionType    m_ValidRegion;
318
319     /** Variables defining the interpolation support region. */
320     SizeType m_Offset;
321     itk::FixedArray<bool,InputDimension>    m_SplineOrderOdd;
322     SizeType      m_SupportSize;
323     IndexType     m_ValidRegionLast;
324   
325     /** Array holding images wrapped from the flat parameters. */
326     CoefficientImagePointer   m_WrappedImage;
327
328     /** Vector image representing the B-spline coefficients 
329      *  in each dimension. */
330     CoefficientImagePointer   m_CoefficientImage;
331
332     /** Jacobian as OutputDimension number of images. */
333     typedef typename JacobianType::ValueType JacobianValueType;
334     typedef typename itk::Vector<JacobianValueType,OutputDimension> JacobianPixelType;
335     typedef itk::Image<JacobianPixelType, OutputDimension> JacobianImageType;
336     typename JacobianImageType::Pointer m_JacobianImage[OutputDimension];
337     typedef itk::ImageRegionIterator<JacobianImageType> IteratorType;
338
339     /** Keep track of last support region used in computing the Jacobian
340      * for fast resetting of Jacobian to zero.
341      */
342     //JV for J calculation
343     mutable RegionType    m_SupportRegion;
344     mutable IndexType     m_SupportIndex;
345     mutable IndexType m_LastJacobianIndex;
346     mutable IteratorType m_Iterator[OutputDimension];
347     mutable JacobianPixelType m_ZeroVector;
348     mutable ContinuousIndexType m_Index;
349     mutable bool m_NeedResetJacobian;
350
351     /** Keep a pointer to the input parameters. */
352     const ParametersType *  m_InputParametersPointer;
353
354     /** Internal parameters buffer. */
355     ParametersType          m_InternalParametersBuffer;
356
357     //JV  the BLUT interpolator
358     typename VectorInterpolatorType::Pointer m_VectorInterpolator;
359   
360     /** Check if a continuous index is inside the valid region. */
361     bool InsideValidRegion( const ContinuousIndexType& index ) const;
362
363     // VD Use external data container for JacobianImage
364     unsigned SetJacobianImageData(JacobianPixelType * jacobianDataPointer, unsigned dim);
365
366     // VD Reset Jacobian
367     void ResetJacobian() const;
368
369     // VD Add MultipleBSplineDeformableTransform as friend to facilitate wrapping
370     friend class MultipleBSplineDeformableTransform<TCoordRep, NInputDimensions, NOutputDimensions>;
371     mutable JacobianType                            m_SharedDataBSplineJacobian;
372   }; //class BSplineDeformableTransform
373
374
375 }  // namespace itk
376
377 #ifndef ITK_MANUAL_INSTANTIATION
378 # include "clitkBSplineDeformableTransform.txx"
379 #endif
380
381
382 #endif // __clitkBSplineDeformableTransform_h