2222from ..base_sensor import Sensor
2323
2424
25- def get_link_mesh (link , use_visual_mesh = False ):
26- """
27- Extract mesh from a given link.
28-
29- Args:
30- link: Genesis link object
31- use_visual_mesh: Whether to use visual mesh (True) or collision mesh (False)
32-
33- Returns:
34- trimesh.Trimesh: Combined mesh for the link
35- """
36- meshes = []
37- if use_visual_mesh :
38- geoms = link .vgeoms
39- else :
40- geoms = link .geoms
41-
42- for i , geom in enumerate (geoms ):
43- T = trans_quat_to_T (
44- (geom .get_pos () - link .get_pos ()).cpu (),
45- R_to_quat ((quat_to_R (geom .get_quat ()) @ torch .linalg .inv (quat_to_R (link .get_quat ())))).cpu (),
46- )
47- if T .ndim == 3 :
48- T = T [0 ] # NOTE: we use the canonical space so batch can be ignored
49- mesh = geom .get_trimesh ().copy () # NOTE: avoid in-place write
50- mesh .apply_transform (T )
51- meshes .append (mesh )
52-
53- if len (meshes ) == 0 :
54- # Return empty mesh if no geometry
55- return trimesh .Trimesh ()
56-
57- combined_mesh = trimesh .util .concatenate (meshes )
58- return combined_mesh
59-
60-
6125# Global constants for LiDAR ray casting - will be initialized when needed
6226NO_HIT_RAY_VAL = None
6327NO_HIT_SEGMENTATION_VAL = None
@@ -101,10 +65,6 @@ def __init__(self, vertices: np.ndarray, triangles: np.ndarray):
10165 self .vertices .from_numpy (vertices .astype (np .float32 ))
10266 self .triangles .from_numpy (triangles .astype (np .int32 ))
10367
104- # Pre-store numpy arrays for kernel usage (static mesh optimization)
105- self .vertices_np = vertices .astype (np .float32 )
106- self .triangles_np = triangles .astype (np .int32 )
107-
10868 # Build BVH acceleration structure
10969 self ._build_bvh ()
11070
@@ -264,9 +224,9 @@ def ray_aabb_intersection(ray_start, ray_dir, aabb_min, aabb_max):
264224
265225@ti .kernel
266226def lidar_cast_rays_kernel_bvh (
267- # Mesh data
268- mesh_vertices : ti .types . ndarray ( ndim = 2 ), # [n_vertices, 3]
269- mesh_triangles : ti .types . ndarray ( ndim = 2 ), # [n_triangles, 3]
227+ # Mesh data (now using Taichi fields directly - no CPU->GPU transfer!)
228+ mesh_vertices : ti .template ( ), # GPU Taichi field [n_vertices, 3]
229+ mesh_triangles : ti .template ( ), # GPU Taichi field [n_triangles, 3]
270230 # BVH data structures
271231 bvh_nodes : ti .template (), # The BVH node tree
272232 bvh_morton_codes : ti .template (), # Maps sorted leaves to original triangle indices
@@ -281,7 +241,7 @@ def lidar_cast_rays_kernel_bvh(
281241 world_frame : ti .i32 ,
282242):
283243 """
284- Taichi kernel for LiDAR ray casting, accelerated by a Bounding Volume Hierarchy (BVH) .
244+ Taichi kernel for LiDAR ray casting.
285245 """
286246 n_triangles = mesh_triangles .shape [0 ]
287247
@@ -336,15 +296,10 @@ def lidar_cast_rays_kernel_bvh(
336296 # We need to find the original triangle index.
337297 sorted_leaf_idx = node_idx - (n_triangles - 1 )
338298 original_tri_idx = bvh_morton_codes [0 , sorted_leaf_idx ][1 ]
339-
340- # Get triangle vertices
341- v0_idx = mesh_triangles [original_tri_idx , 0 ]
342- v1_idx = mesh_triangles [original_tri_idx , 1 ]
343- v2_idx = mesh_triangles [original_tri_idx , 2 ]
344-
345- v0 = ti .math .vec3 (mesh_vertices [v0_idx , 0 ], mesh_vertices [v0_idx , 1 ], mesh_vertices [v0_idx , 2 ])
346- v1 = ti .math .vec3 (mesh_vertices [v1_idx , 0 ], mesh_vertices [v1_idx , 1 ], mesh_vertices [v1_idx , 2 ])
347- v2 = ti .math .vec3 (mesh_vertices [v2_idx , 0 ], mesh_vertices [v2_idx , 1 ], mesh_vertices [v2_idx , 2 ])
299+ tri_indices = mesh_triangles [original_tri_idx ]
300+ v0 = mesh_vertices [tri_indices [0 ]]
301+ v1 = mesh_vertices [tri_indices [1 ]]
302+ v2 = mesh_vertices [tri_indices [2 ]]
348303
349304 # Perform the expensive ray-triangle intersection test
350305 hit_result = ray_triangle_intersection (lidar_position , ray_direction_world , v0 , v1 , v2 )
@@ -413,8 +368,8 @@ def cast_rays(
413368
414369 # Call the BVH-accelerated kernel
415370 lidar_cast_rays_kernel_bvh (
416- self .mesh_data .vertices_np ,
417- self .mesh_data .triangles_np ,
371+ self .mesh_data .vertices ,
372+ self .mesh_data .triangles ,
418373 self .mesh_data .bvh .nodes ,
419374 self .mesh_data .bvh .morton_codes ,
420375 lidar_positions ,
0 commit comments