1 利用3d軟件生成一個長方體
邊長隨意,長度隨意
2 導出為模型文件并采樣為點云數據
從mesh表面進行采樣,點數根據自己需求進行設置,此處設置為100000。
采樣結果:
3 識別OBB外接框并可視化長邊方向
import numpy as np
import open3d as o3d
def get_obb_longest_edge_direction(obb):"""計算長邊方向"""# 獲取OBB的旋轉矩陣R = obb.R# 獲取OBB的擴展長度extent = obb.extent# 計算長邊索引longest_edge_index = np.argmax(extent)# 計算長邊方向向量longest_edge_direction = R[:, longest_edge_index]return longest_edge_directiondef create_line_set_of_OBBLongdir(start_point, end_point):# 創建兩個點points = [start_point, end_point]# 創建一條線段lines = [[0, 1]]# 創建lineset對象line_set = o3d.geometry.LineSet(points = o3d.utility.Vector3dVector(points), lines = o3d.utility.Vector2iVector(lines))# 設置線段顏色line_set.colors = o3d.utility.Vector3dVector([[0, 1, 0]])return line_setdef main():# 1. 讀取點云數據file_path = "cahngfangti-Mesh-sample.pcd" # 替換為你的點云文件路徑pcd = o3d.io.read_point_cloud(file_path)# 2. 計算OBB外接框obb = pcd.get_oriented_bounding_box()obb.color = (1, 0, 0) # 設置OBB顏色# 3. 獲取OBB的最長邊方向longest_edge_direction = get_obb_longest_edge_direction(obb)print(f"OBB的最長邊方向為 {longest_edge_direction}")# 4. 獲取OBB的中心點center_point = obb.get_center()end_point = center_point + longest_edge_direction * 20.0# 5. 創建直線line_set = create_line_set_of_OBBLongdir(center_point, end_point)o3d.visualization.draw_geometries([pcd, obb, line_set])if __name__ == "__main__":main()