mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 04:58:24 +01:00
SOURCE DUMP v1
This commit is contained in:
parent
bd62d51ec9
commit
fb20299e9d
386 changed files with 768360 additions and 0 deletions
39
brutal.numerics/Brutal.Numerics.Analyzers/Resources.cs
Normal file
39
brutal.numerics/Brutal.Numerics.Analyzers/Resources.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace Brutal.Numerics.Analyzers;
|
||||
|
||||
public static class Resources
|
||||
{
|
||||
// From framework analyser BRUTAL3001 Usage of System.Drawing
|
||||
public static readonly DiagnosticDescriptor SystemDrawingBrutalCore0004 = new(
|
||||
"BRUTALCORE0004",
|
||||
"Prefer Brutal.Numerics instead of System.Drawing.Color",
|
||||
"Brutal.Numerics has colour constant types",
|
||||
"Obsolete",
|
||||
DiagnosticSeverity.Warning,
|
||||
isEnabledByDefault: true,
|
||||
description: "Use Brutal.Numerics instead of System.Drawing.Color."
|
||||
);
|
||||
|
||||
// From framework analyser BRUTAL3002 Usage of System.Numerics
|
||||
public static readonly DiagnosticDescriptor SystemNumericsBrutalCore0005 = new(
|
||||
"BRUTALCORE0005",
|
||||
"Prefer Brutal.Numerics instead of System.Numerics",
|
||||
"Brutal.Numerics provides optimised numeric functions",
|
||||
"Obsolete",
|
||||
DiagnosticSeverity.Info,
|
||||
isEnabledByDefault: true,
|
||||
description: "Brutal.Numerics instead of System.Numerics."
|
||||
);
|
||||
|
||||
// From framework analyser BRUTAL4002 Usage of float3.Rgb(...) to create grayscale colors
|
||||
public static readonly DiagnosticDescriptor RgbToGreyBrutalCore0006 = new(
|
||||
"BRUTALCORE0006",
|
||||
"Prefer Brutal.Numerics.Color.Gray instead of specifying explicit color channel values",
|
||||
"Brutal.Numerics provides a set of predefined colors",
|
||||
"Usage",
|
||||
DiagnosticSeverity.Info,
|
||||
isEnabledByDefault: true,
|
||||
description: "Brutal.Numerics instead of System.Numerics."
|
||||
);
|
||||
}
|
||||
143
brutal.numerics/Brutal.Numerics.Analyzers/RgbToGrayAnalyzer.cs
Normal file
143
brutal.numerics/Brutal.Numerics.Analyzers/RgbToGrayAnalyzer.cs
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
using System.Collections.Immutable;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Operations;
|
||||
|
||||
namespace Brutal.Numerics.Analyzers;
|
||||
|
||||
/// <summary>
|
||||
/// Analyzer that flags usage of Brutal color types such as `float3.Rgb(...)` and suggests replacing it with
|
||||
/// `float3.Grayscale()`.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public class RgbToGrayAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
// Keep in mind: you have to list your rules here.
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
|
||||
ImmutableArray.Create(Resources.RgbToGreyBrutalCore0006);
|
||||
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
// You must call this method to avoid analyzing generated code.
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
|
||||
|
||||
// You must call this method to enable the Concurrent Execution.
|
||||
context.EnableConcurrentExecution();
|
||||
|
||||
// Only interested in invocation operations, like `float3.Rgb(...)`
|
||||
context.RegisterOperationAction(AnalyzeInvocation, OperationKind.Invocation);
|
||||
|
||||
// Check other 'context.Register...' methods that might be helpful for your purposes.
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called for each invocation expression in the code.
|
||||
/// </summary>
|
||||
/// <param name="context">Operation context.</param>
|
||||
private void AnalyzeInvocation(OperationAnalysisContext context)
|
||||
{
|
||||
if (context.Operation is not IInvocationOperation invocation)
|
||||
return;
|
||||
|
||||
var methodSymbol = invocation.TargetMethod;
|
||||
|
||||
// Must be in Brutal namespace
|
||||
if (methodSymbol.ContainingNamespace.ToDisplayString() != "Brutal.Numerics")
|
||||
return;
|
||||
|
||||
// Color type must match allowed set
|
||||
var typeName = methodSymbol.ContainingType.Name;
|
||||
switch (typeName)
|
||||
{
|
||||
case "float3":
|
||||
case "float4":
|
||||
case "byte3":
|
||||
case "byte4":
|
||||
case "ushort3":
|
||||
case "ushort4":
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
// Only check methods named Rgb or Rgba
|
||||
var methodName = methodSymbol.Name;
|
||||
if (methodName != "Rgb" && methodName != "Rgba")
|
||||
return;
|
||||
|
||||
// Check argument count and argument equality
|
||||
var args = invocation.Arguments;
|
||||
if (methodName == "Rgb")
|
||||
{
|
||||
if (args.Length != 3)
|
||||
return;
|
||||
if (!ArgsAreEqual(args[0], args[1], args[2]))
|
||||
return;
|
||||
}
|
||||
else if (methodName == "Rgba")
|
||||
{
|
||||
if (args.Length != 4)
|
||||
return;
|
||||
if (!ArgsAreEqual(args[0], args[1], args[2]))
|
||||
return;
|
||||
|
||||
var alphaLiteral = args[3].Syntax.ToString();
|
||||
|
||||
switch (typeName)
|
||||
{
|
||||
case "float4":
|
||||
if (alphaLiteral != "1")
|
||||
return;
|
||||
break;
|
||||
case "byte4":
|
||||
if (alphaLiteral != "0xFF")
|
||||
return;
|
||||
break;
|
||||
case "ushort4":
|
||||
if (alphaLiteral != "0xFFFF")
|
||||
return;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
return;
|
||||
|
||||
var location = invocation.Syntax.GetLocation();
|
||||
|
||||
if (invocation.Syntax is InvocationExpressionSyntax invocationExpression)
|
||||
{
|
||||
ExpressionSyntax expr = invocationExpression;
|
||||
|
||||
// Get location of method name
|
||||
var methodNameToken = expr switch
|
||||
{
|
||||
IdentifierNameSyntax id => id.Identifier, // Simple method call like Foo()
|
||||
MemberAccessExpressionSyntax memberAccess => memberAccess.Name.Identifier, // Call like obj.Method()
|
||||
// Handle cases like Console.WriteLine(...)
|
||||
InvocationExpressionSyntax nestedInvocation => nestedInvocation.Expression switch
|
||||
{
|
||||
MemberAccessExpressionSyntax nestedMemberAccess => nestedMemberAccess.Name.Identifier, // Nested like Console.WriteLine(...)
|
||||
IdentifierNameSyntax nestedId => nestedId.Identifier, // Nested like Foo.Bar()
|
||||
_ => default,
|
||||
},
|
||||
_ => default,
|
||||
};
|
||||
|
||||
location = methodNameToken.GetLocation();
|
||||
}
|
||||
|
||||
// Emit a diagnostic at the location of the entire invocation
|
||||
context.ReportDiagnostic(Diagnostic.Create(Resources.RgbToGreyBrutalCore0006, location));
|
||||
}
|
||||
|
||||
private static bool ArgsAreEqual(IArgumentOperation a, IArgumentOperation b, IArgumentOperation c)
|
||||
{
|
||||
var t0 = a.Syntax.ToString();
|
||||
var t1 = b.Syntax.ToString();
|
||||
var t2 = c.Syntax.ToString();
|
||||
return t0 == t1 && t0 == t2;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
using System.Collections.Immutable;
|
||||
using Brutal.Numerics.Analyzers;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Operations;
|
||||
|
||||
namespace Brutal.Numerics.Analyzers;
|
||||
|
||||
/// <summary>
|
||||
/// Analyzer that flags usage of System.Drawing.Color.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public class SystemDrawingAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
// Keep in mind: you have to list your rules here.
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
|
||||
ImmutableArray.Create(Resources.SystemDrawingBrutalCore0004);
|
||||
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
// You must call this method to avoid analyzing generated code.
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
|
||||
|
||||
// You must call this method to enable the Concurrent Execution.
|
||||
context.EnableConcurrentExecution();
|
||||
|
||||
// Subscribe to semantic (compile time) action invocation, e.g. method invocation.
|
||||
context.RegisterOperationAction(
|
||||
AnalyzeOperation,
|
||||
OperationKind.FieldReference,
|
||||
OperationKind.PropertyReference
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executed on the completion of the semantic analysis associated with the Invocation operation.
|
||||
/// </summary>
|
||||
/// <param name="context">Operation context.</param>
|
||||
private void AnalyzeOperation(OperationAnalysisContext context)
|
||||
{
|
||||
// Check if the current operation is a reference to a member (field or property), such as accessing Color.Red.
|
||||
// If not, exit early as this analyzer only targets member references.
|
||||
if (context.Operation is not IMemberReferenceOperation memberRef)
|
||||
return;
|
||||
|
||||
// Get the type that contains the referenced member, e.g., System.Drawing.Color.
|
||||
var containingType = memberRef.Member.ContainingType;
|
||||
if (containingType == null)
|
||||
return;
|
||||
|
||||
// Get the namespace of the containing type (e.g. "System.Drawing").
|
||||
var namespaceName = containingType.ContainingNamespace?.ToDisplayString();
|
||||
|
||||
// Validate the namespace and type name. If the namespace is null or empty, ignore the operation. If the
|
||||
// namespace does not start with "System.Drawing" or if the type is not specifically "Color", ignore it.
|
||||
// Together, this ensures we only target usages like System.Drawing.Color.Red.
|
||||
if (
|
||||
namespaceName != null
|
||||
&& (
|
||||
string.IsNullOrEmpty(namespaceName)
|
||||
|| !namespaceName.StartsWith("System.Drawing", StringComparison.Ordinal)
|
||||
|| containingType.Name != "Color"
|
||||
)
|
||||
)
|
||||
return;
|
||||
|
||||
// At this point, we've confirmed a usage of System.Drawing.Color.<member>. Create and report a diagnostic at
|
||||
// the location where the member is used.
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(Resources.SystemDrawingBrutalCore0004, memberRef.Syntax.GetLocation())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using Brutal.Numerics.Analyzers;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.Diagnostics;
|
||||
using Microsoft.CodeAnalysis.Operations;
|
||||
|
||||
namespace Brutal.Numerics.Analyzers;
|
||||
|
||||
/// <summary>
|
||||
/// Analyzer that reports invalid usage of System.Numerics.
|
||||
/// </summary>
|
||||
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||
public class SystemNumericsAnalyzer : DiagnosticAnalyzer
|
||||
{
|
||||
// Keep in mind: you have to list your rules here.
|
||||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
|
||||
ImmutableArray.Create(Resources.SystemNumericsBrutalCore0005);
|
||||
|
||||
public override void Initialize(AnalysisContext context)
|
||||
{
|
||||
// You must call this method to avoid analyzing generated code.
|
||||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
|
||||
|
||||
// You must call this method to enable the Concurrent Execution.
|
||||
context.EnableConcurrentExecution();
|
||||
|
||||
// Subscribe to semantic (compile time) action invocation, e.g. method invocation.
|
||||
context.RegisterOperationAction(AnalyzeOperation, OperationKind.ObjectCreation);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executed on the completion of the semantic analysis associated with the Invocation operation.
|
||||
/// </summary>
|
||||
/// <param name="context">Operation context.</param>
|
||||
private void AnalyzeOperation(OperationAnalysisContext context)
|
||||
{
|
||||
// Check if the operation is an object creation (ie., 'new TypeName(...)'). Specifically targeting cases like
|
||||
// 'new Vector2(...)'.
|
||||
if (context.Operation is not IObjectCreationOperation objectCreation)
|
||||
return;
|
||||
|
||||
// Retrieve the type being instantiated
|
||||
var containingType = objectCreation.Type;
|
||||
if (containingType == null)
|
||||
return;
|
||||
|
||||
// Get the namespace of the type (e.g. "System.Numerics").
|
||||
var namespaceName = containingType.ContainingNamespace?.ToDisplayString();
|
||||
|
||||
// Ensure the namespace is valid and starts with "System.Numerics". If null or empty, the type is not from a
|
||||
// relevant namespace. If the namespace doesn't match, ignore the operation.
|
||||
if (
|
||||
namespaceName != null
|
||||
&& (
|
||||
string.IsNullOrEmpty(namespaceName)
|
||||
|| !namespaceName.StartsWith("System.Numerics", StringComparison.Ordinal)
|
||||
)
|
||||
)
|
||||
return;
|
||||
|
||||
// At this point, we've identified an instantiation of a System.Numerics type. Create and report a diagnostic
|
||||
// at the source location of the 'new' expression.git
|
||||
context.ReportDiagnostic(
|
||||
Diagnostic.Create(Resources.SystemNumericsBrutalCore0005, context.Operation.Syntax.GetLocation())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,215 @@
|
|||
using System.Collections.Immutable;
|
||||
using System.Composition;
|
||||
using Brutal.Numerics.Analyzers;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CodeActions;
|
||||
using Microsoft.CodeAnalysis.CodeFixes;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Document = Microsoft.CodeAnalysis.Document;
|
||||
|
||||
namespace Brutal.Numerics.Analyzers;
|
||||
|
||||
/// <summary>
|
||||
/// A code fix provider that replaces calls to float3.Rgb(...) with float3.Grayscale().
|
||||
/// All code fixes must be linked to specific analyzers.
|
||||
/// </summary>
|
||||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(RgbToGrayFixProvider)), Shared]
|
||||
public class RgbToGrayFixProvider : CodeFixProvider
|
||||
{
|
||||
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } =
|
||||
[Resources.RgbToGreyBrutalCore0006.Id];
|
||||
|
||||
// If you don't need the 'fix all' behavior, return null.
|
||||
public override FixAllProvider? GetFixAllProvider() => null;
|
||||
|
||||
public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
|
||||
{
|
||||
// We link only one diagnostic and assume there is only one diagnostic in the context.
|
||||
if (context.Diagnostics.Length != 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var diagnostic = context.Diagnostics[0];
|
||||
var diagnosticSpan = diagnostic.Location.SourceSpan;
|
||||
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
|
||||
var diagnosticNode = root?.FindNode(diagnosticSpan);
|
||||
|
||||
var invocation = diagnosticNode?.FirstAncestorOrSelf<InvocationExpressionSyntax>();
|
||||
if (invocation is null)
|
||||
return;
|
||||
|
||||
context.RegisterCodeFix(
|
||||
CodeAction.Create(
|
||||
title: string.Format(Resources.RgbToGreyBrutalCore0006.Id),
|
||||
createChangedDocument: c => ReplaceWithGrayscaleAsync(context.Document, invocation, c),
|
||||
equivalenceKey: nameof(Resources.RgbToGreyBrutalCore0006.Id)
|
||||
),
|
||||
diagnostic
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executed on the quick fix action raised by the user.
|
||||
/// </summary>
|
||||
/// <param name="document">Affected source file.</param>
|
||||
/// <param name="invocation">The original method invocation node.</param>
|
||||
/// <param name="cancellationToken">Any fix is cancellable by the user, so we should support the cancellation token.</param>
|
||||
/// <returns>A modified version of the solution with the method call replaced.</returns>
|
||||
private async Task<Document> ReplaceWithGrayscaleAsync(
|
||||
Document document,
|
||||
InvocationExpressionSyntax invocation,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess)
|
||||
return document;
|
||||
|
||||
var method = await TryGetReplaceableMethod(memberAccess, document, cancellationToken);
|
||||
if (
|
||||
method == null
|
||||
|| !IsMethodOfReplaceableType(method, out var typeName)
|
||||
|| !IsMethodParametersReplaceableByGrayscale(invocation, method.Name, typeName, out var replacementArgExpr)
|
||||
)
|
||||
return document;
|
||||
|
||||
var newInvocation = CreateFunctionCallToGrayscale(invocation, memberAccess, replacementArgExpr!);
|
||||
var newRoot = await ReplaceNode(document, invocation, cancellationToken, newInvocation);
|
||||
|
||||
return document.WithSyntaxRoot(newRoot);
|
||||
}
|
||||
|
||||
private static bool IsMethodOfReplaceableType(IMethodSymbol methodSymbol, out string typeName)
|
||||
{
|
||||
typeName = methodSymbol.ContainingType.Name;
|
||||
switch (typeName)
|
||||
{
|
||||
case "float3" or "float4" or "byte3" or "byte4" or "ushort3" or "ushort4":
|
||||
var methodName = methodSymbol.Name;
|
||||
return methodName is "Rgb" or "Rgba";
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<IMethodSymbol?> TryGetReplaceableMethod(
|
||||
MemberAccessExpressionSyntax memberAccess,
|
||||
Document document,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
|
||||
var symbolInfo = semanticModel.GetSymbolInfo(memberAccess, cancellationToken);
|
||||
if (
|
||||
symbolInfo.Symbol is not IMethodSymbol methodSymbol
|
||||
|| methodSymbol.ContainingNamespace.ToDisplayString() != "Brutal.Numerics"
|
||||
)
|
||||
return null;
|
||||
|
||||
return methodSymbol;
|
||||
}
|
||||
|
||||
private static bool IsMethodParametersReplaceableByGrayscale(
|
||||
InvocationExpressionSyntax invocation,
|
||||
string methodName,
|
||||
string typeName,
|
||||
out ExpressionSyntax? replacementArgExpr
|
||||
)
|
||||
{
|
||||
var args = invocation.ArgumentList.Arguments;
|
||||
|
||||
switch (methodName)
|
||||
{
|
||||
case "Rgb" when args.Count != 3 || !ArgsAreEqual(args[0], args[1], args[2]):
|
||||
replacementArgExpr = null;
|
||||
return false;
|
||||
case "Rgb":
|
||||
replacementArgExpr = args[0].Expression;
|
||||
break;
|
||||
case "Rgba" when args.Count != 4 || !ArgsAreEqual(args[0], args[1], args[2]):
|
||||
replacementArgExpr = null;
|
||||
return false;
|
||||
case "Rgba":
|
||||
{
|
||||
var alphaLiteral = args[3].Expression.ToString();
|
||||
|
||||
switch (typeName)
|
||||
{
|
||||
case "float4":
|
||||
if (alphaLiteral != "1")
|
||||
{
|
||||
replacementArgExpr = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
case "byte4":
|
||||
if (alphaLiteral != "0xFF")
|
||||
{
|
||||
replacementArgExpr = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
case "ushort4":
|
||||
if (alphaLiteral != "0xFFFF")
|
||||
{
|
||||
replacementArgExpr = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
replacementArgExpr = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
replacementArgExpr = args[0].Expression;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
replacementArgExpr = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static InvocationExpressionSyntax CreateFunctionCallToGrayscale(
|
||||
InvocationExpressionSyntax invocation,
|
||||
MemberAccessExpressionSyntax memberAccess,
|
||||
ExpressionSyntax replacementArgExpr
|
||||
)
|
||||
{
|
||||
var newMemberAccess = memberAccess.WithName(SyntaxFactory.IdentifierName("Grayscale"));
|
||||
var newInvocation = invocation
|
||||
.WithExpression(newMemberAccess)
|
||||
.WithArgumentList(
|
||||
SyntaxFactory.ArgumentList(
|
||||
SyntaxFactory.SingletonSeparatedList(SyntaxFactory.Argument(replacementArgExpr))
|
||||
)
|
||||
);
|
||||
return newInvocation;
|
||||
}
|
||||
|
||||
private static async Task<SyntaxNode> ReplaceNode(
|
||||
Document document,
|
||||
InvocationExpressionSyntax invocation,
|
||||
CancellationToken cancellationToken,
|
||||
InvocationExpressionSyntax newInvocation
|
||||
)
|
||||
{
|
||||
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
|
||||
var newRoot = root!.ReplaceNode(invocation, newInvocation);
|
||||
return newRoot;
|
||||
}
|
||||
|
||||
private static bool ArgsAreEqual(ArgumentSyntax a, ArgumentSyntax b, ArgumentSyntax c)
|
||||
{
|
||||
var t0 = a.ToString();
|
||||
var t1 = b.ToString();
|
||||
var t2 = c.ToString();
|
||||
return t0 == t1 && t0 == t2;
|
||||
}
|
||||
}
|
||||
104
brutal.numerics/Brutal.Numerics/Color/Color.Preset.cs
Normal file
104
brutal.numerics/Brutal.Numerics/Color/Color.Preset.cs
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
namespace Brutal.Numerics;
|
||||
|
||||
/// <summary>
|
||||
/// Manages colour globals, and some utilities
|
||||
/// </summary>
|
||||
public static partial class Color
|
||||
{
|
||||
/// <summary>
|
||||
/// Struct storing a default colour value in a variety of formats
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public readonly struct Preset
|
||||
{
|
||||
/// <summary>
|
||||
/// Construct a preset from a float4 colour
|
||||
/// </summary>
|
||||
/// <param name="color">color</param>
|
||||
public Preset(float4 color)
|
||||
{
|
||||
AsFloat4 = color;
|
||||
|
||||
AsUShort4 = ushort4.Pack(AsFloat4, Pack.Float.Normalize);
|
||||
AsByte4 = byte4.Pack(AsFloat4, Pack.Float.Normalize);
|
||||
}
|
||||
|
||||
[FieldOffset(0)]
|
||||
public readonly float4 AsFloat4;
|
||||
|
||||
[FieldOffset(0)]
|
||||
public readonly float3 AsFloat3;
|
||||
|
||||
[FieldOffset(16)]
|
||||
public readonly ushort4 AsUShort4;
|
||||
|
||||
[FieldOffset(16)]
|
||||
public readonly ushort3 AsUShort3;
|
||||
|
||||
[FieldOffset(24)]
|
||||
public readonly byte4 AsByte4;
|
||||
|
||||
[FieldOffset(24)]
|
||||
public readonly byte3 AsByte3;
|
||||
|
||||
public static implicit operator byte3(in Preset preset)
|
||||
{
|
||||
return preset.AsByte3;
|
||||
}
|
||||
|
||||
public static implicit operator byte4(in Preset preset)
|
||||
{
|
||||
return preset.AsByte4;
|
||||
}
|
||||
|
||||
public static implicit operator ushort3(in Preset preset)
|
||||
{
|
||||
return preset.AsUShort3;
|
||||
}
|
||||
|
||||
public static implicit operator ushort4(in Preset preset)
|
||||
{
|
||||
return preset.AsUShort4;
|
||||
}
|
||||
|
||||
public static implicit operator float3(in Preset preset)
|
||||
{
|
||||
return preset.AsFloat3;
|
||||
}
|
||||
|
||||
public static implicit operator float4(in Preset preset)
|
||||
{
|
||||
return preset.AsFloat4;
|
||||
}
|
||||
|
||||
public static implicit operator Preset(in float4 vector)
|
||||
{
|
||||
return new Preset(vector);
|
||||
}
|
||||
|
||||
public static implicit operator Preset(in float3 vector)
|
||||
{
|
||||
return new Preset(new float4(vector, 1));
|
||||
}
|
||||
|
||||
public static implicit operator Preset(in byte3 vector)
|
||||
{
|
||||
return new byte4(vector, byte.MaxValue);
|
||||
}
|
||||
|
||||
public static implicit operator Preset(in byte4 vector)
|
||||
{
|
||||
return new Preset(float4.Unpack(vector, Unpack.Float.Normalize));
|
||||
}
|
||||
|
||||
public static implicit operator Preset(in ushort3 vector)
|
||||
{
|
||||
return new ushort4(vector, ushort.MaxValue);
|
||||
}
|
||||
|
||||
public static implicit operator Preset(in ushort4 vector)
|
||||
{
|
||||
return new Preset(float4.Unpack(vector, Unpack.Float.Normalize));
|
||||
}
|
||||
}
|
||||
}
|
||||
20
brutal.numerics/Brutal.Numerics/Color/Color.cs
Normal file
20
brutal.numerics/Brutal.Numerics/Color/Color.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
namespace Brutal.Numerics;
|
||||
|
||||
/// <summary>
|
||||
/// Manages colour globals, and some utilities
|
||||
/// </summary>
|
||||
public static partial class Color
|
||||
{
|
||||
public static readonly Preset Black = byte3.Grayscale(0);
|
||||
public static readonly Preset White = byte3.Grayscale(0xFF);
|
||||
public static readonly Preset Gray = float3.Grayscale(0.5f);
|
||||
public static readonly Preset Clear = byte4.HexColor(0x000000000);
|
||||
|
||||
public static readonly Preset Red = byte3.HexColor(0xFF0000);
|
||||
public static readonly Preset Green = byte3.HexColor(0x00FF00);
|
||||
public static readonly Preset Blue = byte3.HexColor(0x0000FF);
|
||||
|
||||
public static readonly Preset Cyan = byte3.HexColor(0x00FFFF);
|
||||
public static readonly Preset Magenta = byte3.HexColor(0xFF00FF);
|
||||
public static readonly Preset Yellow = byte3.HexColor(0xFFFF00);
|
||||
}
|
||||
9
brutal.numerics/Brutal.Numerics/GlobalUsings.cs
Normal file
9
brutal.numerics/Brutal.Numerics/GlobalUsings.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
global using System.Diagnostics;
|
||||
global using System.Diagnostics.CodeAnalysis;
|
||||
global using System.Globalization;
|
||||
global using System.Numerics;
|
||||
global using System.Runtime.CompilerServices;
|
||||
global using System.Runtime.InteropServices;
|
||||
global using System.Runtime.Intrinsics;
|
||||
global using System.Text.Unicode;
|
||||
global using System.Xml.Serialization;
|
||||
318
brutal.numerics/Brutal.Numerics/Matrix/IMatrix.cs
Normal file
318
brutal.numerics/Brutal.Numerics/Matrix/IMatrix.cs
Normal file
|
|
@ -0,0 +1,318 @@
|
|||
namespace Brutal.Numerics;
|
||||
|
||||
/// <summary>Defines a generic matrix type.</summary>
|
||||
/// <typeparam name="TSelf">The type that implements the interface.</typeparam>
|
||||
/// <typeparam name="T">The component type.</typeparam>
|
||||
public interface IMatrix<TSelf, T> : IEquatable<TSelf>, IFormattable
|
||||
where TSelf : IMatrix<TSelf, T>
|
||||
{
|
||||
/// <summary>Gets the stride of one component in bytes.</summary>
|
||||
static abstract int ComponentSize { get; }
|
||||
|
||||
/// <summary>Gets the number of rows.</summary>
|
||||
static abstract int RowCount { get; }
|
||||
|
||||
/// <summary>Gets the number of columns.</summary>
|
||||
static abstract int ColumnCount { get; }
|
||||
|
||||
/// <summary>Gets or sets the element at the specified row and column.</summary>
|
||||
/// <param name="row">The row of the element to get or set.</param>
|
||||
/// <param name="column">The column of the element to get or set.</param>
|
||||
/// <returns>The element at <paramref name="row" />, <paramref name="column" />.</returns>
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// <paramref name="row" /> was less than zero or greater than the number of
|
||||
/// rows.
|
||||
/// </exception>
|
||||
/// <exception cref="ArgumentOutOfRangeException">
|
||||
/// <paramref name="column" /> was less than zero or greater than the number
|
||||
/// of columns.
|
||||
/// </exception>
|
||||
T this[int row, int column] { get; set; }
|
||||
|
||||
/// <summary>Gets the identity matrix.</summary>
|
||||
static abstract TSelf Identity { get; }
|
||||
|
||||
/// <summary>Returns true if this matrix is the identity matrix.</summary>
|
||||
bool IsIdentity { get; }
|
||||
|
||||
/// <summary>Construct a matrix of this type from a span</summary>
|
||||
static abstract TSelf Create(ReadOnlySpan<T> span);
|
||||
|
||||
/// <summary>Cast this value to a span</summary>
|
||||
Span<T> AsSpan();
|
||||
|
||||
/// <summary>A read-only view over the elements, safe in the readonly contexts where <see cref="AsSpan" /> would view a defensive copy.</summary>
|
||||
ReadOnlySpan<T> AsReadOnlySpan();
|
||||
}
|
||||
|
||||
/// <summary>Everything a 3x2 (2D affine) floating-point matrix can do.
|
||||
/// Multiplying by a <typeparamref name="TVector2" /> yields a <typeparamref name="TVector3" />.</summary>
|
||||
public interface IFloatingMatrix3x2<TSelf, TComponent, TVector2, TVector3>
|
||||
: IMatrix<TSelf, TComponent>,
|
||||
IMultiplyOperators<TSelf, TVector2, TVector3>
|
||||
where TSelf : IFloatingMatrix3x2<TSelf, TComponent, TVector2, TVector3>
|
||||
{
|
||||
/// <summary>Static-method twin of the <c>matrix * vector2</c> operator (expands to 3 components).</summary>
|
||||
static abstract TVector3 Multiply(TSelf matrix, TVector2 vector);
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.Translation" />
|
||||
TVector2 Translation { get; }
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.GetDeterminant" />
|
||||
TComponent GetDeterminant();
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.Invert" />
|
||||
static abstract bool Invert(TSelf matrix, out TSelf result);
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateRotation(float)" />
|
||||
static abstract TSelf CreateRotation(TComponent radians);
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateSkew(float, float)" />
|
||||
static abstract TSelf CreateSkew(TComponent radiansX, TComponent radiansY);
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateSkew(float, float, Vector2)" />
|
||||
static abstract TSelf CreateSkew(TComponent radiansX, TComponent radiansY, TVector2 centerPoint);
|
||||
}
|
||||
|
||||
/// <summary>Everything a 4x4 floating-point matrix can do.
|
||||
/// The operators and factories are declared inline for the same CS0695 reason as the vector bundles.
|
||||
/// CreateReflection/CreateShadow are absent: they take a plane, and planes have no interface yet.</summary>
|
||||
public interface IFloatingMatrix4x4<TSelf, TComponent, TVector3, TVector4, TQuaternion> : IMatrix<TSelf, TComponent>
|
||||
where TSelf : IFloatingMatrix4x4<TSelf, TComponent, TVector3, TVector4, TQuaternion>
|
||||
{
|
||||
/// <inheritdoc cref="Matrix4x4.Translation" />
|
||||
TVector3 Translation { get; }
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.GetDeterminant" />
|
||||
TComponent GetDeterminant();
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.op_Addition" />
|
||||
static abstract TSelf operator +(TSelf left, TSelf right);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.op_Subtraction" />
|
||||
static abstract TSelf operator -(TSelf left, TSelf right);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.op_UnaryNegation" />
|
||||
static abstract TSelf operator -(TSelf value);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.op_Multiply(Matrix4x4, Matrix4x4)" />
|
||||
static abstract TSelf operator *(TSelf left, TSelf right);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.op_Multiply(Matrix4x4, float)" />
|
||||
static abstract TSelf operator *(TSelf left, TComponent right);
|
||||
|
||||
/// <summary>Multiplies a matrix by a column vector: each result component is the dot of a row with the vector.</summary>
|
||||
/// <remarks>The W-row translation is not applied; transform points with vector-times-matrix, which is the row-vector product.</remarks>
|
||||
static abstract TVector4 operator *(TSelf matrix, TVector4 vector);
|
||||
|
||||
/// <inheritdoc cref="operator *(TSelf, TVector4)" />
|
||||
static abstract TVector4 Multiply(TSelf matrix, TVector4 vector);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.Lerp" />
|
||||
static abstract TSelf Lerp(TSelf a, TSelf b, TComponent amount);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.Transpose" />
|
||||
static abstract TSelf Transpose(TSelf matrix);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.Transform" />
|
||||
static abstract TSelf Transform(TSelf value, TQuaternion rotation);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.Invert" />
|
||||
static abstract bool Invert(TSelf matrix, out TSelf result);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.Decompose" />
|
||||
static abstract bool Decompose(
|
||||
TSelf matrix,
|
||||
out TVector3 scale,
|
||||
out TQuaternion rotation,
|
||||
out TVector3 translation
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateBillboard" />
|
||||
static abstract TSelf CreateBillboard(
|
||||
TVector3 objectPosition,
|
||||
TVector3 cameraPosition,
|
||||
TVector3 cameraUpVector,
|
||||
TVector3 cameraForwardVector
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateConstrainedBillboard" />
|
||||
static abstract TSelf CreateConstrainedBillboard(
|
||||
TVector3 objectPosition,
|
||||
TVector3 cameraPosition,
|
||||
TVector3 rotateAxis,
|
||||
TVector3 cameraForwardVector,
|
||||
TVector3 objectForwardVector
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateFromAxisAngle" />
|
||||
static abstract TSelf CreateFromAxisAngle(TVector3 axis, TComponent angle);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateFromQuaternion" />
|
||||
static abstract TSelf CreateFromQuaternion(TQuaternion quaternion);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateFromYawPitchRoll" />
|
||||
static abstract TSelf CreateFromYawPitchRoll(TComponent yaw, TComponent pitch, TComponent roll);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateLookAt" />
|
||||
static abstract TSelf CreateLookAt(TVector3 cameraPosition, TVector3 cameraTarget, TVector3 cameraUpVector);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateLookAtLeftHanded" />
|
||||
static abstract TSelf CreateLookAtLeftHanded(
|
||||
TVector3 cameraPosition,
|
||||
TVector3 cameraTarget,
|
||||
TVector3 cameraUpVector
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateLookTo" />
|
||||
static abstract TSelf CreateLookTo(TVector3 cameraPosition, TVector3 cameraDirection, TVector3 cameraUpVector);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateLookToLeftHanded" />
|
||||
static abstract TSelf CreateLookToLeftHanded(
|
||||
TVector3 cameraPosition,
|
||||
TVector3 cameraDirection,
|
||||
TVector3 cameraUpVector
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateOrthographic" />
|
||||
static abstract TSelf CreateOrthographic(
|
||||
TComponent width,
|
||||
TComponent height,
|
||||
TComponent zNearPlane,
|
||||
TComponent zFarPlane
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateOrthographicOffCenter" />
|
||||
static abstract TSelf CreateOrthographicOffCenter(
|
||||
TComponent left,
|
||||
TComponent right,
|
||||
TComponent bottom,
|
||||
TComponent top,
|
||||
TComponent zNearPlane,
|
||||
TComponent zFarPlane
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateOrthographicOffCenterLeftHanded" />
|
||||
static abstract TSelf CreateOrthographicOffCenterLeftHanded(
|
||||
TComponent left,
|
||||
TComponent right,
|
||||
TComponent bottom,
|
||||
TComponent top,
|
||||
TComponent zNearPlane,
|
||||
TComponent zFarPlane
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreatePerspective" />
|
||||
static abstract TSelf CreatePerspective(
|
||||
TComponent width,
|
||||
TComponent height,
|
||||
TComponent nearPlaneDistance,
|
||||
TComponent farPlaneDistance
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreatePerspectiveLeftHanded" />
|
||||
static abstract TSelf CreatePerspectiveLeftHanded(
|
||||
TComponent width,
|
||||
TComponent height,
|
||||
TComponent nearPlaneDistance,
|
||||
TComponent farPlaneDistance
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreatePerspectiveFieldOfView" />
|
||||
static abstract TSelf CreatePerspectiveFieldOfView(
|
||||
TComponent fieldOfView,
|
||||
TComponent aspectRatio,
|
||||
TComponent nearPlaneDistance,
|
||||
TComponent farPlaneDistance
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreatePerspectiveFieldOfViewLeftHanded" />
|
||||
static abstract TSelf CreatePerspectiveFieldOfViewLeftHanded(
|
||||
TComponent fieldOfView,
|
||||
TComponent aspectRatio,
|
||||
TComponent nearPlaneDistance,
|
||||
TComponent farPlaneDistance
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreatePerspectiveOffCenter" />
|
||||
static abstract TSelf CreatePerspectiveOffCenter(
|
||||
TComponent left,
|
||||
TComponent right,
|
||||
TComponent bottom,
|
||||
TComponent top,
|
||||
TComponent nearPlaneDistance,
|
||||
TComponent farPlaneDistance
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreatePerspectiveOffCenterLeftHanded" />
|
||||
static abstract TSelf CreatePerspectiveOffCenterLeftHanded(
|
||||
TComponent left,
|
||||
TComponent right,
|
||||
TComponent bottom,
|
||||
TComponent top,
|
||||
TComponent nearPlaneDistance,
|
||||
TComponent farPlaneDistance
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateRotationX(float)" />
|
||||
static abstract TSelf CreateRotationX(TComponent radians);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateRotationX(float, Vector3)" />
|
||||
static abstract TSelf CreateRotationX(TComponent radians, TVector3 centerPoint);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateRotationY(float)" />
|
||||
static abstract TSelf CreateRotationY(TComponent radians);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateRotationY(float, Vector3)" />
|
||||
static abstract TSelf CreateRotationY(TComponent radians, TVector3 centerPoint);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateRotationZ(float)" />
|
||||
static abstract TSelf CreateRotationZ(TComponent radians);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateRotationZ(float, Vector3)" />
|
||||
static abstract TSelf CreateRotationZ(TComponent radians, TVector3 centerPoint);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateScale(float, float, float)" />
|
||||
static abstract TSelf CreateScale(TComponent xScale, TComponent yScale, TComponent zScale);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateScale(float, float, float, Vector3)" />
|
||||
static abstract TSelf CreateScale(TComponent xScale, TComponent yScale, TComponent zScale, TVector3 centerPoint);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateScale(Vector3)" />
|
||||
static abstract TSelf CreateScale(TVector3 scales);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateScale(float)" />
|
||||
static abstract TSelf CreateScale(TComponent scale);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateScale(float, Vector3)" />
|
||||
static abstract TSelf CreateScale(TComponent scale, TVector3 centerPoint);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateTranslation(float, float, float)" />
|
||||
static abstract TSelf CreateTranslation(TComponent xPosition, TComponent yPosition, TComponent zPosition);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateTranslation(Vector3)" />
|
||||
static abstract TSelf CreateTranslation(TVector3 position);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateViewport" />
|
||||
static abstract TSelf CreateViewport(
|
||||
TComponent x,
|
||||
TComponent y,
|
||||
TComponent width,
|
||||
TComponent height,
|
||||
TComponent minDepth,
|
||||
TComponent maxDepth
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateViewportLeftHanded" />
|
||||
static abstract TSelf CreateViewportLeftHanded(
|
||||
TComponent x,
|
||||
TComponent y,
|
||||
TComponent width,
|
||||
TComponent height,
|
||||
TComponent minDepth,
|
||||
TComponent maxDepth
|
||||
);
|
||||
|
||||
/// <inheritdoc cref="Matrix4x4.CreateWorld" />
|
||||
static abstract TSelf CreateWorld(TVector3 position, TVector3 forward, TVector3 up);
|
||||
}
|
||||
60
brutal.numerics/Brutal.Numerics/Matrix/Unsupported.double.cs
Normal file
60
brutal.numerics/Brutal.Numerics/Matrix/Unsupported.double.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
// ReSharper disable InconsistentNaming
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
// These types are not well-supported in Brutal, yet.
|
||||
// They are provided for completeness, but they may not have full functionality.
|
||||
// FEEL FREE TO IMPLEMENT THEM IF YOU NEED THEM
|
||||
|
||||
public struct double2x2(double2 x, double2 y)
|
||||
{
|
||||
public static readonly double2x2 Identity = new(new(1, 0), new(0, 1));
|
||||
|
||||
public double2 X = x;
|
||||
public double2 Y = y;
|
||||
}
|
||||
|
||||
public struct double3x3(double3 x, double3 y, double3 z)
|
||||
{
|
||||
public static readonly double3x3 Identity = new(new(1, 0, 0), new(0, 1, 0), new(0, 0, 1));
|
||||
|
||||
public double3 X = x;
|
||||
public double3 Y = y;
|
||||
public double3 Z = z;
|
||||
}
|
||||
|
||||
public struct double2x3(double2 x, double2 y, double2 z)
|
||||
{
|
||||
public double2 X = x;
|
||||
public double2 Y = y;
|
||||
public double2 Z = z;
|
||||
}
|
||||
|
||||
public struct double2x4(double4 x, double4 y)
|
||||
{
|
||||
public double4 X = x;
|
||||
public double4 Y = y;
|
||||
}
|
||||
|
||||
public struct double4x2(double2 x, double2 y, double2 z, double2 w)
|
||||
{
|
||||
public double2 X = x;
|
||||
public double2 Y = y;
|
||||
public double2 Z = z;
|
||||
public double2 W = w;
|
||||
}
|
||||
|
||||
public struct double3x4(double4 x, double4 y, double4 z)
|
||||
{
|
||||
public double4 X = x;
|
||||
public double4 Y = y;
|
||||
public double4 Z = z;
|
||||
}
|
||||
|
||||
public struct double4x3(double3 x, double3 y, double3 z, double3 w)
|
||||
{
|
||||
public double3 X = x;
|
||||
public double3 Y = y;
|
||||
public double3 Z = z;
|
||||
public double3 W = w;
|
||||
}
|
||||
60
brutal.numerics/Brutal.Numerics/Matrix/Unsupported.float.cs
Normal file
60
brutal.numerics/Brutal.Numerics/Matrix/Unsupported.float.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
// ReSharper disable InconsistentNaming
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
// These types are not well-supported in Brutal, yet.
|
||||
// They are provided for completeness, but they may not have full functionality.
|
||||
// FEEL FREE TO IMPLEMENT THEM IF YOU NEED THEM
|
||||
|
||||
public struct float2x2(float2 x, float2 y)
|
||||
{
|
||||
public static readonly float2x2 Identity = new(new(1, 0), new(0, 1));
|
||||
|
||||
public float2 X = x;
|
||||
public float2 Y = y;
|
||||
}
|
||||
|
||||
public struct float3x3(float3 x, float3 y, float3 z)
|
||||
{
|
||||
public static readonly float3x3 Identity = new(new(1, 0, 0), new(0, 1, 0), new(0, 0, 1));
|
||||
|
||||
public float3 X = x;
|
||||
public float3 Y = y;
|
||||
public float3 Z = z;
|
||||
}
|
||||
|
||||
public struct float2x3(float2 x, float2 y, float2 z)
|
||||
{
|
||||
public float2 X = x;
|
||||
public float2 Y = y;
|
||||
public float2 Z = z;
|
||||
}
|
||||
|
||||
public struct float2x4(float4 x, float4 y)
|
||||
{
|
||||
public float4 X = x;
|
||||
public float4 Y = y;
|
||||
}
|
||||
|
||||
public struct float4x2(float2 x, float2 y, float2 z, float2 w)
|
||||
{
|
||||
public float2 X = x;
|
||||
public float2 Y = y;
|
||||
public float2 Z = z;
|
||||
public float2 W = w;
|
||||
}
|
||||
|
||||
public struct float3x4(float4 x, float4 y, float4 z)
|
||||
{
|
||||
public float4 X = x;
|
||||
public float4 Y = y;
|
||||
public float4 Z = z;
|
||||
}
|
||||
|
||||
public struct float4x3(float3 x, float3 y, float3 z, float3 w)
|
||||
{
|
||||
public float3 X = x;
|
||||
public float3 Y = y;
|
||||
public float3 Z = z;
|
||||
public float3 W = w;
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
// <auto-generated /> - edit Matrix.access.tt to modify
|
||||
#nullable enable
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
public partial struct double3x2
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public double this[int row, int column]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
readonly get
|
||||
{
|
||||
if ((uint)row >= RowCount)
|
||||
throw new ArgumentOutOfRangeException(nameof(row));
|
||||
|
||||
return Unsafe.Add(ref Unsafe.AsRef(in X), row)[column];
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set
|
||||
{
|
||||
if ((uint)row >= RowCount)
|
||||
throw new ArgumentOutOfRangeException(nameof(row));
|
||||
|
||||
Unsafe.Add(ref X, row)[column] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly void Deconstruct(out double2 x, out double2 y, out double2 z)
|
||||
{
|
||||
x = X;
|
||||
y = Y;
|
||||
z = Z;
|
||||
}
|
||||
|
||||
public Span<double> AsSpan()
|
||||
{
|
||||
return MemoryMarshal.CreateSpan(ref Unsafe.As<double3x2, double>(ref this), 6);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly ReadOnlySpan<double> AsReadOnlySpan()
|
||||
{
|
||||
return MemoryMarshal.CreateReadOnlySpan(in Unsafe.As<double3x2, double>(ref Unsafe.AsRef(in this)), 6);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
// <auto-generated /> - edit Matrix.equality.tt to modify
|
||||
#nullable enable
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
public partial struct double3x2
|
||||
{
|
||||
/// <inheritdoc cref="Matrix3x2.Equals(object?)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override readonly bool Equals([NotNullWhen(true)] object? obj)
|
||||
{
|
||||
return obj is double3x2 other && Equals(other);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.Equals(Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly bool Equals(double3x2 other)
|
||||
{
|
||||
// This function needs to account for floating-point equality around NaN
|
||||
// and so must behave equivalently to the underlying double/double.Equals
|
||||
|
||||
return X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.GetHashCode()" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override readonly int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(X, Y, Z);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
// <auto-generated /> - edit Matrix.format.tt to modify
|
||||
#nullable enable
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
public partial struct double3x2
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public readonly string ToString(string? format, IFormatProvider? formatProvider)
|
||||
{
|
||||
return $"{{ X:{X.ToString(format, formatProvider)}, Y:{Y.ToString(format, formatProvider)}, Z:{Z.ToString(format, formatProvider)} }}";
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public readonly override string ToString()
|
||||
{
|
||||
return $"{{ X:{X}, Y:{Y}, Z:{Z} }}";
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
// <auto-generated /> - edit Matrix.tt to modify
|
||||
#nullable enable
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
/// <summary>
|
||||
/// Implements a 3x2 matrix with double components.
|
||||
/// </summary>
|
||||
[DebuggerTypeProxy(typeof(DebugView))]
|
||||
public partial struct double3x2 : IMatrix<double3x2, double>
|
||||
, IFloatingMatrix3x2<double3x2, double, double2, double3>
|
||||
{
|
||||
internal class DebugView(double3x2 m)
|
||||
{
|
||||
public double2 X => m.X;
|
||||
public double2 Y => m.Y;
|
||||
public double2 Z => m.Z;
|
||||
}
|
||||
|
||||
/// <summary>The first row of the matrix.</summary>
|
||||
[XmlIgnore]
|
||||
public double2 X;
|
||||
|
||||
/// <summary>The second row of the matrix.</summary>
|
||||
[XmlIgnore]
|
||||
public double2 Y;
|
||||
|
||||
/// <summary>The third row of the matrix.</summary>
|
||||
[XmlIgnore]
|
||||
public double2 Z;
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2(float, float, float, float, float, float)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public double3x2(
|
||||
double m11,
|
||||
double m12,
|
||||
double m21,
|
||||
double m22,
|
||||
double m31,
|
||||
double m32
|
||||
)
|
||||
{
|
||||
X = new double2(m11, m12);
|
||||
Y = new double2(m21, m22);
|
||||
Z = new double2(m31, m32);
|
||||
}
|
||||
|
||||
/// <summary>Creates a matrix from the leading 6 elements of a span, row by row.</summary>
|
||||
/// <param name="span">The elements to read; must contain at least 6, and any beyond that are ignored.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException"><paramref name="span" /> holds fewer than 6 elements.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public double3x2(ReadOnlySpan<double> span)
|
||||
{
|
||||
if (span.Length < 6)
|
||||
throw new ArgumentOutOfRangeException(nameof(span), "Length must be at least 6.");
|
||||
|
||||
this = Unsafe.ReadUnaligned<double3x2>(ref Unsafe.As<double, byte>(ref MemoryMarshal.GetReference(span)));
|
||||
}
|
||||
|
||||
static double3x2 IMatrix<double3x2, double>.Create(ReadOnlySpan<double> span) => new(span);
|
||||
|
||||
/// <inheritdoc />
|
||||
public static double3x2 Identity
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = double2.UnitX;
|
||||
result.Y = double2.UnitY;
|
||||
result.Z = double2.Zero;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public static int ComponentSize => sizeof(double);
|
||||
|
||||
/// <inheritdoc />
|
||||
public static int RowCount => 3;
|
||||
|
||||
/// <inheritdoc />
|
||||
public static int ColumnCount => 2;
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.IsIdentity" />
|
||||
public readonly bool IsIdentity
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => X == double2.UnitX && Y == double2.UnitY && Z == double2.Zero;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,381 @@
|
|||
// <auto-generated /> - edit Matrix.numerics.tt to modify
|
||||
#nullable enable
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
public partial struct double3x2
|
||||
{
|
||||
private const double RotationEpsilon = 0.001 * double.Pi / 180; // 0.1% of a degree
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.Translation" />
|
||||
[XmlIgnore]
|
||||
public double2 Translation
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
readonly get => Z;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set => Z = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateRotation(float)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateRotation(double radians)
|
||||
{
|
||||
radians = double.Ieee754Remainder(radians, double.Pi * 2);
|
||||
|
||||
double c;
|
||||
double s;
|
||||
|
||||
if (radians > -RotationEpsilon && radians < RotationEpsilon)
|
||||
{
|
||||
// Exact case for zero rotation.
|
||||
c = 1;
|
||||
s = 0;
|
||||
}
|
||||
else if (radians > double.Pi / 2 - RotationEpsilon && radians < double.Pi / 2 + RotationEpsilon)
|
||||
{
|
||||
// Exact case for 90 degree rotation.
|
||||
c = 0;
|
||||
s = 1;
|
||||
}
|
||||
else if (radians < -double.Pi + RotationEpsilon || radians > double.Pi - RotationEpsilon)
|
||||
{
|
||||
// Exact case for 180 degree rotation.
|
||||
c = -1;
|
||||
s = 0;
|
||||
}
|
||||
else if (radians > -double.Pi / 2 - RotationEpsilon && radians < -double.Pi / 2 + RotationEpsilon)
|
||||
{
|
||||
// Exact case for 270 degree rotation.
|
||||
c = 0;
|
||||
s = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Arbitrary rotation.
|
||||
(s, c) = double.SinCos(radians);
|
||||
}
|
||||
|
||||
// [ c s ]
|
||||
// [ -s c ]
|
||||
// [ 0 0 ]
|
||||
|
||||
double3x2 result;
|
||||
|
||||
result.X = new double2(c, s);
|
||||
result.Y = new double2(-s, c);
|
||||
result.Z = double2.Zero;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateRotation(float, Vector2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateRotation(double radians, double2 centerPoint)
|
||||
{
|
||||
radians = double.Ieee754Remainder(radians, double.Pi * 2);
|
||||
|
||||
double c,
|
||||
s;
|
||||
|
||||
if (radians > -RotationEpsilon && radians < RotationEpsilon)
|
||||
{
|
||||
// Exact case for zero rotation.
|
||||
c = 1;
|
||||
s = 0;
|
||||
}
|
||||
else if (radians > double.Pi / 2 - RotationEpsilon && radians < double.Pi / 2 + RotationEpsilon)
|
||||
{
|
||||
// Exact case for 90 degree rotation.
|
||||
c = 0;
|
||||
s = 1;
|
||||
}
|
||||
else if (radians < -double.Pi + RotationEpsilon || radians > double.Pi - RotationEpsilon)
|
||||
{
|
||||
// Exact case for 180 degree rotation.
|
||||
c = -1;
|
||||
s = 0;
|
||||
}
|
||||
else if (radians > -double.Pi / 2 - RotationEpsilon && radians < -double.Pi / 2 + RotationEpsilon)
|
||||
{
|
||||
// Exact case for 270 degree rotation.
|
||||
c = 0;
|
||||
s = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Arbitrary rotation.
|
||||
(s, c) = double.SinCos(radians);
|
||||
}
|
||||
|
||||
var x = centerPoint.X * (1 - c) + centerPoint.Y * s;
|
||||
var y = centerPoint.Y * (1 - c) - centerPoint.X * s;
|
||||
|
||||
// [ c s ]
|
||||
// [ -s c ]
|
||||
// [ x y ]
|
||||
|
||||
double3x2 result;
|
||||
|
||||
result.X = new double2(c, s);
|
||||
result.Y = new double2(-s, c);
|
||||
result.Z = new double2(x, y);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateScale(Vector2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateScale(double2 scales)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = new double2(scales.X, 0);
|
||||
result.Y = new double2(0, scales.Y);
|
||||
result.Z = double2.Zero;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateScale(float, float)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateScale(double scaleX, double scaleY)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = new double2(scaleX, 0);
|
||||
result.Y = new double2(0, scaleY);
|
||||
result.Z = double2.Zero;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateScale(float, float, Vector2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateScale(double scaleX, double scaleY, double2 centerPoint)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = new double2(scaleX, 0);
|
||||
result.Y = new double2(0, scaleY);
|
||||
result.Z = centerPoint * (double2.One - new double2(scaleX, scaleY));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateScale(Vector2, Vector2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateScale(double2 scales, double2 centerPoint)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = new double2(scales.X, 0);
|
||||
result.Y = new double2(0, scales.Y);
|
||||
result.Z = centerPoint * (double2.One - scales);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateScale(float)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateScale(double scale)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = new double2(scale, 0);
|
||||
result.Y = new double2(0, scale);
|
||||
result.Z = double2.Zero;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateScale(float, Vector2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateScale(double scale, double2 centerPoint)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = new double2(scale, 0);
|
||||
result.Y = new double2(0, scale);
|
||||
result.Z = centerPoint * (double2.One - new double2(scale));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateSkew(float, float)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateSkew(double radiansX, double radiansY)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = new double2(1, double.Tan(radiansY));
|
||||
result.Y = new double2(double.Tan(radiansX), 1);
|
||||
result.Z = double2.Zero;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateSkew(float, float, Vector2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateSkew(double radiansX, double radiansY, double2 centerPoint)
|
||||
{
|
||||
var xTan = double.Tan(radiansX);
|
||||
var yTan = double.Tan(radiansY);
|
||||
|
||||
var tx = -centerPoint.Y * xTan;
|
||||
var ty = -centerPoint.X * yTan;
|
||||
|
||||
double3x2 result;
|
||||
|
||||
result.X = new double2(1, yTan);
|
||||
result.Y = new double2(xTan, 1);
|
||||
result.Z = new double2(tx, ty);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateTranslation(Vector2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateTranslation(double2 position)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = double2.UnitX;
|
||||
result.Y = double2.UnitY;
|
||||
result.Z = position;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.CreateTranslation(float, float)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 CreateTranslation(double positionX, double positionY)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = double2.UnitX;
|
||||
result.Y = double2.UnitY;
|
||||
result.Z = new double2(positionX, positionY);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.Invert(Matrix3x2, out Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool Invert(double3x2 matrix, out double3x2 result)
|
||||
{
|
||||
var det = matrix.X.X * matrix.Y.Y - matrix.Y.X * matrix.X.Y;
|
||||
|
||||
if (double.Abs(det) < double.Epsilon)
|
||||
{
|
||||
var vNaN = new double2(double.NaN);
|
||||
|
||||
result.X = vNaN;
|
||||
result.Y = vNaN;
|
||||
result.Z = vNaN;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
var invDet = 1.0 / det;
|
||||
|
||||
result.X = new double2(+matrix.Y.Y * invDet, -matrix.X.Y * invDet);
|
||||
result.Y = new double2(-matrix.Y.X * invDet, +matrix.X.X * invDet);
|
||||
result.Z = new double2(
|
||||
(matrix.Y.X * matrix.Z.Y - matrix.Z.X * matrix.Y.Y) * invDet,
|
||||
(matrix.Z.X * matrix.X.Y - matrix.X.X * matrix.Z.Y) * invDet
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.Lerp(Matrix3x2, Matrix3x2, float)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 Lerp(double3x2 a, double3x2 b, double amount)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = double2.Lerp(a.X, b.X, amount);
|
||||
result.Y = double2.Lerp(a.Y, b.Y, amount);
|
||||
result.Z = double2.Lerp(a.Z, b.Z, amount);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.GetDeterminant()" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly double GetDeterminant()
|
||||
{
|
||||
// There isn't actually any such thing as a determinant for a non-square matrix,
|
||||
// but this 3x2 type is really just an optimization of a 3x3 where we happen to
|
||||
// know the rightmost column is always (0, 0, 1). So we expand to 3x3 format:
|
||||
//
|
||||
// [ X.X, X.Y, 0 ]
|
||||
// [ Y.X, Y.Y, 0 ]
|
||||
// [ Z.X, Z.Y, 1 ]
|
||||
//
|
||||
// Sum the diagonal products:
|
||||
// (X.X * Y.Y * 1) + (X.Y * 0 * Z.X) + (0 * Y.X * Z.Y)
|
||||
//
|
||||
// Subtract the opposite diagonal products:
|
||||
// (Z.X * Y.Y * 0) + (Z.Y * 0 * X.X) + (1 * Y.X * X.Y)
|
||||
//
|
||||
// Collapse out the constants and oh look, this is just a 2x2 determinant!
|
||||
|
||||
return X.X * Y.Y - Y.X * X.Y;
|
||||
}
|
||||
|
||||
/// <summary>Performs matrix multiplication to multiply a matrix by a vector to compute the product.</summary>
|
||||
/// <param name="matrix">The matrix.</param>
|
||||
/// <param name="vector">The vector.</param>
|
||||
/// <returns>The product vector.</returns>
|
||||
/// <remarks>
|
||||
/// The <see cref="Multiply(double3x2, double2)" /> method defines the operation
|
||||
/// of the multiplication operator for <see cref="double3x2" /> objects.
|
||||
/// </remarks>
|
||||
public static double3 Multiply(double3x2 matrix, double2 vector)
|
||||
{
|
||||
double3 result;
|
||||
|
||||
result.X = double2.Dot(matrix.X, vector);
|
||||
result.Y = double2.Dot(matrix.Y, vector);
|
||||
result.Z = double2.Dot(matrix.Z, vector);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.Add(Matrix3x2, Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 Add(double3x2 a, double3x2 b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.Multiply(Matrix3x2, Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 Multiply(double3x2 a, double3x2 b)
|
||||
{
|
||||
return a * b;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.Multiply(Matrix3x2, float)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 Multiply(double3x2 a, double b)
|
||||
{
|
||||
return a * b;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.Negate(Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 Negate(double3x2 value)
|
||||
{
|
||||
return -value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.Subtract(Matrix3x2, Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 Subtract(double3x2 a, double3x2 b)
|
||||
{
|
||||
return a - b;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
// <auto-generated /> - edit Matrix.operators.tt to modify
|
||||
#nullable enable
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
public partial struct double3x2
|
||||
{
|
||||
/// <inheritdoc cref="Matrix3x2.op_Addition(Matrix3x2, Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 operator +(double3x2 left, double3x2 right)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = left.X + right.X;
|
||||
result.Y = left.Y + right.Y;
|
||||
result.Z = left.Z + right.Z;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.op_Equality(Matrix3x2, Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator ==(double3x2 left, double3x2 right)
|
||||
{
|
||||
return left.X == right.X && left.Y == right.Y && left.Z == right.Z;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.op_Inequality(Matrix3x2, Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static bool operator !=(double3x2 left, double3x2 right)
|
||||
{
|
||||
return left.X != right.X || left.Y != right.Y || left.Z != right.Z;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.op_Multiply(Matrix3x2, Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 operator *(double3x2 left, double3x2 right)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = new double2(
|
||||
left.X.X * right.X.X + left.X.Y * right.Y.X,
|
||||
left.X.X * right.X.Y + left.X.Y * right.Y.Y
|
||||
);
|
||||
result.Y = new double2(
|
||||
left.Y.X * right.X.X + left.Y.Y * right.Y.X,
|
||||
left.Y.X * right.X.Y + left.Y.Y * right.Y.Y
|
||||
);
|
||||
result.Z = new double2(
|
||||
left.Z.X * right.X.X + left.Z.Y * right.Y.X + right.Z.X,
|
||||
left.Z.X * right.X.Y + left.Z.Y * right.Y.Y + right.Z.Y
|
||||
);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Multiply(double3x2, double2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3 operator *(double3x2 matrix, double2 vector)
|
||||
{
|
||||
return Multiply(matrix, vector);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.op_Multiply(Matrix3x2, float)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 operator *(double3x2 left, double right)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = left.X * right;
|
||||
result.Y = left.Y * right;
|
||||
result.Z = left.Z * right;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.op_Subtraction(Matrix3x2, Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 operator -(double3x2 left, double3x2 right)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = left.X - right.X;
|
||||
result.Y = left.Y - right.Y;
|
||||
result.Z = left.Z - right.Z;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.op_UnaryNegation(Matrix3x2)" />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static double3x2 operator -(double3x2 value)
|
||||
{
|
||||
double3x2 result;
|
||||
|
||||
result.X = -value.X;
|
||||
result.Y = -value.Y;
|
||||
result.Z = -value.Z;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
// <auto-generated /> - edit Matrix.pack.tt to modify
|
||||
#nullable enable
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
public partial struct double3x2
|
||||
{
|
||||
public static double3x2 Unpack(in float3x2 vector, Func<float, double> elementConverter)
|
||||
{
|
||||
MatrixPack.Pack(vector, out double3x2 result, elementConverter);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void Unpack(
|
||||
in ReadOnlySpan<float3x2> source,
|
||||
Span<double3x2> destination,
|
||||
Func<float, double> elementConverter
|
||||
)
|
||||
{
|
||||
MatrixPack.Pack(source, destination, elementConverter);
|
||||
}
|
||||
|
||||
public static double3x2 Unpack(in float3x2 vector)
|
||||
{
|
||||
return Unpack(vector, f => f);
|
||||
}
|
||||
|
||||
public static void Unpack(in ReadOnlySpan<float3x2> source, Span<double3x2> destination)
|
||||
{
|
||||
Unpack(source, destination, f => f);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
// <auto-generated /> - edit Matrix.swizzle.tt to modify
|
||||
#nullable enable
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
public partial struct double3x2
|
||||
{
|
||||
/// <inheritdoc cref="Matrix3x2.M11" />
|
||||
[XmlAttribute]
|
||||
public double M11
|
||||
{
|
||||
readonly get => X.X;
|
||||
set => X.X = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.M12" />
|
||||
[XmlAttribute]
|
||||
public double M12
|
||||
{
|
||||
readonly get => X.Y;
|
||||
set => X.Y = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.M21" />
|
||||
[XmlAttribute]
|
||||
public double M21
|
||||
{
|
||||
readonly get => Y.X;
|
||||
set => Y.X = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.M22" />
|
||||
[XmlAttribute]
|
||||
public double M22
|
||||
{
|
||||
readonly get => Y.Y;
|
||||
set => Y.Y = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.M31" />
|
||||
[XmlAttribute]
|
||||
public double M31
|
||||
{
|
||||
readonly get => Z.X;
|
||||
set => Z.X = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="Matrix3x2.M32" />
|
||||
[XmlAttribute]
|
||||
public double M32
|
||||
{
|
||||
readonly get => Z.Y;
|
||||
set => Z.Y = value;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
// <auto-generated /> - edit Matrix.access.tt to modify
|
||||
#nullable enable
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
public partial struct double4x4
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public double this[int row, int column]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
readonly get
|
||||
{
|
||||
if ((uint)row >= RowCount)
|
||||
throw new ArgumentOutOfRangeException(nameof(row));
|
||||
|
||||
return Unsafe.Add(ref Unsafe.AsRef(in X), row)[column];
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
set
|
||||
{
|
||||
if ((uint)row >= RowCount)
|
||||
throw new ArgumentOutOfRangeException(nameof(row));
|
||||
|
||||
Unsafe.Add(ref X, row)[column] = value;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly void Deconstruct(out double4 x, out double4 y, out double4 z, out double4 w)
|
||||
{
|
||||
x = X;
|
||||
y = Y;
|
||||
z = Z;
|
||||
w = W;
|
||||
}
|
||||
|
||||
public Span<double> AsSpan()
|
||||
{
|
||||
return MemoryMarshal.CreateSpan(ref Unsafe.As<double4x4, double>(ref this), 16);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly ReadOnlySpan<double> AsReadOnlySpan()
|
||||
{
|
||||
return MemoryMarshal.CreateReadOnlySpan(in Unsafe.As<double4x4, double>(ref Unsafe.AsRef(in this)), 16);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue