Initial Commit

This commit is contained in:
2026-05-27 23:29:53 +01:00
commit fbc70f96e6
10000 changed files with 1102152 additions and 0 deletions

View File

@@ -0,0 +1,163 @@
# OpenSpec Change: enterprise-gaming-ik-solver
- **Change ID**: enterprise-gaming-ik-solver
- **Package**: `@suaveplan/ik-solver`
- **Category**: gaming/rigs
- **Target path**: `gaming/rigs/ik-solver`
- **Status**: PROPOSED
- **Date**: 2026-04-24
- **Reference spec**: `/mnt/s/web/suaveplan/enterprise/ik-solver.md`
---
## Summary
Deliver `@suaveplan/ik-solver` on the genesis branch from scratch at enterprise quality. The package provides pure-math inverse kinematics solvers: two-bone analytic IK (arm/leg chains), look-at (head/eye), and multi-chain FABRIK for arbitrary-length chains. No Three.js dependency — callers pass `Vec3`/`Quat` structs and receive solved bone rotations. Genesis adds configurable iteration limits, convergence thresholds, Zod chain config schemas, structured errors, OTel solve-time histogram, and 100% coverage with property tests for geometric correctness.
---
## Current-state audit (genesis)
Package does not exist on genesis yet — fresh build.
---
## Available workspace deps on genesis
| Package | Role |
|---|---|
| `@suaveplan/error` (`workspace:^`) | `IkSolverError`; `IK.*` codes |
| `@suaveplan/types` (`workspace:^`) | Shared primitives |
| `zod` (`catalog:forms`) | Chain config schemas |
| `@opentelemetry/api` (`catalog:observability`) | Solve duration histogram, iteration count gauge |
---
## Public API surface
```ts
// Two-bone analytic IK (arm/leg)
function solveTwoBone(chain: TwoBoneChain, target: Vec3, hint?: Vec3): TwoBoneSolution
interface TwoBoneChain {
root: Vec3
mid: Vec3
tip: Vec3
upperLength: number
lowerLength: number
}
interface TwoBoneSolution {
upperRotation: Quat
lowerRotation: Quat
reached: boolean
}
// Look-at IK
function solveLookAt(
bonePosition: Vec3,
boneForward: Vec3,
boneUp: Vec3,
target: Vec3,
clampAngle?: number // max angle in radians
): Quat
// FABRIK multi-chain
function solveFabrik(chain: FabrikChain, options?: FabrikOptions): FabrikSolution
interface FabrikChain {
joints: Vec3[]
lengths: number[]
target: Vec3
root?: Vec3 // if set, root is constrained to original position
}
interface FabrikOptions {
maxIterations?: number // default 10
tolerance?: number // convergence threshold, default 0.001
constraints?: JointConstraint[]
}
interface JointConstraint {
jointIndex: number
minAngle: number
maxAngle: number
axis: Vec3
}
interface FabrikSolution {
joints: Vec3[]
rotations: Quat[]
iterations: number
converged: boolean
}
// Utility: build chain from bone descriptors
function chainFromBones(bones: BoneDescriptor[], indices: number[]): FabrikChain
```
### Error codes
```ts
type IkErrorCode =
| 'IK_CHAIN_TOO_SHORT'
| 'IK_DEGENERATE_CHAIN'
| 'IK_MAX_ITERATIONS_EXCEEDED'
| 'IK_INVALID_CONSTRAINT'
```
---
## New additions for genesis
1. **Hint vector for two-bone** — optional pole-vector hint controls elbow/knee direction.
2. **Joint constraints in FABRIK** — hinge and cone constraints applied per iteration.
3. **OTel** — `ik.solve_duration_ms` histogram; `ik.iterations` histogram.
4. **Property tests** — two-bone: tip distance from target ≤ tolerance after solve; look-at: resulting forward aligns with target direction.
---
## File layout
```text
gaming/rigs/ik-solver/
package.json
tsconfig.json
vitest.config.ts
src/
index.ts
error-codes.ts
constants/index.ts
types/common.ts
types/common.md
solvers/
two-bone/
two-bone.ts
two-bone.md
two-bone.types.ts
two-bone.test.ts
index.ts
look-at/
look-at.ts
look-at.md
look-at.test.ts
index.ts
fabrik/
fabrik.ts
fabrik.md
fabrik.types.ts
fabrik.test.ts
index.ts
utils/
vec3-math.ts
vec3-math.md
vec3-math.test.ts
tests/
property.test.ts
```
---
## Impact
**Downstream**: `@suaveplan/r3f-animation` uses these solvers for foot IK and hand IK; `@suaveplan/animation-events` uses look-at for gaze control. Prerequisites: `@suaveplan/error`, `@suaveplan/types`.

View File

@@ -0,0 +1,51 @@
# Tasks: enterprise-gaming-ik-solver
## 0. Pre-flight (non-negotiable)
- [ ] 0.1 Confirm clean working tree (`git status` reports nothing) and no in-flight worktrees with uncommitted work (`git worktree list` then `git status` in each)
- [ ] 0.2 If any worktrees exist under `.claude/worktrees/`, account for each one: still-active agent, work-pending-merge, or stale-and-prune. Stale worktrees MUST be removed (`git worktree remove`) before opening a new one — code that has been lost twice on main is the failure mode this guards against.
- [ ] 0.3 Confirm the package set in `MANIFEST.md` matches `find packages -maxdepth 3 -name package.json` count. Drift here means a prior change archived without regenerating the manifest.
## Phase 1 — Scaffold
- [ ] Create `gaming/rigs/ik-solver/` (`@suaveplan/ik-solver`, `0.1.0`, `sideEffects: false`)
- [ ] `tsconfig.json`, `vitest.config.ts`
## Phase 2 — Error codes & constants
- [ ] `src/error-codes.ts` + `src/constants/index.ts` — `DEFAULT_MAX_ITERATIONS`, `DEFAULT_TOLERANCE`
## Phase 3 — Types
- [ ] `src/types/common.ts` + `common.md`
## Phase 4 — Vec3 math utils
- [ ] `src/utils/vec3-math.ts` — add/sub/scale/dot/cross/normalize/distance/lerp
- [ ] Tests + docs
## Phase 5 — Two-bone solver
- [ ] `src/solvers/two-bone/two-bone.ts` — law of cosines analytic solution + pole hint
- [ ] Tests (reachable/unreachable targets, degenerate chain)
- [ ] Docs
## Phase 6 — Look-at solver
- [ ] `src/solvers/look-at/look-at.ts` — quaternion from-to with up-vector and angle clamp
- [ ] Tests + docs
## Phase 7 — FABRIK solver
- [ ] `src/solvers/fabrik/fabrik.ts` — forward/backward pass, joint constraints, convergence check
- [ ] Tests + docs
## Phase 8 — Property tests
- [ ] `tests/property.test.ts` — two-bone reach, look-at alignment, FABRIK convergence
## Phase 9 — Pre-publish gate
- [ ] `bunx turbo lint typecheck test --filter=@suaveplan/ik-solver` green
- [ ] 100% coverage
- [ ] Publish: `bun scripts/publish.ts --package @suaveplan/ik-solver`