From 539ba4fc03d7bda2bf35096321da97f7aa0a8b95 Mon Sep 17 00:00:00 2001 From: MeltyPlayer Date: Sun, 5 Jan 2025 00:44:41 -0600 Subject: [PATCH] Returned triangulated vertices as indices, so they're easier to use when returned. --- .../Vrml/Vrml/src/api/VrmlModelImporter.cs | 4 +-- .../Formats/Vrml/Vrml/src/util/EarClipping.cs | 33 +++++++++++-------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/FinModelUtility/Formats/Vrml/Vrml/src/api/VrmlModelImporter.cs b/FinModelUtility/Formats/Vrml/Vrml/src/api/VrmlModelImporter.cs index 311db1771..e74a06982 100644 --- a/FinModelUtility/Formats/Vrml/Vrml/src/api/VrmlModelImporter.cs +++ b/FinModelUtility/Formats/Vrml/Vrml/src/api/VrmlModelImporter.cs @@ -285,12 +285,10 @@ var points2d points3d.Select(t => t.LocalPosition).ToArray()); try { - var indexByVec3m = new Dictionary(new EqualityComparer()); var vec3msWithIndices = new List(); for (var i = 0; i < points2d.Count; ++i) { var point2d = points2d[i]; var vec3m = new Vector3m(point2d.X, point2d.Y, 0); - indexByVec3m[vec3m] = i; vec3msWithIndices.Add(vec3m); } @@ -300,7 +298,7 @@ var points2d return earClipping .Result - .Select(v => points3d[indexByVec3m[v]]) + .Select(i => points3d[i]) .ToArray(); } catch { var delaunator = new Delaunator( diff --git a/FinModelUtility/Formats/Vrml/Vrml/src/util/EarClipping.cs b/FinModelUtility/Formats/Vrml/Vrml/src/util/EarClipping.cs index 5f7a36211..916767284 100644 --- a/FinModelUtility/Formats/Vrml/Vrml/src/util/EarClipping.cs +++ b/FinModelUtility/Formats/Vrml/Vrml/src/util/EarClipping.cs @@ -8,7 +8,7 @@ namespace vrml.util; public class EarClipping { private Polygon mainPointList_; private Vector3 normal_; - public List Result { get; private set; } + public List Result { get; private set; } public void SetPoints(List points) { if (points == null || points.Count < 3) { @@ -20,7 +20,7 @@ public void SetPoints(List points) { this.mainPointList_ = new Polygon(); this.LinkAndAddToList_(this.mainPointList_, points); - this.Result = new List(); + this.Result = new List(); } // calculating normal using Newell's method @@ -38,22 +38,24 @@ private void CalcNormal_(List points) { private void LinkAndAddToList_(Polygon polygon, List points) { ConnectionEdge prev = null, first = null; - Dictionary)> pointsHashSet = new(); + Dictionary)> pointsHashSet = new(); int pointCount = 0; for (int i = 0; i < points.Count; i++) { // we don't wanna have duplicates Vector3m p0; + int p0Index; List incidentEdges; if (pointsHashSet.ContainsKey(points[i])) { - (p0, incidentEdges) = pointsHashSet[points[i]]; + (p0, p0Index, incidentEdges) = pointsHashSet[points[i]]; } else { p0 = points[i]; + p0Index = i; incidentEdges = new List(); - pointsHashSet.Add(p0, (p0, incidentEdges)); + pointsHashSet.Add(p0, (p0, p0Index, incidentEdges)); pointCount++; } - ConnectionEdge current = new(p0, polygon, incidentEdges); + ConnectionEdge current = new(p0, p0Index, polygon, incidentEdges); first = (i == 0) ? current : first; // remember first @@ -72,7 +74,7 @@ private void LinkAndAddToList_(Polygon polygon, List points) { } public void Triangulate() { - if (this.normal_.Equals(Vector3m.Zero())) + if (this.normal_ == Vector3.Zero) throw new Exception("The input is not a valid polygon"); List nonConvexPoints @@ -93,9 +95,9 @@ List nonConvexPoints nonConvexPoints)) { // cut off ear guard = true; - this.Result.Add(cur.prev_.Origin); - this.Result.Add(cur.Origin); - this.Result.Add(cur.next_.Origin); + this.Result.Add(cur.prev_.OriginIndex); + this.Result.Add(cur.OriginIndex); + this.Result.Add(cur.next_.OriginIndex); // Check if prev and next are still nonconvex. If not, then remove from non convex list if (this.IsConvex_(cur.prev_)) { @@ -149,9 +151,9 @@ private bool IsPointInTriangle_(Vector3m prevPoint, Vector3m nextPoint, List nonConvexPoints) { foreach (var nonConvexPoint in nonConvexPoints) { - if (nonConvexPoint.Origin == prevPoint || - nonConvexPoint.Origin == curPoint || - nonConvexPoint.Origin == nextPoint) + if (nonConvexPoint.Origin.Equals(prevPoint) || + nonConvexPoint.Origin.Equals(curPoint) || + nonConvexPoint.Origin.Equals(nextPoint)) continue; if (Misc.PointInOrOnTriangle(prevPoint, curPoint, @@ -205,12 +207,15 @@ public override int GetHashCode() { } internal Vector3m Origin { get; } + internal int OriginIndex { get; } + internal ConnectionEdge prev_; internal ConnectionEdge next_; internal Polygon Polygon { get; set; } - public ConnectionEdge(Vector3m p0, Polygon parentPolygon, List incidentEdges) { + public ConnectionEdge(Vector3m p0, int p0Index, Polygon parentPolygon, List incidentEdges) { this.Origin = p0; + this.OriginIndex = p0Index; this.Polygon = parentPolygon; this.IncidentEdges = incidentEdges; this.IncidentEdges.Add(this);