Skip to content

Commit 53b27b5

Browse files
authored
Merge pull request #1473 from dominikreisach/datastructures-bounding-boxes
added bounding box computations for Datastructure class, added to_point method for Mesh class
2 parents 96c2ea7 + 1bc1dcd commit 53b27b5

File tree

10 files changed

+113
-3
lines changed

10 files changed

+113
-3
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@
4242
- Joseph Kenny <<[email protected]>> [@jckenny59](https://github.com/jckenny59)
4343
- Panayiotis Papacharalambous <<[email protected]>> [@papachap](https://github.com/papachap)
4444
- Oliver Bucklin <<[email protected]>> [@obucklin](https://github.com/obucklin)
45+
- Dominik Reisach <<[email protected]>> [@dominikreisach](https://github.com/dominikreisach)

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## Unreleased
99

1010
### Added
11+
* Implemented `to_points` method in `compas.datastructures.Mesh`, which before raised a `NotImplementedError`.
12+
* Implemented `compute_aabb` method in `compas.datastructures.Datastructure`, which before raised a `NotImplementedError`. Made use of the `compas.geometry.bbox.bounding_box` function.
13+
* Implemented `compute_obb` method in `compas.datastructures.Datastructure`, which before raised a `NotImplementedError`. Made use of the `compas.geometry.bbox_numpy.oriented_bounding_box_numpy` function.
14+
* Added `vertices_to_points` method in `compas.datastructures.CellNetwork`.
15+
* Added `to_points` method in `compas.datastructures.VolMesh`.
16+
* Added test function `test_vertices_to_points`in `test_cell_network.py`.
17+
* Added test function `test_to_points` in `test_graph.py`.
18+
* Added test function `test_to_points` in `test_volmesh.py`.
19+
* Added test functions `test_to_points`, `test_compute_aabb`, and `test_compute_obb` in `test_mesh.py`.
1120

1221
### Changed
1322

src/compas/datastructures/cell_network/cell_network.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,17 @@ def faces_to_mesh(self, faces, data=False):
11711171
mesh.add_face(vertices, fkey=fkey)
11721172
return mesh
11731173

1174+
def vertices_to_points(self):
1175+
"""Convert the vertices of the cell network to a collection of points.
1176+
1177+
Returns
1178+
-------
1179+
list[list[float]]
1180+
The points representing the vertices of the cell network.
1181+
1182+
"""
1183+
return [self.vertex_coordinates(vertex) for vertex in self.vertices()]
1184+
11741185
# --------------------------------------------------------------------------
11751186
# General
11761187
# --------------------------------------------------------------------------

src/compas/datastructures/datastructure.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ def compute_aabb(self):
8686
:class:`compas.geometry.Box`
8787
8888
"""
89-
raise NotImplementedError
89+
from compas.geometry import Box
90+
from compas.geometry.bbox import bounding_box
91+
92+
return Box.from_bounding_box(bounding_box(self.to_points()))
9093

9194
def compute_obb(self):
9295
"""Compute the oriented bounding box of the datastructure.
@@ -96,7 +99,10 @@ def compute_obb(self):
9699
:class:`compas.geometry.Box`
97100
98101
"""
99-
raise NotImplementedError
102+
from compas.geometry import Box
103+
from compas.geometry.bbox_numpy import oriented_bounding_box_numpy
104+
105+
return Box.from_bounding_box(oriented_bounding_box_numpy(self.to_points()))
100106

101107
def transform(self, transformation):
102108
"""Transforms the data structure.

src/compas/datastructures/mesh/mesh.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ def to_points(self):
732732
The points representing the vertices of the mesh.
733733
734734
"""
735-
raise NotImplementedError
735+
return [self.vertex_coordinates(vertex) for vertex in self.vertices()]
736736

737737
def to_polygons(self):
738738
"""Convert the mesh to a collection of polygons.

src/compas/datastructures/volmesh/volmesh.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,17 @@ def to_vertices_and_cells(self):
559559
cells.append(faces)
560560
return vertices, cells
561561

562+
def to_points(self):
563+
"""Convert the volmesh to a collection of points.
564+
565+
Returns
566+
-------
567+
list[float[float]]
568+
The points representing the vertices of the volmesh.
569+
570+
"""
571+
return [self.vertex_coordinates(vertex) for vertex in self.vertices()]
572+
562573
def cell_to_mesh(self, cell):
563574
# type: (int) -> Mesh
564575
"""Construct a mesh object from from a cell of a volmesh.

tests/compas/datastructures/test_cell_network.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,14 @@ def test_cell_network_boundary(example_cell_network):
8383
assert set(ds.faces_without_cell()) == {11}
8484
assert set(ds.edges_without_face()) == {(15, 13), (14, 12)}
8585
assert set(ds.nonmanifold_edges()) == {(6, 7), (4, 5), (5, 6), (7, 4)}
86+
87+
88+
# ==============================================================================
89+
# Conversion
90+
# ==============================================================================
91+
92+
93+
def test_vertices_to_points(example_cell_network):
94+
ds = example_cell_network
95+
points = ds.vertices_to_points()
96+
assert len(points) == ds.number_of_vertices()

tests/compas/datastructures/test_graph.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,20 @@ def test_graph_to_networkx():
276276
# assert g2.attributes["val"] == (0, 0, 0), "Graph attributes must be preserved"
277277

278278

279+
@pytest.mark.parametrize(
280+
"filepath",
281+
[
282+
compas.get("lines.obj"),
283+
compas.get("grid_irregular.obj"),
284+
],
285+
)
286+
def test_to_points(filepath):
287+
graph = Graph.from_obj(filepath)
288+
points = graph.to_points()
289+
290+
assert len(points) == graph.number_of_nodes(), "Number of points must match number of nodes"
291+
292+
279293
# ==============================================================================
280294
# Methods
281295
# ==============================================================================

tests/compas/datastructures/test_mesh.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,23 @@ def test_to_lines():
340340
assert len(lines) == mesh.number_of_edges()
341341

342342

343+
def test_to_points():
344+
# tri
345+
mesh = Mesh.from_shape(Polyhedron.from_platonicsolid(4))
346+
points = mesh.to_points()
347+
assert len(points) == 4
348+
349+
# quad
350+
mesh = Mesh.from_shape(Polyhedron.from_platonicsolid(6))
351+
points = mesh.to_points()
352+
assert len(points) == 8
353+
354+
# ngon
355+
mesh = Mesh.from_shape(Polyhedron.from_platonicsolid(12))
356+
points = mesh.to_points()
357+
assert len(points) == 20
358+
359+
343360
# --------------------------------------------------------------------------
344361
# helpers
345362
# --------------------------------------------------------------------------
@@ -1264,3 +1281,26 @@ def test_face_attributes_includes_all_defaults(box):
12641281
]
12651282

12661283
assert box.face_attribute(random_fkey, "attr3") == "value3"
1284+
1285+
1286+
# --------------------------------------------------------------------------
1287+
# bounding volumes
1288+
# --------------------------------------------------------------------------
1289+
1290+
if not compas.IPY:
1291+
1292+
def test_compute_aabb():
1293+
mesh = Mesh.from_obj(compas.get("tubemesh.obj"))
1294+
aabb = mesh.compute_aabb()
1295+
1296+
assert isinstance(aabb, Box)
1297+
assert len(aabb.points) == 8
1298+
assert aabb.contains_points(mesh.to_points())
1299+
1300+
def test_compute_obb():
1301+
mesh = Mesh.from_obj(compas.get("tubemesh.obj"))
1302+
obb = mesh.compute_obb()
1303+
1304+
assert isinstance(obb, Box)
1305+
assert len(obb.points) == 8
1306+
assert obb.contains_points(mesh.to_points())

tests/compas/datastructures/test_volmesh.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ def test_cells_where_predicate():
346346
# Conversion
347347
# ==============================================================================
348348

349+
350+
def test_to_points():
351+
vmesh = VolMesh.from_obj(compas.get("boxes.obj"))
352+
points = vmesh.to_points()
353+
assert len(points) == 27
354+
355+
349356
# ==============================================================================
350357
# Methods
351358
# ==============================================================================

0 commit comments

Comments
 (0)