Skip to content

Commit

Permalink
Returned triangulated vertices as indices, so they're easier to use w…
Browse files Browse the repository at this point in the history
…hen returned.
  • Loading branch information
MeltyPlayer committed Jan 5, 2025
1 parent a475fd2 commit 539ba4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,10 @@ var points2d
points3d.Select(t => t.LocalPosition).ToArray());

try {
var indexByVec3m = new Dictionary<Vector3m, int>(new EqualityComparer());
var vec3msWithIndices = new List<Vector3m>();
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);
}

Expand All @@ -300,7 +298,7 @@ var points2d

return earClipping
.Result
.Select(v => points3d[indexByVec3m[v]])
.Select(i => points3d[i])
.ToArray();
} catch {
var delaunator = new Delaunator(
Expand Down
33 changes: 19 additions & 14 deletions FinModelUtility/Formats/Vrml/Vrml/src/util/EarClipping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace vrml.util;
public class EarClipping {
private Polygon mainPointList_;
private Vector3 normal_;
public List<Vector3m> Result { get; private set; }
public List<int> Result { get; private set; }

public void SetPoints(List<Vector3m> points) {
if (points == null || points.Count < 3) {
Expand All @@ -20,7 +20,7 @@ public void SetPoints(List<Vector3m> points) {
this.mainPointList_ = new Polygon();
this.LinkAndAddToList_(this.mainPointList_, points);

this.Result = new List<Vector3m>();
this.Result = new List<int>();
}

// calculating normal using Newell's method
Expand All @@ -38,22 +38,24 @@ private void CalcNormal_(List<Vector3m> points) {

private void LinkAndAddToList_(Polygon polygon, List<Vector3m> points) {
ConnectionEdge prev = null, first = null;
Dictionary<Vector3m, (Vector3m, List<ConnectionEdge>)> pointsHashSet = new();
Dictionary<Vector3m, (Vector3m, int, List<ConnectionEdge>)> pointsHashSet = new();
int pointCount = 0;
for (int i = 0; i < points.Count; i++) {
// we don't wanna have duplicates
Vector3m p0;
int p0Index;
List<ConnectionEdge> 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<ConnectionEdge>();
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

Expand All @@ -72,7 +74,7 @@ private void LinkAndAddToList_(Polygon polygon, List<Vector3m> 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<ConnectionEdge> nonConvexPoints
Expand All @@ -93,9 +95,9 @@ List<ConnectionEdge> 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_)) {
Expand Down Expand Up @@ -149,9 +151,9 @@ private bool IsPointInTriangle_(Vector3m prevPoint,
Vector3m nextPoint,
List<ConnectionEdge> 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,
Expand Down Expand Up @@ -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<ConnectionEdge> incidentEdges) {
public ConnectionEdge(Vector3m p0, int p0Index, Polygon parentPolygon, List<ConnectionEdge> incidentEdges) {
this.Origin = p0;
this.OriginIndex = p0Index;
this.Polygon = parentPolygon;
this.IncidentEdges = incidentEdges;
this.IncidentEdges.Add(this);
Expand Down

0 comments on commit 539ba4f

Please sign in to comment.