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_NearestNeighorInterpolateImageFunction_HXX
21 #define MIRTK_NearestNeighorInterpolateImageFunction_HXX
22 
23 #include "mirtk/NearestNeighborInterpolateImageFunction.h"
24 
25 #include "mirtk/InterpolateImageFunction.hxx"
26 #include "mirtk/Math.h"
27 
28 
29 namespace mirtk {
30 
31 
32 // =============================================================================
33 // Construction/Destruction
34 // =============================================================================
35 
36 // -----------------------------------------------------------------------------
37 template <class TImage>
38 GenericNearestNeighborInterpolateImageFunction<TImage>
GenericNearestNeighborInterpolateImageFunction()39 ::GenericNearestNeighborInterpolateImageFunction()
40 {
41 }
42 
43 // -----------------------------------------------------------------------------
44 template <class TImage>
45 GenericNearestNeighborInterpolateImageFunction<TImage>
~GenericNearestNeighborInterpolateImageFunction()46 ::~GenericNearestNeighborInterpolateImageFunction()
47 {
48 }
49 
50 // =============================================================================
51 // Domain checks
52 // =============================================================================
53 
54 // -----------------------------------------------------------------------------
55 template <class TImage>
56 void GenericNearestNeighborInterpolateImageFunction<TImage>
BoundingInterval(double x,int & i,int & I) const57 ::BoundingInterval(double x, int &i, int &I) const
58 {
59   i = I = iround(x);
60 }
61 
62 // =============================================================================
63 // Evaluation
64 // =============================================================================
65 
66 // -----------------------------------------------------------------------------
67 template <class TImage>
68 inline typename GenericNearestNeighborInterpolateImageFunction<TImage>::VoxelType
69 GenericNearestNeighborInterpolateImageFunction<TImage>
Get(double x,double y,double z,double t) const70 ::Get(double x, double y, double z, double t) const
71 {
72   const int i = iround(x);
73   const int j = iround(y);
74   const int k = iround(z);
75   const int l = iround(t);
76 
77   if (this->Input()->IsInside(i, j, k, l)) {
78     return this->Input()->Get(i, j, k, l);
79   } else {
80     return voxel_cast<VoxelType>(this->DefaultValue());
81   }
82 }
83 
84 // -----------------------------------------------------------------------------
85 template <class TImage>
86 inline typename GenericNearestNeighborInterpolateImageFunction<TImage>::VoxelType
87 GenericNearestNeighborInterpolateImageFunction<TImage>
GetWithPadding(double x,double y,double z,double t) const88 ::GetWithPadding(double x, double y, double z, double t) const
89 {
90   const int i = iround(x);
91   const int j = iround(y);
92   const int k = iround(z);
93   const int l = iround(t);
94 
95   if (this->Input()->IsInsideForeground(i, j, k, l)) {
96     return this->Input()->Get(i, j, k, l);
97   } else {
98     return voxel_cast<VoxelType>(this->DefaultValue());
99   }
100 }
101 
102 // -----------------------------------------------------------------------------
103 template <class TImage> template <class TOtherImage>
104 inline typename TOtherImage::VoxelType
105 GenericNearestNeighborInterpolateImageFunction<TImage>
Get(const TOtherImage * input,double x,double y,double z,double t) const106 ::Get(const TOtherImage *input, double x, double y, double z, double t) const
107 {
108   return input->Get(iround(x), iround(y), iround(z), iround(t));
109 }
110 
111 // -----------------------------------------------------------------------------
112 template <class TImage> template <class TOtherImage>
113 inline typename TOtherImage::VoxelType
114 GenericNearestNeighborInterpolateImageFunction<TImage>
GetWithPadding(const TOtherImage * input,double x,double y,double z,double t) const115 ::GetWithPadding(const TOtherImage *input, double x, double y, double z, double t) const
116 {
117   const int i = iround(x);
118   const int j = iround(y);
119   const int k = iround(z);
120   const int l = iround(t);
121 
122   if (input->IsForeground(i, j, k, l)) {
123     return input->Get(i, j, k, l);
124   } else {
125     return voxel_cast<VoxelType>(this->DefaultValue());
126   }
127 }
128 
129 // -----------------------------------------------------------------------------
130 template <class TImage>
131 inline typename GenericNearestNeighborInterpolateImageFunction<TImage>::VoxelType
132 GenericNearestNeighborInterpolateImageFunction<TImage>
GetInside(double x,double y,double z,double t) const133 ::GetInside(double x, double y, double z, double t) const
134 {
135   return Get(this->Input(), x, y, z, t);
136 }
137 
138 // -----------------------------------------------------------------------------
139 template <class TImage>
140 inline typename GenericNearestNeighborInterpolateImageFunction<TImage>::VoxelType
141 GenericNearestNeighborInterpolateImageFunction<TImage>
GetOutside(double x,double y,double z,double t) const142 ::GetOutside(double x, double y, double z, double t) const
143 {
144   if (this->Extrapolator()) {
145     return Get(this->Extrapolator(), x, y, z, t);
146   } else {
147     return Get(x, y, z, t);
148   }
149 }
150 
151 // -----------------------------------------------------------------------------
152 template <class TImage>
153 inline typename GenericNearestNeighborInterpolateImageFunction<TImage>::VoxelType
154 GenericNearestNeighborInterpolateImageFunction<TImage>
GetWithPaddingInside(double x,double y,double z,double t) const155 ::GetWithPaddingInside(double x, double y, double z, double t) const
156 {
157   return GetWithPadding(this->Input(), x, y, z, t);
158 }
159 
160 // -----------------------------------------------------------------------------
161 template <class TImage>
162 inline typename GenericNearestNeighborInterpolateImageFunction<TImage>::VoxelType
163 GenericNearestNeighborInterpolateImageFunction<TImage>
GetWithPaddingOutside(double x,double y,double z,double t) const164 ::GetWithPaddingOutside(double x, double y, double z, double t) const
165 {
166   if (this->Extrapolator()) {
167     return GetWithPadding(this->Extrapolator(), x, y, z, t);
168   } else {
169     return GetWithPadding(x, y, z, t);
170   }
171 }
172 
173 
174 } // namespace mirtk
175 
176 #endif // MIRTK_NearestNeighorInterpolateImageFunction_HXX
177