Local preservation snapshot, not a new deployment or simulation acceptance.
49 lines
2.2 KiB
Python
49 lines
2.2 KiB
Python
"""PCL plane inliers for ROI refinement; raw geometry remains unchanged."""
|
|
import ctypes
|
|
import math
|
|
|
|
import numpy as np
|
|
|
|
|
|
class PclGround:
|
|
height_m = .05
|
|
|
|
def __init__(self, geometry):
|
|
self.library = ctypes.CDLL('/opt/missioncore/libpcl_ground.so')
|
|
self.extract = self.library.pcl_ground_indices
|
|
self.extract.argtypes = [ctypes.POINTER(ctypes.c_float), ctypes.c_int,
|
|
ctypes.c_float, ctypes.c_float, ctypes.POINTER(ctypes.c_int),
|
|
ctypes.POINTER(ctypes.c_float)]
|
|
self.extract.restype = ctypes.c_int
|
|
self.angle = math.radians(geometry['max_slope_degrees'])
|
|
|
|
def mask(self, points, *, ground_anchor=None):
|
|
points = np.ascontiguousarray(points, dtype=np.float32)
|
|
if (points.ndim != 2 or points.shape[1] != 3 or len(points) > 30000
|
|
or not np.isfinite(points).all()):
|
|
raise ValueError('Invalid PCL ground input')
|
|
result = np.zeros(len(points), dtype=bool)
|
|
if len(points) < 3:
|
|
return result
|
|
indices = np.empty(len(points), np.int32)
|
|
model = np.zeros(4, np.float32)
|
|
count = self.extract(points.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
|
|
len(points), self.angle, self.height_m,
|
|
indices.ctypes.data_as(ctypes.POINTER(ctypes.c_int)),
|
|
model.ctypes.data_as(ctypes.POINTER(ctypes.c_float)))
|
|
if not 0 <= count <= len(points):
|
|
raise RuntimeError('PCL ground segmentation failed')
|
|
selected = indices[:count]
|
|
if np.any(selected < 0) or np.any(selected >= len(points)):
|
|
raise RuntimeError('PCL returned an invalid source index')
|
|
# A horizontal tabletop / another floor is not the rover's ground.
|
|
# Without a matching measured contact reference no association clears.
|
|
if ground_anchor is not None:
|
|
anchor = np.asarray(ground_anchor, dtype=float)
|
|
if anchor.shape != (3,) or not np.isfinite(anchor).all():
|
|
raise ValueError('Invalid ground reference')
|
|
if abs(anchor @ model[:3] + model[3]) > self.height_m:
|
|
return result
|
|
result[selected] = True
|
|
return result
|