ImageGradientFunction.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_ImageGradientFunction_HXX
21 #define MIRTK_ImageGradientFunction_HXX
22 
23 #include "mirtk/ImageGradientFunction.h"
24 
25 
26 namespace mirtk {
27 
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 // GenericInterpolateImageFunction
31 ////////////////////////////////////////////////////////////////////////////////
32 
33 // =============================================================================
34 // Construction/Destruction
35 // =============================================================================
36 
37 // -----------------------------------------------------------------------------
38 template <class TImage>
41 {
42 }
43 
44 // -----------------------------------------------------------------------------
45 template <class TImage>
48 {
49 }
50 
51 // -----------------------------------------------------------------------------
52 template <class TImage>
55 ::New(enum ExtrapolationMode mode, const BaseImage *image)
56 {
57  const TImage *img = NULL;
58  if (image) {
59  img = dynamic_cast<const TImage *>(image);
60  if (!img) {
61  cerr << this->NameOfClass() << "::New(irtkExtrapolationMode): Invalid input image type" << endl;
62  exit(1);
63  }
64  }
65  return ExtrapolatorType::New(mode, img);
66 }
67 
68 // =============================================================================
69 // Initialization
70 // =============================================================================
71 
72 // -----------------------------------------------------------------------------
73 template <class TImage>
75 {
76  ImageGradientFunction::Input(dynamic_cast<const TImage *>(input));
77  if (input && !this->_Input) {
78  cerr << this->NameOfClass() << "::Input: Invalid input image type" << endl;
79  exit(1);
80  }
81 }
82 
83 // -----------------------------------------------------------------------------
84 template <class TImage>
86 {
87  // Ensure that input has the right type
88  if (!this->_Input) {
89  cerr << this->NameOfClass() << "::Initialize: Missing input image" << endl;
90  exit(1);
91  } else if (!dynamic_cast<const TImage *>(this->_Input)) {
92  cerr << this->NameOfClass() << "::Initialize: Invalid input image type" << endl;
93  exit(1);
94  }
95  // Initialize extrapolator, i.e., infinite discrete image
96  if (this->_InfiniteInput) {
97  if (!dynamic_cast<const ExtrapolatorType *>(this->_InfiniteInput)) {
98  cerr << this->NameOfClass() << "::Initialize: Invalid extrapolator type" << endl;
99  exit(1);
100  }
101  }
102  // Initialize base class
104 }
105 
106 // -----------------------------------------------------------------------------
107 template <class TImage>
110 {
112  if (input && !dynamic_cast<const ExtrapolatorType *>(this->_InfiniteInput)) {
113  cerr << this->NameOfClass() << "::Extrapolator: Invalid extrapolator type" << endl;
114  exit(1);
115  }
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 // Explicit template instantiations
120 ////////////////////////////////////////////////////////////////////////////////
121 
122 // -----------------------------------------------------------------------------
123 #define mirtkGradientInterpolatorInstantiations(clsname) \
124  template class clsname<mirtk::BaseImage>; \
125  template class clsname<mirtk::GenericImage<mirtk::GreyPixel> >; \
126  template class clsname<mirtk::GenericImage<float> >; \
127  template class clsname<mirtk::GenericImage<double> >
128 
129 
130 } // namespace mirtk
131 
132 #endif // MIRTK_ImageGradientFunction_HXX
133 
134 ////////////////////////////////////////////////////////////////////////////////
135 // Instantiation
136 ////////////////////////////////////////////////////////////////////////////////
137 
138 #ifndef MIRTK_ImageGradientFunctionNew_HXX
139 #define MIRTK_ImageGradientFunctionNew_HXX
140 
141 #include "mirtk/LinearImageGradientFunction.h"
142 #include "mirtk/LinearImageGradientFunction2D.h"
143 #include "mirtk/LinearImageGradientFunction3D.h"
144 
145 #include "mirtk/FastLinearImageGradientFunction.h"
146 #include "mirtk/FastLinearImageGradientFunction2D.h"
147 #include "mirtk/FastLinearImageGradientFunction3D.h"
148 
149 
150 namespace mirtk {
151 
152 
153 // -----------------------------------------------------------------------------
154 template <class TImage>
157 ::New(enum InterpolationMode mode, const TImage *image)
158 {
159  mode = InterpolationWithoutPadding(mode);
160 
162 
163  int dim = 0;
164  if (image) {
165  if (image->Z() == 1) dim = 2;
166  else if (image->T() == 1 || image->GetTSize() == .0) dim = 3;
167  else dim = 4;
168  }
169 
170  switch (dim) {
171  case 2: {
172  switch (mode) {
173  case Interpolation_Linear:
175  break;
176  case Interpolation_FastLinear:
178  break;
179  default:
180  p = NULL;
181  }
182  }
183  case 3: {
184  switch (mode) {
185  case Interpolation_Linear:
187  break;
188  case Interpolation_FastLinear:
190  break;
191  default:
192  p = NULL;
193  }
194  }
195  default: {
196  switch (mode) {
197  case Interpolation_Linear:
199  break;
200  case Interpolation_FastLinear:
202  break;
203  default:
204  p = NULL;
205  }
206  }
207  }
208 
209  // Initialize interpolator
210  if (p) {
211  p->NumberOfDimensions(dim);
212  p->Input(image);
213  // Throw error if no suitable interpolator available
214  } else {
215  cerr << "GenericImageGradientFunction::New: Interpolation mode (" << mode;
216  cerr << ") not supported for " << (dim ? ToString(dim) : "N") << "D images" << endl;
217  exit(1);
218  }
219 
220  return p;
221 }
222 
223 // -----------------------------------------------------------------------------
224 template <class TImage>
227 ::New(enum InterpolationMode imode, enum ExtrapolationMode emode, const TImage *image)
228 {
231  if (emode != Extrapolation_Default) p->Extrapolator(p->New(emode, image), true);
232  return p;
233 }
234 
235 
236 } // namespace mirtk
237 
238 #endif // MIRTK_ImageGradientFunctionNew_HXX
virtual ~GenericImageGradientFunction()
Destructor.
const ImageType * Input() const
Get input image.
virtual void Input(const BaseImage *)
Set input image.
virtual void Initialize(bool coeff=false)
ExtrapolateImageFunction * Extrapolator()
InterpolationMode
Image interpolation modes.
static GenericImageGradientFunction * New(enum InterpolationMode, const TImage *=NULL)
Construct interpolator with default infinite extension of input image.
GenericImageGradientFunction()
Default constructor.
Definition: IOConfig.h:41
virtual void Extrapolator(ExtrapolateImageFunction *, bool=false)
Set extrapolate image function for evaluation outside of image domain.
InterpolationMode InterpolationWithoutPadding(InterpolationMode m)
Get corresponding interpolation without padding.
string ToString(const EnergyMeasure &value, int w, char c, bool left)
Convert energy measure enumeration value to string.
ExtrapolationMode
Image extrapolation modes.
virtual void Initialize(bool coeff=false)
const BaseImage * Input() const
Get input image.