Octree#

octree_find_leaf.py#

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# Copyright (c) 2018-2023 www.open3d.org
 5# SPDX-License-Identifier: MIT
 6# ----------------------------------------------------------------------------
 7
 8import open3d as o3d
 9import numpy as np
10
11if __name__ == "__main__":
12    N = 2000
13    armadillo_data = o3d.data.ArmadilloMesh()
14    pcd = o3d.io.read_triangle_mesh(
15        armadillo_data.path).sample_points_poisson_disk(N)
16    # Fit to unit cube.
17    pcd.scale(1 / np.max(pcd.get_max_bound() - pcd.get_min_bound()),
18              center=pcd.get_center())
19    pcd.colors = o3d.utility.Vector3dVector(np.random.uniform(0, 1,
20                                                              size=(N, 3)))
21
22    octree = o3d.geometry.Octree(max_depth=4)
23    octree.convert_from_point_cloud(pcd, size_expand=0.01)
24    print('Displaying input octree ...')
25    o3d.visualization.draw([octree])
26    print('Finding leaf node containing the first point of pointcloud ...')
27    print(octree.locate_leaf_node(pcd.points[0]))

octree_from_voxel_grid.py#

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# Copyright (c) 2018-2023 www.open3d.org
 5# SPDX-License-Identifier: MIT
 6# ----------------------------------------------------------------------------
 7
 8import open3d as o3d
 9import numpy as np
10
11if __name__ == "__main__":
12    N = 2000
13    armadillo_data = o3d.data.ArmadilloMesh()
14    pcd = o3d.io.read_triangle_mesh(
15        armadillo_data.path).sample_points_poisson_disk(N)
16    # Fit to unit cube.
17    pcd.scale(1 / np.max(pcd.get_max_bound() - pcd.get_min_bound()),
18              center=pcd.get_center())
19    pcd.colors = o3d.utility.Vector3dVector(np.random.uniform(0, 1,
20                                                              size=(N, 3)))
21    print('Displaying input voxel grid ...')
22    voxel_grid = o3d.geometry.VoxelGrid.create_from_point_cloud(pcd,
23                                                                voxel_size=0.05)
24    o3d.visualization.draw([voxel_grid])
25
26    octree = o3d.geometry.Octree(max_depth=4)
27    octree.create_from_voxel_grid(voxel_grid)
28    print('Displaying octree ..')
29    o3d.visualization.draw([octree])

octree_point_cloud.py#

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# Copyright (c) 2018-2023 www.open3d.org
 5# SPDX-License-Identifier: MIT
 6# ----------------------------------------------------------------------------
 7
 8import open3d as o3d
 9import numpy as np
10
11if __name__ == "__main__":
12    N = 2000
13    armadillo_data = o3d.data.ArmadilloMesh()
14    pcd = o3d.io.read_triangle_mesh(
15        armadillo_data.path).sample_points_poisson_disk(N)
16    # Fit to unit cube.
17    pcd.scale(1 / np.max(pcd.get_max_bound() - pcd.get_min_bound()),
18              center=pcd.get_center())
19    pcd.colors = o3d.utility.Vector3dVector(np.random.uniform(0, 1,
20                                                              size=(N, 3)))
21    print('Displaying input pointcloud ...')
22    o3d.visualization.draw([pcd])
23
24    octree = o3d.geometry.Octree(max_depth=4)
25    octree.convert_from_point_cloud(pcd, size_expand=0.01)
26    print('Displaying octree ..')
27    o3d.visualization.draw([octree])

octree_traversal.py#

 1# ----------------------------------------------------------------------------
 2# -                        Open3D: www.open3d.org                            -
 3# ----------------------------------------------------------------------------
 4# Copyright (c) 2018-2023 www.open3d.org
 5# SPDX-License-Identifier: MIT
 6# ----------------------------------------------------------------------------
 7
 8import open3d as o3d
 9import numpy as np
10
11
12def f_traverse(node, node_info):
13    early_stop = False
14
15    if isinstance(node, o3d.geometry.OctreeInternalNode):
16        if isinstance(node, o3d.geometry.OctreeInternalPointNode):
17            n = 0
18            for child in node.children:
19                if child is not None:
20                    n += 1
21            print(
22                "{}{}: Internal node at depth {} has {} children and {} points ({})"
23                .format('    ' * node_info.depth,
24                        node_info.child_index, node_info.depth, n,
25                        len(node.indices), node_info.origin))
26
27            # We only want to process nodes / spatial regions with enough points.
28            early_stop = len(node.indices) < 250
29    elif isinstance(node, o3d.geometry.OctreeLeafNode):
30        if isinstance(node, o3d.geometry.OctreePointColorLeafNode):
31            print("{}{}: Leaf node at depth {} has {} points with origin {}".
32                  format('    ' * node_info.depth, node_info.child_index,
33                         node_info.depth, len(node.indices), node_info.origin))
34    else:
35        raise NotImplementedError('Node type not recognized!')
36
37    # Early stopping: if True, traversal of children of the current node will be skipped.
38    return early_stop
39
40
41if __name__ == "__main__":
42    N = 2000
43    armadillo_data = o3d.data.ArmadilloMesh()
44    pcd = o3d.io.read_triangle_mesh(
45        armadillo_data.path).sample_points_poisson_disk(N)
46    # Fit to unit cube.
47    pcd.scale(1 / np.max(pcd.get_max_bound() - pcd.get_min_bound()),
48              center=pcd.get_center())
49    pcd.colors = o3d.utility.Vector3dVector(np.random.uniform(0, 1,
50                                                              size=(N, 3)))
51
52    octree = o3d.geometry.Octree(max_depth=4)
53    octree.convert_from_point_cloud(pcd, size_expand=0.01)
54    print('Displaying input octree ...')
55    o3d.visualization.draw([octree])
56    print('Traversing octree ...')
57    octree.traverse(f_traverse)