Skip to content

Commit eaef96d

Browse files
authored
feat: helper methods (#32)
* feat: add method to extract up direction from 4x4 matrix - cleanup unneccessary extensions * feat: add method to clamp Vector3d by magnitude
1 parent 0a111e4 commit eaef96d

File tree

5 files changed

+53
-44
lines changed

5 files changed

+53
-44
lines changed

src/FixedMathSharp/Extensions/Fixed4x4.Extensions.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,29 @@ public static class Fixed4x4Extensions
66
{
77
#region Extraction, and Setters
88

9-
/// <inheritdoc cref="Fixed4x4.ExtractScale(Fixed4x4)" />
10-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11-
public static Vector3d ExtractScale(this Fixed4x4 matrix)
12-
{
13-
return Fixed4x4.ExtractScale(matrix);
14-
}
15-
169
/// <inheritdoc cref="Fixed4x4.ExtractLossyScale(Fixed4x4)" />
1710
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1811
public static Vector3d ExtractLossyScale(this Fixed4x4 matrix)
1912
{
2013
return Fixed4x4.ExtractLossyScale(matrix);
2114
}
2215

23-
/// <inheritdoc cref="Fixed4x4.ExtractTranslation(Fixed4x4)" />
24-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
25-
public static Vector3d ExtractTranslation(this Fixed4x4 matrix)
16+
/// <inheritdoc cref="Fixed4x4.SetGlobalScale(Fixed4x4, Vector3d)" />
17+
public static Fixed4x4 SetGlobalScale(this ref Fixed4x4 matrix, Vector3d globalScale)
2618
{
27-
return Fixed4x4.ExtractTranslation(matrix);
19+
return matrix = Fixed4x4.SetGlobalScale(matrix, globalScale);
2820
}
2921

30-
/// <inheritdoc cref="Fixed4x4.ExtractRotation(Fixed4x4)" />
31-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
32-
public static FixedQuaternion ExtractRotation(this Fixed4x4 matrix)
22+
/// <inheritdoc cref="Fixed4x4.SetTranslation(Fixed4x4, Vector3d)" />
23+
public static Fixed4x4 SetTranslation(this ref Fixed4x4 matrix, Vector3d position)
3324
{
34-
return Fixed4x4.ExtractRotation(matrix);
25+
return matrix = Fixed4x4.SetTranslation(matrix, position);
3526
}
3627

37-
/// <inheritdoc cref="Fixed4x4.SetGlobalScale(Fixed4x4, Vector3d)" />
38-
public static Fixed4x4 SetGlobalScale(this ref Fixed4x4 matrix, Vector3d globalScale)
28+
/// <inheritdoc cref="Fixed4x4.SetRotation(Fixed4x4, FixedQuaternion)" />
29+
public static Fixed4x4 SetRotation(this ref Fixed4x4 matrix, FixedQuaternion rotation)
3930
{
40-
return matrix = Fixed4x4.SetGlobalScale(matrix, globalScale);
31+
return matrix = Fixed4x4.SetRotation(matrix, rotation);
4132
}
4233

4334
/// <inheritdoc cref="Fixed4x4.TransformPoint(Fixed4x4, Vector3d)" />

src/FixedMathSharp/Extensions/Vector3d.Extensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public static Vector3d ClampOneInPlace(this Vector3d v)
2020
return v;
2121
}
2222

23+
public static Vector3d ClampMagnitude(this Vector3d value, Fixed64 maxMagnitude)
24+
{
25+
return Vector3d.ClampMagnitude(value, maxMagnitude);
26+
}
27+
2328
/// <summary>
2429
/// Checks if the distance between two vectors is less than or equal to a specified factor.
2530
/// </summary>

src/FixedMathSharp/Numerics/Fixed4x4.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,16 @@ public Fixed4x4(
6363

6464
public readonly bool IsAffine => (m33 == Fixed64.One) && (m03 == Fixed64.Zero && m13 == Fixed64.Zero && m23 == Fixed64.Zero);
6565

66-
/// <summary>
67-
/// Gets or sets the translation component of this matrix.
68-
/// </summary>
69-
/// <returns>
70-
/// The translation component of the current instance.
71-
/// </returns>
72-
public readonly Vector3d Translation => this.ExtractTranslation();
66+
/// <inheritdoc cref="ExtractTranslation(Fixed4x4)" />
67+
public readonly Vector3d Translation => ExtractTranslation(this);
68+
69+
public readonly Vector3d Up => ExtractUp(this);
7370

74-
public readonly Vector3d Scale => this.ExtractScale();
71+
/// <inheritdoc cref="ExtractScale(Fixed4x4)" />
72+
public readonly Vector3d Scale => ExtractScale(this);
7573

76-
public readonly FixedQuaternion Rotation => this.ExtractRotation();
74+
/// <inheritdoc cref="ExtractRotation(Fixed4x4)" />
75+
public readonly FixedQuaternion Rotation => ExtractRotation(this);
7776

7877
/// <summary>
7978
/// Calculates the determinant of a 4x4 matrix.
@@ -275,6 +274,19 @@ public static Vector3d ExtractTranslation(Fixed4x4 matrix)
275274
return new Vector3d(matrix.m30, matrix.m31, matrix.m32);
276275
}
277276

277+
/// <summary>
278+
/// Extracts the up direction from the 4x4 matrix.
279+
/// </summary>
280+
/// <remarks>
281+
/// This is the surface normal if the matrix represents ground orientation.
282+
/// </remarks>
283+
/// <param name="matrix"></param>
284+
/// <returns>A <see cref="Vector3d"/> representing the up direction.</returns>
285+
public static Vector3d ExtractUp(Fixed4x4 matrix)
286+
{
287+
return new Vector3d(matrix.m10, matrix.m11, matrix.m12).Normalize();
288+
}
289+
278290
/// <summary>
279291
/// Extracts the scaling factors from the matrix by calculating the magnitudes of the basis vectors (non-lossy).
280292
/// </summary>

src/FixedMathSharp/Numerics/Vector3d.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,17 @@ public static Vector3d Clamp(Vector3d value, Vector3d min, Vector3d max)
622622
);
623623
}
624624

