|
| 1 | +# L3.1 — Foot-Flat Correction Implementation Plan |
| 2 | + |
| 3 | +> **For agentic workers:** Use superpowers:executing-plans to implement task-by-task. Steps use checkbox (`- [ ]`) syntax. |
| 4 | +
|
| 5 | +**Goal:** Keep planted soles level with the floor so grounded lower-body moves (squat, lunge, deadlift) rest flat instead of balancing on the toes, without disturbing authored angles, tiptoe moves, or swing feet. |
| 6 | + |
| 7 | +**Architecture:** A new `levelPlantedFeet(m, activeGroundLock)` in `contacts.ts` rotates each ground-locked ankle so its sole normal (local `-Y`) points world-down, weighted by planted-ness and skipped on authored plantarflex. Wired into the viewer `frame()` (before the floor clamp) and `load()`. |
| 8 | + |
| 9 | +**Tech Stack:** TypeScript ESM (`.js` specifiers), Three.js, Vitest. |
| 10 | + |
| 11 | +## Global Constraints |
| 12 | + |
| 13 | +- Immutability: never mutate shared keyframe quaternions; write bone quaternions in place only (matches `alignFloorPalms`). |
| 14 | +- `plantarflex` = ankle local Euler **+X** (toe-down); `dorsiflex` = **−X**. Sole-down normal = ankle local `(0,−1,0)`. |
| 15 | +- Constants named + exported for tests: `PLANT_FADE = 0.06` (m), `PLANTARFLEX_SKIP = 15 * DEG` (rad). |
| 16 | +- Reuse the `alignFloorPalms` idiom (`getWorldQuaternion` → `setFromUnitVectors(current, DOWN)` → back to local via parent inverse). |
| 17 | +- Clamp the corrected ankle to its ROM (`eulerRomFor("ankle_left"/"ankle_right")`), widened to admit the authored angle. |
| 18 | +- TDD; keep every existing suite green; typecheck clean. |
| 19 | +- Test a file: `npx vitest run <path>` from repo root. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +### Task 1: `levelPlantedFeet` in contacts.ts |
| 24 | + |
| 25 | +**Files:** |
| 26 | +- Modify: `packages/posecode-render/src/contacts.ts` |
| 27 | +- Test: `packages/posecode-render/test/contacts.test.ts` (create if absent) |
| 28 | + |
| 29 | +**Interfaces:** |
| 30 | +- Produces: `levelPlantedFeet(m: Mannequin, activeGroundLock: readonly string[]): void`, plus exported consts `PLANT_FADE`, `PLANTARFLEX_SKIP`. |
| 31 | + |
| 32 | +- [ ] **Step 1: Write the failing tests** |
| 33 | + |
| 34 | +```ts |
| 35 | +// packages/posecode-render/test/contacts.test.ts |
| 36 | +import { describe, it, expect } from "vitest"; |
| 37 | +import * as THREE from "three"; |
| 38 | +import { buildMannequin } from "../src/mannequin.js"; |
| 39 | +import { levelPlantedFeet } from "../src/contacts.js"; |
| 40 | + |
| 41 | +const DEG = Math.PI / 180; |
| 42 | + |
| 43 | +/** World-space sole normal (ankle local -Y) for a foot. */ |
| 44 | +function soleNormal(m: ReturnType<typeof buildMannequin>, side: "left" | "right") { |
| 45 | + const ankle = m.bones.get(`ankle_${side}`)!; |
| 46 | + const q = ankle.getWorldQuaternion(new THREE.Quaternion()); |
| 47 | + return new THREE.Vector3(0, -1, 0).applyQuaternion(q).normalize(); |
| 48 | +} |
| 49 | + |
| 50 | +describe("levelPlantedFeet", () => { |
| 51 | + it("levels a tilted planted foot so the sole faces down", () => { |
| 52 | + const m = buildMannequin(); |
| 53 | + // Tilt the whole leg forward by rotating the knee so the foot pitches. |
| 54 | + m.bones.get("knee_left")!.rotation.x = 40 * DEG; |
| 55 | + m.root.updateMatrixWorld(true); |
| 56 | + levelPlantedFeet(m, ["feet"]); |
| 57 | + m.root.updateMatrixWorld(true); |
| 58 | + const n = soleNormal(m, "left"); |
| 59 | + // sole normal points world-down (0,-1,0): dot with DOWN ~ 1 |
| 60 | + expect(n.dot(new THREE.Vector3(0, -1, 0))).toBeGreaterThan(0.98); |
| 61 | + }); |
| 62 | + |
| 63 | + it("leaves an authored-plantarflex foot on its toes", () => { |
| 64 | + const m = buildMannequin(); |
| 65 | + m.bones.get("ankle_left")!.rotation.x = 30 * DEG; // plantarflex (toe-down) |
| 66 | + m.root.updateMatrixWorld(true); |
| 67 | + const before = m.bones.get("ankle_left")!.quaternion.clone(); |
| 68 | + levelPlantedFeet(m, ["feet"]); |
| 69 | + expect(m.bones.get("ankle_left")!.quaternion.angleTo(before)).toBeLessThan(1e-6); |
| 70 | + }); |
| 71 | + |
| 72 | + it("does not touch a swing foot lifted off the floor", () => { |
| 73 | + const m = buildMannequin(); |
| 74 | + // Lift the foot well above the floor by bending the knee back and raising hip. |
| 75 | + m.bones.get("hip_left")!.rotation.x = -60 * DEG; |
| 76 | + m.root.position.y = 0.5; |
| 77 | + m.root.updateMatrixWorld(true); |
| 78 | + const before = m.bones.get("ankle_left")!.quaternion.clone(); |
| 79 | + levelPlantedFeet(m, ["feet"]); |
| 80 | + expect(m.bones.get("ankle_left")!.quaternion.angleTo(before)).toBeLessThan(1e-3); |
| 81 | + }); |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +- [ ] **Step 2: Run to verify failure** |
| 86 | + |
| 87 | +Run: `npx vitest run packages/posecode-render/test/contacts.test.ts` |
| 88 | +Expected: FAIL — `levelPlantedFeet` not exported. |
| 89 | + |
| 90 | +- [ ] **Step 3: Implement `levelPlantedFeet`** (append to `contacts.ts`) |
| 91 | + |
| 92 | +```ts |
| 93 | +import { eulerRomFor } from "posecode-parser"; |
| 94 | + |
| 95 | +const SOLE_LOCAL = new THREE.Vector3(0, -1, 0); |
| 96 | +const DEG = Math.PI / 180; |
| 97 | +/** Foot mesh-bottom height at/below which the sole is fully leveled (m). */ |
| 98 | +export const PLANT_FADE = 0.06; |
| 99 | +/** Authored plantarflex (ankle local +X) beyond this opts out of leveling (rad). */ |
| 100 | +export const PLANTARFLEX_SKIP = 15 * DEG; |
| 101 | + |
| 102 | +const FOOT_SIDES: Array<"left" | "right"> = ["left", "right"]; |
| 103 | +const TMP_EULER = new THREE.Euler(); |
| 104 | + |
| 105 | +/** |
| 106 | + * Level each ground-locked foot: rotate the ankle so the sole normal points |
| 107 | + * world-down (the whole sole rests flat), weighted by how planted the foot is |
| 108 | + * and skipped when the ankle is authored into plantarflexion (tiptoe intent). |
| 109 | + * Analogue of `alignFloorPalms` for feet. |
| 110 | + */ |
| 111 | +export function levelPlantedFeet(m: Mannequin, activeGroundLock: readonly string[]): void { |
| 112 | + if (!activeGroundLock.includes("feet")) return; |
| 113 | + let changed = false; |
| 114 | + for (const side of FOOT_SIDES) { |
| 115 | + const ankle = m.bones.get(`ankle_${side}`); |
| 116 | + if (!ankle?.parent) continue; |
| 117 | + // Tiptoe opt-out: authored plantarflex (local +X) beyond the threshold. |
| 118 | + TMP_EULER.setFromQuaternion(ankle.quaternion, "XYZ"); |
| 119 | + if (TMP_EULER.x > PLANTARFLEX_SKIP) continue; |
| 120 | + // Planted-ness weight from the foot mesh bottom height. |
| 121 | + const box = new THREE.Box3().setFromObject(ankle); |
| 122 | + const y = Number.isFinite(box.min.y) ? box.min.y : 0; |
| 123 | + const weight = THREE.MathUtils.clamp((PLANT_FADE - y) / PLANT_FADE, 0, 1); |
| 124 | + if (weight <= 1e-3) continue; |
| 125 | + // Minimal rotation aligning the sole normal to world-down. |
| 126 | + const world = ankle.getWorldQuaternion(new THREE.Quaternion()); |
| 127 | + const current = SOLE_LOCAL.clone().applyQuaternion(world).normalize(); |
| 128 | + const correction = new THREE.Quaternion().setFromUnitVectors(current, DOWN); |
| 129 | + if (weight < 1) correction.slerp(new THREE.Quaternion(), 1 - weight); |
| 130 | + const desiredWorld = correction.multiply(world); |
| 131 | + const parentWorld = ankle.parent.getWorldQuaternion(new THREE.Quaternion()); |
| 132 | + const local = parentWorld.invert().multiply(desiredWorld); |
| 133 | + // Clamp to ankle ROM, widened to admit the authored angle. |
| 134 | + const rom = eulerRomFor(`ankle_${side}`); |
| 135 | + if (rom) { |
| 136 | + TMP_EULER.setFromQuaternion(local, "XYZ"); |
| 137 | + const authored = new THREE.Euler().setFromQuaternion(ankle.quaternion, "XYZ"); |
| 138 | + const cx = THREE.MathUtils.clamp( |
| 139 | + TMP_EULER.x, |
| 140 | + Math.min(rom.x.min * DEG, authored.x), |
| 141 | + Math.max(rom.x.max * DEG, authored.x), |
| 142 | + ); |
| 143 | + const cz = THREE.MathUtils.clamp( |
| 144 | + TMP_EULER.z, |
| 145 | + Math.min(rom.z.min * DEG, authored.z), |
| 146 | + Math.max(rom.z.max * DEG, authored.z), |
| 147 | + ); |
| 148 | + TMP_EULER.set(cx, TMP_EULER.y, cz, "XYZ"); |
| 149 | + local.setFromEuler(TMP_EULER); |
| 150 | + } |
| 151 | + ankle.quaternion.copy(local); |
| 152 | + changed = true; |
| 153 | + } |
| 154 | + if (changed) m.root.updateMatrixWorld(true); |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +(Note: `DOWN` already exists at the top of `contacts.ts`.) |
| 159 | + |
| 160 | +- [ ] **Step 4: Run to verify pass** |
| 161 | + |
| 162 | +Run: `npx vitest run packages/posecode-render/test/contacts.test.ts` |
| 163 | +Expected: PASS (3 tests). |
| 164 | + |
| 165 | +- [ ] **Step 5: Commit** |
| 166 | + |
| 167 | +```bash |
| 168 | +git add packages/posecode-render/src/contacts.ts packages/posecode-render/test/contacts.test.ts |
| 169 | +git commit -m "feat(render): levelPlantedFeet — plantigrade foot-flat correction" |
| 170 | +``` |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +### Task 2: Wire into the viewer frame loop + load |
| 175 | + |
| 176 | +**Files:** |
| 177 | +- Modify: `packages/posecode-render/src/index.ts` |
| 178 | +- Test: `packages/posecode-render/test/render.test.ts` (add end-to-end squat-flat test) |
| 179 | + |
| 180 | +**Interfaces:** |
| 181 | +- Consumes: `levelPlantedFeet` (Task 1). |
| 182 | + |
| 183 | +- [ ] **Step 1: Write the failing end-to-end test** (append to `render.test.ts`) |
| 184 | + |
| 185 | +```ts |
| 186 | +it("rests a squatting foot flat on the floor (not on the toes)", () => { |
| 187 | + const src = [ |
| 188 | + 'posecode exercise "sq"', |
| 189 | + " rig humanoid", |
| 190 | + " pose start = standing", |
| 191 | + ' step "Descend" 1s settle:', |
| 192 | + " hips: flex 80", |
| 193 | + " knees: flex 95", |
| 194 | + " pelvis: hinge 25", |
| 195 | + " ground-lock: feet", |
| 196 | + ].join("\n"); |
| 197 | + const { ir } = parse(src); |
| 198 | + const tl = buildTimeline(ir!); |
| 199 | + const m = buildMannequin(); |
| 200 | + tl.sample(1, m.bones); |
| 201 | + m.root.updateMatrixWorld(true); |
| 202 | + // Simulate the frame-loop contact stages relevant to feet: |
| 203 | + groundFigure(m); |
| 204 | + applyGroundLock(m, ["feet"]); |
| 205 | + levelPlantedFeet(m, ["feet"]); |
| 206 | + m.root.updateMatrixWorld(true); |
| 207 | + const ankle = m.bones.get("ankle_left")!; |
| 208 | + const soleNormal = new THREE.Vector3(0, -1, 0) |
| 209 | + .applyQuaternion(ankle.getWorldQuaternion(new THREE.Quaternion())) |
| 210 | + .normalize(); |
| 211 | + expect(soleNormal.dot(new THREE.Vector3(0, -1, 0))).toBeGreaterThan(0.9); // sole ~flat |
| 212 | +}); |
| 213 | +``` |
| 214 | + |
| 215 | +Add `levelPlantedFeet` to the render.test.ts import from `../src/contacts.js`. |
| 216 | + |
| 217 | +- [ ] **Step 2: Run to verify failure** |
| 218 | + |
| 219 | +Run: `npx vitest run packages/posecode-render/test/render.test.ts` |
| 220 | +Expected: FAIL — `levelPlantedFeet` not imported / sole not level. |
| 221 | + |
| 222 | +- [ ] **Step 3: Wire into `index.ts`** |
| 223 | + |
| 224 | +Import alongside the existing contacts import: |
| 225 | + |
| 226 | +```ts |
| 227 | +import { alignFloorPalms, levelPlantedFeet } from "./contacts.js"; |
| 228 | +``` |
| 229 | + |
| 230 | +In `frame()`, immediately after the `alignFloorPalms(mannequin, info.reaches, info.pins);` line and before the floor-clamp bbox block: |
| 231 | + |
| 232 | +```ts |
| 233 | + levelPlantedFeet(mannequin, info.groundLock); |
| 234 | +``` |
| 235 | + |
| 236 | +In `load()`, after `groundFigureOf(mannequin);` and before `captureGroundTargets();`: |
| 237 | + |
| 238 | +```ts |
| 239 | + levelPlantedFeet(mannequin, timeline.sample(0, mannequin.bones).groundLock); |
| 240 | +``` |
| 241 | + |
| 242 | +(If calling `sample` twice is awkward, capture the phase-0 groundLock from `timeline.segments`/IR instead; simplest is to read `ir.phases[0]?.groundLock ?? []`.) |
| 243 | + |
| 244 | +- [ ] **Step 4: Run to verify pass** |
| 245 | + |
| 246 | +Run: `npx vitest run packages/posecode-render` |
| 247 | +Expected: PASS, all render tests green. |
| 248 | + |
| 249 | +- [ ] **Step 5: Commit** |
| 250 | + |
| 251 | +```bash |
| 252 | +git add packages/posecode-render/src/index.ts packages/posecode-render/test/render.test.ts |
| 253 | +git commit -m "feat(render): apply foot-flat in the viewer frame loop and on load" |
| 254 | +``` |
| 255 | + |
| 256 | +--- |
| 257 | + |
| 258 | +### Task 3: Editor discoverability + squat demo fix + full verify |
| 259 | + |
| 260 | +**Files:** |
| 261 | +- Modify: `packages/posecode-language/src/vocab.ts` (`ground-lock` doc) |
| 262 | +- Modify: `spec/examples/squat.posecode` (drop the spurious plantarflex) |
| 263 | + |
| 264 | +- [ ] **Step 1: Update the ground-lock doc** |
| 265 | + |
| 266 | +```ts |
| 267 | +// vocab.ts KEYWORD_DOCS |
| 268 | + "ground-lock": "Pins effectors (hands / feet) to the floor for this phase. Planted feet auto-level flat to the floor unless the ankle is plantarflexed (tiptoe).", |
| 269 | +``` |
| 270 | + |
| 271 | +- [ ] **Step 2: Fix the squat demo** |
| 272 | + |
| 273 | +In `spec/examples/squat.posecode`, remove the `ankles: plantarflex 50` line from the Descend |
| 274 | +step (and its `plantarflex 0` in Drive up), letting foot-flat land the sole. Keep everything |
| 275 | +else. |
| 276 | + |
| 277 | +- [ ] **Step 3: Language tests + full suite + typecheck** |
| 278 | + |
| 279 | +Run: `npx vitest run` (whole workspace) and `npm run typecheck`. |
| 280 | +Expected: all green; the 76 example tests still pass (squat still parses, now flatter). |
| 281 | + |
| 282 | +- [ ] **Step 4: Browser verify** |
| 283 | + |
| 284 | +Start the playground (`preview_start name playground`), open `/play.html#doc=squat` and |
| 285 | +`/play.html#doc=releve`. Confirm: squat rests flat on both feet; relevé stays on the balls of |
| 286 | +the feet. Check console for errors. Screenshot both. |
| 287 | + |
| 288 | +- [ ] **Step 5: Commit** |
| 289 | + |
| 290 | +```bash |
| 291 | +git add packages/posecode-language/src/vocab.ts spec/examples/squat.posecode |
| 292 | +git commit -m "feat: squat rests flat via foot-flat; document ground-lock leveling (L3.1)" |
| 293 | +``` |
| 294 | + |
| 295 | +--- |
| 296 | + |
| 297 | +## Self-Review |
| 298 | + |
| 299 | +- Foot-flat mechanism (align sole to down) → Task 1. ✅ |
| 300 | +- Planted-ness soft blend → Task 1 (`weight`). ✅ |
| 301 | +- Plantarflex opt-out → Task 1 (`PLANTARFLEX_SKIP`). ✅ |
| 302 | +- ROM clamp risk mitigation → Task 1 (eulerRomFor widen). ✅ |
| 303 | +- Frame-loop + load wiring → Task 2. ✅ |
| 304 | +- DSL discoverability (no new keyword) → Task 3 Step 1. ✅ |
| 305 | +- Squat demo + relevé opt-out verification → Task 3. ✅ |
| 306 | +- Tests first each task; existing suites green → all tasks. ✅ |
| 307 | +- **Placeholder scan:** none. **Type consistency:** `levelPlantedFeet(m, activeGroundLock)`, |
| 308 | + `PLANT_FADE`, `PLANTARFLEX_SKIP` used consistently. |
0 commit comments