FastLinearImageGradientFunction.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_FastLinearImageGradientFunction_H
21 #define MIRTK_FastLinearImageGradientFunction_H
22 
23 #include "mirtk/BaseImage.h"
24 #include "mirtk/ImageGradientFunction.h"
25 
26 
27 namespace mirtk {
28 
29 
30 /**
31  * Fast linear interpolation of generic image gradient
32  *
33  * This image gradient evaluation function approximates the image gradient
34  * using the derivative of the linear interpolation kernel. The difference
35  * to the GenericLinearImageGradientFunction is that the resulting finite
36  * differences are not necessarily central. In the extreme case, where the
37  * gradient is evaluated at a voxel center, the gradient computation
38  * corresponds to a forward difference scheme. When the evaluation point is in
39  * between two voxel centers, however, the resulting gradient interpolation is
40  * a central difference. Side effects resulting from this are generally only
41  * noticeable at coarse image resolutions.
42  */
43 template <class TImage>
45 : public GenericImageGradientFunction<TImage>
46 {
47  mirtkGenericGradientInterpolatorMacro(
49  Interpolation_FastLinear
50  );
51 
52 public:
53 
54  // ---------------------------------------------------------------------------
55  // Construction/Destruction
56 
57  /// Default constructor
59 
60  /// Destructor
62 
63  /// Initialize interpolation function
64  virtual void Initialize(bool = false);
65 
66  // ---------------------------------------------------------------------------
67  // Domain checks
68 
69  /// Returns interval of discrete image indices whose values are needed for
70  /// interpolation of the image value at a given continuous coordinate
71  virtual void BoundingInterval(double, int &, int &) const;
72 
73  // ---------------------------------------------------------------------------
74  // Linear interpolation weights
75 
76  /// Returns truncated integral part of floating point
77  ///
78  /// \param[in] x Floating point number.
79  /// \param[out] w Linear interpolation weights for closest lattice nodes.
80  static int ComputeWeights(double, Real[2]);
81 
82  // ---------------------------------------------------------------------------
83  // Evaluation
84 
85  /// Get gradient of given 2D image at arbitrary location (in pixels)
86  ///
87  /// This function is used to interpolate the image gradient at arbitrary
88  /// locations when no extrapolator was set.
89  GradientType Get2D(double, double, double = 0, double = 0) const;
90 
91  /// Get gradient of given 2D image at arbitrary location (in pixels)
92  ///
93  /// This function is used to only interpolate foreground image values.
94  /// If fully outside the foreground region, the _DefaultValue is returned.
95  GradientType GetWithPadding2D(double, double, double = 0, double = 0) const;
96 
97  /// Get gradient of given 2D image at arbitrary location (in pixels)
98  ///
99  /// If the location is inside the finite domain of the image, an actual image
100  /// instance can be passed as first argument directly such as an instance of
101  /// GenericImage. Otherwise, an image function which extends the finite
102  /// image domain to an infinite lattice is needed, i.e., an instance of a
103  /// subclass of ExtrapolateImageFunction.
104  template <class TOtherImage>
105  GradientType Get2D(const TOtherImage *, double, double, double = 0, double = 0) const;
106 
107  /// Get gradient of given 2D image at arbitrary location (in pixels)
108  ///
109  /// This function is used to only interpolate foreground image values.
110  /// If fully outside the foreground region, the _DefaultValue is returned.
111  ///
112  /// If the location is inside the finite domain of the image, an actual image
113  /// instance can be passed as first argument directly such as an instance of
114  /// GenericImage. Otherwise, an image function which extends the finite
115  /// image domain to an infinite lattice is needed, i.e., an instance of a
116  /// subclass of ExtrapolateImageFunction.
117  template <class TOtherImage>
118  GradientType GetWithPadding2D(const TOtherImage *, double, double, double = 0, double = 0) const;
119 
120  /// Get gradient of given 3D image at arbitrary location (in pixels)
121  ///
122  /// This function is used to interpolate the image value at arbitrary
123  /// locations when no extrapolator was set.
124  GradientType Get3D(double, double, double = 0, double = 0) const;
125 
126  /// Get gradient of given 3D image at arbitrary location (in pixels)
127  ///
128  /// This function is used to only interpolate foreground image values.
129  /// If fully outside the foreground region, the _DefaultValue is returned.
130  GradientType GetWithPadding3D(double, double, double = 0, double = 0) const;
131 
132  /// Get gradient of given 3D image at arbitrary location (in pixels)
133  ///
134  /// If the location is inside the finite domain of the image, an actual image
135  /// instance can be passed as first argument directly such as an instance of
136  /// GenericImage. Otherwise, an image function which extends the finite
137  /// image domain to an infinite lattice is needed, i.e., an instance of a
138  /// subclass of ExtrapolateImageFunction.
139  template <class TOtherImage>
140  GradientType Get3D(const TOtherImage *, double, double, double = 0, double = 0) const;
141 
142  /// Get gradient of given 3D image at arbitrary location (in pixels)
143  ///
144  /// This function is used to only interpolate foreground image values.
145  /// If fully outside the foreground region, the _DefaultValue is returned.
146  ///
147  /// If the location is inside the finite domain of the image, an actual image
148  /// instance can be passed as first argument directly such as an instance of
149  /// GenericImage. Otherwise, an image function which extends the finite
150  /// image domain to an infinite lattice is needed, i.e., an instance of a
151  /// subclass of ExtrapolateImageFunction.
152  template <class TOtherImage>
153  GradientType GetWithPadding3D(const TOtherImage *, double, double, double = 0, double = 0) const;
154 
155  /// Get gradient of given 3D image at arbitrary location (in pixels)
156  ///
157  /// This function is used to interpolate the image value at arbitrary
158  /// locations when no extrapolator was set.
159  GradientType Get4D(double, double, double = 0, double = 0) const;
160 
161  /// Get gradient of given 4D image at arbitrary location (in pixels)
162  ///
163  /// This function is used to only interpolate foreground image values.
164  /// If fully outside the foreground region, the _DefaultValue is returned.
165  GradientType GetWithPadding4D(double, double, double = 0, double = 0) const;
166 
167  /// Get gradient of given 4D image at arbitrary location (in pixels)
168  ///
169  /// If the location is inside the finite domain of the image, an actual image
170  /// instance can be passed as first argument directly such as an instance of
171  /// GenericImage. Otherwise, an image function which extends the finite
172  /// image domain to an infinite lattice is needed, i.e., an instance of a
173  /// subclass of ExtrapolateImageFunction.
174  template <class TOtherImage>
175  GradientType Get4D(const TOtherImage *, double, double, double = 0, double = 0) const;
176 
177  /// Get gradient of given 4D image at arbitrary location (in pixels)
178  ///
179  /// This function is used to only interpolate foreground image values.
180  /// If fully outside the foreground region, the _DefaultValue is returned.
181  ///
182  /// If the location is inside the finite domain of the image, an actual image
183  /// instance can be passed as first argument directly such as an instance of
184  /// GenericImage. Otherwise, an image function which extends the finite
185  /// image domain to an infinite lattice is needed, i.e., an instance of a
186  /// subclass of ExtrapolateImageFunction.
187  template <class TOtherImage>
188  GradientType GetWithPadding4D(const TOtherImage *, double, double, double = 0, double = 0) const;
189 
190  /// Get gradient of given image at arbitrary location (in pixels)
191  ///
192  /// This function is used to interpolate the image value at arbitrary
193  /// locations when no extrapolator was set.
194  virtual GradientType Get(double, double, double = 0, double = 0) const;
195 
196  /// Get value of given image at arbitrary location (in pixels)
197  ///
198  /// This function is used to only interpolate foreground image values.
199  /// If fully outside the foreground region, the _DefaultValue is returned.
200  virtual GradientType GetWithPadding(double, double, double = 0, double = 0) const;
201 
202  /// Get gradient of given image at arbitrary location (in pixels)
203  ///
204  /// If the location is inside the finite domain of the image, an actual image
205  /// instance can be passed as first argument directly such as an instance of
206  /// GenericImage. Otherwise, an image function which extends the finite
207  /// image domain to an infinite lattice is needed, i.e., an instance of a
208  /// subclass of ExtrapolateImageFunction.
209  template <class TOtherImage>
210  GradientType Get(const TOtherImage *, double, double, double = 0, double = 0) const;
211 
212  /// Get gradient of given image at arbitrary location (in pixels)
213  ///
214  /// This function is used to only interpolate foreground image values.
215  /// If fully outside the foreground region, the _DefaultValue is returned.
216  ///
217  /// If the location is inside the finite domain of the image, an actual image
218  /// instance can be passed as first argument directly such as an instance of
219  /// GenericImage. Otherwise, an image function which extends the finite
220  /// image domain to an infinite lattice is needed, i.e., an instance of a
221  /// subclass of ExtrapolateImageFunction.
222  template <class TOtherImage>
223  GradientType GetWithPadding(const TOtherImage *, double, double, double = 0, double = 0) const;
224 
225  /// Evaluate generic image gradient without handling boundary conditions
226  ///
227  /// This version is faster than EvaluateOutside, but is only defined inside
228  /// the domain for which all image values required for interpolation are
229  /// defined and thus require no extrapolation of the finite image.
230  virtual GradientType GetInside(double, double, double = 0, double = 0) const;
231 
232  /// Evaluate generic image at an arbitrary location (in pixels)
233  virtual GradientType GetOutside(double, double, double = 0, double = 0) const;
234 
235  /// Evaluate generic image gradient without handling boundary conditions
236  ///
237  /// If the location is partially inside the foreground region of the image,
238  /// only the foreground values are interpolated. Otherwise, the _DefaultValue
239  /// is returned.
240  ///
241  /// This version is faster than GetWithPaddingOutside, but is only defined
242  /// inside the domain for which all image values required for interpolation
243  /// are defined and thus require no extrapolation of the finite image.
244  virtual GradientType GetWithPaddingInside(double, double, double = 0, double = 0) const;
245 
246  /// Evaluate generic image gradient at an arbitrary location (in pixels)
247  ///
248  /// If the location is partially inside the foreground region of the image,
249  /// only the foreground values are interpolated. Otherwise, the _DefaultValue
250  /// is returned.
251  virtual GradientType GetWithPaddingOutside(double, double, double = 0, double = 0) const;
252 
253 };
254 
255 /**
256  * Fast linear interpolation of any scalar image gradient
257  */
259 : public GenericFastLinearImageGradientFunction<BaseImage>
260 {
261  mirtkObjectMacro(FastLinearImageGradientFunction);
262 
263 public:
264 
265  /// Constructor
267 
268 };
269 
270 
271 } // namespace mirtk
272 
273 #endif // MIRTK_FastLinearImageGradientFunction_H
virtual GradientType GetWithPadding(double, double, double=0, double=0) const
GradientType Get2D(double, double, double=0, double=0) const
GradientType Get3D(double, double, double=0, double=0) const
virtual GradientType GetOutside(double, double, double=0, double=0) const
Evaluate generic image at an arbitrary location (in pixels)
virtual GradientType GetWithPaddingInside(double, double, double=0, double=0) const
virtual void Initialize(bool=false)
Initialize interpolation function.
GradientType GetWithPadding3D(double, double, double=0, double=0) const
Definition: IOConfig.h:41
virtual void BoundingInterval(double, int &, int &) const
GradientType GetWithPadding4D(double, double, double=0, double=0) const
virtual GradientType Get(double, double, double=0, double=0) const
GradientType GetWithPadding2D(double, double, double=0, double=0) const
GradientType Get4D(double, double, double=0, double=0) const
virtual GradientType GetInside(double, double, double=0, double=0) const
virtual GradientType GetWithPaddingOutside(double, double, double=0, double=0) const