Skip to content

Commit 9ee4634

Browse files
committed
feat: add barycentric helpers
1 parent 19c308d commit 9ee4634

4 files changed

Lines changed: 88 additions & 0 deletions

File tree

src/FixedMathSharp/Core/FixedMath.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,34 @@ public static Fixed64 BarycentricCoordinate(
379379
Fixed64 weightC
380380
) => coordA + (coordB - coordA) * weightB + (coordC - coordA) * weightC;
381381

382+
/// <summary>
383+
/// Returns the second-order scalar product sum for three barycentric vertices.
384+
/// </summary>
385+
/// <remarks>
386+
/// Computes <c>a * a + b * b + c * c + a * b + a * c + b * c</c>.
387+
/// </remarks>
388+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
389+
public static Fixed64 SumSquaredBarycentricProducts(Fixed64 a, Fixed64 b, Fixed64 c) =>
390+
(a * a) + (b * b) + (c * c) + (a * b) + (a * c) + (b * c);
391+
392+
/// <summary>
393+
/// Returns the cross scalar product sum for two sets of three barycentric vertices.
394+
/// </summary>
395+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
396+
public static Fixed64 SumBarycentricProducts(
397+
Fixed64 firstA,
398+
Fixed64 firstB,
399+
Fixed64 firstC,
400+
Fixed64 secondA,
401+
Fixed64 secondB,
402+
Fixed64 secondC)
403+
{
404+
Fixed64 firstSum = firstA + firstB + firstC;
405+
Fixed64 secondSum = secondA + secondB + secondC;
406+
Fixed64 matchingProducts = (firstA * secondA) + (firstB * secondB) + (firstC * secondC);
407+
return firstSum * secondSum + matchingProducts;
408+
}
409+
382410
/// <summary>
383411
/// Adds two fixed-point numbers by adding their raw Q32.32 payloads without saturation.
384412
/// </summary>

src/FixedMathSharp/Numerics/Matrices/Fixed3x3.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,28 @@ public static Fixed3x3 CreateScale(Vector3d scale) =>
359359
public static Fixed3x3 CreateScale(Fixed64 scaleFactor) =>
360360
CreateScale(new Vector3d(scaleFactor, scaleFactor, scaleFactor));
361361

362+
/// <summary>
363+
/// Creates a symmetric matrix containing second-order barycentric product sums for three vectors.
364+
/// </summary>
365+
/// <remarks>
366+
/// The diagonal contains squared component sums, and the off-diagonal elements contain cross-component sums.
367+
/// </remarks>
368+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
369+
public static Fixed3x3 CreateBarycentricProductSums(Vector3d a, Vector3d b, Vector3d c)
370+
{
371+
Fixed64 xx = FixedMath.SumSquaredBarycentricProducts(a.X, b.X, c.X);
372+
Fixed64 yy = FixedMath.SumSquaredBarycentricProducts(a.Y, b.Y, c.Y);
373+
Fixed64 zz = FixedMath.SumSquaredBarycentricProducts(a.Z, b.Z, c.Z);
374+
Fixed64 xy = FixedMath.SumBarycentricProducts(a.X, b.X, c.X, a.Y, b.Y, c.Y);
375+
Fixed64 xz = FixedMath.SumBarycentricProducts(a.X, b.X, c.X, a.Z, b.Z, c.Z);
376+
Fixed64 yz = FixedMath.SumBarycentricProducts(a.Y, b.Y, c.Y, a.Z, b.Z, c.Z);
377+
378+
return new Fixed3x3(
379+
xx, xy, xz,
380+
xy, yy, yz,
381+
xz, yz, zz);
382+
}
383+
362384
/// <summary>
363385
/// Normalizes the basis vectors of a 3x3 matrix to ensure they are orthogonal and unit length.
364386
/// </summary>

tests/FixedMathSharp.Tests/Core/FixedMath.Tests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,28 @@ public void BarycentricCoordinate_WeightsSecondAndThirdValues()
458458
Assert.Equal(new Fixed64(25), FixedMath.BarycentricCoordinate(new Fixed64(10), new Fixed64(20), new Fixed64(30), Fixed64.Half, Fixed64.Half));
459459
}
460460

461+
[Fact]
462+
public void SumSquaredBarycentricProducts_ReturnsSecondOrderSimplexProductSum()
463+
{
464+
var result = FixedMath.SumSquaredBarycentricProducts(new Fixed64(2), new Fixed64(3), new Fixed64(5));
465+
466+
Assert.Equal(new Fixed64(69), result);
467+
}
468+
469+
[Fact]
470+
public void SumBarycentricProducts_ReturnsCrossSimplexProductSum()
471+
{
472+
var result = FixedMath.SumBarycentricProducts(
473+
new Fixed64(1),
474+
new Fixed64(2),
475+
new Fixed64(3),
476+
new Fixed64(4),
477+
new Fixed64(5),
478+
new Fixed64(6));
479+
480+
Assert.Equal(new Fixed64(122), result);
481+
}
482+
461483
#endregion
462484

463485
#region Test: MoveTowards Method

tests/FixedMathSharp.Tests/Numerics/Matrices/Fixed3x3.Tests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ public void CreateScale_WithUniformFixed64_SetsAllDiagonalComponents()
7373
matrix);
7474
}
7575

76+
[Fact]
77+
public void CreateBarycentricProductSums_ReturnsSymmetricProductSumMatrix()
78+
{
79+
var result = Fixed3x3.CreateBarycentricProductSums(
80+
new Vector3d(1, 2, 3),
81+
new Vector3d(4, 5, 6),
82+
new Vector3d(7, 8, 9));
83+
84+
Assert.Equal(
85+
new Fixed3x3(
86+
new Fixed64(105), new Fixed64(258), new Fixed64(306),
87+
new Fixed64(258), new Fixed64(159), new Fixed64(378),
88+
new Fixed64(306), new Fixed64(378), new Fixed64(225)),
89+
result);
90+
}
91+
7692
[Fact]
7793
public void InvertDiagonal_WorksCorrectly()
7894
{

0 commit comments

Comments
 (0)