diff --git a/src/dtcc_builder/geometry/__init__.py b/src/dtcc_builder/geometry/__init__.py index fdf3844..6ad6cb2 100644 --- a/src/dtcc_builder/geometry/__init__.py +++ b/src/dtcc_builder/geometry/__init__.py @@ -1,6 +1,3 @@ -from . import multisurface, surface +from . import multisurface, surface, mesh -__all__ = [ - "multisurface", - "surface", -] +__all__ = ["multisurface", "surface", "mesh"] diff --git a/src/dtcc_builder/geometry/mesh.py b/src/dtcc_builder/geometry/mesh.py new file mode 100644 index 0000000..c0a8a62 --- /dev/null +++ b/src/dtcc_builder/geometry/mesh.py @@ -0,0 +1,39 @@ +from dtcc_builder import _dtcc_builder + +from dtcc_model import Surface, MultiSurface, Mesh +from dtcc_builder.register import register_model_method + +import numpy as np + + +@register_model_method +def ray_intersection( + mesh: Mesh, origin: np.ndarray, direction: np.ndarray +) -> np.ndarray: + """ + Compute the intersection points of a ray with a mesh. + + Args: + mesh (Mesh): The mesh. + origin (np.ndarray): The origin of the ray. + direction (np.ndarray): The direction of the ray. + + Returns: + np.ndarray: The intersection points. + """ + + origin = np.array(origin, dtype=np.float64) + direction = np.array(direction, dtype=np.float64) + ms = MultiSurface() + for face in mesh.faces: + s = Surface( + vertices=np.array( + ( + mesh.vertices[face[0]], + mesh.vertices[face[1]], + mesh.vertices[face[2]], + ) + ) + ) + ms.surfaces.append(s) + return ms.ray_intersection(origin, direction) diff --git a/tests/python/test_intersection.py b/tests/python/test_intersection.py index 230af73..d0a72b8 100644 --- a/tests/python/test_intersection.py +++ b/tests/python/test_intersection.py @@ -3,7 +3,7 @@ import numpy as np import dtcc_builder -from dtcc_model import Surface, MultiSurface +from dtcc_model import Surface, MultiSurface, Mesh class TestSurfaceIntersection(unittest.TestCase): @@ -51,3 +51,26 @@ def test_ray_miss(self): self.assertTrue(isinstance(intersection, np.ndarray)) self.assertTrue(np.isnan(intersection).all()) + + +class TestMeshIntersection(unittest.TestCase): + def test_mesh_ray_intersection(self): + vertices = np.array( + [ + [0, 0, 0], + [1, 0, 0], + [1, 1, 0], + [0, 1, 0], + [0, 0, 1], + [1, 0, 1], + [1, 1, 1], + [0, 1, 1], + ] + ) + faces = np.array([[0, 1, 2], [0, 2, 3], [4, 5, 6], [4, 6, 7]]) + mesh = Mesh(vertices=vertices, faces=faces) + origin = [0.5, 0.5, 2] + direction = [0, 0, -1] + intersection = mesh.ray_intersection(origin, direction) + self.assertTrue(isinstance(intersection, np.ndarray)) + self.assertTrue((intersection == [0.5, 0.5, 1]).all())