|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Numerics; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +//[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Microsoft.Maui.Graphics.Tests")] |
| 7 | + |
| 8 | +namespace Microsoft.Maui.Graphics |
| 9 | +{ |
| 10 | + static class Matrix3x2Extensions |
| 11 | + { |
| 12 | + public static bool IsZero(this in Matrix3x2 matrix) |
| 13 | + { |
| 14 | + if (matrix.M11 != 0) return false; |
| 15 | + if (matrix.M12 != 0) return false; |
| 16 | + if (matrix.M21 != 0) return false; |
| 17 | + if (matrix.M22 != 0) return false; |
| 18 | + if (matrix.M31 != 0) return false; |
| 19 | + if (matrix.M32 != 0) return false; |
| 20 | + return true; |
| 21 | + } |
| 22 | + |
| 23 | + public static bool IsFinite(this in Matrix3x2 matrix) |
| 24 | + { |
| 25 | + bool result = true; |
| 26 | + |
| 27 | +#if NETSTANDARD2_0 || TIZEN40 |
| 28 | + result &= !(float.IsNaN(matrix.M11) || float.IsInfinity(matrix.M11)); |
| 29 | + result &= !(float.IsNaN(matrix.M12) || float.IsInfinity(matrix.M12)); |
| 30 | + result &= !(float.IsNaN(matrix.M21) || float.IsInfinity(matrix.M21)); |
| 31 | + result &= !(float.IsNaN(matrix.M22) || float.IsInfinity(matrix.M22)); |
| 32 | + result &= !(float.IsNaN(matrix.M31) || float.IsInfinity(matrix.M31)); |
| 33 | + result &= !(float.IsNaN(matrix.M32) || float.IsInfinity(matrix.M32)); |
| 34 | +#else |
| 35 | + result &= float.IsFinite(matrix.M11); |
| 36 | + result &= float.IsFinite(matrix.M12); |
| 37 | + result &= float.IsFinite(matrix.M21); |
| 38 | + result &= float.IsFinite(matrix.M22); |
| 39 | + result &= float.IsFinite(matrix.M31); |
| 40 | + result &= float.IsFinite(matrix.M32); |
| 41 | +#endif |
| 42 | + return result; |
| 43 | + } |
| 44 | + |
| 45 | + public static Vector2 GetScale(this in Matrix3x2 matrix) |
| 46 | + { |
| 47 | + var sx = matrix.M12 == 0 ? Math.Abs(matrix.M11) : new Vector2(matrix.M11, matrix.M12).Length(); |
| 48 | + var sy = matrix.M21 == 0 ? Math.Abs(matrix.M22) : new Vector2(matrix.M21, matrix.M22).Length(); |
| 49 | + if (matrix.GetDeterminant() < 0) sy = -sy; |
| 50 | + return new Vector2(sx, sy); |
| 51 | + } |
| 52 | + |
| 53 | + public static float GetRotation(this in Matrix3x2 matrix) |
| 54 | + { |
| 55 | + return (float) Math.Atan2(matrix.M12, matrix.M11); |
| 56 | + } |
| 57 | + |
| 58 | + public static Vector2 GetTranslation(this in Matrix3x2 matrix) |
| 59 | + { |
| 60 | + return matrix.Translation; |
| 61 | + } |
| 62 | + |
| 63 | + public static Matrix3x2 WithScale(this Matrix3x2 matrix, Vector2 scale) |
| 64 | + { |
| 65 | + var sx = matrix.M12 == 0 ? Math.Abs(matrix.M11) : new Vector2(matrix.M11, matrix.M12).Length(); |
| 66 | + var sy = matrix.M21 == 0 ? Math.Abs(matrix.M22) : new Vector2(matrix.M21, matrix.M22).Length(); |
| 67 | + // if (matrix.GetDeterminant() < 0) sy = -sy; |
| 68 | + |
| 69 | + scale /= new Vector2(sx, sy); |
| 70 | + |
| 71 | + matrix.M11 *= scale.X; |
| 72 | + matrix.M12 *= scale.X; |
| 73 | + matrix.M21 *= scale.Y; |
| 74 | + matrix.M22 *= scale.Y; |
| 75 | + return matrix; |
| 76 | + |
| 77 | + // var t = matrix.Translation; |
| 78 | + // var r = matrix.GetRotation(); |
| 79 | + // return CreateMatrix3x2(scale, r, t); |
| 80 | + } |
| 81 | + |
| 82 | + public static Matrix3x2 WithoutScale(this in Matrix3x2 matrix) |
| 83 | + { |
| 84 | + return matrix.WithScale(Vector2.One); |
| 85 | + } |
| 86 | + |
| 87 | + public static Matrix3x2 WithRotation(this in Matrix3x2 matrix, float radians) |
| 88 | + { |
| 89 | + var t = matrix.Translation; |
| 90 | + var s = matrix.GetScale(); |
| 91 | + return CreateMatrix3x2(s, radians, t); |
| 92 | + } |
| 93 | + |
| 94 | + public static Matrix3x2 WithoutRotation(this in Matrix3x2 matrix) |
| 95 | + { |
| 96 | + var t = matrix.Translation; |
| 97 | + var s = matrix.GetScale(); |
| 98 | + return CreateMatrix3x2(s, 0, t); |
| 99 | + } |
| 100 | + |
| 101 | + public static Matrix3x2 WithTranslation(this Matrix3x2 matrix, Vector2 translation) |
| 102 | + { |
| 103 | + matrix.Translation = translation; |
| 104 | + return matrix; |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Creates a matrix from an SRT. |
| 109 | + /// </summary> |
| 110 | + /// <param name="scale">The scale</param> |
| 111 | + /// <param name="rotation">The rotation, in radians</param> |
| 112 | + /// <param name="translation">the translation</param> |
| 113 | + /// <returns>A Matrix3x2</returns> |
| 114 | + /// <remarks> |
| 115 | + /// This is equivalent to:<br/> |
| 116 | + /// <c> |
| 117 | + /// m = Matrix3x2.CreateScale(scale)<br/> |
| 118 | + /// m *= Matri3x2.CreateRotation(rotation)<br/> |
| 119 | + /// m *= Matri3x2.CreateTranslation(translation)<br/> |
| 120 | + /// </c> |
| 121 | + /// </remarks> |
| 122 | + internal static Matrix3x2 CreateMatrix3x2(Vector2 scale, float rotation, Vector2 translation) |
| 123 | + { |
| 124 | + var m = Matrix3x2.CreateRotation(rotation); |
| 125 | + m.M11 *= scale.X; |
| 126 | + m.M12 *= scale.X; |
| 127 | + m.M21 *= scale.Y; |
| 128 | + m.M22 *= scale.Y; |
| 129 | + m.M31 = translation.X; |
| 130 | + m.M32 = translation.Y; |
| 131 | + return m; |
| 132 | + } |
| 133 | + |
| 134 | + public static float GetLengthScale(this in Matrix3x2 matrix) |
| 135 | + { |
| 136 | + var determinant = matrix.GetDeterminant(); |
| 137 | + var areaScale = Math.Abs(determinant); |
| 138 | + return (float) Math.Sqrt(areaScale); |
| 139 | + } |
| 140 | + |
| 141 | + public static void CopyTo(this in Matrix3x2 matrix, float[] dst, int offset = 0, int count = 6) |
| 142 | + { |
| 143 | + count = Math.Min(dst.Length, count); |
| 144 | + |
| 145 | + dst[offset + 0] = matrix.M11; |
| 146 | + dst[offset + 1] = matrix.M12; |
| 147 | + dst[offset + 2] = matrix.M21; |
| 148 | + dst[offset + 3] = matrix.M22; |
| 149 | + if (count > 4) |
| 150 | + { |
| 151 | + dst[offset + 4] = matrix.M31; |
| 152 | + dst[offset + 5] = matrix.M32; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public static Vector2 Transform(this Matrix3x2 target, float x, float y) |
| 157 | + { |
| 158 | + Vector2 vector2 = new Vector2(x, y); |
| 159 | + return Vector2.Transform(vector2, target); |
| 160 | + } |
| 161 | + |
| 162 | + |
| 163 | + public static void DeconstructScales(this in Matrix3x2 value, out float scale, out float scalex, out float scaley) |
| 164 | + { |
| 165 | + var det = value.GetDeterminant(); |
| 166 | + scale = (float) Math.Sqrt(Math.Abs(det)); |
| 167 | + scalex = value.M12 == 0 ? Math.Abs(value.M11) : new Vector2(value.M11, value.M12).Length(); |
| 168 | + scaley = value.M21 == 0 ? Math.Abs(value.M22) : new Vector2(value.M21, value.M22).Length(); |
| 169 | + if (det < 0) scaley = -scaley; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments