File size: 17,486 Bytes
5154c4e | 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 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | from __future__ import annotations
import math
import os
import struct
import zipfile
import xml.etree.ElementTree as ET
from pathlib import Path
OUTPUT_ROOT = Path(os.environ.get("EVAL_OUTPUT_ROOT", "/home/user/Desktop"))
SPEC = {'output': 'task-033_output.stl',
'bbox': [60.0, 42.0, 30.0],
'bbox_tol': 1.0,
'min_triangles': 40,
'checks': [{'kind': 'rect_outline', 'width': 54.0, 'height': 36.0, 'zs': [3.0, 30.0], 'tol': 1.0},
{'kind': 'cylinder', 'axis': 'z', 'center': [27.0, 18.0], 'radius': 3.0, 'span': 25.0, 'bins': 4}]}
def _parse_stl(path: Path):
data = path.read_bytes()
triangles = []
if len(data) >= 84:
count = struct.unpack("<I", data[80:84])[0]
if 84 + count * 50 == len(data):
for i in range(count):
values = struct.unpack("<12fH", data[84 + i * 50:134 + i * 50])
coords = values[3:12]
triangles.append(tuple((coords[j], coords[j + 1], coords[j + 2]) for j in range(0, 9, 3)))
return triangles
vertices = []
for raw in data.decode("utf-8", errors="ignore").splitlines():
parts = raw.strip().split()
if len(parts) == 4 and parts[0].lower() == "vertex":
vertices.append(tuple(float(v) for v in parts[1:]))
if len(vertices) == 3:
triangles.append(tuple(vertices))
vertices = []
return triangles
def _parse_off(path: Path):
lines = [line.strip() for line in path.read_text(encoding="utf-8", errors="ignore").splitlines() if line.strip() and not line.startswith("#")]
if not lines or lines[0] != "OFF":
return []
n_vertices, n_faces, _ = [int(v) for v in lines[1].split()[:3]]
vertices = [tuple(float(v) for v in lines[2 + i].split()[:3]) for i in range(n_vertices)]
triangles = []
for line in lines[2 + n_vertices:2 + n_vertices + n_faces]:
parts = [int(v) for v in line.split()]
if parts[0] >= 3:
ids = parts[1:1 + parts[0]]
for i in range(1, len(ids) - 1):
triangles.append((vertices[ids[0]], vertices[ids[i]], vertices[ids[i + 1]]))
return triangles
def _parse_3mf(path: Path):
with zipfile.ZipFile(path) as archive:
model_name = next(name for name in archive.namelist() if name.lower().endswith(".model"))
root = ET.fromstring(archive.read(model_name))
vertices = []
triangles = []
for elem in root.iter():
tag = elem.tag.split("}")[-1]
if tag == "vertex":
vertices.append((float(elem.attrib["x"]), float(elem.attrib["y"]), float(elem.attrib["z"])))
elif tag == "triangle":
triangles.append((vertices[int(elem.attrib["v1"])], vertices[int(elem.attrib["v2"])], vertices[int(elem.attrib["v3"])]))
return triangles
def _mesh_triangles(path: Path):
suffix = path.suffix.lower()
if suffix == ".stl":
return _parse_stl(path)
if suffix == ".off":
return _parse_off(path)
if suffix == ".3mf":
return _parse_3mf(path)
return []
class Mesh:
def __init__(self, triangles):
self.triangles = triangles
seen = {}
for tri in triangles:
for point in tri:
seen[(round(point[0], 3), round(point[1], 3), round(point[2], 3))] = point
self.vertices = list(seen.values())
if self.vertices:
self.mins = [min(p[i] for p in self.vertices) for i in range(3)]
self.maxs = [max(p[i] for p in self.vertices) for i in range(3)]
else:
self.mins = [0.0, 0.0, 0.0]
self.maxs = [0.0, 0.0, 0.0]
@property
def extents(self):
return [self.maxs[i] - self.mins[i] for i in range(3)]
@property
def center(self):
return [(self.maxs[i] + self.mins[i]) / 2 for i in range(3)]
def _mesh_topology_ok(mesh: Mesh, expected_components: int) -> bool:
vertex_ids = {}
indexed_faces = []
face_keys = set()
for triangle in mesh.triangles:
face = []
for point in triangle:
key = tuple(round(float(value), 5) for value in point)
if key not in vertex_ids:
vertex_ids[key] = len(vertex_ids)
face.append(vertex_ids[key])
if len(set(face)) != 3:
return False
face_key = tuple(sorted(face))
if face_key in face_keys:
return False
face_keys.add(face_key)
indexed_faces.append(face)
parent = list(range(len(indexed_faces)))
def find(item):
while parent[item] != item:
parent[item] = parent[parent[item]]
item = parent[item]
return item
def union(first, second):
a, b = find(first), find(second)
if a != b:
parent[b] = a
edge_owners = {}
for face_index, (a, b, c) in enumerate(indexed_faces):
for edge in ((a, b), (b, c), (c, a)):
edge_owners.setdefault(tuple(sorted(edge)), []).append((face_index, edge))
if not edge_owners or any(len(owners) != 2 for owners in edge_owners.values()):
return False
for owners in edge_owners.values():
first, second = owners
if first[1][0] != second[1][1] or first[1][1] != second[1][0]:
return False
union(first[0], second[0])
return len({find(index) for index in range(len(indexed_faces))}) == expected_components
def _close(actual, expected, tol):
return abs(float(actual) - float(expected)) <= tol
def _bbox_ok(mesh: Mesh, expected, tol):
return all(_close(actual, want, tol) for actual, want in zip(mesh.extents, expected))
def _axis_components(point, axis):
x, y, z = point
if axis == "z":
return x, y, z
if axis == "y":
return x, z, y
if axis == "x":
return y, z, x
raise ValueError(axis)
def _angle_in_range(angle, start, end):
if start is None:
return True
deg = math.degrees(angle)
if deg < 0:
deg += 360
start = float(start) % 360
end = float(end) % 360
if start <= end:
return start <= deg <= end
return deg >= start or deg <= end
def _cylinder_points(mesh: Mesh, check):
axis = check["axis"]
center_a, center_b = check["center"]
radius = float(check["radius"])
tol = float(check.get("tol", max(0.35, radius * 0.18)))
axial_range = check.get("axial_range")
angle_range = check.get("angle_range")
points = []
for point in mesh.vertices:
a, b, axial = _axis_components(point, axis)
if axial_range and not (float(axial_range[0]) - 0.75 <= axial <= float(axial_range[1]) + 0.75):
continue
angle = math.atan2(b - center_b, a - center_a)
if angle_range and not _angle_in_range(angle, angle_range[0], angle_range[1]):
continue
dist = math.hypot(a - center_a, b - center_b)
if abs(dist - radius) <= tol:
points.append((angle, axial))
return points
def _has_cylinder(mesh: Mesh, check):
points = _cylinder_points(mesh, check)
if len(points) < int(check.get("min_points", 8)):
return False
bins = {int(((angle + math.pi) / (2 * math.pi)) * 72) % 72 for angle, _ in points}
if len(bins) < int(check.get("bins", 10)):
return False
axials = [axis for _, axis in points]
return max(axials) - min(axials) >= float(check.get("span", 0.0))
def _has_circle_at(mesh: Mesh, check):
axis = check["axis"]
center_a, center_b = check.get("center", [0.0, 0.0])
radius = float(check["radius"])
axial = float(check["axial"])
tol = float(check.get("tol", 0.6))
bins = set()
for point in mesh.vertices:
a, b, t = _axis_components(point, axis)
if abs(t - axial) > float(check.get("axial_tol", 0.75)):
continue
dist = math.hypot(a - center_a, b - center_b)
if abs(dist - radius) <= tol:
angle = math.atan2(b - center_b, a - center_a)
bins.add(int(((angle + math.pi) / (2 * math.pi)) * 72) % 72)
return len(bins) >= int(check.get("bins", 10))
def _has_slot(mesh: Mesh, check):
cx, cy = check["center"]
length = float(check["length"])
width = float(check["width"])
span = float(check["span"])
tol = float(check.get("tol", 0.8))
pts = [p for p in mesh.vertices if abs(p[0] - cx) <= length / 2 + tol and abs(p[1] - cy) <= width / 2 + tol]
if len(pts) < 8:
return False
if max(p[2] for p in pts) - min(p[2] for p in pts) < span:
return False
if max(p[0] for p in pts) - min(p[0] for p in pts) < length - 1.0:
return False
if max(p[1] for p in pts) - min(p[1] for p in pts) < width - 0.8:
return False
offset = (length - width) / 2
left = {"axis": "z", "center": [cx - offset, cy], "radius": width / 2, "span": span, "bins": 4, "tol": 0.5}
right = {"axis": "z", "center": [cx + offset, cy], "radius": width / 2, "span": span, "bins": 4, "tol": 0.5}
return _has_cylinder(mesh, left) and _has_cylinder(mesh, right)
def _near_vertex(mesh: Mesh, target, tol):
tx, ty, tz = target
return any(abs(x - tx) <= tol and abs(y - ty) <= tol and abs(z - tz) <= tol for x, y, z in mesh.vertices)
def _has_rect_outline(mesh: Mesh, check):
width = float(check["width"])
height = float(check["height"])
tol = float(check.get("tol", 0.75))
for z in check["zs"]:
for x in (-width / 2, width / 2):
for y in (-height / 2, height / 2):
if not _near_vertex(mesh, (x, y, float(z)), tol):
return False
return True
def _has_box_outline(mesh: Mesh, check):
tol = float(check.get("tol", 1.0))
for x in check["xs"]:
for y in check["ys"]:
for z in check["zs"]:
if not _near_vertex(mesh, (float(x), float(y), float(z)), tol):
return False
return True
def _cluster_count(values, gap):
if not values:
return 0
values = sorted(values)
clusters = 1
last = values[0]
for value in values[1:]:
if value - last > gap:
clusters += 1
last = value
return clusters
def _has_rib_clusters(mesh: Mesh, check):
xs = []
for x, y, z in mesh.vertices:
if check["x_range"][0] <= x <= check["x_range"][1] and check["y_range"][0] <= y <= check["y_range"][1] and check["z_range"][0] <= z <= check["z_range"][1]:
xs.append(x)
return _cluster_count(xs, float(check.get("gap", 8.0))) >= int(check["count"])
def _has_raised(mesh: Mesh, check):
pts = [p for p in mesh.vertices if p[2] >= float(check["z_min"])]
if len(pts) < int(check.get("min_points", 8)):
return False
return (max(p[0] for p in pts) - min(p[0] for p in pts) >= float(check["x_span"]) and
max(p[1] for p in pts) - min(p[1] for p in pts) >= float(check["y_span"]))
def _has_steps(mesh: Mesh, check):
tol = float(check.get("tol", 0.75))
depth = float(check["depth"])
for item in check["levels"]:
z = float(item["top"])
width = float(item["width"])
for x in (-width / 2, width / 2):
for y in (-depth / 2, depth / 2):
if not _near_vertex(mesh, (x, y, z), tol):
return False
return True
def _has_angular_clusters(mesh: Mesh, check):
center = check.get("center", [0.0, 0.0])
r0, r1 = check["radius_range"]
z0, z1 = check["z_range"]
angles = []
for x, y, z in mesh.vertices:
r = math.hypot(x - center[0], y - center[1])
if float(r0) <= r <= float(r1) and float(z0) <= z <= float(z1):
deg = math.degrees(math.atan2(y - center[1], x - center[0]))
if deg < 0:
deg += 360
angles.append(deg)
if not angles:
return False
angles.sort()
clusters = 1
last = angles[0]
gap = float(check.get("gap", 6.0))
for angle in angles[1:]:
if angle - last > gap:
clusters += 1
last = angle
if angles[0] + 360 - angles[-1] <= gap and clusters > 1:
clusters -= 1
return clusters >= int(check["count"])
def _parse_dxf(path: Path):
raw = [line.strip() for line in path.read_text(encoding="utf-8", errors="ignore").splitlines()]
pairs = []
for i in range(0, len(raw) - 1, 2):
pairs.append((raw[i], raw[i + 1]))
entities = []
current = None
for code, value in pairs:
if code == "0":
if current:
entities.append(current)
current = {"type": value, "points": []}
continue
if current is None:
continue
if code == "10":
current.setdefault("_x", []).append(float(value))
elif code == "20":
xs = current.setdefault("_x", [])
if xs:
current["points"].append((xs.pop(0), float(value)))
elif code == "40":
current["radius"] = float(value)
if current:
entities.append(current)
circles = []
polylines = []
for ent in entities:
if ent.get("type") == "CIRCLE" and ent.get("points"):
x, y = ent["points"][0]
circles.append((x, y, float(ent.get("radius", 0.0))))
elif ent.get("type") in {"LWPOLYLINE", "POLYLINE"} and ent.get("points"):
polylines.append(ent["points"])
return circles, polylines
def _dxf_circle(circles, check):
cx, cy = check["center"]
r = float(check["radius"])
tol = float(check.get("tol", 0.5))
return any(abs(x - cx) <= tol and abs(y - cy) <= tol and abs(rad - r) <= tol for x, y, rad in circles)
def _dxf_rect(polylines, check):
width = float(check["width"])
height = float(check["height"])
cx, cy = check.get("center", [0.0, 0.0])
tol = float(check.get("tol", 0.75))
for pts in polylines:
xs = [p[0] for p in pts]
ys = [p[1] for p in pts]
if abs((max(xs) - min(xs)) - width) <= tol and abs((max(ys) - min(ys)) - height) <= tol:
if abs((max(xs) + min(xs)) / 2 - cx) <= tol and abs((max(ys) + min(ys)) / 2 - cy) <= tol:
return True
return False
def _dxf_outline_min(polylines, check):
width = float(check["min_width"])
height = float(check["min_height"])
for pts in polylines:
xs = [p[0] for p in pts]
ys = [p[1] for p in pts]
if max(xs) - min(xs) >= width and max(ys) - min(ys) >= height:
return True
return False
def _run_mesh_check(mesh: Mesh, check):
kind = check["kind"]
if kind == "cylinder":
return _has_cylinder(mesh, check)
if kind == "circle_at":
return _has_circle_at(mesh, check)
if kind == "slot":
return _has_slot(mesh, check)
if kind == "rib_clusters":
return _has_rib_clusters(mesh, check)
if kind == "rect_outline":
return _has_rect_outline(mesh, check)
if kind == "box_outline":
return _has_box_outline(mesh, check)
if kind == "raised":
return _has_raised(mesh, check)
if kind == "steps":
return _has_steps(mesh, check)
if kind == "angular_clusters":
return _has_angular_clusters(mesh, check)
return False
def _evaluate_mesh(path: Path) -> bool:
mesh = Mesh(_mesh_triangles(path))
if not _mesh_topology_ok(mesh, int(SPEC.get("components", 1))):
return False
if len(mesh.triangles) < int(SPEC.get("min_triangles", 1)):
return False
if len(mesh.vertices) < int(SPEC.get("min_vertices", 8)):
return False
tol = float(SPEC.get("bbox_tol", 1.0))
if "bbox" in SPEC and not _bbox_ok(mesh, SPEC["bbox"], tol):
return False
if "bbox_sorted_xy" in SPEC:
actual = sorted(mesh.extents[:2])
expected = sorted([float(v) for v in SPEC["bbox_sorted_xy"]])
if any(abs(a - b) > tol for a, b in zip(actual, expected)):
return False
if "z_extent" in SPEC and abs(mesh.extents[2] - float(SPEC["z_extent"])) > tol:
return False
if "center" in SPEC:
center_tol = float(SPEC.get("center_tol", tol))
if any(abs(a - b) > center_tol for a, b in zip(mesh.center, SPEC["center"])):
return False
return all(_run_mesh_check(mesh, check) for check in SPEC.get("checks", []))
def _evaluate_dxf(path: Path) -> bool:
circles, polylines = _parse_dxf(path)
for check in SPEC.get("checks", []):
if check["kind"] == "dxf_circle" and not _dxf_circle(circles, check):
return False
if check["kind"] == "dxf_rect" and not _dxf_rect(polylines, check):
return False
if check["kind"] == "dxf_outline_min" and not _dxf_outline_min(polylines, check):
return False
return True
def _evaluate_csg(path: Path) -> bool:
text = path.read_text(encoding="utf-8", errors="ignore")
if len(text) < int(SPEC.get("min_size", 1)):
return False
return all(token in text for token in SPEC.get("tokens", []))
def evaluate() -> bool:
path = OUTPUT_ROOT / SPEC["output"]
if not path.exists() or path.stat().st_size <= 0:
return False
suffix = path.suffix.lower()
if suffix == ".dxf":
return _evaluate_dxf(path)
if suffix == ".csg":
return _evaluate_csg(path)
return _evaluate_mesh(path)
if __name__ == "__main__":
try:
ok = evaluate()
except Exception:
ok = False
print("True" if ok else "False")
|