[ind2pos] fix rounding and bounds
This commit is contained in:
parent
43d5fa8b4f
commit
1cc47da386
2 changed files with 23 additions and 2 deletions
|
|
@ -47,13 +47,13 @@ class GridPosMixin(GridBase):
|
||||||
else:
|
else:
|
||||||
low_bound = -0.5
|
low_bound = -0.5
|
||||||
high_bound = -0.5
|
high_bound = -0.5
|
||||||
if (ind < low_bound).any() or (ind > self.shape - high_bound).any():
|
if (ind < low_bound).any() or (ind > self.shape + high_bound).any():
|
||||||
raise GridError(f'Position outside of grid: {ind}')
|
raise GridError(f'Position outside of grid: {ind}')
|
||||||
|
|
||||||
if round_ind:
|
if round_ind:
|
||||||
rind = numpy.clip(numpy.round(ind).astype(int), 0, self.shape - 1)
|
rind = numpy.clip(numpy.round(ind).astype(int), 0, self.shape - 1)
|
||||||
sxyz = self.shifted_xyz(which_shifts)
|
sxyz = self.shifted_xyz(which_shifts)
|
||||||
position = [sxyz[a][rind[a]].astype(int) for a in range(3)]
|
position = [sxyz[a][rind[a]] for a in range(3)]
|
||||||
else:
|
else:
|
||||||
sexyz = self.shifted_exyz(which_shifts)
|
sexyz = self.shifted_exyz(which_shifts)
|
||||||
position = [numpy.interp(ind[a], numpy.arange(sexyz[a].size) - 0.5, sexyz[a])
|
position = [numpy.interp(ind[a], numpy.arange(sexyz[a].size) - 0.5, sexyz[a])
|
||||||
|
|
|
||||||
|
|
@ -118,6 +118,27 @@ def test_draw_2shift_4x4() -> None:
|
||||||
assert_allclose(arr, correct)
|
assert_allclose(arr, correct)
|
||||||
|
|
||||||
|
|
||||||
|
def test_ind2pos_round_preserves_float_centers() -> None:
|
||||||
|
grid = Grid([[0, 1, 3], [0, 2], [0, 1]], shifts=[[0, 0, 0]])
|
||||||
|
|
||||||
|
pos = grid.ind2pos(numpy.array([1, 0, 0]), which_shifts=0)
|
||||||
|
|
||||||
|
assert_allclose(pos, [2.0, 1.0, 0.5])
|
||||||
|
|
||||||
|
|
||||||
|
def test_ind2pos_enforces_bounds_for_rounded_and_fractional_indices() -> None:
|
||||||
|
grid = Grid([[0, 1, 3], [0, 2], [0, 1]], shifts=[[0, 0, 0]])
|
||||||
|
|
||||||
|
with pytest.raises(GridError):
|
||||||
|
grid.ind2pos(numpy.array([2, 0, 0]), which_shifts=0, check_bounds=True)
|
||||||
|
|
||||||
|
edge_pos = grid.ind2pos(numpy.array([1.5, 0.5, 0.5]), which_shifts=0, round_ind=False, check_bounds=True)
|
||||||
|
assert_allclose(edge_pos, [3.0, 2.0, 1.0])
|
||||||
|
|
||||||
|
with pytest.raises(GridError):
|
||||||
|
grid.ind2pos(numpy.array([1.6, 0.5, 0.5]), which_shifts=0, round_ind=False, check_bounds=True)
|
||||||
|
|
||||||
|
|
||||||
def test_draw_polygon_accepts_coplanar_nx3_vertices() -> None:
|
def test_draw_polygon_accepts_coplanar_nx3_vertices() -> None:
|
||||||
grid = Grid([[0, 1, 2], [0, 1, 2], [0, 1]], shifts=[[0, 0, 0]])
|
grid = Grid([[0, 1, 2], [0, 1, 2], [0, 1]], shifts=[[0, 0, 0]])
|
||||||
arr_2d = grid.allocate(0)
|
arr_2d = grid.allocate(0)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue