Skip to content

Commit 1ace498

Browse files
committed
feat: new features and refactoring (#28) (#29)
* cleanup and refactor Fixed3x3 and Fixed4x4 - added world space translation methods to both - refactored Fixed4x4 TRS method - added SRT method to Fixed4x4 * added new FixedCurve class * added FixedMath SmoothStep and CubicInterpolate methods * linting and restructuring +semver: minor
1 parent 298da4e commit 1ace498

21 files changed

+809
-99
lines changed

src/FixedMathSharp/FixedMath.cs renamed to src/FixedMathSharp/Core/FixedMath.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,45 @@ public static Fixed64 FastMod(Fixed64 x, Fixed64 y)
248248
return Fixed64.FromRaw(x.m_rawValue % y.m_rawValue);
249249
}
250250

251+
/// <summary>
252+
/// Performs a smooth step interpolation between two values.
253+
/// </summary>
254+
/// <remarks>
255+
/// The interpolation follows a cubic Hermite curve where the function starts at `a`,
256+
/// accelerates, and then decelerates towards `b`, ensuring smooth transitions.
257+
/// </remarks>
258+
/// <param name="a">The starting value.</param>
259+
/// <param name="b">The ending value.</param>
260+
/// <param name="t">A value between 0 and 1 that represents the interpolation factor.</param>
261+
/// <returns>The interpolated value between `a` and `b`.</returns>
262+
public static Fixed64 SmoothStep(Fixed64 a, Fixed64 b, Fixed64 t)
263+
{
264+
t = t * t * (Fixed64.Three - Fixed64.Two * t);
265+
return LinearInterpolate(a, b, t);
266+
}
267+
268+
/// <summary>
269+
/// Performs a cubic Hermite interpolation between two points, using specified tangents.
270+
/// </summary>
271+
/// <remarks>
272+
/// This method interpolates smoothly between `p0` and `p1` while considering the tangents `m0` and `m1`.
273+
/// It is useful for animation curves and smooth motion transitions.
274+
/// </remarks>
275+
/// <param name="p0">The first point.</param>
276+
/// <param name="p1">The second point.</param>
277+
/// <param name="m0">The tangent (slope) at `p0`.</param>
278+
/// <param name="m1">The tangent (slope) at `p1`.</param>
279+
/// <param name="t">A value between 0 and 1 that represents the interpolation factor.</param>
280+
/// <returns>The interpolated value between `p0` and `p1`.</returns>
281+
public static Fixed64 CubicInterpolate(Fixed64 p0, Fixed64 p1, Fixed64 m0, Fixed64 m1, Fixed64 t)
282+
{
283+
Fixed64 t2 = t * t;
284+
Fixed64 t3 = t2 * t;
285+
return (Fixed64.Two * p0 - Fixed64.Two * p1 + m0 + m1) * t3
286+
+ (-Fixed64.Three * p0 + Fixed64.Three * p1 - Fixed64.Two * m0 - m1) * t2
287+
+ m0 * t + p0;
288+
}
289+
251290
/// <summary>
252291
/// Performs linear interpolation between two fixed-point values based on the interpolant t (0 greater or equal to `t` and less than or equal to 1).
253292
/// </summary>

src/FixedMathSharp/FixedTrigonometry.cs renamed to src/FixedMathSharp/Core/FixedTrigonometry.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Runtime.CompilerServices;
3-
using System.Threading;
43

54
namespace FixedMathSharp
65
{

src/FixedMathSharp/Fixed3x3.cs renamed to src/FixedMathSharp/Numerics/Fixed3x3.cs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,32 @@ public static Fixed3x3 CreateShear(Fixed64 shX, Fixed64 shY, Fixed64 shZ)
192192
);
193193
}
194194

