Skip to content

Commit 31f58e0

Browse files
committed
fix: updating bbox for 2d case as well as 3d.
Also updating tests for 2d
1 parent 0bf11e9 commit 31f58e0

File tree

2 files changed

+122
-9
lines changed

2 files changed

+122
-9
lines changed

LoopStructural/datatypes/_bounding_box.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(
1212
maximum: Optional[np.ndarray] = None,
1313
nsteps: Optional[np.ndarray] = None,
1414
step_vector: Optional[np.ndarray] = None,
15-
dimensions: int = 3,
15+
dimensions: Optional[int] = None,
1616
):
1717
"""A bounding box for a model, defined by the
1818
origin, maximum and number of steps in each direction
@@ -32,7 +32,13 @@ def __init__(
3232
maximum = origin + nsteps * step_vector
3333
self._origin = np.array(origin)
3434
self._maximum = np.array(maximum)
35-
self.dimensions = dimensions
35+
if dimensions is None:
36+
if self.origin is None:
37+
raise LoopValueError("Origin is not set")
38+
self.dimensions = len(self.origin)
39+
print(self.dimensions)
40+
else:
41+
self.dimensions = dimensions
3642
if nsteps is None:
3743
self.nsteps = np.array([50, 50, 25])
3844
self.name_map = {
@@ -182,13 +188,13 @@ def is_inside(self, xyz):
182188
def regular_grid(self, nsteps=None, shuffle=False, order="C"):
183189
if nsteps is None:
184190
nsteps = self.nsteps
185-
x = np.linspace(self.origin[0], self.maximum[0], nsteps[0])
186-
y = np.linspace(self.origin[1], self.maximum[1], nsteps[1])
187-
z = np.linspace(self.origin[2], self.maximum[2], nsteps[2])
188-
xx, yy, zz = np.meshgrid(x, y, z, indexing="ij")
189-
locs = np.array(
190-
[xx.flatten(order=order), yy.flatten(order=order), zz.flatten(order=order)]
191-
).T
191+
coordinates = [
192+
np.linspace(self.origin[i], self.maximum[i], nsteps[i]) for i in range(self.dimensions)
193+
]
194+
195+
coordinate_grid = np.meshgrid(*coordinates, indexing="ij")
196+
197+
locs = np.array([c.flatten(order=order) for c in coordinate_grid]).T
192198
if shuffle:
193199
# logger.info("Shuffling points")
194200
rng.shuffle(locs)
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
from LoopStructural.datatypes import BoundingBox
2+
import numpy as np
3+
4+
5+
def test_create_bounding_box():
6+
bbox = BoundingBox(origin=[0, 0, 0], maximum=[1, 1, 1])
7+
assert bbox.origin[0] == 0
8+
assert bbox.origin[1] == 0
9+
assert bbox.origin[2] == 0
10+
assert bbox.maximum[0] == 1
11+
assert bbox.maximum[1] == 1
12+
assert bbox.maximum[2] == 1
13+
assert bbox.dimensions == 3
14+
assert np.all(bbox.bb == np.array([[0, 0, 0], [1, 1, 1]]))
15+
assert bbox.valid is True
16+
17+
18+
def test_create_bounding_box_2d():
19+
bbox = BoundingBox(origin=[0, 0], maximum=[1, 1], dimensions=2)
20+
assert bbox.origin[0] == 0
21+
assert bbox.origin[1] == 0
22+
assert bbox.maximum[0] == 1
23+
assert bbox.maximum[1] == 1
24+
assert bbox.dimensions == 2
25+
assert np.all(bbox.bb == np.array([[0, 0], [1, 1]]))
26+
assert bbox.valid is True
27+
28+
29+
def test_create_bounding_box_from_points():
30+
bbox = BoundingBox(dimensions=3)
31+
bbox.fit(np.array([[0, 0, 0], [1, 1, 1]]))
32+
assert bbox.origin[0] == 0
33+
assert bbox.origin[1] == 0
34+
assert bbox.origin[2] == 0
35+
assert bbox.maximum[0] == 1
36+
assert bbox.maximum[1] == 1
37+
assert bbox.maximum[2] == 1
38+
assert np.all(bbox.bb == np.array([[0, 0, 0], [1, 1, 1]]))
39+
assert bbox.valid is True
40+
41+
42+
def test_create_bounding_box_from_points_2d():
43+
bbox = BoundingBox(dimensions=2)
44+
bbox.fit(np.array([[0, 0], [1, 1]]))
45+
assert bbox.origin[0] == 0
46+
assert bbox.origin[1] == 0
47+
assert bbox.maximum[0] == 1
48+
assert bbox.maximum[1] == 1
49+
assert np.all(bbox.bb == np.array([[0, 0], [1, 1]]))
50+
assert bbox.valid is True
51+
52+
53+
def test_create_3d_bounding_box_from_2d_points():
54+
bbox = BoundingBox(dimensions=3)
55+
try:
56+
bbox.fit(np.array([[0, 0], [1, 1]]))
57+
except Exception as e:
58+
assert str(e) == "locations array is 2D but bounding box is 3"
59+
else:
60+
assert False
61+
62+
63+
def test_create_with_buffer():
64+
bbox = BoundingBox(origin=[0, 0, 0], maximum=[1, 1, 1])
65+
bbox = bbox.with_buffer(0.2)
66+
assert bbox.origin[0] == -0.2
67+
assert bbox.origin[1] == -0.2
68+
assert bbox.origin[2] == -0.2
69+
assert bbox.maximum[0] == 1.2
70+
assert bbox.maximum[1] == 1.2
71+
assert bbox.maximum[2] == 1.2
72+
assert np.all(bbox.bb == np.array([[-0.2, -0.2, -0.2], [1.2, 1.2, 1.2]]))
73+
assert bbox.valid is True
74+
75+
76+
def test_is_inside():
77+
bbox = BoundingBox(origin=[0, 0, 0], maximum=[1, 1, 1])
78+
assert np.all(bbox.is_inside(np.array([0.5, 0.5, 0.5])))
79+
assert not np.all(bbox.is_inside(np.array([0.5, 0.5, 1.5])))
80+
assert not np.all(bbox.is_inside(np.array([0.5, 0.5, -0.5])))
81+
assert not np.all(bbox.is_inside(np.array([0.5, 1.5, 0.5])))
82+
assert not np.all(bbox.is_inside(np.array([0.5, -0.5, 0.5])))
83+
assert not np.all(bbox.is_inside(np.array([1.5, 0.5, 0.5])))
84+
assert not np.all(bbox.is_inside(np.array([-0.5, 0.5, 0.5])))
85+
86+
87+
def test_regular_grid_3d():
88+
bbox = BoundingBox(origin=[0, 0, 0], maximum=[1, 1, 1])
89+
print(bbox.dimensions)
90+
grid = bbox.regular_grid((10, 10, 10))
91+
assert grid.shape == (10 * 10 * 10, 3)
92+
93+
94+
def test_regular_grid_2d():
95+
bbox = BoundingBox(origin=[0, 0], maximum=[1, 1], dimensions=2)
96+
print(bbox.dimensions)
97+
grid = bbox.regular_grid((10, 10))
98+
assert grid.shape == (10 * 10, 2)
99+
100+
101+
if __name__ == "__main__":
102+
test_create_bounding_box()
103+
test_create_bounding_box_from_points()
104+
test_create_with_buffer()
105+
test_is_inside()
106+
test_regular_grid_3d()
107+
test_regular_grid_2d()

0 commit comments

Comments
 (0)