-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTriangulatePolygon.cs
189 lines (151 loc) · 5.73 KB
/
TriangulatePolygon.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace Flat.Physics
{
public enum WindingOrder
{
Clockwise, CounterClockwise, Invalid
}
public static class PolygonHelper
{
public static float FindPolygonArea(Vector2[] vertices)
{
float totalArea = 0f;
for(int i = 0; i < vertices.Length; i++)
{
Vector2 a = vertices[i];
Vector2 b = vertices[(i+1) % vertices.Length];
float dy = (a.Y + b.Y) / 2f;
float dx = b.X - a.X;
float area = dy * dx;
totalArea += area;
}
return MathF.Abs(totalArea);
}
public static bool Triangulate(Vector2[] vertices, out int[] triangles, out string errorMessage)
{
triangles = null;
errorMessage = string.Empty;
if(vertices is null)
{
errorMessage = "The vertex list is null.";
return false;
}
if(vertices.Length < 3)
{
errorMessage = "The vertex list must have at least 3 vertices.";
return false;
}
if(vertices.Length > 1024)
{
errorMessage = "The max vertex list length is 1024";
return false;
}
//if (!PolygonHelper.IsSimplePolygon(vertices))
//{
// errorMessage = "The vertex list does not define a simple polygon.";
// return false;
//}
//if(PolygonHelper.ContainsColinearEdges(vertices))
//{
// errorMessage = "The vertex list contains colinear edges.";
// return false;
//}
//PolygonHelper.ComputePolygonArea(vertices, out float area, out WindingOrder windingOrder);
//if(windingOrder is WindingOrder.Invalid)
//{
// errorMessage = "The vertices list does not contain a valid polygon.";
// return false;
//}
//if(windingOrder is WindingOrder.CounterClockwise)
//{
// Array.Reverse(vertices);
//}
List<int> indexList = new List<int>();
for(int i = 0; i < vertices.Length; i++)
{
indexList.Add(i);
}
int totalTriangleCount = vertices.Length - 2;
int totalTriangleIndexCount = totalTriangleCount * 3;
triangles = new int[totalTriangleIndexCount];
int triangleIndexCount = 0;
while(indexList.Count > 3)
{
for (int i = 0; i < indexList.Count; i++)
{
int a = indexList[i];
int b = Util.GetItem(indexList, i - 1);
int c = Util.GetItem(indexList, i + 1);
Vector2 va = vertices[a];
Vector2 vb = vertices[b];
Vector2 vc = vertices[c];
Vector2 va_to_vb = vb - va;
Vector2 va_to_vc = vc - va;
// Is ear test vertex convex?
if(Util.Cross(va_to_vb, va_to_vc) < 0f)
{
continue;
}
bool isEar = true;
// Does test ear contain any polygon vertices?
for(int j = 0; j < vertices.Length; j++)
{
if(j == a || j == b || j == c)
{
continue;
}
Vector2 p = vertices[j];
if(PolygonHelper.IsPointInTriangle(p, vb, va, vc))
{
isEar = false;
break;
}
}
if(isEar)
{
triangles[triangleIndexCount++] = b;
triangles[triangleIndexCount++] = a;
triangles[triangleIndexCount++] = c;
indexList.RemoveAt(i);
break;
}
}
}
triangles[triangleIndexCount++] = indexList[0];
triangles[triangleIndexCount++] = indexList[1];
triangles[triangleIndexCount++] = indexList[2];
return true;
}
public static bool IsPointInTriangle(Vector2 p, Vector2 a, Vector2 b, Vector2 c)
{
Vector2 ab = b - a;
Vector2 bc = c - b;
Vector2 ca = a - c;
Vector2 ap = p - a;
Vector2 bp = p - b;
Vector2 cp = p - c;
float cross1 = Util.Cross(ab, ap);
float cross2 = Util.Cross(bc, bp);
float cross3 = Util.Cross(ca, cp);
if(cross1 > 0f || cross2 > 0f || cross3 > 0f)
{
return false;
}
return true;
}
public static bool IsSimplePolygon(Vector2[] vertices)
{
throw new NotImplementedException();
}
public static bool ContainsColinearEdges(Vector2[] vertices)
{
throw new NotImplementedException();
}
public static void ComputePolygonArea(Vector2[] vertices, out float area, out WindingOrder windingOrder)
{
throw new NotImplementedException();
}
}
}