-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLUT.py
30 lines (26 loc) · 996 Bytes
/
LUT.py
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
##!python 3
class LUT(object):
# An array-like object that provides interpolated values between set points.
def __init__(self, points):
self.points = sorted(points)
def __getitem__(self, x):
if x < self.points[0][0]:
return self.points[0][1]
if x > self.points[-1][0]:
return self.points[-1][1]
lower_point, upper_point = self._GetBoundingPoints(x)
return self._Interpolate(x, lower_point, upper_point)
def _GetBoundingPoints(self, x):
# """Get the lower/upper points that bound x."""
lower_point = None
upper_point = self.points[0]
for point in self.points[1:]:
lower_point = upper_point
upper_point = point
if x <= upper_point[0]:
break
return lower_point, upper_point
def _Interpolate(self, x, lower_point, upper_point):
# """Interpolate a Y value for x given lower & upper bounding points."""
slope = (float(upper_point[1] - lower_point[1]) /(upper_point[0] - lower_point[0]))
return lower_point[1] + (slope * (x - lower_point[0]))