Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
ray mesh intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
dwastberg committed Sep 30, 2024
1 parent aa0eeec commit 58a4069
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/dtcc_builder/geometry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from . import multisurface, surface
from . import multisurface, surface, mesh

__all__ = [
"multisurface",
"surface",
]
__all__ = ["multisurface", "surface", "mesh"]
39 changes: 39 additions & 0 deletions src/dtcc_builder/geometry/mesh.py
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 24 additions & 1 deletion tests/python/test_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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())

0 comments on commit 58a4069

Please sign in to comment.