From a152f9322e62f2eff62f835e71e77bbaa61e6c00 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 5 Sep 2022 03:11:30 +0200 Subject: [PATCH] Fix affine transform logic --- .../D2D1AffineTransformMapperFactory.cs | 22 ++++++++++++------- .../Shaders/Interop/Helpers/Point64.cs | 8 ++++--- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs index 1f6b5fe8..f7028dd6 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Factories/D2D1AffineTransformMapperFactory.cs @@ -88,16 +88,22 @@ internal sealed class D2D1AffineTransformMapperFactory : D2D1TransformMapperF Point64 bottomRight = new(rectangle.Right, rectangle.Bottom); // Transform them with the current matrix - topLeft.Transform(in matrix); - topRight.Transform(in matrix); - bottomLeft.Transform(in matrix); - bottomRight.Transform(in matrix); + var transformedTopLeft = topLeft.Transform(in matrix); + var transformedTopRight = topRight.Transform(in matrix); + var transformedBottomLeft = bottomLeft.Transform(in matrix); + var transformedBottomRight = bottomRight.Transform(in matrix); // Calculate the bounding box of the transformed points - long x = Math.Min(Math.Min(topLeft.X, topRight.X), Math.Min(bottomLeft.X, bottomRight.X)); - long y = Math.Min(Math.Min(topLeft.Y, topRight.Y), Math.Min(bottomLeft.Y, bottomRight.Y)); - long width = Math.Max(Math.Max(topLeft.X, topRight.X), Math.Max(bottomLeft.X, bottomRight.X)) - rectangle.Left; - long height = Math.Max(Math.Max(topLeft.Y, topRight.Y), Math.Max(bottomLeft.Y, bottomRight.Y)) - rectangle.Top; + double left = Math.Min(Math.Min(transformedTopLeft.X, transformedTopRight.X), Math.Min(transformedBottomLeft.X, transformedBottomRight.X)); + double top = Math.Min(Math.Min(transformedTopLeft.Y, transformedTopRight.Y), Math.Min(transformedBottomLeft.Y, transformedBottomRight.Y)); + double right = Math.Max(Math.Max(transformedTopLeft.X, transformedTopRight.X), Math.Max(transformedBottomLeft.X, transformedBottomRight.X)); + double bottom = Math.Max(Math.Max(transformedTopLeft.Y, transformedTopRight.Y), Math.Max(transformedBottomLeft.Y, transformedBottomRight.Y)); + + // Round each coordinate as needed and compute the rectangle bounds + long x = (long)Math.Floor(left); + long y = (long)Math.Floor(top); + long width = (long)Math.Ceiling(right) - x; + long height = (long)Math.Ceiling(bottom) - y; rectangle = new(x, y, width, height); } diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Point64.cs b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Point64.cs index 17b2a19b..6bd7dec5 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Point64.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/Helpers/Point64.cs @@ -53,7 +53,8 @@ internal struct Point64 /// Transforms the current point using an input matrix. /// /// The transformation matrix to use. - public void Transform(in Matrix3x2 matrix) + /// The transformed points, with no rounding. + public (double X, double Y) Transform(in Matrix3x2 matrix) { // A 3x2 transformation matrix can be thought of a 2x2 matrix with an extra // 1x2 row representing absolute offsets. As such, these do not compute into @@ -75,7 +76,8 @@ internal struct Point64 // [ M11, M12 ] // [ M21, M22 ] // [ x, y] [ M31, M32 ] = [ x * M11 + y * M21 + M31, x * M12 + y * M22 + m32] - this.x = (long)Math.Round(this.x * matrix.M11 + this.y * matrix.M21 + matrix.M31); - this.y = (long)Math.Round(this.x * matrix.M12 + this.y * matrix.M22 + matrix.M32); + return ( + Math.Round((double)this.x * matrix.M11 + (double)this.y * matrix.M21 + matrix.M31), + Math.Round((double)this.x * matrix.M12 + (double)this.y * matrix.M22 + matrix.M32)); } } \ No newline at end of file