ZiadMansourM / photogrammetry

Photogrammetry: Close Range 3D scanning. Our graduation project 🎓
https://docs.scanmate.sreboy.com/
GNU General Public License v3.0
5 stars 0 forks source link

Saving Point Cloud as STL (Meshing) #32

Open MohamedWZS opened 1 year ago

MohamedWZS commented 1 year ago
import numpy as np
import open3d as o3d
import matplotlib.pyplot as plt

# Define the number of points in the point cloud
N = 1_000_000

# Generate random x, y, and z coordinates between -0.5 and 0.5
x = np.random.rand(N) - 0.5
y = np.random.rand(N) - 0.5
z = np.random.rand(N) - 0.5

# Scale the coordinates to create a cube with sides of length 1 meter
x *= 1.0
y *= 1.0
z *= 1.0

# Combine the coordinates into a single NumPy array
point_cloud = np.column_stack((x, y, z))

# Create an Open3D PointCloud object
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(point_cloud[:,:3])

# Estimate the normals for the point cloud
pcd.estimate_normals()

# Apply the Ball-Pivoting Algorithm to create a mesh
radii = [0.005, 0.01, 0.02, 0.04]
bpa_mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_ball_pivoting(
    pcd, o3d.utility.DoubleVector(radii)
)

# Save the mesh as an STL file
o3d.io.write_triangle_mesh("point_cloud.stl", bpa_mesh)

This code generates a point cloud and creates a mesh from it using the Ball-Pivoting Algorithm. Then it saves the mesh as an STL file named point_cloud.stl. Note that the Ball-Pivoting Algorithm may not be suitable for all types of point clouds, and you may need to adjust the radii values or explore alternative meshing algorithms depending on your data.