Upload anchor_utils.py
Browse files- anchor_utils.py +96 -0
anchor_utils.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#@title mesh utils
|
| 2 |
+
|
| 3 |
+
import glob
|
| 4 |
+
import os
|
| 5 |
+
import open3d as o3d
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
def read_off(file_path):
|
| 9 |
+
with open(file_path, 'r') as file:
|
| 10 |
+
lines = file.readlines()
|
| 11 |
+
|
| 12 |
+
assert lines[0].strip() == "OFF"
|
| 13 |
+
|
| 14 |
+
n_vertices, n_faces, _ = map(int, lines[1].strip().split())
|
| 15 |
+
|
| 16 |
+
vertices = []
|
| 17 |
+
for i in range(2, 2 + n_vertices):
|
| 18 |
+
vertex = list(map(float, lines[i].strip().split()))
|
| 19 |
+
vertices.append(vertex)
|
| 20 |
+
|
| 21 |
+
faces = []
|
| 22 |
+
for i in range(2 + n_vertices, 2 + n_vertices + n_faces):
|
| 23 |
+
face = list(map(int, lines[i].strip().split()))[1:]
|
| 24 |
+
faces.append(face)
|
| 25 |
+
|
| 26 |
+
return vertices, faces
|
| 27 |
+
|
| 28 |
+
def write_ply(file_path, vertices, faces):
|
| 29 |
+
with open(file_path, 'w') as file:
|
| 30 |
+
file.write("ply\n")
|
| 31 |
+
file.write("format ascii 1.0\n")
|
| 32 |
+
file.write(f"element vertex {len(vertices)}\n")
|
| 33 |
+
file.write("property float x\n")
|
| 34 |
+
file.write("property float y\n")
|
| 35 |
+
file.write("property float z\n")
|
| 36 |
+
file.write(f"element face {len(faces)}\n")
|
| 37 |
+
file.write("property list uchar int vertex_indices\n")
|
| 38 |
+
file.write("end_header\n")
|
| 39 |
+
|
| 40 |
+
for vertex in vertices:
|
| 41 |
+
file.write(f"{' '.join(map(str, vertex))}\n")
|
| 42 |
+
|
| 43 |
+
for face in faces:
|
| 44 |
+
file.write(f"{len(face)} {' '.join(map(str, face))}\n")
|
| 45 |
+
|
| 46 |
+
def scale_vertices(vertices, scale_factor):
|
| 47 |
+
return [[coord * scale_factor for coord in vertex] for vertex in vertices]
|
| 48 |
+
|
| 49 |
+
def convert_off_to_ply(off_file_path, ply_file_path, scale_factor=1.0):
|
| 50 |
+
vertices, faces = read_off(off_file_path)
|
| 51 |
+
scaled_vertices = scale_vertices(vertices, scale_factor)
|
| 52 |
+
write_ply(ply_file_path, scaled_vertices, faces)
|
| 53 |
+
|
| 54 |
+
def remove_outliers_and_convert_to_point_cloud(ply_file_path, nb_neighbors=40, std_ratio=1.0, voxel_size=0.005):
|
| 55 |
+
point_cloud = o3d.io.read_point_cloud(ply_file_path)
|
| 56 |
+
|
| 57 |
+
voxel_down = point_cloud.voxel_down_sample(voxel_size=voxel_size)
|
| 58 |
+
|
| 59 |
+
cl, ind = voxel_down.remove_statistical_outlier(nb_neighbors=nb_neighbors, std_ratio=std_ratio)
|
| 60 |
+
|
| 61 |
+
cleaned_ply_path = ply_file_path.replace('.ply', '_cleaned.ply')
|
| 62 |
+
o3d.io.write_point_cloud(cleaned_ply_path, cl)
|
| 63 |
+
|
| 64 |
+
cleaned_pcd_path = ply_file_path.replace('.ply', '_cleaned.pcd')
|
| 65 |
+
o3d.io.write_point_cloud(cleaned_pcd_path, cl)
|
| 66 |
+
|
| 67 |
+
return cleaned_ply_path, cleaned_pcd_path
|
| 68 |
+
|
| 69 |
+
def convert_pointcloud_to_mesh(ply_file_path, obj_file_path):
|
| 70 |
+
# Read the point cloud from the PLY file
|
| 71 |
+
pcd = o3d.io.read_point_cloud(ply_file_path)
|
| 72 |
+
|
| 73 |
+
# Estimate the normals of the point cloud
|
| 74 |
+
pcd.estimate_normals(search_param=o3d.geometry.KDTreeSearchParamHybrid(radius=0.05, max_nn=1000))
|
| 75 |
+
|
| 76 |
+
# Compute the nearest neighbor distances and calculate the average
|
| 77 |
+
nn_distances = pcd.compute_nearest_neighbor_distance()
|
| 78 |
+
avg_nn_distance = np.mean(nn_distances)
|
| 79 |
+
|
| 80 |
+
# Adjust the ball pivoting radius to a smaller value for a finer mesh
|
| 81 |
+
radius_of_ball = 3.5 * avg_nn_distance
|
| 82 |
+
print('radius of ball', radius_of_ball)
|
| 83 |
+
|
| 84 |
+
# Create a mesh using the Ball Pivoting Algorithm
|
| 85 |
+
bpa_mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_ball_pivoting(
|
| 86 |
+
pcd,
|
| 87 |
+
o3d.utility.DoubleVector([radius_of_ball, radius_of_ball])
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Simplify the mesh with a higher target number of triangles for more detail
|
| 91 |
+
target_number_of_triangles = 10000 # Increase this value for a finer mesh
|
| 92 |
+
simplified_mesh = bpa_mesh.simplify_quadric_decimation(target_number_of_triangles=target_number_of_triangles)
|
| 93 |
+
|
| 94 |
+
# Save the resulting mesh as an OBJ file
|
| 95 |
+
o3d.io.write_triangle_mesh(obj_file_path, simplified_mesh)
|
| 96 |
+
print(f'Saved: {obj_file_path}')
|