-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpolate.h
128 lines (104 loc) · 4.19 KB
/
interpolate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//-----------------------------------------------------------------------------
// (Bi)Linear interpolation in 2D and 3D
//-----------------------------------------------------------------------------
//
// Copyright (C) 2018 Arend Lammertink
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//-----------------------------------------------------------------------------
//
// Based on:
//
// Interpolation in Two or More Dimensions
// http://www.aip.de/groups/soe/local/numres/bookcpdf/c3-6.pdf
//
// How to build a lookup table in C (SDCC compiler) with linear interpolation
// http://bit.ly/LUT_c_linear_interpolation
//
// Linear interpolation: calculate correction based on 2D table
// http://bit.ly/Interpolate2D
//
// 2D Array Interpolation
// http://bit.ly/biliniar_barycentric_interpolation
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Multi-include protection
//-----------------------------------------------------------------------------
#ifndef _INTERPOL_H
#define _INTERPOL_H
//-----------------------------------------------------------------------------
// TODO :
//
// fix16.h includes a number of lerp (linear interpolation) routines. These
// could perhaps be used to speed things up.
//
// There are no routines for casting uint16_t integers to/from Fix16. For
// now, we cast to float before casting to Fix16.
//
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <stdint.h>
#include <string.h>
using namespace std;
#ifdef SUPPORT_INTEGER_ARITMETHIC
# include <fix16.hpp>
#endif
//-----------------------------------------------------------------------------
// 1D linear interpolation
//-----------------------------------------------------------------------------
template <typename X, typename Y>
inline Y interpolate( X x, X x_1, X x_2, Y y_1, Y y_2 )
{
Y one = 1.0f; // avoid ambigious operator overload
// cast indexes to Y
Y _x = x;
Y _x_1 = x_1;
Y _x_2 = x_2;
Y dx = (_x - _x_1) / (_x_2 - _x_1); // 0 <= dx <= 1
return (one-dx)*y_1 + dx*y_2;
}
//-----------------------------------------------------------------------------
// 2D bilinear interpolation
//-----------------------------------------------------------------------------
template <typename X, typename Y>
inline Y interpolate( X x1, X x2, X x_1, X x_2, X x_3, X x_4,
Y y_1, Y y_2, Y y_3, Y y_4 )
{
Y one = 1.0f; // avoid ambigious operator overload
// cast indexes to Y
Y _x1 = x1;
Y _x2 = x2;
Y _x_1 = x_1;
Y _x_2 = x_2;
Y _x_3 = x_3;
Y _x_4 = x_4;
Y dx1 = (_x1 - _x_1) / (_x_2 - _x_1); // 0 <= dx1 <= 1
Y dx2 = (_x2 - _x_3) / (_x_4 - _x_3); // 0 <= dx2 <= 1
return (one-dx1)*(one-dx2)*y_1 + dx1*(one-dx2)*y_2 +
dx1*dx2*y_3 + (one-dx1)*dx2*y_4;
}
//-----------------------------------------------------------------------------
// Specialization for integers by casting to Fix16
//-----------------------------------------------------------------------------
#ifdef SUPPORT_INTEGER_ARITMETHIC
// Specializations generated by python script.
# include "interpol1d_spec.h"
# include "interpol2d_spec.h"
#endif // SUPPORT_INTEGER_ARITMETHIC
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
#endif // End multi-include protection