-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.py
More file actions
66 lines (45 loc) · 1.85 KB
/
commands.py
File metadata and controls
66 lines (45 loc) · 1.85 KB
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
"""Type definitions for the drawing-robot pipeline.
The output JSON shape mirrors the user's TypeScript discriminated union:
type DrawingCommand =
| { kind: "line", distance: number, penDown: boolean }
| { kind: "spin", degrees: number }
| { kind: "arc", radius: number, degrees: number }
Conventions:
- Coordinates are image-space pixel coordinates (x right, y down).
- Angles are measured in this frame: positive = counter-clockwise in the
(x-right, y-down) frame, which is clockwise when rendered to a screen.
This is consistent with SVG, so the renderer round-trips correctly.
- For arcs, `radius` is always positive; the sign of `degrees` indicates
direction (positive = curve to the left of current heading).
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import List, Literal, TypedDict, Union
import numpy as np
# ---- Output JSON command types (match the TS discriminated union) -----------
class LineCommand(TypedDict):
kind: Literal["line"]
distance: float
penDown: bool
class SpinCommand(TypedDict):
kind: Literal["spin"]
degrees: float
class ArcCommand(TypedDict):
kind: Literal["arc"]
radius: float
degrees: float
DrawingCommand = Union[LineCommand, SpinCommand, ArcCommand]
# ---- Internal geometric primitives (used during fitting) --------------------
@dataclass
class LinePrimitive:
start: np.ndarray # shape (2,)
end: np.ndarray # shape (2,)
@dataclass
class ArcPrimitive:
center: np.ndarray # shape (2,)
radius: float
start: np.ndarray # shape (2,) — point on the circle
end: np.ndarray # shape (2,) — point on the circle
ccw: bool # direction of traversal from start to end
Primitive = Union[LinePrimitive, ArcPrimitive]
Stroke = List[Primitive] # connected chain of primitives sharing endpoints