/** * The legs (rig-c/pose#47): foreshortening by length, and the hip derived from * the supporting foot. Pure — no file, no clock, no process. * * **Leg scale.** A knee bending toward the camera is seen from the front only * as a shorter leg. Each mapped leg segment (two of one side's hip, knee and * ankle) is scaled along its own x axis (`scalex`): its picture length on the * frame over its *straight length*, the 95th percentile of its picture length * over the sequence, clamped to a declared range. The frames clamped are listed. * * **A scaled parent turns its child.** rig-c's `scalex` multiplies the bone's * x axis, and a child with the `normal` inherit mode takes the parent's whole * world matrix — so under a thigh scaled by s, a shin at angle φ in the thigh's * axes points at atan2(sin φ, s·cos φ), not φ, and its own length is scaled by * |(s·cos φ, sin φ)|. A child with `noScale` or `noScaleOrReflection` * (rig-c/pose#153) still has its direction carried through the parent's scaled * matrix, so its angle is skewed the same way, while rig-c normalises that * direction to unit length so the parent's scale is not inherited. The rotate * key `turn − turn above` assumes unit scale. A mapped bone below a scaled bone * is therefore keyed through its parent's posed matrix (`posePath`): with * rig-c's semantics, a `rotate` key k and a `scalex` key s' put its world x * axis on the target angle at the asked length (under `noScale`, s' is the * bone's own asked scale s directly). Its art is still sheared by a `normal` * parent's scale; that is measured, not hidden (`shear` in the report). * * **Planted feet.** The hip is no longer the pose's mid_hip travel scaled. On * every frame one foot supports: the one whose reference ankle is lower in the * picture, changing only when the other ankle has been ≥ `PLANT_SWITCH` leg * lengths lower for ≥ `PLANT_SWITCH_SECONDS`, and taking support from the frame * it crossed that margin once confirmed (rig-c/pose#102). The supporting foot is held where * it was on the frame it took support, moved in y onto the one floor every span * shares (rig-c/pose#85, `plantHip`), and the hip is placed so it stays there, * through the posed (scaled) legs. A change of support blends the two * derivations in y over `PLANT_BLEND` frames while taking the new derivation's x * immediately (rig-c/pose#102). A jump — both ankles and mid_hip more * than `JUMP_RISE` leg lengths above their rolling floor (the 95th-percentile * picture y over ± `FLOOR_SECONDS`), for ≥ `JUMP_FRAMES` frames, excluding a * hand-over whose back ankle stays up and holds its height in y (`< STILL_SPEED` * for `STILL_FRAMES` frames) when the front foot lands (a raised heel, * rig-c/pose#101), and starting from the first preceding push-off frame where * both ankles are already above their rolling floor while mid_hip lags * (rig-c/pose#142) — releases the plant: the hip follows the pose's travel from * where it was, and re-plants on landing. A step does too: while the supporting ankle moves more than * `STEP_SPEED` leg lengths a frame its plant is released, the other foot takes * support if it is planted (not released, not moving) and otherwise the hip * follows the pose; the released ankle may support again once it has moved * less than `STILL_SPEED` for `STILL_FRAMES` frames (rig-c/pose#47, round 2). * The floor is rolling because an ankle's picture y drifts with depth: under * one floor for the sequence, HQ's dancer standing still read as jumping. * * **In place** (rig-c/pose#92). A sequence declared `travel: "in-place"` is a * gait that does not travel: its supporting foot moves back in the picture * while the body stays, so holding it turns that slide into travel. Under the * declaration each plant's anchor follows its own ankle's track (`follow`, the * picture x scaled) from the frame it planted, its y still on the one floor; no * step is read, since a foot that follows its track has no plant to drag; and * the hip's x drift from the first frame to the last is spread back over the * frames (`closeLoop`), so the loop carries no net travel. */ import type { Chain, JointMap, Rest } from './map.ts'; import type { ContactKeysReading } from './contacts.ts'; type Pt = [number, number]; /** A world 2×2: columns (a, c) — the x axis — and (b, d) — the y axis. */ export type M2 = [number, number, number, number]; const DEG = 180 / Math.PI; /** A change of support needs the other ankle this many leg lengths lower … */ export const PLANT_SWITCH = 0.02; /** … for at least this long, in seconds (first frame's time to the end of the last, as END-ON times a run). */ export const PLANT_SWITCH_SECONDS = 0.1; /** A change of support (and a landing) blends the hip over this many frames. */ export const PLANT_BLEND = 3; /** Both ankles and mid_hip more than this many leg lengths above their rolling floors: a jump. */ export const JUMP_RISE = 0.05; /** A rolling floor: this percentile of the picture y around the frame. */ export const FLOOR_PERCENTILE = 0.95; /** A segment's straight length: this percentile of its picture length over the sequence. */ export const STRAIGHT_PERCENTILE = 0.95; export type Side = 'r' | 'l'; export const SIDES: readonly Side[] = ['r', 'l']; /** A segment of one side's hip, knee and ankle. */ export function isLegSegment(a: string, b: string): boolean { const side = (x: string) => (x.startsWith('r_') ? 'r' : x.startsWith('l_') ? 'l' : null); if (a === b || side(a) === null || side(a) !== side(b)) return false; const LEG = ['hip', 'knee', 'ankle']; return LEG.includes(a.slice(2)) && LEG.includes(b.slice(2)); } /** Linear-interpolated percentile (numpy's default) of the finite values. */ export function percentile(xs: readonly number[], q: number): number { const s = xs.filter((x) => Number.isFinite(x)).sort((a, b) => a - b); if (s.length === 0) return NaN; const pos = q * (s.length - 1); const lo = Math.floor(pos); const hi = Math.min(s.length - 1, lo + 1); return s[lo]! + (s[hi]! - s[lo]!) * (pos - lo); } export interface Leg { side: Side; chain: Chain; /** The chain's last bone: its x axis ends at the ankle (`:tip`). */ ankleBone: string; } /** * Each side's leg: the (non-reach) chain whose last joint is that side's * ankle. Every refusal names what it found. */ export function legsOf(map: JointMap, why: string): { legs: Record } | { problems: string[] } { const problems: string[] = []; const legs: Partial> = {}; for (const side of SIDES) { const ankle = `${side}_ankle`; const found = map.chains.filter((c) => !c.reach && c.joints[c.joints.length - 1] === ankle); if (found.length !== 1) { problems.push(`${why} needs one chain ending at "${ankle}" (its last bone's x axis ends at the ankle); the map has ${found.length === 0 ? 'none' : found.map((c) => `"${c.name}"`).join(', ')}`); continue; } const chain = found[0]!; const k = chain.bones.length - 1; if (chain.aim[k] !== 'x') problems.push(`${why}: chain "${chain.name}" ends at "${ankle}" with bone "${chain.bones[k]}" aimed ${JSON.stringify(chain.aim[k])}; the ankle is read as that bone's tip, so it must be aimed along its own x axis ("x")`); legs[side] = { side, chain, ankleBone: chain.bones[k]! }; } return problems.length > 0 ? { problems } : { legs: legs as Record }; } // --- 2×2 ------------------------------------------------------------------- const mul = (p: M2, q: M2): M2 => [p[0] * q[0] + p[1] * q[2], p[0] * q[1] + p[1] * q[3], p[2] * q[0] + p[3] * q[2], p[2] * q[1] + p[3] * q[3]]; const inv = (m: M2): M2 => { const det = m[0] * m[3] - m[1] * m[2]; return [m[3] / det, -m[1] / det, -m[2] / det, m[0] / det]; }; const apply = (m: M2, v: Pt): Pt => [m[0] * v[0] + m[1] * v[1], m[2] * v[0] + m[3] * v[1]]; const rot = (deg: number): M2 => { const r = deg / DEG; return [Math.cos(r), -Math.sin(r), Math.sin(r), Math.cos(r)]; }; const scaleX = (s: number): M2 => [s, 0, 0, 1]; export const restMatrix = (rest: Rest, bone: string): M2 => { const b = rest.get(bone)!; return [b.a, b.b, b.c, b.d]; }; /** * How one mapped bone is posed on a frame: its world turn from setup (the * target less the rest angle), its target (the world angle of its aim), whether * its aim is its own x axis, and the scale asked of its x axis (1: none). */ export interface Posing { turn: number; target: number; aimX: boolean; } export interface PathPose { /** Each bone's posed origin, relative to the first bone's origin. */ origin: Map; /** Each bone's posed world 2×2. */ m: Map; /** For a mapped bone under a scaled bone: the rotate key (degrees, wrapped) and the scalex key that land it. */ solved: Map; } /** * World 2×2 of a `noScale` or `noScaleOrReflection` bone under `Wp` at rotate * key `k` (degrees) and scalex key `sk` (rig-c/pose#153), matching rig-c's * `modeMatrix` (`rigc/src/core/world.ts`): the bone's local x direction is * rotated by `k`, carried through `Wp` and normalised to unit length before its * setup local frame (`F0`) and `sk` are applied. */ export function poseNoScale(Wp: M2, Wp0: M2, W0: M2, inherit: 'noScale' | 'noScaleOrReflection', k: number, sk: number): M2 { const L0 = mul(inv(Wp0), W0); const l0: Pt = [L0[0], L0[2]]; const len0 = Math.hypot(W0[0], W0[2]); const u0: Pt = [W0[0] / len0, W0[2] / len0]; const flip0 = inherit === 'noScale' && Wp0[0] * Wp0[3] - Wp0[1] * Wp0[2] < 0 ? -1 : 1; const turned0: M2 = [u0[0], -u0[1] * flip0, u0[1], u0[0] * flip0]; const F0 = mul(inv(turned0), W0); const u = apply(Wp, apply(rot(k), l0)); const invLen = 1 / Math.sqrt(u[0] * u[0] + u[1] * u[1]); const ux = u[0] * invLen; const uy = u[1] * invLen; const flip = inherit === 'noScale' && Wp[0] * Wp[3] - Wp[1] * Wp[2] < 0 ? -1 : 1; const turned: M2 = [ux, -uy * flip, uy, ux * flip]; return mul(mul(turned, F0), scaleX(sk)); } /** * Pose a path of bones, parents first, the first the path's top (its origin is * (0, 0)). A mapped bone with no scaled bone above it turns rigidly from setup * (R(turn) · setup world · diag(scale, 1)); a mapped bone under a scaled bone * is solved through its parent's posed matrix (the header); an unmapped bone * keeps its setup local matrix (or its `noScale` unit frame, #153). Every setup * matrix and origin is rig-c's. */ export function posePath(rest: Rest, path: readonly string[], posing: (b: string) => Posing | null, scaleOf: (b: string) => number | null): PathPose | string { const origin = new Map(); const m = new Map(); const solved = new Map(); const scaledAbove = new Set(); for (const b of path) { const rb = rest.get(b)!; const W0 = restMatrix(rest, b); const pz = posing(b); const s = scaleOf(b) ?? 1; const p = rb.parent; const top = origin.size === 0; if (top || p === null || !m.has(p)) { if (!top) return `bone "${b}" is on the path but its parent "${p}" is not`; origin.set(b, [0, 0]); m.set(b, mul(mul(rot(pz?.turn ?? 0), W0), scaleX(s))); if (scaleOf(b) !== null) scaledAbove.add(b); continue; } const Wp0 = restMatrix(rest, p); const Wp = m.get(p)!; const rp = rest.get(p)!; const local = apply(inv(Wp0), [rb.worldX - rp.worldX, rb.worldY - rp.worldY]); const po = origin.get(p)!; const o = apply(Wp, local); origin.set(b, [po[0] + o[0], po[1] + o[1]]); const L0 = mul(inv(Wp0), W0); const under = scaledAbove.has(p); const noScale = rb.inherit === 'noScale' || rb.inherit === 'noScaleOrReflection' ? rb.inherit : null; if (pz === null) m.set(b, noScale !== null ? poseNoScale(Wp, Wp0, W0, noScale, 0, 1) : mul(Wp, L0)); else if (!under) m.set(b, mul(mul(rot(pz.turn), W0), scaleX(s))); else { if (!pz.aimX) return `bone "${b}" is mapped under a scaled bone and aimed other than along its x axis; only an "x" aim is solved through a scaled parent`; const len0 = Math.hypot(W0[0], W0[2]); const X: Pt = [s * len0 * Math.cos(pz.target / DEG), s * len0 * Math.sin(pz.target / DEG)]; const v = apply(inv(Wp), X); const l0: Pt = [L0[0], L0[2]]; const k = (Math.atan2(v[1], v[0]) - Math.atan2(l0[1], l0[0])) * DEG; // Under noScale / noScaleOrReflection (#153), rig-c normalises the direction carried through Wp to unit length, so the parent's scale is not inherited and the bone's own scalex is s directly. const sk = noScale !== null ? s : Math.hypot(v[0], v[1]) / Math.hypot(l0[0], l0[1]); m.set(b, noScale !== null ? poseNoScale(Wp, Wp0, W0, noScale, k, sk) : mul(mul(mul(Wp, rot(k)), L0), scaleX(sk))); solved.set(b, { rotate: k, scale: sk }); } if (under || scaleOf(b) !== null) scaledAbove.add(b); } return { origin, m, solved }; } /** Where `bone`'s x axis ends on a posed path: its origin plus its world x column times its length. */ export function tipOf(rest: Rest, pose: PathPose, bone: string): Pt { const o = pose.origin.get(bone)!; const w = pose.m.get(bone)!; const len = rest.get(bone)!.length; return [o[0] + w[0] * len, o[1] + w[2] * len]; } /** The bones from `top` down to each of `ends` (inclusive), parents first; null when an end does not descend from top. */ export function pathFrom(rest: Rest, top: string, ends: readonly string[]): string[] | null { const set = new Set([top]); for (const e of ends) { const up: string[] = []; let b: string | null = e; while (b !== null && b !== top) { up.push(b); b = rest.get(b)!.parent; } if (b === null) return null; for (const x of up) set.add(x); } const depth = (b: string): number => { let d = 0; for (let a = rest.get(b)!.parent; a !== null; a = rest.get(a)!.parent) d++; return d; }; return [...set].sort((x, y) => depth(x) - depth(y) || (x < y ? -1 : x > y ? 1 : 0)); } /** The root of `b`'s ancestry. */ export function rootOf(rest: Rest, b: string): string { let x = b; for (let p = rest.get(x)!.parent; p !== null; p = rest.get(x)!.parent) x = p; return x; } // --- leg scale ------------------------------------------------------------- export interface SupportScaleRun { side: Side; frames: [number, number]; /** The lowest and highest scale asked on the run's frames. */ asked: { min: number; max: number }; /** The lowest and highest scale keyed on the run's frames (clamped at `bound` when `asked` went under #95's bound). */ used: { min: number; max: number }; /** Present when `asked` went under #95's bound on any frame of the run: the lowest bound on the run, and the largest hip rise (world units, ≥ 0). */ clamped?: { bound: number; hipRise: number }; } export interface ClampedScaleRun { frames: [number, number]; limit: 'low' | 'high'; /** The lowest and highest scale asked (`picture length ÷ straight`) on the clamped run's frames (#225). */ asked: { min: number; max: number }; } export interface LegScale { chain: string; bone: string; joints: [string, string]; /** The straight length: the larger of the two sides' 95th-percentile picture lengths for this segment (#148), weighted by the bone's setup length share when the rig's legs differ at setup (#225), px. */ straight: number; /** The picture length over the straight length, before the clamp: lowest, median, highest. */ ratio: { min: number; median: number; max: number }; /** The range the scale is clamped to. */ limits: [number, number]; /** Runs of frames clamped at the low and at the high limit. */ low: Array<[number, number]>; high: Array<[number, number]>; /** Runs of frames clamped at the low or high limit, with the asked scale range on each run (#225). */ clamped?: ClampedScaleRun[]; /** The scale keyed on each frame (clamped). */ scale: number[]; /** The scale the pose asked for on each frame before the clamp floor (`min(ratio, limits[1])`, rig-c/pose#107). */ asked: number[]; /** The picture's scale on each frame, before either limit: picture length ÷ straight (rig-c/pose#237). */ picture: number[]; /** * rig-c/pose#237: on the frames this segment was clamped (`scale ≠ picture`) * while its side was free (the other foot supporting, or tracked), what the * clamps on that leg did to the free ankle: its height keyed against its * height with every segment of that side at the picture's scale, the other * leg as keyed on both. Absent with no supports read, or no clamped frame. */ free?: FreeAnkleShift; /** * rig-c/pose#133: under `--plant-feet`, runs of supporting frames on this * segment where the asked scale went under `limits[0]` and followed `asked` * down to the bound #95 derives from the leg's own range (`min(limits[0], r(i))`). * Absent when no supporting frame on this segment asked under `limits[0]`. */ support?: SupportScaleRun[]; } /** One segment's scale per frame: picture length ÷ straight length (the larger of the two sides' 95th percentiles, #148, scaled per leg when setup leg lengths differ, #225), clamped; its asked scale without the clamp floor (#107); and the runs clamped with their asked values (#225). */ export function legScaleOf(lengths: readonly number[], limits: [number, number], straight = percentile(lengths, STRAIGHT_PERCENTILE)): Omit { const ratio = lengths.map((l) => l / straight); const scale = ratio.map((r) => Math.min(Math.max(r, limits[0]), limits[1])); const asked = ratio.map((r) => Math.min(r, limits[1])); const sorted = [...ratio].sort((a, b) => a - b); const mid = sorted.length >> 1; const median = sorted.length % 2 === 1 ? sorted[mid]! : (sorted[mid - 1]! + sorted[mid]!) / 2; const low = runs(ratio.map((r) => r < limits[0])); const high = runs(ratio.map((r) => r > limits[1])); const spanAsked = ([a, b]: [number, number]): { min: number; max: number } => { const sub = ratio.slice(a, b + 1); return { min: Math.min(...sub), max: Math.max(...sub) }; }; const clamped: ClampedScaleRun[] = [ ...low.map((frames) => ({ frames, limit: 'low' as const, asked: spanAsked(frames) })), ...high.map((frames) => ({ frames, limit: 'high' as const, asked: spanAsked(frames) })), ]; return { straight, ratio: { min: sorted[0]!, median, max: sorted[sorted.length - 1]! }, limits, low, high, ...(clamped.length > 0 ? { clamped } : {}), scale, asked, picture: ratio, }; } /** rig-c/pose#237: one mapped leg segment's picture length ÷ its straight length (the one `--leg-scale` scales against), read with or without the flag. */ export interface LegSegmentRatio { chain: string; bone: string; joints: [string, string]; /** px; null where it was not measured (`why`). */ straight: number | null; /** Before any clamp: lowest, median, highest; null where it was not measured. */ ratio: { min: number; median: number; max: number } | null; why?: string; } /** A free ankle's height moved by the leg-scale clamps (rig-c/pose#237): world units, and in that side's rig leg at setup (thigh origin → ankle). */ export interface AnkleShift { units: number; legLengths: number; } /** What the clamps on a free leg did to its ankle's height over a set of frames (rig-c/pose#237). */ export interface FreeAnkleShift { side: Side; /** Runs of the frames counted. */ frames: Array<[number, number]>; /** How many frames. */ count: number; /** The largest height above the picture's, and the largest below it (both ≥ 0); null where no frame was that way. */ above: AnkleShift | null; below: AnkleShift | null; } /** * rig-c/pose#237: per side and frame, the free ankle's world y as keyed less * its world y with that side's leg segments at the picture's scale — on a frame * the side is free (`freeOf`) and at least one of its segments' keyed scale is * not the picture's; null on every other frame. `tipY` poses the legs by a * scale lookup and returns that side's ankle world y; both poses share every * other bone's scale, so the supporting leg cancels. */ export function freeAnkleShiftOf(n: number, legScale: readonly LegScale[], freeOf: (i: number) => Side | null, tipY: (i: number, side: Side, scaleOf: (bone: string) => number | null) => number): Record> { const out: Record> = { r: new Array(n).fill(null), l: new Array(n).fill(null) }; const sideOf = (ls: LegScale): Side => (ls.joints[0].startsWith('r_') ? 'r' : 'l'); const byBone = new Map(legScale.map((ls) => [ls.bone, ls])); for (let i = 0; i < n; i++) { const f = freeOf(i); if (f === null) continue; const own = legScale.filter((ls) => sideOf(ls) === f); if (!own.some((ls) => ls.scale[i] !== ls.picture[i])) continue; const keyed = (b: string): number | null => byBone.get(b)?.scale[i] ?? null; const picture = (b: string): number | null => { const ls = byBone.get(b); if (ls === undefined) return null; return sideOf(ls) === f ? ls.picture[i]! : ls.scale[i]!; }; out[f][i] = tipY(i, f, keyed) - tipY(i, f, picture); } return out; } /** The frames of `frames` with a shift on `side`, as a `FreeAnkleShift` in that side's rig leg `leg` (world units); null when none has one. */ export function shiftOver(side: Side, frames: readonly number[], shift: Record>, leg: number): FreeAnkleShift | null { const hit = frames.filter((i) => shift[side][i] !== null && shift[side][i] !== undefined); if (hit.length === 0) return null; const vs = hit.map((i) => shift[side][i]!); const up = Math.max(0, ...vs); const down = Math.max(0, ...vs.map((v) => -v)); const of = (u: number): AnkleShift => ({ units: u, legLengths: u / leg }); return { side, frames: runsOf(hit), count: hit.length, above: vs.some((v) => v > 0) ? of(up) : null, below: vs.some((v) => v < 0) ? of(down) : null }; } /** Consecutive runs of a sorted list of frames. */ function runsOf(xs: readonly number[]): Array<[number, number]> { const out: Array<[number, number]> = []; for (const x of xs) { const last = out[out.length - 1]; if (last !== undefined && last[1] === x - 1) last[1] = x; else out.push([x, x]); } return out; } function runs(flags: readonly boolean[]): Array<[number, number]> { const out: Array<[number, number]> = []; for (let i = 0; i < flags.length; i++) { if (!flags[i]) continue; let j = i; while (j + 1 < flags.length && flags[j + 1]) j++; out.push([i, j]); i = j; } return out; } // --- supports, jumps and steps --------------------------------------------- /** The points a jump reads, each above its rolling floor by more than `JUMP_RISE` leg lengths. */ export const JUMP_POINTS = ['r_ankle', 'l_ankle', 'mid_hip'] as const; /** A jump lasts at least this many frames. */ export const JUMP_FRAMES = 2; /** A rolling floor: `FLOOR_PERCENTILE` of the picture y over ± this many seconds around the frame. */ export const FLOOR_SECONDS = 1.0; /** A supporting ankle moving more than this many leg lengths a frame is stepping: its plant is released. */ export const STEP_SPEED = 0.03; /** A released ankle re-plants once it moves less than this many leg lengths a frame … */ export const STILL_SPEED = 0.01; /** … for this many frames. */ export const STILL_FRAMES = 2; export interface Step { side: Side; /** First frame the ankle moved past `STEP_SPEED`, and the last frame before it re-planted (or the last frame). */ frames: [number, number]; /** The frame it re-planted on (still for `STILL_FRAMES`), or null if it never did. */ replant: number | null; /** The side that took support when it released, or null: the hip followed the pose's travel. */ to: Side | null; } export interface Feet { /** Each side's median picture length hip → ankle, px; the unit of every threshold below (mid_hip: their mean). */ legLength: Record; /** Per frame: the side that supports, or null (a jump, or a step with no other foot planted). */ support: Array; /** Runs of one supporting side: [first, last] frames and the side. */ supports: Array<{ side: Side; frames: [number, number] }>; /** Runs of jump frames. */ jumps: Array<[number, number]>; /** Every release of a supporting foot by a step. */ steps: Step[]; /** The frames a change of support (or a landing, or a re-plant) was keyed on: the blend starts there. */ changes: Array<{ frame: number; from: Side | null; to: Side }>; /** * rig-c/pose#113, read from the lower ankle: each run of frames whose * supporting side differs from the reading before #113 (`was`, `now`; null: * no foot is planted). Absent where the rule does not apply (in place, or * without `lowerAnkle`). */ lowered?: Lowered[]; /** * rig-c/pose#119: runs of frames after a jump or a release where no foot is * planted because the only foot free under #113 is the higher one, and the * lower foot supports as a tracked foot (its x follows its own scaled ankle * track, its y sits on #85's floor) until a foot plants. Absent where the * rule does not apply. */ tracked?: Array<{ side: Side; frames: [number, number] }>; /** * rig-c/pose#280: the second support signal (the ankle's vertical speed), how * often it contradicts ankle height, and which one decided each frame. Set by * `supportsOf` on every reading. */ signal?: SupportSignal; /** * rig-c/pose#303: what `--contact-keys` did — the keys, the frames they cover, * the support without them and the runs they changed. Set by `contactKeysOf`, * applied last; absent without the flag. */ contactKeys?: ContactKeysReading; } /** A run of frames whose support the lower-ankle rule (rig-c/pose#113) reads differently from the reading before it. */ export interface Lowered { frames: [number, number]; was: Side | null; now: Side | null; /** * Why no foot supports, where `now` is null: `released` — the supporting foot * was released on the run's first frame because the other ankle had been lower * past the margin for `PLANT_SWITCH_SECONDS` and could not take support * (released by a step, or moving); `higher` — the only foot free to take * support was the higher one. */ cause?: 'released' | 'higher'; } /** Each frame's rolling floor of a picture-y series: its `FLOOR_PERCENTILE` over the frames within ± `FLOOR_SECONDS`. */ export function rollingFloor(ys: readonly number[], times: readonly number[]): number[] { return ys.map((_, i) => { const w: number[] = []; for (let j = 0; j < ys.length; j++) if (Math.abs(times[j]! - times[i]!) <= FLOOR_SECONDS + 1e-9) w.push(ys[j]!); return percentile(w, FLOOR_PERCENTILE); }); } /** * Which foot supports on each frame, from the reference ankles and hips (picture * px, y down) — the header's rules. `times` are the key times; `secondsOf` * times a run of frames as the rest of the retarget does. `inPlace` (#92): no * ankle is moving for the step rules, so nothing is released and every foot is * free to take support. * * `lowerAnkle` (rig-c/pose#113, `--plant-feet` on a sequence not declared in * place): a foot whose ankle is higher in the picture than the other's by * `PLANT_SWITCH` leg lengths or more never takes support — not when the plant * starts, not when a step releases the other foot. A supporting foot whose * other ankle has been that much lower for `PLANT_SWITCH_SECONDS` hands support * to it when it is free (as before) and is otherwise released (the hip follows * the pose until a foot may take support). Steps and jumps are read as before. * The frames whose support this changes are `lowered`. On a frame where no foot * is planted because the only foot free is the higher one (rig-c/pose#119), the * lower foot is tracked (`tracked`). * * rig-c/pose#280: the ankle's vertical speed is read beside ankle height on * every clip (`speedSignalOf`), and on a frontal view it decides the support * (`frontalOf`); `signal` says which signal decided each frame. */ export function supportsOf(ankle: Record, hip: Record, times: readonly number[], secondsOf: (run: [number, number]) => number, inPlace = false, lowerAnkle = false): Feet { return frontalOf(ankle, hip, heightSupportsOf(ankle, hip, times, secondsOf, inPlace, lowerAnkle)); } /** The support read from ankle height alone: the rules above, #113's included (the whole reading before rig-c/pose#280). */ function heightSupportsOf(ankle: Record, hip: Record, times: readonly number[], secondsOf: (run: [number, number]) => number, inPlace: boolean, lowerAnkle: boolean): Feet { if (inPlace || !lowerAnkle) return readSupports(ankle, hip, times, secondsOf, inPlace, false).feet; const { feet, released } = readSupports(ankle, hip, times, secondsOf, false, true); const before = readSupports(ankle, hip, times, secondsOf, false, false).feet.support; const lowered: Lowered[] = []; feet.support.forEach((now, i) => { const was = before[i] ?? null; if (was === now) return; const last = lowered[lowered.length - 1]; if (last !== undefined && last.frames[1] === i - 1 && last.was === was && last.now === now) last.frames[1] = i; else lowered.push({ frames: [i, i], was, now, ...(now === null ? { cause: released.includes(i) ? ('released' as const) : ('higher' as const) } : {}) }); }); return { ...feet, lowered }; } /** The reader; `released` (#113): the frames a supporting foot was released because the lower foot could not take support. */ function readSupports(ankle: Record, hip: Record, times: readonly number[], secondsOf: (run: [number, number]) => number, inPlace: boolean, lowerAnkle: boolean): { feet: Feet; released: number[] } { const n = ankle.r.length; const legLength = {} as Record; for (const s of SIDES) legLength[s] = percentile(ankle[s].map((a, i) => Math.hypot(a[0] - hip[s][i]![0], a[1] - hip[s][i]![1])), 0.5); const mean = (legLength.r + legLength.l) / 2; // A jump: both ankles and mid_hip above their rolling floors, for ≥ JUMP_FRAMES frames. // #101: at a hand-over the front foot has not landed yet while the back ankle lifts its heel; when // the front foot lands (the first frame after its lifted run) while the back ankle stays up, a back // ankle that holds its height in y (< STILL_SPEED a frame for STILL_FRAMES frames) is a raised heel. // #142: a run's push-off lifts the ankles off the floor while mid_hip is already rising from its // crouch bottom; a jump starts at the first immediately preceding frame where both ankles are above // their rolling floors and mid_hip is rising, so the support span ends at its ankle's push-off. const midY = hip.r.map((h, i) => (h[1] + hip.l[i]![1]) / 2); const rises: Array<{ ys: number[]; unit: number }> = [ { ys: ankle.r.map((a) => a[1]), unit: legLength.r }, { ys: ankle.l.map((a) => a[1]), unit: legLength.l }, { ys: midY, unit: mean }, ]; const up = rises.map(({ ys, unit }) => { const floor = rollingFloor(ys, times); return ys.map((y, i) => floor[i]! - y > JUMP_RISE * unit); }); const ankleRuns = { r: runs(up[0]!), l: runs(up[1]!) }; const stillY = (s: Side, from: number): boolean => { if (from + STILL_FRAMES > n) return false; for (let k = from; k < from + STILL_FRAMES; k++) { if (!up[s === 'r' ? 0 : 1]![k] || Math.abs(ankle[s][k]![1] - ankle[s][k - 1]![1]) / legLength[s] >= STILL_SPEED) return false; } return true; }; const raisedHeel = (a: number, b: number): boolean => { const rSpan = ankleRuns.r.find(([s, e]) => s <= a && b <= e)!; const lSpan = ankleRuns.l.find(([s, e]) => s <= a && b <= e)!; if (rSpan[1] === lSpan[1]) return false; const [fSpan, back]: [[number, number], Side] = rSpan[1] < lSpan[1] ? [rSpan, 'l'] : [lSpan, 'r']; return stillY(back, fSpan[1] + 1); }; const lifted = Array.from({ length: n }, (_, i) => up.every((u) => u[i])); const jumps = runs(lifted).filter(([a, b]) => b - a + 1 >= JUMP_FRAMES && !raisedHeel(a, b)); for (const j of jumps) while (j[0] > 1 && up[0]![j[0] - 1] && up[1]![j[0] - 1] && midY[j[0] - 1]! < midY[j[0] - 2]!) j[0]--; const jump = new Array(n).fill(false); for (const [a, b] of jumps) for (let i = a; i <= b; i++) jump[i] = true; // Ankle speeds, leg lengths a frame (0 on the first frame). const speed = {} as Record; for (const s of SIDES) speed[s] = ankle[s].map((a, i) => (i === 0 ? 0 : Math.hypot(a[0] - ankle[s][i - 1]![0], a[1] - ankle[s][i - 1]![1]) / legLength[s])); // In place (rig-c/pose#92) the supporting foot follows its own track instead of being held, so a // moving ankle has no plant to drag and nothing is released: no step, and every foot is free. const moving = (s: Side, i: number) => !inPlace && speed[s][i]! > STEP_SPEED; const still = (s: Side, i: number) => { for (let k = i - STILL_FRAMES + 1; k <= i; k++) if (k >= 0 && !(speed[s][k]! < STILL_SPEED)) return false; return true; }; const support: Array = new Array(n).fill(null); const trkAt: Array = new Array(n).fill(null); const changes: Feet['changes'] = []; const steps: Step[] = []; const open: Partial> = {}; let cur: Side | null = null; let lowerFrom = -1; let freeFrom = -1; const released: number[] = []; const lower = (cands: Side[], i: number): Side => (cands.length === 2 ? (ankle.l[i]![1] > ankle.r[i]![1] ? 'l' : 'r') : cands[0]!); for (let i = 0; i < n; i++) { // A released ankle re-plants once it has been still for STILL_FRAMES frames. for (const s of SIDES) { const st = open[s]; if (st !== undefined && still(s, i)) { st.replant = i; st.frames[1] = i - 1; delete open[s]; } } if (jump[i]) { cur = null; lowerFrom = -1; freeFrom = -1; continue; } const free = (s: Side) => open[s] === undefined && !moving(s, i); // #113: a foot higher than the other by PLANT_SWITCH leg lengths or more does not take support. const higher = (s: Side) => ankle[s === 'r' ? 'l' : 'r'][i]![1] - ankle[s][i]![1] >= PLANT_SWITCH * mean; const may = (s: Side) => free(s) && !(lowerAnkle && higher(s)); if (cur !== null && moving(cur, i)) { const other: Side = cur === 'r' ? 'l' : 'r'; const to: Side | null = may(other) ? other : null; const st: Step = { side: cur, frames: [i, n - 1], replant: null, to }; steps.push(st); open[cur] = st; if (to !== null) changes.push({ frame: i, from: cur, to }); cur = to; lowerFrom = -1; freeFrom = -1; } else if (cur === null) { const cands = SIDES.filter(may); if (cands.length > 0) { cur = lower(cands, i); changes.push({ frame: i, from: null, to: cur }); lowerFrom = -1; freeFrom = -1; } } else { const other: Side = cur === 'r' ? 'l' : 'r'; if ((free(other) || lowerAnkle) && higher(cur)) { if (lowerFrom < 0) lowerFrom = i; if (free(other)) { if (freeFrom < 0) freeFrom = i; // #102: once confirmed over PLANT_SWITCH_SECONDS, the incoming foot takes support from the landing frame it crossed the margin on. if (secondsOf([freeFrom, i]) >= PLANT_SWITCH_SECONDS - 1e-9) { for (let k = freeFrom; k < i; k++) support[k] = other; changes.push({ frame: freeFrom, from: cur, to: other }); cur = other; lowerFrom = -1; freeFrom = -1; } } else { freeFrom = -1; if (secondsOf([lowerFrom, i]) >= PLANT_SWITCH_SECONDS - 1e-9) { cur = null; // #113: the lower foot may not take support (released, or moving); neither foot supports released.push(i); lowerFrom = -1; } } } else { lowerFrom = -1; freeFrom = -1; } } if (lowerAnkle && cur === null && released[released.length - 1] !== i && SIDES.some((x) => free(x) && higher(x))) { trkAt[i] = lower(['r', 'l'], i); } support[i] = cur; } const supports: Feet['supports'] = []; for (let i = 0; i < n; i++) { const s = support[i]; if (s === null || s === undefined) continue; const last = supports[supports.length - 1]; if (last !== undefined && last.side === s && last.frames[1] === i - 1) last.frames[1] = i; else supports.push({ side: s, frames: [i, i] }); } const tracked: Array<{ side: Side; frames: [number, number] }> = []; for (let i = 0; i < n; i++) { const s = trkAt[i]; if (s === null || s === undefined) continue; const last = tracked[tracked.length - 1]; if (last !== undefined && last.side === s && last.frames[1] === i - 1) last.frames[1] = i; else tracked.push({ side: s, frames: [i, i] }); } return { feet: { legLength, support, supports, jumps, steps, changes, ...(tracked.length > 0 ? { tracked } : {}) }, released }; } // --- the second signal: the ankle's vertical speed against the ground (rig-c/pose#280) --- /** A foot's contact on one frame, read from the ankles' vertical speed: one side, both, or neither (null). */ export type Contact = Side | 'both' | null; export interface SpeedSignal { /** Each side's ground speed: the median of its ankle's vertical step (picture px a frame, y down) over the frames outside every jump. */ ground: Record; /** Per frame: the feet that move with the ground, on the step into the frame or the step out of it, and are not `DEPTH_BAND` or more above the other (null on a jump frame or where neither does). */ contact: Contact[]; /** The frames the two signals contradict: the lower ankle (by ≥ `PLANT_SWITCH` leg lengths) leaves the ground's speed while the higher moves with it. */ contradict: number[]; /** The frames both signals read: outside every jump. */ read: number; } /** * The ankle's vertical speed as a support signal (rig-c/pose#280). A planted * foot moves with the ground: still under a fixed camera, or sliding at the * ground's speed in a gait drawn in place. Each side's ground speed is the * median of its ankle's vertical step over the frames outside every jump — * a walking foot is down on more than half of them — and a foot moves with it * on a step that departs from it by less than `STILL_SPEED` of that side's leg * length. A frame's foot is down when either the step into the frame or the * step out of it moves with the ground, so a drawing a foot lands or lifts on * reads it down — unless it is `DEPTH_BAND` or more above the other ankle, * more than depth draws between two feet on the floor (rig-c/pose#369). * `contradict` lists the frames where this and ankle height disagree in the * way a frontal view's depth makes them: the lower ankle is the moving one, the * higher the one moving with the ground. */ export function speedSignalOf(ankle: Record, feet: Pick): SpeedSignal { const n = ankle.r.length; const jump = new Array(n).fill(false); for (const [a, b] of feet.jumps) for (let i = a; i <= b; i++) jump[i] = true; const step = (s: Side, i: number): number | null => (i < 1 || i >= n || jump[i] || jump[i - 1] ? null : ankle[s][i]![1] - ankle[s][i - 1]![1]); const ground = {} as Record; for (const s of SIDES) { const xs: number[] = []; for (let i = 1; i < n; i++) { const v = step(s, i); if (v !== null) xs.push(v); } ground[s] = xs.length === 0 ? 0 : percentile(xs, 0.5); } const withGround = (s: Side, i: number): boolean => { const v = step(s, i); return v !== null && Math.abs(v - ground[s]) < STILL_SPEED * feet.legLength[s]; }; const mean = (feet.legLength.r + feet.legLength.l) / 2; // rig-c/pose#369: a foot higher than the other by DEPTH_BAND leg lengths or more is off the floor, whatever its speed. const aloft = (s: Side, i: number) => ankle[s === 'r' ? 'l' : 'r'][i]![1] - ankle[s][i]![1] >= DEPTH_BAND * mean; const down = (s: Side, i: number) => (withGround(s, i) || withGround(s, i + 1)) && !aloft(s, i); const contact: Contact[] = []; const contradict: number[] = []; let read = 0; for (let i = 0; i < n; i++) { if (jump[i]) { contact.push(null); continue; } read++; const r = down('r', i); const l = down('l', i); contact.push(r && l ? 'both' : r ? 'r' : l ? 'l' : null); const gap = ankle.l[i]![1] - ankle.r[i]![1]; const lower: Side | null = Math.abs(gap) < PLANT_SWITCH * mean ? null : gap > 0 ? 'l' : 'r'; if (lower !== null && !down(lower, i) && down(lower === 'r' ? 'l' : 'r', i)) contradict.push(i); } return { ground, contact, contradict, read }; } /** * rig-c/pose#369: the most height a frontal view draws between two feet on the * floor, in leg lengths (the clip's mean). Seen from the front a foot's depth is * drawn as height, so a planted foot behind may sit above one in front — by a * step's depth foreshortened by the camera's elevation: 0.14 on the tests' * synthetic frontal walk (#280), where that is the gap on every frame both feet * are down. A foot higher than the other by this much or more is in the air and * the vertical speed does not read it down. Near the top of a run's swing the * lifted foot is nearly still while the landed foot still settles, so on a run's * short stances it read as moving with the ground and was planted (0.36 above * on the tests' frontal run carried on a floor, 0.72 on a frontal run * reference); ankle height reads those frames. */ export const DEPTH_BAND = 0.25; /** * A clip whose two support signals contradict on more than this share of the * frames both read is read as a frontal view (rig-c/pose#280), when its stride * also shows in depth (`strideInDepth`). A frontal walk contradicts at every * hand-over, for as long as the nearer foot is in the air below the planted one: * 18 of 96 frames (18.8 %) on the tests' synthetic frontal walk, where ankle * height alone hands support to the nearer foot 1 or 2 frames before it lands * (16 frames in all, declared in place). Every clip the other tests read with * its stride in depth contradicts on none; the shortest contradiction a walk * can make, one frame a step, is 8.3 % of a 1 s cycle at 24 fps (2 of 24). */ export const FRONTAL_DISAGREE = 0.05; /** rig-c/pose#280: the two support signals of a clip, and which one decided each frame's support. */ export interface SupportSignal { /** The frames the signals contradict (`speedSignalOf`), of `read` frames both read. */ contradict: number[]; read: number; /** Each side's ankle range in the picture, relative to its hip (5th to 95th percentile), px: the stride shows in y on a frontal view and in x on a side view. */ range: Record; /** The stride shows in depth (y range over x range on both sides), the signals contradict on more than `FRONTAL_DISAGREE` of the frames, and the ground's motion is resolved (`groundResolved`): the support comes from the vertical speed. */ frontal: boolean; /** Each side's ground speed (`speedSignalOf`): the median vertical step of its ankle, picture px a frame, y down. */ ground: Record; /** The hips' ground speed: the median vertical step of mid_hip outside every jump, picture px a frame, y down (the body approaching a fixed camera, rig-c/pose#379). */ hipStep: number; /** * rig-c/pose#379: the ground moves in the picture by at least `STILL_SPEED` a * frame — each side's planted feet slide that fast (a gait in place), or the * hips do (a gait travelling toward a fixed camera). Only then can the vertical * speed tell a still ankle (a heel held on its toe, a swing foot at its apex * or hovering before it lands) from one moving with the ground, and only then * does it decide. False: the clip is read by ankle height whatever the * contradictions, which the `support:` line then says. */ groundResolved: boolean; /** Per frame, the vertical speed's reading: the side down, both, or null (a jump, or neither foot moving with the ground). */ contact: Contact[]; /** Per frame, ankle height's reading (the support before #280). */ height: Array; /** Per frame, the signal that decided the support: `speed`, `height`, or null where no foot supports. */ by: Array<'speed' | 'height' | null>; } /** Each side's ankle range relative to its hip, px, x and y (5th to 95th percentile). */ function rangeOf(ankle: Record, hip: Record): Record { const span = (xs: number[]) => percentile(xs, 0.95) - percentile(xs, 0.05); const out = {} as Record; for (const s of SIDES) out[s] = { x: span(ankle[s].map((a, i) => a[0] - hip[s][i]![0])), y: span(ankle[s].map((a, i) => a[1] - hip[s][i]![1])) }; return out; } /** rig-c/pose#379: the hips' ground speed — the median vertical step of mid_hip over the steps outside every jump, picture px a frame (0 with none). */ function hipStepOf(hip: Record, jumps: readonly (readonly [number, number])[]): number { const n = hip.r.length; const jump = new Array(n).fill(false); for (const [a, b] of jumps) for (let i = a; i <= b; i++) jump[i] = true; const steps: number[] = []; for (let i = 1; i < n; i++) { if (jump[i] || jump[i - 1]) continue; steps.push((hip.r[i]![1] + hip.l[i]![1]) / 2 - (hip.r[i - 1]![1] + hip.l[i - 1]![1]) / 2); } return steps.length === 0 ? 0 : percentile(steps, 0.5); } /** * rig-c/pose#280: the support from the ankle's vertical speed, on a frontal * view. Seen from the front, a foot's depth is drawn as height, so the lower * ankle is the nearer foot, which may be the one in the air. A clip is read as * frontal when both hold: its stride shows in depth — on each side the ankle's * range relative to its hip is larger in y than in x (a side view's stride is * in x) — and the two signals contradict on more than `FRONTAL_DISAGREE` of the * frames both read. Then each frame's support is the vertical speed's: the one * foot down; on frames both are down, the foot that landed (the other side from * the support before the run, or, at the clip's start, the side that stays down * after it), so support passes on the frame the incoming foot lands (as #102 * takes it from the landing frame); and ankle height's reading where neither * foot moves with the ground. A plant ends where the other foot lands, so no * step releases one and none is tracked (#113's `lowered` and #119's `tracked` * are not read). Otherwise the feet are ankle height's, unchanged. * * rig-c/pose#379: a third condition, the ground's motion. The speed reads a * foot down on a step within `STILL_SPEED` of its ground speed, and a still * ankle steps by 0 — so where the ground itself moves by less than * `STILL_SPEED` a frame, "with the ground" and "still" are one reading, and the * signal plants every still ankle: the outgoing heel held on its toe while the * other foot lands, the swing foot paused at its apex or hovering before it * lands. On a level-camera walk read that way, support fragmented from one span * a step to three, and the landing foot was drawn through the floor. Depth drawn * as height is exactly a ground that moves: a foot on the floor slides in y as * it goes back (a gait in place), or the hips come down the picture as the body * approaches (a gait travelling toward a fixed camera). So the speed decides * only when each side's ground speed, or the hips' median vertical step, is at * least `STILL_SPEED` of the leg — the same bound that defines its reading, not * a new one. The tests' frontal walk slides at 0.0121 and 0.0167 leg lengths a * frame, and read travelling its hips come down at 0.0144; the level-camera walk * the tests draw with a held heel and a held apex at 0 (`test/ground.test.ts`). * A frontal clip whose ground moves less than that is read by ankle height, and * the `support:` line says so. */ function frontalOf(ankle: Record, hip: Record, feet: Feet): Feet { const sig = speedSignalOf(ankle, feet); const range = rangeOf(ankle, hip); const strideInDepth = SIDES.every((s) => range[s].y > range[s].x); const mean = (feet.legLength.r + feet.legLength.l) / 2; const hipStep = hipStepOf(hip, feet.jumps); const feetResolved = SIDES.every((s) => !(Math.abs(sig.ground[s]) < STILL_SPEED * feet.legLength[s])); const hipResolved = !(Math.abs(hipStep) < STILL_SPEED * mean); const groundResolved = feetResolved || hipResolved; const frontal = strideInDepth && sig.read > 0 && sig.contradict.length > FRONTAL_DISAGREE * sig.read; // rig-c/pose#379: the speed decides only where the ground's motion is resolved (header). const decides = frontal && groundResolved; const height = feet.support.map((s) => s ?? null); const signal = (by: SupportSignal['by']): SupportSignal => ({ contradict: sig.contradict, read: sig.read, range, frontal: decides, ground: sig.ground, hipStep, groundResolved, contact: sig.contact, height, by }); if (!decides) return { ...feet, signal: signal(height.map((s) => (s === null ? null : 'height'))) }; const n = height.length; const other = (s: Side): Side => (s === 'r' ? 'l' : 'r'); const support: Array = new Array(n).fill(null); const by: SupportSignal['by'] = new Array<'speed' | 'height' | null>(n).fill(null); const fromHeight = (i: number) => { support[i] = height[i]!; by[i] = height[i] === null ? null : 'height'; }; for (let i = 0; i < n; i++) { const c = sig.contact[i] ?? null; if (c === 'r' || c === 'l') { support[i] = c; by[i] = 'speed'; } else if (c === 'both') { let b = i; while (b + 1 < n && sig.contact[b + 1] === 'both') b++; const before = i > 0 ? (support[i - 1] ?? null) : null; const after = b + 1 < n ? (sig.contact[b + 1] ?? null) : null; const landed: Side | null = before !== null ? other(before) : after === 'r' || after === 'l' ? after : null; for (let k = i; k <= b; k++) { if (landed === null) fromHeight(k); else { support[k] = landed; by[k] = 'speed'; } } i = b; } else fromHeight(i); } const supports: Feet['supports'] = []; const changes: Feet['changes'] = []; for (let i = 0; i < n; i++) { const s = support[i]; if (s === null || s === undefined) continue; const prev = i > 0 ? (support[i - 1] ?? null) : null; if (prev !== s) changes.push({ frame: i, from: prev, to: s }); const last = supports[supports.length - 1]; if (last !== undefined && last.side === s && last.frames[1] === i - 1) last.frames[1] = i; else supports.push({ side: s, frames: [i, i] }); } return { legLength: feet.legLength, support, supports, jumps: feet.jumps, steps: [], changes, signal: signal(by) }; } /** One support span's shift onto the floor (rig-c/pose#85): its side, its frames, and floor − its planted height, world units. */ export interface FloorShift { side: Side; frames: [number, number]; shift: number; } /** Where the planted feet were put (rig-c/pose#85): the floor's world y, whether `--floor` gave it, and each support span's shift. */ export interface Floor { y: number; given: boolean; shifts: FloorShift[]; } let trackFn: ((i: number, s: Side) => number) | undefined; /** rig-c/pose#285: `plantHip`'s `held`, read by `placeHip` as `trackFn` is. */ let heldFn: ((i: number, s: Side, at: number) => Pt) | undefined; /** * The hip's world origin on every frame, from the supporting foot (header), * with every support span on one floor (rig-c/pose#85). `offset(i, side)` is * the ankle's posed position relative to the hip's origin on frame i; * `travel(i)` the pose's own travel of the hip from frame 0, world. `start` is * the hip's origin on frame 0 (its setup origin). * * **One floor.** A span's planted height is the world y its supporting ankle is * held at when it is held where the picture puts it on the frame it takes * support. A 2D rig has one floor, so every span is planted on it instead: the * floor is `floor` when given (`--floor`, world units), and otherwise the median * of the spans' planted heights; each span's anchor moves in y by floor − its * planted height, and the hip with it, so everything above follows. A change of * support still blends the two derivations' y over `PLANT_BLEND` frames, now * between two spans on the floor, while holding the planted foot's x from the * frame it lands on (#102); a jump follows the pose's travel from the hip * it left (on the floor), carried so the landing frame meets the floor and no * frame inside the flight puts a foot below it (#103). On a blend frame the hip is * between the two derivations, so the supporting ankle is off its anchor: * `blends` lists those frames with the anchor's y, and `holdBlend` stretches * the supporting leg to keep it there. Null `floor` in the result: there is no * support span. * * `follow(i, side)` (#92, an in-place sequence): the supporting ankle's track, * world x. A plant's anchor x then moves by follow(i) − follow(the frame it * planted), and the loop is closed (`closeLoop`, `closure` in the result; null * without `follow`). `track(i, side)` (#119): the same world-x ankle track for * `feet.tracked` frames on a sequence not declared in place. * * `held(i, side, at)` (rig-c/pose#285, `--leg-scale`): the side's ankle on * frame i posed as `offset` poses it, but with the leg scales of frame `at`. * A plant hands over on the frame the outgoing foot first reads higher than the * incoming one, so on that frame and the blend after it the outgoing foot has * left the floor, and the picture already draws its leg shortened by the lift; * the outgoing plant then holds that ankle with the leg as it was on its last * supporting frame, so the lift is not read as the floor dropping under the * incoming foot. Absent, `offset` everywhere. * * In place (`follow`, #285) the spans' planted heights are a loop's: their drift * over the cycles is closed (`closeHeights`) before the floor is read from them. */ export function plantHip(feet: Feet, offset: (i: number, s: Side) => Pt, travel: (i: number) => Pt, start: Pt, floor?: number, follow?: (i: number, s: Side) => number, track?: (i: number, s: Side) => number, held?: (i: number, s: Side, at: number) => Pt): { hip: Pt[]; floor: Floor | null; blends: BlendFrame[]; closure: Closure | null } { trackFn = track; heldFn = held; try { const picture = placeHip(feet, offset, travel, start, null, follow); const read = feet.supports.map((sp) => picture.anchors.get(sp.frames[0])); if (read.length === 0) return { hip: picture.hip, floor: null, blends: picture.blends, closure: null }; if (read.some((h) => h === undefined)) throw new Error('plantHip: a support span starts on a frame no plant started on'); const heights = follow === undefined ? (read as number[]) : closeHeights(read as number[], feet.supports); const y = floor ?? percentile(heights, 0.5); const shifts = feet.supports.map((sp, k) => ({ side: sp.side, frames: [sp.frames[0], sp.frames[1]] as [number, number], shift: y - heights[k]! })); const placed = placeHip(feet, offset, travel, start, y, follow); const closure = follow === undefined ? null : closeLoop(placed.hip, feet.supports); return { hip: placed.hip, floor: { y, given: floor !== undefined, shifts }, blends: placed.blends, closure }; } finally { trackFn = undefined; heldFn = undefined; } } /** In place (rig-c/pose#92): the hip's x travel from the first frame to the last before the loop was closed, world units, and the frames it was spread over. */ export interface Closure { drift: number; frames: number; } /** * Close an in-place loop (rig-c/pose#92, #224): when support spans repeat across * multiple cycles, measure the per-cycle x drift between matching phase frames * (`cf = 0` of cycle 1 vs cycle `k + 1`) and subtract that linear trend so the * within-cycle offset (`cf 0 → last frame of cycle`) is preserved; on a single * cycle (or when no repeating support spans are given), subtract the whole-clip * displacement `hip[n - 1].x - hip[0].x`. The y is the floor's (#85) and is not * touched. In place. */ export function closeLoop(hip: Pt[], supports?: Feet['supports']): Closure { const n = hip.length; if (n <= 1) return { drift: 0, frames: n }; let perFrame = (hip[n - 1]![0] - hip[0]![0]) / (n - 1); if (supports !== undefined && supports.length > 0) { const bySide: Record = { r: [], l: [] }; for (const sp of supports) { if (sp.frames[0] > 0) bySide[sp.side].push(sp.frames[0]); } const starts = bySide.r.length >= bySide.l.length ? bySide.r : bySide.l; if (starts.length >= 2) { const period = Math.round((starts[starts.length - 1]! - starts[0]!) / (starts.length - 1)); const cycles = period > 0 ? Math.floor((n - 1) / period) : 0; if (cycles >= 1 && cycles * period < n) { const span = cycles * period; perFrame = (hip[span]![0] - hip[0]![0]) / span; } } } const drift = perFrame * (n - 1); for (let i = 0; i < n; i++) hip[i]![0] -= perFrame * i; return { drift, frames: n }; } /** * The planted heights of an in-place loop's support spans, their drift over the * cycles closed (rig-c/pose#285). A span's planted height is chained from the * span before it through the hand-over frame, and support passes only to a foot * already lower than the other, so every hand-over steps the chain the same way: * over identical cycles the heights descend as a staircase while the picture's * floor stays where it is. As `closeLoop` closes the hip's x, the drift between * matching phases — the first and the last span of the side with more spans not * starting on frame 0, a whole number of cycles apart — is taken out linearly by * each span's first frame, so span k and the span a cycle later have one height * and what stays is the within-cycle difference between the two feet. Fewer than * two such spans: nothing to close, the heights as read. */ export function closeHeights(heights: readonly number[], supports: Feet['supports']): number[] { const bySide: Record = { r: [], l: [] }; supports.forEach((sp, k) => { if (sp.frames[0] > 0) bySide[sp.side].push(k); }); const ks = bySide.r.length >= bySide.l.length ? bySide.r : bySide.l; if (ks.length < 2) return [...heights]; const a = ks[0]!; const b = ks[ks.length - 1]!; const perFrame = (heights[b]! - heights[a]!) / (supports[b]!.frames[0] - supports[a]!.frames[0]); return heights.map((h, k) => h - perFrame * supports[k]!.frames[0]); } /** A frame inside the blend after a change of support (or a landing): the side now supporting, and the world y its plant holds the ankle at. */ export interface BlendFrame { frame: number; side: Side; y: number; } /** * The hip placed from the supporting foot, each plant's anchor held where the * picture puts it (`floorY` null) or at world y `floorY`; and each plant's * picture anchor y, by the frame its support span starts on. */ function placeHip(feet: Feet, offset: (i: number, s: Side) => Pt, travel: (i: number) => Pt, start: Pt, floorY: number | null, follow?: (i: number, s: Side) => number): { hip: Pt[]; anchors: Map; blends: BlendFrame[] } { const n = feet.support.length; const hip: Pt[] = []; const anchors = new Map(); const blends: BlendFrame[] = []; const tracked = new Array(n).fill(null); for (const t of feet.tracked ?? []) for (let i = t.frames[0]; i <= t.frames[1]; i++) tracked[i] = t.side; const trkX = trackFn ?? follow; /** How the hip is derived on a frame: held over a foot's anchor, tracking the lower foot on the floor (#119), or carried by the pose's travel from a frame's hip (and for a jump against `floorY`, carried so the landing frame meets the floor and frames inside the flight never go below it, #103). */ type Mode = | { kind: 'plant'; side: Side; anchor: Pt; from: number } | { kind: 'track'; side: Side; anchor: Pt; from: number; at: Pt } | { kind: 'travel'; from: number; at: Pt; end?: number; land?: number; carry?: number }; /** #285: a plant's ankle on frame i — past its span (the hand-over frame, the blend after it), posed with its leg as on its last supporting frame. */ const held = heldFn; const plantOffset = (side: Side, i: number, from: number): Pt => { if (held === undefined || (feet.support[i] ?? null) === side) return offset(i, side); let last = i; while (last > from && (feet.support[last] ?? null) !== side) last--; return held(i, side, last); }; const of = (mode: Mode, i: number): Pt => { if (mode.kind === 'plant') { const o = plantOffset(mode.side, i, mode.from); // In place (#92) the anchor's x follows the supporting ankle's own track from the frame it planted. const dx = follow === undefined ? 0 : follow(i, mode.side) - follow(mode.from, mode.side); return [mode.anchor[0] + dx - o[0], mode.anchor[1] - o[1]]; } if (mode.kind === 'track') { const o = offset(i, mode.side); const dx = trkX === undefined ? o[0] - offset(mode.from, mode.side)[0] : trkX(i, mode.side) - trkX(mode.from, mode.side); const y = floorY === null ? mode.at[1] + travel(i)[1] - travel(mode.from)[1] : mode.anchor[1] - o[1]; return [mode.anchor[0] + dx - o[0], y]; } const a = travel(i); const b = travel(mode.from); let y = mode.at[1] + a[1] - b[1]; if (mode.carry !== undefined && mode.land !== undefined && mode.end !== undefined && floorY !== null) { if ((mode.land < n || follow !== undefined) && mode.land > mode.from) { y += mode.carry * ((Math.min(i, mode.land) - mode.from) / (mode.land - mode.from)); } if (i <= mode.end) { y = Math.max(y, floorY - Math.min(offset(i, 'r')[1], offset(i, 'l')[1])); } } return [mode.at[0] + a[0] - b[0], y]; }; /** A travel span from `startFrame` off frame `from`'s hip `at`: carried to meet `floorY` at landing when it contains a jump (#103, and wrapped to frame 0 on an in-place loop, #224). */ const makeTravel = (startFrame: number, from: number, at: Pt): Mode => { let end = startFrame; while (end + 1 < n && (feet.support[end + 1] ?? null) === null && (tracked[end + 1] ?? null) === null) end++; const isJump = floorY !== null && feet.jumps.some(([ja, jb]) => ja <= end && jb >= startFrame); if (!isJump) return { kind: 'travel', from, at }; const land = end + 1; let carry = 0; const landIdx = land < n ? land : follow !== undefined ? 0 : -1; const landSide = landIdx >= 0 ? (feet.support[landIdx] ?? tracked[landIdx] ?? null) : null; if (landSide !== null) { const rawLandY = at[1] + travel(landIdx)[1] - travel(from)[1]; const targetLandY = floorY - offset(landIdx, landSide)[1]; carry = targetLandY - rawLandY; } return { kind: 'travel', from, at, end, land, carry }; }; /** A plant from frame i: its anchor's x where the foot is, its y there or on the floor. */ const plant = (i: number, side: Side, at: Pt): Mode => { anchors.set(i, at[1]); return { kind: 'plant', side, anchor: [at[0], floorY ?? at[1]], from: i }; }; let mode: Mode | null = null; let from: Mode | null = null; let blendAt = -1; let blendEnd = -1; let blendToFlight = false; for (let i = 0; i < n; i++) { const s = feet.support[i] ?? null; const tr = s === null ? (tracked[i] ?? null) : null; if (i === 0) { if (s !== null) { const o = offset(0, s); const anchorY = floorY ?? (start[1] + o[1]); mode = plant(0, s, [start[0] + o[0], start[1] + o[1]]); // #224: in a periodic in-place loop whose last frame is in flight, the first cycle's plant blends from that flight just like cycles 2..N. if (follow !== undefined && n > 1 && (feet.support[n - 1] ?? null) === null && (tracked[n - 1] ?? null) === null) { from = { kind: 'travel', from: 0, at: [start[0], anchorY - o[1]] }; blendAt = 0; blendEnd = 0; while (blendEnd + 1 < n && (feet.support[blendEnd + 1] ?? null) === s) blendEnd++; blendToFlight = blendEnd + 1 < n && (feet.support[blendEnd + 1] ?? null) === null && (tracked[blendEnd + 1] ?? null) === null; } } else if (tr !== null) { const o = offset(0, tr); mode = { kind: 'track', side: tr, anchor: [start[0] + o[0], floorY ?? (start[1] + o[1])], from: 0, at: start }; } else { mode = makeTravel(0, 0, start); } } else if (s !== null) { if (mode!.kind !== 'plant' || mode!.side !== s) { // The new support is held where it is on this frame, under the hip the old derivation gives here. const here = of(mode!, i); const o = offset(i, s); from = mode; mode = plant(i, s, [here[0] + o[0], here[1] + o[1]]); blendAt = i; blendEnd = i; while (blendEnd + 1 < n && (feet.support[blendEnd + 1] ?? null) === s) blendEnd++; blendToFlight = blendEnd + 1 < n && (feet.support[blendEnd + 1] ?? null) === null && (tracked[blendEnd + 1] ?? null) === null; } } else if (tr !== null) { if (mode!.kind !== 'track' || mode!.side !== tr) { const prev = hip[i - 1]!; const o = offset(i - 1, tr); mode = { kind: 'track', side: tr, anchor: [prev[0] + o[0], floorY ?? (prev[1] + o[1])], from: i - 1, at: prev }; from = null; blendAt = -1; } } else if (mode!.kind !== 'travel') { mode = makeTravel(i, i - 1, hip[i - 1]!); from = null; blendAt = -1; } const to = of(mode!, i); if (from !== null && i - blendAt < PLANT_BLEND) { const stepIn = i - blendAt + 1; const w = (blendToFlight && blendEnd - blendAt < PLANT_BLEND ? Math.min(stepIn, blendEnd - i + 1) : stepIn) / (PLANT_BLEND + 1); const f = of(from, i); // #102: blend y only; x takes the new derivation immediately so the planted foot does not slide across the blend. hip.push([to[0], f[1] + (to[1] - f[1]) * w]); const m = mode!; if (m.kind === 'plant') blends.push({ frame: i, side: m.side, y: m.anchor[1] }); } else { from = null; hip.push(to); } } return { hip, anchors, blends }; } // --- stance (rig-c/pose#60) -------------------------------------------------- /** * A free leg is scaled within these factors to reach the floor (raised from below or lowered from above); past them the frame is reported, not forced. * The default: a frame whose leg the picture folds further has its lower bound from the leg's own range (`stanceRangeOf`, #95). */ export const STANCE_BOUNDS: readonly [number, number] = [0.92, 1.08]; /** Per side, per frame: the lower bound of the leg's stretch (#95). */ export type StanceLower = Record; /** Maximum per-frame relative step of the continuous figure-scale curve (`rig-c/pose#163`). */ export const FIGURE_SCALE_STEP = 0.01; /** Consecutive frames whose raw figure height stays within this many pixels over ≥ `FROZEN_MIN_FRAMES` frames hold their span median (`rig-c/pose#163`). */ export const FROZEN_STEP_PX = 0.5; export const FROZEN_MIN_FRAMES = 5; function medianOf(xs: readonly number[]): number { const s = [...xs].sort((a, b) => a - b); const m = s.length >> 1; return s.length % 2 === 1 ? s[m]! : (s[m - 1]! + s[m]!) / 2; } /** * Continuous per-frame figure scale `s[i]` (`s[0] = 1`, `rig-c/pose#163`, * `rig-c/pose#161`): L2 projection of `log(raw)` onto the rate-limited corridor * `|log(s[i+1] / s[i])| <= log(1 + FIGURE_SCALE_STEP)` (`≤ 1%` per frame), * holding any frozen span (`≥ FROZEN_MIN_FRAMES` frames where consecutive raw * heights move by `≤ FROZEN_STEP_PX`) constant at its span median. */ export function figureScaleOf(raw: readonly number[], maxStep = FIGURE_SCALE_STEP): number[] { const m = raw.length; if (m === 0) return []; const u = raw.map((v) => Math.log(Math.max(1e-6, v))); const spans: Array<{ lo: number; hi: number; val: number }> = []; for (let i = 0; i < m - 1;) { if (Math.abs(raw[i + 1]! - raw[i]!) > FROZEN_STEP_PX) { i++; continue; } let j = i + 1; while (j + 1 < m && Math.abs(raw[j + 1]! - raw[j]!) <= FROZEN_STEP_PX) j++; if (j - i + 1 >= FROZEN_MIN_FRAMES) { const med = Math.log(Math.max(1e-6, medianOf(raw.slice(i, j + 1)))); for (let k = i; k <= j; k++) u[k] = med; spans.push({ lo: i, hi: j, val: med }); } i = j + 1; } const r0 = Math.log(1 + maxStep); const r = new Array(Math.max(0, m - 1)).fill(r0); for (const sp of spans) { for (let i = sp.lo; i < sp.hi; i++) r[i] = 0; } for (let k = 0; k + 1 < spans.length; k++) { const a = spans[k]!; const b = spans[k + 1]!; const gap = b.lo - a.hi; if (gap > 0) { const needed = Math.abs(b.val - a.val) / gap; if (needed > r0) { for (let i = a.hi; i < b.lo; i++) { r[i] = Math.max(needed, Math.abs(u[i + 1]! - u[i]!)) * 1.0001; } } } } const lam = new Array(Math.max(0, m - 1)).fill(0); const y = [...u]; for (let iter = 0; iter < 5000; iter++) { let maxChange = 0; for (let i = 0; i < m - 1; i++) { const z = lam[i]! + 0.5 * (y[i + 1]! - y[i]!); const thresh = 0.5 * r[i]!; const next = z > thresh ? z - thresh : z < -thresh ? z + thresh : 0; const d = next - lam[i]!; if (Math.abs(d) > 1e-12) { lam[i] = next; y[i] = y[i]! + d; y[i + 1] = y[i + 1]! - d; if (Math.abs(d) > maxChange) maxChange = Math.abs(d); } } if (maxChange < 1e-11) break; } const y0 = y[0]!; return y.map((v) => Math.exp(v - y0)); } /** * The leg's own range (rig-c/pose#95). Per side, r(i) = the leg's picture * length hip → ankle on frame i ÷ its straight length (`STRAIGHT_PERCENTILE` * of that length over the sequence, as `--leg-scale` reads a segment's), and * the lower bound on frame i is min(`STANCE_BOUNDS[0]`, r(i)): the rig leg may * fold as far as the picture's leg is folded on that frame, never less far * than the default. A leg with no straight length keeps the default. */ export function stanceRangeOf(ankle: Record, hip: Record, inPlace = false, figureScale?: readonly number[]): StanceLower { const out = {} as Record; const n = ankle.r.length; const scale = !inPlace && n > 25 ? (figureScale ?? figureScaleOf(ankle.r.map((ar, i) => Math.max( Math.hypot(ar[0] - hip.r[i]![0], ar[1] - hip.r[i]![1]), Math.hypot(ankle.l[i]![0] - hip.l[i]![0], ankle.l[i]![1] - hip.l[i]![1]), )))) : null; for (const s of SIDES) { const raw = ankle[s].map((a, i) => Math.hypot(a[0] - hip[s][i]![0], a[1] - hip[s][i]![1])); const len = scale !== null ? raw.map((l, i) => (scale[i]! > 1e-6 ? l / scale[i]! : l)) : raw; const straight = percentile(len, STRAIGHT_PERCENTILE); out[s] = len.map((l) => (straight > 1e-9 && Number.isFinite(l) ? Math.min(STANCE_BOUNDS[0], l / straight) : STANCE_BOUNDS[0])); } return out; } /** * Under `--plant-feet` and `--leg-scale`, a supporting leg segment that asks a * scale under `--leg-scale`'s floor (`limits[0]`) follows its asked scale down * to the bound #95 derives from the leg's own range (`min(limits[0], r(i))`) so * the supporting ankle stays on #85's floor after #107 (rig-c/pose#133). The * free leg keeps the configured clamp. When an asked scale goes under #95's * bound (`lower`, defaulting to the segment's own range `asked[i]`), it is * clamped there and the hip rises by the vertical remainder (`askedOffset − * usedOffset`), never sinks. */ export function scaleSupport( legScale: LegScale[], scales: Map, feet: Feet, legs: Record, hip: Pt[], askedOffset: (i: number, s: Side) => Pt, usedOffset: (i: number, s: Side) => Pt, lower?: StanceLower, ): void { const n = feet.support.length; const tracked = new Array(n).fill(null); for (const t of feet.tracked ?? []) for (let i = t.frames[0]; i <= t.frames[1]; i++) tracked[i] = t.side; const byBone = new Map(legScale.map((ls) => [ls.bone, ls])); const copied = new Set(); const ensureCopy = (b: string): number[] => { const cur = scales.get(b)!; if (copied.has(b)) return cur; const copy = [...cur]; scales.set(b, copy); copied.add(b); return copy; }; const perBone = new Map>(); for (let i = 0; i < n; i++) { const s = feet.support[i] ?? tracked[i] ?? null; if (s === null) continue; const touched: Array<{ bone: string; asked: number; used: number; bound: number; clamped: boolean }> = []; for (const b of legs[s].chain.bones) { const ls = byBone.get(b); if (ls === undefined) continue; const asked = ls.asked[i]!; const floor = ls.limits[0]; if (!(asked < floor)) continue; const r = lower !== undefined ? lower[s][i]! : asked; const bound = Math.min(floor, r); const used = Math.max(asked, bound); const clamped = used > asked + 1e-12; ensureCopy(b)[i] = used; touched.push({ bone: b, asked, used, bound, clamped }); } if (touched.length === 0) continue; let hipRise = 0; if (touched.some((t) => t.clamped)) { hipRise = Math.max(0, askedOffset(i, s)[1] - usedOffset(i, s)[1]); hip[i]![1] += hipRise; } for (const t of touched) { const list = perBone.get(t.bone) ?? []; list.push({ frame: i, side: s, ...t, hipRise }); perBone.set(t.bone, list); } } for (const ls of legScale) { const entries = perBone.get(ls.bone); if (entries === undefined || entries.length === 0) continue; const out: SupportScaleRun[] = []; for (const e of entries) { const last = out[out.length - 1]; if (last !== undefined && last.side === e.side && last.frames[1] === e.frame - 1 && (last.clamped !== undefined) === e.clamped) { last.frames[1] = e.frame; last.asked = { min: Math.min(last.asked.min, e.asked), max: Math.max(last.asked.max, e.asked) }; last.used = { min: Math.min(last.used.min, e.used), max: Math.max(last.used.max, e.used) }; if (last.clamped !== undefined) { last.clamped = { bound: Math.min(last.clamped.bound, e.bound), hipRise: Math.max(last.clamped.hipRise, e.hipRise) }; } } else { out.push({ side: e.side, frames: [e.frame, e.frame], asked: { min: e.asked, max: e.asked }, used: { min: e.used, max: e.used }, ...(e.clamped ? { clamped: { bound: e.bound, hipRise: e.hipRise } } : {}), }); } } ls.support = out; } } /** The lower bound on frame i for side s: the leg's own (#95), or the default. */ function lowerOf(lower: StanceLower | undefined, s: Side, i: number): number { return lower?.[s][i] ?? STANCE_BOUNDS[0]; } /** The lowest lower bound per side over `frames`, where any is below the default (#95); null where none is. */ function rangeUsed(lower: StanceLower | undefined, frames: ReadonlyArray<{ side: Side; frame: number }>): Partial> | null { const out: Partial> = {}; for (const { side, frame } of frames) { const lo = lowerOf(lower, side, frame); if (lo < STANCE_BOUNDS[0]) out[side] = Math.min(out[side] ?? Infinity, lo); } return Object.keys(out).length === 0 ? null : out; } /** A lifted free foot is planted again only when its height comes back under this fraction of the tolerance (#87): hysteresis, a stated factor, not tuned. */ export const STANCE_RELEASE = 0.8; /** A run of one state shorter than this many seconds, between two runs of the other state, takes their state (#87): a foot cannot take a stance for one frame. */ export const STANCE_MIN_SECONDS = 0.1; /** * A free ankle's state on a frame (#60, #114): `both`, within the tolerance of * the supporting one in either direction, so both feet are planted; `lifted`, * past it above; `below`, past it below — a support error, not a stance, never * planted. Past the tolerance either way the pose's height is kept. */ export type StanceState = 'both' | 'lifted' | 'below'; /** A run of frames with one free side in one state. */ export interface StanceRun { /** `both`: the free ankle within the tolerance of the floor, so both feet are planted; `lifted`: past it above, `below`: past it below (#114), the pose's height kept. */ state: StanceState; /** The side that is not supporting. */ side: Side; frames: [number, number]; /** The free ankle's largest height above the supporting one in the picture, in that side's leg lengths (negative where it is below on every frame). */ maxHeight: number; /** `below` (#114): the free ankle's largest distance below the supporting one in the picture, in that side's leg lengths. */ maxDepth?: number; /** `both`: the smallest and largest stretch keyed (bounded); `lifted`: null. */ k: { min: number; max: number } | null; /** #87: a run read in `state`, shorter than `STANCE_MIN_SECONDS` between two runs of the other state, which took their state; its frames are in their run too, and `k` is null. */ absorbed?: true; /** rig-c/pose#237, on a `lifted` or `below` run under `--leg-scale`: the frames whose free ankle the clamps moved off the pose's height, and by how much. Absent when the pose's height was kept on every frame. */ moved?: FreeAnkleShift; } /** A run of planted-both frames whose stretch needed a factor past the bounds (or could not reach: `needed` null). */ export interface StanceOver { side: Side; frames: [number, number]; /** The stretch the floor needed furthest from 1, or null where the free ankle is not below its leg's root. */ needed: number | null; /** The free ankle's largest distance left from the floor after the bound, world units. */ left: number; /** #95: the bound on the frame of `needed`, where the leg's own range set it below the default. */ bound?: [number, number]; } export interface Stance { /** The tolerance, in leg lengths (FEET's: the median picture hip → ankle of the free side). */ tolerance: number; /** A lifted foot is planted again under `release × tolerance` (#87). */ release: number; /** The shortest run kept as its own state, seconds (#87). */ minSeconds: number; bounds: readonly [number, number]; /** #95: per side, the lowest lower bound the leg's own range set on a planted-both frame, where any is below `bounds[0]`. */ range?: Partial>; runs: StanceRun[]; over: StanceOver[]; /** Per frame: the free side and the stretch keyed on it, or null (lifted, a jump, no support). */ stretch: Array<{ side: Side; k: number } | null>; } /** World positions (y up) on one frame: each leg chain's first bone's origin and each ankle. */ export interface StancePosed { root: Record; ankle: Record; } /** * Both feet planted when the free ankle is near the floor (rig-c/pose#60). On a * frame with a supporting side (so outside every jump), the free ankle's height * above the supporting one is read in the picture (`ankle`, px, y down) in the * free side's leg length — the unit of every FEET threshold. The tolerance is a * distance (#114): within `tolerance` of the supporting ankle, above it or below * it (|h| ≤ tolerance), the frame is planted both: the free leg is to be stretched along its chain by * k = (root y − floor y) / (root y − ankle y) on the posed rig (`posed`, world, * y up), the floor being the supporting ankle's posed y; k is held within * `STANCE_BOUNDS` (a free ankle below the floor shortens its leg, one above * lengthens it), its lower bound on the frame the leg's own (`lower`, #95) * where the picture folds that leg further, and a frame past them is listed in * `over`. Past the tolerance above, the foot is lifted and keeps the pose's * height. Past it below (h < −tolerance), the free ankle is far below the * supporting one: a support error, not a stance. That frame is never planted * both, keeps the pose's height, and is read `below` (#114); before #114 the * gap was signed, and a lifted foot read as supporting had the grounded leg * shrunk up to it. * * The state is read with hysteresis (#87) on |h|: a planted foot leaves the * stance when |h| goes past `tolerance`, and one past it is planted again only * under `STANCE_RELEASE × tolerance`, so noise at the threshold does not flip it. A * run of one state shorter than `STANCE_MIN_SECONDS` (`secondsOf`, the * retarget's timing of a run) between two runs of the other state, on the same * free side and with no frame between, takes their state, and is listed with * `absorbed`. */ /** * Under `--stance` and `--leg-scale` (rig-c/pose#137), a free foot read `both` * (planted beside the supporting one within `tolerance`) whose posed ankle sits * above the floor (`ankle[free][1] > ankle[support][1]`) while neither the * whole leg (`lo >= STANCE_BOUNDS[0]`) nor its thigh (`s0 >= STANCE_BOUNDS[0]`) * is folded below `STANCE_BOUNDS[0]`, yet its shin was shortened below it * (`s1 < STANCE_BOUNDS[0]`), owes that shin shortening to a low knee / high * ankle split along a straight stance leg in the picture. Keeping the shortened * shin scale makes `--stance` multiply both thigh and shin by `needed`, * over-stretching the thigh (pushing the knee down), leaving the shin and foot * squashed, and exceeding `STANCE_BOUNDS[1]` so the free ankle floats off #85's * floor. Lifting the shin's scale to `min(1, s0)` before `stanceOf` reads * `needed` restores the straight leg's shin proportion first so the chain is * stretched only by the remaining gap. */ export function unshortenStanceShin( scales: Map, legs: Record, posed: (i: number) => StancePosed, lower?: StanceLower, ): (i: number, free?: Side) => StancePosed { const copied = new Set(); const ensureCopy = (b: string): number[] => { const cur = scales.get(b)!; if (copied.has(b)) return cur; const copy = [...cur]; scales.set(b, copy); copied.add(b); return copy; }; return (i: number, free?: Side): StancePosed => { const p = posed(i); if (free === undefined) return p; if (lowerOf(lower, free, i) < STANCE_BOUNDS[0]) return p; const support: Side = free === 'r' ? 'l' : 'r'; const floor = p.ankle[support][1]; const root = p.root[free][1]; const a = p.ankle[free][1]; if (!(root - a > 1e-9 && a > floor + 1e-9)) return p; const bones = legs[free].chain.bones; if (bones.length < 2) return p; const b0 = bones[0]!; const b1 = bones[bones.length - 1]!; const arr0 = scales.get(b0); const arr1 = scales.get(b1); if (arr0 === undefined || arr1 === undefined) return p; const s0 = arr0[i]!; const s1 = arr1[i]!; if (!(s0 >= STANCE_BOUNDS[0] && s1 < STANCE_BOUNDS[0])) return p; ensureCopy(b1)[i] = Math.min(1, s0); return posed(i); }; } export function stanceOf(feet: Feet, ankle: Record, tolerance: number, posed: (i: number, free?: Side) => StancePosed, secondsOf: (run: [number, number]) => number, lower?: StanceLower): Stance { const n = feet.support.length; const stretch: Stance['stretch'] = new Array(n).fill(null); const tracked = new Array(n).fill(null); for (const t of feet.tracked ?? []) for (let i = t.frames[0]; i <= t.frames[1]; i++) tracked[i] = t.side; type Read = { state: StanceState; side: Side; height: number }; const read: Array = new Array(n).fill(null); for (let i = 0; i < n; i++) { const s = feet.support[i] ?? tracked[i] ?? null; if (s === null) continue; const f: Side = s === 'r' ? 'l' : 'r'; read[i] = { state: 'both', side: f, height: (ankle[s][i]![1] - ankle[f][i]![1]) / feet.legLength[f] }; } // Blocks of consecutive frames with one free side. Inside a block the state changes only when the // other state's condition (out of the stance: |h| past the tolerance; planted again: |h| under // STANCE_RELEASE of it) holds for STANCE_MIN_SECONDS; a shorter crossing with the current state on // both sides of it is absorbed. A crossing that runs to the block's end is taken: nothing of the // current state follows it. Out of the stance, a frame is `lifted` or `below` by the sign of h; a // frame absorbed into it takes the state of the frame before it. const away = (h: number): 'lifted' | 'below' => (h > 0 ? 'lifted' : 'below'); const absorbed: StanceRun[] = []; for (let i = 0; i < n; ) { const r0 = read[i]; if (r0 === null || r0 === undefined) { i++; continue; } let end = i; while (end + 1 < n && read[end + 1] !== null && read[end + 1]!.side === r0.side) end++; let planted = Math.abs(r0.height) <= tolerance; let j = i; while (j <= end) { const crosses = (k: number) => (planted ? Math.abs(read[k]!.height) > tolerance : Math.abs(read[k]!.height) <= STANCE_RELEASE * tolerance); if (!crosses(j)) { read[j]!.state = planted ? 'both' : away(read[j]!.height); j++; continue; } let k = j; while (k + 1 <= end && crosses(k + 1)) k++; if (k < end && secondsOf([j, k]) < STANCE_MIN_SECONDS) { let maxHeight = -Infinity; let far = 0; for (let x = j; x <= k; x++) { read[x]!.state = planted ? 'both' : read[x - 1]!.state; maxHeight = Math.max(maxHeight, read[x]!.height); if (Math.abs(read[x]!.height) > Math.abs(far)) far = read[x]!.height; } const other: StanceState = planted ? away(far) : 'both'; absorbed.push({ state: other, side: r0.side, frames: [j, k], maxHeight, ...(other === 'below' ? { maxDepth: -far } : {}), k: null, absorbed: true }); } else { planted = !planted; for (let x = j; x <= k; x++) read[x]!.state = planted ? 'both' : away(read[x]!.height); } j = k + 1; } i = end + 1; } type Row = { state: StanceState; side: Side; height: number; k: number | null; needed: number | null; over: boolean; left: number; lo: number }; const rows: Array = new Array(n).fill(null); for (let i = 0; i < n; i++) { const r = read[i]; if (r === null || r === undefined) continue; const s: Side = r.side === 'r' ? 'l' : 'r'; const f = r.side; const height = r.height; const lo = lowerOf(lower, f, i); if (r.state !== 'both') { rows[i] = { state: r.state, side: f, height, k: null, needed: null, over: false, left: 0, lo }; continue; } const p = posed(i, f); const floor = p.ankle[s][1]; const root = p.root[f][1]; const a = p.ankle[f][1]; const ok = root - a > 1e-9 && root - floor > 1e-9; const needed = ok ? (root - floor) / (root - a) : null; const k = needed === null ? 1 : Math.min(Math.max(needed, lo), STANCE_BOUNDS[1]); const over = needed === null || needed < lo || needed > STANCE_BOUNDS[1]; const left = Math.abs(root - k * (root - a) - floor); rows[i] = { state: 'both', side: f, height, k, needed, over, left, lo }; if (k !== 1) stretch[i] = { side: f, k }; } const boundOf = (lo: number): { bound?: [number, number] } => (lo < STANCE_BOUNDS[0] ? { bound: [lo, STANCE_BOUNDS[1]] } : {}); const runs: StanceRun[] = []; const over: StanceOver[] = []; for (let i = 0; i < n; i++) { const r = rows[i]; if (r === null) continue; const last = runs[runs.length - 1]; if (last !== undefined && last.frames[1] === i - 1 && last.state === r.state && last.side === r.side) { last.frames[1] = i; last.maxHeight = Math.max(last.maxHeight, r.height); if (last.maxDepth !== undefined) last.maxDepth = Math.max(last.maxDepth, -r.height); if (last.k !== null && r.k !== null) last.k = { min: Math.min(last.k.min, r.k), max: Math.max(last.k.max, r.k) }; } else runs.push({ state: r.state, side: r.side, frames: [i, i], maxHeight: r.height, ...(r.state === 'below' ? { maxDepth: -r.height } : {}), k: r.k === null ? null : { min: r.k, max: r.k } }); if (!r.over) continue; const o = over[over.length - 1]; if (o !== undefined && o.frames[1] === i - 1 && o.side === r.side) { o.frames[1] = i; const takes = o.needed !== null && (r.needed === null || Math.abs(r.needed - 1) > Math.abs(o.needed - 1)); o.needed = o.needed === null || r.needed === null ? null : takes ? r.needed : o.needed; if (takes) { delete o.bound; Object.assign(o, boundOf(r.lo)); } o.left = Math.max(o.left, r.left); } else over.push({ side: r.side, frames: [i, i], needed: r.needed, left: r.left, ...boundOf(r.lo) }); } for (const x of absorbed) runs.push(x); runs.sort((a, b) => a.frames[0] - b.frames[0] || (a.absorbed === true ? 1 : 0) - (b.absorbed === true ? 1 : 0)); const range = rangeUsed(lower, rows.flatMap((r, i) => (r !== null && r.state === 'both' ? [{ side: r.side, frame: i }] : []))); return { tolerance, release: STANCE_RELEASE, minSeconds: STANCE_MIN_SECONDS, bounds: STANCE_BOUNDS, ...(range === null ? {} : { range }), runs, over, stretch }; } // --- the blend held on the floor (rig-c/pose#85) ------------------------------ /** A supporting ankle already within this many world units of its anchor on a blend frame is left as it is: the landing check's origin tolerance (`verify.ts` ORIGIN_TOLERANCE). */ export const BLEND_HOLD_WITHIN = 0.01; /** A run of blend frames on one supporting side whose leg was stretched. */ export interface BlendRun { side: Side; frames: [number, number]; /** The smallest and largest stretch keyed (bounded). */ k: { min: number; max: number }; /** #95: the lowest lower bound on the run's frames, where the leg's own range set it below the default. */ lower?: number; } export interface BlendHold { bounds: readonly [number, number]; /** #95: per side, the lowest lower bound the leg's own range set on a blend frame, where any is below `bounds[0]`. */ range?: Partial>; runs: BlendRun[]; /** Runs whose stretch needed a factor past the bounds: held at the bound, the remainder left to the blend. */ over: StanceOver[]; /** Per frame: the supporting side and the stretch keyed on it, or null. */ stretch: Array<{ side: Side; k: number } | null>; } /** * #425: with the source driving a walk's hip height, every supporting leg * must reach the floor. Use the blend hold's geometry and existing bounds, * but keep this solve separate from the report's blend-only reading. */ export function holdSupportingFloor(feet: Pick, floor: number, posed: (i: number) => StancePosed, lower?: StanceLower): Pick { const frames: BlendFrame[] = []; feet.support.forEach((side, frame) => { if (side !== null) frames.push({ frame, side, y: floor }); }); const held = holdBlend(frames, feet.support.length, posed, lower); return { stretch: held.stretch, over: held.over }; } /** * The supporting foot does not leave the floor during a blend (rig-c/pose#85). * On a blend frame the hip is between the old derivation and the new, so the * supporting ankle is off the y its plant holds. Its leg is stretched along its * chain about the chain's root, as `--stance` stretches a free leg (`stanceOf`): * k = (root y − anchor y) / (root y − ankle y) on the posed rig (`posed`, world, * y up), held within `STANCE_BOUNDS` — its lower bound on the frame the leg's own * (`lower`, #95) as `stanceOf`'s; a frame past them is listed in `over`, and * the remainder stays with the blend. An ankle within `BLEND_HOLD_WITHIN` of the * anchor is not stretched. */ export function holdBlend(blends: readonly BlendFrame[], n: number, posed: (i: number) => StancePosed, lower?: StanceLower): BlendHold { const stretch: BlendHold['stretch'] = new Array(n).fill(null); const runs: BlendRun[] = []; const over: StanceOver[] = []; const held: Array<{ side: Side; frame: number }> = []; for (const b of blends) { const p = posed(b.frame); const root = p.root[b.side][1]; const a = p.ankle[b.side][1]; if (Math.abs(a - b.y) <= BLEND_HOLD_WITHIN) continue; held.push({ side: b.side, frame: b.frame }); const ok = root - a > 1e-9 && root - b.y > 1e-9; const needed = ok ? (root - b.y) / (root - a) : null; const lo = lowerOf(lower, b.side, b.frame); const k = needed === null ? 1 : Math.min(Math.max(needed, lo), STANCE_BOUNDS[1]); const left = Math.abs(root - k * (root - a) - b.y); const bound = lo < STANCE_BOUNDS[0] ? { bound: [lo, STANCE_BOUNDS[1]] as [number, number] } : {}; if (k !== 1) { stretch[b.frame] = { side: b.side, k }; const last = runs[runs.length - 1]; if (last !== undefined && last.side === b.side && last.frames[1] === b.frame - 1) { last.frames[1] = b.frame; last.k = { min: Math.min(last.k.min, k), max: Math.max(last.k.max, k) }; if (lo < STANCE_BOUNDS[0]) last.lower = Math.min(last.lower ?? Infinity, lo); } else runs.push({ side: b.side, frames: [b.frame, b.frame], k: { min: k, max: k }, ...(lo < STANCE_BOUNDS[0] ? { lower: lo } : {}) }); } if (needed !== null && needed >= lo && needed <= STANCE_BOUNDS[1]) continue; const o = over[over.length - 1]; if (o !== undefined && o.side === b.side && o.frames[1] === b.frame - 1) { o.frames[1] = b.frame; const takes = o.needed !== null && (needed === null || Math.abs(needed - 1) > Math.abs(o.needed - 1)); o.needed = o.needed === null || needed === null ? null : takes ? needed : o.needed; if (takes) { delete o.bound; Object.assign(o, bound); } o.left = Math.max(o.left, left); } else over.push({ side: b.side, frames: [b.frame, b.frame], needed, left, ...bound }); } const range = rangeUsed(lower, held); return { bounds: STANCE_BOUNDS, ...(range === null ? {} : { range }), runs, over, stretch }; }