diff --git a/pkNX.Structures.FlatBuffers/Util/Math/AABB.cs b/pkNX.Structures.FlatBuffers/Util/Math/AABB.cs index b619cdc4..00fb4228 100644 --- a/pkNX.Structures.FlatBuffers/Util/Math/AABB.cs +++ b/pkNX.Structures.FlatBuffers/Util/Math/AABB.cs @@ -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} }}"; } diff --git a/pkNX.Structures.FlatBuffers/Util/Math/Matrix4x3f.cs b/pkNX.Structures.FlatBuffers/Util/Math/Matrix4x3f.cs index 657990a2..f7955785 100644 --- a/pkNX.Structures.FlatBuffers/Util/Math/Matrix4x3f.cs +++ b/pkNX.Structures.FlatBuffers/Util/Math/Matrix4x3f.cs @@ -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} }}"; } diff --git a/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedAABB.cs b/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedAABB.cs index e737d3a1..65ac6b0a 100644 --- a/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedAABB.cs +++ b/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedAABB.cs @@ -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} }}"; } diff --git a/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedSphere.cs b/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedSphere.cs index beb86d4a..586eebe4 100644 --- a/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedSphere.cs +++ b/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedSphere.cs @@ -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; } diff --git a/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedVec3f.cs b/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedVec3f.cs index 9bba2e85..ee26ae78 100644 --- a/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedVec3f.cs +++ b/pkNX.Structures.FlatBuffers/Util/Math/Packed/PackedVec3f.cs @@ -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 { [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); } diff --git a/pkNX.Structures.FlatBuffers/Util/Math/Transform.cs b/pkNX.Structures.FlatBuffers/Util/Math/Transform.cs index 171f1d66..3b86267e 100644 --- a/pkNX.Structures.FlatBuffers/Util/Math/Transform.cs +++ b/pkNX.Structures.FlatBuffers/Util/Math/Transform.cs @@ -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} }}"; } diff --git a/pkNX.Structures.FlatBuffers/Util/Math/Vec2f.cs b/pkNX.Structures.FlatBuffers/Util/Math/Vec2f.cs index 320b983d..410c30da 100644 --- a/pkNX.Structures.FlatBuffers/Util/Math/Vec2f.cs +++ b/pkNX.Structures.FlatBuffers/Util/Math/Vec2f.cs @@ -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 diff --git a/pkNX.Structures.FlatBuffers/Util/Math/Vec3f.cs b/pkNX.Structures.FlatBuffers/Util/Math/Vec3f.cs index 37d8c04e..47464c5b 100644 --- a/pkNX.Structures.FlatBuffers/Util/Math/Vec3f.cs +++ b/pkNX.Structures.FlatBuffers/Util/Math/Vec3f.cs @@ -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;