|
| 1 | +from pyrep.objects.object import Object |
| 2 | +from pyrep.const import ObjectType |
| 3 | +from pyrep.backend import sim |
| 4 | +from typing import List, Optional |
| 5 | + |
| 6 | +class Octree(Object): |
| 7 | + """An octree object.""" |
| 8 | + |
| 9 | + @staticmethod |
| 10 | + def create(voxel_size: float, point_size: Optional[float] = None, |
| 11 | + options: int = 0) -> 'Octree': |
| 12 | + """Creates an octree object and inserts in the scene. |
| 13 | + |
| 14 | + :param voxelSize: The resolution of octree voxels. |
| 15 | + :param options: Octree options. |
| 16 | + :param pointSize: Point representation size of voxels. |
| 17 | + :return: The newly created Octree. |
| 18 | + """ |
| 19 | + if point_size is None: |
| 20 | + point_size = voxel_size |
| 21 | + handle = sim.simCreateOctree(voxel_size, options, point_size) |
| 22 | + return Octree(handle) |
| 23 | + |
| 24 | + def _get_requested_type(self) -> ObjectType: |
| 25 | + return ObjectType.OCTREE |
| 26 | + |
| 27 | + def insert_voxels(self, points: List[float], color: Optional[float] = None, |
| 28 | + options: int = 0) -> None: |
| 29 | + """Inserts voxels into the octree. |
| 30 | + |
| 31 | + :param points: A list of x,y,z numbers. |
| 32 | + :param color: A list containing RGB data, or None. |
| 33 | + :param options: Voxel insertion options. |
| 34 | + """ |
| 35 | + if not isinstance(points, list): |
| 36 | + raise ValueError( |
| 37 | + 'Octree.insert_voxels: points parameter is not a list.') |
| 38 | + if len(points) % 3 is not 0: |
| 39 | + raise ValueError( |
| 40 | + 'Octree.insert_voxels: points parameter length ' |
| 41 | + 'not a multiple of 3.') |
| 42 | + if color is not None: |
| 43 | + if not isinstance(color, list): |
| 44 | + raise ValueError( |
| 45 | + 'Octree.insert_voxels: color parameter not a list.') |
| 46 | + elif len(color) is not 3: |
| 47 | + raise ValueError( |
| 48 | + 'Octree.insert_voxels: color parameter not an RGB list.') |
| 49 | + sim.simInsertVoxelsIntoOctree(self._handle, options, points, color,None) |
| 50 | + return |
| 51 | + |
| 52 | + def remove_voxels(self, points : Optional[List[float]], |
| 53 | + options: int = 0) -> None: |
| 54 | + """Remove voxels from the octree. |
| 55 | + |
| 56 | + :param points: A list of x,y,z numbers, or None to clear the octree. |
| 57 | + :param options: Voxel removal options. |
| 58 | + """ |
| 59 | + if points is not None: |
| 60 | + if not isinstance(points, list): |
| 61 | + raise ValueError( |
| 62 | + 'Octree.insert_voxels: points parameter is not a list.') |
| 63 | + if len(points) % 3 is not 0: |
| 64 | + raise ValueError( |
| 65 | + 'Octree.insert_voxels: points parameter length ' |
| 66 | + 'not a multiple of 3.') |
| 67 | + sim.simRemoveVoxelsFromOctree(self._handle, options, points) |
| 68 | + |
| 69 | + def get_voxels(self) -> list: |
| 70 | + """Returns voxels from the octree. |
| 71 | + |
| 72 | + :return: List of voxel x,y,z coordinates. |
| 73 | + """ |
| 74 | + return sim.simGetOctreeVoxels(self._handle) |
| 75 | + |
| 76 | + def insert_object(self, obj: Object, color: Optional[float] = None, |
| 77 | + options: int = 0) -> None: |
| 78 | + """Inserts object into the octree. |
| 79 | + |
| 80 | + :param obj: Object to insert. |
| 81 | + :param color: A list containing RGB data, or None. |
| 82 | + :param options: Object insertion options. |
| 83 | + """ |
| 84 | + if color is not None: |
| 85 | + if not isinstance(color, list): |
| 86 | + raise ValueError( |
| 87 | + 'Octree.insert_object: color parameter not a list.') |
| 88 | + elif len(color) is not 3: |
| 89 | + raise ValueError( |
| 90 | + 'Octree.insert_object: color parameter not an RGB list.') |
| 91 | + sim.simInsertObjectIntoOctree(self._handle, obj.get_handle(), options, |
| 92 | + color, 0) |
| 93 | + return |
| 94 | + |
| 95 | + def subtract_object(self, obj: Object, options: int = 0) -> None: |
| 96 | + """Subtract object from the octree. |
| 97 | + |
| 98 | + :param obj: Object to subtract. |
| 99 | + :param options: Object subtraction options. |
| 100 | + """ |
| 101 | + sim.simSubtractObjectFromOctree(self._handle, obj.get_handle(), options) |
| 102 | + return |
| 103 | + |
| 104 | + def check_point_occupancy(self, points: List[float], |
| 105 | + options: int = 0) -> bool: |
| 106 | + if not isinstance(points, list): |
| 107 | + raise ValueError( |
| 108 | + 'Octree.check_point_occupancy: points parameter is not a list.') |
| 109 | + if len(points) % 3 is not 0: |
| 110 | + raise ValueError( |
| 111 | + 'Octree._check_point_occupancy: points parameter length ' |
| 112 | + 'not a multiple of 3.') |
| 113 | + return sim.simCheckOctreePointOccupancy(self._handle, options, points) |
| 114 | + |
| 115 | + def clear_voxels(self) -> None: |
| 116 | + """Clears all voxels from the octree. |
| 117 | + """ |
| 118 | + sim.simRemoveVoxelsFromOctree(self._handle, 0, None) |
0 commit comments