Ray Casting#
ray_casting_closest_geometry.py#
1# ----------------------------------------------------------------------------
2# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# Copyright (c) 2018-2024 www.open3d.org
5# SPDX-License-Identifier: MIT
6# ----------------------------------------------------------------------------
7
8import open3d as o3d
9import numpy as np
10import matplotlib.pyplot as plt
11import matplotlib.animation as anim
12import sys
13
14if __name__ == "__main__":
15 cube = o3d.t.geometry.TriangleMesh.from_legacy(
16 o3d.geometry.TriangleMesh.create_box().translate([-1.2, -1.2, 0]))
17 sphere = o3d.t.geometry.TriangleMesh.from_legacy(
18 o3d.geometry.TriangleMesh.create_sphere(0.5).translate([0.7, 0.8, 0]))
19
20 scene = o3d.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# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# Copyright (c) 2018-2024 www.open3d.org
5# SPDX-License-Identifier: MIT
6# ----------------------------------------------------------------------------
7
8import open3d as o3d
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 open3d.t.geometry.TriangleMesh .
16 armadillo_data = o3d.data.ArmadilloMesh()
17 mesh = o3d.io.read_triangle_mesh(armadillo_data.path)
18 mesh = o3d.t.geometry.TriangleMesh.from_legacy(mesh)
19
20 # Create a scene and add the triangle mesh.
21 scene = o3d.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# - Open3D: www.open3d.org -
3# ----------------------------------------------------------------------------
4# Copyright (c) 2018-2024 www.open3d.org
5# SPDX-License-Identifier: MIT
6# ----------------------------------------------------------------------------
7
8import open3d as o3d
9import numpy as np
10import matplotlib.pyplot as plt
11
12if __name__ == "__main__":
13 # Create meshes and convert to open3d.t.geometry.TriangleMesh .
14 cube = o3d.geometry.TriangleMesh.create_box().translate([0, 0, 0])
15 cube = o3d.t.geometry.TriangleMesh.from_legacy(cube)
16 torus = o3d.geometry.TriangleMesh.create_torus().translate([0, 0, 2])
17 torus = o3d.t.geometry.TriangleMesh.from_legacy(torus)
18 sphere = o3d.geometry.TriangleMesh.create_sphere(radius=0.5).translate(
19 [1, 2, 3])
20 sphere = o3d.t.geometry.TriangleMesh.from_legacy(sphere)
21
22 scene = o3d.t.geometry.RaycastingScene()
23 scene.add_triangles(cube)
24 scene.add_triangles(torus)
25 _ = scene.add_triangles(sphere)
26
27 rays = o3d.t.geometry.RaycastingScene.create_rays_pinhole(
28 fov_deg=90,
29 center=[0, 0, 2],
30 eye=[2, 3, 0],
31 up=[0, 1, 0],
32 width_px=640,
33 height_px=480,
34 )
35 # We can directly pass the rays tensor to the cast_rays function.
36 ans = scene.cast_rays(rays)
37 plt.imshow(ans['t_hit'].numpy())
38 plt.show()
39 plt.imshow(np.abs(ans['primitive_normals'].numpy()))
40 plt.show()
41 plt.imshow(np.abs(ans['geometry_ids'].numpy()), vmax=3)
42 plt.show()