PartialBSplineFreeFormTransformationSV.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2013-2015 Imperial College London
5  * Copyright 2013-2015 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef MIRTK_PartialBSplineFreeFormTransformationSV_H
21 #define MIRTK_PartialBSplineFreeFormTransformationSV_H
22 
23 #include "mirtk/Transformation.h"
24 #include "mirtk/BSplineFreeFormTransformationSV.h"
25 
26 
27 namespace mirtk {
28 
29 
30 /**
31  * Decorator for SV FFD transformation.
32  *
33  * This decorator wraps a SV FFD transformation but defines it's own attributes
34  * regarding integration and whether or not to invert the transformation by
35  * default. As the SV FFD is parameterized by stationary velocities, inverting
36  * the transformation is simply achieved by a negative upper integration limit.
37  * Two instances of this decorator are in particular used by
38  * SymmetricBSplineFreeFormTransformation for the two half transformations
39  * to deform both input images.
40  */
42 {
43  mirtkTransformationMacro(PartialBSplineFreeFormTransformationSV);
44 
45  // ---------------------------------------------------------------------------
46  // Attributes
47 
48  /// Pointer to decorated transformation
49  mirtkPublicAggregateMacro(BSplineFreeFormTransformationSV, Transformation);
50 
51  /// Fraction of decorated transformation, negative value corresponds to inverse
52  mirtkPublicAttributeMacro(double, Fraction);
53 
54  // ---------------------------------------------------------------------------
55  // Construction/Destruction
56 
57 public:
58 
59  /// Constructor
61 
62  /// Copy constructor
64 
65  /// Destructor
67 
68  // ---------------------------------------------------------------------------
69  // Transformation parameters (DoFs)
70 
71  /// Copy active transformation parameters (DoFs) from given
72  /// transformation if possible and return \c false, otherwise
73  virtual bool CopyFrom(const class Transformation *);
74 
75  /// Get number of transformation parameters
76  virtual int NumberOfDOFs() const;
77 
78  /// Put value of transformation parameter
79  virtual void Put(int, double);
80 
81  /// Put values of transformation parameters
82  virtual void Put(const DOFValue *);
83 
84  /// Add change to transformation parameters
85  virtual void Add(const DOFValue *);
86 
87  /// Update transformation parameters given parametric gradient
88  virtual double Update(const DOFValue *);
89 
90  /// Get value of transformation parameter
91  virtual double Get(int) const;
92 
93  /// Get values of transformation parameters
94  virtual void Get(DOFValue *) const;
95 
96  /// Put status of transformation parameter
97  virtual void PutStatus(int, DOFStatus);
98 
99  /// Get status of transformation parameter
100  virtual DOFStatus GetStatus(int) const;
101 
102  /// Checks whether transformation depends on the same vector of parameters
103  virtual bool HasSameDOFsAs(const class Transformation *) const;
104 
105  /// Checks whether the transformation is an identity mapping
106  virtual bool IsIdentity() const;
107 
108  // ---------------------------------------------------------------------------
109  // Parameters (non-DoFs)
110 
111  // Import other overloads
113 
114  /// Set named (non-DoF) parameter from value as string
115  virtual bool Set(const char *, const char *);
116 
117  /// Get (non-DoF) parameters as key/value as string map
118  virtual ParameterList Parameter() const;
119 
120  // ---------------------------------------------------------------------------
121  // Point transformation
122 
123 private:
124 
125  /// Partial upper integration limit used given the temporal origin of both target
126  /// and source images. If both images have the same temporal origin, the sign
127  /// of the fraction determines the direction of integration. Otherwise, if the
128  /// temporal origin of the two images differs, the signed difference between
129  /// these determines the direction of integration.
130  double UpperIntegrationLimit(double t, double t0) const;
131 
132 public:
133 
134  /// Whether the caching of the transformation displacements is required
135  /// (or preferred) by this transformation. For some transformations such as
136  /// those parameterized by velocities, caching of the displacements for
137  /// each target voxel results in better performance or is needed for example
138  /// for the scaling and squaring method.
139  virtual bool RequiresCachingOfDisplacements() const;
140 
141  /// Transforms a single point using the global transformation component only
142  virtual void GlobalTransform(double &, double &, double &, double = 0, double = NaN) const;
143 
144  /// Transforms a single point using the local transformation component only
145  virtual void LocalTransform(double &, double &, double &, double = 0, double = NaN) const;
146 
147  /// Transforms a single point
148  virtual void Transform(double &, double &, double &, double = 0, double = NaN) const;
149 
150  /// Transforms a single point using the inverse of the global transformation only
151  virtual void GlobalInverse(double &, double &, double &, double = 0, double = NaN) const;
152 
153  /// Transforms a single point using the inverse of the local transformation only
154  virtual bool LocalInverse(double &, double &, double &, double = 0, double = NaN) const;
155 
156  /// Transforms a single point using the inverse of the transformation
157  virtual bool Inverse(double &, double &, double &, double = 0, double = NaN) const;
158 
159  /// Calculates the displacement vectors for a whole image domain
160  ///
161  /// \attention The displacements are computed at the positions after applying the
162  /// current displacements at each voxel. These displacements are then
163  /// added to the current displacements. Therefore, set the input
164  /// displacements to zero if only interested in the displacements of
165  /// this transformation at the voxel positions.
166  virtual void Displacement(GenericImage<double> &, double, double = NaN, const WorldCoordsImage * = NULL) const;
167 
168  /// Calculates the displacement vectors for a whole image domain
169  ///
170  /// \attention The displacements are computed at the positions after applying the
171  /// current displacements at each voxel. These displacements are then
172  /// added to the current displacements. Therefore, set the input
173  /// displacements to zero if only interested in the displacements of
174  /// this transformation at the voxel positions.
175  virtual void Displacement(GenericImage<float> &, double, double = NaN, const WorldCoordsImage * = NULL) const;
176 
177  /// Calculates the inverse displacement vectors for a whole image domain
178  ///
179  /// \attention The displacements are computed at the positions after applying the
180  /// current displacements at each voxel. These displacements are then
181  /// added to the current displacements. Therefore, set the input
182  /// displacements to zero if only interested in the displacements of
183  /// this transformation at the voxel positions.
184  ///
185  /// \returns Always zero.
186  virtual int InverseDisplacement(GenericImage<double> &, double, double = NaN, const WorldCoordsImage * = NULL) const;
187 
188  /// Calculates the inverse displacement vectors for a whole image domain
189  ///
190  /// \attention The displacements are computed at the positions after applying the
191  /// current displacements at each voxel. These displacements are then
192  /// added to the current displacements. Therefore, set the input
193  /// displacements to zero if only interested in the displacements of
194  /// this transformation at the voxel positions.
195  ///
196  /// \returns Always zero.
197  virtual int InverseDisplacement(GenericImage<float> &, double, double = NaN, const WorldCoordsImage * = NULL) const;
198 
199  // ---------------------------------------------------------------------------
200  // Derivatives
205 
206  /// Calculates the Jacobian of the global transformation w.r.t world coordinates
207  virtual void GlobalJacobian(Matrix &, double, double, double, double = 0, double = NaN) const;
208 
209  /// Calculates the Jacobian of the local transformation w.r.t world coordinates
210  virtual void LocalJacobian(Matrix &, double, double, double, double = 0, double = NaN) const;
211 
212  /// Calculates the Jacobian of the transformation w.r.t world coordinates
213  virtual void Jacobian(Matrix &, double, double, double, double = 0, double = NaN) const;
214 
215  /// Calculates the Hessian for each component of the global transformation w.r.t world coordinates
216  virtual void GlobalHessian(Matrix [3], double, double, double, double = 0, double = NaN) const;
217 
218  /// Calculates the Hessian for each component of the local transformation w.r.t world coordinates
219  virtual void LocalHessian(Matrix [3], double, double, double, double = 0, double = NaN) const;
220 
221  /// Calculates the Hessian for each component of the transformation w.r.t world coordinates
222  virtual void Hessian(Matrix [3], double, double, double, double = 0, double = NaN) const;
223 
224  /// Calculates the Jacobian of the transformation w.r.t the transformation parameters
225  virtual void JacobianDOFs(double [3], int, double, double, double, double = 0, double = NaN) const;
226 
227  /// Applies the chain rule to convert spatial non-parametric gradient
228  /// to a gradient w.r.t the parameters of this transformation.
229  virtual void ParametricGradient(const GenericImage<double> *, double *,
230  const WorldCoordsImage *,
231  const WorldCoordsImage *,
232  double = NaN, double = 1) const;
233 
234 
235  /// Applies the chain rule to convert spatial non-parametric gradient
236  /// to a gradient w.r.t the parameters of this transformation.
237  virtual void ParametricGradient(const GenericImage<double> **, int, double *,
238  const WorldCoordsImage *,
239  const WorldCoordsImage *,
240  const double * = NULL, double = 1) const;
241 
242  // ---------------------------------------------------------------------------
243  // I/O
244 
245  // Do not hide methods of base class
246  using Transformation::Print;
247  using Transformation::Read;
248  using Transformation::Write;
249 
250  /// Prints information about the transformation
251  virtual void Print(ostream &, Indent = 0) const;
252 
253  /// Reads transformation from a file stream
254  virtual Cifstream &Read(Cifstream &);
255 
256  /// Writes transformation to a file stream
257  virtual Cofstream &Write(Cofstream &) const;
258 
259 };
260 
261 
262 } // namespace mirtk
263 
264 #endif // MIRTK_PartialBSplineFreeFormTransformationSV_H
virtual ParameterList Parameter() const
Get (non-DoF) parameters as key/value as string map.
virtual bool HasSameDOFsAs(const class Transformation *) const
Checks whether transformation depends on the same vector of parameters.
virtual bool LocalInverse(double &, double &, double &, double=0, double=NaN) const
Transforms a single point using the inverse of the local transformation only.
virtual double Get(int) const
Get value of transformation parameter.
virtual int InverseDisplacement(GenericImage< double > &, double, double=NaN, const WorldCoordsImage *=NULL) const
virtual void LocalHessian(Matrix [3], double, double, double, double=0, double=NaN) const
Calculates the Hessian for each component of the local transformation w.r.t world coordinates...
virtual bool CopyFrom(const class Transformation *)
virtual void Hessian(Matrix [3], double, double, double, double=0, double=NaN) const
Calculates the Hessian for each component of the transformation w.r.t world coordinates.
double DOFValue
Type of transformation parameter value.
virtual bool Set(const char *, const char *)
Set named (non-DoF) parameter from value as string.
virtual bool IsIdentity() const
Checks whether the transformation is an identity mapping.
virtual void PutStatus(int, DOFStatus)
Put status of transformation parameter.
virtual void JacobianDOFs(double [3], int, double, double, double, double=0, double=NaN) const
Calculates the Jacobian of the transformation w.r.t the transformation parameters.
Array< Pair< string, string > > ParameterList
Ordered list of parameter name/value pairs.
Definition: Object.h:38
virtual void Add(const DOFValue *)
Add change to transformation parameters.
virtual Cofstream & Write(Cofstream &) const
Writes transformation to a file stream.
virtual void Write(const char *) const
Writes a transformation to a file.
Status
Enumeration of common states for entities such as objective function parameters.
Definition: Status.h:28
Definition: IOConfig.h:41
virtual void Jacobian(Matrix &, double, double, double, double=0, double=NaN) const
Calculates the Jacobian of the transformation w.r.t world coordinates.
virtual void GlobalTransform(double &, double &, double &, double=0, double=NaN) const
Transforms a single point using the global transformation component only.
virtual void Print(ostream &, Indent=0) const
Prints information about the transformation.
virtual void Put(int, double)
Put value of transformation parameter.
virtual DOFStatus GetStatus(int) const
Get status of transformation parameter.
virtual void Displacement(GenericImage< double > &, double, double=NaN, const WorldCoordsImage *=NULL) const
virtual void Transform(double &, double &, double &, double=0, double=NaN) const
Transforms a single point.
virtual void Jacobian(Matrix &, double, double, double, double=0, double=NaN) const
Calculates the Jacobian of the transformation w.r.t world coordinates.
virtual ~PartialBSplineFreeFormTransformationSV()
Destructor.
virtual void Read(const char *)
Reads a transformation from a file.
virtual void GlobalInverse(double &, double &, double &, double=0, double=NaN) const
Transforms a single point using the inverse of the global transformation only.
virtual void LocalTransform(double &, double &, double &, double=0, double=NaN) const
Transforms a single point using the local transformation component only.
virtual double Update(const DOFValue *)
Update transformation parameters given parametric gradient.
virtual ParameterList Parameter() const
Get (non-DoF) parameters as key/value as string map.
virtual Cifstream & Read(Cifstream &)
Reads transformation from a file stream.
virtual void LocalJacobian(Matrix &, double, double, double, double=0, double=NaN) const
Calculates the Jacobian of the local transformation w.r.t world coordinates.
virtual void GlobalHessian(Matrix [3], double, double, double, double=0, double=NaN) const
Calculates the Hessian for each component of the global transformation w.r.t world coordinates...
virtual void LocalJacobian(Matrix &, double, double, double, double=0, double=NaN) const
Calculates the Jacobian of the local transformation w.r.t world coordinates.
PartialBSplineFreeFormTransformationSV(BSplineFreeFormTransformationSV *=NULL, double=1.0)
Constructor.
virtual bool Inverse(double &, double &, double &, double=0, double=NaN) const
Transforms a single point using the inverse of the transformation.
virtual void GlobalJacobian(Matrix &, double, double, double, double=0, double=NaN) const
Calculates the Jacobian of the global transformation w.r.t world coordinates.
void Print(Indent=0) const
Prints information about the transformation.
virtual void GlobalJacobian(Matrix &, double, double, double, double=0, double=NaN) const
Calculates the Jacobian of the global transformation w.r.t world coordinates.
virtual void ParametricGradient(const GenericImage< double > *, double *, const WorldCoordsImage *, const WorldCoordsImage *, double=NaN, double=1) const
virtual void ParametricGradient(const GenericImage< double > *, double *, const WorldCoordsImage *, const WorldCoordsImage *, double=NaN, double=1) const
virtual int NumberOfDOFs() const
Get number of transformation parameters.