CultMath: HLSL vector mul(v, v) = dot for float2/3/4, closing the quaternion-conversion overload trap
mul(float4, float4) would otherwise bind to a quaternion product once mul(quaternion, quaternion) lands. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
55bf180647
commit
14f7ad9422
3 changed files with 35 additions and 0 deletions
|
|
@ -515,6 +515,12 @@ public static partial class math
|
|||
// HLSL mul: mul(m, v) treats v as a column vector, mul(v, m) as a row vector.
|
||||
public static float mul(float left, float right) => left * right;
|
||||
|
||||
// mul(v, v) is the inner product, as in HLSL. It also keeps mul(float4, float4) from binding to the
|
||||
// quaternion product through float4's implicit conversion to quaternion.
|
||||
public static float mul(float2 left, float2 right) => dot(left, right);
|
||||
public static float mul(float3 left, float3 right) => dot(left, right);
|
||||
public static float mul(float4 left, float4 right) => dot(left, right);
|
||||
|
||||
public static float2 mul(float2x2 m, float2 v) =>
|
||||
new(m._m00 * v.x + m._m01 * v.y, m._m10 * v.x + m._m11 * v.y);
|
||||
|
||||
|
|
|
|||
27
packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs
Normal file
27
packages/cultmath/tests/CultMath.Tests/QuaternionTests.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
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)));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4bee56d64b8862632f92e8fdbe20379c
|
||||
Loading…
Reference in a new issue