AttributeError: 'open3d.open3d_pybind.geometry.PointCloud' object has no attribute 'triangulate
时间: 2023-06-22 18:43:47 浏览: 241
很抱歉,我之前的回答有误。Open3D的PointCloud对象确实没有`triangulate`方法。为了将点云三角化,可以使用以下代码:
```python
import rospy
import moveit_msgs.msg as moveit_msgs
import open3d as o3d
import numpy as np
def point_cloud_to_scene(point_cloud_topic, camera_frame_id, scene_publisher):
# Subscribe to point cloud topic
point_cloud = rospy.wait_for_message(point_cloud_topic, sensor_msgs.msg.PointCloud2)
# Convert point cloud to numpy array
points = np.array(list(pc2.read_points(point_cloud, skip_nans=True)))
# Convert numpy array to Open3D point cloud
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(points)
# Triangulate point cloud
tri = o3d.geometry.TriangleMesh.create_from_point_cloud_alpha_shape(pcd, alpha=0.03)
# Convert Open3D triangle mesh to MoveIt scene
vertices = np.asarray(tri.vertices)
triangles = np.asarray(tri.triangles)
scene_msg = moveit_msgs.PlanningScene()
scene_msg.is_diff = True
object_msg = moveit_msgs.PlanningSceneWorld()
object_msg.collision_objects.append(moveit_msgs.CollisionObject())
object_msg.collision_objects[0].id = "point_cloud"
object_msg.collision_objects[0].header.frame_id = camera_frame_id
object_msg.collision_objects[0].meshes.append(moveit_msgs.Mesh())
object_msg.collision_objects[0].meshes[0].vertices = [moveit_msgs.Vertex(x=vertices[i][0], y=vertices[i][1], z=vertices[i][2]) for i in range(vertices.shape[0])]
object_msg.collision_objects[0].meshes[0].triangles = [moveit_msgs.Triangle(mesh.vertices[i][0], mesh.vertices[i][1], mesh.vertices[i][2]) for i in range(triangles.shape[0])]
scene_msg.world.collision_objects = object_msg.collision_objects
scene_publisher.publish(scene_msg)
```
这个函数会将点云转换为Open3D的PointCloud对象,然后使用`create_from_point_cloud_alpha_shape`函数进行三角化,生成Open3D的TriangleMesh对象。接下来,将TriangleMesh对象转换为MoveIt环境scene,并发布到MoveIt Planning Scene的话题上。
请注意,这个函数需要安装Open3D库。
阅读全文