Skip to content

Generate clean 2D game sprites & animation atlases — component-row pipeline: state rows, alpha cleanup, frame extraction, runtime atlases. Codex/Claude skill.

feat(sway-inspect): a seam point where a third region moves more than the pair is no seam — record schema 3

Soohong Kim83f7240683ef
diff --git a/sprite_gen/video/sway_inspect.py b/sprite_gen/video/sway_inspect.py
index bda3bd0..2976c55 100644
--- a/sprite_gen/video/sway_inspect.py
+++ b/sprite_gen/video/sway_inspect.py
@@ -26,12 +26,14 @@ was already written, the strip itself where none was) and solves it with the sam
- `follow-inspect.source.png`, every cell of the strip as cut with each region's carried ellipse ringed;
- `follow-inspect.moved.png`, the same cells as `video-follow` would write them, ringed alike.
-A move does three things to the picture, and the record (schema 2) writes each apart. It stretches it: the least
+A move does three things to the picture, and the record (schema 3) writes each apart. It stretches it: the least
area a pixel takes (`jacobian_min`, D). It tilts a line: the move's steepest slope, |offset|·π/(2·smaller radius)
-(`bend`, G, and `bend_deg` = atan G) — the old `fold_ratio` is this number, and at 1 a line tilts 45°. And where
-two regions overlap, the larger move takes over across a curve and the slope jumps there: per pair, the largest
-jump on that curve (`seams`: the norm of the difference of the two moves' gradients, and its angle). A floor on
-the area (`--stretch-floor`) holds D up and lowers G and the seam only with it.
+(`bend`, G, and `bend_deg` = atan G; a region with a flat share falls over 1 − flat of its radius) — the old
+`fold_ratio` is this number, and at 1 a line tilts 45°. And where two regions overlap, the larger move takes over
+across a curve and the slope jumps there: per pair, the largest jump on that curve (`seams`: the norm of the
+difference of the two moves' gradients, and its angle) — at a point of it where a third region moves a pixel more
+than both, neither of the two moves it, and that point is no seam. A floor on the area (`--stretch-floor`) holds D up
+and lowers G and the seam only with it.
The two boards are apart so that whoever says what is inside an ellipse can read the cells as cut alone,
without the answer beside them. The record holds no path, no time and nothing of the machine: the same loop
@@ -66,7 +68,7 @@ from sprite_gen.util.console import utf8_streams
VERB = "video-sway-inspect"
OLD_VERB = "video-follow-inspect" # the old name, kept: it runs this and says its own name
KIND = "video-follow-inspection"
-SCHEMA_VERSION = 2 # 2: the bend and the seams beside the least area; the read_on and the stretch floor asked
+SCHEMA_VERSION = 3 # 2: the bend and the seams beside the least area; the read_on and the stretch floor asked. 3: no seam where a third region moves more
RECORD, SOURCE_BOARD, MOVED_BOARD = "follow-inspect.json", "follow-inspect.source.png", "follow-inspect.moved.png"
UNKNOWN = "unknown" # what is inside a carried ellipse: the engine does not read it
SCALE_DEFAULT = 2 # board pixels per cell pixel: at 2 or more a ring covers no cell pixel whole
@@ -75,6 +77,7 @@ PLACES = 4 # decimals of the offsets, ratios and areas: under what two machines
DEGREES = 2 # decimals of an angle
SEAM_STEP = 0.25 # px between the points along each row where a seam is looked for, before it is found by halving
SEAM_HALVINGS = 30
+SEAM_TIE = 1e-9 # a third region's weighted move this close to the pair's is a tie, and the point is kept
GROUND = (128, 128, 128) # under a cell's clear pixels
BETWEEN = (32, 32, 32) # between the cells, and under their numbers
INK = (255, 255, 255) # a cell's number
@@ -135,14 +138,18 @@ def _weight_and_gradient(x: np.ndarray, y: np.ndarray, ellipse: tuple[float, ...
return weight, slope * u / rx, slope * v / ry
-def seam(a: tuple[float, float, float, float], b: tuple[float, float, float, float], move_a: tuple[float, float],
- move_b: tuple[float, float], one_gain: bool) -> tuple[float, list[float]] | None:
+def seam(a: tuple[float, ...], b: tuple[float, ...], move_a: tuple[float, float], move_b: tuple[float, float], one_gain: bool,
+ others: list[tuple[tuple[float, ...], tuple[float, float]]] = ()) -> tuple[float, list[float]] | None:
"""Where two overlapping regions, carried into a cell and moved by their own offsets, meet: `video-follow` moves a
pixel by the larger of their weighted moves (by the larger weight where they move by one gain), so across the
curve where the two are equal the move's gradient jumps from one region's to the other's, and a straight line
bends there. The largest jump on that curve — the norm of the difference of the two gradients (each the offset
times its weight's gradient) — and the point it is at; None where no such curve is inside both.
+ `others` are the cell's other moving regions, each carried and with its offset: a pixel is moved by the largest of
+ every region's weighted move, so where one of them moves a point of the curve more than both of the two, neither
+ moves it, and the point is no seam; None where every point is such.
+
The curve is looked for along rows SEAM_STEP apart, between points SEAM_STEP apart inside both ellipses where the
larger move changes hands, and found there by halving."""
xs = np.arange(int((max(a[0] + a[2], b[0] + b[2]) - min(a[0] - a[2], b[0] - b[2])) / SEAM_STEP) + 1) * SEAM_STEP + min(a[0] - a[2], b[0] - b[2])
@@ -165,10 +172,18 @@ def seam(a: tuple[float, float, float, float], b: tuple[float, float, float, flo
same_side = (larger(mid, y)[0] > 0) == left
lo, hi = np.where(same_side, mid, lo), np.where(same_side, hi, mid)
x = (lo + hi) / 2
- _, ax, ay = _weight_and_gradient(x, y, a)
- _, bx, by = _weight_and_gradient(x, y, b)
+ wa, ax, ay = _weight_and_gradient(x, y, a)
+ wb, bx, by = _weight_and_gradient(x, y, b)
(oax, oay), (obx, oby) = move_a, move_b
jump = np.sqrt((oax * ax - obx * bx) ** 2 + (oax * ay - obx * by) ** 2 + (oay * ax - oby * bx) ** 2 + (oay * ay - oby * by) ** 2)
+ if others:
+ # As `sway.move_field` moves a pixel: by the largest of the weighted moves, |offset| times weight.
+ pair = np.maximum(math.hypot(*move_a) * wa, math.hypot(*move_b) * wb)
+ third = np.max([math.hypot(*move) * _weight_and_gradient(x, y, ellipse)[0] for ellipse, move in others], axis=0)
+ kept = third <= pair + SEAM_TIE
+ if not kept.any():
+ return None
+ jump = np.where(kept, jump, -1.0)
k = int(np.argmax(jump))
return float(jump[k]), [round(float(x[k]), 3), round(float(y[k]), 3)]
@@ -312,7 +327,9 @@ def inspect_loop(loop_dir: Path, regions: list[tuple[float, ...]], out_dir: Path
moving = [i for i, hold in enumerate(answer.held) if not hold]
for i in moving:
for j in (j for j in moving if j > i):
- found = seam(*(tuple(entries[m]["ellipse"]) for m in (i, j)), offsets[i], offsets[j], answer.gains[i] == answer.gains[j])
+ others = [(tuple(entries[m]["ellipse"]), offsets[m]) for m in moving if m not in (i, j)]
+ found = seam(*(tuple(entries[m]["ellipse"]) for m in (i, j)), offsets[i], offsets[j], answer.gains[i] == answer.gains[j],
+ others)
if found is not None:
seams.append({"regions": [i, j], "jump": round(found[0], PLACES), "deg": _degrees(found[0]), "at": found[1]})
cells.append({