|
| 1 | +import numpy as np |
| 2 | +from compas.geometry import Point |
| 3 | +from compas.plugins import plugin |
| 4 | + |
| 5 | +from compas_cgal import measure_ext |
| 6 | + |
| 7 | +from .types import VerticesFaces |
| 8 | + |
| 9 | + |
| 10 | +def mesh_area(mesh: VerticesFaces) -> float: |
| 11 | + """Compute the area of a triangle mesh. |
| 12 | +
|
| 13 | + Parameters |
| 14 | + ---------- |
| 15 | + mesh : :attr:`compas_cgal.types.VerticesFaces` |
| 16 | + The mesh. |
| 17 | +
|
| 18 | + Returns |
| 19 | + ------- |
| 20 | + float |
| 21 | + The area of the mesh. |
| 22 | +
|
| 23 | + """ |
| 24 | + V, F = mesh |
| 25 | + V = np.asarray(V, dtype=np.float64, order="C") |
| 26 | + F = np.asarray(F, dtype=np.int32, order="C") |
| 27 | + return measure_ext.area(V, F) |
| 28 | + |
| 29 | + |
| 30 | +@plugin(category="trimesh", pluggable_name="trimesh_volume") |
| 31 | +def mesh_volume(mesh: VerticesFaces) -> float: |
| 32 | + """Compute the volume of a closed triangle mesh. |
| 33 | +
|
| 34 | + Parameters |
| 35 | + ---------- |
| 36 | + mesh : :attr:`compas_cgal.types.VerticesFaces` |
| 37 | + The mesh. |
| 38 | +
|
| 39 | + Returns |
| 40 | + ------- |
| 41 | + float |
| 42 | + The volume of the mesh. |
| 43 | +
|
| 44 | + Examples |
| 45 | + -------- |
| 46 | + >>> from compas.geometry import Box |
| 47 | + >>> from compas_cgal.measure import mesh_volume |
| 48 | +
|
| 49 | + >>> box = Box(1) |
| 50 | + >>> mesh = box.to_vertices_and_faces(triangulated=True) |
| 51 | +
|
| 52 | + >>> mesh_volume(mesh) |
| 53 | + 1.0 |
| 54 | +
|
| 55 | + """ |
| 56 | + V, F = mesh |
| 57 | + V = np.asarray(V, dtype=np.float64, order="C") |
| 58 | + F = np.asarray(F, dtype=np.int32, order="C") |
| 59 | + return measure_ext.volume(V, F) |
| 60 | + |
| 61 | + |
| 62 | +def mesh_centroid(mesh: VerticesFaces) -> list[float]: |
| 63 | + """Compute the centroid of a the volume of a closed triangle mesh. |
| 64 | +
|
| 65 | + Parameters |
| 66 | + ---------- |
| 67 | + mesh : :attr:`compas_cgal.types.VerticesFaces` |
| 68 | + The mesh. |
| 69 | +
|
| 70 | + Returns |
| 71 | + ------- |
| 72 | + list |
| 73 | + The centroid of the mesh. |
| 74 | +
|
| 75 | + """ |
| 76 | + V, F = mesh |
| 77 | + V = np.asarray(V, dtype=np.float64, order="C") |
| 78 | + F = np.asarray(F, dtype=np.int32, order="C") |
| 79 | + vector_of_double: measure_ext.VectorDouble = measure_ext.centroid(V, F) |
| 80 | + point = Point(*vector_of_double) |
| 81 | + return point |
0 commit comments