diff --git a/src/Avalonia.Visuals/Matrix.cs b/src/Avalonia.Visuals/Matrix.cs index 3c8e5e39f2..24ec343106 100644 --- a/src/Avalonia.Visuals/Matrix.cs +++ b/src/Avalonia.Visuals/Matrix.cs @@ -9,8 +9,6 @@ namespace Avalonia /// public readonly struct Matrix : IEquatable { - private const float DecomposeEpsilon = 0.0001f; - private readonly double _m11; private readonly double _m12; private readonly double _m21; @@ -322,14 +320,19 @@ namespace Avalonia } } + /// + /// Decomposes given matrix into transform operations. + /// + /// Matrix to decompose. + /// Decomposed matrix. + /// The status of the operation. public static bool TryDecomposeTransform(Matrix matrix, out Decomposed decomposed) { decomposed = default; var determinant = matrix.GetDeterminant(); - // Based upon constant in System.Numerics.Matrix4x4. - if (Math.Abs(determinant) < DecomposeEpsilon) + if (MathUtilities.IsZero(determinant)) { return false; } diff --git a/src/Avalonia.Visuals/Media/Transformation/TransformOperation.cs b/src/Avalonia.Visuals/Media/Transformation/TransformOperation.cs index cdf31f8e5b..36f5dd98f1 100644 --- a/src/Avalonia.Visuals/Media/Transformation/TransformOperation.cs +++ b/src/Avalonia.Visuals/Media/Transformation/TransformOperation.cs @@ -2,6 +2,9 @@ using System.Runtime.InteropServices; namespace Avalonia.Media.Transformation { + /// + /// Represents a single primitive transform (like translation, rotation, scale, etc.). + /// public struct TransformOperation { public OperationType Type; @@ -18,8 +21,14 @@ namespace Avalonia.Media.Transformation Identity } + /// + /// Returns whether operation produces the identity matrix. + /// public bool IsIdentity => Matrix.IsIdentity; + /// + /// Bakes this operation to a transform matrix. + /// public void Bake() { Matrix = Matrix.Identity; @@ -53,11 +62,22 @@ namespace Avalonia.Media.Transformation } } - public static bool IsOperationIdentity(ref TransformOperation? operation) - { - return !operation.HasValue || operation.Value.IsIdentity; - } - + /// + /// Returns new identity transform operation. + /// + public static TransformOperation Identity => + new TransformOperation { Matrix = Matrix.Identity, Type = OperationType.Identity }; + + /// + /// Attempts to interpolate between two transform operations. + /// + /// Source operation. + /// Target operation. + /// Interpolation progress. + /// Interpolation result that will be filled in when operation was successful. + /// + /// Based upon https://www.w3.org/TR/css-transforms-1/#interpolation-of-transform-functions. + /// public static bool TryInterpolate(TransformOperation? from, TransformOperation? to, double progress, ref TransformOperation result) { @@ -69,8 +89,10 @@ namespace Avalonia.Media.Transformation return true; } - TransformOperation fromValue = fromIdentity ? default : from.Value; - TransformOperation toValue = toIdentity ? default : to.Value; + // ReSharper disable PossibleInvalidOperationException + TransformOperation fromValue = fromIdentity ? Identity : from.Value; + TransformOperation toValue = toIdentity ? Identity : to.Value; + // ReSharper restore PossibleInvalidOperationException var interpolationType = toIdentity ? fromValue.Type : toValue.Type; @@ -139,7 +161,7 @@ namespace Avalonia.Media.Transformation { var fromMatrix = fromIdentity ? Matrix.Identity : fromValue.Matrix; var toMatrix = toIdentity ? Matrix.Identity : toValue.Matrix; - + if (!Matrix.TryDecomposeTransform(fromMatrix, out Matrix.Decomposed fromDecomposed) || !Matrix.TryDecomposeTransform(toMatrix, out Matrix.Decomposed toDecomposed)) { @@ -165,6 +187,11 @@ namespace Avalonia.Media.Transformation return true; } + private static bool IsOperationIdentity(ref TransformOperation? operation) + { + return !operation.HasValue || operation.Value.IsIdentity; + } + [StructLayout(LayoutKind.Explicit)] public struct DataLayout { diff --git a/src/Avalonia.Visuals/Media/Transformation/TransformOperations.cs b/src/Avalonia.Visuals/Media/Transformation/TransformOperations.cs index 9f711a2d63..334bb93562 100644 --- a/src/Avalonia.Visuals/Media/Transformation/TransformOperations.cs +++ b/src/Avalonia.Visuals/Media/Transformation/TransformOperations.cs @@ -1,9 +1,12 @@ using System; using System.Collections.Generic; -using JetBrains.Annotations; namespace Avalonia.Media.Transformation { + /// + /// Contains a list of that represent primitive transforms that will be + /// applied in declared order. + /// public sealed class TransformOperations : ITransform { public static TransformOperations Identity { get; } = new TransformOperations(new List()); @@ -19,6 +22,9 @@ namespace Avalonia.Media.Transformation Value = ApplyTransforms(); } + /// + /// Returns whether all operations combined together produce the identity matrix. + /// public bool IsIdentity { get; } public IReadOnlyList Operations => _operations;