有3d gcode预览,基本能按要求切片,但是缩放后切片会失败

This commit is contained in:
2026-04-12 17:09:19 +08:00
parent 3020957367
commit a3f8a31432
3280 changed files with 1433 additions and 634630 deletions

View File

@@ -1,35 +1,21 @@
import time
import numpy as np
from stl import mesh
def simplify_stl_grid(input_path, output_path, target_ratio=0.1):
m = mesh.Mesh.from_file(input_path)
vertices = m.vectors.reshape(-1, 3)
min_v = vertices.min(axis=0)
max_v = vertices.max(axis=0)
bbox_size = max_v - min_v
max_dim = np.max(bbox_size)
# Adjust resolution to rough target_ratio by guessing.
# The number of vertices drops roughly by (resolution_factor)^2.
# So if we want 10% faces, resolution_factor can be heuristically set.
# Let's try 0.05
grid_size = max_dim * 0.05
v_idx = np.round((vertices - min_v) / grid_size).astype(np.int32)
_, unique_idx, inv_idx = np.unique(v_idx, axis=0, return_index=True, return_inverse=True)
new_vertices = vertices[unique_idx]
faces = inv_idx.reshape(-1, 3)
valid = (faces[:,0] != faces[:,1]) & (faces[:,1] != faces[:,2]) & (faces[:,0] != faces[:,2])
valid_faces = faces[valid]
new_m = mesh.Mesh(np.zeros(valid_faces.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(valid_faces):
for j in range(3):
new_m.vectors[i][j] = new_vertices[f[j]]
new_m.update_normals()
new_m.save(output_path)
# create a dummy huge point cloud
vertices = np.random.rand(1000000, 3) * 100
min_v = vertices.min(axis=0)
g_size = 2.0
v_idx = np.round((vertices - min_v) / g_size).astype(np.int32)
t0 = time.time()
_, unique_idx, inv_idx = np.unique(v_idx, axis=0, return_index=True, return_inverse=True)
print("axis=0 time:", time.time() - t0)
v_idx = v_idx.astype(np.int64)
t0 = time.time()
max_idx = v_idx.max(axis=0) + 1
v_1d = v_idx[:, 0] + v_idx[:, 1] * max_idx[0] + v_idx[:, 2] * max_idx[0] * max_idx[1]
_, unique_idx, inv_idx = np.unique(v_1d, return_index=True, return_inverse=True)
print("1D hash time:", time.time() - t0)