CAD / CODE /evaluation /eval /eval_geometric042.py
641071660J's picture
Add frozen CODE evaluation toolkit and portable full-GT scoring CLI
15d1f1d verified
Raw History Blame Contribute Delete
2.66 kB
#!/usr/bin/env python3
"""Metric-v4.2: near-straight ARC flattening with endpoint snapping.
DXF start/end angles are sometimes rounded, so the computed chord endpoint of
a huge-radius near-line can miss the adjoining LINE by a few hundredths of a
millimetre. V4.2 snaps only such flattened-arc endpoints to an existing line
endpoint inside the same sub-tolerance used to decide visual straightness.
"""
from __future__ import annotations
import math
from typing import Any
try:
from . import eval_geometric04 as BASE
from . import eval_geometric041 as ARC
except ImportError:
import eval_geometric04 as BASE
import eval_geometric041 as ARC
extract_entities = BASE.extract_entities
legacy_dimension_entities = BASE.legacy_dimension_entities
representation_quality = BASE.representation_quality
score_dimension_consistency = BASE.score_dimension_consistency
score_disk_semantics = BASE.score_disk_semantics
def _nearest_endpoint(
point: tuple[float, float],
endpoints: list[tuple[float, float]],
tolerance: float,
) -> tuple[float, float]:
if not endpoints:
return point
nearest = min(endpoints, key=lambda candidate: math.dist(point, candidate))
return nearest if math.dist(point, nearest) <= tolerance else point
def canonicalize_entities(
entities: dict[str, Any],
*,
abs_tol: float = BASE.CANONICAL_ABS_TOL,
rel_tol: float = BASE.CANONICAL_REL_TOL,
) -> dict[str, Any]:
diagonal = max(BASE.EV2.bbox_diagonal(entities), 1.0)
flatten_tolerance = max(0.05, 0.001 * diagonal)
original_lines = list(entities.get("lines", []))
endpoints = [
(float(x), float(y))
for line in original_lines
for x, y in (line[:2], line[2:4])
]
prepared = {
"lines": original_lines,
"circles": list(entities.get("circles", [])),
"arcs": [],
"dims": list(entities.get("dims", [])),
}
for arc in entities.get("arcs", []):
if ARC._sagitta(arc) <= flatten_tolerance:
chord = ARC._arc_chord(arc)
first = _nearest_endpoint((chord[0], chord[1]), endpoints, flatten_tolerance)
second = _nearest_endpoint((chord[2], chord[3]), endpoints, flatten_tolerance)
prepared["lines"].append((*first, *second, chord[4]))
else:
prepared["arcs"].append(arc)
return BASE.canonicalize_entities(
prepared,
abs_tol=abs_tol,
rel_tol=rel_tol,
)
__all__ = [
"canonicalize_entities",
"extract_entities",
"legacy_dimension_entities",
"representation_quality",
"score_dimension_consistency",
"score_disk_semantics",
]