Skip to content

develop to main #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,45 @@ public static Fixed64 FastMod(Fixed64 x, Fixed64 y)
return Fixed64.FromRaw(x.m_rawValue % y.m_rawValue);
}

/// <summary>
/// Performs a smooth step interpolation between two values.
/// </summary>
/// <remarks>
/// The interpolation follows a cubic Hermite curve where the function starts at `a`,
/// accelerates, and then decelerates towards `b`, ensuring smooth transitions.
/// </remarks>
/// <param name="a">The starting value.</param>
/// <param name="b">The ending value.</param>
/// <param name="t">A value between 0 and 1 that represents the interpolation factor.</param>
/// <returns>The interpolated value between `a` and `b`.</returns>
public static Fixed64 SmoothStep(Fixed64 a, Fixed64 b, Fixed64 t)
{
t = t * t * (Fixed64.Three - Fixed64.Two * t);
return LinearInterpolate(a, b, t);
}

/// <summary>
/// Performs a cubic Hermite interpolation between two points, using specified tangents.
/// </summary>
/// <remarks>
/// This method interpolates smoothly between `p0` and `p1` while considering the tangents `m0` and `m1`.
/// It is useful for animation curves and smooth motion transitions.
/// </remarks>
/// <param name="p0">The first point.</param>
/// <param name="p1">The second point.</param>
/// <param name="m0">The tangent (slope) at `p0`.</param>
/// <param name="m1">The tangent (slope) at `p1`.</param>
/// <param name="t">A value between 0 and 1 that represents the interpolation factor.</param>
/// <returns>The interpolated value between `p0` and `p1`.</returns>
public static Fixed64 CubicInterpolate(Fixed64 p0, Fixed64 p1, Fixed64 m0, Fixed64 m1, Fixed64 t)
{
Fixed64 t2 = t * t;
Fixed64 t3 = t2 * t;
return (Fixed64.Two * p0 - Fixed64.Two * p1 + m0 + m1) * t3
+ (-Fixed64.Three * p0 + Fixed64.Three * p1 - Fixed64.Two * m0 - m1) * t2
+ m0 * t + p0;
}

/// <summary>
/// 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).
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using System.Threading;

namespace FixedMathSharp
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,32 @@
);
}

/// <summary>
/// Creates a scaling matrix that applies a uniform or non-uniform scale transformation.
/// </summary>
/// <param name="scale">The scale factors along the X, Y, and Z axes.</param>
/// <returns>A 3x3 scaling matrix.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fixed3x3 CreateScale(Vector3d scale)
{
return new Fixed3x3(
scale.x, Fixed64.Zero, Fixed64.Zero,
Fixed64.Zero, scale.y, Fixed64.Zero,
Fixed64.Zero, Fixed64.Zero, scale.z
);
}

/// <summary>
/// Creates a uniform scaling matrix with the same scale factor on all axes.
/// </summary>
/// <param name="scaleFactor">The uniform scale factor.</param>
/// <returns>A 3x3 scaling matrix with uniform scaling.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fixed3x3 CreateScale(Fixed64 scaleFactor)
{
return CreateScale(new Vector3d(scaleFactor, scaleFactor, scaleFactor));
}

/// <summary>
/// Normalizes the basis vectors of a 3x3 matrix to ensure they are orthogonal and unit length.
/// </summary>
Expand Down Expand Up @@ -317,7 +343,7 @@

#endregion

#region Matrix Operations
#region Static Matrix Operations

/// <summary>
/// Linearly interpolates between two matrices.
Expand Down Expand Up @@ -380,11 +406,46 @@
return true;
}

/// <summary>
/// Transforms a direction vector from local space to world space using this transformation matrix.
/// Ignores translation.
/// </summary>
/// <param name="matrix">The transformation matrix.</param>
/// <param name="direction">The local-space direction vector.</param>
/// <returns>The transformed direction in world space.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3d TransformDirection(Fixed3x3 matrix, Vector3d direction)
{
return new Vector3d(
matrix.m00 * direction.x + matrix.m01 * direction.y + matrix.m02 * direction.z,
matrix.m10 * direction.x + matrix.m11 * direction.y + matrix.m12 * direction.z,
matrix.m20 * direction.x + matrix.m21 * direction.y + matrix.m22 * direction.z
);
}

/// <summary>
/// Transforms a direction from world space into the local space of the matrix.
/// Ignores translation.
/// </summary>
/// <param name="matrix">The transformation matrix.</param>
/// <param name="direction">The world-space direction.</param>
/// <returns>The transformed local-space direction.</returns>
public static Vector3d InverseTransformDirection(Fixed3x3 matrix, Vector3d direction)
{
if (!Invert(matrix, out Fixed3x3? inverseMatrix) || !inverseMatrix.HasValue)
throw new InvalidOperationException("Matrix is not invertible.");

return new Vector3d(
inverseMatrix.Value.m00 * direction.x + inverseMatrix.Value.m01 * direction.y + inverseMatrix.Value.m02 * direction.z,
inverseMatrix.Value.m10 * direction.x + inverseMatrix.Value.m11 * direction.y + inverseMatrix.Value.m12 * direction.z,
inverseMatrix.Value.m20 * direction.x + inverseMatrix.Value.m21 * direction.y + inverseMatrix.Value.m22 * direction.z
);
}

#endregion

#region Operators


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Fixed3x3 operator -(Fixed3x3 a, Fixed3x3 b)
{
Expand Down Expand Up @@ -495,7 +556,7 @@
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj)

Check warning on line 559 in src/FixedMathSharp/Numerics/Fixed3x3.cs

View workflow job for this annotation

GitHub Actions / build-and-test-linux

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).

Check warning on line 559 in src/FixedMathSharp/Numerics/Fixed3x3.cs

View workflow job for this annotation

GitHub Actions / build-and-test-windows

Nullability of type of parameter 'obj' doesn't match overridden member (possibly because of nullability attributes).
{
return obj is Fixed3x3 other && Equals(other);
}
Expand Down
Loading
Loading