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;
69 /** Standard Jacobian container. */
70 typedef typename Superclass::JacobianType JacobianType;
72 /** Standard vector type for this class. */
73 typedef itk::Vector<TCoordRep,
74 InputDimension> InputVectorType;
75 typedef itk::Vector<TCoordRep,
76 OutputDimension> OutputVectorType;
78 /** Standard covariant vector type for this class. */
79 typedef itk::CovariantVector<TCoordRep,
80 InputDimension> InputCovariantVectorType;
81 typedef itk::CovariantVector<TCoordRep,
82 OutputDimension> OutputCovariantVectorType;
84 /** Standard vnl_vector type for this class. */
85 typedef vnl_vector_fixed<TCoordRep,
86 InputDimension> InputVnlVectorType;
87 typedef vnl_vector_fixed<TCoordRep,
88 OutputDimension> OutputVnlVectorType;
90 /** Standard coordinate point type for this class. */
91 typedef itk::Point<TCoordRep,
92 InputDimension> InputPointType;
93 typedef itk::Point<TCoordRep,
94 OutputDimension> OutputPointType;
96 //JV Parameters as images with OutputDimension number of components per Pixel
97 typedef typename ParametersType::ValueType ParametersValueType;
98 typedef typename itk::Vector<ParametersValueType, OutputDimension> PixelType;
99 typedef itk::Image<PixelType, InputDimension> CoefficientImageType;
100 typedef typename CoefficientImageType::Pointer CoefficientImagePointer;
103 /** Typedefs for specifying the extend to the grid. */
104 typedef itk::ImageRegion<InputDimension> RegionType;
105 typedef typename RegionType::IndexType IndexType;
106 typedef typename RegionType::SizeType SizeType;
107 typedef typename CoefficientImageType::SpacingType SpacingType;
108 typedef typename CoefficientImageType::DirectionType DirectionType;
109 typedef typename CoefficientImageType::PointType OriginType;
110 typedef itk::ContinuousIndex<TCoordRep, InputDimension> ContinuousIndexType;
112 //JV added for the BLUT interpolator
113 //typedef itk::Vector<TCoordRep, InputDimension> OutputSpacingType;
115 //JV m_VectorInterpolator
116 typedef VectorBSplineResampleImageFunctionWithLUT
117 <CoefficientImageType, TCoordRep> VectorInterpolatorType;
118 typedef typename VectorInterpolatorType::CoefficientDataType CoefficientDataType;
119 typedef typename VectorInterpolatorType::CoefficientDataType WeightsDataType;
121 /** Typedef of the bulk transform. */
122 typedef itk::Transform<ScalarType, InputDimension, OutputDimension> BulkTransformType;
123 typedef BulkTransformType* BulkTransformPointer;
125 /** Typedef of the mask */
126 typedef itk::SpatialObject< InputDimension > MaskType;
127 typedef MaskType* MaskPointer;
129 //====================================================================
131 //====================================================================
132 //JV added for the BLUT interpolator
133 void SetSplineOrder(const unsigned int & splineOrder);
134 void SetSplineOrders(const SizeType & splineOrders);
135 itkGetMacro( SplineOrders, SizeType );
136 itkGetConstMacro( SplineOrders, SizeType );
137 void SetLUTSamplingFactor(const int & samplingFactor);
138 void SetLUTSamplingFactors(const SizeType & samplingFactors);
139 itkGetMacro( LUTSamplingFactors, SizeType );
140 itkGetConstMacro( LUTSamplingFactors,SizeType );
141 //void SetOutputSpacing(const OutputSpacingType & outputSpacing);
142 //itkGetMacro( OutputSpacing, OutputSpacingType );
143 //itkGetConstMacro( OutputSpacing, OutputSpacingType );
145 void SetParameters(const ParametersType & parameters);
147 void SetFixedParameters(const ParametersType & parameters);
149 void SetParametersByValue(const ParametersType & parameters);
153 /** Get the Transformation Parameters. */
154 virtual const ParametersType& GetParameters(void) const;
156 /** Get the Transformation Fixed Parameters. */
157 virtual const ParametersType& GetFixedParameters(void) const;
159 // The coefficientImage
160 virtual CoefficientImagePointer GetCoefficientImage()
161 { return m_CoefficientImage; }
162 virtual const CoefficientImagePointer GetCoefficientImage() const
163 { return m_CoefficientImage; }
164 virtual void SetCoefficientImage(CoefficientImagePointer image);
166 /** This method specifies the region over which the grid resides. */
167 virtual void SetGridRegion( const RegionType& region );
168 itkGetMacro( GridRegion, RegionType );
169 itkGetConstMacro( GridRegion, RegionType );
171 /** This method specifies the grid spacing or resolution. */
172 virtual void SetGridSpacing( const SpacingType& spacing );
173 itkGetMacro( GridSpacing, SpacingType );
174 itkGetConstMacro( GridSpacing, SpacingType );
176 /** This method specifies the grid directions . */
177 virtual void SetGridDirection( const DirectionType & spacing );
178 itkGetMacro( GridDirection, DirectionType );
179 itkGetConstMacro( GridDirection, DirectionType );
181 /** This method specifies the grid origin. */
182 virtual void SetGridOrigin( const OriginType& origin );
183 itkGetMacro( GridOrigin, OriginType );
184 itkGetConstMacro( GridOrigin, OriginType );
186 // Set the bulk transform, real pointer
187 // itkSetConstObjectMacro( BulkTransform, BulkTransformType );
188 // itkGetConstObjectMacro( BulkTransform, BulkTransformType );
189 void SetBulkTransform(BulkTransformPointer b){m_BulkTransform=b;}
190 BulkTransformPointer GetBulkTransform(void) {return m_BulkTransform;}
192 // Set mask, inside transform applies, outside zero, real pointer
193 void SetMask(MaskPointer m){m_Mask=m;}
194 MaskPointer GetMask(void){return m_Mask;}
195 // itkSetConstObjectMacro( Mask, MaskType );
196 // itkGetConstObjectMacro( Mask, MaskType );
198 /** Transform points by a BSpline deformable transformation. */
199 OutputPointType TransformPoint(const InputPointType &point ) const;
201 // JV added for just the deformable part, without bulk
202 OutputPointType DeformablyTransformPoint(const InputPointType &point ) const;
204 /** Parameter index array type. */
205 typedef itk::Array<unsigned long> ParameterIndexArrayType;
207 /** Transform points by a BSpline deformable transformation.
208 * On return, weights contains the interpolation weights used to compute the
209 * deformation and indices of the x (zeroth) dimension coefficient parameters
210 * in the support region used to compute the deformation.
211 * Parameter indices for the i-th dimension can be obtained by adding
212 * ( i * this->GetNumberOfParametersPerDimension() ) to the indices array.
215 // JV not implemented
216 // virtual void TransformPoint( const InputPointType & inputPoint,
217 // OutputPointType & outputPoint,
218 // WeightsType & weights,
219 // ParameterIndexArrayType & indices,
220 // bool & inside ) const;
221 // virtual void DeformablyTransformPoint( const InputPointType & inputPoint,
222 // OutputPointType & outputPoint,
223 // WeightsType & weights,
224 // ParameterIndexArrayType & indices,
225 // bool & inside ) const;
226 // virtual void GetJacobian( const InputPointType & inputPoint,
227 // WeightsType & weights,
228 // ParameterIndexArrayType & indices
231 /** Method to transform a vector -
232 * not applicable for this type of transform. */
233 virtual OutputVectorType TransformVector(const InputVectorType &) const
235 itkExceptionMacro(<< "Method not applicable for deformable transform." );
236 return OutputVectorType();
239 /** Method to transform a vnl_vector -
240 * not applicable for this type of transform */
241 virtual OutputVnlVectorType TransformVector(const InputVnlVectorType &) const
243 itkExceptionMacro(<< "Method not applicable for deformable transform. ");
244 return OutputVnlVectorType();
247 /** Method to transform a CovariantVector -
248 * not applicable for this type of transform */
249 virtual OutputCovariantVectorType TransformCovariantVector(
250 const InputCovariantVectorType &) const
252 itkExceptionMacro(<< "Method not applicable for deformable transfrom. ");
253 return OutputCovariantVectorType();
256 /** Compute the Jacobian Matrix of the transformation at one point */
257 virtual const JacobianType& GetJacobian(const InputPointType &point ) const;
259 /** Return the number of parameters that completely define the Transfom */
260 virtual unsigned int GetNumberOfParameters(void) const;
262 /** Return the number of parameters per dimension */
263 unsigned int GetNumberOfParametersPerDimension(void) const;
265 /** Return the region of the grid wholly within the support region */
266 itkGetConstReferenceMacro( ValidRegion, RegionType );
268 /** Indicates that this transform is linear. That is, given two
269 * points P and Q, and scalar coefficients a and b, then
271 * T( a*P + b*Q ) = a * T(P) + b * T(Q)
273 virtual bool IsLinear() const { return false; }
275 //unsigned int GetNumberOfAffectedWeights() const;
278 /** Print contents of an BSplineDeformableTransform. */
279 void PrintSelf(std::ostream &os, itk::Indent indent) const;
282 BSplineDeformableTransform();
283 virtual ~BSplineDeformableTransform();
285 /** Wrap flat array into images of coefficients. */
288 /** Convert an input point to a continuous index inside the BSpline grid */
289 void TransformPointToContinuousIndex( const InputPointType & point, ContinuousIndexType & index ) const;
292 BSplineDeformableTransform(const Self&); //purposely not implemented
293 void operator=(const Self&); //purposely not implemented
295 /** The bulk transform. */
296 BulkTransformPointer m_BulkTransform;
299 // JV added for BLUT interpolator
300 SizeType m_SplineOrders;
301 SizeType m_LUTSamplingFactors;
303 /** Variables defining the coefficient grid extend. */
304 RegionType m_GridRegion;
305 SpacingType m_GridSpacing;
306 DirectionType m_GridDirection;
307 OriginType m_GridOrigin;
309 DirectionType m_PointToIndex;
310 DirectionType m_IndexToPoint;
312 RegionType m_ValidRegion;
314 /** Variables defining the interpolation support region. */
316 itk::FixedArray<bool,InputDimension> m_SplineOrderOdd;
317 SizeType m_SupportSize;
318 IndexType m_ValidRegionLast;
320 /** Array holding images wrapped from the flat parameters. */
321 CoefficientImagePointer m_WrappedImage;
323 /** Vector image representing the B-spline coefficients
324 * in each dimension. */
325 CoefficientImagePointer m_CoefficientImage;
327 /** Jacobian as OutputDimension number of images. */
328 typedef typename JacobianType::ValueType JacobianValueType;
329 typedef typename itk::Vector<JacobianValueType,OutputDimension> JacobianPixelType;
330 typedef itk::Image<JacobianPixelType, OutputDimension> JacobianImageType;
331 typename JacobianImageType::Pointer m_JacobianImage[OutputDimension];
332 typedef itk::ImageRegionIterator<JacobianImageType> IteratorType;
334 /** Keep track of last support region used in computing the Jacobian
335 * for fast resetting of Jacobian to zero.
337 //JV for J calculation
338 mutable RegionType m_SupportRegion;
339 mutable IndexType m_SupportIndex;
340 mutable IndexType m_LastJacobianIndex;
341 mutable IteratorType m_Iterator[OutputDimension];
342 mutable JacobianPixelType m_ZeroVector;
343 mutable ContinuousIndexType m_Index;
344 mutable bool m_NeedResetJacobian;
346 /** Keep a pointer to the input parameters. */
347 const ParametersType * m_InputParametersPointer;
349 /** Internal parameters buffer. */
350 ParametersType m_InternalParametersBuffer;
352 //JV the BLUT interpolator
353 typename VectorInterpolatorType::Pointer m_VectorInterpolator;
355 /** Check if a continuous index is inside the valid region. */
356 bool InsideValidRegion( const ContinuousIndexType& index ) const;
358 // VD Use external data container for JacobianImage
359 unsigned SetJacobianImageData(JacobianPixelType * jacobianDataPointer, unsigned dim);
362 void ResetJacobian() const;
364 // VD Add MultipleBSplineDeformableTransform as friend to facilitate wrapping
365 friend class MultipleBSplineDeformableTransform<TCoordRep, NInputDimensions, NOutputDimensions>;
367 }; //class BSplineDeformableTransform
373 # include "clitkBSplineDeformableTransform.txx"
377 #endif // __clitkBSplineDeformableTransform_h