Ray Casting

ray_casting_closest_geometry.py

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# The MIT License (MIT)
 5#
 6# Copyright (c) 2018-2021 www.open3d.org
 7#
 8# Permission is hereby granted, free of charge, to any person obtaining a copy
 9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29import matplotlib.pyplot as plt
30import matplotlib.animation as anim
31import sys
32
33if __name__ == "__main__":
34    cube = o3d.t.geometry.TriangleMesh.from_legacy(
35        o3d.geometry.TriangleMesh.create_box().translate([-1.2, -1.2, 0]))
36    sphere = o3d.t.geometry.TriangleMesh.from_legacy(
37        o3d.geometry.TriangleMesh.create_sphere(0.5).translate([0.7, 0.8, 0]))
38
39    scene = o3d.t.geometry.RaycastingScene()
40    # Add triangle meshes and remember ids.
41    mesh_ids = {}
42    mesh_ids[scene.add_triangles(cube)] = 'cube'
43    mesh_ids[scene.add_triangles(sphere)] = 'sphere'
44
45    # Compute range.
46    xyz_range = np.linspace([-2, -2, -2], [2, 2, 2], num=64)
47    # Query_points is a [64,64,64,3] array.
48    query_points = np.stack(np.meshgrid(*xyz_range.T),
49                            axis=-1).astype(np.float32)
50    closest_points = scene.compute_closest_points(query_points)
51    distance = np.linalg.norm(query_points - closest_points['points'].numpy(),
52                              axis=-1)
53    rays = np.concatenate([query_points, np.ones_like(query_points)], axis=-1)
54    intersection_counts = scene.count_intersections(rays).numpy()
55    is_inside = intersection_counts % 2 == 1
56    distance[is_inside] *= -1
57    signed_distance = distance
58    closest_geometry = closest_points['geometry_ids'].numpy()
59
60    # We can visualize the slices of the distance field and closest geometry directly with matplotlib.
61    fig, axes = plt.subplots(1, 2)
62    print(
63        "Visualizing sdf and closest geometry at each point for a cube and sphere ..."
64    )
65
66    def show_slices(i=int):
67        print(f"Displaying slice no.: {i}")
68        if i >= 64:
69            sys.exit()
70        axes[0].imshow(signed_distance[:, :, i])
71        axes[1].imshow(closest_geometry[:, :, i])
72
73    animator = anim.FuncAnimation(fig, show_slices, interval=100)
74    plt.show()

ray_casting_sdf.py

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# The MIT License (MIT)
 5#
 6# Copyright (c) 2018-2021 www.open3d.org
 7#
 8# Permission is hereby granted, free of charge, to any person obtaining a copy
 9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29import matplotlib.pyplot as plt
30import matplotlib.animation as anim
31import sys
32
33if __name__ == "__main__":
34    # Load mesh and convert to open3d.t.geometry.TriangleMesh .
35    armadillo_data = o3d.data.ArmadilloMesh()
36    mesh = o3d.io.read_triangle_mesh(armadillo_data.path)
37    mesh = o3d.t.geometry.TriangleMesh.from_legacy(mesh)
38
39    # Create a scene and add the triangle mesh.
40    scene = o3d.t.geometry.RaycastingScene()
41    scene.add_triangles(mesh)
42
43    min_bound = mesh.vertex['positions'].min(0).numpy()
44    max_bound = mesh.vertex['positions'].max(0).numpy()
45
46    xyz_range = np.linspace(min_bound, max_bound, num=64)
47
48    # Query_points is a [64,64,64,3] array.
49    query_points = np.stack(np.meshgrid(*xyz_range.T),
50                            axis=-1).astype(np.float32)
51
52    # Signed distance is a [64,64,64] array.
53    signed_distance = scene.compute_signed_distance(query_points)
54
55    # We can visualize the slices of the distance field directly with matplotlib.
56    fig = plt.figure()
57    print("Visualizing sdf at each point for the armadillo mesh ...")
58
59    def show_slices(i=int):
60        print(f"Displaying slice no.: {i}")
61        if i >= 64:
62            sys.exit()
63        plt.imshow(signed_distance.numpy()[:, :, i % 64])
64
65    animator = anim.FuncAnimation(fig, show_slices, interval=100)
66    plt.show()

ray_casting_to_image.py

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# The MIT License (MIT)
 5#
 6# Copyright (c) 2018-2021 www.open3d.org
 7#
 8# Permission is hereby granted, free of charge, to any person obtaining a copy
 9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24# IN THE SOFTWARE.
25# ----------------------------------------------------------------------------
26
27import open3d as o3d
28import numpy as np
29import matplotlib.pyplot as plt
30
31if __name__ == "__main__":
32    # Create meshes and convert to open3d.t.geometry.TriangleMesh .
33    cube = o3d.geometry.TriangleMesh.create_box().translate([0, 0, 0])
34    cube = o3d.t.geometry.TriangleMesh.from_legacy(cube)
35    torus = o3d.geometry.TriangleMesh.create_torus().translate([0, 0, 2])
36    torus = o3d.t.geometry.TriangleMesh.from_legacy(torus)
37    sphere = o3d.geometry.TriangleMesh.create_sphere(radius=0.5).translate(
38        [1, 2, 3])
39    sphere = o3d.t.geometry.TriangleMesh.from_legacy(sphere)
40
41    scene = o3d.t.geometry.RaycastingScene()
42    scene.add_triangles(cube)
43    scene.add_triangles(torus)
44    _ = scene.add_triangles(sphere)
45
46    rays = o3d.t.geometry.RaycastingScene.create_rays_pinhole(
47        fov_deg=90,
48        center=[0, 0, 2],
49        eye=[2, 3, 0],
50        up=[0, 1, 0],
51        width_px=640,
52        height_px=480,
53    )
54    # We can directly pass the rays tensor to the cast_rays function.
55    ans = scene.cast_rays(rays)
56    plt.imshow(ans['t_hit'].numpy())
57    plt.show()
58    plt.imshow(np.abs(ans['primitive_normals'].numpy()))
59    plt.show()
60    plt.imshow(np.abs(ans['geometry_ids'].numpy()), vmax=3)
61    plt.show()