CultMath: rotation algebra (quaternion clamps, inverse, normalize) (#184)
This commit is contained in:
parent
e503edf2b0
commit
be036b1fda
7 changed files with 650 additions and 4 deletions
463
packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs
Normal file
463
packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs
Normal file
|
|
@ -0,0 +1,463 @@
|
|||
using CultMath;
|
||||
using Xunit;
|
||||
using static CultMath.math;
|
||||
|
||||
namespace CultMath.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The rotation algebra of math.Quaternion.cs. Conventions: Hamilton, xyzw, radians, right-handed,
|
||||
/// mul(a, b) applies b first. Rotations are compared by what they do to vectors or by |dot| near 1,
|
||||
/// never by component equality (q and -q are one rotation), except where a test says bit-identical.
|
||||
/// </summary>
|
||||
public sealed class QuaternionTests
|
||||
{
|
||||
[Fact]
|
||||
public void MulOfTwoFloat4IsTheInnerProduct()
|
||||
{
|
||||
// quaternion converts implicitly from float4; without the vector overloads this call would bind
|
||||
// to the quaternion product and return a float4.
|
||||
var a = new float4(1.0f, 2.0f, 3.0f, 4.0f);
|
||||
var b = new float4(5.0f, 6.0f, 7.0f, 8.0f);
|
||||
float r = mul(a, b);
|
||||
Assert.Equal(70.0f, r);
|
||||
Assert.Equal(dot(a, b), r);
|
||||
Assert.Equal(11.0f, mul(new float2(1.0f, 2.0f), new float2(3.0f, 4.0f)));
|
||||
Assert.Equal(32.0f, mul(new float3(1.0f, 2.0f, 3.0f), new float3(4.0f, 5.0f, 6.0f)));
|
||||
}
|
||||
|
||||
private const float Eps = 2e-5f;
|
||||
|
||||
private static float Uniform(System.Random r, float lo, float hi) => lo + (hi - lo) * r.NextSingle();
|
||||
|
||||
private static float3 RandomUnit3(System.Random r)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var v = new float3(Uniform(r, -1, 1), Uniform(r, -1, 1), Uniform(r, -1, 1));
|
||||
var l = length(v);
|
||||
if (l > 0.1f && l <= 1.0f) return v / l;
|
||||
}
|
||||
}
|
||||
|
||||
private static quaternion RandomRotation(System.Random r) =>
|
||||
quaternion.AxisAngle(RandomUnit3(r), Uniform(r, -MathF.PI, MathF.PI));
|
||||
|
||||
private static void AssertVec(float3 expected, float3 actual, float eps = Eps)
|
||||
{
|
||||
Assert.True(length(expected - actual) <= eps, $"expected {expected}, got {actual}");
|
||||
}
|
||||
|
||||
private static void AssertSameRotation(quaternion a, quaternion b, float eps = Eps)
|
||||
{
|
||||
var d = abs(dot((float4)a, (float4)b));
|
||||
Assert.True(d >= 1.0f - eps, $"|dot| = {d} for {a} vs {b}");
|
||||
}
|
||||
|
||||
// atan2 form: acos resolves nothing below about 5e-4 rad.
|
||||
private static float AngleBetween(float3 a, float3 b) => atan2(length(cross(a, b)), dot(a, b));
|
||||
|
||||
[Fact]
|
||||
public void CompositionAppliesTheRightOperandFirst()
|
||||
{
|
||||
var r = new System.Random(11);
|
||||
for (var i = 0; i < 200; i++)
|
||||
{
|
||||
var a = RandomRotation(r);
|
||||
var b = RandomRotation(r);
|
||||
var v = new float3(Uniform(r, -3, 3), Uniform(r, -3, 3), Uniform(r, -3, 3));
|
||||
AssertVec(rotate(a, rotate(b, v)), rotate(mul(a, b), v), 1e-4f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MulByInverseIsIdentity()
|
||||
{
|
||||
var r = new System.Random(12);
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
var q = RandomRotation(r);
|
||||
AssertSameRotation(mul(q, inverse(q)), quaternion.identity);
|
||||
AssertSameRotation(mul(inverse(q), q), quaternion.identity);
|
||||
}
|
||||
// Not a unit quaternion: inverse divides by dot(q, q).
|
||||
var s = new quaternion(0.0f, 0.0f, 2.0f, 0.0f);
|
||||
var p = mul(s, inverse(s));
|
||||
Assert.True(abs(p.w - 1.0f) <= 1e-6f && abs(p.x) <= 1e-6f && abs(p.y) <= 1e-6f && abs(p.z) <= 1e-6f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InverseOfZeroIsNonFinite()
|
||||
{
|
||||
var q = inverse(new quaternion(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
Assert.False(float.IsFinite(q.w));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConjugateNegatesTheVectorPartAndEqualsInverseForUnit()
|
||||
{
|
||||
var q = new quaternion(1.0f, -2.0f, 3.0f, 4.0f);
|
||||
Assert.Equal(new quaternion(-1.0f, 2.0f, -3.0f, 4.0f), conjugate(q));
|
||||
var r = new System.Random(13);
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var u = RandomRotation(r);
|
||||
var c = conjugate(u);
|
||||
var n = inverse(u);
|
||||
Assert.True(abs(c.x - n.x) <= 1e-6f && abs(c.y - n.y) <= 1e-6f && abs(c.z - n.z) <= 1e-6f && abs(c.w - n.w) <= 1e-6f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AxisAngleIsRightHanded()
|
||||
{
|
||||
var q = quaternion.AxisAngle(new float3(0, 0, 1), MathF.PI / 2);
|
||||
AssertVec(new float3(0, 1, 0), rotate(q, new float3(1, 0, 0)));
|
||||
AssertVec(new float3(-1, 0, 0), rotate(q, new float3(0, 1, 0)));
|
||||
AssertVec(new float3(0, 0, 1), rotate(q, new float3(0, 0, 1)));
|
||||
var h = quaternion.AxisAngle(new float3(0, 0, 1), 0.8f);
|
||||
Assert.Equal(sin(0.4f) , h.z);
|
||||
Assert.Equal(cos(0.4f), h.w);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SlerpEndpointsAndMidpoint()
|
||||
{
|
||||
var r = new System.Random(14);
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var a = RandomRotation(r);
|
||||
var b = RandomRotation(r);
|
||||
AssertSameRotation(slerp(a, b, 0.0f), a);
|
||||
AssertSameRotation(slerp(a, b, 1.0f), b);
|
||||
}
|
||||
var mid = slerp(quaternion.identity, quaternion.AxisAngle(new float3(0, 0, 1), 1.0f), 0.5f);
|
||||
AssertSameRotation(mid, quaternion.AxisAngle(new float3(0, 0, 1), 0.5f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SlerpTakesTheShortestArc()
|
||||
{
|
||||
var z = new float3(0, 0, 1);
|
||||
var a = quaternion.identity;
|
||||
var b = quaternion.AxisAngle(z, 1.0f);
|
||||
// -b is the same rotation with dot(a, -b) < 0; the long way would pass through 1 - pi.
|
||||
var nb = new quaternion(-b.x, -b.y, -b.z, -b.w);
|
||||
Assert.True(dot((float4)a, (float4)nb) < 0.0f);
|
||||
AssertSameRotation(slerp(a, nb, 0.5f), quaternion.AxisAngle(z, 0.5f));
|
||||
AssertSameRotation(slerp(a, nb, 0.25f), quaternion.AxisAngle(z, 0.25f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SlerpOfNearlyParallelQuaternionsIsFiniteAndUnit()
|
||||
{
|
||||
var a = quaternion.AxisAngle(new float3(0, 1, 0), 0.3f);
|
||||
foreach (var b in new[] { a, quaternion.AxisAngle(new float3(0, 1, 0), 0.3f + 1e-6f), normalize(new quaternion(a.x + 1e-7f, a.y, a.z, a.w)) })
|
||||
{
|
||||
var s = slerp(a, b, 0.37f);
|
||||
var l = length((float4)s);
|
||||
Assert.True(float.IsFinite(l) && abs(l - 1.0f) <= 1e-5f, $"{s}");
|
||||
AssertSameRotation(s, a, 1e-5f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SwingTwistRecomposesAndSplitsAboutTheAxis()
|
||||
{
|
||||
var r = new System.Random(15);
|
||||
for (var i = 0; i < 200; i++)
|
||||
{
|
||||
var q = RandomRotation(r);
|
||||
var axis = RandomUnit3(r);
|
||||
swing_twist(q, axis, out var swing, out var twist);
|
||||
AssertSameRotation(mul(swing, twist), q, 1e-4f);
|
||||
Assert.True(length(cross(new float3(twist.x, twist.y, twist.z), axis)) <= 1e-5f, "twist axis parallel");
|
||||
Assert.True(abs(dot(new float3(swing.x, swing.y, swing.z), axis)) <= 1e-5f, "swing axis perpendicular");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SwingTwistOfAHalfTurnSwingHasIdentityTwist()
|
||||
{
|
||||
var q = quaternion.AxisAngle(new float3(1, 0, 0), MathF.PI);
|
||||
swing_twist(q, new float3(0, 0, 1), out var swing, out var twist);
|
||||
AssertSameRotation(twist, quaternion.identity);
|
||||
AssertSameRotation(swing, q);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampTwistReturnsTheInputBitIdenticalInsideTheLimit()
|
||||
{
|
||||
var z = new float3(0, 0, 1);
|
||||
var r = new System.Random(16);
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var twist = quaternion.AxisAngle(z, Uniform(r, -0.9f, 0.9f));
|
||||
Assert.True(twist == clamp_twist(twist, z, -1.0f, 1.0f));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampTwistReturnsTheBoundaryAngleOutside()
|
||||
{
|
||||
var z = new float3(0, 0, 1);
|
||||
AssertSameRotation(clamp_twist(quaternion.AxisAngle(z, 2.0f), z, -1.0f, 0.5f), quaternion.AxisAngle(z, 0.5f));
|
||||
AssertSameRotation(clamp_twist(quaternion.AxisAngle(z, -2.0f), z, -1.0f, 0.5f), quaternion.AxisAngle(z, -1.0f));
|
||||
// Wrapped: 3 pi / 2 about z is -pi / 2, below the lower limit.
|
||||
AssertSameRotation(clamp_twist(quaternion.AxisAngle(z, 3.0f * MathF.PI / 2.0f), z, -1.0f, 1.0f), quaternion.AxisAngle(z, -1.0f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampHingeDiscardsTheSwing()
|
||||
{
|
||||
var z = new float3(0, 0, 1);
|
||||
var r = new System.Random(17);
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var q = RandomRotation(r);
|
||||
var h = clamp_hinge(q, z, -0.7f, 0.7f);
|
||||
// A hinge result is a pure rotation about the axis within the limits.
|
||||
Assert.True(abs(h.x) <= 1e-6f && abs(h.y) <= 1e-6f);
|
||||
var angle = 2.0f * atan2(h.z, h.w);
|
||||
Assert.True(angle >= -0.7f - 1e-5f && angle <= 0.7f + 1e-5f);
|
||||
}
|
||||
// Inside the limit about the axis, the twist survives and the swing does not.
|
||||
var swing = quaternion.AxisAngle(new float3(1, 0, 0), 0.6f);
|
||||
var twist = quaternion.AxisAngle(z, 0.4f);
|
||||
AssertSameRotation(clamp_hinge(mul(swing, twist), z, -1.0f, 1.0f), twist, 1e-4f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampConeReturnsTheInputBitIdenticalInsideTheCone()
|
||||
{
|
||||
var z = new float3(0, 0, 1);
|
||||
var r = new System.Random(18);
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var swing = quaternion.AxisAngle(RandomUnit3(r), Uniform(r, 0.0f, 0.5f));
|
||||
var inside = AngleBetween(rotate(swing, z), z);
|
||||
Assert.True(inside < 0.5f);
|
||||
Assert.True(swing == clamp_cone(swing, z, 0.6f));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampConeReturnsTheBoundaryAngleOutside()
|
||||
{
|
||||
var z = new float3(0, 0, 1);
|
||||
var r = new System.Random(19);
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var swing = quaternion.AxisAngle(RandomUnit3(r), Uniform(r, 1.0f, 3.0f));
|
||||
if (AngleBetween(rotate(swing, z), z) <= 0.4f) continue;
|
||||
var c = clamp_cone(swing, z, 0.4f);
|
||||
Assert.True(abs(AngleBetween(rotate(c, z), z) - 0.4f) <= 1e-4f);
|
||||
// Clamped toward the original direction: same plane, so the clamped and original axes agree.
|
||||
Assert.True(dot(cross(z, rotate(c, z)), cross(z, rotate(swing, z))) > 0.0f);
|
||||
}
|
||||
// Half turn: the cross product vanishes; the clamp still lands on the boundary.
|
||||
var half = quaternion.AxisAngle(new float3(1, 0, 0), MathF.PI);
|
||||
var ch = clamp_cone(half, z, 0.4f);
|
||||
Assert.True(abs(AngleBetween(rotate(ch, z), z) - 0.4f) <= 1e-4f);
|
||||
}
|
||||
|
||||
private static float TwistAngle(quaternion q, float3 axis) => 2.0f * atan2(dot(new float3(q.x, q.y, q.z), axis), q.w);
|
||||
|
||||
[Fact]
|
||||
public void InverseIsTheTrueInverseAcrossTheFloatRange()
|
||||
{
|
||||
// dot(q, q) underflows (5e-21 each, 1e-30) or overflows (5e19 each) in float; the inverse is still
|
||||
// conjugate(q) / |q|^2. Expected values are computed in double.
|
||||
foreach (var (x, w) in new[] { (5e-21f, 5e-21f), (1e-30f, 1e-30f), (5e19f, 5e19f), (3e-25f, -4e-25f) })
|
||||
{
|
||||
var q = new quaternion(x, x, x, w);
|
||||
var n = (double)x * x * 3 + (double)w * w;
|
||||
var r = inverse(q);
|
||||
Assert.True(RelativelyEqual(-x / n, r.x) && RelativelyEqual(-x / n, r.y) && RelativelyEqual(-x / n, r.z) && RelativelyEqual(w / n, r.w), $"{q} -> {r}");
|
||||
}
|
||||
// The result itself beyond the float range is infinite, not a wrong finite number.
|
||||
Assert.True(float.IsInfinity(inverse(new quaternion(1e-40f, 0.0f, 0.0f, 1e-40f)).w));
|
||||
}
|
||||
|
||||
private static bool RelativelyEqual(double expected, float actual) =>
|
||||
float.IsFinite(actual) && Math.Abs(expected - actual) <= 1e-5 * Math.Abs(expected);
|
||||
|
||||
[Fact]
|
||||
public void SwingTwistOfAnExactHalfTurnHasIdentityTwist()
|
||||
{
|
||||
// Exactly zero projection onto the axis (and zero w): the twist must be identity, not NaN.
|
||||
foreach (var q in new[] { new quaternion(1.0f, 0.0f, 0.0f, 0.0f), new quaternion(0.0f, 1.0f, 0.0f, 0.0f) })
|
||||
{
|
||||
swing_twist(q, new float3(0, 0, 1), out var swing, out var twist);
|
||||
Assert.Equal(quaternion.identity, twist);
|
||||
AssertSameRotation(swing, q, 1e-6f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampTwistTakesTheNearerBoundaryAcrossTheWrap()
|
||||
{
|
||||
var z = new float3(0, 0, 1);
|
||||
// 3.1 against [-3.0, -2.5]: -3.0 is 0.18 rad away across the wrap, -2.5 is 5.6 rad away.
|
||||
AssertSameRotation(clamp_twist(quaternion.AxisAngle(z, 3.1f), z, -3.0f, -2.5f), quaternion.AxisAngle(z, -3.0f));
|
||||
// And the mirror image.
|
||||
AssertSameRotation(clamp_twist(quaternion.AxisAngle(z, -3.1f), z, 2.5f, 3.0f), quaternion.AxisAngle(z, 3.0f));
|
||||
// A window centred on pi: an angle just past -pi is inside it and unchanged.
|
||||
var inside = quaternion.AxisAngle(z, -3.0f);
|
||||
Assert.True(inside == clamp_twist(inside, z, 3.2f, 4.0f));
|
||||
Assert.True(inside == clamp_twist(inside, z, 2.5f, 3.4f));
|
||||
// Outside a window beyond pi: -2.0 (= 4.28) is nearer 4.0 than 3.2.
|
||||
AssertSameRotation(clamp_twist(quaternion.AxisAngle(z, -2.0f), z, 3.2f, 4.0f), quaternion.AxisAngle(z, 4.0f));
|
||||
// A window of more than half a turn admits what it contains and clamps what it does not.
|
||||
var a = quaternion.AxisAngle(z, 3.5f);
|
||||
Assert.True(a == clamp_twist(a, z, -1.0f, 4.0f));
|
||||
AssertSameRotation(clamp_twist(quaternion.AxisAngle(z, 2.0f), z, -1.0f, 1.0f), quaternion.AxisAngle(z, 1.0f));
|
||||
// A full turn or more admits everything.
|
||||
var any = quaternion.AxisAngle(z, 2.9f);
|
||||
Assert.True(any == clamp_twist(any, z, -3.5f, 3.5f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampHingeUsesTheGivenAxis()
|
||||
{
|
||||
var r = new System.Random(20);
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
var axis = RandomUnit3(r);
|
||||
var q = RandomRotation(r);
|
||||
var h = clamp_hinge(q, axis, -0.7f, 0.7f);
|
||||
Assert.True(length(cross(new float3(h.x, h.y, h.z), axis)) <= 1e-5f, "pure rotation about the given axis");
|
||||
var angle = TwistAngle(h, axis);
|
||||
Assert.True(angle >= -0.7f - 1e-5f && angle <= 0.7f + 1e-5f);
|
||||
|
||||
// Inside the limit the twist about that axis survives and the swing does not.
|
||||
var perp = normalize(cross(axis, abs(axis.x) < 0.9f ? new float3(1, 0, 0) : new float3(0, 1, 0)));
|
||||
var twist = quaternion.AxisAngle(axis, 0.4f);
|
||||
AssertSameRotation(clamp_hinge(mul(quaternion.AxisAngle(perp, 0.6f), twist), axis, -1.0f, 1.0f), twist, 1e-4f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampConeEnforcesTinyLimits()
|
||||
{
|
||||
var z = new float3(0, 0, 1);
|
||||
foreach (var angle in new[] { 1e-4f, 3e-5f, 1e-5f * 1.5f })
|
||||
{
|
||||
var swing = quaternion.AxisAngle(new float3(1, 0, 0), angle);
|
||||
var c = clamp_cone(swing, z, 1e-5f);
|
||||
Assert.False(swing == c, $"swing of {angle} rad against 1e-5 was returned unchanged");
|
||||
Assert.True(abs(AngleBetween(rotate(c, z), z) - 1e-5f) <= 2e-6f, $"{c}");
|
||||
}
|
||||
// Inside a tiny limit stays bit-identical.
|
||||
var inside = quaternion.AxisAngle(new float3(1, 0, 0), 5e-6f);
|
||||
Assert.True(inside == clamp_cone(inside, z, 1e-5f));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampConeOfAnExactHalfTurnLandsOnTheBoundary()
|
||||
{
|
||||
// rotate((1,0,0,0), z) is exactly -z, so cross(axis, rotated) is exactly zero: the swing's own axis
|
||||
// is used and the result is finite and on the boundary.
|
||||
var z = new float3(0, 0, 1);
|
||||
var c = clamp_cone(new quaternion(1.0f, 0.0f, 0.0f, 0.0f), z, 0.4f);
|
||||
Assert.True(float.IsFinite(c.x) && float.IsFinite(c.w), $"{c}");
|
||||
Assert.True(abs(AngleBetween(rotate(c, z), z) - 0.4f) <= 1e-4f);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampConeUsesTheGivenAxis()
|
||||
{
|
||||
var r = new System.Random(21);
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
var axis = RandomUnit3(r);
|
||||
var swing = quaternion.AxisAngle(RandomUnit3(r), Uniform(r, 1.0f, 3.0f));
|
||||
if (AngleBetween(rotate(swing, axis), axis) <= 0.4f) continue;
|
||||
var c = clamp_cone(swing, axis, 0.4f);
|
||||
Assert.True(abs(AngleBetween(rotate(c, axis), axis) - 0.4f) <= 1e-4f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampTwistWithInfiniteLimitsReturnsTheInputUnchanged()
|
||||
{
|
||||
var z = new float3(0, 0, 1);
|
||||
var inf = float.PositiveInfinity;
|
||||
var r = new System.Random(22);
|
||||
for (var i = 0; i < 50; i++)
|
||||
{
|
||||
var twist = quaternion.AxisAngle(z, Uniform(r, -3.1f, 3.1f));
|
||||
Assert.True(twist == clamp_twist(twist, z, -inf, inf));
|
||||
Assert.True(twist == clamp_twist(twist, z, -1.0f, inf));
|
||||
Assert.True(twist == clamp_twist(twist, z, -inf, 1.0f));
|
||||
Assert.True(twist == clamp_hinge(twist, z, -inf, inf) || length((float4)clamp_hinge(twist, z, -inf, inf) - (float4)twist) <= 1e-6f);
|
||||
}
|
||||
var q = quaternion.AxisAngle(new float3(1, 0, 0), 0.7f);
|
||||
var h = clamp_hinge(q, z, -inf, inf);
|
||||
Assert.True(float.IsFinite(h.w) && float.IsFinite(h.z));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BothClampsPropagateNaN()
|
||||
{
|
||||
var z = new float3(0, 0, 1);
|
||||
var nan = float.NaN;
|
||||
var good = quaternion.AxisAngle(z, 0.3f);
|
||||
var bad = new quaternion(nan, nan, nan, nan);
|
||||
foreach (var c in new[]
|
||||
{
|
||||
clamp_twist(bad, z, -1.0f, 1.0f),
|
||||
clamp_twist(good, z, nan, 1.0f),
|
||||
clamp_twist(good, z, -1.0f, nan),
|
||||
clamp_twist(good, z, nan, nan),
|
||||
clamp_hinge(bad, z, -1.0f, 1.0f),
|
||||
clamp_cone(bad, z, 0.5f),
|
||||
clamp_cone(quaternion.AxisAngle(new float3(1, 0, 0), 1.0f), z, nan),
|
||||
})
|
||||
{
|
||||
// Any NaN component poisons the rotation: it is never a plausible unit quaternion.
|
||||
Assert.True(float.IsNaN(c.x) && float.IsNaN(c.y) && float.IsNaN(c.z), $"{c}");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NormalizeAndInverseAgreeAcrossTheFloatRange()
|
||||
{
|
||||
foreach (var scale in new[] { 1e-30f, 1e-20f, 1e-12f, 1.0f, 1e19f, 1e30f })
|
||||
{
|
||||
var q = new quaternion(1.0f * scale, -2.0f * scale, 0.5f * scale, 3.0f * scale);
|
||||
var n = normalize(q);
|
||||
var expected = normalize(new quaternion(1.0f, -2.0f, 0.5f, 3.0f));
|
||||
Assert.True(length((float4)n - (float4)expected) <= 1e-6f, $"{scale}: {n}");
|
||||
AssertSameRotation(mul(q, inverse(q)), quaternion.identity);
|
||||
}
|
||||
Assert.Equal(quaternion.identity, normalize(new quaternion(0.0f, 0.0f, 0.0f, 0.0f)));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampConeNearAHalfTurnUsesTheSwingAxisOnlyBelowTheThreshold()
|
||||
{
|
||||
var z = new float3(0, 0, 1);
|
||||
var twist = quaternion.AxisAngle(z, 0.5f);
|
||||
// A near half turn about x with a twist, so that the swing's own axis differs from the cross direction.
|
||||
foreach (var (w, usesSwingAxis) in new[] { (2.5e-7f, true), (5e-5f, false) })
|
||||
{
|
||||
var q = mul(normalize(new quaternion(1.0f, 0.0f, 0.0f, w)), twist);
|
||||
var c = clamp_cone(q, z, 0.4f);
|
||||
var swingAxis = normalize(new float3(q.x, q.y, q.z));
|
||||
var expected = quaternion.AxisAngle(usesSwingAxis ? swingAxis : new float3(1, 0, 0), 0.4f);
|
||||
AssertSameRotation(c, expected, 1e-5f);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClampConeOfATinySwingUsesTheCrossDirectionNotTheSwingAxis()
|
||||
{
|
||||
// Angle 1e-7 against 1e-8: the cross product is below the half-turn threshold but the swing points
|
||||
// toward the axis, so the clamp plane is the cross direction (z component exactly zero), not the
|
||||
// tilted swing axis.
|
||||
var z = new float3(0, 0, 1);
|
||||
var swing = quaternion.AxisAngle(normalize(new float3(1, 0, 0.5f)), 1e-7f);
|
||||
var c = clamp_cone(swing, z, 1e-8f);
|
||||
Assert.True(abs(c.x - 5e-9f) <= 1e-9f && abs(c.z) <= 1e-12f, $"{c}");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4bee56d64b8862632f92e8fdbe20379c
|
||||
Loading…
Reference in a new issue