]> Creatis software - clitk.git/blob - registration/clitkMultipleBSplineDeformableTransform.h
Fixes for ITK v4.8rc02
[clitk.git] / registration / clitkMultipleBSplineDeformableTransform.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 __clitkMultipleBSplineDeformableTransform_h
19 #define __clitkMultipleBSplineDeformableTransform_h
20
21 #include <clitkBSplineDeformableTransform.h>
22
23 #include <itkNearestNeighborInterpolateImageFunction.h>
24
25 #include <vector>
26
27 namespace clitk
28 {
29   template <
30     class TCoordRep = double,
31     unsigned int NInputDimensions = 3,
32     unsigned int NOutputDimensions = 3 >
33   class ITK_EXPORT MultipleBSplineDeformableTransform : public itk::Transform< TCoordRep, NInputDimensions, NOutputDimensions >
34   {
35   public:
36
37     //====================================================================
38     // Typedefs
39     //====================================================================
40     typedef MultipleBSplineDeformableTransform<TCoordRep, NInputDimensions, NOutputDimensions>  Self;
41     typedef itk::Transform<TCoordRep, NInputDimensions, NOutputDimensions>              Superclass;
42     typedef itk::SmartPointer<Self>                                                     Pointer;
43     typedef itk::SmartPointer<const Self>                                               ConstPointer;
44
45     /** New macro for creation of through the object factory.*/
46     itkNewMacro( Self );
47
48     /** Run-time type information (and related methods). */
49     itkTypeMacro( MultipleBSplineDeformableTransform, Transform );
50
51     /** Dimension of the domain space. */
52     itkStaticConstMacro(OutputDimension, unsigned int, NOutputDimensions);
53
54     /** Dimension of the input model. */
55     itkStaticConstMacro(InputDimension, unsigned int, NInputDimensions);
56
57     /** Standard scalar type for this class. */
58     typedef typename Superclass::ScalarType ScalarType;
59
60     /** Standard parameters container. */
61     typedef typename Superclass::ParametersType ParametersType;
62 #if ITK_VERSION_MAJOR >= 4
63     typedef typename Superclass::NumberOfParametersType NumberOfParametersType;
64 #endif
65
66     /** Standard Jacobian container. */
67     typedef typename Superclass::JacobianType JacobianType;
68
69     /** Standard vector type for this class. */
70     typedef itk::Vector<TCoordRep,
71                         InputDimension> InputVectorType;
72     typedef itk::Vector<TCoordRep,
73                         OutputDimension> OutputVectorType;
74
75     /** Standard covariant vector type for this class. */
76     typedef itk::CovariantVector<TCoordRep,
77                                  InputDimension> InputCovariantVectorType;
78     typedef itk::CovariantVector<TCoordRep,
79                                  OutputDimension> OutputCovariantVectorType;
80
81     /** Standard vnl_vector type for this class. */
82     typedef vnl_vector_fixed<TCoordRep,
83                              InputDimension> InputVnlVectorType;
84     typedef vnl_vector_fixed<TCoordRep,
85                              OutputDimension> OutputVnlVectorType;
86
87     /** Standard coordinate point type for this class. */
88     typedef itk::Point<TCoordRep,
89                        InputDimension> InputPointType;
90     typedef itk::Point<TCoordRep,
91                        OutputDimension> OutputPointType;
92
93     //JV Parameters as images with OutputDimension number of components per Pixel
94     typedef typename ParametersType::ValueType                          ParametersValueType;
95     typedef typename itk::Vector<ParametersValueType, OutputDimension>  PixelType;
96     typedef itk::Image<PixelType, InputDimension>                       CoefficientImageType;
97     typedef typename CoefficientImageType::Pointer                      CoefficientImagePointer;
98
99     /** Typedefs for specifying the extend to the grid. */
100     typedef itk::ImageRegion<InputDimension>                RegionType;
101     typedef typename RegionType::IndexType                  IndexType;
102     typedef typename RegionType::SizeType                   SizeType;
103     typedef typename CoefficientImageType::SpacingType      SpacingType;
104     typedef typename CoefficientImageType::DirectionType    DirectionType;
105     typedef typename CoefficientImageType::PointType        OriginType;
106     typedef itk::ContinuousIndex<TCoordRep, InputDimension> ContinuousIndexType;
107
108     /** Typedef of the bulk transform. */
109     typedef itk::Transform<ScalarType, InputDimension, OutputDimension> BulkTransformType;
110     typedef BulkTransformType*                  BulkTransformPointer;
111
112     typedef itk::Image< unsigned char, InputDimension>   ImageLabelType;
113     typedef ImageLabelType* ImageLabelPointer;
114     //typedef typename ImageLabelType::Pointer ImageLabelPointer;
115
116     typedef itk::NearestNeighborInterpolateImageFunction<ImageLabelType, TCoordRep> ImageLabelInterpolator;
117     typedef typename ImageLabelInterpolator::Pointer ImageLabelInterpolatorPointer;
118
119     void SetLabels(ImageLabelPointer labels);
120     ImageLabelPointer GetLabels() {return m_labels;}
121
122     itkGetMacro(nLabels, unsigned);
123
124     void SetSplineOrder(const unsigned int &  splineOrder);
125     void SetSplineOrders(const SizeType &  splineOrders);
126     /*
127     itkGetMacro( SplineOrders, SizeType );
128     itkGetConstMacro( SplineOrders, SizeType );
129     */
130
131     void SetLUTSamplingFactor(const int &  samplingFactor);
132     void SetLUTSamplingFactors(const SizeType &  samplingFactors);
133     /*
134     itkGetMacro( LUTSamplingFactors, SizeType );
135     itkGetConstMacro( LUTSamplingFactors,SizeType );
136     */
137
138     void SetParameters(const ParametersType & parameters);
139
140     void SetFixedParameters(const ParametersType & parameters);
141
142     void SetParametersByValue(const ParametersType & parameters);
143
144     void SetIdentity();
145
146     /** Get the Transformation Parameters. */
147     virtual const ParametersType& GetParameters(void) const;
148
149     /** Get the Transformation Fixed Parameters. */
150     virtual const ParametersType& GetFixedParameters(void) const;
151
152     // The coefficientImage
153     virtual const std::vector<CoefficientImagePointer>& GetCoefficientImages() const;
154     virtual void SetCoefficientImages(std::vector<CoefficientImagePointer>& images);
155
156     /** This method specifies the region over which the grid resides. */
157     virtual void SetGridRegion( const RegionType& region );
158     /*
159     itkGetMacro( GridRegion, RegionType );
160     itkGetConstMacro( GridRegion, RegionType );
161     */
162
163     /** This method specifies the grid spacing or resolution. */
164     virtual void SetGridSpacing( const SpacingType& spacing );
165     /*
166     itkGetMacro( GridSpacing, SpacingType );
167     itkGetConstMacro( GridSpacing, SpacingType );
168     */
169
170     /** This method specifies the grid directions . */
171     virtual void SetGridDirection( const DirectionType & spacing );
172     /*
173     itkGetMacro( GridDirection, DirectionType );
174     itkGetConstMacro( GridDirection, DirectionType );
175     */
176
177     /** This method specifies the grid origin. */
178     virtual void SetGridOrigin( const OriginType& origin );
179     /*
180     itkGetMacro( GridOrigin, OriginType );
181     itkGetConstMacro( GridOrigin, OriginType );
182     */
183
184     // Set the bulk transform, real pointer
185     void SetBulkTransform(BulkTransformPointer b);
186     BulkTransformPointer GetBulkTransform(void) {return m_BulkTransform;}
187
188     /** Transform points by a BSpline deformable transformation. */
189     OutputPointType  TransformPoint(const InputPointType  &point ) const;
190
191     // JV added for just the deformable part, without bulk
192     OutputPointType  DeformablyTransformPoint(const InputPointType  &point ) const;
193
194     /** Method to transform a vector -
195      *  not applicable for this type of transform. */
196     virtual OutputVectorType TransformVector(const InputVectorType &) const
197     {
198       itkExceptionMacro(<< "Method not applicable for deformable transform." );
199       return OutputVectorType();
200     }
201
202     /** Method to transform a vnl_vector -
203      *  not applicable for this type of transform */
204     virtual OutputVnlVectorType TransformVector(const InputVnlVectorType &) const
205     {
206       itkExceptionMacro(<< "Method not applicable for deformable transform. ");
207       return OutputVnlVectorType();
208     }
209
210     /** Method to transform a CovariantVector -
211      *  not applicable for this type of transform */
212     virtual OutputCovariantVectorType TransformCovariantVector(const InputCovariantVectorType &) const
213     {
214       itkExceptionMacro(<< "Method not applicable for deformable transfrom. ");
215       return OutputCovariantVectorType();
216     }
217
218     /** Compute the Jacobian Matrix of the transformation at one point */
219 #if ITK_VERSION_MAJOR >= 4
220     virtual void ComputeJacobianWithRespectToParameters (const InputPointType &p, JacobianType &jacobian) const;
221     virtual void ComputeJacobianWithRespectToPosition (const InputPointType &p, JacobianType &jacobian) const
222     {
223       itkExceptionMacro( "ComputeJacobianWithRespectToPosition not yet implemented for " << this->GetNameOfClass() );
224     }
225 #else
226     virtual const JacobianType& GetJacobian(const InputPointType  &point ) const;
227 #endif
228
229     /** Return the number of parameters that completely define the Transfom */
230 #if ITK_VERSION_MAJOR >= 4
231     virtual NumberOfParametersType GetNumberOfParameters(void) const;
232 #else
233     virtual unsigned int GetNumberOfParameters(void) const;
234 #endif
235
236     /** Return the number of parameters per dimension */
237     unsigned int GetNumberOfParametersPerDimension(void) const;
238
239     virtual bool IsLinear() const { return false; }
240
241     typedef  clitk::BSplineDeformableTransform<TCoordRep,InputDimension, OutputDimension > TransformType;
242
243     const std::vector<typename TransformType::Pointer> &GetTransforms() { return m_trans; }
244
245   protected:
246
247     void PrintSelf(std::ostream &os, itk::Indent indent) const;
248
249     MultipleBSplineDeformableTransform();
250     virtual ~MultipleBSplineDeformableTransform();
251
252     void TransformPointToContinuousIndex( const InputPointType & point, ContinuousIndexType & index ) const;
253
254   private:
255     MultipleBSplineDeformableTransform(const Self&); //purposely not implemented
256     void operator=(const Self&); //purposely not implemented
257
258     /** The bulk transform. */
259     BulkTransformPointer  m_BulkTransform;
260
261     /** Jacobian as OutputDimension number of images. */
262     typedef typename JacobianType::ValueType JacobianValueType;
263     typedef typename itk::Vector<JacobianValueType,OutputDimension> JacobianPixelType;
264     typedef itk::Image<JacobianPixelType, OutputDimension> JacobianImageType;
265     typename JacobianImageType::Pointer m_JacobianImage[OutputDimension];
266     typedef itk::ImageRegionIterator<JacobianImageType> IteratorType;
267
268     /** Keep a pointer to the input parameters. */
269     const ParametersType *  m_InputParametersPointer;
270
271     /** Internal parameters buffer. */
272     ParametersType          m_InternalParametersBuffer;
273
274     unsigned int                                    m_nLabels;
275     ImageLabelPointer                               m_labels;
276     ImageLabelInterpolatorPointer                   m_labelInterpolator;
277     std::vector<typename TransformType::Pointer>    m_trans;
278     std::vector<ParametersType>                     m_parameters;
279     mutable std::vector<CoefficientImagePointer>    m_CoefficientImages;
280     mutable int                                     m_LastJacobian;
281 #if ITK_VERSION_MAJOR >= 4
282     mutable JacobianType                            m_SharedDataBSplineJacobian;
283 #endif
284
285     void InitJacobian();
286     // FIXME it seems not used
287     bool InsideValidRegion( const ContinuousIndexType& index ) const;
288   };
289
290 }  // namespace clitk
291
292 #ifndef ITK_MANUAL_INSTANTIATION
293 # include "clitkMultipleBSplineDeformableTransform.txx"
294 #endif
295
296 #endif // __clitkMultipleBSplineDeformableTransform_h