private static void FindEarVertices() { for (int i = 0; i < convexVertices.Count; i++) { Vertex c = convexVertices[i]; if (IsEar(c)) { earVertices.AddLast(c); Log("Ear: {0}", c); } } }
/// <summary> /// Triangulates a 2D polygon produced the indexes required to render the points as a triangle list. /// </summary> /// <param name="inputVertices">The polygon vertices in counter-clockwise winding order.</param> /// <param name="desiredWindingOrder">The desired output winding order.</param> /// <param name="outputVertices">The resulting vertices that include any reversals of winding order and holes.</param> /// <param name="indices">The resulting indices for rendering the shape as a triangle list.</param> public static void Triangulate( float2[] inputVertices, WindingOrder desiredWindingOrder, out float2[] outputVertices, out int[] indices) { Log("\nBeginning triangulation..."); List <Triangle> triangles = new List <Triangle>(); //make sure we have our vertices wound properly if (DetermineWindingOrder(inputVertices) == WindingOrder.Clockwise) { outputVertices = ReverseWindingOrder(inputVertices); } else { outputVertices = (float2[])inputVertices.Clone(); } //clear all of the lists polygonVertices.Clear(); earVertices.Clear(); convexVertices.Clear(); reflexVertices.Clear(); //generate the cyclical list of vertices in the polygon for (int i = 0; i < outputVertices.Length; i++) { polygonVertices.AddLast(new Vertex(outputVertices[i], i)); } //categorize all of the vertices as convex, reflex, and ear FindConvexAndReflexVertices(); FindEarVertices(); //clip all the ear vertices while (polygonVertices.Count > 3 && earVertices.Count > 0) { ClipNextEar(triangles); } //if there are still three points, use that for the last triangle if (polygonVertices.Count == 3) { triangles.Add(new Triangle( polygonVertices[0].Value, polygonVertices[1].Value, polygonVertices[2].Value)); } //add all of the triangle indices to the output array indices = new int[triangles.Count * 3]; //move the if statement out of the loop to prevent all the //redundant comparisons if (desiredWindingOrder == WindingOrder.CounterClockwise) { for (int i = 0; i < triangles.Count; i++) { indices[i * 3] = triangles[i].A.Index; indices[i * 3 + 1] = triangles[i].B.Index; indices[i * 3 + 2] = triangles[i].C.Index; } } else { for (int i = 0; i < triangles.Count; i++) { indices[i * 3] = triangles[i].C.Index; indices[i * 3 + 1] = triangles[i].B.Index; indices[i * 3 + 2] = triangles[i].A.Index; } } }