InterpolateImageFunction.hxx
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_InterpolateImageFunction_HXX
21 #define MIRTK_InterpolateImageFunction_HXX
22 
23 #include "mirtk/InterpolateImageFunction.h"
24 
25 #include "mirtk/Voxel.h"
26 
27 
28 namespace mirtk {
29 
30 
31 ////////////////////////////////////////////////////////////////////////////////
32 // GenericInterpolateImageFunction
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 // =============================================================================
36 // Construction/Destruction
37 // =============================================================================
38 
39 // -----------------------------------------------------------------------------
40 template <class TImage>
43 {
44 }
45 
46 // -----------------------------------------------------------------------------
47 template <class TImage>
50 {
51 }
52 
53 // -----------------------------------------------------------------------------
54 template <class TImage>
57 ::New(enum ExtrapolationMode mode, const BaseImage *image)
58 {
59  const TImage *img = NULL;
60  if (image) {
61  img = dynamic_cast<const TImage *>(image);
62  if (!img) {
63  cerr << this->NameOfClass() << "::New(irtkExtrapolationMode): Invalid input image type" << endl;
64  exit(1);
65  }
66  }
67  return ExtrapolatorType::New(mode, img);
68 }
69 
70 // =============================================================================
71 // Initialization
72 // =============================================================================
73 
74 // -----------------------------------------------------------------------------
75 template <class TImage>
77 {
78  InterpolateImageFunction::Input(dynamic_cast<const TImage *>(input));
79  if (input && !this->_Input) {
80  cerr << this->NameOfClass() << "::Input: Invalid input image type" << endl;
81  exit(1);
82  }
83 }
84 
85 // -----------------------------------------------------------------------------
86 template <class TImage>
88 {
89  // Ensure that input has the right type
90  if (!this->_Input) {
91  cerr << this->NameOfClass() << "::Initialize: Missing input image" << endl;
92  exit(1);
93  } else if (!dynamic_cast<const TImage *>(this->_Input)) {
94  cerr << this->NameOfClass() << "::Initialize: Invalid input image type" << endl;
95  exit(1);
96  }
97  // Initialize extrapolator, i.e., infinite discrete image
98  if (this->_InfiniteInput) {
99  if (!dynamic_cast<const ExtrapolatorType *>(this->_InfiniteInput)) {
100  cerr << this->NameOfClass() << "::Initialize: Invalid extrapolator type" << endl;
101  exit(1);
102  }
103  }
104  // Initialize base class
106 }
107 
108 // -----------------------------------------------------------------------------
109 template <class TImage>
112 {
114  if (input && !dynamic_cast<const ExtrapolatorType *>(this->_InfiniteInput)) {
115  cerr << this->NameOfClass() << "::Extrapolator: Invalid extrapolator type" << endl;
116  exit(1);
117  }
118 }
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 // Explicit instantiation macro
122 ////////////////////////////////////////////////////////////////////////////////
123 
124 // -----------------------------------------------------------------------------
125 #define mirtkInterpolatorInstantiations(clsname) \
126  template class clsname<mirtk::BaseImage>; \
127  template class clsname<mirtk::GenericImage<mirtk::GreyPixel> >; \
128  template class clsname<mirtk::GenericImage<float> >; \
129  template class clsname<mirtk::GenericImage<float2> >; \
130  template class clsname<mirtk::GenericImage<float3> >; \
131  template class clsname<mirtk::GenericImage<mirtk::Float3> >; \
132  template class clsname<mirtk::GenericImage<float3x3> >; \
133  template class clsname<mirtk::GenericImage<double> >; \
134  template class clsname<mirtk::GenericImage<double2> >; \
135  template class clsname<mirtk::GenericImage<double3> >; \
136  template class clsname<mirtk::GenericImage<mirtk::Double3> >; \
137  template class clsname<mirtk::GenericImage<double3x3> >
138 
139 
140 } // namespace mirtk
141 
142 #endif // MIRTK_InterpolateImageFunction_HXX
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 // Instantiation
146 ////////////////////////////////////////////////////////////////////////////////
147 
148 #ifndef MIRTK_InterpolateImageFunctionNew_HXX
149 #define MIRTK_InterpolateImageFunctionNew_HXX
150 
151 // ND
152 #include "mirtk/NearestNeighborInterpolateImageFunction.h"
153 #include "mirtk/LinearInterpolateImageFunction.h"
154 #include "mirtk/BSplineInterpolateImageFunction.h"
155 #include "mirtk/CubicBSplineInterpolateImageFunction.h"
156 #include "mirtk/FastCubicBSplineInterpolateImageFunction.h"
157 #include "mirtk/CSplineInterpolateImageFunction.h"
158 #include "mirtk/GaussianInterpolateImageFunction.h"
159 #include "mirtk/SincInterpolateImageFunction.h"
160 
161 // 2D
162 #include "mirtk/LinearInterpolateImageFunction2D.h"
163 #include "mirtk/BSplineInterpolateImageFunction2D.h"
164 #include "mirtk/CubicBSplineInterpolateImageFunction2D.h"
165 #include "mirtk/FastCubicBSplineInterpolateImageFunction2D.h"
166 #include "mirtk/CSplineInterpolateImageFunction2D.h"
167 #include "mirtk/GaussianInterpolateImageFunction2D.h"
168 #include "mirtk/SincInterpolateImageFunction2D.h"
169 
170 // 3D
171 #include "mirtk/LinearInterpolateImageFunction3D.h"
172 #include "mirtk/BSplineInterpolateImageFunction3D.h"
173 #include "mirtk/CubicBSplineInterpolateImageFunction3D.h"
174 #include "mirtk/FastCubicBSplineInterpolateImageFunction3D.h"
175 #include "mirtk/CSplineInterpolateImageFunction3D.h"
176 #include "mirtk/GaussianInterpolateImageFunction3D.h"
177 #include "mirtk/SincInterpolateImageFunction3D.h"
178 
179 #include "mirtk/ShapeBasedInterpolateImageFunction.h" // 3D scalar image only
180 
181 // 4D
182 #include "mirtk/LinearInterpolateImageFunction4D.h"
183 #include "mirtk/BSplineInterpolateImageFunction4D.h"
184 #include "mirtk/CubicBSplineInterpolateImageFunction4D.h"
185 #include "mirtk/FastCubicBSplineInterpolateImageFunction4D.h"
186 #include "mirtk/CSplineInterpolateImageFunction4D.h"
187 #include "mirtk/GaussianInterpolateImageFunction4D.h"
188 #include "mirtk/SincInterpolateImageFunction4D.h"
189 
190 
191 namespace mirtk {
192 
193 
194 // -----------------------------------------------------------------------------
195 template <class TImage>
198 ::New(enum InterpolationMode mode, const TImage *image)
199 {
200  mode = InterpolationWithoutPadding(mode);
201 
203 
204  int dim = 0;
205  if (image) {
206  if (image->Z() == 1) dim = 2;
207  else if (image->T() == 1 || image->GetTSize() == .0) dim = 3;
208  else dim = 4;
209  }
210 
211  switch (dim) {
212  case 2: {
213  switch (mode) {
214  case Interpolation_NN:
216  break;
217  case Interpolation_Linear:
218  case Interpolation_FastLinear:
220  break;
221  case Interpolation_BSpline:
223  break;
224  case Interpolation_CubicBSpline:
226  break;
227  case Interpolation_FastCubicBSpline:
229  break;
230  case Interpolation_CSpline:
232  break;
233  case Interpolation_Gaussian:
235  break;
236  case Interpolation_Sinc:
238  break;
239  default:
240  p = NULL;
241  }
242  }
243  case 3: {
244  switch (mode) {
245  case Interpolation_NN:
247  break;
248  case Interpolation_Linear:
249  case Interpolation_FastLinear:
251  break;
252  case Interpolation_BSpline:
254  break;
255  case Interpolation_CubicBSpline:
257  break;
258  case Interpolation_FastCubicBSpline:
260  break;
261  case Interpolation_CSpline:
263  break;
264  case Interpolation_Gaussian:
266  break;
267  case Interpolation_Sinc:
269  break;
270  default:
271  p = NULL;
272  }
273  }
274  case 4: {
275  switch (mode) {
276  case Interpolation_NN:
278  break;
279  case Interpolation_Linear:
280  case Interpolation_FastLinear:
282  break;
283  case Interpolation_BSpline:
285  break;
286  case Interpolation_CubicBSpline:
288  break;
289  case Interpolation_FastCubicBSpline:
291  break;
292  case Interpolation_CSpline:
294  break;
295  case Interpolation_Gaussian:
297  break;
298  case Interpolation_Sinc:
300  break;
301  default:
302  p = NULL;
303  }
304  }
305  default: {
306  switch (mode) {
307  case Interpolation_NN:
309  break;
310  case Interpolation_Linear:
311  case Interpolation_FastLinear:
313  break;
314  case Interpolation_BSpline:
316  break;
317  case Interpolation_CubicBSpline:
319  break;
320  case Interpolation_FastCubicBSpline:
322  break;
323  case Interpolation_CSpline:
325  break;
326  case Interpolation_Gaussian:
328  break;
329  case Interpolation_Sinc:
331  break;
332  default:
333  p = NULL;
334  }
335  }
336  }
337 
338  // Initialize interpolator
339  if (p) {
340  p->NumberOfDimensions(dim);
341  p->Input(image);
342  // Throw error if no suitable interpolator available
343  } else {
344  cerr << "GenericInterpolateImageFunction::New: Interpolation mode (" << mode;
345  cerr << ") not supported for " << (dim ? ToString(dim) : "N") << "D images" << endl;
346  exit(1);
347  }
348 
349  return p;
350 }
351 
352 // -----------------------------------------------------------------------------
353 template <class TImage>
356 ::New(enum InterpolationMode imode, enum ExtrapolationMode emode, const TImage *image)
357 {
360  if (emode != Extrapolation_Default) p->Extrapolator(p->New(emode, image), true);
361  return p;
362 }
363 
364 
365 } // namespace mirtk
366 
367 #endif // MIRTK_InterpolateImageFunctionNew_HXX
const ImageType * Input() const
Get input image.
virtual void Extrapolator(ExtrapolateImageFunction *, bool=false)
Set extrapolate image function for evaluation outside of image domain.
InterpolationMode
Image interpolation modes.
Definition: IOConfig.h:41
InterpolationMode InterpolationWithoutPadding(InterpolationMode m)
Get corresponding interpolation without padding.
static GenericInterpolateImageFunction * New(enum InterpolationMode=Interpolation_Default, const TImage *=NULL)
Construct interpolator with default infinite extension of input image.
string ToString(const EnergyMeasure &value, int w, char c, bool left)
Convert energy measure enumeration value to string.
ExtrapolationMode
Image extrapolation modes.
ExtrapolateImageFunction * Extrapolator()
virtual void Input(const BaseImage *)
Set input image.
const BaseImage * Input() const
Get input image.