File size: 2,660 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 | #!/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",
]
|