BSplineFreeFormTransformationStatistical.h
1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2008-2015 Imperial College London
5  * Copyright 2015 Stefan Pszczolkowski Parraguez
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_BSplineFreeFormTransformationStatistical_H
21 #define MIRTK_BSplineFreeFormTransformationStatistical_H
22 
23 #include "mirtk/BSplineFreeFormTransformation3D.h"
24 
25 #include "mirtk/Matrix.h"
26 #include "mirtk/Vector.h"
27 #include "mirtk/Vector3D.h"
28 
29 #include <string>
30 
31 
32 namespace mirtk {
33 
34 
35 /**
36  * Class for free-form transformations based on tensor product B-splines.
37  *
38  * This class implements 3D statistical free-form transformation using B-splines.
39  */
41 {
42  mirtkTransformationMacro(BSplineFreeFormTransformationStatistical);
43 
44  // ---------------------------------------------------------------------------
45  // Attributes
46 
47  /// Basis vectors (colums of the Matrix object)
48  mirtkReadOnlyAttributeMacro(Matrix, BasisVectors);
49 
50  /// Mean vector
51  mirtkReadOnlyAttributeMacro(mirtk::Vector, MeanVector);
52 
53  /// Name of file from which statistical deformation model was read
54  mirtkAttributeMacro(string, ModelFile);
55 
56 public:
57 
58  /// Default constructor
60 
61  /// Constructor based on a basis vectors matrix and a mean vector
63  CPStatus ****,
64  const Matrix &,
65  const Vector &);
66 
67  /// Copy Constructor
69 
70  /// Destructor
72 
73  // Import other Initialize overloads
75 
76  /// Initialize free-form transformation
77  virtual void Initialize(const ImageAttributes &);
78 
79  // ---------------------------------------------------------------------------
80  // Approximation/Interpolation
81 
82  /// Approximate displacements: This function takes a set of points and a set
83  /// of displacements and finds !new! parameters such that the resulting
84  /// transformation approximates the displacements as good as possible.
85  virtual void ApproximateDOFs(const double *, const double *, const double *, const double *,
86  const double *, const double *, const double *, int);
87 
88  /// Finds gradient of approximation error: This function takes a set of points
89  /// and a set of errors. It finds a gradient w.r.t. the transformation parameters
90  /// which minimizes the L2 norm of the approximation error and adds it to the
91  /// input gradient with the given weight.
92  virtual void ApproximateDOFsGradient(const double *, const double *, const double *, const double *,
93  const double *, const double *, const double *, int,
94  double *, double = 1.0) const;
95 
96  /// Interpolates displacements: This function takes a set of displacements defined
97  /// at the control points and finds a FFD which interpolates these displacements.
98  virtual void Interpolate(const double *, const double *, const double * = NULL);
99 
100  // ---------------------------------------------------------------------------
101  // Lattice
102 
104 
105  /// Crop/pad lattice to discard passive control points at the boundary,
106  /// keeping only a layer of passive control points of given width.
107  /// The DoF values of passive control points are optionally reset to zero.
108  virtual bool CropPadPassiveCPs(int, int, int = 0, int = 0, bool = false);
109 
110  // ---------------------------------------------------------------------------
111  // Transformation parameters (DOFs)
112 
113  /// Get norm of the gradient vector
114  virtual double DOFGradientNorm(const double *) const;
115 
116  /// Puts a transformation parameter
117  virtual void Put(int, DOFValue);
118 
119  /// Puts transformation parameters
120  virtual void Put(const DOFValue *);
121 
122  /// Add change to transformation parameters
123  virtual void Add(const DOFValue *);
124 
125  // ---------------------------------------------------------------------------
126  // Update
127 
128  /// Update control point displacements after change of parameters
129  void UpdateCPs();
130 
131  /// Update parameters after change of control point displacements
132  void UpdateDOFs();
133 
134  // ---------------------------------------------------------------------------
135  // Parameters (non-DoFs)
136 
138 
139  /// Set named (non-DoF) parameter from value as string
140  virtual bool Set(const char *, const char *);
141 
142  /// Get (non-DoF) parameters as key/value as string map
143  virtual ParameterList Parameter() const;
144 
145  // ---------------------------------------------------------------------------
146  // Derivatives
147 
148  /// Applies the chain rule to convert spatial non-parametric gradient
149  /// to a gradient w.r.t the parameters of this transformation
150  virtual void ParametricGradient(const GenericImage<double> *, double *,
151  const WorldCoordsImage *,
152  const WorldCoordsImage *,
153  double = 1, double = 1) const;
154 
155  // ---------------------------------------------------------------------------
156  // Properties
157 
158  /// Calculates the gradient of the bending energy w.r.t the transformation parameters
159  virtual void BendingEnergyGradient(double *, double = 1, bool = false, bool = true, bool = true) const;
160 
161  // ---------------------------------------------------------------------------
162  // I/O
163 
164  // Do not hide methods of base class
167 
168  /// Prints the parameters of the transformation
169  virtual void Print(ostream &, Indent = 0) const;
170 
171  /// Writes a transformation to a file stream
172  virtual Cofstream &Write(Cofstream &) const;
173 
174  /// Reads statistical deformation model from a file
175  virtual void ReadSDM(const char *);
176 
177  /// Writes statistical deformation model to a file
178  virtual void WriteSDM(const char *);
179 
180  // ---------------------------------------------------------------------------
181  // Other
182 
183  /// Verifies that the transformation is well constructed
184  /// according to class-specific rules
185  virtual void Verify();
186 
187 protected:
188 
189  /// Reads transformation parameters from a file stream
191 
192 };
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 // Inline definitions
196 ////////////////////////////////////////////////////////////////////////////////
197 
198 // =============================================================================
199 // Transformation parameters
200 // =============================================================================
201 
202 // -----------------------------------------------------------------------------
203 inline double BSplineFreeFormTransformationStatistical::DOFGradientNorm(const double *gradient) const
204 {
205  return Transformation::DOFGradientNorm(gradient);
206 }
207 
208 // -----------------------------------------------------------------------------
210 {
211  Transformation::Put(i, x);
212  this->UpdateCPs();
213 }
214 
215 // -----------------------------------------------------------------------------
217 {
219  this->UpdateCPs();
220 }
221 
222 // -----------------------------------------------------------------------------
224 {
226  this->UpdateCPs();
227 }
228 
229 
230 } // namespace mirtk
231 
232 #endif // MIRTK_BSplineFreeFormTransformationStatistical_H
virtual void Add(const DOFValue *)
Add change to transformation parameters.
bool CropPadPassiveCPs(int=0, bool=false)
virtual void ParametricGradient(const GenericImage< double > *, double *, const WorldCoordsImage *, const WorldCoordsImage *, double=1, double=1) const
virtual ParameterList Parameter() const
Get (non-DoF) parameters as key/value as string map.
double DOFValue
Type of transformation parameter value.
virtual void Interpolate(const double *, const double *, const double *=NULL)
virtual bool CropPadPassiveCPs(int, int, int=0, int=0, bool=false)
virtual void WriteSDM(const char *)
Writes statistical deformation model to a file.
virtual void Initialize(const ImageAttributes &)
Initialize free-form transformation.
Array< Pair< string, string > > ParameterList
Ordered list of parameter name/value pairs.
Definition: Object.h:38
virtual ParameterList Parameter() const
Get (non-DoF) parameters as key/value as string map.
virtual void Put(int, double)
Put value of transformation parameter.
virtual Cifstream & ReadDOFs(Cifstream &, TransformationType)
Reads transformation parameters from a file stream.
virtual void Write(const char *) const
Writes a transformation to a file.
virtual void Add(const DOFValue *)
Add change to transformation parameters.
Definition: IOConfig.h:41
void UpdateCPs()
Update control point displacements after change of parameters.
virtual double DOFGradientNorm(const double *) const
Get norm of the gradient vector.
virtual void ApproximateDOFsGradient(const double *, const double *, const double *, const double *, const double *, const double *, const double *, int, double *, double=1.0) const
virtual ~BSplineFreeFormTransformationStatistical()
Destructor.
virtual void Put(int, DOFValue)
Puts a transformation parameter.
virtual bool Set(const char *, const char *)
Set named (non-DoF) parameter from value as string.
virtual double DOFGradientNorm(const double *) const
Get norm of the gradient vector.
virtual void Print(ostream &, Indent=0) const
Prints the parameters of the transformation.
void UpdateDOFs()
Update parameters after change of control point displacements.
virtual void ApproximateDOFs(const double *, const double *, const double *, const double *, const double *, const double *, const double *, int)
virtual void Print(ostream &, Indent=0) const
Prints the parameters of the transformation.
virtual void ReadSDM(const char *)
Reads statistical deformation model from a file.
virtual Cofstream & Write(Cofstream &) const
Writes a transformation to a file stream.
BSplineFreeFormTransformationStatistical()
Default constructor.
virtual void Initialize(const ImageAttributes &)
Initialize free-form transformation.
virtual void BendingEnergyGradient(double *, double=1, bool=false, bool=true, bool=true) const
Calculates the gradient of the bending energy w.r.t the transformation parameters.