Repository navigation
Expand file tree
/
Copy pathhybrid.py
More file actions
220 lines (203 loc) · 8.28 KB
/
Copy pathhybrid.py
File metadata and controls
220 lines (203 loc) · 8.28 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Local repair of a prismatic solid that fails validation.
Fusion keeps the face groups it could convert and leaves the rest as
surfaces; the industry norm is never all-or-nothing. Here: find where the
rebuilt solid deviates from the mesh, box those regions, and replace the
solid inside the boxes with the exact faceted geometry of the mesh:
result = (P - B) U (F ∩ B)
P = prismatic solid, F = faceted solid of the mesh, B = union of patch
boxes. Outside the boxes the clean analytic faces survive; inside, the
part is exact. The patched solid is re-validated by the caller.
"""
import numpy as np
import trimesh
def bad_regions(mesh, metrics_pts, dist, rev_pts, rev_dist, threshold,
link_radius, margin, max_regions=40):
"""Cluster points that deviate more than `threshold` (from either
direction) into regions; return a list of (lo, hi) boxes."""
pts = []
if len(metrics_pts):
pts.append(metrics_pts[dist > threshold])
if rev_pts is not None and len(rev_pts):
pts.append(rev_pts[rev_dist > threshold])
if not pts:
return []
P = np.vstack(pts)
if len(P) == 0:
return []
from scipy.spatial import cKDTree
pairs = cKDTree(P).query_pairs(link_radius, output_type='ndarray')
parent = np.arange(len(P))
def find(i):
while parent[i] != i:
parent[i] = parent[parent[i]]
i = parent[i]
return i
for a, b in pairs:
ra, rb = find(a), find(b)
if ra != rb:
parent[max(ra, rb)] = min(ra, rb)
roots = np.array([find(i) for i in range(len(P))])
boxes = []
for r in np.unique(roots):
Q = P[roots == r]
if len(Q) < 3:
continue
lo = Q.min(axis=0) - margin
hi = Q.max(axis=0) + margin
boxes.append((lo, hi))
# merge overlapping boxes
merged = True
while merged and len(boxes) > 1:
merged = False
out = []
used = [False] * len(boxes)
for i in range(len(boxes)):
if used[i]:
continue
lo, hi = boxes[i]
for j in range(i + 1, len(boxes)):
if used[j]:
continue
lo2, hi2 = boxes[j]
if np.all(lo <= hi2) and np.all(lo2 <= hi):
lo, hi = np.minimum(lo, lo2), np.maximum(hi, hi2)
used[j] = True
merged = True
out.append((lo, hi))
boxes = out
boxes.sort(key=lambda b: -np.prod(b[1] - b[0]))
return boxes[:max_regions]
_FACETED_CACHE = {}
def faceted_reference(mesh, verbose=False):
"""Exact faceted solid of the mesh, cached per mesh object (patching
runs several rounds; sewing thousands of triangles is the slow part)."""
import cadquery as cq
from .rebuild import faceted_solid
key = id(mesh)
hit = _FACETED_CACHE.get(key)
if hit is not None and hit[0] is mesh:
return hit[1], hit[2]
fshape, stats = faceted_solid(mesh, verbose=verbose)
F = cq.Shape.cast(fshape)
_FACETED_CACHE.clear()
_FACETED_CACHE[key] = (mesh, F, stats)
return F, stats
def patch_solid(solid, mesh, boxes, verbose=True, fuzzies=(5e-2, 2e-2, 1e-1)):
"""(P - B) U (F ∩ B) for the union of the boxes, tried with several
fuzzy tolerances; the first attempt that yields one valid solid is
returned as a CadQuery Workplane, else None."""
import cadquery as cq
from .rebuild import finish_solid
from OCP.BRepCheck import BRepCheck_Analyzer
P = solid.val() if hasattr(solid, 'val') else cq.Shape.cast(solid)
F, stats = faceted_reference(mesh)
if not stats.get('is_solid', False):
if verbose:
print('[patch] faceted reference is not a closed solid; cannot patch')
return None
box_shapes = [cq.Solid.makeBox(*(hi - lo), pnt=cq.Vector(*lo)) for lo, hi in boxes]
B = box_shapes[0] if len(box_shapes) == 1 else box_shapes[0].fuse(*box_shapes[1:])
for fuzzy in fuzzies:
try:
outside = P.cut(B, tol=fuzzy)
inside = F.intersect(B, tol=fuzzy)
R = outside.fuse(inside, tol=fuzzy).clean()
except Exception as e:
if verbose:
print(f'[patch] boolean failed at fuzzy {fuzzy} ({type(e).__name__})')
continue
if len(R.Solids()) > 1:
try:
R2 = R.Solids()[0].fuse(*R.Solids()[1:], tol=2 * fuzzy).clean()
if len(R2.Solids()) == 1:
R = R2
except Exception:
pass
if len(R.Solids()) == 1 and not BRepCheck_Analyzer(R.wrapped).IsValid():
try:
from OCP.ShapeFix import ShapeFix_Shape
fx = ShapeFix_Shape(R.wrapped)
fx.SetPrecision(fuzzy)
fx.SetMaxTolerance(10 * fuzzy)
fx.Perform()
R3 = cq.Shape.cast(fx.Shape())
if len(R3.Solids()) == 1 and BRepCheck_Analyzer(R3.wrapped).IsValid():
R = R3
except Exception:
pass
if len(R.Solids()) == 1 and BRepCheck_Analyzer(R.wrapped).IsValid():
return finish_solid(cq.Workplane('XY').newObject([R]), verbose=False)
if verbose:
print('[patch] no fuzzy value produced one valid solid')
return None
def try_hybrid(solid, mesh, metrics, accept_max, tol, verbose=True,
max_bad_frac=0.25, rounds=3):
"""Attempt a local patch. Returns (patched_workplane, info) or
(None, info) when patching is not sensible / failed.
Up to `rounds` passes: regions still deviating after a patch are added
to the box set and the patch is rebuilt from the original solid."""
from .pipeline import tessellate_solid, sample_points
threshold = max(0.6 * accept_max, 2 * tol)
diag = float(np.linalg.norm(mesh.bounding_box.primitive.extents))
link = max(1.0, 4 * tol, 0.01 * diag)
margin = max(0.5, 3 * tol)
boxes = []
info = {'bad_frac': None, 'patches': 0}
current = solid
for rnd in range(rounds):
rb = tessellate_solid(current)
pts, n_uni = sample_points(mesh)
_, dist, _ = trimesh.proximity.closest_point(rb, pts)
rpts, rdist = None, None
if mesh.is_watertight:
rpts, _ = sample_points(rb, max(2000, n_uni // 2), include_vertices=False)
_, rdist, _ = trimesh.proximity.closest_point(mesh, rpts)
bad_frac = float((dist[:n_uni] > threshold).mean())
if rnd == 0:
info['bad_frac'] = bad_frac
if bad_frac > max_bad_frac:
if verbose:
print(f'[patch] {bad_frac:.0%} of the surface deviates; too much to patch')
return None, info
new_boxes = bad_regions(mesh, pts, dist, rpts, rdist, threshold, link, margin)
if not new_boxes:
break
boxes = _merge_boxes(boxes + new_boxes)
if verbose:
print(f'[patch] round {rnd + 1}: {len(boxes)} region(s) deviate > {threshold:.2f} mm '
f'({bad_frac:.1%} of the surface); replacing them with exact faceted geometry')
patched = patch_solid(solid, mesh, boxes, verbose=verbose)
if patched is None:
break
current = patched
info['patches'] = len(boxes)
info['boxes'] = [[np.round(lo, 2).tolist(), np.round(hi, 2).tolist()] for lo, hi in boxes]
# good enough? (the caller re-validates properly; this is a cheap check)
rb2 = tessellate_solid(current)
_, d2, _ = trimesh.proximity.closest_point(rb2, pts)
if d2.max() <= accept_max:
break
if current is solid:
return None, info
return current, info
def _merge_boxes(boxes):
merged = True
while merged and len(boxes) > 1:
merged = False
out = []
used = [False] * len(boxes)
for i in range(len(boxes)):
if used[i]:
continue
lo, hi = boxes[i]
for j in range(i + 1, len(boxes)):
if used[j]:
continue
lo2, hi2 = boxes[j]
if np.all(lo <= hi2) and np.all(lo2 <= hi):
lo, hi = np.minimum(lo, lo2), np.maximum(hi, hi2)
used[j] = True
merged = True
out.append((lo, hi))
boxes = out
return boxes