-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrender.test.ts
More file actions
1136 lines (1040 loc) · 40.3 KB
/
Copy pathrender.test.ts
File metadata and controls
1136 lines (1040 loc) · 40.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, it, expect } from "vitest";
import fs from "node:fs";
import * as THREE from "three";
import { buildMannequin } from "../src/mannequin.js";
import { buildTimeline } from "../src/timeline.js";
import { solveCCD } from "../src/ik.js";
import { poseFor } from "../src/poses.js";
import { buildProps } from "../src/props.js";
import { applyGroundLock, groundFigure } from "../src/groundlock.js";
import { PALM_LOCAL_NORMAL, formFists, levelPlantedFeet, wrapGrip } from "../src/contacts.js";
import { effectorBoneId, missingReachTarget, solveReachToPoint } from "../src/reach.js";
import { parse, eulerRomFor } from "posecode-parser";
const DEG = Math.PI / 180;
describe("mannequin", () => {
it("exposes all 27 named bones (incl. fingers)", () => {
const m = buildMannequin();
expect(m.bones.size).toBe(27);
for (const id of [
"pelvis",
"chest",
"elbow_left",
"knee_right",
"wrist_left",
"index_right",
"thumb_left",
]) {
expect(m.bones.has(id)).toBe(true);
}
});
it("declares hand and foot effector groups", () => {
const m = buildMannequin();
expect(m.effectors.hands).toEqual(["wrist_left", "wrist_right"]);
expect(m.effectors.hand_left).toEqual(["wrist_left"]);
expect(m.effectors.forearms).toEqual(["elbow_left", "elbow_right"]);
expect(m.effectors.feet).toEqual(["ankle_left", "ankle_right"]);
expect(m.effectors.foot_right).toEqual(["ankle_right"]);
expect(m.effectors.back).toEqual(["pelvis", "spine", "chest"]);
expect(m.effectors.fist_left).toEqual(["wrist_left"]);
expect(m.effectors.fists).toEqual(["wrist_left", "wrist_right"]);
expect(m.effectors.knees).toEqual(["knee_left", "knee_right"]);
expect(effectorBoneId("fist_right")).toBe("wrist_right");
expect(effectorBoneId("knee_left")).toBe("knee_left");
});
});
describe("timeline", () => {
it("starts standing poses with relaxed palms facing the thighs", () => {
const joints = poseFor("standing").joints!;
expect(joints.elbow_left).toEqual([0, -80, 0]);
expect(joints.elbow_right).toEqual([0, 80, 0]);
const m = buildMannequin();
for (const [boneId, rotation] of Object.entries(joints)) {
const [x, y, z] = rotation;
m.bones.get(boneId)!.rotation.set(x * DEG, y * DEG, z * DEG);
}
m.root.updateMatrixWorld(true);
const pelvis = m.bones.get("pelvis")!.getWorldPosition(new THREE.Vector3());
const expectPalmsInward = (): void => {
for (const side of ["left", "right"] as const) {
const wrist = m.bones.get(`wrist_${side}`)!;
const palm = new THREE.Vector3(...PALM_LOCAL_NORMAL)
.applyQuaternion(wrist.getWorldQuaternion(new THREE.Quaternion()))
.normalize();
const towardBody = pelvis.clone()
.sub(wrist.getWorldPosition(new THREE.Vector3()))
.normalize();
expect(palm.dot(towardBody)).toBeGreaterThan(0.8);
}
};
expectPalmsInward();
formFists(m, new Set(["left", "right"]));
expectPalmsInward(); // finger curl never changes palm facing
});
const PUSHUP = [
'posecode exercise "Push-up"',
" rig humanoid",
" pose start = plank",
' step "Lower" 2s ease-in:',
" elbows: flex 90",
' step "Press" 1s ease-out:',
" elbows: extend 0",
" repeat 5",
].join("\n");
it("omits a redundant wrap when the final phase returns to the base pose", () => {
const { ir } = parse(PUSHUP);
const tl = buildTimeline(ir!);
expect(tl.duration).toBeCloseTo(3, 5);
expect(tl.bonesUsed).toContain("elbow_left");
});
it("keeps a wrap segment when the final phase differs from the base pose", () => {
const src = [
'posecode exercise "Hold curl"',
" rig humanoid",
" pose start = standing",
' step "Curl" 1s settle:',
" elbows: flex 90",
" repeat 1",
].join("\n");
const { ir } = parse(src);
expect(buildTimeline(ir!).duration).toBeCloseTo(2, 5);
});
it("bends the elbow as the Lower phase progresses", () => {
const { ir } = parse(PUSHUP);
const tl = buildTimeline(ir!);
const m = buildMannequin();
tl.sample(0, m.bones);
const start = m.bones.get("elbow_left")!.quaternion.clone();
tl.sample(2, m.bones); // end of Lower
const lowered = m.bones.get("elbow_left")!.quaternion.clone();
expect(start.angleTo(lowered)).toBeGreaterThan(1.0); // ~90deg in radians
});
// Regression: a large rest-to-rest move (biceps curl: elbow flex 135 +
// supinate 80, near-antipodal endpoints) must sweep monotonically instead of
// lingering near rest and snapping to the target near the phase boundary.
it("curls the forearm monotonically from rest (no interpolation snap)", () => {
const CURL = [
'posecode exercise "Curl"',
" rig humanoid",
" pose start = standing",
' step "Curl" 1.1s settle:',
" elbows: flex 135",
" elbows: supinate 80",
' step "Lower" 1.4s drive:',
" elbows: flex 15",
" elbows: supinate 80",
" repeat 10",
].join("\n");
const { ir } = parse(CURL);
const tl = buildTimeline(ir!);
const m = buildMannequin();
const forearm = new THREE.Vector3(0, -1, 0); // wrist sits at elbow-local -Y
const wq = new THREE.Quaternion();
const dir = new THREE.Vector3();
let prev: THREE.Vector3 | null = null;
// Walk the 1.1s Curl segment in even steps. The forearm swings ~135deg
// total, so at this resolution each step is small; the overshoot bug instead
// parked the forearm near rest then jumped ~135deg in a single step. Assert
// no adjacent step exceeds 0.9rad (~50deg): the fix keeps every step under
// ~0.5rad, while the snap produced a ~2.3rad jump.
for (let t = 0; t <= 1.1 + 1e-9; t += 1.1 / 22) {
tl.sample(t, m.bones);
m.root.updateMatrixWorld(true);
m.bones.get("elbow_left")!.getWorldQuaternion(wq);
dir.copy(forearm).applyQuaternion(wq).normalize();
if (prev) expect(dir.angleTo(prev)).toBeLessThan(0.9);
prev = dir.clone();
}
});
it("follows a large reversing joint arc without hiding a full rotation", () => {
const REVERSAL = [
'posecode stretch "Shoulder abduction"',
" rig humanoid",
" pose start = standing",
' step "Raise" 2.5s flow:',
" shoulders: abduct 160",
' step "Lower" 2.5s settle:',
" shoulders: abduct 0",
" repeat 1",
].join("\n");
const { ir } = parse(REVERSAL);
const tl = buildTimeline(ir!);
const m = buildMannequin();
const euler = new THREE.Euler();
let previous = 0;
for (let t = 0; t <= 2.5 + 1e-9; t += 0.125) {
tl.sample(t, m.bones);
euler.setFromQuaternion(m.bones.get("shoulder_right")!.quaternion, "XYZ");
const angle = -euler.z / DEG;
expect(angle).toBeGreaterThanOrEqual(previous - 1e-4);
expect(angle - previous).toBeLessThan(15);
previous = angle;
}
expect(previous).toBeCloseTo(160, 4);
tl.sample(1.25, m.bones);
euler.setFromQuaternion(m.bones.get("shoulder_right")!.quaternion, "XYZ");
expect(-euler.z / DEG).toBeCloseTo(80, 1);
});
it("blends reach constraints across phase boundaries", () => {
const src = [
'posecode stretch "Cross-body reach"',
" rig humanoid",
" pose start = standing",
' step "Reach" 1s flow:',
" reach: hand_right shoulder_left",
' step "Return" 1s settle:',
" shoulder_right: flex 0",
" repeat 1",
].join("\n");
const { ir } = parse(src);
const tl = buildTimeline(ir!);
const m = buildMannequin();
expect(tl.sample(0, m.bones).reaches).toEqual([]);
expect(tl.sample(0.5, m.bones).reaches[0]?.weight).toBeCloseTo(0.5, 5);
expect(tl.sample(1, m.bones).reaches[0]?.weight).toBeCloseTo(1, 5);
expect(tl.sample(1.5, m.bones).reaches[0]?.weight).toBeCloseTo(0.25, 5);
expect(tl.sample(2 - 1e-5, m.bones).reaches).toEqual([]);
});
});
describe("hip-hinge coupling", () => {
const DEADLIFT = [
'posecode exercise "Hinge"',
" rig humanoid",
" pose start = standing",
' step "Lower" 2s ease-in-out:',
" pelvis: hinge 90",
" ground-lock: feet",
" repeat 2",
].join("\n");
it("keeps the thighs world-vertical when the pelvis hinges", () => {
const { ir } = parse(DEADLIFT);
const tl = buildTimeline(ir!);
const m = buildMannequin();
const root = m.root;
// Sample the bottom of the hinge (end of the 2s Lower phase).
tl.sample(2, m.bones);
root.updateMatrixWorld(true);
// The torso (chest) should have tipped forward toward horizontal: its rest
// down-axis (0,-1,0) pitches away from vertical, so |y| collapses toward 0.
const chestDir = new THREE.Vector3(0, -1, 0).applyQuaternion(
m.bones.get("chest")!.getWorldQuaternion(new THREE.Quaternion()),
);
expect(Math.abs(chestDir.y)).toBeLessThan(0.4);
// ...while the thigh (hip → knee) stays essentially vertical, because the
// renderer counter-rotates the hips against the pelvis hinge.
const hipPos = m.bones.get("hip_left")!.getWorldPosition(new THREE.Vector3());
const kneePos = m.bones
.get("knee_left")!
.getWorldPosition(new THREE.Vector3());
const thigh = kneePos.sub(hipPos).normalize();
expect(thigh.y).toBeLessThan(-0.95); // points almost straight down
});
});
describe("spatial choreography (turn & travel)", () => {
it("interpolates root yaw across a turn phase", () => {
const src = [
'posecode exercise "Half turn"',
" rig humanoid",
" pose start = standing",
' step "Spin" 1s linear:',
" turn: 180",
" ground-lock: feet",
" repeat 1",
].join("\n");
const { ir } = parse(src);
const tl = buildTimeline(ir!);
const m = buildMannequin();
// Midway through the 1s spin → ~90°; at the end → 180° (π radians).
expect(tl.sample(0.5, m.bones).rootYaw).toBeCloseTo(Math.PI / 2, 3);
expect(tl.sample(1, m.bones).rootYaw).toBeCloseTo(Math.PI, 3);
});
it("interpolates the root ground offset across a travel phase", () => {
const src = [
'posecode exercise "Step over"',
" rig humanoid",
" pose start = standing",
' step "Go" 1s linear:',
" travel: 0.5 -0.4",
" ground-lock: feet",
" repeat 1",
].join("\n");
const { ir } = parse(src);
const tl = buildTimeline(ir!);
const m = buildMannequin();
const end = tl.sample(1, m.bones).rootOffset;
expect(end.x).toBeCloseTo(0.5, 3);
expect(end.z).toBeCloseTo(-0.4, 3);
// Travel extent (for camera framing) is the largest offset magnitude.
expect(tl.travelExtent).toBeCloseTo(Math.hypot(0.5, 0.4), 3);
});
it("carries root velocity smoothly through flow travel waypoints", () => {
const src = [
'posecode exercise "Corner"',
" rig humanoid",
" pose start = standing",
' step "Across" 1s flow:',
" travel: 1 0",
' step "Forward" 1s flow:',
" travel: 1 1",
" repeat 1",
].join("\n");
const { ir } = parse(src);
const tl = buildTimeline(ir!);
const m = buildMannequin();
const eps = 1e-3;
const before = tl.sample(1 - eps, m.bones).rootOffset;
const at = tl.sample(1, m.bones).rootOffset;
const after = tl.sample(1 + eps, m.bones).rootOffset;
const velocityBefore = {
x: (at.x - before.x) / eps,
z: (at.z - before.z) / eps,
};
const velocityAfter = {
x: (after.x - at.x) / eps,
z: (after.z - at.z) / eps,
};
expect(at).toEqual({ x: 1, z: 0 });
expect(velocityBefore.x).toBeCloseTo(velocityAfter.x, 2);
expect(velocityBefore.z).toBeCloseTo(velocityAfter.z, 2);
});
it("leaves yaw and offset at home for movements that never turn/travel", () => {
const src = [
'posecode exercise "Curl"',
" rig humanoid",
' step "Up" 1s linear:',
" elbows: flex 90",
" repeat 1",
].join("\n");
const { ir } = parse(src);
const tl = buildTimeline(ir!);
const m = buildMannequin();
const info = tl.sample(1, m.bones);
expect(info.rootYaw).toBe(0);
expect(info.rootOffset).toEqual({ x: 0, z: 0 });
expect(tl.travelExtent).toBe(0);
});
});
describe("lying & seated poses", () => {
it("lays the supine torso horizontal", () => {
const spec = poseFor("supine");
expect(spec.root?.rotationDeg).toEqual([-90, 0, 0]);
const m = buildMannequin();
const [rx, ry, rz] = spec.root!.rotationDeg!;
m.root.rotation.set(rx * DEG, ry * DEG, rz * DEG);
m.root.updateMatrixWorld(true);
// The torso's long axis (chest local +Y) should be ~horizontal when lying.
const up = new THREE.Vector3(0, 1, 0).applyQuaternion(
m.bones.get("chest")!.getWorldQuaternion(new THREE.Quaternion()),
);
expect(Math.abs(up.y)).toBeLessThan(0.1);
});
it("grounds a lying figure with a bounding-box drop", () => {
const m = buildMannequin();
m.root.rotation.x = poseFor("prone").root!.rotationDeg![0] * DEG;
m.root.updateMatrixWorld(true);
// Mirror groundFigure(): drop so the lowest mesh point rests at y=0.
const box = new THREE.Box3().setFromObject(m.root);
m.root.position.y -= box.min.y;
m.root.updateMatrixWorld(true);
const grounded = new THREE.Box3().setFromObject(m.root);
expect(grounded.min.y).toBeCloseTo(0, 5);
});
});
describe("reach-IK", () => {
it("drives a wrist to a world point within the arm's reach", () => {
const m = buildMannequin();
m.root.updateMatrixWorld(true);
const wrist = m.bones.get("wrist_right")!;
const shoulder = m.bones.get("shoulder_right")!;
// A target ~0.47m from the shoulder (inside the ~0.54m arm reach) so the
// hand can actually arrive (a body landmark like a knee is out of range from
// a neutral stand, which is exactly why touch-toes hinges the torso first).
const target = shoulder
.getWorldPosition(new THREE.Vector3())
.add(new THREE.Vector3(0.3, -0.2, 0.3));
solveCCD(
{ joints: [shoulder, m.bones.get("elbow_right")!], effector: wrist, target },
20,
);
m.root.updateMatrixWorld(true);
const after = wrist.getWorldPosition(new THREE.Vector3()).distanceTo(target);
expect(after).toBeLessThan(0.06);
});
it("solves a canonical knee effector through the hip and reports residual", () => {
const m = buildMannequin();
m.root.updateMatrixWorld(true);
const hip = m.bones.get("hip_left")!;
const knee = m.bones.get("knee_left")!;
const hipPos = hip.getWorldPosition(new THREE.Vector3());
const radius = knee.getWorldPosition(new THREE.Vector3()).sub(hipPos);
const target = hipPos.clone().add(radius.applyAxisAngle(new THREE.Vector3(1, 0, 0), -0.2));
const kneeBefore = knee.quaternion.clone();
const residual = solveReachToPoint(m, "knee_left", "world", target);
expect(residual.reason).toBeUndefined();
expect(residual.distance).not.toBeNull();
expect(residual.distance!).toBeLessThan(0.04);
expect(residual.reached).toBe(true);
// The endpoint knee does not rotate itself; its hip is the reach chain.
expect(knee.quaternion.angleTo(kneeBefore)).toBeLessThan(1e-8);
expect(hip.quaternion.angleTo(new THREE.Quaternion())).toBeGreaterThan(0.05);
});
it("retains an unreachable fist residual and an unresolved target diagnostic", () => {
const m = buildMannequin();
m.root.updateMatrixWorld(true);
const far = new THREE.Vector3(5, 5, 5);
const residual = solveReachToPoint(m, "fist_right", "far", far);
expect(residual.distance).not.toBeNull();
expect(residual.distance!).toBeGreaterThan(5);
expect(residual.reached).toBe(false);
expect(missingReachTarget("fist_right", "missing_anchor")).toMatchObject({
effector: "fist_right",
target: "missing_anchor",
distance: null,
reached: false,
reason: "missing-target",
});
});
});
describe("hand rig", () => {
it("curls every finger on `fingers: flex`", () => {
const { ir, warnings } = parse(
[
'posecode posture "Fist"',
" rig humanoid",
' step "Close" 1s ease-out:',
" fingers: flex 80",
" repeat 1",
].join("\n"),
);
expect(warnings).toEqual([]);
const tl = buildTimeline(ir!);
const m = buildMannequin();
tl.sample(1, m.bones); // end of the Close phase
for (const id of ["index_left", "middle_right", "pinky_left", "thumb_right"]) {
const q = m.bones.get(id)!.quaternion;
expect(Math.abs(q.x) + Math.abs(q.y) + Math.abs(q.z)).toBeGreaterThan(0.1);
}
});
});
describe("ground-lock (shared solver)", () => {
// The solver extracted into groundlock.ts is what both the viewer and the
// headless eval harness call, so exercise it directly.
function posedRaw(source: string): ReturnType<typeof buildMannequin> {
const { ir } = parse(source);
const tl = buildTimeline(ir!);
const m = buildMannequin();
tl.sample(tl.segments[0]!.end - 1e-4, m.bones);
m.root.updateMatrixWorld(true);
return m;
}
it("keeps the canonical demi-plié turned out without foot friction", () => {
const source = fs.readFileSync(
new URL("../../../spec/examples/demi-plie.posecode", import.meta.url),
"utf8",
);
const { ir, errors } = parse(source);
expect(errors).toEqual([]);
const timeline = buildTimeline(ir!);
const m = buildMannequin();
expect(timeline.basePose.joints?.hip_left?.[1]).toBe(30);
expect(timeline.basePose.joints?.hip_right?.[1]).toBe(-30);
timeline.sample(0, m.bones);
groundFigure(m);
const basePosition = m.root.position.clone();
const baseRotation = m.root.quaternion.clone();
const anchors = new Map(
["ankle_left", "ankle_right"].map((id) => [
id,
m.bones.get(id)!.getWorldPosition(new THREE.Vector3()),
]),
);
let maxDrift = 0;
for (let t = 0; t < timeline.duration; t += 0.025) {
m.root.position.copy(basePosition);
m.root.quaternion.copy(baseRotation);
const info = timeline.sample(t, m.bones);
m.root.updateMatrixWorld(true);
applyGroundLock(m, info.groundLock, anchors);
levelPlantedFeet(m, info.groundLock);
m.root.updateMatrixWorld(true);
for (const id of ["ankle_left", "ankle_right"]) {
const point = m.bones.get(id)!.getWorldPosition(new THREE.Vector3());
const anchor = anchors.get(id)!;
maxDrift = Math.max(maxDrift, Math.hypot(point.x - anchor.x, point.z - anchor.z));
}
}
expect(maxDrift).toBeLessThan(0.01);
});
it("drops the body and plants the foot mesh for a feet-only squat", () => {
const m = posedRaw(
[
'posecode exercise "Squat"',
" rig humanoid",
" pose start = standing",
' step "Descend" 1s ease-in-out:',
" hips: flex 80",
" knees: flex 95",
" ground-lock: feet",
].join("\n"),
);
applyGroundLock(m, ["feet"]);
m.root.updateMatrixWorld(true);
// Pelvis well below standing rest (~0.95m): the body lowered into a squat.
const pelvisY = m.bones.get("pelvis")!.getWorldPosition(new THREE.Vector3()).y;
expect(pelvisY).toBeLessThan(0.85);
// Foot MESH sole rests on the floor (ankle bone rides ~0.04m above it).
const soleY = new THREE.Box3().setFromObject(m.bones.get("ankle_left")!).min.y;
expect(Math.abs(soleY)).toBeLessThan(0.01);
});
it("plants only the requested foot for a single-foot ground lock", () => {
const m = posedRaw(
[
'posecode exercise "One-leg balance"',
" rig humanoid",
" pose start = standing",
' step "Lift left" 1s linear:',
" hip_left: flex 55",
" knee_left: flex 75",
" ground-lock: foot_right",
].join("\n"),
);
applyGroundLock(m, ["foot_right"]);
m.root.updateMatrixWorld(true);
const rightSole = new THREE.Box3().setFromObject(m.bones.get("ankle_right")!).min.y;
const leftSole = new THREE.Box3().setFromObject(m.bones.get("ankle_left")!).min.y;
expect(Math.abs(rightSole)).toBeLessThan(0.01);
expect(leftSole).toBeGreaterThan(0.1);
});
it("normalizes and plants a human-readable single-foot lock", () => {
const source = [
'posecode exercise "Layup"',
" rig humanoid",
" pose start = standing",
' step "Plant" 1s settle:',
" knee_left: flex 86",
" hip_left: flex 62",
" ground-lock: left foot",
].join("\n");
const { ir, errors } = parse(source);
expect(errors).toEqual([]);
expect(ir?.phases[0]?.groundLock).toEqual(["foot_left"]);
const m = posedRaw(source);
applyGroundLock(m, ir!.phases[0]!.groundLock);
const soleY = new THREE.Box3().setFromObject(m.bones.get("ankle_left")!).min.y;
expect(Math.abs(soleY)).toBeLessThan(0.01);
});
it("plants the torso surface for a supine back lock", () => {
const m = buildMannequin();
const spec = poseFor("supine");
m.root.position.set(...spec.root!.position!);
const [rx, ry, rz] = spec.root!.rotationDeg!;
m.root.rotation.set(rx * DEG, ry * DEG, rz * DEG);
// Put the limbs into an asymmetric dead-bug phase before solving contact.
m.bones.get("shoulder_right")!.rotation.x = -150 * DEG;
m.bones.get("hip_left")!.rotation.x = -20 * DEG;
m.bones.get("knee_left")!.rotation.x = 5 * DEG;
m.root.updateMatrixWorld(true);
applyGroundLock(m, ["back"]);
// Measure meshes owned by the pelvis/spine/chest only. Descendant limbs
// are intentionally excluded: moving an arm must not lift the back.
const boneNodes = new Set(m.bones.values());
const backBox = new THREE.Box3();
for (const id of ["pelvis", "spine", "chest"]) {
for (const child of m.bones.get(id)!.children) {
if (!boneNodes.has(child)) backBox.union(new THREE.Box3().setFromObject(child));
}
}
expect(backBox.min.y).toBeCloseTo(0, 3);
});
it("is a no-op when no effectors are ground-locked", () => {
const m = posedRaw(
[
'posecode posture "Reach"',
" rig humanoid",
' step "Up" 1s linear:',
" shoulders: flex 90",
].join("\n"),
);
const before = m.root.position.clone();
applyGroundLock(m, []);
expect(m.root.position.equals(before)).toBe(true);
});
it("groundFigure drops the lowest mesh point onto the floor", () => {
const m = posedRaw(
[
'posecode posture "Stand"',
" rig humanoid",
" pose start = standing",
' step "Hold" 1s linear:',
" spine: hold neutral",
].join("\n"),
);
m.root.position.y += 0.5; // lift off the floor
m.root.updateMatrixWorld(true);
groundFigure(m);
m.root.updateMatrixWorld(true);
expect(new THREE.Box3().setFromObject(m.root).min.y).toBeCloseTo(0, 2);
});
});
describe("contact pins", () => {
// Replicate the viewer's applyPins: translate the root so the pinned effector
// sits on the anchor, then read the pelvis height.
const BONE: Record<string, string> = {
hand_left: "wrist_left",
hand_right: "wrist_right",
foot_left: "ankle_left",
foot_right: "ankle_right",
};
function pelvisYPinned(
source: string,
t: number,
anchors: Map<string, THREE.Vector3>,
): number {
const { ir } = parse(source);
const tl = buildTimeline(ir!);
const m = buildMannequin();
const info = tl.sample(t, m.bones);
m.root.updateMatrixWorld(true);
const delta = new THREE.Vector3();
let n = 0;
for (const p of info.pins) {
const eff = m.bones.get(BONE[p.effector] ?? p.effector);
const a = anchors.get(p.anchor);
if (!eff || !a) continue;
delta.add(a.clone().sub(eff.getWorldPosition(new THREE.Vector3())));
n++;
}
if (n > 0) {
m.root.position.add(delta.multiplyScalar(1 / n));
m.root.updateMatrixWorld(true);
}
return m.bones.get("pelvis")!.getWorldPosition(new THREE.Vector3()).y;
}
const PULLUP = [
'posecode exercise "Pull-up"',
" rig humanoid",
" prop bar",
" pose start = standing",
' step "Hang" 1.5s ease-in-out:',
" shoulders: flex 175",
" elbows: flex 5",
" pin: hands bar",
' step "Pull up" 1.2s ease-out:',
" shoulders: flex 150",
" elbows: flex 130",
" pin: hands bar",
" repeat 2",
].join("\n");
it("parses pins into the IR", () => {
const { ir, errors, warnings } = parse(PULLUP);
expect(errors).toEqual([]);
expect(warnings).toEqual([]);
expect(ir!.props).toContain("bar");
expect(ir!.phases[0]!.pins).toEqual([
{ effector: "hand_left", anchor: "bar" },
{ effector: "hand_right", anchor: "bar" },
]);
});
it("raises the body when pinned to the bar and the elbows flex", () => {
const anchors = buildProps(["bar"]).anchors;
// Sample inside each phase (not on the boundary, where sample() returns the
// next keyframe's empty pins).
const hang = pelvisYPinned(PULLUP, 1.49, anchors); // straight-arm hang
const top = pelvisYPinned(PULLUP, 2.69, anchors); // elbows flexed → pulled up
expect(top).toBeGreaterThan(hang + 0.2); // pelvis climbs toward the bar
});
});
describe("props", () => {
it("exposes named anchors for declared props", () => {
const { anchors, group } = buildProps(["chair", "bar", "wall"]);
expect(anchors.has("seat")).toBe(true);
expect(anchors.has("bar")).toBe(true);
expect(anchors.has("wall")).toBe(true);
expect(anchors.get("bar")!.y).toBeGreaterThan(1.5); // overhead
expect(group.children.length).toBeGreaterThan(0);
});
});
describe("ccd ik", () => {
it("brings an effector close to its target", () => {
const root = new THREE.Object3D();
const j0 = new THREE.Object3D();
const j1 = new THREE.Object3D();
const effector = new THREE.Object3D();
root.add(j0);
j0.add(j1);
j1.position.set(0, 1, 0);
j1.add(effector);
effector.position.set(0, 1, 0);
root.updateMatrixWorld(true);
const target = new THREE.Vector3(1.4, 0.6, 0);
const distSq = solveCCD({ joints: [j0, j1], effector, target }, 30);
expect(Math.sqrt(distSq)).toBeLessThan(0.05);
});
});
describe("ROM-constrained reach-IK", () => {
// The viewer's radian limit boxes, minus the current-pose widening (the rig
// starts at rest here, which is inside every box).
function limitsFor(id: string) {
const rom = eulerRomFor(id);
if (!rom) return null;
const r = (d: number) => d * DEG;
return {
x: [r(rom.x.min), r(rom.x.max)] as [number, number],
y: [r(rom.y.min), r(rom.y.max)] as [number, number],
z: [r(rom.z.min), r(rom.z.max)] as [number, number],
};
}
function armChain(m: ReturnType<typeof buildMannequin>) {
const shoulder = m.bones.get("shoulder_right")!;
const elbow = m.bones.get("elbow_right")!;
return {
joints: [shoulder, elbow],
limits: [limitsFor("shoulder_right"), limitsFor("elbow_right")],
effector: m.bones.get("wrist_right")!,
};
}
it("still converges on a reachable in-front target", () => {
const m = buildMannequin();
m.root.updateMatrixWorld(true);
const { joints, limits, effector } = armChain(m);
const target = joints[0]!
.getWorldPosition(new THREE.Vector3())
.add(new THREE.Vector3(0.3, -0.2, 0.3));
solveCCD({ joints, limits, effector, target }, 20);
m.root.updateMatrixWorld(true);
expect(
effector.getWorldPosition(new THREE.Vector3()).distanceTo(target),
).toBeLessThan(0.06);
});
it("never pushes a chain joint past its ROM chasing an unsafe target", () => {
const m = buildMannequin();
m.root.updateMatrixWorld(true);
const { joints, limits, effector } = armChain(m);
const [shoulder, elbow] = joints;
// High behind the back (figure faces +Z): reaching it would demand
// shoulder extension far past the configured 60° ceiling.
const target = shoulder!
.getWorldPosition(new THREE.Vector3())
.add(new THREE.Vector3(0, 0.3, -0.5));
solveCCD({ joints, limits, effector, target }, 30);
m.root.updateMatrixWorld(true);
const eps = 1e-6;
for (const [joint, lim] of [
[shoulder!, limits[0]!],
[elbow!, limits[1]!],
] as const) {
const e = new THREE.Euler().setFromQuaternion(joint.quaternion, "XYZ");
expect(e.x).toBeGreaterThanOrEqual(lim.x[0] - eps);
expect(e.x).toBeLessThanOrEqual(lim.x[1] + eps);
expect(e.y).toBeGreaterThanOrEqual(lim.y[0] - eps);
expect(e.y).toBeLessThanOrEqual(lim.y[1] + eps);
expect(e.z).toBeGreaterThanOrEqual(lim.z[0] - eps);
expect(e.z).toBeLessThanOrEqual(lim.z[1] + eps);
}
// Sanity: unconstrained CCD DOES violate the shoulder ceiling here, so the
// clamp above is load-bearing, not vacuous.
const m2 = buildMannequin();
m2.root.updateMatrixWorld(true);
const free = armChain(m2);
const target2 = free.joints[0]!
.getWorldPosition(new THREE.Vector3())
.add(new THREE.Vector3(0, 0.3, -0.5));
solveCCD({ joints: free.joints, effector: free.effector, target: target2 }, 30);
const eFree = new THREE.Euler().setFromQuaternion(
free.joints[0]!.quaternion,
"XYZ",
);
expect(eFree.x).toBeGreaterThan(60 * DEG + 0.05); // past configured extension
});
it("keeps a knee hinge-only: no lateral splay while a foot reaches", () => {
const m = buildMannequin();
m.root.updateMatrixWorld(true);
const hip = m.bones.get("hip_right")!;
const knee = m.bones.get("knee_right")!;
const ankle = m.bones.get("ankle_right")!;
// A target out to the side tempts unconstrained CCD to twist the knee
// sideways; the ROM box zeroes the knee's Y/Z axes.
const target = hip
.getWorldPosition(new THREE.Vector3())
.add(new THREE.Vector3(-0.5, -0.3, 0.2));
solveCCD(
{
joints: [hip, knee],
limits: [limitsFor("hip_right"), limitsFor("knee_right")],
effector: ankle,
target,
},
30,
);
const e = new THREE.Euler().setFromQuaternion(knee.quaternion, "XYZ");
expect(Math.abs(e.y)).toBeLessThan(1e-6);
expect(Math.abs(e.z)).toBeLessThan(1e-6);
});
});
describe("frontal plane direction", () => {
it("abduction carries arms and legs AWAY from the midline on both sides", () => {
const { ir, errors } = parse(
[
'posecode exercise "Open"',
" rig humanoid",
" pose start = standing",
' step "Open" 1s linear:',
" shoulders: abduct 80",
" hips: abduct 30",
" repeat 1",
].join("\n"),
);
expect(errors).toEqual([]);
const tl = buildTimeline(ir!);
const m = buildMannequin();
tl.sample(1, m.bones);
m.root.updateMatrixWorld(true);
// Each distal joint must sit FURTHER from the midline (|x|) than its
// proximal joint, on the SAME side: the inverted sign swung all four
// limbs across the body instead.
for (const [distal, proximal] of [
["wrist_left", "shoulder_left"],
["wrist_right", "shoulder_right"],
["ankle_left", "hip_left"],
["ankle_right", "hip_right"],
] as const) {
const d = m.bones.get(distal)!.getWorldPosition(new THREE.Vector3());
const p = m.bones.get(proximal)!.getWorldPosition(new THREE.Vector3());
expect(Math.sign(d.x)).toBe(Math.sign(p.x));
expect(Math.abs(d.x)).toBeGreaterThan(Math.abs(p.x));
}
});
});
describe("face marker", () => {
it("marks the head's front (+Z) so facing and neck turns are readable", () => {
const m = buildMannequin();
const face = m.bones.get("head")!.getObjectByName("face");
expect(face).toBeTruthy();
expect(face!.children.length).toBeGreaterThanOrEqual(3); // nose + two eyes
// Every face element sits on the figure's front side of the head ball.
for (const part of face!.children) {
expect(part.position.z).toBeGreaterThan(0.04);
}
});
});
describe("hand rig articulation", () => {
it("finger bones carry their own digit meshes, so curls are visible", () => {
const m = buildMannequin();
m.root.updateMatrixWorld(true);
const index = m.bones.get("index_right")!;
expect(index.children.length).toBeGreaterThan(0); // the digit capsule
// Curl the finger 90° and check the digit's far end actually moves.
const digit = index.children[0]!;
const tipBefore = digit.getWorldPosition(new THREE.Vector3());
index.rotation.x = -90 * DEG;
m.root.updateMatrixWorld(true);
const tipAfter = digit.getWorldPosition(new THREE.Vector3());
expect(tipBefore.distanceTo(tipAfter)).toBeGreaterThan(0.015);
});
});
describe("dip bars prop", () => {
it("exposes a `bars` grip anchor at support height", () => {
const { anchors, group } = buildProps(["dip-bars"]);
expect(anchors.has("bars")).toBe(true);
const grip = anchors.get("bars")!;
expect(grip.y).toBeGreaterThan(0.9); // high enough that feet clear the floor
expect(grip.y).toBeLessThan(1.6); // but well below the pull-up bar
expect(group.children.length).toBeGreaterThan(0);
expect(anchors.get("bars_left")!.x).toBeGreaterThan(0);
expect(anchors.get("bars_right")!.x).toBeLessThan(0);
expect(anchors.get("bars_left")!.y).toBeCloseTo(grip.y, 6);
expect(anchors.get("bars_left")!.distanceTo(anchors.get("bars_right")!)).toBeGreaterThan(0.4);
});
// Same root-translation logic as the viewer's applyPins (see contact pins).
function pelvisYAt(source: string, t: number, anchors: Map<string, THREE.Vector3>): number {
const BONE: Record<string, string> = {
hand_left: "wrist_left",
hand_right: "wrist_right",
foot_left: "ankle_left",
foot_right: "ankle_right",
};
const { ir } = parse(source);
const tl = buildTimeline(ir!);
const m = buildMannequin();
const info = tl.sample(t, m.bones);
m.root.updateMatrixWorld(true);
const delta = new THREE.Vector3();
let n = 0;
for (const pin of info.pins) {
const eff = m.bones.get(BONE[pin.effector] ?? pin.effector);
const a = anchors.get(pin.anchor);
if (!eff || !a) continue;
delta.add(a.clone().sub(eff.getWorldPosition(new THREE.Vector3())));
n++;
}
if (n > 0) {
m.root.position.add(delta.multiplyScalar(1 / n));
m.root.updateMatrixWorld(true);
}
return m.bones.get("pelvis")!.getWorldPosition(new THREE.Vector3()).y;
}
it("lowers the body between the bars as the elbows flex (dip bottom)", () => {
const dips = [
'posecode exercise "Dips"',
" rig humanoid",
" prop dip-bars",
" pose start = standing",
' step "Support" 1s ease-out:',
" elbows: flex 5",
" knees: flex 70",
" pin: hands bars",
' step "Lower" 1s ease-in-out:',
" shoulders: extend 30",
" elbows: flex 90",
" knees: flex 70",
" pin: hands bars",
" repeat 4",
].join("\n");
const anchors = buildProps(["dip-bars"]).anchors;
const support = pelvisYAt(dips, 0.99, anchors);
const bottom = pelvisYAt(dips, 1.99, anchors);
expect(support).toBeGreaterThan(0.9); // hips held up at bar height
expect(bottom).toBeLessThan(support - 0.08); // body sinks into the dip
});
});
describe("cobra", () => {
it("arches the chest and head up off the floor during the Lift", () => {
const src = [
'posecode stretch "Cobra"',
" rig humanoid",
" pose start = prone",
' step "Lift" 2.5s ease-in-out:',
" spine: extend 30",
" chest: extend 15",
" neck: extend 25",
" shoulders: flex 50",
" elbows: flex 25",
" reach: hands floor",
" repeat 1",
].join("\n");