File size: 2,697 Bytes
15d1f1d | 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 | #!/usr/bin/env python3
"""Metric-v4.1 representation normalisation.
Some CAD exports encode an almost-straight outline edge as a huge-radius ARC.
At engineering-drawing tolerance that is the same geometry as its chord, but a
primitive-family matcher treats it as unrelated to a LINE. V4.1 flattens only
arcs whose sagitta is below 0.05 mm or 0.1% of the drawing diagonal, then uses
the otherwise unchanged and tested v4 implementation.
"""
from __future__ import annotations
import math
from typing import Any
try:
from . import eval_geometric02 as EV2
from . import eval_geometric04 as BASE
except ImportError:
import eval_geometric02 as EV2
import eval_geometric04 as BASE
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 _arc_chord(arc: tuple[Any, ...]) -> tuple[float, float, float, float, str]:
cx, cy, radius, start, end, layer = arc
start_rad = math.radians(float(start))
end_rad = math.radians(float(end))
return (
float(cx) + float(radius) * math.cos(start_rad),
float(cy) + float(radius) * math.sin(start_rad),
float(cx) + float(radius) * math.cos(end_rad),
float(cy) + float(radius) * math.sin(end_rad),
str(layer),
)
def _sagitta(arc: tuple[Any, ...]) -> float:
radius = abs(float(arc[2]))
span = math.radians(float((float(arc[4]) - float(arc[3])) % 360.0))
return radius * (1.0 - math.cos(span / 2.0))
def canonicalize_entities(
entities: dict[str, Any],
*,
abs_tol: float = BASE.CANONICAL_ABS_TOL,
rel_tol: float = BASE.CANONICAL_REL_TOL,
) -> dict[str, Any]:
"""Flatten visually straight arcs, then apply v4 collinear merging."""
diagonal = max(BASE.EV2.bbox_diagonal(entities), 1.0)
flatten_tolerance = max(0.05, 0.001 * diagonal)
prepared = {
"lines": list(entities.get("lines", [])),
"circles": list(entities.get("circles", [])),
"arcs": [],
"dims": list(entities.get("dims", [])),
}
for arc in entities.get("arcs", []):
if _sagitta(arc) <= flatten_tolerance:
prepared["lines"].append(_arc_chord(arc))
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",
]
|