crop-demo-1.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import json
  2. import numpy as np
  3. import open3d as o3d
  4. CUBOID_EXTENT_METERS = 200
  5. METERS_BELOW_START = 5
  6. METERS_ABOVE_START = 30
  7. def main():
  8. ## Point Cloud
  9. points = np.array([
  10. ## These points lie inside the cuboid
  11. [-2770.94365061042, 722.0595600050154, -20.004812609192445],
  12. [-2755.94365061042, 710.0595600050154, -20.004812609192445],
  13. [-2755.94365061042, 710.0595600050154, -15.004812609192445],
  14. ## These points lie outside the cuboid
  15. [-2755.94365061042 + CUBOID_EXTENT_METERS, 710.0595600050154, -15.004812609192445],
  16. [-2755.94365061042, 710.0595600050154 + CUBOID_EXTENT_METERS, -15.004812609192445],
  17. ]).reshape([-1, 3])
  18. point_cloud = o3d.geometry.PointCloud()
  19. point_cloud.points = o3d.utility.Vector3dVector(points)
  20. ## Start point here corresponds to an ego vehicle position start in a point cloud
  21. start_position = {'x': -2755.94365061042, 'y': 722.0595600050154, 'z': -20.004812609192445}
  22. cuboid_points = getCuboidPoints(start_position)
  23. points = o3d.utility.Vector3dVector(cuboid_points)
  24. oriented_bounding_box = o3d.geometry.OrientedBoundingBox.create_from_points(points)
  25. point_cloud_crop = point_cloud.crop(oriented_bounding_box)
  26. # View original point cloud with the cuboid, all 5 points present
  27. o3d.visualization.draw_geometries([point_cloud, oriented_bounding_box])
  28. # View cropped point cloud with the cuboid, only 3 points present
  29. o3d.visualization.draw_geometries([point_cloud_crop, oriented_bounding_box])
  30. def getCuboidPoints(start_position):
  31. return np.array([
  32. # Vertices Polygon1
  33. [start_position['x'] + (CUBOID_EXTENT_METERS / 2), start_position['y'] + (CUBOID_EXTENT_METERS / 2), start_position['z'] + METERS_ABOVE_START], # face-topright
  34. [start_position['x'] - (CUBOID_EXTENT_METERS / 2), start_position['y'] + (CUBOID_EXTENT_METERS / 2), start_position['z'] + METERS_ABOVE_START], # face-topleft
  35. [start_position['x'] - (CUBOID_EXTENT_METERS / 2), start_position['y'] - (CUBOID_EXTENT_METERS / 2), start_position['z'] + METERS_ABOVE_START], # rear-topleft
  36. [start_position['x'] + (CUBOID_EXTENT_METERS / 2), start_position['y'] - (CUBOID_EXTENT_METERS / 2), start_position['z'] + METERS_ABOVE_START], # rear-topright
  37. # Vertices Polygon 2
  38. [start_position['x'] + (CUBOID_EXTENT_METERS / 2), start_position['y'] + (CUBOID_EXTENT_METERS / 2), start_position['z'] - METERS_BELOW_START],
  39. [start_position['x'] - (CUBOID_EXTENT_METERS / 2), start_position['y'] + (CUBOID_EXTENT_METERS / 2), start_position['z'] - METERS_BELOW_START],
  40. [start_position['x'] - (CUBOID_EXTENT_METERS / 2), start_position['y'] - (CUBOID_EXTENT_METERS / 2), start_position['z'] - METERS_BELOW_START],
  41. [start_position['x'] + (CUBOID_EXTENT_METERS / 2), start_position['y'] - (CUBOID_EXTENT_METERS / 2), start_position['z'] - METERS_BELOW_START],
  42. ]).astype("float64")
  43. if __name__ == '__main__':
  44. main()