Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
true-real-michael committed Nov 12, 2023
1 parent b651377 commit 6569749
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
6 changes: 3 additions & 3 deletions octreelib/grid/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


class GridConfig(GridConfigBase):
_compatible_octree_types = [OctreeManager]
_compatible_octree_manager_types = [OctreeManager]


class Grid(GridBase):
Expand All @@ -38,7 +38,7 @@ def __init__(self, grid_config: GridConfig):
self.__pose_voxel_coordinates: Dict[int, List[VoxelBase]] = {}

# {voxel -> octree manager}
self.__octrees: Dict[VoxelBase, grid_config.octree_type] = {}
self.__octrees: Dict[VoxelBase, grid_config.octree_manager_type] = {}

def insert_points(self, pose_number: int, points: PointCloud):
"""
Expand Down Expand Up @@ -73,7 +73,7 @@ def insert_points(self, pose_number: int, points: PointCloud):
self._grid_config.grid_voxel_edge_length,
)
if target_voxel not in self.__octrees:
self.__octrees[target_voxel] = self._grid_config.octree_type(
self.__octrees[target_voxel] = self._grid_config.octree_manager_type(
self._grid_config.octree_config,
np.array(voxel_coordinates),
self._grid_config.grid_voxel_edge_length,
Expand Down
14 changes: 7 additions & 7 deletions octreelib/grid/grid_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ class GridConfigBase(ABC):
"""
Config for Grid
octree_type: type of Octree used
octree_manager_type: type of OctreeManager used
octree_config: config to be forwarded to the octrees
debug: debug mode
grid_voxel_edge_length: initial size of voxels
corner: corner of a grid
"""

_compatible_octree_types = []
_compatible_octree_manager_types = []

octree_type: Type[T]
octree_manager_type: Type[T]
octree_config: OctreeConfigBase
debug: bool = False
grid_voxel_edge_length: int = 1
Expand All @@ -70,17 +70,17 @@ def compatible_octree_types(self):
"""
:return: Types of Octrees which are compatible with this
"""
return self._compatible_octree_types
return self._compatible_octree_manager_types

def __post_init__(self):
"""
Check that
:raises TypeError: if given octree_type is not compatible with this type of grid.
"""
if self.octree_type not in self.compatible_octree_types:
if self.octree_manager_type not in self.compatible_octree_types:
raise TypeError(
f"Cannot use the provided octree type {self.octree_type.__name__}. "
f"The compatible octree types are [{', '.join(cls.__name__ for cls in self.compatible_octree_types)}]."
f"Cannot use the provided octree manager type {self.octree_manager_type.__name__}. "
f"The compatible octree manager types are [{', '.join(cls.__name__ for cls in self.compatible_octree_types)}]."
)


Expand Down
2 changes: 0 additions & 2 deletions octreelib/internal/point.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import annotations

from typing import Annotated, Literal

import numpy as np
Expand Down
44 changes: 26 additions & 18 deletions octreelib/octree/octree.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,6 @@ class OctreeConfig(OctreeConfigBase):


class OctreeNode(OctreeNodeBase):
def __generate_children(self):
child_edge_length = self.edge_length / np.float_(2)
children_corners_offsets = itertools.product([0, child_edge_length], repeat=3)
return [
OctreeNode(
self.corner_min + offset,
child_edge_length,
8 * self._position + internal_position,
)
for internal_position, offset in enumerate(children_corners_offsets)
]

def __unify(self):
self._points = self.get_points()
self._has_children = False
self._children = []

def subdivide(self, subdivision_criteria: List[Callable[[PointCloud], bool]]):
"""
Subdivide node based on the subdivision criteria.
Expand Down Expand Up @@ -79,17 +62,19 @@ def insert_points(self, points: PointCloud):
:param points: Points to insert.
"""
if self._has_children:
# distribute points to children
clouds = []

for offset in itertools.product([0, self.edge_length / 2], repeat=3):
child_corner_min = self.corner_min + np.array(offset)
child_corner_max = child_corner_min + self.edge_length / 2
# find points in the child
mask = np.all(
(points >= child_corner_min) & (points < child_corner_max), axis=1
)
child_points = points[mask]
clouds.append(child_points)

# insert points to children
for child, cloud in zip(self._children, clouds):
child.insert_points(cloud)
else:
Expand Down Expand Up @@ -167,6 +152,29 @@ def n_points(self):
else len(self._points)
)

def __generate_children(self):
"""
Generate children of the node.
"""
child_edge_length = self.edge_length / np.float_(2)
children_corners_offsets = itertools.product([0, child_edge_length], repeat=3)
return [
OctreeNode(
self.corner_min + offset,
child_edge_length,
8 * self._position + internal_position,
)
for internal_position, offset in enumerate(children_corners_offsets)
]

def __unify(self):
"""
Unify (unsubdivide) children of the node.
"""
self._points = self.get_points()
self._has_children = False
self._children = []


class Octree(OctreeBase, Generic[T]):
"""
Expand Down
8 changes: 4 additions & 4 deletions test/grid/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def points_are_same(points_first: PointCloud, points_second: PointCloud):
def generated_grid():
grid = Grid(
GridConfig(
octree_type=OctreeManager,
octree_manager_type=OctreeManager,
octree_config=OctreeConfig(),
grid_voxel_edge_length=5,
)
Expand Down Expand Up @@ -155,15 +155,15 @@ def test_invalid_octree_type():
try:
Grid(
GridConfig(
octree_type=Octree,
octree_manager_type=Octree,
octree_config=OctreeConfig(),
grid_voxel_edge_length=5,
)
)
except TypeError as e:
assert str(e) == (
"Cannot use the provided octree type Octree. "
"The compatible octree types are [OctreeManager]."
"Cannot use the provided octree manager type Octree. "
"The compatible octree manager types are [OctreeManager]."
)
else:
raise AssertionError("This type of octree should have caused an exception")

0 comments on commit 6569749

Please sign in to comment.