ITK  6.0.0
Insight Toolkit
itkLinearInterpolateImageFunction.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkLinearInterpolateImageFunction_h
19#define itkLinearInterpolateImageFunction_h
20
23#include <algorithm> // For max.
24
25namespace itk
26{
50template <typename TInputImage, typename TCoordinate = double>
51class ITK_TEMPLATE_EXPORT LinearInterpolateImageFunction : public InterpolateImageFunction<TInputImage, TCoordinate>
52{
53public:
54 ITK_DISALLOW_COPY_AND_MOVE(LinearInterpolateImageFunction);
55
61
63 itkOverrideGetNameOfClassMacro(LinearInterpolateImageFunction);
64
66 itkNewMacro(Self);
67
69 using typename Superclass::OutputType;
70
72 using typename Superclass::InputImageType;
73
75 using typename Superclass::InputPixelType;
76
78 using typename Superclass::RealType;
79
81 static constexpr unsigned int ImageDimension = Superclass::ImageDimension;
82
84 using typename Superclass::IndexType;
85
87 using typename Superclass::SizeType;
88
90 using typename Superclass::ContinuousIndexType;
92
102 EvaluateAtContinuousIndex(const ContinuousIndexType & index) const override
103 {
104 return this->EvaluateOptimized(Dispatch<ImageDimension>(), index);
105 }
106
108 GetRadius() const override
109 {
110 return SizeType::Filled(1);
111 }
112
113protected:
116 void
117 PrintSelf(std::ostream & os, Indent indent) const override;
118
119private:
121 {};
122 template <unsigned int>
123 struct Dispatch : public DispatchBase
124 {};
125
126 inline OutputType
128 {
129 return 0;
130 }
131
132 inline OutputType
134 {
135 IndexType basei;
136 basei[0] = std::max(Math::Floor<IndexValueType>(index[0]), this->m_StartIndex[0]);
137
138 const InternalComputationType distance = index[0] - static_cast<InternalComputationType>(basei[0]);
139
140 const TInputImage * const inputImagePtr = this->GetInputImage();
141 const RealType & val0 = inputImagePtr->GetPixel(basei);
142 if (distance <= 0.)
143 {
144 return (static_cast<OutputType>(val0));
145 }
146
147 ++basei[0];
148 if (basei[0] > this->m_EndIndex[0])
149 {
150 return (static_cast<OutputType>(val0));
151 }
152 const RealType & val1 = inputImagePtr->GetPixel(basei);
153
154 return (static_cast<OutputType>(val0 + (val1 - val0) * distance));
155 }
156
157 inline OutputType
159 {
160 IndexType basei;
161
162 basei[0] = std::max(Math::Floor<IndexValueType>(index[0]), this->m_StartIndex[0]);
163 const InternalComputationType distance0 = index[0] - static_cast<InternalComputationType>(basei[0]);
164
165 basei[1] = std::max(Math::Floor<IndexValueType>(index[1]), this->m_StartIndex[1]);
166 const InternalComputationType distance1 = index[1] - static_cast<InternalComputationType>(basei[1]);
167
168 const TInputImage * const inputImagePtr = this->GetInputImage();
169 const RealType & val00 = inputImagePtr->GetPixel(basei);
170 if (distance0 <= 0. && distance1 <= 0.)
171 {
172 return (static_cast<OutputType>(val00));
173 }
174 if (distance1 <= 0.) // if they have the same "y"
175 {
176 ++basei[0]; // then interpolate across "x"
177 if (basei[0] > this->m_EndIndex[0])
178 {
179 return (static_cast<OutputType>(val00));
180 }
181 const RealType & val10 = inputImagePtr->GetPixel(basei);
182 return (static_cast<OutputType>(val00 + (val10 - val00) * distance0));
183 }
184 else if (distance0 <= 0.) // if they have the same "x"
185 {
186 ++basei[1]; // then interpolate across "y"
187 if (basei[1] > this->m_EndIndex[1])
188 {
189 return (static_cast<OutputType>(val00));
190 }
191 const RealType & val01 = inputImagePtr->GetPixel(basei);
192 return (static_cast<OutputType>(val00 + (val01 - val00) * distance1));
193 }
194 // fall-through case:
195 // interpolate across "xy"
196 ++basei[0];
197 if (basei[0] > this->m_EndIndex[0]) // interpolate across "y"
198 {
199 --basei[0];
200 ++basei[1];
201 if (basei[1] > this->m_EndIndex[1])
202 {
203 return (static_cast<OutputType>(val00));
204 }
205 const RealType & val01 = inputImagePtr->GetPixel(basei);
206 return (static_cast<OutputType>(val00 + (val01 - val00) * distance1));
207 }
208 const RealType & val10 = inputImagePtr->GetPixel(basei);
209
210 const RealType & valx0 = val00 + (val10 - val00) * distance0;
211
212 ++basei[1];
213 if (basei[1] > this->m_EndIndex[1]) // interpolate across "x"
214 {
215 return (static_cast<OutputType>(valx0));
216 }
217 const RealType & val11 = inputImagePtr->GetPixel(basei);
218 --basei[0];
219 const RealType & val01 = inputImagePtr->GetPixel(basei);
220
221 const RealType & valx1 = val01 + (val11 - val01) * distance0;
222
223 return (static_cast<OutputType>(valx0 + (valx1 - valx0) * distance1));
224 }
225
226 inline OutputType
228 {
229 IndexType basei;
230 basei[0] = std::max(Math::Floor<IndexValueType>(index[0]), this->m_StartIndex[0]);
231 const InternalComputationType distance0 = index[0] - static_cast<InternalComputationType>(basei[0]);
232
233 basei[1] = std::max(Math::Floor<IndexValueType>(index[1]), this->m_StartIndex[1]);
234 const InternalComputationType distance1 = index[1] - static_cast<InternalComputationType>(basei[1]);
235
236 basei[2] = std::max(Math::Floor<IndexValueType>(index[2]), this->m_StartIndex[2]);
237 const InternalComputationType distance2 = index[2] - static_cast<InternalComputationType>(basei[2]);
238
239 const TInputImage * const inputImagePtr = this->GetInputImage();
240 const RealType & val000 = inputImagePtr->GetPixel(basei);
241 if (distance0 <= 0. && distance1 <= 0. && distance2 <= 0.)
242 {
243 return (static_cast<OutputType>(val000));
244 }
245
246 if (distance2 <= 0.)
247 {
248 if (distance1 <= 0.) // interpolate across "x"
249 {
250 ++basei[0];
251 if (basei[0] > this->m_EndIndex[0])
252 {
253 return (static_cast<OutputType>(val000));
254 }
255 const RealType & val100 = inputImagePtr->GetPixel(basei);
256
257 return static_cast<OutputType>(val000 + (val100 - val000) * distance0);
258 }
259 if (distance0 <= 0.) // interpolate across "y"
260 {
261 ++basei[1];
262 if (basei[1] > this->m_EndIndex[1])
263 {
264 return (static_cast<OutputType>(val000));
265 }
266 const RealType & val010 = inputImagePtr->GetPixel(basei);
267
268 return static_cast<OutputType>(val000 + (val010 - val000) * distance1);
269 }
270 else // interpolate across "xy"
271 {
272 ++basei[0];
273 if (basei[0] > this->m_EndIndex[0]) // interpolate across "y"
274 {
275 --basei[0];
276 ++basei[1];
277 if (basei[1] > this->m_EndIndex[1])
278 {
279 return (static_cast<OutputType>(val000));
280 }
281 const RealType & val010 = inputImagePtr->GetPixel(basei);
282 return static_cast<OutputType>(val000 + (val010 - val000) * distance1);
283 }
284 const RealType & val100 = inputImagePtr->GetPixel(basei);
285 const RealType & valx00 = val000 + (val100 - val000) * distance0;
286
287 ++basei[1];
288 if (basei[1] > this->m_EndIndex[1]) // interpolate across "x"
289 {
290 return (static_cast<OutputType>(valx00));
291 }
292 const RealType & val110 = inputImagePtr->GetPixel(basei);
293
294 --basei[0];
295 const RealType & val010 = inputImagePtr->GetPixel(basei);
296 const RealType & valx10 = val010 + (val110 - val010) * distance0;
297
298 return static_cast<OutputType>(valx00 + (valx10 - valx00) * distance1);
299 }
300 }
301 else
302 {
303 if (distance1 <= 0.)
304 {
305 if (distance0 <= 0.) // interpolate across "z"
306 {
307 ++basei[2];
308 if (basei[2] > this->m_EndIndex[2])
309 {
310 return (static_cast<OutputType>(val000));
311 }
312 const RealType & val001 = inputImagePtr->GetPixel(basei);
313
314 return static_cast<OutputType>(val000 + (val001 - val000) * distance2);
315 }
316 // interpolate across "xz"
317 ++basei[0];
318 if (basei[0] > this->m_EndIndex[0]) // interpolate across "z"
319 {
320 --basei[0];
321 ++basei[2];
322 if (basei[2] > this->m_EndIndex[2])
323 {
324 return (static_cast<OutputType>(val000));
325 }
326 const RealType & val001 = inputImagePtr->GetPixel(basei);
327
328 return static_cast<OutputType>(val000 + (val001 - val000) * distance2);
329 }
330 const RealType & val100 = inputImagePtr->GetPixel(basei);
331
332 const RealType & valx00 = val000 + (val100 - val000) * distance0;
333
334 ++basei[2];
335 if (basei[2] > this->m_EndIndex[2]) // interpolate across "x"
336 {
337 return (static_cast<OutputType>(valx00));
338 }
339 const RealType & val101 = inputImagePtr->GetPixel(basei);
340
341 --basei[0];
342 const RealType & val001 = inputImagePtr->GetPixel(basei);
343
344 const RealType & valx01 = val001 + (val101 - val001) * distance0;
345
346 return static_cast<OutputType>(valx00 + (valx01 - valx00) * distance2);
347 }
348 else if (distance0 <= 0.) // interpolate across "yz"
349 {
350 ++basei[1];
351 if (basei[1] > this->m_EndIndex[1]) // interpolate across "z"
352 {
353 --basei[1];
354 ++basei[2];
355 if (basei[2] > this->m_EndIndex[2])
356 {
357 return (static_cast<OutputType>(val000));
358 }
359 const RealType & val001 = inputImagePtr->GetPixel(basei);
360
361 return static_cast<OutputType>(val000 + (val001 - val000) * distance2);
362 }
363 const RealType & val010 = inputImagePtr->GetPixel(basei);
364
365 const RealType & val0x0 = val000 + (val010 - val000) * distance1;
366
367 ++basei[2];
368 if (basei[2] > this->m_EndIndex[2]) // interpolate across "y"
369 {
370 return (static_cast<OutputType>(val0x0));
371 }
372 const RealType & val011 = inputImagePtr->GetPixel(basei);
373
374 --basei[1];
375 const RealType & val001 = inputImagePtr->GetPixel(basei);
376
377 const RealType & val0x1 = val001 + (val011 - val001) * distance1;
378
379 return static_cast<OutputType>(val0x0 + (val0x1 - val0x0) * distance2);
380 }
381 else // interpolate across "xyz"
382 {
383 ++basei[0];
384 if (basei[0] > this->m_EndIndex[0]) // interpolate across "yz"
385 {
386 --basei[0];
387 ++basei[1];
388 if (basei[1] > this->m_EndIndex[1]) // interpolate across "z"
389 {
390 --basei[1];
391 ++basei[2];
392 if (basei[2] > this->m_EndIndex[2])
393 {
394 return (static_cast<OutputType>(val000));
395 }
396 const RealType & val001 = inputImagePtr->GetPixel(basei);
397
398 return static_cast<OutputType>(val000 + (val001 - val000) * distance2);
399 }
400 const RealType & val010 = inputImagePtr->GetPixel(basei);
401 const RealType & val0x0 = val000 + (val010 - val000) * distance1;
402
403 ++basei[2];
404 if (basei[2] > this->m_EndIndex[2]) // interpolate across "y"
405 {
406 return (static_cast<OutputType>(val0x0));
407 }
408 const RealType & val011 = inputImagePtr->GetPixel(basei);
409
410 --basei[1];
411 const RealType & val001 = inputImagePtr->GetPixel(basei);
412
413 const RealType & val0x1 = val001 + (val011 - val001) * distance1;
414
415 return static_cast<OutputType>(val0x0 + (val0x1 - val0x0) * distance2);
416 }
417 const RealType & val100 = inputImagePtr->GetPixel(basei);
418
419 const RealType & valx00 = val000 + (val100 - val000) * distance0;
420
421 ++basei[1];
422 if (basei[1] > this->m_EndIndex[1]) // interpolate across "xz"
423 {
424 --basei[1];
425 ++basei[2];
426 if (basei[2] > this->m_EndIndex[2]) // interpolate across "x"
427 {
428 return (static_cast<OutputType>(valx00));
429 }
430 const RealType & val101 = inputImagePtr->GetPixel(basei);
431
432 --basei[0];
433 const RealType & val001 = inputImagePtr->GetPixel(basei);
434
435 const RealType & valx01 = val001 + (val101 - val001) * distance0;
436
437 return static_cast<OutputType>(valx00 + (valx01 - valx00) * distance2);
438 }
439 const RealType & val110 = inputImagePtr->GetPixel(basei);
440
441 --basei[0];
442 const RealType & val010 = inputImagePtr->GetPixel(basei);
443
444 const RealType & valx10 = val010 + (val110 - val010) * distance0;
445
446 const RealType & valxx0 = valx00 + (valx10 - valx00) * distance1;
447
448 ++basei[2];
449 if (basei[2] > this->m_EndIndex[2]) // interpolate across "xy"
450 {
451 return (static_cast<OutputType>(valxx0));
452 }
453 const RealType & val011 = inputImagePtr->GetPixel(basei);
454
455 ++basei[0];
456 const RealType & val111 = inputImagePtr->GetPixel(basei);
457
458 --basei[1];
459 const RealType & val101 = inputImagePtr->GetPixel(basei);
460
461 --basei[0];
462 const RealType & val001 = inputImagePtr->GetPixel(basei);
463
464 const RealType & valx01 = val001 + (val101 - val001) * distance0;
465 const RealType & valx11 = val011 + (val111 - val011) * distance0;
466 const RealType & valxx1 = valx01 + (valx11 - valx01) * distance1;
467
468 return (static_cast<OutputType>(valxx0 + (valxx1 - valxx0) * distance2));
469 }
470 }
471 }
472
473 inline OutputType
475 {
476 return this->EvaluateUnoptimized(index);
477 }
478
480 virtual inline OutputType
482
485 template <typename RealTypeScalarRealType>
486 void
487 MakeZeroInitializer(const TInputImage * const inputImagePtr,
489 {
490 // Variable length vector version to get the size of the pixel correct.
491 constexpr typename TInputImage::IndexType idx = { { 0 } };
492 const typename TInputImage::PixelType & tempPixel = inputImagePtr->GetPixel(idx);
493 const unsigned int sizeOfVarLengthVector = tempPixel.GetSize();
494 tempZeros.SetSize(sizeOfVarLengthVector);
495 tempZeros.Fill(RealTypeScalarRealType{});
496 }
497
498 template <typename RealTypeScalarRealType>
499 void
500 MakeZeroInitializer(const TInputImage * const itkNotUsed(inputImagePtr), RealTypeScalarRealType & tempZeros) const
501 {
502 // All other cases
503 tempZeros = RealTypeScalarRealType{};
504 }
505};
506} // end namespace itk
507
508#ifndef ITK_MANUAL_INSTANTIATION
509# include "itkLinearInterpolateImageFunction.hxx"
510#endif
511
512#endif
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Base class for all image interpolators.
typename NumericTraits< typename TInputImage::PixelType >::RealType RealType
Light weight base class for most itk classes.
Linearly interpolate an image at specified positions.
typename ContinuousIndexType::ValueType InternalComputationType
void PrintSelf(std::ostream &os, Indent indent) const override
OutputType EvaluateOptimized(const Dispatch< 2 > &, const ContinuousIndexType &index) const
OutputType EvaluateOptimized(const Dispatch< 1 > &, const ContinuousIndexType &index) const
virtual OutputType EvaluateUnoptimized(const ContinuousIndexType &index) const
OutputType EvaluateOptimized(const Dispatch< 3 > &, const ContinuousIndexType &index) const
OutputType EvaluateAtContinuousIndex(const ContinuousIndexType &index) const override
OutputType EvaluateOptimized(const Dispatch< 0 > &, const ContinuousIndexType &) const
void MakeZeroInitializer(const TInputImage *const, RealTypeScalarRealType &tempZeros) const
~LinearInterpolateImageFunction() override=default
OutputType EvaluateOptimized(const DispatchBase &, const ContinuousIndexType &index) const
void MakeZeroInitializer(const TInputImage *const inputImagePtr, VariableLengthVector< RealTypeScalarRealType > &tempZeros) const
A method to generically set all components to zero.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....