195+
/// <summary>
196+
/// Creates a scaling matrix that applies a uniform or non-uniform scale transformation.
197+
/// </summary>
198+
/// <param name="scale">The scale factors along the X, Y, and Z axes.</param>
199+
/// <returns>A 3x3 scaling matrix.</returns>
200+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
201+
public static Fixed3x3 CreateScale(Vector3d scale)
202+
{
203+
return new Fixed3x3(
204+
scale.x, Fixed64.Zero, Fixed64.Zero,
205+
Fixed64.Zero, scale.y, Fixed64.Zero,
206+
Fixed64.Zero, Fixed64.Zero, scale.z
207+
);
208+
}
209+
210+
/// <summary>
211+
/// Creates a uniform scaling matrix with the same scale factor on all axes.
212+
/// </summary>
213+
/// <param name="scaleFactor">The uniform scale factor.</param>
214+
/// <returns>A 3x3 scaling matrix with uniform scaling.</returns>
215+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
216+
public static Fixed3x3 CreateScale(Fixed64 scaleFactor)
217+
{
218+
return CreateScale(new Vector3d(scaleFactor, scaleFactor, scaleFactor));
219+
}
220+
195221
/// <summary>
196222
/// Normalizes the basis vectors of a 3x3 matrix to ensure they are orthogonal and unit length.
197223
/// </summary>
@@ -317,7 +343,7 @@ public static Vector3d ExtractLossyScale(Fixed3x3 matrix)
317343

318344
#endregion
319345

320-
#region Matrix Operations
346+
#region Static Matrix Operations
321347

322348
/// <summary>
323349
/// Linearly interpolates between two matrices.
@@ -380,11 +406,46 @@ public static bool Invert(Fixed3x3 matrix, out Fixed3x3? result)
380406
return true;
381407
}
382408

409+
/// <summary>
410+
/// Transforms a direction vector from local space to world space using this transformation matrix.
411+
/// Ignores translation.
412+
/// </summary>
413+
/// <param name="matrix">The transformation matrix.</param>
414+
/// <param name="direction">The local-space direction vector.</param>
415+
/// <returns>The transformed direction in world space.</returns>
416+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
417+
public static Vector3d TransformDirection(Fixed3x3 matrix, Vector3d direction)
418+
{
419+
return new Vector3d(
420+
matrix.m00 * direction.x + matrix.m01 * direction.y + matrix.m02 * direction.z,
421+
matrix.m10 * direction.x + matrix.m11 * direction.y + matrix.m12 * direction.z,
422+
matrix.m20 * direction.x + matrix.m21 * direction.y + matrix.m22 * direction.z
423+
);
424+
}
425+
426+
/// <summary>
427+
/// Transforms a direction from world space into the local space of the matrix.
428+
/// Ignores translation.
429+
/// </summary>
430+
/// <param name="matrix">The transformation matrix.</param>
431+
/// <param name="direction">The world-space direction.</param>
432+
/// <returns>The transformed local-space direction.</returns>
433+
public static Vector3d InverseTransformDirection(Fixed3x3 matrix, Vector3d direction)
434+
{
435+
if (!Invert(matrix, out Fixed3x3? inverseMatrix) || !inverseMatrix.HasValue)
436+
throw new InvalidOperationException("Matrix is not invertible.");
437+
438+
return new Vector3d(
439+
inverseMatrix.Value.m00 * direction.x + inverseMatrix.Value.m01 * direction.y + inverseMatrix.Value.m02 * direction.z,
440+
inverseMatrix.Value.m10 * direction.x + inverseMatrix.Value.m11 * direction.y + inverseMatrix.Value.m12 * direction.z,
441+
inverseMatrix.Value.m20 * direction.x + inverseMatrix.Value.m21 * direction.y + inverseMatrix.Value.m22 * direction.z
442+
);
443+
}
444+
383445
#endregion
384446

385447
#region Operators
386448

387-
388449
[MethodImpl(MethodImplOptions.AggressiveInlining)]
389450
public static Fixed3x3 operator -(Fixed3x3 a, Fixed3x3 b)
390451
{

0 commit comments

Comments
 (0)