1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
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"
23 #include "itkTransform.h"
25 #include "itkImageRegion.h"
26 #include "itkSpatialObject.h"
30 // Forward declaration needed for friendship
31 template <class TCoordRep, unsigned int NInputDimensions, unsigned int NOutputDimensions>
32 class ITK_EXPORT MultipleBSplineDeformableTransform;
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 >
43 //====================================================================
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;
51 /** New macro for creation of through the object factory.*/
54 /** Run-time type information (and related methods). */
55 itkTypeMacro( BSplineDeformableTransform, Transform );
57 /** Dimension of the domain space. */
58 itkStaticConstMacro(OutputDimension, unsigned int, NOutputDimensions);
60 /** Dimension of the input model. */
61 itkStaticConstMacro(InputDimension, unsigned int, NInputDimensions);
63 /** Standard scalar type for this class. */
64 typedef typename Superclass::ScalarType ScalarType;
66 /** Standard parameters container. */
67 typedef typename Superclass::ParametersType ParametersType;
68 typedef typename Superclass::NumberOfParametersType NumberOfParametersType;
70 /** Standard Jacobian container. */
71 typedef typename Superclass::JacobianType JacobianType;
73 /** Standard vector type for this class. */
74 typedef itk::Vector<TCoordRep,
75 InputDimension> InputVectorType;
76 typedef itk::Vector<TCoordRep,
77 OutputDimension> OutputVectorType;
79 /** Standard covariant vector type for this class. */
80 typedef itk::CovariantVector<TCoordRep,
81 InputDimension> InputCovariantVectorType;
82 typedef itk::CovariantVector<TCoordRep,
83 OutputDimension> OutputCovariantVectorType;
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;
91 /** Standard coordinate point type for this class. */
92 typedef itk::Point<TCoordRep,
93 InputDimension> InputPointType;
94 typedef itk::Point<TCoordRep,
95 OutputDimension> OutputPointType;
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;
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;
113 //JV added for the BLUT interpolator
114 //typedef itk::Vector<TCoordRep, InputDimension> OutputSpacingType;
116 //JV m_VectorInterpolator
117 typedef VectorBSplineResampleImageFunctionWithLUT
118 <CoefficientImageType, TCoordRep> VectorInterpolatorType;
119 typedef typename VectorInterpolatorType::CoefficientDataType CoefficientDataType;
120 typedef typename VectorInterpolatorType::CoefficientDataType WeightsDataType;
122 /** Typedef of the bulk transform. */
123 typedef itk::Transform<ScalarType, InputDimension, OutputDimension> BulkTransformType;
124 typedef BulkTransformType* BulkTransformPointer;
126 /** Typedef of the mask */
127 typedef itk::SpatialObject< InputDimension > MaskType;
128 typedef MaskType* MaskPointer;
130 //====================================================================
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 );
146 void SetParameters(const ParametersType & parameters);
148 void SetFixedParameters(const ParametersType & parameters);
150 void SetParametersByValue(const ParametersType & parameters);
154 /** Get the Transformation Parameters. */
155 virtual const ParametersType& GetParameters(void) const;
157 /** Get the Transformation Fixed Parameters. */
158 virtual const ParametersType& GetFixedParameters(void) const;
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);
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 );
172 /** This method specifies the grid spacing or resolution. */
173 virtual void SetGridSpacing( const SpacingType& spacing );
174 itkGetMacro( GridSpacing, SpacingType );
175 itkGetConstMacro( GridSpacing, SpacingType );
177 /** This method specifies the grid directions . */
178 virtual void SetGridDirection( const DirectionType & spacing );
179 itkGetMacro( GridDirection, DirectionType );
180 itkGetConstMacro( GridDirection, DirectionType );
182 /** This method specifies the grid origin. */
183 virtual void SetGridOrigin( const OriginType& origin );
184 itkGetMacro( GridOrigin, OriginType );
185 itkGetConstMacro( GridOrigin, OriginType );
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;}
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 );
199 /** Transform points by a BSpline deformable transformation. */
200 OutputPointType TransformPoint(const InputPointType &point ) const;
202 // JV added for just the deformable part, without bulk
203 OutputPointType DeformablyTransformPoint(const InputPointType &point ) const;
205 /** Parameter index array type. */
206 typedef itk::Array<unsigned long> ParameterIndexArrayType;
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.
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
232 /** Method to transform a vector -
233 * not applicable for this type of transform. */
234 virtual OutputVectorType TransformVector(const InputVectorType &) const
236 itkExceptionMacro(<< "Method not applicable for deformable transform." );
237 return OutputVectorType();
240 /** Method to transform a vnl_vector -
241 * not applicable for this type of transform */
242 virtual OutputVnlVectorType TransformVector(const InputVnlVectorType &) const
244 itkExceptionMacro(<< "Method not applicable for deformable transform. ");
245 return OutputVnlVectorType();
248 /** Method to transform a CovariantVector -
249 * not applicable for this type of transform */
250 virtual OutputCovariantVectorType TransformCovariantVector(
251 const InputCovariantVectorType &) const
253 itkExceptionMacro(<< "Method not applicable for deformable transfrom. ");
254 return OutputCovariantVectorType();
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
261 itkExceptionMacro( "ComputeJacobianWithRespectToPosition not yet implemented for " << this->GetNameOfClass() );
264 /** Return the number of parameters that completely define the Transfom */
265 virtual NumberOfParametersType GetNumberOfParameters(void) const;
267 /** Return the number of parameters per dimension */
268 unsigned int GetNumberOfParametersPerDimension(void) const;
270 /** Return the region of the grid wholly within the support region */
271 itkGetConstReferenceMacro( ValidRegion, RegionType );
273 /** Indicates that this transform is linear. That is, given two
274 * points P and Q, and scalar coefficients a and b, then
276 * T( a*P + b*Q ) = a * T(P) + b * T(Q)
278 virtual bool IsLinear() const { return false; }
280 //unsigned int GetNumberOfAffectedWeights() const;
283 /** Print contents of an BSplineDeformableTransform. */
284 void PrintSelf(std::ostream &os, itk::Indent indent) const;
287 BSplineDeformableTransform();
288 virtual ~BSplineDeformableTransform();
290 /** Wrap flat array into images of coefficients. */
293 /** Convert an input point to a continuous index inside the BSpline grid */
294 void TransformPointToContinuousIndex( const InputPointType & point, ContinuousIndexType & index ) const;
297 BSplineDeformableTransform(const Self&); //purposely not implemented
298 void operator=(const Self&); //purposely not implemented
300 /** The bulk transform. */
301 BulkTransformPointer m_BulkTransform;
304 // JV added for BLUT interpolator
305 SizeType m_SplineOrders;
306 SizeType m_LUTSamplingFactors;
308 /** Variables defining the coefficient grid extend. */
309 RegionType m_GridRegion;
310 SpacingType m_GridSpacing;
311 DirectionType m_GridDirection;
312 OriginType m_GridOrigin;
314 DirectionType m_PointToIndex;
315 DirectionType m_IndexToPoint;
317 RegionType m_ValidRegion;
319 /** Variables defining the interpolation support region. */
321 itk::FixedArray<bool,InputDimension> m_SplineOrderOdd;
322 SizeType m_SupportSize;
323 IndexType m_ValidRegionLast;
325 /** Array holding images wrapped from the flat parameters. */
326 CoefficientImagePointer m_WrappedImage;
328 /** Vector image representing the B-spline coefficients
329 * in each dimension. */
330 CoefficientImagePointer m_CoefficientImage;
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;
339 /** Keep track of last support region used in computing the Jacobian
340 * for fast resetting of Jacobian to zero.
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;
351 /** Keep a pointer to the input parameters. */
352 const ParametersType * m_InputParametersPointer;
354 /** Internal parameters buffer. */
355 ParametersType m_InternalParametersBuffer;
357 //JV the BLUT interpolator
358 typename VectorInterpolatorType::Pointer m_VectorInterpolator;
360 /** Check if a continuous index is inside the valid region. */
361 bool InsideValidRegion( const ContinuousIndexType& index ) const;
363 // VD Use external data container for JacobianImage
364 unsigned SetJacobianImageData(JacobianPixelType * jacobianDataPointer, unsigned dim);
367 void ResetJacobian() const;
369 // VD Add MultipleBSplineDeformableTransform as friend to facilitate wrapping
370 friend class MultipleBSplineDeformableTransform<TCoordRep, NInputDimensions, NOutputDimensions>;
371 mutable JacobianType m_SharedDataBSplineJacobian;
372 }; //class BSplineDeformableTransform
377 #ifndef ITK_MANUAL_INSTANTIATION
378 # include "clitkBSplineDeformableTransform.txx"
382 #endif // __clitkBSplineDeformableTransform_h