Skip to content

Commit 3d90210

Browse files
authored
fix: step_vector take into account dimensions=2
2 parents b9c7500 + 9677e0e commit 3d90210

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

.github/workflows/release-please.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
documentation-deploy:
7979
runs-on: ubuntu-latest
8080
needs: release-please
81-
# if: ${{ needs.release-please.outputs.release_created }}
81+
if: ${{ needs.release-please.outputs.release_created }}
8282
steps:
8383
- uses: actions/checkout@v4
8484
- run: |
@@ -92,7 +92,7 @@ jobs:
9292
folder: docs/build/html # The folder the action should deploy.
9393

9494
conda-deploy:
95-
name: Uploading to Loop3d for python ${{ matrix.os }})
95+
name: Building conda package for python ${{ matrix.os }})
9696
needs: ["documentation-test", "continuous-integration"]
9797
runs-on: ${{matrix.os}}
9898
strategy:

LoopStructural/datatypes/_bounding_box.py

Lines changed: 16 additions & 10 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 = {
@@ -93,7 +99,7 @@ def nelements(self, nelements):
9399
box_vol = self.volume
94100
ele_vol = box_vol / nelements
95101
# calculate the step vector of a regular cube
96-
step_vector = np.zeros(3)
102+
step_vector = np.zeros(self.dimensions)
97103
step_vector[:] = ele_vol ** (1.0 / 3.0)
98104
# number of steps is the length of the box / step vector
99105
nsteps = np.ceil((self.maximum - self.origin) / step_vector).astype(int)
@@ -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)

tests/unit/utils/test_bounding_box.py renamed to tests/unit/datatypes/test_bounding_box.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,24 @@ def test_create_bounding_box():
1010
assert bbox.maximum[0] == 1
1111
assert bbox.maximum[1] == 1
1212
assert bbox.maximum[2] == 1
13+
assert bbox.dimensions == 3
1314
assert np.all(bbox.bb == np.array([[0, 0, 0], [1, 1, 1]]))
1415
assert bbox.valid is True
1516

1617

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+
1729
def test_create_bounding_box_from_points():
18-
bbox = BoundingBox()
30+
bbox = BoundingBox(dimensions=3)
1931
bbox.fit(np.array([[0, 0, 0], [1, 1, 1]]))
2032
assert bbox.origin[0] == 0
2133
assert bbox.origin[1] == 0
@@ -27,6 +39,27 @@ def test_create_bounding_box_from_points():
2739
assert bbox.valid is True
2840

2941

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+
3063
def test_create_with_buffer():
3164
bbox = BoundingBox(origin=[0, 0, 0], maximum=[1, 1, 1])
3265
bbox = bbox.with_buffer(0.2)
@@ -51,8 +84,24 @@ def test_is_inside():
5184
assert not np.all(bbox.is_inside(np.array([-0.5, 0.5, 0.5])))
5285

5386

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+
54101
if __name__ == "__main__":
55102
test_create_bounding_box()
56103
test_create_bounding_box_from_points()
57104
test_create_with_buffer()
58105
test_is_inside()
106+
test_regular_grid_3d()
107+
test_regular_grid_2d()

0 commit comments

Comments
 (0)