625+
public static Vector3d ClampMagnitude(Vector3d value, Fixed64 maxMagnitude)
626+
{
627+
Fixed64 magnitudeSqr = value.SqrMagnitude;
628+
if (magnitudeSqr > maxMagnitude * maxMagnitude)
629+
{
630+
Fixed64 magnitude = FixedMath.Sqrt(magnitudeSqr); // Get actual magnitude
631+
return (value / magnitude) * maxMagnitude; // Scale vector to max magnitude
632+
}
633+
return value;
634+
}
635+
625636
/// <summary>
626637
/// Determines if two vectors are exactly parallel by checking if their cross product is zero.
627638
/// </summary>

tests/FixedMathSharp.Tests/Fixed4x4.Tests.cs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ public void FixedMatrix4x4_CreateTranslation_WorksCorrectly()
3838
var matrix = Fixed4x4.CreateTranslation(translation);
3939

4040
// Extract the translation to verify
41-
var extractedTranslation = matrix.ExtractTranslation();
42-
43-
Assert.Equal(translation, extractedTranslation);
41+
Assert.Equal(translation, matrix.Translation);
4442
}
4543

4644
[Fact]
@@ -50,9 +48,7 @@ public void FixedMatrix4x4_CreateScale_WorksCorrectly()
5048
var matrix = Fixed4x4.CreateScale(scale);
5149

5250
// Extract the scale to verify
53-
var extractedScale = matrix.ExtractScale();
54-
55-
Assert.Equal(scale, extractedScale);
51+
Assert.Equal(scale, matrix.Scale);
5652
}
5753

5854
[Fact]
@@ -84,14 +80,10 @@ public void FixedMatrix4x4_SetTransform_WorksCorrectly()
8480
matrix.SetTransform(translation, rotation, scale);
8581

8682
// Extract and validate translation, scale, and rotation
87-
var extractedTranslation = matrix.ExtractTranslation();
88-
var extractedScale = matrix.ExtractScale();
89-
var extractedRotation = matrix.ExtractRotation();
90-
91-
Assert.Equal(translation, extractedTranslation);
92-
Assert.Equal(scale, extractedScale);
93-
Assert.True(extractedRotation.FuzzyEqual(rotation, new Fixed64(0.0001)),
94-
$"Extracted rotation {extractedRotation} does not match expected {rotation}.");
83+
Assert.Equal(translation, matrix.Translation);
84+
Assert.Equal(scale, matrix.Scale);
85+
Assert.True(matrix.Rotation.FuzzyEqual(rotation, new Fixed64(0.0001)),
86+
$"Extracted rotation {matrix.Rotation} does not match expected {rotation}.");
9587
}
9688

9789
[Fact]
@@ -248,9 +240,7 @@ public void FixedMatrix4x4_SetGlobalScale_WorksWithoutRotation()
248240
matrix.SetGlobalScale(globalScale);
249241

250242
// Extract the final scale
251-
var extractedScale = matrix.ExtractScale();
252-
253-
Assert.Equal(globalScale, extractedScale);
243+
Assert.Equal(globalScale, matrix.Scale);
254244
}
255245

256246
[Fact]

0 commit comments

Comments
 (0)