Add D2D1TransformMapperFactory<T>.Transform APIs

このコミットが含まれているのは:
Sergio Pedri committed 2022-09-06 02:32:57 +02:00
1 親 d2551a0d19
コミット f2c7ebdcff
5ファイル変更
+219 -14

検索条件に一致するファイルはありません

+22 -1
ファイルの表示
@@ -1,4 +1,5 @@
using ComputeSharp.D2D1.Shaders.Interop.Factories;
using System.Numerics;
using ComputeSharp.D2D1.Shaders.Interop.Factories;
namespace ComputeSharp.D2D1;
@@ -52,6 +53,26 @@ public static class D2D1TransformMapperFactory<T>
return new D2D1InflateTransformMapperFactory<T> { Parameters = new D2D1InflateTransformMapperFactory<T>.DynamicLeftTopRightBottomAmount { Accessor = accessor } };
}
/// <summary>
/// Creates an <see cref="ID2D1TransformMapperFactory{T}"/> instance for an affine transform.
/// </summary>
/// <param name="matrix">The fixed transformation matrix to use.</param>
/// <returns>The resulting <see cref="ID2D1TransformMapperFactory{T}"/> instance for an affine transform.</returns>
public static ID2D1TransformMapperFactory<T> Transform(Matrix3x2 matrix)
{
return new D2D1AffineTransformMapperFactory<T> { Parameters = new D2D1AffineTransformMapperFactory<T>.FixedMatrix { Amount = matrix } };
}
/// <summary>
/// Creates an <see cref="ID2D1TransformMapperFactory{T}"/> instance for an affine transform.
/// </summary>
/// <param name="accessor">The input <see cref="Accessor{TResult}"/> instance to retrieve the transformation matrix to use.</param>
/// <returns>The resulting <see cref="ID2D1TransformMapperFactory{T}"/> instance for an affine transform.</returns>
public static ID2D1TransformMapperFactory<T> Transform(Accessor<Matrix3x2> accessor)
{
return new D2D1AffineTransformMapperFactory<T> { Parameters = new D2D1AffineTransformMapperFactory<T>.DynamicMatrix { Accessor = accessor } };
}
/// <summary>
/// An accessor of parameters to be used in D2D1 transform mapping implementations.
/// </summary>
+104
ファイルの表示
@@ -0,0 +1,104 @@
using System;
using System.Numerics;
using ComputeSharp.D2D1.Shaders.Interop.Factories.Abstract;
using ComputeSharp.D2D1.Shaders.Interop.Helpers;
namespace ComputeSharp.D2D1.Shaders.Interop.Factories;
/// <summary>
/// A custom <see cref="D2D1TransformMapperFactory{T, TSelf, TParameters, TTransformMapper}"/> implementation for an affine transform.
/// </summary>
/// <typeparam name="T">The type of D2D1 pixel shader associated to the transform mapper.</typeparam>
internal sealed class D2D1AffineTransformMapperFactory<T> : D2D1TransformMapperFactory<T, D2D1AffineTransformMapperFactory<T>, Matrix3x2, D2D1AffineTransformMapperFactory<T>.TransformMapper>
where T : unmanaged, ID2D1PixelShader
{
/// <summary>
/// A <see cref="D2D1TransformMapperParametersAccessor{T, TParameters}"/> implementation for a fixed affine transform matrix.
/// </summary>
public sealed class FixedMatrix : D2D1TransformMapperParametersAccessor<T, Matrix3x2>
{
/// <summary>
/// Gets the fixed affine transform matrix.
/// </summary>
public Matrix3x2 Amount { get; init; }
/// <inheritdoc/>
public override Matrix3x2 Get(in T shader)
{
return Amount;
}
}
/// <summary>
/// A <see cref="D2D1TransformMapperParametersAccessor{T, TParameters}"/> implementation for a dynamic affine transform matrix.
/// </summary>
public sealed class DynamicMatrix : D2D1TransformMapperParametersAccessor<T, Matrix3x2>
{
/// <summary>
/// Gets the <see cref="D2D1TransformMapperFactory{T}.Accessor{TResult}"/> instance to get the dynamic affine transform matrix.
/// </summary>
public D2D1TransformMapperFactory<T>.Accessor<Matrix3x2>? Accessor { get; init; }
/// <inheritdoc/>
public override Matrix3x2 Get(in T shader)
{
return Accessor!(in shader);
}
}
/// <summary>
/// A custom <see cref="D2D1TransformMapper{T, TParameters}"/> implementation for an affine transform.
/// </summary>
public sealed class TransformMapper : D2D1TransformMapper<T, Matrix3x2>
{
/// <inheritdoc/>
protected override void TransformInputToOutput(Matrix3x2 parameters, ref Rectangle64 rectangle)
{
Transform(parameters, ref rectangle);
}
/// <inheritdoc/>
protected override void TransformOutputToInput(Matrix3x2 parameters, ref Rectangle64 rectangle)
{
if (!Matrix3x2.Invert(parameters, out Matrix3x2 matrix))
{
static void Throw()
{
throw new ArgumentException("The input transform matrix can't be inverted, so it can't be used for a transform mapper.");
}
Throw();
}
Transform(matrix, ref rectangle);
}
/// <summary>
/// Transforms a target rectangle using a given transform matrix.
/// </summary>
/// <param name="matrix">The transform matrix to use.</param>
/// <param name="rectangle">The rectangle to transform.</param>
private static void Transform(Matrix3x2 matrix, ref Rectangle64 rectangle)
{
// Get the rectangle corners
Point64 topLeft = new(rectangle.Left, rectangle.Top);
Point64 topRight = new(rectangle.Right, rectangle.Top);
Point64 bottomLeft = new(rectangle.Left, rectangle.Bottom);
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);
// 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;
rectangle = new(x, y, width, height);
}
}
}
@@ -64,6 +64,9 @@ internal sealed class D2D1InflateTransformMapperFactory<T> : D2D1TransformMapper
/// </summary>
public sealed class DynamicAmount : D2D1TransformMapperParametersAccessor<T, (int, int, int, int)>
{
/// <summary>
/// Gets the <see cref="D2D1TransformMapperFactory{T}.Accessor{TResult}"/> instance to get the dynamic inflate amount.
/// </summary>
public D2D1TransformMapperFactory<T>.Accessor<int>? Accessor { get; init; }
/// <inheritdoc/>
@@ -80,6 +83,9 @@ internal sealed class D2D1InflateTransformMapperFactory<T> : D2D1TransformMapper
/// </summary>
public sealed class DynamicLeftTopRightBottomAmount : D2D1TransformMapperParametersAccessor<T, (int, int, int, int)>
{
/// <summary>
/// Gets the <see cref="D2D1TransformMapperFactory{T}.Accessor{TResult}"/> instance to get the dynamic inflate LTRB amount.
/// </summary>
public D2D1TransformMapperFactory<T>.Accessor<(int, int, int, int)>? Accessor { get; init; }
/// <inheritdoc/>
+87
ファイルの表示
@@ -0,0 +1,87 @@
using System;
using System.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace ComputeSharp.D2D1.Shaders.Interop.Helpers;
/// <summary>
/// A 64-bit version of the <see cref="Point"/> type.
/// </summary>
internal struct Point64 : IEquatable<Point64>
{
/// <summary>
/// The left coordinate of the current point.
/// </summary>
private long x;
/// <summary>
/// The top coordinate of the current point.
/// </summary>
private long y;
/// <summary>
/// Creates a new <see cref="Point64"/> instance with the specified parameters.
/// </summary>
/// <param name="x">The horizontal coordinate of the point.</param>
/// <param name="y">The vertical coordinate of the point.</param>
public Point64(long x, long y)
{
this.x = x;
this.y = y;
}
/// <summary>
/// Gets the horizontal coordinate of the current point.
/// </summary>
public readonly long X
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.x;
}
/// <summary>
/// Gets the vertical coordinate of the current point.
/// </summary>
public readonly long Y
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this.y;
}
/// <summary>
/// Transforms the current point using an input matrix.
/// </summary>
/// <param name="matrix">The transformation matrix to use.</param>
public void 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
// the actual cross product with the point (which would not be doable otherwise).
//
// Consider a 3x2 matrix as follows:
//
// [ M11 , M12 ]
// [ M21 , M22 ]
// [ OffsetX (M31), OffsetY (M32) ]
//
// We first transform the point by doint the cross product with the 2x2 sub-matrix:
//
// [ M11, M12 ]
// [ x, y] [ M21, M22 ] = [ x * M11 + y * M21, x * M12 + y * M22]
//
// Then we sum the absolute offsets, which results in the following formula:
//
// [ 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);
}
/// <inheritdoc/>
public readonly bool Equals(Point64 other)
{
return this.x == other.x && this.y == other.y;
}
}
-13
ファイルの表示
@@ -132,19 +132,6 @@ internal struct Rectangle64 : IEquatable<Rectangle64>
this.height = height;
}
/// <summary>
/// Inflates the current rectangle by a specified amount.
/// </summary>
/// <param name="x">The horizontal inflation amount.</param>
/// <param name="y">The vertical inflation amount.</param>
public void Inflate(long x, long y)
{
this.x -= x;
this.y -= y;
this.width += (2 * x);
this.height += (2 * y);
}
/// <summary>
/// Inflates the current rectangle by a specified amount.
/// </summary>