Skip to content

Commit 4be5594

Browse files
committed
feat: added vector4d
1 parent 4a2e4aa commit 4be5594

8 files changed

Lines changed: 1411 additions & 0 deletions

File tree

src/FixedMathSharp/Numerics/Extensions/Fixed4x4.Extensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public static Vector3d TransformPoint(this Fixed4x4 matrix, Vector3d point)
4848
return Fixed4x4.TransformPoint(matrix, point);
4949
}
5050

51+
/// <inheritdoc cref="Fixed4x4.Transform(Fixed4x4, Vector4d)" />
52+
public static Vector4d Transform(this Fixed4x4 matrix, Vector4d vector)
53+
{
54+
return Fixed4x4.Transform(matrix, vector);
55+
}
56+
5157
/// <inheritdoc cref="Fixed4x4.InverseTransformPoint(Fixed4x4, Vector3d)" />
5258
public static Vector3d InverseTransformPoint(this Fixed4x4 matrix, Vector3d point)
5359
{
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Runtime.CompilerServices;
2+
3+
namespace FixedMathSharp;
4+
5+
/// <summary>
6+
/// Provides extension methods for the Vector4d type to support additional vector operations and comparisons.
7+
/// </summary>
8+
public static partial class Vector4dExtensions
9+
{
10+
#region Vector4d Operations
11+
12+
/// <summary>
13+
/// Clamps each component of the vector to the range [-1, 1] and returns the clamped vector.
14+
/// </summary>
15+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
16+
public static Vector4d ClampOneInPlace(this Vector4d v)
17+
{
18+
v.x = v.x.ClampOne();
19+
v.y = v.y.ClampOne();
20+
v.z = v.z.ClampOne();
21+
v.w = v.w.ClampOne();
22+
return v;
23+
}
24+
25+
#endregion
26+
27+
#region Conversion
28+
29+
/// <inheritdoc cref="Vector4d.Abs(Vector4d)" />
30+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
31+
public static Vector4d Abs(this Vector4d value)
32+
{
33+
return Vector4d.Abs(value);
34+
}
35+
36+
/// <inheritdoc cref="Vector4d.Sign(Vector4d)" />
37+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
38+
public static Vector4d Sign(this Vector4d value)
39+
{
40+
return Vector4d.Sign(value);
41+
}
42+
43+
#endregion
44+
45+
#region Equality
46+
47+
/// <summary>
48+
/// Compares two vectors for approximate equality, allowing a fixed absolute difference.
49+
/// </summary>
50+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
51+
public static bool FuzzyEqualAbsolute(this Vector4d me, Vector4d other, Fixed64 allowedDifference)
52+
{
53+
return (me.x - other.x).Abs() <= allowedDifference &&
54+
(me.y - other.y).Abs() <= allowedDifference &&
55+
(me.z - other.z).Abs() <= allowedDifference &&
56+
(me.w - other.w).Abs() <= allowedDifference;
57+
}
58+
59+
/// <summary>
60+
/// Compares two vectors for approximate equality, allowing a fractional difference.
61+
/// </summary>
62+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
63+
public static bool FuzzyEqual(this Vector4d me, Vector4d other, Fixed64? percentage = null)
64+
{
65+
Fixed64 p = percentage ?? Fixed64.Epsilon;
66+
return me.x.FuzzyComponentEqual(other.x, p) &&
67+
me.y.FuzzyComponentEqual(other.y, p) &&
68+
me.z.FuzzyComponentEqual(other.z, p) &&
69+
me.w.FuzzyComponentEqual(other.w, p);
70+
}
71+
72+
#endregion
73+
}

src/FixedMathSharp/Numerics/Fixed4x4.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,32 @@ public Fixed4x4(
175175
this.m30 = m30; this.m31 = m31; this.m32 = m32; this.m33 = m33;
176176
}
177177

178+
/// <summary>
179+
/// Creates a matrix from four row vectors.
180+
/// </summary>
181+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
182+
public static Fixed4x4 FromRows(Vector4d row0, Vector4d row1, Vector4d row2, Vector4d row3)
183+
{
184+
return new Fixed4x4(
185+
row0.x, row0.y, row0.z, row0.w,
186+
row1.x, row1.y, row1.z, row1.w,
187+
row2.x, row2.y, row2.z, row2.w,
188+
row3.x, row3.y, row3.z, row3.w);
189+
}
190+
191+
/// <summary>
192+
/// Creates a matrix from four column vectors.
193+
/// </summary>
194+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
195+
public static Fixed4x4 FromColumns(Vector4d column0, Vector4d column1, Vector4d column2, Vector4d column3)
196+
{
197+
return new Fixed4x4(
198+
column0.x, column1.x, column2.x, column3.x,
199+
column0.y, column1.y, column2.y, column3.y,
200+
column0.z, column1.z, column2.z, column3.z,
201+
column0.w, column1.w, column2.w, column3.w);
202+
}
203+
178204
#endregion
179205

180206
#region Properties
@@ -1279,6 +1305,15 @@ private static bool FullInvert(Fixed4x4 matrix, out Fixed4x4 result)
12791305
return true;
12801306
}
12811307

1308+
/// <summary>
1309+
/// Transforms a 4D vector by a 4x4 matrix, preserving the computed W component.
1310+
/// </summary>
1311+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1312+
public static Vector4d Transform(Fixed4x4 matrix, Vector4d vector)
1313+
{
1314+
return Vector4d.Transform(matrix, vector);
1315+
}
1316+
12821317
/// <summary>
12831318
/// Transforms a point from local space to world space using this transformation matrix.
12841319
/// </summary>

src/FixedMathSharp/Numerics/Vector2d.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,15 @@ public readonly Vector3d ToVector3d(Fixed64 z)
847847
return new Vector3d(x, z, y);
848848
}
849849

850+
/// <summary>
851+
/// Converts this <see cref="Vector2d"/> to a <see cref="Vector4d"/> with explicit Z and W components.
852+
/// </summary>
853+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
854+
public readonly Vector4d ToVector4d(Fixed64 z, Fixed64 w)
855+
{
856+
return new Vector4d(x, y, z, w);
857+
}
858+
850859
/// <summary>
851860
/// Deconstructs the current instance into its X and Y coordinate values as single-precision floating-point numbers.
852861
/// </summary>

src/FixedMathSharp/Numerics/Vector3d.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,15 @@ public Vector2d ToVector2d()
15381538
return new Vector2d(x, z);
15391539
}
15401540

1541+
/// <summary>
1542+
/// Converts this <see cref="Vector3d"/> to a <see cref="Vector4d"/> with an explicit W component.
1543+
/// </summary>
1544+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1545+
public Vector4d ToVector4d(Fixed64 w)
1546+
{
1547+
return new Vector4d(x, y, z, w);
1548+
}
1549+
15411550
/// <summary>
15421551
/// Deconstructs the vector into its X, Y, and Z components as single-precision floating-point values.
15431552
/// </summary>

0 commit comments

Comments
 (0)