Octree

octree_find_leaf.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
29
30if __name__ == "__main__":
31    N = 2000
32    armadillo_data = o3d.data.ArmadilloMesh()
33    pcd = o3d.io.read_triangle_mesh(
34        armadillo_data.path).sample_points_poisson_disk(N)
35    # Fit to unit cube.
36    pcd.scale(1 / np.max(pcd.get_max_bound() - pcd.get_min_bound()),
37              center=pcd.get_center())
38    pcd.colors = o3d.utility.Vector3dVector(np.random.uniform(0, 1,
39                                                              size=(N, 3)))
40
41    octree = o3d.geometry.Octree(max_depth=4)
42    octree.convert_from_point_cloud(pcd, size_expand=0.01)
43    print('Displaying input octree ...')
44    o3d.visualization.draw([octree])
45    print('Finding leaf node containing the first point of pointcloud ...')
46    print(octree.locate_leaf_node(pcd.points[0]))

octree_from_voxel_grid.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
29
30if __name__ == "__main__":
31    N = 2000
32    armadillo_data = o3d.data.ArmadilloMesh()
33    pcd = o3d.io.read_triangle_mesh(
34        armadillo_data.path).sample_points_poisson_disk(N)
35    # Fit to unit cube.
36    pcd.scale(1 / np.max(pcd.get_max_bound() - pcd.get_min_bound()),
37              center=pcd.get_center())
38    pcd.colors = o3d.utility.Vector3dVector(np.random.uniform(0, 1,
39                                                              size=(N, 3)))
40    print('Displaying input voxel grid ...')
41    voxel_grid = o3d.geometry.VoxelGrid.create_from_point_cloud(pcd,
42                                                                voxel_size=0.05)
43    o3d.visualization.draw([voxel_grid])
44
45    octree = o3d.geometry.Octree(max_depth=4)
46    octree.create_from_voxel_grid(voxel_grid)
47    print('Displaying octree ..')
48    o3d.visualization.draw([octree])

octree_point_cloud.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
29
30if __name__ == "__main__":
31    N = 2000
32    armadillo_data = o3d.data.ArmadilloMesh()
33    pcd = o3d.io.read_triangle_mesh(
34        armadillo_data.path).sample_points_poisson_disk(N)
35    # Fit to unit cube.
36    pcd.scale(1 / np.max(pcd.get_max_bound() - pcd.get_min_bound()),
37              center=pcd.get_center())
38    pcd.colors = o3d.utility.Vector3dVector(np.random.uniform(0, 1,
39                                                              size=(N, 3)))
40    print('Displaying input pointcloud ...')
41    o3d.visualization.draw([pcd])
42
43    octree = o3d.geometry.Octree(max_depth=4)
44    octree.convert_from_point_cloud(pcd, size_expand=0.01)
45    print('Displaying octree ..')
46    o3d.visualization.draw([octree])

octree_traversal.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
29
30
31def f_traverse(node, node_info):
32    early_stop = False
33
34    if isinstance(node, o3d.geometry.OctreeInternalNode):
35        if isinstance(node, o3d.geometry.OctreeInternalPointNode):
36            n = 0
37            for child in node.children:
38                if child is not None:
39                    n += 1
40            print(
41                "{}{}: Internal node at depth {} has {} children and {} points ({})"
42                .format('    ' * node_info.depth,
43                        node_info.child_index, node_info.depth, n,
44                        len(node.indices), node_info.origin))
45
46            # We only want to process nodes / spatial regions with enough points.
47            early_stop = len(node.indices) < 250
48    elif isinstance(node, o3d.geometry.OctreeLeafNode):
49        if isinstance(node, o3d.geometry.OctreePointColorLeafNode):
50            print("{}{}: Leaf node at depth {} has {} points with origin {}".
51                  format('    ' * node_info.depth, node_info.child_index,
52                         node_info.depth, len(node.indices), node_info.origin))
53    else:
54        raise NotImplementedError('Node type not recognized!')
55
56    # Early stopping: if True, traversal of children of the current node will be skipped.
57    return early_stop
58
59
60if __name__ == "__main__":
61    N = 2000
62    armadillo_data = o3d.data.ArmadilloMesh()
63    pcd = o3d.io.read_triangle_mesh(
64        armadillo_data.path).sample_points_poisson_disk(N)
65    # Fit to unit cube.
66    pcd.scale(1 / np.max(pcd.get_max_bound() - pcd.get_min_bound()),
67              center=pcd.get_center())
68    pcd.colors = o3d.utility.Vector3dVector(np.random.uniform(0, 1,
69                                                              size=(N, 3)))
70
71    octree = o3d.geometry.Octree(max_depth=4)
72    octree.convert_from_point_cloud(pcd, size_expand=0.01)
73    print('Displaying input octree ...')
74    o3d.visualization.draw([octree])
75    print('Traversing octree ...')
76    octree.traverse(f_traverse)