44from compas_libigl import _intersections
55
66
7+ def _conversion_libigl_to_compas (hits_per_ray ):
8+ """Convert libigl barycentric coordinates to COMPAS barycentric coordinates.
9+
10+ Parameters
11+ ----------
12+ hits_per_ray : list[tuple[int, float, float, float]]
13+ Tuples of (face_index, u, v, distance) from libigl ray intersection
14+
15+ Returns
16+ -------
17+ list[tuple[int, float, float, float]]
18+ Tuples of (face_index, w, u, v) in COMPAS barycentric coordinate ordering
19+
20+ Note
21+ ----
22+ libigl uses: P = (1-u-v)*v0 + u*v1 + v*v2
23+ This function returns [w, u, v] = [1-u-v, u, v] to match COMPAS ordering
24+ """
25+
26+ hits_compas = []
27+ for h in hits_per_ray :
28+ idx , u , v , _ = h
29+ w = 1.0 - u - v
30+ hits_compas .append ([idx , w , v , u ])
31+ return hits_compas
32+
33+
34+ def barycenter_to_point (u , v , w , p1 , p2 , p3 ):
35+ """Convert COMPAS barycentric coordinates to a point.
36+
37+ Parameters
38+ ----------
39+ u : float
40+ The u coordinate
41+ v : float
42+ The v coordinate
43+ w : float
44+ The w coordinate
45+ p1 : tuple[float, float, float]
46+ The first point
47+ p2 : tuple[float, float, float]
48+ The second point
49+ p3 : tuple[float, float, float]
50+ The third point
51+
52+
53+ Returns
54+ -------
55+ list[float]
56+ The point at the intersection of the ray and the mesh
57+
58+ Note
59+ ----
60+ libigl uses: P = (1-u-v)*v0 + u*v1 + v*v2
61+ This function returns [w, u, v] = [1-u-v, u, v] to match COMPAS ordering
62+ """
63+ w = 1 - u - v # barycentric coordinates
64+
65+ phit = [u * p1 [0 ] + v * p2 [0 ] + w * p3 [0 ], u * p1 [1 ] + v * p2 [1 ] + w * p3 [1 ], u * p1 [2 ] + v * p2 [2 ] + w * p3 [2 ]]
66+
67+ return phit
68+
69+
770@plugin (category = "intersections" )
871def intersection_ray_mesh (ray , M ):
972 """Compute the intersection(s) between a ray and a mesh.
@@ -24,16 +87,29 @@ def intersection_ray_mesh(ray, M):
2487
2588 0. the index of the intersected face
2689 1. the u coordinate of the intersection in the barycentric coordinates of the face
27- 2. the u coordinate of the intersection in the barycentric coordinates of the face
90+ 2. the v coordinate of the intersection in the barycentric coordinates of the face
2891 3. the distance between the ray origin and the hit
92+
93+ Note
94+ ----
95+ The barycentric coordinates (u, v) follow the libigl convention where:
96+ - For a triangle with vertices (v0, v1, v2) at face indices F[face_id]
97+ - The intersection point P = (1-u-v)*v0 + u*v1 + v*v2
98+ - This differs from COMPAS barycentric_coordinates which uses a different vertex ordering
2999 """
30100 point , vector = ray
31101 vertices , faces = M
32102 P = np .asarray (point , dtype = np .float64 )
33103 D = np .asarray (vector , dtype = np .float64 )
34104 V = np .asarray (vertices , dtype = np .float64 )
35105 F = np .asarray (faces , dtype = np .int32 )
36- return _intersections .intersection_ray_mesh (P , D , V , F )
106+
107+ hits_per_ray = _intersections .intersection_ray_mesh (P , D , V , F )
108+
109+ # Convert libigl barycentric coordinates to COMPAS convention
110+ hits_compas = _conversion_libigl_to_compas (hits_per_ray )
111+
112+ return hits_compas
37113
38114
39115def intersection_rays_mesh (rays , M ):
@@ -55,7 +131,7 @@ def intersection_rays_mesh(rays, M):
55131
56132 0. the index of the intersected face
57133 1. the u coordinate of the intersection in the barycentric coordinates of the face
58- 2. the u coordinate of the intersection in the barycentric coordinates of the face
134+ 2. the v coordinate of the intersection in the barycentric coordinates of the face
59135 3. the distance between the ray origin and the hit
60136 """
61137 points , vectors = zip (* rays )
@@ -64,4 +140,12 @@ def intersection_rays_mesh(rays, M):
64140 D = np .asarray (vectors , dtype = np .float64 )
65141 V = np .asarray (vertices , dtype = np .float64 )
66142 F = np .asarray (faces , dtype = np .int32 )
67- return _intersections .intersection_rays_mesh (P , D , V , F )
143+
144+ hits_per_ray = _intersections .intersection_rays_mesh (P , D , V , F )
145+
146+ # Convert libigl barycentric coordinates to COMPAS convention
147+ hits_per_ray_compas = []
148+ for hit in hits_per_ray :
149+ hits_per_ray_compas .append (_conversion_libigl_to_compas (hit ))
150+
151+ return hits_per_ray_compas
0 commit comments