Arm solve: least-violating swivel, clamped, when none is feasible; continuity test across the feasible edge
The zero-twist clamp fallback jumped the shoulder target up to 1.4 rad as a goal left the feasible set. Now the swivel minimises swing past the cone plus twist past its range (in double, golden section over a 36-step grid), and swing and twist clamp to the limits: at the edge that is the last feasible swivel. The target is linear in the swivel half-angle, so the scan reads cos(phi/2) A + sin(phi/2) B, and the least swing over the swivel is a 2x2 eigenvalue that skips the feasible scan when nothing fits the cone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c9b2345eba
commit
e8c6d7e5fb
3 changed files with 204 additions and 27 deletions
|
|
@ -255,8 +255,10 @@ muscle layer and the will mix and graft mask see IK like any other layer. Arms a
|
|||
so the analytic solve is exact; FABRIK and CCD wait for graft chains that are not two-bone. The elbow swivel is
|
||||
the one needing no shoulder twist, because the shoulder's twist muscle tracks a twist weakly (fact 7); what this
|
||||
gives up is an authored elbow direction. Where that swivel leaves the cone, the swivel is the nearest one inside
|
||||
the cone and the twist limits, the feasible swivel range of Tolani, Goswami and Badler (2000); the swing clamps
|
||||
only when no swivel is feasible (fact 8). Reach under gravity lands short by PD sag (fact 9); that is its own cut.
|
||||
the cone and the twist limits, the feasible swivel range of Tolani, Goswami and Badler (2000) (fact 8). When no
|
||||
swivel is feasible, the swivel is the one whose limit violation (swing past the cone plus twist past its range) is
|
||||
least, with swing and twist clamped to the limits: at the edge of the feasible set that is the last feasible swivel,
|
||||
so a goal crossing the edge moves the targets continuously (the zero-twist clamp it replaces jumped 1.4 rad there). Reach under gravity lands short by PD sag (fact 9); that is its own cut.
|
||||
|
||||
**Goal-target rationale.** A target is tracked by percept Id (fact 11), selection scores (goal, percept) pairs,
|
||||
and a retarget inside the goal waits the deliberate latency but not the dwell, held only by the commitment
|
||||
|
|
|
|||
|
|
@ -88,17 +88,26 @@ namespace Lodur
|
|||
/// point for the wrist anchor: the elbow flex is fixed by the distance, and the swivel about the shoulder-to-wrist line is the one that
|
||||
/// leaves the shoulder target no twist, the shoulder's twist muscle being weak. When that swivel puts the swing past the cone, the swivel
|
||||
/// is the one nearest it whose swing is inside the cone and whose twist is inside the twist limits (the joint-limit-feasible swivel range of
|
||||
/// Tolani, Goswami and Badler 2000); only when no swivel is feasible does the swing clamp to the cone.
|
||||
/// Tolani, Goswami and Badler 2000). When no swivel is feasible, the swivel is the one whose limit violation (swing past the cone plus
|
||||
/// twist past its limits) is least, and its swing and twist clamp to the limits; as a goal leaves the feasible set that swivel is the
|
||||
/// last feasible one, so the targets move continuously across the edge.
|
||||
/// </summary>
|
||||
public sealed class ArmChain
|
||||
{
|
||||
private readonly Quat _parentRestInverse, _frame, _frameInverse;
|
||||
private readonly IkVec _parentCentre, _shoulderAnchor, _upper, _lower, _elbowAxis, _twistAxis;
|
||||
private readonly float _flexSign, _flexLo, _flexHi, _cone, _twistLo, _twistHi, _coneSin2;
|
||||
private readonly float _flexSign, _flexLo, _flexHi, _cone, _twistLo, _twistHi, _coneSin2, _coneSinHalf, _coneCosHalf;
|
||||
|
||||
// Swivel search: steps of Pi / SwivelSteps outward from the zero-twist swivel, then bisection to the feasible boundary.
|
||||
// Swivel search: steps of Pi / SwivelSteps outward from the zero-twist swivel, then bisection to the feasible boundary. When none is
|
||||
// feasible, the least-violating of LeastSteps steps round the circle, refined by golden section.
|
||||
private const int SwivelSteps = 90;
|
||||
private const int SwivelBisections = 16;
|
||||
private const int LeastSteps = 36;
|
||||
private const int LeastRefinements = 28;
|
||||
|
||||
// Among swivels that violate the limits equally (the arm along the twist axis, where the swivel only twists) the least twist wins.
|
||||
// Small against the violation's slope of about one, so it barely moves the least-violating swivel.
|
||||
private const double TwistPreference = 1e-5;
|
||||
|
||||
private ArmChain(BodyPlan plan, Side side)
|
||||
{
|
||||
|
|
@ -140,8 +149,9 @@ namespace Lodur
|
|||
_cone = s.Cone;
|
||||
_twistLo = s.Lower;
|
||||
_twistHi = s.Upper;
|
||||
var half = MathF.Sin(0.5f * _cone);
|
||||
_coneSin2 = half * half;
|
||||
_coneSinHalf = MathF.Sin(0.5f * _cone);
|
||||
_coneCosHalf = MathF.Cos(0.5f * _cone);
|
||||
_coneSin2 = _coneSinHalf * _coneSinHalf;
|
||||
_shoulderAnchor = shoulder;
|
||||
_frame = Ik.JointFrame(s);
|
||||
_frameInverse = _frame.Conjugate();
|
||||
|
|
@ -174,7 +184,8 @@ namespace Lodur
|
|||
|
||||
/// <summary>
|
||||
/// Targets that put the wrist anchor on the world point (tx, ty, tz), given the parent part's world rotation and position.
|
||||
/// Returns false when the point was out of reach or a limit clamped; the targets are then the nearest the chain can go.
|
||||
/// Returns false when the point was out of reach or a limit clamped; the targets are then the limits-clamped pose of the swivel that
|
||||
/// violates the limits least, continuous with the reached targets at the edge of the feasible set.
|
||||
/// </summary>
|
||||
public bool Solve(in Quat parentRotation, float px, float py, float pz, float tx, float ty, float tz, out Quat shoulder, out Quat elbow)
|
||||
{
|
||||
|
|
@ -199,13 +210,14 @@ namespace Lodur
|
|||
var num = -q.Dot(_twistAxis);
|
||||
var den = arc.W * vHat.Dot(_twistAxis) + vHat.Cross(q).Dot(_twistAxis);
|
||||
var zeroTwist = 2f * MathF.Atan2(num, den);
|
||||
var local = Local(vHat, arc, zeroTwist);
|
||||
if (!InsideCone(local) && !NearestFeasible(vHat, arc, zeroTwist, out local))
|
||||
// The target is linear in the half-angle of the swivel: Local(phi) = cos(phi / 2) A + sin(phi / 2) B.
|
||||
var a = _frameInverse * arc * _frame;
|
||||
var b = _frameInverse * (new Quat(vHat.X, vHat.Y, vHat.Z, 0f) * arc) * _frame;
|
||||
var local = Local(a, b, zeroTwist);
|
||||
if (!InsideCone(local) && !(SomeSwivelInsideCone(a, b) && NearestFeasible(a, b, zeroTwist, out local)))
|
||||
{
|
||||
// No swivel is feasible: the zero-twist swing, clamped to the cone.
|
||||
local = Local(vHat, arc, zeroTwist);
|
||||
var vecLen = MathF.Sqrt(local.X * local.X + local.Y * local.Y + local.Z * local.Z);
|
||||
local = Quat.FromAxisAngle(local.X / vecLen, local.Y / vecLen, local.Z / vecLen, _cone);
|
||||
// No swivel is feasible: the least-violating one, its swing and twist clamped to the limits.
|
||||
local = ClampToLimits(Local(a, b, LeastViolating(a, b, zeroTwist)));
|
||||
reached = false;
|
||||
}
|
||||
|
||||
|
|
@ -214,16 +226,28 @@ namespace Lodur
|
|||
return reached;
|
||||
}
|
||||
|
||||
// The shoulder target in the joint frame for the swivel phi about vHat, with w >= 0.
|
||||
private Quat Local(IkVec vHat, in Quat arc, float phi)
|
||||
// The shoulder target in the joint frame for the swivel phi, with w >= 0.
|
||||
private static Quat Local(in Quat a, in Quat b, float phi)
|
||||
{
|
||||
var local = _frameInverse * (Ik.About(vHat, phi) * arc) * _frame;
|
||||
var c = MathF.Cos(0.5f * phi);
|
||||
var s = MathF.Sin(0.5f * phi);
|
||||
var local = new Quat(c * a.X + s * b.X, c * a.Y + s * b.Y, c * a.Z + s * b.Z, c * a.W + s * b.W);
|
||||
return local.W < 0f ? new Quat(-local.X, -local.Y, -local.Z, -local.W) : local;
|
||||
}
|
||||
|
||||
// Swing is the angle between the frame z and its image: inside the cone when sin^2(swing / 2) = x^2 + y^2 <= sin^2(cone / 2).
|
||||
private bool InsideCone(in Quat local) => local.X * local.X + local.Y * local.Y <= _coneSin2;
|
||||
|
||||
// x^2 + y^2 is a quadratic form in (cos(phi / 2), sin(phi / 2)); its least value over the swivel is the form's smaller eigenvalue.
|
||||
private bool SomeSwivelInsideCone(in Quat a, in Quat b)
|
||||
{
|
||||
var maa = a.X * a.X + a.Y * a.Y;
|
||||
var mbb = b.X * b.X + b.Y * b.Y;
|
||||
var mab = a.X * b.X + a.Y * b.Y;
|
||||
var half = 0.5f * (maa - mbb);
|
||||
return 0.5f * (maa + mbb) - MathF.Sqrt(half * half + mab * mab) <= _coneSin2;
|
||||
}
|
||||
|
||||
private bool Feasible(in Quat local)
|
||||
{
|
||||
if (!InsideCone(local)) return false;
|
||||
|
|
@ -233,7 +257,7 @@ namespace Lodur
|
|||
|
||||
// The feasible swivel nearest phi0: step outward both ways, and at the first step that lands inside, bisect back to the boundary;
|
||||
// of the two sides, the one with the smaller twist. False when no swivel is feasible.
|
||||
private bool NearestFeasible(IkVec vHat, in Quat arc, float phi0, out Quat local)
|
||||
private bool NearestFeasible(in Quat a, in Quat b, float phi0, out Quat local)
|
||||
{
|
||||
local = default;
|
||||
const float step = MathF.PI / SwivelSteps;
|
||||
|
|
@ -244,17 +268,17 @@ namespace Lodur
|
|||
for (var sign = -1; sign <= 1; sign += 2)
|
||||
{
|
||||
var phi = phi0 + sign * k * step;
|
||||
if (!Feasible(Local(vHat, arc, phi))) continue;
|
||||
if (!Feasible(Local(a, b, phi))) continue;
|
||||
var inside = phi;
|
||||
var outside = phi0 + sign * (k - 1) * step;
|
||||
for (var b = 0; b < SwivelBisections; b++)
|
||||
for (var i = 0; i < SwivelBisections; i++)
|
||||
{
|
||||
var mid = 0.5f * (inside + outside);
|
||||
if (Feasible(Local(vHat, arc, mid))) inside = mid;
|
||||
if (Feasible(Local(a, b, mid))) inside = mid;
|
||||
else outside = mid;
|
||||
}
|
||||
|
||||
var candidate = Local(vHat, arc, inside);
|
||||
var candidate = Local(a, b, inside);
|
||||
var twist = MathF.Abs(2f * MathF.Atan2(candidate.Z, candidate.W));
|
||||
if (twist < bestTwist)
|
||||
{
|
||||
|
|
@ -269,6 +293,102 @@ namespace Lodur
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Radians past the limits at the swivel phi, swing past the cone plus twist past its range, with the small twist preference. In
|
||||
// double: the least point of a smooth minimum is found only to the square root of the arithmetic's precision.
|
||||
private double Violation(in Quat a, in Quat b, double phi)
|
||||
{
|
||||
var c = Math.Cos(0.5 * phi);
|
||||
var s = Math.Sin(0.5 * phi);
|
||||
var x = c * a.X + s * b.X;
|
||||
var y = c * a.Y + s * b.Y;
|
||||
var z = c * a.Z + s * b.Z;
|
||||
var w = c * a.W + s * b.W;
|
||||
if (w < 0.0)
|
||||
{
|
||||
z = -z;
|
||||
w = -w;
|
||||
}
|
||||
|
||||
var swing = 2.0 * Math.Atan2(Math.Sqrt(x * x + y * y), Math.Sqrt(z * z + w * w));
|
||||
var twist = 2.0 * Math.Atan2(z, w);
|
||||
var over = twist < _twistLo ? _twistLo - twist : (twist > _twistHi ? twist - _twistHi : 0.0);
|
||||
return Math.Max(0.0, swing - _cone) + over + TwistPreference * Math.Abs(twist);
|
||||
}
|
||||
|
||||
// The least-violating swivel: the least of LeastSteps steps round the circle from phi0, refined by golden section within a step
|
||||
// either side.
|
||||
private float LeastViolating(in Quat a, in Quat b, float phi0)
|
||||
{
|
||||
const double step = 2.0 * Math.PI / LeastSteps;
|
||||
var best = (double)phi0;
|
||||
var least = Violation(a, b, best);
|
||||
for (var k = 1; k < LeastSteps; k++)
|
||||
{
|
||||
var phi = phi0 + k * step;
|
||||
var v = Violation(a, b, phi);
|
||||
if (v < least)
|
||||
{
|
||||
least = v;
|
||||
best = phi;
|
||||
}
|
||||
}
|
||||
|
||||
const double g = 0.6180339887498949;
|
||||
var lo = best - step;
|
||||
var hi = best + step;
|
||||
var p = hi - g * (hi - lo);
|
||||
var q = lo + g * (hi - lo);
|
||||
var fp = Violation(a, b, p);
|
||||
var fq = Violation(a, b, q);
|
||||
for (var i = 0; i < LeastRefinements; i++)
|
||||
{
|
||||
if (fp <= fq)
|
||||
{
|
||||
hi = q;
|
||||
q = p;
|
||||
fq = fp;
|
||||
p = hi - g * (hi - lo);
|
||||
fp = Violation(a, b, p);
|
||||
}
|
||||
else
|
||||
{
|
||||
lo = p;
|
||||
p = q;
|
||||
fp = fq;
|
||||
q = lo + g * (hi - lo);
|
||||
fq = Violation(a, b, q);
|
||||
}
|
||||
}
|
||||
|
||||
return (float)(fp <= fq ? p : q);
|
||||
}
|
||||
|
||||
// Twist clamped to its range and swing to the cone, the swing direction kept. The target is swing * twist, the twist about the frame
|
||||
// z and the swing about an axis in its xy plane.
|
||||
private Quat ClampToLimits(in Quat local)
|
||||
{
|
||||
// At a half-turn swing the twist is undefined: read it as none.
|
||||
var tl = MathF.Sqrt(local.Z * local.Z + local.W * local.W);
|
||||
var tz = tl > 1e-6f ? local.Z / tl : 0f;
|
||||
var tw = tl > 1e-6f ? local.W / tl : 1f;
|
||||
var sx = tw * local.X - tz * local.Y;
|
||||
var sy = tz * local.X + tw * local.Y;
|
||||
var sw = tl;
|
||||
var s2 = sx * sx + sy * sy;
|
||||
if (s2 > _coneSin2)
|
||||
{
|
||||
var k = _coneSinHalf / MathF.Sqrt(s2);
|
||||
sx *= k;
|
||||
sy *= k;
|
||||
sw = _coneCosHalf;
|
||||
}
|
||||
|
||||
var half = 0.5f * Ik.Clamp(2f * MathF.Atan2(tz, tw), _twistLo, _twistHi);
|
||||
tz = MathF.Sin(half);
|
||||
tw = MathF.Cos(half);
|
||||
return new Quat(tw * sx + tz * sy, tw * sy - tz * sx, sw * tz, sw * tw);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,14 @@
|
|||
using Lodur;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace Lodur.Core.Tests;
|
||||
|
||||
public sealed class IkTests
|
||||
{
|
||||
private static readonly BodyPlan Plan = BodyPlans.Humanoid();
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public IkTests(ITestOutputHelper output) => _output = output;
|
||||
|
||||
private static (float X, float Y, float Z) Add((float X, float Y, float Z) a, (float X, float Y, float Z) b) => (a.X + b.X, a.Y + b.Y, a.Z + b.Z);
|
||||
private static (float X, float Y, float Z) Sub((float X, float Y, float Z) a, (float X, float Y, float Z) b) => (a.X - b.X, a.Y - b.Y, a.Z - b.Z);
|
||||
|
|
@ -90,27 +94,29 @@ public sealed class IkTests
|
|||
{
|
||||
var arm = ArmChain.Of(Plan, side);
|
||||
var joint = Plan.Joints[arm.ShoulderJoint];
|
||||
var (n, twisted) = (0, 0);
|
||||
var (n, reached, twisted) = (0, 0, 0);
|
||||
foreach (var (rot, pos, target) in Grid(arm, 21 + (int)side, 600))
|
||||
{
|
||||
arm.Solve(rot, pos.X, pos.Y, pos.Z, target.X, target.Y, target.Z, out var sq, out _);
|
||||
var ok = arm.Solve(rot, pos.X, pos.Y, pos.Z, target.X, target.Y, target.Z, out var sq, out _);
|
||||
var swing = 2f * MathF.Asin(MathF.Min(1f, MathF.Sqrt(sq.X * sq.X + sq.Y * sq.Y)));
|
||||
var twist = 2f * MathF.Atan2(sq.Z, sq.W);
|
||||
Assert.True(swing <= joint.Cone + 1e-4f, $"swing {swing} past the cone {joint.Cone} for {target}");
|
||||
Assert.True(twist >= joint.Lower - 1e-4f && twist <= joint.Upper + 1e-4f, $"twist {twist} outside [{joint.Lower}, {joint.Upper}] for {target}");
|
||||
if (MathF.Abs(twist) > 1e-5f)
|
||||
{
|
||||
// A twist is taken only to keep the swing inside the cone, so the swivel sits on the boundary of the feasible range.
|
||||
twisted++;
|
||||
// A twist is taken only to keep the swing inside the cone, so the swivel sits on the boundary of the feasible range; an
|
||||
// unreached goal's least-violating swivel, clamped, sits on the cone.
|
||||
if (ok) twisted++;
|
||||
var onLimit = MathF.Abs(swing - joint.Cone) < 1e-3f || MathF.Abs(twist - joint.Lower) < 1e-3f || MathF.Abs(twist - joint.Upper) < 1e-3f;
|
||||
Assert.True(onLimit, $"shoulder target twists by {twist} rad at swing {swing} for {target}, off every limit");
|
||||
}
|
||||
|
||||
n++;
|
||||
if (ok) reached++;
|
||||
}
|
||||
|
||||
Assert.Equal(600, n);
|
||||
Assert.InRange(twisted, 1, n / 2);
|
||||
Assert.InRange(twisted, 1, reached / 2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -128,6 +134,51 @@ public sealed class IkTests
|
|||
Assert.True(MathF.Abs(twist) > 0.05f && twist >= joint.Lower && twist <= joint.Upper, $"twist {twist}");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(Side.Left)]
|
||||
[InlineData(Side.Right)]
|
||||
public void A_goal_crossing_out_of_the_feasible_set_moves_the_targets_continuously(Side side)
|
||||
{
|
||||
// Two stretches of a circle about the shoulder where a goal leaves the feasible set: past the edge no swivel keeps both swing and
|
||||
// twist inside the limits. Each stretch is 2000 steps of about 0.02 mm.
|
||||
var arm = ArmChain.Of(Plan, side);
|
||||
var centre = Plan.Parts[arm.ParentPart].Position;
|
||||
var s = arm.ShoulderAnchor;
|
||||
var (worstTurn, worstMove) = (0f, 0f);
|
||||
foreach (var (from, to) in new[] { (0.25f, 0.33f), (2.65f, 2.74f) })
|
||||
{
|
||||
const int steps = 2000;
|
||||
var crossings = 0;
|
||||
var (lastReached, lastShoulder, lastWrist) = (false, Quat.Identity, (0f, 0f, 0f));
|
||||
for (var i = 0; i <= steps; i++)
|
||||
{
|
||||
var th = from + (to - from) * i / steps;
|
||||
var goal = (s.X + 0.45f * MathF.Cos(th), s.Y + 0.45f * MathF.Sin(th), s.Z + 0.25f + 0.1f * MathF.Sin(2f * th));
|
||||
var reached = arm.Solve(Quat.Identity, centre.X, centre.Y, centre.Z, goal.Item1, goal.Item2, goal.Item3, out var sq, out var eq);
|
||||
var wrist = Wrist(arm, Quat.Identity, centre, sq, eq);
|
||||
if (i > 0)
|
||||
{
|
||||
if (reached != lastReached) crossings++;
|
||||
var dot = MathF.Abs(sq.X * lastShoulder.X + sq.Y * lastShoulder.Y + sq.Z * lastShoulder.Z + sq.W * lastShoulder.W);
|
||||
worstTurn = MathF.Max(worstTurn, 2f * MathF.Acos(MathF.Min(1f, dot)));
|
||||
worstMove = MathF.Max(worstMove, Len(Sub(wrist, lastWrist)));
|
||||
}
|
||||
|
||||
(lastReached, lastShoulder, lastWrist) = (reached, sq, wrist);
|
||||
}
|
||||
|
||||
// Tripwire: the stretch still crosses the edge, so the bounds below still test a crossing.
|
||||
Assert.Equal(1, crossings);
|
||||
}
|
||||
|
||||
_output.WriteLine($"{side}: largest step of the shoulder target {worstTurn:F4} rad, of the wrist {worstMove * 1000f:F4} mm");
|
||||
// deterministic-bound-margin: each bound is the geometric mean of the shipped worst and the least mutant. Shipped worst, both
|
||||
// arms: 0.0117 rad and 0.0216 mm. Mutant, the zero-twist swing clamped to the cone past the edge (the fallback before this test):
|
||||
// 1.3752 rad and 269.72 mm. Bounds sqrt(0.0117 * 1.3752) = 0.127 rad and sqrt(0.0216 * 269.72) = 2.41 mm.
|
||||
Assert.True(worstTurn < 0.127f, $"the shoulder target jumped {worstTurn} rad in one step");
|
||||
Assert.True(worstMove < 0.00241f, $"the wrist jumped {worstMove} m in one step");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Out_of_reach_points_the_straight_arm_at_the_target_and_reports_not_reached()
|
||||
{
|
||||
|
|
@ -267,12 +318,16 @@ public sealed class IkTests
|
|||
var look = LookChain.Of(Plan);
|
||||
var s = arm.ShoulderAnchor;
|
||||
var rot = Quat.FromAxisAngle(0f, 1f, 0f, 0.2f);
|
||||
var up = Add((0f, 1.4f, 0f), Rot(rot, Sub(s, Plan.Parts[arm.ParentPart].Position)));
|
||||
Assert.False(arm.Solve(rot, 0f, 1.4f, 0f, up.X, up.Y + 0.4f, up.Z, out _, out _));
|
||||
void Work(int n)
|
||||
{
|
||||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
var t = 0.3f + i % 7 * 0.03f;
|
||||
arm.Solve(rot, 0f, 1.4f, 0f, s.X, s.Y - t, s.Z + t, out _, out _);
|
||||
// Straight up from the shoulder, in reach: no swivel is feasible, so the least-violating search runs.
|
||||
arm.Solve(rot, 0f, 1.4f, 0f, up.X, up.Y + t, up.Z, out _, out _);
|
||||
look.Solve(rot, 0f, 1.4f, 0f, t, 1.7f, 2f, out _);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue