Skip to content

Commit 207de00

Browse files
committed
optimized meshdata transmit.
1 parent fb56c8e commit 207de00

File tree

1 file changed

+10
-58
lines changed

1 file changed

+10
-58
lines changed

genesis/sensors/lidar/lidar.py

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,6 @@
2222
from ..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(
46-
(
47-
quat_to_R(geom.get_quat())
48-
@ torch.linalg.inv(quat_to_R(link.get_quat()))
49-
)
50-
).cpu(),
51-
)
52-
if T.ndim == 3:
53-
T = T[0] # NOTE: we use the canonical space so batch can be ignored
54-
mesh = geom.get_trimesh().copy() # NOTE: avoid in-place write
55-
mesh.apply_transform(T)
56-
meshes.append(mesh)
57-
58-
if len(meshes) == 0:
59-
# Return empty mesh if no geometry
60-
return trimesh.Trimesh()
61-
62-
combined_mesh = trimesh.util.concatenate(meshes)
63-
return combined_mesh
64-
65-
6625
# Global constants for LiDAR ray casting - will be initialized when needed
6726
NO_HIT_RAY_VAL = None
6827
NO_HIT_SEGMENTATION_VAL = None
@@ -105,9 +64,6 @@ def __init__(self, vertices: np.ndarray, triangles: np.ndarray):
10564
self.vertices.from_numpy(vertices.astype(np.float32))
10665
self.triangles.from_numpy(triangles.astype(np.int32))
10766

108-
# Pre-store numpy arrays for kernel usage (static mesh optimization)
109-
self.vertices_np = vertices.astype(np.float32)
110-
self.triangles_np = triangles.astype(np.int32)
11167

11268
# Build BVH acceleration structure
11369
self._build_bvh()
@@ -265,9 +221,9 @@ def ray_aabb_intersection(ray_start, ray_dir, aabb_min, aabb_max):
265221

266222
@ti.kernel
267223
def lidar_cast_rays_kernel_bvh(
268-
# Mesh data
269-
mesh_vertices: ti.types.ndarray(ndim=2), # [n_vertices, 3]
270-
mesh_triangles: ti.types.ndarray(ndim=2), # [n_triangles, 3]
224+
# Mesh data (now using Taichi fields directly - no CPU->GPU transfer!)
225+
mesh_vertices: ti.template(), # GPU Taichi field [n_vertices, 3]
226+
mesh_triangles: ti.template(), # GPU Taichi field [n_triangles, 3]
271227

272228
# BVH data structures
273229
bvh_nodes: ti.template(), # The BVH node tree
@@ -285,7 +241,7 @@ def lidar_cast_rays_kernel_bvh(
285241
world_frame: ti.i32,
286242
):
287243
"""
288-
Taichi kernel for LiDAR ray casting, accelerated by a Bounding Volume Hierarchy (BVH).
244+
Taichi kernel for LiDAR ray casting.
289245
"""
290246
n_triangles = mesh_triangles.shape[0]
291247

@@ -342,15 +298,11 @@ def lidar_cast_rays_kernel_bvh(
342298
# We need to find the original triangle index.
343299
sorted_leaf_idx = node_idx - (n_triangles - 1)
344300
original_tri_idx = bvh_morton_codes[0, sorted_leaf_idx][1]
301+
tri_indices = mesh_triangles[original_tri_idx]
302+
v0 = mesh_vertices[tri_indices[0]]
303+
v1 = mesh_vertices[tri_indices[1]]
304+
v2 = mesh_vertices[tri_indices[2]]
345305

346-
# Get triangle vertices
347-
v0_idx = mesh_triangles[original_tri_idx, 0]
348-
v1_idx = mesh_triangles[original_tri_idx, 1]
349-
v2_idx = mesh_triangles[original_tri_idx, 2]
350-
351-
v0 = ti.math.vec3(mesh_vertices[v0_idx, 0], mesh_vertices[v0_idx, 1], mesh_vertices[v0_idx, 2])
352-
v1 = ti.math.vec3(mesh_vertices[v1_idx, 0], mesh_vertices[v1_idx, 1], mesh_vertices[v1_idx, 2])
353-
v2 = ti.math.vec3(mesh_vertices[v2_idx, 0], mesh_vertices[v2_idx, 1], mesh_vertices[v2_idx, 2])
354306

355307
# Perform the expensive ray-triangle intersection test
356308
hit_result = ray_triangle_intersection(lidar_position, ray_direction_world, v0, v1, v2)
@@ -417,8 +369,8 @@ def cast_rays(self, lidar_positions, lidar_quaternions, ray_vectors, far_plane,
417369

418370
# Call the BVH-accelerated kernel
419371
lidar_cast_rays_kernel_bvh(
420-
self.mesh_data.vertices_np,
421-
self.mesh_data.triangles_np,
372+
self.mesh_data.vertices,
373+
self.mesh_data.triangles,
422374
self.mesh_data.bvh.nodes,
423375
self.mesh_data.bvh.morton_codes,
424376
lidar_positions, lidar_quaternions, ray_vectors,

0 commit comments

Comments
 (0)