From 5d691bedb7d6c782c8001f2e0e713a136d4bf43e Mon Sep 17 00:00:00 2001 From: That_One_Nerd Date: Tue, 22 Aug 2023 14:05:10 -0400 Subject: [PATCH] More improvements to the triangle. WIP, doesn't compile. --- Nerd_STF/Mathematics/Abstract/IPolygon.cs | 3 +- Nerd_STF/Mathematics/Geometry/Triangle.cs | 101 +++++++++++++++++++++- 2 files changed, 101 insertions(+), 3 deletions(-) diff --git a/Nerd_STF/Mathematics/Abstract/IPolygon.cs b/Nerd_STF/Mathematics/Abstract/IPolygon.cs index 7283538..4131aa3 100644 --- a/Nerd_STF/Mathematics/Abstract/IPolygon.cs +++ b/Nerd_STF/Mathematics/Abstract/IPolygon.cs @@ -2,8 +2,7 @@ public interface IPolygon : IAverage, IEquatable, IFloatArray, IGroup, IIndexAll, IIndexRangeAll, - ILerp, IMedian, ISplittable, - ITriangulate + ILerp, IMedian, ITriangulate where T : IPolygon { public float Area { get; } diff --git a/Nerd_STF/Mathematics/Geometry/Triangle.cs b/Nerd_STF/Mathematics/Geometry/Triangle.cs index 2af07f7..4a7941f 100644 --- a/Nerd_STF/Mathematics/Geometry/Triangle.cs +++ b/Nerd_STF/Mathematics/Geometry/Triangle.cs @@ -2,7 +2,8 @@ public class Triangle : IClosestTo, IContains, IFromTuple, IPolygon, - ISubdivide, IWithinRange + ISplittable, + ISubdivide, IWithinRange { public float Area { @@ -135,6 +136,46 @@ public class Triangle : IClosestTo, IContains, } } + public static Triangle Average(params Triangle[] vals) + { + (Float3[] As, Float3[] Bs, Float3[] Cs) = SplitArray(vals); + return (Float3.Average(As), Float3.Average(Bs), Float3.Average(Cs)); + } + public static Triangle Lerp(Triangle a, Triangle b, float t, bool clamp = true) => + (Float3.Lerp(a.a, b.a, t, clamp), Float3.Lerp(a.b, b.b, t, clamp), Float3.Lerp(a.c, a.c, t, clamp)); + public static (Float3[] As, Float3[] Bs, Float3[] Cs) SplitArray(params Triangle[] vals) + { + Float3[] As = new Float3[vals.Length], + Bs = new Float3[vals.Length], + Cs = new Float3[vals.Length]; + for (int i = 0; i < vals.Length; i++) + { + As[i] = vals[i].a; + Bs[i] = vals[i].b; + Cs[i] = vals[i].c; + } + return (As, Bs, Cs); + } + + public static float[] ToFloatArrayAll(params Triangle[] vals) + { + float[] result = new float[9 * vals.Length]; + for (int i = 0; i < vals.Length; i++) + { + int p = i * 9; + result[p + 0] = vals[i].a.x; + result[p + 1] = vals[i].a.y; + result[p + 2] = vals[i].a.z; + result[p + 3] = vals[i].b.x; + result[p + 4] = vals[i].b.y; + result[p + 5] = vals[i].b.z; + result[p + 6] = vals[i].c.x; + result[p + 7] = vals[i].c.y; + result[p + 8] = vals[i].c.z; + } + return result; + } + public bool Equals(Triangle? other) => other is not null && a == other.a && b == other.b && c == other.c; public override bool Equals(object? obj) @@ -155,6 +196,64 @@ public class Triangle : IClosestTo, IContains, public Float3[] GetAllVerts() => new[] { a, b, c }; + public Triangle[] Subdivide() + { + Float3 abMid = AB.Midpoint, + bcMid = BC.Midpoint, + caMid = CA.Midpoint; + + return new Triangle[4] + { + (a, abMid, caMid), + (abMid, b, bcMid), + (caMid, bcMid, c), + (abMid, bcMid, caMid) + }; + } + public Triangle[] Subdivide(int iterations) + { + Triangle[] active = new[] { this }; + for (int i = 0; i < iterations; i++) + { + List newTris = new(); + foreach (Triangle tri in active) newTris.AddRange(tri.Subdivide()); + active = newTris.ToArray(); + } + return active; + } + + public Triangle[] Triangulate() => new[] { this }; + + public bool WithinRange(Float3 point, float range) => + WithinRange(point, range, Calculus.DefaultStep); + public bool WithinRange(Float3 point, float range, float step) + { + // Just like line, this is probably optimizable but who cares? + return AB.WithinRange(point, range, step) || + BC.WithinRange(point, range, step) || + CA.WithinRange(point, range, step); + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + public IEnumerator GetEnumerator() + { + yield return a; + yield return b; + yield return c; + } + + public Float3[] ToArray() => new[] { a, b, c }; + public Fill ToFill() + { + Triangle @this = this; + return i => @this[i]; + } + public List ToList() => new() { a, b, c }; + + public float[] ToFloatArray() => new[] { a.x, a.y, a.z, + b.x, b.y, b.z, + c.x, c.y, c.z}; + public static Triangle operator +(Triangle t, Float3 offset) => new(t.a + offset, t.b + offset, t.c + offset); public static Triangle operator -(Triangle t, Float3 offset) =>