|
| 1 | +using System.Numerics; |
| 2 | +using kajarlabs.Framework.Numerics; |
| 3 | + |
| 4 | +namespace kajarlabs.Framework.Scene; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Stores a cached draw-space 2D affine transform matrix and its inverse. |
| 8 | +/// </summary> |
| 9 | +public readonly struct DrawInfo : IEquatable<DrawInfo> |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Gets an identity <see cref="DrawInfo"/> with identity matrix and inverse. |
| 13 | + /// </summary> |
| 14 | + public static readonly DrawInfo IDENTITY = new(Matrix3x2.Identity, Matrix3x2.Identity, isInvertible: true); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Initializes a new instance of the <see cref="DrawInfo"/> struct. |
| 18 | + /// </summary> |
| 19 | + public DrawInfo(Matrix3x2 matrix, Matrix3x2 matrixInverse, bool isInvertible) |
| 20 | + { |
| 21 | + Matrix = matrix; |
| 22 | + MatrixInverse = matrixInverse; |
| 23 | + IsInvertible = isInvertible; |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Gets the local-to-draw-space row-vector affine matrix. |
| 28 | + /// </summary> |
| 29 | + public Matrix3x2 Matrix { get; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Gets the cached draw-space-to-local inverse matrix, or <see cref="Matrix3x2.Identity"/> when <see cref="IsInvertible"/> is <see langword="false"/>. |
| 33 | + /// </summary> |
| 34 | + public Matrix3x2 MatrixInverse { get; } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Gets a value indicating whether <see cref="Matrix"/> is finite and non-singular. |
| 38 | + /// </summary> |
| 39 | + public bool IsInvertible { get; } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Creates a <see cref="DrawInfo"/> from <paramref name="matrix"/>, computing its inverse when non-singular. |
| 43 | + /// </summary> |
| 44 | + public static DrawInfo Create(Matrix3x2 matrix) |
| 45 | + { |
| 46 | + var isInvertible = matrix.TryInvert(out var inverse); |
| 47 | + return new DrawInfo(matrix, isInvertible ? inverse : Matrix3x2.Identity, isInvertible); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Determines whether two <see cref="DrawInfo"/> values are equal. |
| 52 | + /// </summary> |
| 53 | + public static bool operator ==(DrawInfo left, DrawInfo right) => left.Equals(right); |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Determines whether two <see cref="DrawInfo"/> values are not equal. |
| 57 | + /// </summary> |
| 58 | + public static bool operator !=(DrawInfo left, DrawInfo right) => !left.Equals(right); |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Composes <paramref name="localTransform"/> onto this parent <see cref="DrawInfo"/> in row-vector order |
| 62 | + /// (<c>localTransform * Matrix</c>). |
| 63 | + /// </summary> |
| 64 | + public DrawInfo ApplyLocal(Matrix3x2 localTransform) |
| 65 | + => Create(localTransform * Matrix); |
| 66 | + |
| 67 | + /// <inheritdoc /> |
| 68 | + public bool Equals(DrawInfo other) |
| 69 | + => IsInvertible == other.IsInvertible |
| 70 | + && Matrix.Equals(other.Matrix) |
| 71 | + && MatrixInverse.Equals(other.MatrixInverse); |
| 72 | + |
| 73 | + /// <inheritdoc /> |
| 74 | + public override bool Equals(object? obj) => obj is DrawInfo other && Equals(other); |
| 75 | + |
| 76 | + /// <inheritdoc /> |
| 77 | + public override int GetHashCode() => HashCode.Combine(Matrix, MatrixInverse, IsInvertible); |
| 78 | +} |
0 commit comments