-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
135 lines (105 loc) · 4.32 KB
/
test.py
File metadata and controls
135 lines (105 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
Test script for quad_converter
Reads an OBJ file, converts triangles to quads, and saves the result as a new OBJ file
"""
import quad_converter
import numpy as np
import os
def load_obj(obj_path):
vertices = []
faces = []
print(f"[Info] Loading: {obj_path}")
with open(obj_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
if line.startswith("v "):
# Vertex: v x y z
parts = line.split()
if len(parts) >= 4:
x, y, z = float(parts[1]), float(parts[2]), float(parts[3])
vertices.append([x, y, z])
elif line.startswith("f "):
# Face: f v1 v2 v3 or f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3
parts = line.split()[1:]
if len(parts) != 3:
print(f"[Warning] Skipping non-triangle face: {line}")
continue
idx = []
for p in parts:
# Handle formats like "1", "1/1", "1/1/1", "1//1"
if "/" in p:
p = p.split("/")[0]
vi = int(p)
# OBJ uses 1-based indexing, convert to 0-based
if vi < 0:
vi = len(vertices) + 1 + vi
idx.append(vi - 1)
faces.append(idx)
V = np.array(vertices, dtype=np.float64)
F = np.array(faces, dtype=np.int32)
print(f"[Info] Loaded {len(V)} vertices, {len(F)} triangles")
return V, F
def save_obj(output_path, vertices, quads, tris):
"""Save vertices + quads + triangles to OBJ file"""
print(f"[Info] Saving to: {output_path}")
with open(output_path, "w", encoding="utf-8") as f:
f.write("# Quad-dominant mesh generated by quad_converter\n")
f.write(f"# Vertices: {len(vertices)}\n")
f.write(f"# Quads: {len(quads)}\n")
f.write(f"# Triangles: {len(tris)}\n\n")
# Write vertices
for v in vertices:
f.write(f"v {v[0]:.9f} {v[1]:.9f} {v[2]:.9f}\n")
f.write("\n")
# Write quad faces (4 vertices)
for q in quads:
# Convert 0-based to 1-based indexing
a, b, c, d = int(q[0]) + 1, int(q[1]) + 1, int(q[2]) + 1, int(q[3]) + 1
f.write(f"f {a} {b} {c} {d}\n")
# Write triangle faces (3 vertices)
for t in tris:
a, b, c = int(t[0]) + 1, int(t[1]) + 1, int(t[2]) + 1
f.write(f"f {a} {b} {c}\n")
print(f"[OK] Saved successfully!")
def main():
# write your own input and output paths here!!!
input_obj = "<your input tri-obj path>"
output_obj = "<your output quad-dominant obj path>"
vertices, triangles = load_obj(input_obj)
print("\n[Info] Converting triangles to quads...")
print(f"[Info] Using method: blossom (optimal matching)")
print(f"[Info] Angle threshold: 150.0 degrees")
result = quad_converter.tri_to_quad(
vertices,
triangles,
angle_threshold_deg=150.0,
method="blossom",
verbose=True
)
new_vertices = result["new_vertices"]
quad_faces = result["quads"]
tri_faces = result["tris"]
stats = result["merge_stats"]
print("\n" + "="*60)
print("Conversion Results:")
print("="*60)
print(f"Generated quads: {len(quad_faces)}")
print(f"Remaining triangles: {len(tri_faces)}")
print(f"Total faces: {len(quad_faces) + len(tri_faces)}")
print(f"Vertices: {len(new_vertices)}")
if stats:
print("\nDetailed Statistics:")
for key, value in stats.items():
print(f" {key}: {value}")
original_tri_count = len(triangles)
quad_count = len(quad_faces)
conversion_rate = (quad_count * 2) / original_tri_count * 100 if original_tri_count > 0 else 0
print(f"\nConversion rate: {conversion_rate:.1f}% of triangles converted to quads")
print("="*60 + "\n")
save_obj(output_obj, new_vertices, quad_faces, tri_faces)
print(f"\n✅ Done! Check the output file:")
print(f" {output_obj}")
if __name__ == "__main__":
main()