fix(web): robot rig places bones on the URDF chain and keeps bound worlds
Some checks failed
M6 deployable RC / quick (push) Has been cancelled
M6 deployable RC / chromium (push) Has been cancelled
M6 deployable RC / release (push) Has been cancelled

The rig step placed every bone at its parent-link-relative URDF offset and
the engine's keep_transform world recursion treated already-bound ancestors
as plain PAROBJECT, so binding in chain order dropped the ancestor bone
matrix and every link jumped on the rig click (deltas up to 1.26).

- set_parent: refresh the pose before computing the world and compose
  PARBONE ancestors like BKE_object_calc_parent/ob_parbone.
- robot-workflow rig: accumulate URDF joint frames from rootLink (fixed
  joints included) and place head/axis on the real joint position.
- robot-rig-web now asserts zero world drift, bone pivots on the URDF
  joints, bone Y on the joint axes, joint_3 rotation moving only its
  subtree about the true axis, and 恢复零位 restoring every link.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
mes123456
2026-11-29 19:20:27 +08:00
parent 8f08c1bda9
commit 013ea78a45
7 changed files with 133 additions and 40 deletions

View File

@@ -227,6 +227,11 @@ test('joint_1 pose moves the link chain and 恢复零位 restores it [robot-pose
* link to it (desktop ob_parbone semantics). The check runs on the engine
* snapshot, never on the tool's own report. */
const EXPECTED_BONES: Record<string, string> = { link_1: 'joint_1', link_2: 'joint_2', link_3: 'joint_3', link_4: 'joint_4', link_5: 'joint_5', link_6: 'joint_6' };
/* URDF joint axes in the sample are expressed in the joint (parent link) frame;
* every joint rpy is zero, so the axis equals the armature-space bone Y. */
const URDF_AXIS: Record<string, number[]> = { joint_1: [0, 0, 1], joint_2: [0, 1, 0], joint_3: [0, 1, 0], joint_4: [1, 0, 0], joint_5: [0, 1, 0], joint_6: [1, 0, 0] };
const translation = (matrix: number[] | null | undefined) => (matrix && matrix.length === 16 ? [matrix[12], matrix[13], matrix[14]] : null);
const matrixMaxDelta = (a: number[] | null | undefined, b: number[] | null | undefined) => (a && b && a.length === b.length ? Math.max(...a.map((value, index) => Math.abs(value - b[index]))) : Number.POSITIVE_INFINITY);
test('establishing the armature binds every link to its joint bone [robot-rig-web]', async ({ page }, testInfo) => {
const errors = consoleCollector(page);
@@ -241,39 +246,95 @@ test('establishing the armature binds every link to its joint bone [robot-rig-we
await expect(dialog).toContainText('骨架已建立(6 骨骼 + 刚性绑定)', { timeout: 180_000 });
const rigged = await readAfterRevision(page, before.revision ?? 0, [ROOT_OBJECT, ...LINKS]);
const bones = (rigged.armatures ?? []).flatMap((armature) => armature.bones).map((bone) => bone.name).sort();
const riggedBones = (rigged.armatures ?? []).flatMap((armature) => armature.bones);
const bones = riggedBones.map((bone) => bone.name).sort();
const boneByName = (list: typeof riggedBones, name: string) => list.find((bone) => bone.name === name) ?? null;
const bound = Object.fromEntries(Object.entries(rigged.nodes ?? {})
.filter(([, node]) => node.parentType === 'BONE')
.map(([name, node]) => [name, node.parentBoneName]));
const worldDelta = Object.fromEntries(Object.keys(EXPECTED_BONES).map((link) => [link,
matrixDistance(before.nodes?.[link]?.worldMatrix ?? null, rigged.nodes?.[link]?.worldMatrix ?? null)]));
/* A bone pivot must sit on the true URDF joint position: the chain-accumulated
* origin of the joint that drives it, which for this sample equals the child
* link's pre-rig world translation. */
const boneHeadDelta = Object.fromEntries(Object.entries(EXPECTED_BONES).map(([link, bone]) => [bone,
matrixMaxDelta(translation(boneByName(riggedBones, bone)?.restMatrix), translation(before.nodes?.[link]?.worldMatrix))]));
const boneAxis = Object.fromEntries(riggedBones.map((bone) => [bone.name,
bone.restMatrix ? [bone.restMatrix[4], bone.restMatrix[5], bone.restMatrix[6]].map((value) => Math.round(value * 1e6) / 1e6) : null]));
const boneAxisDelta = Object.fromEntries(Object.entries(URDF_AXIS).map(([bone, axis]) => [bone,
boneAxis[bone] ? Math.max(...axis.map((value, index) => Math.abs(value - boneAxis[bone]![index]))) : Number.POSITIVE_INFINITY]));
/* 调整关节角度只围绕该关节:pose joint_3 and read the subtree back. */
const rigRevision = rigged.revision ?? 0;
await dialog.getByLabel('joint_3 角度', { exact: true }).fill('30');
await dialog.getByRole('button', { name: '应用关节角度', exact: true }).click();
const posed = await readAfterRevision(page, rigRevision, [ROOT_OBJECT, ...LINKS]);
const posedBones = (posed.armatures ?? []).flatMap((armature) => armature.bones);
const posedDelta = Object.fromEntries(Object.keys(EXPECTED_BONES).map((link) => [link,
matrixDistance(rigged.nodes?.[link]?.worldMatrix ?? null, posed.nodes?.[link]?.worldMatrix ?? null)]));
const joint3Rest = translation(boneByName(riggedBones, 'joint_3')?.restMatrix);
const joint3Posed = translation(boneByName(posedBones, 'joint_3')?.poseMatrix);
const joint5Posed = translation(boneByName(posedBones, 'joint_5')?.poseMatrix);
const joint5Rest = translation(boneByName(riggedBones, 'joint_5')?.restMatrix);
/* joint_5 is a child of joint_3, so it must ride the circle of constant radius
* about joint_3's Y axis while joint_3's own pivot stays put. */
const radius = (point: number[] | null, center: number[] | null) => (point && center ? Math.hypot(point[0] - center[0], point[2] - center[2]) : Number.POSITIVE_INFINITY);
const joint5Radius = { rest: radius(joint5Rest, joint3Rest), posed: radius(joint5Posed, joint3Posed) };
const joint3X = boneByName(posedBones, 'joint_3')?.poseMatrix;
const posedXAxis = joint3X ? [joint3X[0], joint3X[1], joint3X[2]] : null;
const thirty = Math.PI / 6;
await dialog.getByRole('button', { name: '恢复零位', exact: true }).click();
const restored = await readAfterRevision(page, posed.revision ?? rigRevision, [ROOT_OBJECT, ...LINKS]);
const restoreDelta = Object.fromEntries(Object.keys(EXPECTED_BONES).map((link) => [link,
matrixDistance(rigged.nodes?.[link]?.worldMatrix ?? null, restored.nodes?.[link]?.worldMatrix ?? null)]));
const evidence = {
schemaVersion: 1,
case: 'robot-rig-web',
candidate: CANDIDATE,
revisions: { before: before.revision ?? null, rigged: rigged.revision ?? null },
revisions: { before: before.revision ?? null, rigged: rigged.revision ?? null, posed: posed.revision ?? null, restored: restored.revision ?? null },
bones,
bound,
worldDelta,
boneHeadDelta,
boneAxis,
boneAxisDelta,
matrices: Object.fromEntries(Object.keys(EXPECTED_BONES).map((link) => [link, { before: before.nodes?.[link]?.worldMatrix ?? null, rigged: rigged.nodes?.[link]?.worldMatrix ?? null, boundMatrix: rigged.nodes?.[link]?.parentBoneMatrix ?? null }])),
bonePose: Object.fromEntries((rigged.armatures ?? []).flatMap((armature) => armature.bones).map((bone) => [bone.name, { rest: bone.restMatrix, pose: bone.poseMatrix }])),
bonePose: Object.fromEntries(riggedBones.map((bone) => [bone.name, { rest: bone.restMatrix, pose: bone.poseMatrix }])),
jointPose: {
joint: 'joint_3', degrees: 30,
worldDelta: posedDelta,
joint3Pivot: { rest: joint3Rest, posed: joint3Posed },
joint5Head: { rest: joint5Rest, posed: joint5Posed },
joint5Radius,
posedXAxis,
restoreDelta,
},
rawNodes: Object.fromEntries(Object.entries(rigged.nodes ?? {}).filter(([, node]) => node.parentType === 'BONE' || node.type === 'ARMATURE')),
console: { productErrors: productConsole(errors), rawErrorCount: errors.length },
residuals: {
worldPreservation: {
status: 'deferred',
note: 'rig 阶段骨骼父级的世界保持偏差归入机器人逆解一并处理;本用例只判定 URDF 层级与装配绑定关系',
deltas: worldDelta,
},
},
acceptance: 'NOT_CLAIMED',
};
await attachEvidence(testInfo, 'robot-rig-web', evidence);
expect(bones).toEqual([...REVOLUTE].sort());
for (const [link, bone] of Object.entries(EXPECTED_BONES)) expect(bound[link]).toBe(bone);
/* World preservation across the rig step is recorded in evidence (residuals)
* and handled together with robot inverse kinematics; this case asserts the
* URDF hierarchy and assembly binding only. */
for (const [link, bone] of Object.entries(EXPECTED_BONES)) {
expect(bound[link]).toBe(bone);
/* Rigid binding keeps the rendered world: three.js only sees the world
* matrix, so any drift here is a visible jump on the rig click. */
expect(worldDelta[link], `${link} world preserved`).toBeLessThan(1e-5);
expect(boneHeadDelta[bone], `${bone} pivot on the URDF joint`).toBeLessThan(1e-5);
expect(boneAxisDelta[bone], `${bone} Y along the URDF axis`).toBeLessThan(1e-5);
}
/* Only the joint_3 subtree moves; upstream links stay bit-identical. */
expect(posedDelta.link_1).toBeLessThan(1e-9);
expect(posedDelta.link_2).toBeLessThan(1e-9);
for (const link of ['link_3', 'link_4', 'link_5', 'link_6']) expect(posedDelta[link], `${link} follows joint_3`).toBeGreaterThan(0.01);
expect(matrixMaxDelta(joint3Posed, joint3Rest), 'joint_3 pivot does not translate').toBeLessThan(1e-6);
expect(Math.abs(joint5Radius.posed - joint5Radius.rest), 'joint_5 rides the joint_3 circle').toBeLessThan(1e-3);
expect(Math.abs(Math.abs(posedXAxis![2]) - Math.sin(thirty)), 'rotation magnitude about the joint axis').toBeLessThan(1e-3);
expect(Math.abs(posedXAxis![0] - Math.cos(thirty)), 'rotation magnitude about the joint axis').toBeLessThan(1e-3);
expect(Math.abs(posedXAxis![1])).toBeLessThan(1e-6);
for (const link of Object.keys(EXPECTED_BONES)) expect(restoreDelta[link], `${link} restored`).toBeLessThan(1e-6);
expect(productConsole(errors)).toEqual([]);
});