|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Numerics; |
| 5 | + |
| 6 | +namespace SixLabors.ImageSharp.Common.Helpers; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Provides helper methods for performing quad distortion transformations. |
| 10 | +/// </summary> |
| 11 | +internal static class QuadDistortionHelper |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Computes the projection matrix for a quad distortion transformation. |
| 15 | + /// </summary> |
| 16 | + /// <param name="rectangle">The source rectangle.</param> |
| 17 | + /// <param name="topLeft">The top-left point of the distorted quad.</param> |
| 18 | + /// <param name="topRight">The top-right point of the distorted quad.</param> |
| 19 | + /// <param name="bottomRight">The bottom-right point of the distorted quad.</param> |
| 20 | + /// <param name="bottomLeft">The bottom-left point of the distorted quad.</param> |
| 21 | + /// <returns>The computed projection matrix for the quad distortion.</returns> |
| 22 | + /// <remarks> |
| 23 | + /// This method is based on the algorithm described in the following article: |
| 24 | + /// https://blog.mbedded.ninja/mathematics/geometry/projective-transformations/ |
| 25 | + /// </remarks> |
| 26 | + public static Matrix4x4 ComputeQuadDistortMatrix(Rectangle rectangle, PointF topLeft, PointF topRight, PointF bottomRight, PointF bottomLeft) |
| 27 | + { |
| 28 | + PointF p1 = new(rectangle.X, rectangle.Y); |
| 29 | + PointF p2 = new(rectangle.X + rectangle.Width, rectangle.Y); |
| 30 | + PointF p3 = new(rectangle.X + rectangle.Width, rectangle.Y + rectangle.Height); |
| 31 | + PointF p4 = new(rectangle.X, rectangle.Y + rectangle.Height); |
| 32 | + |
| 33 | + PointF q1 = topLeft; |
| 34 | + PointF q2 = topRight; |
| 35 | + PointF q3 = bottomRight; |
| 36 | + PointF q4 = bottomLeft; |
| 37 | + |
| 38 | + // @formatter:off |
| 39 | + float[][] matrixData = |
| 40 | + [ |
| 41 | + [p1.X, p1.Y, 1, 0, 0, 0, -p1.X * q1.X, -p1.Y * q1.X], |
| 42 | + [0, 0, 0, p1.X, p1.Y, 1, -p1.X * q1.Y, -p1.Y * q1.Y], |
| 43 | + [p2.X, p2.Y, 1, 0, 0, 0, -p2.X * q2.X, -p2.Y * q2.X], |
| 44 | + [0, 0, 0, p2.X, p2.Y, 1, -p2.X * q2.Y, -p2.Y * q2.Y], |
| 45 | + [p3.X, p3.Y, 1, 0, 0, 0, -p3.X * q3.X, -p3.Y * q3.X], |
| 46 | + [0, 0, 0, p3.X, p3.Y, 1, -p3.X * q3.Y, -p3.Y * q3.Y], |
| 47 | + [p4.X, p4.Y, 1, 0, 0, 0, -p4.X * q4.X, -p4.Y * q4.X], |
| 48 | + [0, 0, 0, p4.X, p4.Y, 1, -p4.X * q4.Y, -p4.Y * q4.Y], |
| 49 | + ]; |
| 50 | + |
| 51 | + float[] b = |
| 52 | + [ |
| 53 | + q1.X, |
| 54 | + q1.Y, |
| 55 | + q2.X, |
| 56 | + q2.Y, |
| 57 | + q3.X, |
| 58 | + q3.Y, |
| 59 | + q4.X, |
| 60 | + q4.Y, |
| 61 | + ]; |
| 62 | + |
| 63 | + GaussianEliminationSolver<float>.Solve(matrixData, b); |
| 64 | + |
| 65 | + #pragma warning disable SA1117 |
| 66 | + Matrix4x4 projectionMatrix = new( |
| 67 | + b[0], b[3], 0, b[6], |
| 68 | + b[1], b[4], 0, b[7], |
| 69 | + 0, 0, 1, 0, |
| 70 | + b[2], b[5], 0, 1); |
| 71 | + #pragma warning restore SA1117 |
| 72 | + |
| 73 | + // @formatter:on |
| 74 | + return projectionMatrix; |
| 75 | + } |
| 76 | +} |
0 commit comments