Ray Casting#

ray_casting_closest_geometry.py#

 1# ----------------------------------------------------------------------------
 2# -                        CloudViewer: www.cloudViewer.org                  -
 3# ----------------------------------------------------------------------------
 4# Copyright (c) 2018-2024 www.cloudViewer.org
 5# SPDX-License-Identifier: MIT
 6# ----------------------------------------------------------------------------
 7
 8import cloudViewer as cv3d
 9import numpy as np
10import matplotlib.pyplot as plt
11import matplotlib.animation as anim
12import sys
13
14if __name__ == "__main__":
15    cube = cv3d.t.geometry.TriangleMesh.from_legacy(
16        cv3d.geometry.ccMesh.create_box().translate([-1.2, -1.2, 0]))
17    sphere = cv3d.t.geometry.TriangleMesh.from_legacy(
18        cv3d.geometry.ccMesh.create_sphere(0.5).translate([0.7, 0.8, 0]))
19
20    scene = cv3d.t.geometry.RaycastingScene()
21    # Add triangle meshes and remember ids.
22    mesh_ids = {}
23    mesh_ids[scene.add_triangles(cube)] = 'cube'
24    mesh_ids[scene.add_triangles(sphere)] = 'sphere'
25
26    # Compute range.
27    xyz_range = np.linspace([-2, -2, -2], [2, 2, 2], num=64)
28    # Query_points is a [64,64,64,3] array.
29    query_points = np.stack(np.meshgrid(*xyz_range.T),
30                            axis=-1).astype(np.float32)
31    closest_points = scene.compute_closest_points(query_points)
32    distance = np.linalg.norm(query_points - closest_points['points'].numpy(),
33                              axis=-1)
34    rays = np.concatenate([query_points, np.ones_like(query_points)], axis=-1)
35    intersection_counts = scene.count_intersections(rays).numpy()
36    is_inside = intersection_counts % 2 == 1
37    distance[is_inside] *= -1
38    signed_distance = distance
39    closest_geometry = closest_points['geometry_ids'].numpy()
40
41    # We can visualize the slices of the distance field and closest geometry directly with matplotlib.
42    fig, axes = plt.subplots(1, 2)
43    print(
44        "Visualizing sdf and closest geometry at each point for a cube and sphere ..."
45    )
46
47    def show_slices(i=int):
48        print(f"Displaying slice no.: {i}")
49        if i >= 64:
50            sys.exit()
51        axes[0].imshow(signed_distance[:, :, i])
52        axes[1].imshow(closest_geometry[:, :, i])
53
54    animator = anim.FuncAnimation(fig, show_slices, interval=100)
55    plt.show()

ray_casting_sdf.py#

 1# ----------------------------------------------------------------------------
 2# -                        CloudViewer: www.cloudViewer.org                  -
 3# ----------------------------------------------------------------------------
 4# Copyright (c) 2018-2024 www.cloudViewer.org
 5# SPDX-License-Identifier: MIT
 6# ----------------------------------------------------------------------------
 7
 8import cloudViewer as cv3d
 9import numpy as np
10import matplotlib.pyplot as plt
11import matplotlib.animation as anim
12import sys
13
14if __name__ == "__main__":
15    # Load mesh and convert to cloudViewer.t.geometry.TriangleMesh .
16    armadillo_data = cv3d.data.ArmadilloMesh()
17    mesh = cv3d.io.read_triangle_mesh(armadillo_data.path)
18    mesh = cv3d.t.geometry.TriangleMesh.from_legacy(mesh)
19
20    # Create a scene and add the triangle mesh.
21    scene = cv3d.t.geometry.RaycastingScene()
22    scene.add_triangles(mesh)
23
24    min_bound = mesh.vertex.positions.min(0).numpy()
25    max_bound = mesh.vertex.positions.max(0).numpy()
26
27    xyz_range = np.linspace(min_bound, max_bound, num=64)
28
29    # Query_points is a [64,64,64,3] array.
30    query_points = np.stack(np.meshgrid(*xyz_range.T),
31                            axis=-1).astype(np.float32)
32
33    # Signed distance is a [64,64,64] array.
34    signed_distance = scene.compute_signed_distance(query_points)
35
36    # We can visualize the slices of the distance field directly with matplotlib.
37    fig = plt.figure()
38    print("Visualizing sdf at each point for the armadillo mesh ...")
39
40    def show_slices(i=int):
41        print(f"Displaying slice no.: {i}")
42        if i >= 64:
43            sys.exit()
44        plt.imshow(signed_distance.numpy()[:, :, i % 64])
45
46    animator = anim.FuncAnimation(fig, show_slices, interval=100)
47    plt.show()

ray_casting_to_image.py#

 1# ----------------------------------------------------------------------------
 2# -                        CloudViewer: www.cloudViewer.org                  -
 3# ----------------------------------------------------------------------------
 4# Copyright (c) 2018-2024 www.cloudViewer.org
 5# SPDX-License-Identifier: MIT
 6# ----------------------------------------------------------------------------
 7
 8import cloudViewer as cv3d
 9import numpy as np
10import matplotlib.pyplot as plt
11
12if __name__ == "__main__":
13    # Create meshes and convert to cloudViewer.t.geometry.TriangleMesh .
14    cube = cv3d.geometry.ccMesh.create_box().translate([0, 0, 0])
15    cube = cv3d.t.geometry.TriangleMesh.from_legacy(cube)
16    torus = cv3d.geometry.ccMesh.create_torus().translate([0, 0, 2])
17    torus = cv3d.t.geometry.TriangleMesh.from_legacy(torus)
18    sphere = cv3d.geometry.ccMesh.create_sphere(radius=0.5).translate([1, 2, 3])
19    sphere = cv3d.t.geometry.TriangleMesh.from_legacy(sphere)
20
21    scene = cv3d.t.geometry.RaycastingScene()
22    scene.add_triangles(cube)
23    scene.add_triangles(torus)
24    _ = scene.add_triangles(sphere)
25
26    rays = cv3d.t.geometry.RaycastingScene.create_rays_pinhole(
27        fov_deg=90,
28        center=[0, 0, 2],
29        eye=[2, 3, 0],
30        up=[0, 1, 0],
31        width_px=640,
32        height_px=480,
33    )
34    # We can directly pass the rays tensor to the cast_rays function.
35    ans = scene.cast_rays(rays)
36    plt.imshow(ans['t_hit'].numpy())
37    plt.show()
38    plt.imshow(np.abs(ans['primitive_normals'].numpy()))
39    plt.show()
40    plt.imshow(np.abs(ans['geometry_ids'].numpy()), vmax=3)
41    plt.show()