Isis 3 Programmer Reference
Interpolator.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #include <string>
9 #include "IException.h"
10 #include "Interpolator.h"
11 
12 using namespace std;
13 namespace Isis {
17  Interpolator::Interpolator() {
18  Init();
19  }
20 
28  Interpolator::Interpolator(const interpType &type) {
29  Init();
30  p_type = type;
31  }
32 
34  Interpolator::~Interpolator() {}
35 
37  void Interpolator::Init() {
38  p_type = None;
39  }
40 
56  double Interpolator::Interpolate(const double isamp, const double iline,
57  const double buf[]) {
58 
59  switch(p_type) {
60  case None: {
61  string message = "Interpolator type not set";
62  throw IException(IException::Programmer, message, _FILEINFO_);
63  break;
64  }
65  case NearestNeighborType:
66  return NearestNeighbor(isamp, iline, buf);
67  break;
68  case BiLinearType:
69  return BiLinear(isamp, iline, buf);
70  break;
71  case CubicConvolutionType:
72  return CubicConvolution(isamp, iline, buf);
73  break;
74  }
75 
76  string message = "Invalid interpolator";
77  throw IException(IException::Programmer, message, _FILEINFO_);
78  }
85  void Interpolator::SetType(const interpType &type) {
86  p_type = type;
87  }
88 
89 
101  double Interpolator::NearestNeighbor(const double isamp, const double iline,
102  const double buf[]) {
103  return buf[0];
104  }
105 
106 
118  double Interpolator::BiLinear(const double isamp, const double iline,
119  const double buf[]) {
120 
121  // Get the fractional portions of the sample and line coordinates
122  double j = int (isamp);
123  double k = int (iline);
124  double a = isamp - j;
125  double b = iline - k;
126 
127  // If any of the 4 pixels are special pixels, drop down to a nearest neighbor
128  for(int i = 0; i < 4; i++) {
129  if(Isis::IsSpecial(buf[i])) {
130  return NearestNeighbor(isamp, iline,
131  &buf[(int)(a + 0.5) + 2 * (int)(b+0.5)]);
132  break;
133  }
134  }
135 
136  // Otherwise do the bilinear
137  return (1.0 - a) * (1.0 - b) * buf[0] +
138  a * (1.0 - b) * buf[1] +
139  (1.0 - a) * b * buf[2] +
140  a * b * buf[3];
141 
142  }
143 
156  double Interpolator::CubicConvolution(const double isamp,
157  const double iline,
158  const double buf[]) {
159 
160  // If any of the 16 pixels are special pixels, drop down to a bilinear
161  for(int i = 0; i < 16; i++) {
162  if(Isis::IsSpecial(buf[i])) {
163  double tbuf[4] = {buf[5], buf[6], buf[9], buf[10]};
164  return BiLinear(isamp, iline, tbuf);
165  break;
166  }
167  }
168 
169  double j = int (isamp);
170  double k = int (iline);
171  double a = isamp - j;
172  double b = iline - k;
173 
181  return -b * (1.0 - b) * (1.0 - b) * (-a * (1.0 - a) * (1.0 - a) * buf[0] +
182  (1.0 - 2.0 * a * a + a * a * a) * buf[1] +
183  a * (1.0 + a - a * a) * buf[2] -
184  a * a * (1.0 - a) * buf[3]) +
185 
186  (1.0 - 2.0 * b * b + b * b * b) * (-a * (1.0 - a) * (1.0 - a) * buf[4] +
187  (1.0 - 2.0 * a * a + a * a * a) * buf[5] +
188  a * (1.0 + a - a * a) * buf[6] -
189  a * a * (1.0 - a) * buf[7]) +
190 
191  b * (1.0 + b - b * b) * (-a * (1.0 - a) * (1.0 - a) * buf[8] +
192  (1.0 - 2.0 * a * a + a * a * a) * buf[9] +
193  a * (1.0 + a - a * a) * buf[10] -
194  a * a * (1.0 - a) * buf[11]) +
195 
196  b * b * (b - 1.0) * (-a * (1.0 - a) * (1.0 - a) * buf[12] +
197  (1.0 - 2.0 * a * a + a * a * a) * buf[13] +
198  a * (1.0 + a - a * a) * buf[14] -
199  a * a * (1.0 - a) * buf[15]);
200 
201  }
202 
208  int Interpolator::Samples() {
209 
210  switch(p_type) {
211  case None: {
212  string message = "Interpolator type not set";
213  throw IException(IException::Programmer, message, _FILEINFO_);
214  break;
215  }
216  case NearestNeighborType:
217  return 1;
218  break;
219  case BiLinearType:
220  return 2;
221  break;
222  case CubicConvolutionType:
223  return 4;
224  break;
225  }
226 
227  string message = "Invalid interpolator";
228  throw IException(IException::Programmer, message, _FILEINFO_);
229  }
230 
236  int Interpolator::Lines() {
237 
238  switch(p_type) {
239  case None: {
240  string message = "Interpolator type not set";
241  throw IException(IException::Programmer, message, _FILEINFO_);
242  break;
243  }
244  case NearestNeighborType:
245  return 1;
246  break;
247  case BiLinearType:
248  return 2;
249  break;
250  case CubicConvolutionType:
251  return 4;
252  break;
253  }
254 
255  string message = "Invalid interpolator";
256  throw IException(IException::Programmer, message, _FILEINFO_);
257  }
258 
265  double Interpolator::HotSample() {
266 
267  switch(p_type) {
268  case None: {
269  string message = "Interpolator type not set";
270  throw IException(IException::Programmer, message, _FILEINFO_);
271  break;
272  }
273  // To get the correct pixel for NN you have to round the sample
274  case NearestNeighborType:
275  return -0.5;
276  break;
277  // To get the correct pixel for BL you have to truncate the sample
278  case BiLinearType:
279  return 0.0;
280  break;
281  // To get the correct pixel for CC you have to truncate the sample
282  case CubicConvolutionType:
283  return 1.0;
284  break;
285  }
286 
287  string message = "Invalid interpolator";
288  throw IException(IException::Programmer, message, _FILEINFO_);
289  }
290 
297  double Interpolator::HotLine() {
298 
299  switch(p_type) {
300  case None: {
301  string message = "Interpolator type not set";
302  throw IException(IException::Programmer, message, _FILEINFO_);
303  break;
304  }
305  // To get the correct pixel for NN you have to round the line
306  case NearestNeighborType:
307  return -0.5;
308  break;
309  // To get the correct pixel for BL you have to truncate the line
310  case BiLinearType:
311  return 0.0;
312  break;
313  // To get the correct pixel for CC you have to truncate the line
314  case CubicConvolutionType:
315  return 1.0;
316  break;
317  }
318 
319  string message = "Invalid interpolator";
320  throw IException(IException::Programmer, message, _FILEINFO_);
321  }
322 }
Isis::IsSpecial
bool IsSpecial(const double d)
Returns if the input pixel is special.
Definition: SpecialPixel.h:197
Isis::Interpolator::interpType
interpType
The interpolator type, including: None, Nearest Neighbor, BiLinear or Cubic Convultion.
Definition: Interpolator.h:40
Isis::IException
Isis exception class.
Definition: IException.h:91
std
Namespace for the standard library.
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16