Point cloud visualization

Visualize 3D point clouds with colors.

Point clouds are fundamental for many 3D computer vision applications like SLAM, 3D reconstruction, and neural radiance fields. This example demonstrates how to use viser.SceneApi.add_point_cloud() to display point clouds with per-point colors.

The example shows two different point clouds:

  1. A spiral point cloud with height-based color gradient (blue to red)

  2. A random noise cloud with random colors for each point

We also add a coordinate frame using viser.SceneApi.add_frame() to provide spatial reference. Point clouds support various parameters like point_size to control visual appearance.

Source: examples/01_scene/01_point_clouds.py

Point cloud visualization

Code

 1import numpy as np
 2
 3import viser
 4
 5
 6def main():
 7    server = viser.ViserServer()
 8
 9    # Generate a spiral point cloud.
10    num_points = 200
11    t = np.linspace(0, 10, num_points)
12    spiral_positions = np.column_stack(
13        [
14            np.sin(t) * (1 + t / 10),
15            np.cos(t) * (1 + t / 10),
16            t / 5,
17        ]
18    )
19
20    # Create colors based on height (z-coordinate).
21    z_min, z_max = spiral_positions[:, 2].min(), spiral_positions[:, 2].max()
22    normalized_z = (spiral_positions[:, 2] - z_min) / (z_max - z_min)
23
24    # Color gradient from blue (bottom) to red (top).
25    colors = np.zeros((num_points, 3), dtype=np.uint8)
26    colors[:, 0] = (normalized_z * 255).astype(np.uint8)  # Red channel.
27    colors[:, 2] = ((1 - normalized_z) * 255).astype(np.uint8)  # Blue channel.
28
29    # Add the point cloud to the scene.
30    server.scene.add_point_cloud(
31        name="/spiral_cloud",
32        points=spiral_positions,
33        colors=colors,
34        point_size=0.05,
35    )
36
37    # Add a second point cloud - random noise points.
38    num_noise_points = 500
39    noise_positions = np.random.normal(0, 1, (num_noise_points, 3))
40    noise_colors = np.random.randint(0, 255, (num_noise_points, 3), dtype=np.uint8)
41
42    server.scene.add_point_cloud(
43        name="/noise_cloud",
44        points=noise_positions,
45        colors=noise_colors,
46        point_size=0.03,
47    )
48
49    print("Point cloud visualization loaded!")
50    print("- Spiral point cloud with height-based colors")
51    print("- Random noise point cloud with random colors")
52    print("Visit: http://localhost:8080")
53
54    while True:
55        pass
56
57
58if __name__ == "__main__":
59    main()