| """ |
| Pyrender-based voxel visualization renderer. |
| |
| Builds colored cube meshes from voxel centers and renders with pyrender |
| using stored camera intrinsics/extrinsics. |
| """ |
|
|
| import colorsys |
|
|
| import numpy as np |
| import trimesh |
| import pyrender |
|
|
|
|
| def build_palette(names): |
| """Assign evenly-spaced HSV hues; returns dict[name] -> (R, G, B) uint8.""" |
| n = max(len(names), 1) |
| palette = {} |
| for i, name in enumerate(names): |
| hue = i / n |
| r, g, b = colorsys.hsv_to_rgb(hue, 200 / 255, 230 / 255) |
| palette[name] = (int(r * 255), int(g * 255), int(b * 255)) |
| return palette |
|
|
|
|
| def compute_intrinsics(intr): |
| """Convert Blender camera intrinsics dict to (fx, fy, cx, cy).""" |
| focal = intr["focal_length"] |
| sw, sh = intr["sensor_width"], intr["sensor_height"] |
| res_x, res_y = intr["resolution_x"], intr["resolution_y"] |
| fit = intr.get("sensor_fit", "AUTO") |
|
|
| if fit == "VERTICAL": |
| fpx = focal / sh * res_y |
| elif fit == "HORIZONTAL": |
| fpx = focal / sw * res_x |
| else: |
| fpx = focal / sw * res_x if res_x >= res_y else focal / sh * res_y |
|
|
| fx = fy = float(fpx) |
| cx, cy = res_x / 2.0, res_y / 2.0 |
| return fx, fy, cx, cy |
|
|
|
|
| def blender_cam_to_pyrender_pose(cam_extrinsics): |
| """ |
| Normalize Blender's camera-to-world 4x4 for pyrender. |
| Blender cameras use OpenGL convention (X right, Y up, -Z forward). |
| """ |
| cam_mat = np.array(cam_extrinsics, dtype=np.float64) |
| scale = np.linalg.norm(cam_mat[:3, 0]) |
| if abs(scale - 1.0) > 1e-6: |
| cam_mat[:3, :3] /= scale |
| return cam_mat |
|
|
|
|
| def build_voxel_mesh(centers_dict, voxel_sizes, palette): |
| """ |
| Create a single combined trimesh from per-object voxel centers. |
| Each object's cubes are sized by its voxel_size and colored by the palette. |
| """ |
| all_vertices = [] |
| all_faces = [] |
| all_colors = [] |
| vertex_offset = 0 |
|
|
| for name, centers in centers_dict.items(): |
| if centers.shape[0] == 0: |
| continue |
|
|
| voxel_size = voxel_sizes[name] |
| r, g, b = palette.get(name, (200, 200, 200)) |
|
|
| box = trimesh.creation.box(extents=[voxel_size] * 3) |
| n_verts_per_box = len(box.vertices) |
| n_faces_per_box = len(box.faces) |
| n_voxels = len(centers) |
|
|
| vertices = np.tile(box.vertices, (n_voxels, 1)) |
| vertices += np.repeat(centers.astype(np.float64), n_verts_per_box, axis=0) |
|
|
| faces = np.tile(box.faces, (n_voxels, 1)) |
| faces += np.repeat(np.arange(n_voxels) * n_verts_per_box, |
| n_faces_per_box).reshape(-1, 1) |
| faces += vertex_offset |
|
|
| rgba = np.full((n_voxels * n_verts_per_box, 4), 255, dtype=np.uint8) |
| rgba[:, 0], rgba[:, 1], rgba[:, 2] = r, g, b |
|
|
| all_vertices.append(vertices) |
| all_faces.append(faces) |
| all_colors.append(rgba) |
| vertex_offset += n_voxels * n_verts_per_box |
|
|
| if not all_vertices: |
| return None |
|
|
| return trimesh.Trimesh( |
| vertices=np.vstack(all_vertices), |
| faces=np.vstack(all_faces), |
| vertex_colors=np.vstack(all_colors), |
| process=False, |
| ) |
|
|
|
|
| def render_frame(mesh, fx, fy, cx, cy, pose, W, H, background=None): |
| """ |
| Render a single frame with pyrender offscreen. |
| Returns an RGB uint8 image (H, W, 3). |
| If background (RGB) is provided, composites voxels on top. |
| """ |
| if mesh is None: |
| if background is not None: |
| return background.copy() |
| return np.zeros((H, W, 3), dtype=np.uint8) |
|
|
| scene = pyrender.Scene(bg_color=[0, 0, 0, 0], ambient_light=[0.3, 0.3, 0.3]) |
| scene.add(pyrender.Mesh.from_trimesh(mesh, smooth=False)) |
|
|
| camera = pyrender.IntrinsicsCamera(fx=fx, fy=fy, cx=cx, cy=cy, |
| znear=0.05, zfar=1000.0) |
| scene.add(camera, pose=pose) |
|
|
| light = pyrender.DirectionalLight(color=[1.0, 1.0, 1.0], intensity=3.0) |
| scene.add(light, pose=pose) |
|
|
| r = pyrender.OffscreenRenderer(viewport_width=W, viewport_height=H) |
| color_rgb, depth = r.render(scene) |
| r.delete() |
|
|
| if background is not None: |
| out = background.copy() |
| mask = depth > 0 |
| out[mask] = color_rgb[mask] |
| return out |
|
|
| return color_rgb |
|
|