(pcd, filepath, ply_ascii=False, ply_compression=True, csv_delimiter=",")
| 300 | |
| 301 | # export point cloud to file (pcd, ply, e57, csv) |
| 302 | def save_pointcloud(pcd, filepath, ply_ascii=False, ply_compression=True, csv_delimiter=","): |
| 303 | # Create the directory if it does not exist |
| 304 | directory, filename = os.path.split(filepath) |
| 305 | os.makedirs(directory, exist_ok=True) |
| 306 | |
| 307 | # Get the file extension |
| 308 | ext = os.path.splitext(filename)[1][1:] |
| 309 | |
| 310 | if ext == "pcd": |
| 311 | if isinstance(pcd, o3d.t.geometry.Geometry): |
| 312 | o3d.t.io.write_point_cloud(filepath, pcd) |
| 313 | else: |
| 314 | o3d.io.write_point_cloud(filename=filepath, pointcloud=pcd) |
| 315 | |
| 316 | elif ext == "ply": |
| 317 | if isinstance(pcd, o3d.t.geometry.Geometry): |
| 318 | o3d.t.io.write_point_cloud(filepath, pcd, write_ascii=ply_ascii, compressed=ply_compression) |
| 319 | else: |
| 320 | o3d.io.write_point_cloud(filename=filepath, pointcloud=pcd, write_ascii=ply_ascii, compressed=ply_compression) |
| 321 | |
| 322 | # TODO: add support for exporting intensity and RGB colors |
| 323 | elif ext == "csv": |
| 324 | if not isinstance(pcd, np.ndarray): |
| 325 | array = np.asarray(pcd.points) |
| 326 | np.savetxt(filepath, array, delimiter=csv_delimiter) |
| 327 | |
| 328 | elif ext == "e57": |
| 329 | # if a single point cloud is provided, convert it to a list |
| 330 | if isinstance(pcd, list): |
| 331 | pcd_list = pcd |
| 332 | elif isinstance(pcd, o3d.geometry.PointCloud): |
| 333 | pcd_list = [pcd] |
| 334 | |
| 335 | # Create an E57 object with write mode |
| 336 | e57 = pye57.E57(filepath, mode='w') |
| 337 | |
| 338 | # Write each point cloud to the E57 file as a separate scan |
| 339 | for pcd in pcd_list: |
| 340 | # Convert open3d point cloud to numpy array |
| 341 | points = np.asarray(pcd.points) |
| 342 | colors = np.asarray(pcd.colors) * 255 |
| 343 | |
| 344 | # Create a dictionary with keys for each coordinate and color |
| 345 | data_raw = { |
| 346 | "cartesianX": points[:, 0], |
| 347 | "cartesianY": points[:, 1], |
| 348 | "cartesianZ": points[:, 2], |
| 349 | "colorRed" : colors[:, 0], |
| 350 | "colorGreen": colors[:, 1], |
| 351 | "colorBlue" : colors[:, 2]} |
| 352 | |
| 353 | # Write the point cloud data to the E57 file |
| 354 | e57.write_scan_raw(data_raw) |
| 355 | e57.close() |
| 356 | |
| 357 | print("\nexport completed.") |
| 358 | |
| 359 | def save_pointcloud_threaded(pcd, output_path, ply_ascii=False, ply_compression=True, csv_delimiter=","): |
no test coverage detected