File size: 4,838 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
from __future__ import annotations

import math
import xml.etree.ElementTree as ET
from pathlib import Path


TASK_ID = "c-eagle-task-10-ubuntu"
EVAL_DIR = Path(__file__).resolve().parent
INIT_FILE = next(
    (path for path in (EVAL_DIR / "qfn_board.brd", EVAL_DIR / "init_file" / "qfn_board.brd") if path.exists()),
    EVAL_DIR / "qfn_board.brd",
)
TARGET_PADS = ("1", "3", "5", "7")
PAD_TOL = 0.8
VIA_TOL = 1e-3


def _sig(elem: ET.Element) -> tuple[tuple[str, str], ...]:
    return tuple(sorted(elem.attrib.items()))


def _element_position(root: ET.Element, name: str) -> tuple[float, float]:
    el = root.find(f".//elements/element[@name='{name}']")
    if el is None:
        raise ValueError(f"missing element {name}")
    return float(el.get("x", "0")), float(el.get("y", "0"))


def _pad_centers(root: ET.Element) -> dict[str, tuple[float, float]]:
    pkg = root.find(".//packages/package[@name='QFN8']")
    if pkg is None:
        raise ValueError("missing QFN8 package")
    ux, uy = _element_position(root, "U1")
    pads: dict[str, tuple[float, float]] = {}
    for smd in pkg.findall("smd"):
        name = smd.get("name")
        if not name:
            continue
        pads[name] = (ux + float(smd.get("x", "0")), uy + float(smd.get("y", "0")))
    return pads


def _nearest_pad(point: tuple[float, float], pads: dict[str, tuple[float, float]]) -> tuple[str, float]:
    best_name = ""
    best_dist = float("inf")
    px, py = point
    for name, (x, y) in pads.items():
        dist = math.hypot(px - x, py - y)
        if dist < best_dist:
            best_name = name
            best_dist = dist
    return best_name, best_dist


def _nearest_point(
    point: tuple[float, float],
    points: dict[tuple[float, float], str],
) -> tuple[str, float]:
    best_name = ""
    best_dist = float("inf")
    px, py = point
    for (x, y), name in points.items():
        dist = math.hypot(px - x, py - y)
        if dist < best_dist:
            best_name = name
            best_dist = dist
    return best_name, best_dist


def _wire_match(
    wire: ET.Element,
    pads: dict[str, tuple[float, float]],
    vias: dict[tuple[float, float], str],
) -> tuple[str, str] | None:
    p1 = (float(wire.get("x1", "nan")), float(wire.get("y1", "nan")))
    p2 = (float(wire.get("x2", "nan")), float(wire.get("y2", "nan")))
    candidates = []
    for near, far in ((p1, p2), (p2, p1)):
        pad, pad_dist = _nearest_pad(near, pads)
        via_name, via_dist = _nearest_point(far, vias)
        if pad in TARGET_PADS and pad_dist <= PAD_TOL and via_dist <= VIA_TOL:
            candidates.append((pad, via_name))
    if len(candidates) != 1:
        return None
    return candidates[0]


def evaluate(submission_dir: str) -> bool:
    sub = Path(submission_dir).resolve()
    target = None
    for name in ("fanout.brd",):
        p = sub / name
        if p.exists():
            target = p
            break
    if target is None or not INIT_FILE.exists():
        return False

    try:
        starter = ET.parse(INIT_FILE).getroot()
        result = ET.parse(target).getroot()
    except Exception:
        return False

    if not any(
        el.get("name") == "U1" and el.get("package") == "QFN8"
        for el in result.findall(".//element")
    ):
        return False

    if len(result.findall(".//element")) != len(starter.findall(".//element")):
        return False

    orig_wire_sigs = {_sig(node) for node in starter.findall(".//wire")}
    orig_via_sigs = {_sig(node) for node in starter.findall(".//via")}
    added_wires = [node for node in result.findall(".//wire") if _sig(node) not in orig_wire_sigs]
    added_vias = [node for node in result.findall(".//via") if _sig(node) not in orig_via_sigs]
    if len(added_wires) != 4 or len(added_vias) != 4:
        return False

    pads = _pad_centers(result)
    via_points: dict[tuple[float, float], str] = {}
    for via in added_vias:
        point = (float(via.get("x", "nan")), float(via.get("y", "nan")))
        via_points[point] = via.get("id") or via.get("name") or f"{point[0]},{point[1]}"

    used_pads: set[str] = set()
    used_vias: set[str] = set()
    for wire in added_wires:
        match = _wire_match(wire, pads, via_points)
        if match is None:
            return False
        pad, via_name = match
        if pad in used_pads or via_name in used_vias:
            return False
        used_pads.add(pad)
        used_vias.add(via_name)

    return used_pads == set(TARGET_PADS) and len(used_vias) == 4


if __name__ == "__main__":
    import sys

    if len(sys.argv) > 2:
        print("usage: python eval.py SUBMISSION_DIR", file=sys.stderr)
        sys.exit(2)
    submission_dir = sys.argv[1] if len(sys.argv) == 2 else Path(__file__).resolve().parent
    print("True" if evaluate(submission_dir) else "False")