mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-26 13:08:24 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
4 changed files with 784842 additions and 0 deletions
86
Brutal.Numerics/Packing/MatrixPack.cs
Normal file
86
Brutal.Numerics/Packing/MatrixPack.cs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
using CommunityToolkit.HighPerformance;
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
/// <summary>
|
||||
/// Packing utilities for converting between matrix types
|
||||
/// </summary>
|
||||
public static class MatrixPack
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert a span of matrices of one type to another using a per-element delegate converter function
|
||||
/// </summary>
|
||||
/// <param name="source">Source span of matrices</param>
|
||||
/// <param name="destination">Destination span of matrices</param>
|
||||
/// <param name="elementConverter">Delegate for converting between source and destination elements</param>
|
||||
/// <typeparam name="TSourceType">Source element type</typeparam>
|
||||
/// <typeparam name="TDestinationElement">Destination element type</typeparam>
|
||||
/// <typeparam name="TSourceMatrix">Source matrix type</typeparam>
|
||||
/// <typeparam name="TDestinationMatrix">Destination matrix type</typeparam>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Source and destination matrix types have different element counts, or are different
|
||||
/// length spans
|
||||
/// </exception>
|
||||
public static void Pack<TSourceType, TSourceMatrix, TDestinationElement, TDestinationMatrix>(
|
||||
ReadOnlySpan<TSourceMatrix> source,
|
||||
Span<TDestinationMatrix> destination,
|
||||
Func<TSourceType, TDestinationElement> elementConverter
|
||||
)
|
||||
where TSourceMatrix : unmanaged, IMatrix<TSourceMatrix, TSourceType>
|
||||
where TDestinationMatrix : unmanaged, IMatrix<TDestinationMatrix, TDestinationElement>
|
||||
where TSourceType : unmanaged
|
||||
where TDestinationElement : unmanaged
|
||||
{
|
||||
if (destination.Length < source.Length)
|
||||
throw new ArgumentException("Destination span is not long enough to hold the converted matrices");
|
||||
if (
|
||||
TSourceMatrix.RowCount != TDestinationMatrix.RowCount
|
||||
|| TSourceMatrix.ColumnCount != TDestinationMatrix.ColumnCount
|
||||
)
|
||||
throw new ArgumentException("Source and destination matrix types have different element counts");
|
||||
|
||||
var src = source.Cast<TSourceMatrix, TSourceType>();
|
||||
var dst = destination.Cast<TDestinationMatrix, TDestinationElement>();
|
||||
|
||||
for (var i = 0; i < src.Length; i++)
|
||||
dst[i] = elementConverter(src[i]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a source matrix (cast as a span of elements) to a destination matrix (created from a span of elements)
|
||||
/// using a per-element delegate converter function
|
||||
/// This slightly quirky signature is a workaround to avoid generics hell where you have to explicitly write each type
|
||||
/// </summary>
|
||||
/// <param name="source">Source matrix</param>
|
||||
/// <param name="destination">Destination matrix, created from a span of elements</param>
|
||||
/// <param name="elementConverter">Per-element conversion delegate</param>
|
||||
/// <typeparam name="TSourceType">Element type of input span</typeparam>
|
||||
/// <typeparam name="TDestinationType">Element type of output matrix</typeparam>
|
||||
/// <typeparam name="TSourceMatrix">Type of input matrix</typeparam>
|
||||
/// <typeparam name="TDestinationMatrix">Type of output matrix</typeparam>
|
||||
public static void Pack<TSourceType, TSourceMatrix, TDestinationType, TDestinationMatrix>(
|
||||
in TSourceMatrix source,
|
||||
out TDestinationMatrix destination,
|
||||
Func<TSourceType, TDestinationType> elementConverter
|
||||
)
|
||||
where TSourceType : unmanaged
|
||||
where TDestinationType : unmanaged
|
||||
where TSourceMatrix : unmanaged, IMatrix<TSourceMatrix, TSourceType>
|
||||
where TDestinationMatrix : unmanaged, IMatrix<TDestinationMatrix, TDestinationType>
|
||||
{
|
||||
if (
|
||||
TSourceMatrix.RowCount != TDestinationMatrix.RowCount
|
||||
|| TSourceMatrix.ColumnCount != TDestinationMatrix.ColumnCount
|
||||
)
|
||||
throw new ArgumentException("Source and destination matrix types have different element counts");
|
||||
|
||||
var src = source.AsSpan();
|
||||
Span<TDestinationType> dst =
|
||||
stackalloc TDestinationType[TDestinationMatrix.RowCount * TDestinationMatrix.ColumnCount];
|
||||
|
||||
for (var i = 0; i < src.Length; i++)
|
||||
dst[i] = elementConverter(src[i]);
|
||||
|
||||
destination = TDestinationMatrix.Create(dst);
|
||||
}
|
||||
}
|
||||
1067
Brutal.Numerics/Packing/PackUtils.cs
Normal file
1067
Brutal.Numerics/Packing/PackUtils.cs
Normal file
File diff suppressed because it is too large
Load diff
79
Brutal.Numerics/Packing/QuaternionPack.cs
Normal file
79
Brutal.Numerics/Packing/QuaternionPack.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
using CommunityToolkit.HighPerformance;
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
/// <summary>
|
||||
/// Packing utilities for converting between vector types
|
||||
/// </summary>
|
||||
public static class QuaternionPack
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert a span of vectors of one type to another using a per-element delegate converter function
|
||||
/// </summary>
|
||||
/// <param name="source">Source span of vectors</param>
|
||||
/// <param name="destination">Destination span of vectors</param>
|
||||
/// <param name="elementConverter">Delegate for converting between source and destination elements</param>
|
||||
/// <typeparam name="TSourceType">Source element type</typeparam>
|
||||
/// <typeparam name="TDestinationElement">Destination element type</typeparam>
|
||||
/// <typeparam name="TSourceQuaternion">Source vector type</typeparam>
|
||||
/// <typeparam name="TDestinationQuaternion">Destination vector type</typeparam>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Source and destination vector types have different element counts, or are different
|
||||
/// length spans
|
||||
/// </exception>
|
||||
public static void Pack<TSourceType, TSourceQuaternion, TDestinationElement, TDestinationQuaternion>(
|
||||
ReadOnlySpan<TSourceQuaternion> source,
|
||||
Span<TDestinationQuaternion> destination,
|
||||
Func<TSourceType, TDestinationElement> elementConverter
|
||||
)
|
||||
where TSourceQuaternion : unmanaged, IQuaternion<TSourceQuaternion, TSourceType>
|
||||
where TDestinationQuaternion : unmanaged, IQuaternion<TDestinationQuaternion, TDestinationElement>
|
||||
where TSourceType : unmanaged
|
||||
where TDestinationElement : unmanaged
|
||||
{
|
||||
if (destination.Length < source.Length)
|
||||
throw new ArgumentException("Destination span is not long enough to hold the converted vectors");
|
||||
if (TSourceQuaternion.ComponentCount != TDestinationQuaternion.ComponentCount)
|
||||
throw new ArgumentException("Source and destination vector types have different element counts");
|
||||
|
||||
var src = source.Cast<TSourceQuaternion, TSourceType>();
|
||||
var dst = destination.Cast<TDestinationQuaternion, TDestinationElement>();
|
||||
|
||||
for (var i = 0; i < src.Length; i++)
|
||||
dst[i] = elementConverter(src[i]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a source vector (cast as a span of elements) to a destination vector (created from a span of elements)
|
||||
/// using a per-element delegate converter function
|
||||
/// This slightly quirky signature is a workaround to avoid generics hell where you have to explicitly write each type
|
||||
/// </summary>
|
||||
/// <param name="source">Source vector</param>
|
||||
/// <param name="destination">Destination vector, created from a span of elements</param>
|
||||
/// <param name="elementConverter">Per-element conversion delegate</param>
|
||||
/// <typeparam name="TSourceType">Element type of input span</typeparam>
|
||||
/// <typeparam name="TDestinationType">Element type of output vector</typeparam>
|
||||
/// <typeparam name="TSourceQuaternion">Type of input vector</typeparam>
|
||||
/// <typeparam name="TDestinationQuaternion">Type of output vector</typeparam>
|
||||
public static void Pack<TSourceType, TSourceQuaternion, TDestinationType, TDestinationQuaternion>(
|
||||
in TSourceQuaternion source,
|
||||
out TDestinationQuaternion destination,
|
||||
Func<TSourceType, TDestinationType> elementConverter
|
||||
)
|
||||
where TSourceType : unmanaged
|
||||
where TDestinationType : unmanaged
|
||||
where TSourceQuaternion : unmanaged, IQuaternion<TSourceQuaternion, TSourceType>
|
||||
where TDestinationQuaternion : unmanaged, IQuaternion<TDestinationQuaternion, TDestinationType>
|
||||
{
|
||||
if (TDestinationQuaternion.ComponentCount < TSourceQuaternion.ComponentCount)
|
||||
throw new ArgumentException("Destination vector type has fewer elements than source vector type");
|
||||
|
||||
var src = source.AsSpan();
|
||||
Span<TDestinationType> dst = stackalloc TDestinationType[TDestinationQuaternion.ComponentCount];
|
||||
|
||||
for (var i = 0; i < src.Length; i++)
|
||||
dst[i] = elementConverter(src[i]);
|
||||
|
||||
destination = TDestinationQuaternion.Create(dst);
|
||||
}
|
||||
}
|
||||
79
Brutal.Numerics/Packing/VectorPack.cs
Normal file
79
Brutal.Numerics/Packing/VectorPack.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
using CommunityToolkit.HighPerformance;
|
||||
|
||||
namespace Brutal.Numerics;
|
||||
|
||||
/// <summary>
|
||||
/// Packing utilities for converting between vector types
|
||||
/// </summary>
|
||||
public static class VectorPack
|
||||
{
|
||||
/// <summary>
|
||||
/// Convert a span of vectors of one type to another using a per-element delegate converter function
|
||||
/// </summary>
|
||||
/// <param name="source">Source span of vectors</param>
|
||||
/// <param name="destination">Destination span of vectors</param>
|
||||
/// <param name="elementConverter">Delegate for converting between source and destination elements</param>
|
||||
/// <typeparam name="TSourceType">Source element type</typeparam>
|
||||
/// <typeparam name="TDestinationElement">Destination element type</typeparam>
|
||||
/// <typeparam name="TSourceVector">Source vector type</typeparam>
|
||||
/// <typeparam name="TDestinationVector">Destination vector type</typeparam>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Source and destination vector types have different element counts, or are different
|
||||
/// length spans
|
||||
/// </exception>
|
||||
public static void Pack<TSourceType, TSourceVector, TDestinationElement, TDestinationVector>(
|
||||
ReadOnlySpan<TSourceVector> source,
|
||||
Span<TDestinationVector> destination,
|
||||
Func<TSourceType, TDestinationElement> elementConverter
|
||||
)
|
||||
where TSourceVector : unmanaged, IVector<TSourceVector, TSourceType>
|
||||
where TDestinationVector : unmanaged, IVector<TDestinationVector, TDestinationElement>
|
||||
where TSourceType : unmanaged
|
||||
where TDestinationElement : unmanaged
|
||||
{
|
||||
if (destination.Length < source.Length)
|
||||
throw new ArgumentException("Destination span is not long enough to hold the converted vectors");
|
||||
if (TSourceVector.ComponentCount != TDestinationVector.ComponentCount)
|
||||
throw new ArgumentException("Source and destination vector types have different element counts");
|
||||
|
||||
var src = source.Cast<TSourceVector, TSourceType>();
|
||||
var dst = destination.Cast<TDestinationVector, TDestinationElement>();
|
||||
|
||||
for (var i = 0; i < src.Length; i++)
|
||||
dst[i] = elementConverter(src[i]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a source vector (cast as a span of elements) to a destination vector (created from a span of elements)
|
||||
/// using a per-element delegate converter function
|
||||
/// This slightly quirky signature is a workaround to avoid generics hell where you have to explicitly write each type
|
||||
/// </summary>
|
||||
/// <param name="source">Source vector</param>
|
||||
/// <param name="destination">Destination vector, created from a span of elements</param>
|
||||
/// <param name="elementConverter">Per-element conversion delegate</param>
|
||||
/// <typeparam name="TSourceType">Element type of input span</typeparam>
|
||||
/// <typeparam name="TDestinationType">Element type of output vector</typeparam>
|
||||
/// <typeparam name="TSourceVector">Type of input vector</typeparam>
|
||||
/// <typeparam name="TDestinationVector">Type of output vector</typeparam>
|
||||
public static void Pack<TSourceType, TSourceVector, TDestinationType, TDestinationVector>(
|
||||
in TSourceVector source,
|
||||
out TDestinationVector destination,
|
||||
Func<TSourceType, TDestinationType> elementConverter
|
||||
)
|
||||
where TSourceType : unmanaged
|
||||
where TDestinationType : unmanaged
|
||||
where TSourceVector : unmanaged, IVector<TSourceVector, TSourceType>
|
||||
where TDestinationVector : unmanaged, IVector<TDestinationVector, TDestinationType>
|
||||
{
|
||||
if (TDestinationVector.ComponentCount < TSourceVector.ComponentCount)
|
||||
throw new ArgumentException("Destination vector type has fewer elements than source vector type");
|
||||
|
||||
var src = source.AsSpan();
|
||||
Span<TDestinationType> dst = stackalloc TDestinationType[TDestinationVector.ComponentCount];
|
||||
|
||||
for (var i = 0; i < src.Length; i++)
|
||||
dst[i] = elementConverter(src[i]);
|
||||
|
||||
destination = TDestinationVector.Create(dst);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue