mirror of
https://github.com/kwsch/pkNX.git
synced 2026-10-01 13:47:47 -05:00
Add some utility functions to the math classes
This commit is contained in:
@@ -13,5 +13,12 @@ public class AABB
|
||||
[FlatBufferItem(0)] public PackedVec3f Min { get; set; }
|
||||
[FlatBufferItem(1)] public PackedVec3f Max { get; set; }
|
||||
|
||||
public PackedVec3f Center => Min + Extents;
|
||||
public PackedVec3f Extents => Size / 2;
|
||||
public PackedVec3f Size => Max - Min;
|
||||
|
||||
public static AABB operator /(AABB l, float r) => new(){ Min = l.Min / r, Max = l.Max / r };
|
||||
|
||||
public static implicit operator AABB(PackedAABB p) => new() { Min = p.Min, Max = p.Max };
|
||||
public override string ToString() => $"{{ Min: {Min}, Max: {Max} }}";
|
||||
}
|
||||
|
||||
@@ -10,4 +10,6 @@ public class Matrix4x3f
|
||||
[FlatBufferItem(1)] public PackedVec3f AxisY { get; set; } = new();
|
||||
[FlatBufferItem(2)] public PackedVec3f AxisZ { get; set; } = new();
|
||||
[FlatBufferItem(3)] public PackedVec3f AxisW { get; set; } = new();
|
||||
|
||||
public override string ToString() => $"{{ 0: {AxisX}, 1: {AxisY}, 2: {AxisZ}, 3: {AxisW} }}";
|
||||
}
|
||||
|
||||
@@ -9,5 +9,13 @@ public class PackedAABB
|
||||
[FlatBufferItem(0)] public PackedVec3f Min { get; set; } = new();
|
||||
[FlatBufferItem(1)] public PackedVec3f Max { get; set; } = new();
|
||||
|
||||
public PackedVec3f Center => Min + Extents;
|
||||
public PackedVec3f Extents => Size / 2;
|
||||
public PackedVec3f Size => Max - Min;
|
||||
|
||||
public static PackedAABB operator /(PackedAABB l, float r) => new(){ Min = l.Min / r, Max = l.Max / r };
|
||||
|
||||
public static implicit operator PackedAABB(AABB p) => new() { Min = p.Min, Max = p.Max };
|
||||
|
||||
public override string ToString() => $"{{ Min: {Min}, Max: {Max} }}";
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using FlatSharp.Attributes;
|
||||
|
||||
@@ -6,6 +7,14 @@ namespace pkNX.Structures.FlatBuffers;
|
||||
[FlatBufferStruct, TypeConverter(typeof(ExpandableObjectConverter))]
|
||||
public class PackedSphere
|
||||
{
|
||||
public PackedSphere() { }
|
||||
|
||||
public PackedSphere(PackedAABB bounds)
|
||||
{
|
||||
Center = bounds.Center;
|
||||
Radius = bounds.Extents.Magnitude;
|
||||
}
|
||||
|
||||
[FlatBufferItem(00)] public PackedVec3f Center { get; set; } = new();
|
||||
[FlatBufferItem(01)] public float Radius { get; set; }
|
||||
|
||||
|
||||
@@ -1,14 +1,80 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using FlatSharp.Attributes;
|
||||
|
||||
namespace pkNX.Structures.FlatBuffers;
|
||||
|
||||
[FlatBufferStruct, TypeConverter(typeof(ExpandableObjectConverter))]
|
||||
public class PackedVec3f
|
||||
public class PackedVec3f : IEquatable<PackedVec3f>
|
||||
{
|
||||
[FlatBufferItem(00)] public float X { get; set; }
|
||||
[FlatBufferItem(01)] public float Y { get; set; }
|
||||
[FlatBufferItem(02)] public float Z { get; set; }
|
||||
|
||||
public override string ToString() => $"{{ X: {X}, Y: {Y}, Z: {Z} }}";
|
||||
|
||||
public static readonly PackedVec3f Zero = new();
|
||||
public static readonly PackedVec3f One = new(1, 1, 1);
|
||||
|
||||
public PackedVec3f() { }
|
||||
|
||||
public PackedVec3f(float x = 0, float y = 0, float z = 0)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
Z = z;
|
||||
}
|
||||
public static implicit operator Vec3f(PackedVec3f v) => new(v.X, v.Y, v.Z);
|
||||
|
||||
public bool IsOne => X is 1 && Y is 1 && Z is 1;
|
||||
public bool IsZero => X is 0 && Y is 0 && Z is 0;
|
||||
|
||||
public float Magnitude => MathF.Sqrt(MagnitudeSqr);
|
||||
public float MagnitudeSqr => Dot(this);
|
||||
public PackedVec3f Normalized => this * (1 / Magnitude);
|
||||
|
||||
public float Dot(PackedVec3f other) => X * other.X + Y * other.Y + Z * other.Z;
|
||||
public PackedVec3f Cross(PackedVec3f other) => new(Y * other.Z - Z * other.Y, Z * other.X - X * other.Z, X * other.Y - Y * other.X);
|
||||
public float DistanceTo(PackedVec3f other) => (this - other).Magnitude;
|
||||
public float DistanceToSqr(PackedVec3f other) => (this - other).MagnitudeSqr;
|
||||
public PackedVec3f Lerp(PackedVec3f other, float t) => this + (other - this) * t;
|
||||
|
||||
public PackedVec3f Clone() => new()
|
||||
{
|
||||
X = X,
|
||||
Y = Y,
|
||||
Z = Z,
|
||||
};
|
||||
|
||||
public bool Equals(PackedVec3f? other)
|
||||
{
|
||||
if (other is null) return false;
|
||||
if (ReferenceEquals(this, other)) return true;
|
||||
return X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z);
|
||||
}
|
||||
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
if (obj is null) return false;
|
||||
if (ReferenceEquals(this, obj)) return true;
|
||||
if (obj.GetType() != GetType()) return false;
|
||||
return Equals((PackedVec3f)obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode() => HashCode.Combine(X, Y, Z);
|
||||
|
||||
public static PackedVec3f operator -(PackedVec3f v) => new(-v.X, -v.Y, -v.Z);
|
||||
|
||||
public static PackedVec3f operator +(PackedVec3f l, PackedVec3f r) => new(l.X + r.X, l.Y + r.Y, l.Z + r.Z);
|
||||
public static PackedVec3f operator -(PackedVec3f l, PackedVec3f r) => new(l.X - r.X, l.Y - r.Y, l.Z - r.Z);
|
||||
public static PackedVec3f operator *(PackedVec3f l, PackedVec3f r) => new(l.X * r.X, l.Y * r.Y, l.Z * r.Z);
|
||||
public static PackedVec3f operator /(PackedVec3f l, PackedVec3f r) => new(l.X / r.X, l.Y / r.Y, l.Z / r.Z);
|
||||
|
||||
public static PackedVec3f operator +(PackedVec3f l, float r) => new(l.X + r, l.Y + r, l.Z + r);
|
||||
public static PackedVec3f operator -(PackedVec3f l, float r) => new(l.X - r, l.Y - r, l.Z - r);
|
||||
public static PackedVec3f operator *(PackedVec3f l, float r) => new(l.X * r, l.Y * r, l.Z * r);
|
||||
public static PackedVec3f operator /(PackedVec3f l, float r) => new(l.X / r, l.Y / r, l.Z / r);
|
||||
|
||||
public static bool operator ==(PackedVec3f? left, PackedVec3f? right) => Equals(left, right);
|
||||
public static bool operator !=(PackedVec3f? left, PackedVec3f? right) => !Equals(left, right);
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ namespace pkNX.Structures.FlatBuffers;
|
||||
[FlatBufferTable, TypeConverter(typeof(ExpandableObjectConverter))]
|
||||
public class Transform
|
||||
{
|
||||
[FlatBufferItem(0)] public PackedVec3f Scale { get; set; } = new();
|
||||
[FlatBufferItem(1)] public PackedVec3f Rotate { get; set; } = new();
|
||||
[FlatBufferItem(2)] public PackedVec3f Translate { get; set; } = new();
|
||||
[FlatBufferItem(0)] public PackedVec3f Scale { get; set; } = PackedVec3f.One;
|
||||
[FlatBufferItem(1)] public PackedVec3f Rotate { get; set; } = PackedVec3f.Zero;
|
||||
[FlatBufferItem(2)] public PackedVec3f Translate { get; set; } = PackedVec3f.Zero;
|
||||
|
||||
public override string ToString() => $"{{ S: {Scale}, R: {Rotate}, T: {Translate} }}";
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using System.ComponentModel;
|
||||
using FlatSharp.Attributes;
|
||||
|
||||
namespace pkNX.Structures.FlatBuffers.Util.Math;
|
||||
namespace pkNX.Structures.FlatBuffers;
|
||||
|
||||
[FlatBufferStruct, TypeConverter(typeof(ExpandableObjectConverter))]
|
||||
public class Vec2f
|
||||
|
||||
@@ -22,6 +22,7 @@ public Vec3f(float x = 0, float y = 0, float z = 0)
|
||||
Y = y;
|
||||
Z = z;
|
||||
}
|
||||
public static implicit operator Vec3f(PackedVec3f v) => new(v.X, v.Y, v.Z);
|
||||
|
||||
public bool IsOne => X is 1 && Y is 1 && Z is 1;
|
||||
public bool IsZero => X is 0 && Y is 0 && Z is 0;
|
||||
|
||||
Reference in New Issue
Block a user