Repository navigation
Expand file tree
/
Copy pathmisc.py
More file actions
437 lines (355 loc) · 12.6 KB
/
Copy pathmisc.py
File metadata and controls
437 lines (355 loc) · 12.6 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import os
import torch
import trimesh
import tempfile
import numpy as np
from tqdm import tqdm
from typing import List, Tuple
from torch.distributed import get_rank
from occwl.face import Face
from occwl.edge import Edge
from occwl.solid import Solid
from OCC.Core.gp import gp_Pnt
from OCC.Core.BRep import BRep_Tool
from OCC.Core.TopAbs import TopAbs_EDGE
from OCC.Core.StlAPI import StlAPI_Writer
from OCC.Core.TopExp import TopExp_Explorer
from OCC.Core.GCPnts import GCPnts_AbscissaPoint
from OCC.Core.GeomAdaptor import GeomAdaptor_Curve
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
from OCC.Core.GeomAPI import GeomAPI_ProjectPointOnCurve
from OCC.Core.TopoDS import TopoDS_Compound, TopoDS_Edge, topods
from cadmodel.coordinatesys import CoordinateSystem
ROUND_JSON = 6
MAX_PART_LENGTH = 64
MAX_VECTOR_LENGTH = 256
EXTRUDE_OPERATIONS = [
"NewBodyFeatureOperation",
"JoinFeatureOperation",
"CutFeatureOperation",
"IntersectFeatureOperation",
]
EXTENT_TYPE = [
"OneSideFeatureExtentType",
"SymmetricFeatureExtentType",
"TwoSidesFeatureExtentType",
]
STANDARD_PLANES = {
"Top": np.array([0, 0, 1]),
"Right": np.array([1, 0, 0]),
"Front": np.array([0, -1, 0]),
}
TOKEN = [
"<|padding|>",
"<|model_end|>",
"<|part_end|>",
"<|sketch_start|>",
"<|extrude_start|>",
"<|chamfer_start|>",
"<|fillet_start|>",
"<|profile_start|>",
"<|loop_start|>",
"<|curve_start|>",
"<|pointer_enable|>",
"<|pointer_disable|>",
"<|clockwise|>",
"<|counter_clockwise|>",
"<|direction_x+|>",
"<|direction_x-|>",
"<|direction_y+|>",
"<|direction_y-|>",
"<|direction_z+|>",
"<|direction_z-|>",
"<|extrude_new|>",
"<|extrude_join|>",
"<|extrude_cut|>",
"<|extrude_intersect|>",
]
def token_quantize(data, min_val=0.0, max_val=1.0, bit=8):
if bit == 0:
return data
q_data = quantize(data, min_val, max_val, bit)
return q_data + len(TOKEN)
def token_dequantize(data, min_val=0.0, max_val=1.0, bit=8):
if bit == 0:
return data
if isinstance(data, (int, float)):
return dequantize(data - len(TOKEN), min_val, max_val, bit)
elif isinstance(data, list):
return dequantize([d - len(TOKEN) for d in data], min_val, max_val, bit)
def quantize(data, min_val=0.0, max_val=1.0, bit=8):
if bit == 0:
return data
levels = 2**bit - 1
range_val = max_val - min_val
if range_val <= 0:
raise ValueError("max_val must be greater than min_val")
def _quantize_scalar(x):
scaled = (x - min_val) / range_val
scaled = min(max(scaled, 0.0), 1.0)
return int(round(scaled * levels))
if isinstance(data, (int, float)):
return _quantize_scalar(data)
elif isinstance(data, np.ndarray):
return np.round(
np.clip((data - min_val) / range_val, 0.0, 1.0) * levels
).astype(np.int32)
elif isinstance(data, list):
return [_quantize_scalar(x) for x in data]
elif isinstance(data, tuple):
return tuple(_quantize_scalar(x) for x in data)
else:
raise TypeError(f"Unsupported data type: {type(data)}")
def dequantize(quantized_data, min_val=0.0, max_val=1.0, bit=8):
if bit == 0:
return quantized_data
levels = 2**bit - 1
range_val = max_val - min_val
if range_val <= 0:
raise ValueError("max_val must be greater than min_val")
def _dequantize_scalar(q):
return float(q) / levels * range_val + min_val
if isinstance(quantized_data, (int, float)):
return _dequantize_scalar(quantized_data)
elif isinstance(quantized_data, np.ndarray):
return quantized_data.astype(np.float64) / levels * range_val + min_val
elif isinstance(quantized_data, list):
return [_dequantize_scalar(q) for q in quantized_data]
elif isinstance(quantized_data, tuple):
return tuple(_dequantize_scalar(q) for q in quantized_data)
else:
raise TypeError(f"Unsupported data type: {type(quantized_data)}")
def quantization_mapping(value, min_val=0.0, max_val=1.0, bit=8):
if bit == 0:
return value
quantized = quantize(value, min_val, max_val, bit)
dequantized = dequantize(quantized, min_val, max_val, bit)
return dequantized
def find_edge_by_points_and_length(
shape: TopoDS_Compound,
points: List[Tuple[float, float, float]],
length: float,
length_tolerance: float = 1e-2,
point_tolerance: float = 1e-4,
) -> TopoDS_Edge:
"""
Find the edge (TopoDS_Edge) that best matches a point sequence and target length.
Candidates are ranked by length error first, then by point projection error.
Args:
shape: TopoDS_Compound
points: List of points, each as (x, y, z)
length: Expected edge length
length_tolerance: Max allowed length deviation
point_tolerance: Point projection distance tolerance
Returns:
Best matching TopoDS_Edge, or None if no valid match is found.
"""
def points_on_edge(edge, pts, tol):
sum_error = 0
curve_handle, first, last = BRep_Tool.Curve(edge)
if curve_handle is None:
return -1
for pt in pts:
pnt = gp_Pnt(*pt)
projector = GeomAPI_ProjectPointOnCurve(pnt, curve_handle)
if projector.NbPoints() == 0:
return -1
dist = projector.LowerDistance()
if dist > tol:
return -1
sum_error += dist
return sum_error
candidates = []
exp = TopExp_Explorer(shape, TopAbs_EDGE)
while exp.More():
edge = topods.Edge(exp.Current())
curve_handle, first, last = BRep_Tool.Curve(edge)
if curve_handle is None:
exp.Next()
continue
if length >= 0:
edge_length = GCPnts_AbscissaPoint.Length(
GeomAdaptor_Curve(curve_handle, first, last)
)
length_err = abs(edge_length - length)
if length_err > length_tolerance:
exp.Next()
continue
else:
length_err = 0
point_error = points_on_edge(edge, points, point_tolerance)
if point_error >= 0:
candidates.append((edge, length_err, point_error))
exp.Next()
if not candidates:
return None
candidates.sort(key=lambda x: (x[1], x[2]))
best_edge = candidates[0][0]
return best_edge
def project_point_to_edge(
csys: CoordinateSystem, point: list, edge: Edge, tolerance: float = 0.2
):
def distance(p1, p2) -> float:
return (
(p1.X() - p2.X()) ** 2 + (p1.Y() - p2.Y()) ** 2 + (p1.Z() - p2.Z()) ** 2
) ** 0.5
point_coords = csys.relative2world(point)
world_point = gp_Pnt(*point_coords)
projector = GeomAPI_ProjectPointOnCurve(world_point, edge.curve())
if projector.NbPoints() > 0:
closest_point = projector.NearestPoint()
if distance(world_point, closest_point) < tolerance:
world_point = np.array(
[closest_point.X(), closest_point.Y(), closest_point.Z()]
)
return csys.world2relative(world_point)[:2]
else:
return np.array(point)
return None
def get_sketch_plane_normal(plane):
if isinstance(plane, str):
return STANDARD_PLANES[plane], np.array([0, 0, 0])
elif isinstance(plane, Face):
face_origin_uv = plane.uv_bounds().center()
face_origin = plane.point(face_origin_uv)
face_normal = plane.normal(face_origin_uv)
face_normal = face_normal / np.linalg.norm(face_normal)
return face_normal, face_origin
else:
raise ValueError("Unable to parse sketch plane normal vector.")
def match_sketch_plane_from_solid(
solid: Solid,
coordinate: CoordinateSystem,
include_standard_planes: bool = False,
normal_tol: float = 1e-4,
point_tol: float = 1e-4,
):
"""
Match a sketch plane against planar faces in a solid.
Args:
solid: Solid object.
coordinate: Sketch plane coordinate system.
include_standard_planes: Whether to also check XOY/YOZ/XOZ planes.
normal_tol: Normal-vector tolerance.
point_tol: Point-to-plane distance tolerance.
Returns:
List of matched Face objects.
"""
matched_faces = []
matched_planes = []
sketch_origin = np.array(coordinate.origin)
sketch_normal = np.array(coordinate.normal)
sketch_normal = sketch_normal / np.linalg.norm(sketch_normal)
if solid is not None:
for face in solid.faces():
if face.surface_type() != "plane":
continue
face_origin_uv = face.uv_bounds().center()
face_origin = face.point(face_origin_uv)
face_normal = face.normal(face_origin_uv)
face_normal = face_normal / np.linalg.norm(face_normal)
dot = np.dot(sketch_normal, face_normal)
if abs(abs(dot) - 1.0) > normal_tol:
continue
vec = face_origin - sketch_origin
dist = abs(np.dot(vec, sketch_normal))
if dist > point_tol:
continue
matched_faces.append(face)
if include_standard_planes:
for name, std_n in STANDARD_PLANES.items():
dot_std = np.dot(sketch_normal, std_n)
if abs(abs(dot_std) - 1.0) > normal_tol:
continue
dist_std = abs(np.dot(std_n, sketch_origin))
if dist_std > point_tol:
continue
matched_planes.append(name)
return matched_faces, matched_planes
def match_curve_from_edges(
edges: List[Edge],
csys: CoordinateSystem,
xy: Tuple[float, float],
tolerance: float = 1e-4,
) -> List[Edge]:
"""
Find all edges that overlap a given point within tolerance.
Args:
- edges: List of Edge objects.
- csys: CoordinateSystem object with relative2world(xy).
- xy: 2D coordinate (x, y).
- tolerance: Matching tolerance.
Returns:
- List of matched edges (can be empty).
"""
def distance(p1, p2) -> float:
return (
(p1.X() - p2.X()) ** 2 + (p1.Y() - p2.Y()) ** 2 + (p1.Z() - p2.Z()) ** 2
) ** 0.5
point_coords = csys.relative2world(xy)
world_point = gp_Pnt(*point_coords)
matched_edges = []
for edge in edges:
curve = edge.curve()
projector = GeomAPI_ProjectPointOnCurve(world_point, curve)
if projector.NbPoints() > 0:
closest_point = projector.NearestPoint()
if distance(world_point, closest_point) <= tolerance:
matched_edges.append(edge)
return matched_edges
class DummyTQDM:
def __init__(self, iterable, *args, **kwargs):
self.iterable = iterable
def __iter__(self):
return iter(self.iterable if self.iterable else [])
def __enter__(self):
return self
def __exit__(self, *args):
pass
def set_postfix(self, *args, **kwargs):
pass
def update(self, *args, **kwargs):
pass
def get_progress_bar(iterable=None, **kwargs):
if (get_rank() % torch.cuda.device_count()) == 0:
return tqdm(iterable, **kwargs)
else:
return DummyTQDM(iterable)
def create_mesh(
model, mode="ascii", linear_deflection=0.001, angular_deflection=0.5
) -> trimesh.Trimesh:
"""
Create a trimesh.Trimesh object from an occwl.Solid model.
Parameters:
model (Solid): The occwl Solid to be meshed.
mode (str): 'ascii' or 'binary' STL export mode. Default is 'ascii'.
linear_deflection (float): Controls mesh accuracy. Smaller = finer mesh.
angular_deflection (float): Controls angular tolerance. Smaller = more accurate.
Returns:
trimesh.Trimesh: The mesh representation of the model.
"""
a_shape = model.build_model().topods_shape()
if a_shape.IsNull():
raise ValueError("Input model shape is null.")
if mode not in ["ascii", "binary"]:
raise ValueError("mode must be 'ascii' or 'binary'")
# Perform OpenCascade mesh generation
mesh = BRepMesh_IncrementalMesh(
a_shape, linear_deflection, False, angular_deflection, True
)
mesh.Perform()
if not mesh.IsDone():
raise RuntimeError("Mesh generation failed.")
# Create a temporary STL file
with tempfile.NamedTemporaryFile(suffix=".stl", delete=False) as temp_file:
filename = temp_file.name
stl_writer = StlAPI_Writer()
stl_writer.SetASCIIMode(mode == "ascii")
stl_writer.Write(a_shape, filename)
if not os.path.exists(filename):
raise IOError(f"Temporary STL file not created: {filename}")
try:
mesh = trimesh.load(filename, force="mesh")
finally:
os.remove(filename)
return mesh