-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsampling.py
More file actions
306 lines (249 loc) · 12.2 KB
/
sampling.py
File metadata and controls
306 lines (249 loc) · 12.2 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# ================================================================
# Created by Gregory Kramida on 9/17/18.
# Copyright (c) 2018 Gregory Kramida
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ================================================================
# routines that sample from a given scalar or vector field at the specified location in various ways
# stdlib
import math
# libraries
import numpy as np
# local
from utils.point2d import Point2d
# TODO: move all the focus coordinate stuff to a separate file
FOCUS_COORDINATES = (34, 80)
def set_focus_coordinates(x, y):
global FOCUS_COORDINATES
FOCUS_COORDINATES = (x, y)
def sample_at(field, x=0, y=0, point=None):
"""
Sample from a 2D scalar field at a given coordinate; return 1 if given coordinate is out-of-bounds
Works with either a named point argument or x and y coordinates positionally following the filed argument.
In case all three are specified and point is not None, it will override the x and y arguments.
:param field: field from which to sample
:type field: numpy.ndarray
:param x: x coordinate for sampling location
:type x: int
:param y: y coordinate for sampling location
:type y: int
:param point: full coordinate for sampling location.
:type point: Point2d
:return: scalar value at given coordinate if (x,y) are within bounds of the scalar field, 1 otherwise
"""
if point is not None:
x = point.x
y = point.y
if x < 0 or x >= field.shape[1] or y < 0 or y >= field.shape[0]:
return 1
return field[y, x]
def sample_flag_at(field, x=0, y=0, point=None):
if point is not None:
x = point.x
y = point.y
if x < 0.0 or x >= field.shape[1] or y < 0.0 or y >= field.shape[0]:
return 0
return field[y, x]
def sample_at_replacement(field, replacement, x=0, y=0, point=None):
if point is not None:
x = point.x
y = point.y
if x < 0 or x >= field.shape[1] or y < 0 or y >= field.shape[0]:
return replacement
return field[y, x]
def focus_coordinates_match(x, y):
return x == FOCUS_COORDINATES[0] and y == FOCUS_COORDINATES[1]
def get_focus_coordinates():
return FOCUS_COORDINATES
def sample_warp(warp_field, x, y, replacement):
if x >= warp_field.shape[1] or y >= warp_field.shape[0] or x < 0 or y < 0:
return replacement
else:
return warp_field[y, x]
def sample_warp_replace_if_zero(warp_field, x, y, replacement):
if x >= warp_field.shape[1] or y >= warp_field.shape[0] or x < 0 or y < 0 or np.linalg.norm(
warp_field[y, x]) == 0.0:
return replacement
else:
return warp_field[y, x]
class BilinearSamplingMetaInfo():
def __init__(self, value00=1, value01=1, value10=1, value11=1, ratios=Point2d(1.0, 1.0),
inverse_ratios=Point2d(0.0, 0.0)):
self.value00 = value00
self.value01 = value01
self.value10 = value10
self.value11 = value11
self.ratios = ratios
self.inverse_ratios = inverse_ratios
def interpolate_bilinearly(values, ratios):
"""
:param values: iterable of 4 values, representing discrete points, in the order {([x=]0,[y=]0), (01), (10), (11)}
:type ratios: Point2d
:param ratios: distances from point (00) to the sample point along x and y axes, respectively
:return:
"""
inverse_ratios = Point2d(1.0, 1.0) - ratios
value00 = values[0]
value01 = values[1]
value10 = values[2]
value11 = values[3]
interpolated_value0 = value00 * inverse_ratios.y + value01 * ratios.y
interpolated_value1 = value10 * inverse_ratios.y + value11 * ratios.y
interpolated_value = interpolated_value0 * inverse_ratios.x + interpolated_value1 * ratios.x
return interpolated_value
def get_bilinear_ratios(x, y):
point = Point2d(x, y)
base_point = Point2d(math.floor(x), math.floor(y))
ratios = point - base_point
return ratios
# The coordinate is considered out-of-bounds if it is further than 1 cell away from the border of the field.
# If the coordinate is outside of the field border but within 1 cell, the border values are repeated to substitute
# for missing values in the interpolation process.
def bilinear_sample_at(field, x=0, y=0, point=None):
"""
Sample from a 2D scalar field at a given coordinate with bilinear interpolation.
If coordinate is out-of-bounds, uses "1" for all samples that fall out of bounds during the interpolation.
Works with either a named point argument or x and y coordinates positionally following the filed argument.
In case all three are specified and point is not None, it will override the x and y arguments.
:param field: field from which to sample
:type field: numpy.ndarray
:param x: x coordinate for sampling location
:type x: int
:param y: y coordinate for sampling location
:type y: int
:param point: full coordinate for sampling location.
:type point: Point2d
:return: bilinearly interpolated scalar value at given coordinate if (x,y) are within bounds of the scalar field,
1 otherwise
"""
if point is not None:
x = point.x
y = point.y
point = Point2d(x, y)
base_point = Point2d(math.floor(point.x), math.floor(point.y))
ratios = point - base_point
inverse_ratios = Point2d(1.0, 1.0) - ratios
value00 = sample_at(field, point=base_point)
value01 = sample_at(field, point=base_point + Point2d(0, 1))
value10 = sample_at(field, point=base_point + Point2d(1, 0))
value11 = sample_at(field, point=base_point + Point2d(1, 1))
interpolated_value0 = value00 * inverse_ratios.y + value01 * ratios.y
interpolated_value1 = value10 * inverse_ratios.y + value11 * ratios.y
interpolated_value = interpolated_value0 * inverse_ratios.x + interpolated_value1 * ratios.x
return interpolated_value
# The coordinate is considered out-of-bounds if it is further than 1 cell away from the border of the field.
# If the coordinate is outside of the field border but within 1 cell, the border values are repeated to substitute
# for missing values in the interpolation process.
def bilinear_sample_at_metainfo(field, x=0, y=0, point=None):
"""
Sample from a 2D scalar field at a given coordinate with bilinear interpolation.
If coordinate is out-of-bounds, uses "1" for all samples that fall out of bounds during the interpolation.
Works with either a named point argument or x and y coordinates positionally following the filed argument.
In case all three are specified and point is not None, it will override the x and y arguments.
:param field: field from which to sample
:type field: numpy.ndarray
:param x: x coordinate for sampling location
:type x: int
:param y: y coordinate for sampling location
:type y: int
:param point: full coordinate for sampling location.
:type point: Point2d
:return: bilinearly interpolated scalar value at given coordinate if (x,y) are within bounds of the scalar field,
1 otherwise
"""
if point is not None:
x = point.x
y = point.y
point = Point2d(x, y)
base_point = Point2d(math.floor(point.x), math.floor(point.y))
ratios = point - base_point
inverse_ratios = Point2d(1.0, 1.0) - ratios
value00 = sample_at(field, point=base_point)
value01 = sample_at(field, point=base_point + Point2d(0, 1))
value10 = sample_at(field, point=base_point + Point2d(1, 0))
value11 = sample_at(field, point=base_point + Point2d(1, 1))
interpolated_value0 = value00 * inverse_ratios.y + value01 * ratios.y
interpolated_value1 = value10 * inverse_ratios.y + value11 * ratios.y
interpolated_value = interpolated_value0 * inverse_ratios.x + interpolated_value1 * ratios.x
metainfo = BilinearSamplingMetaInfo(value00, value01, value10, value11, ratios, inverse_ratios)
return interpolated_value, metainfo
def bilinear_sample_at_replacement(field, x=0, y=0, point=None, replacement=1):
"""
Sample from a 2D scalar field at a given coordinate with bilinear interpolation.
If coordinate is out-of-bounds, uses the replacement argument for all samples that fall out of bounds during
the interpolation.
Works with either a named point argument or x and y coordinates positionally following the filed argument.
In case all three are specified and point is not None, it will override the x and y arguments.
:param replacement: value to use as replacement when sampling out-of-bounds
:param field: field from which to sample
:type field: numpy.ndarray
:param x: x coordinate for sampling location
:type x: int
:param y: y coordinate for sampling location
:type y: int
:param point: full coordinate for sampling location.
:type point: Point2d
:return: bilinearly interpolated scalar value at given coordinate if (x,y) are within bounds of the scalar field,
1 otherwise
"""
if point is not None:
x = point.x
y = point.y
# if x < 0 or x >= field.shape[1] or y < 0 or y >= field.shape[0]:
# return replacement
point = Point2d(x, y)
base_point = Point2d(math.floor(point.x), math.floor(point.y))
ratios = point - base_point
inverse_ratios = Point2d(1.0, 1.0) - ratios
value00 = sample_at_replacement(field, replacement, point=base_point)
value01 = sample_at_replacement(field, replacement, point=base_point + Point2d(0, 1))
value10 = sample_at_replacement(field, replacement, point=base_point + Point2d(1, 0))
value11 = sample_at_replacement(field, replacement, point=base_point + Point2d(1, 1))
interpolated_value0 = value00 * inverse_ratios.y + value01 * ratios.y
interpolated_value1 = value10 * inverse_ratios.y + value11 * ratios.y
interpolated_value = interpolated_value0 * inverse_ratios.x + interpolated_value1 * ratios.x
return interpolated_value
def bilinear_sample_at_replacement_metainfo(field, x=0, y=0, point=None, replacement=1):
"""
Sample from a 2D scalar field at a given coordinate with bilinear interpolation.
If coordinate is out-of-bounds, uses the replacement argument for all samples that fall out of bounds during
the interpolation.
Works with either a named point argument or x and y coordinates positionally following the filed argument.
In case all three are specified and point is not None, it will override the x and y arguments.
:param replacement: value to use as replacement when sampling out-of-bounds
:param field: field from which to sample
:type field: numpy.ndarray
:param x: x coordinate for sampling location
:type x: int
:param y: y coordinate for sampling location
:type y: int
:param point: full coordinate for sampling location.
:type point: Point2d
:return: bilinearly interpolated scalar value at given coordinate if (x,y) are within bounds of the scalar field,
1 otherwise
"""
if point is not None:
x = point.x
y = point.y
point = Point2d(x, y)
base_point = Point2d(math.floor(point.x), math.floor(point.y))
ratios = point - base_point
inverse_ratios = Point2d(1.0, 1.0) - ratios
value00 = sample_at_replacement(field, replacement, point=base_point)
value01 = sample_at_replacement(field, replacement, point=base_point + Point2d(0, 1))
value10 = sample_at_replacement(field, replacement, point=base_point + Point2d(1, 0))
value11 = sample_at_replacement(field, replacement, point=base_point + Point2d(1, 1))
interpolated_value0 = value00 * inverse_ratios.y + value01 * ratios.y
interpolated_value1 = value10 * inverse_ratios.y + value11 * ratios.y
interpolated_value = interpolated_value0 * inverse_ratios.x + interpolated_value1 * ratios.x
metainfo = BilinearSamplingMetaInfo(value00, value01, value10, value11, ratios, inverse_ratios)
return interpolated_value, metainfo