private static void ValidateAdjacentVertex(Vertex vertex) { Log("Validating: {0}...", vertex); if (reflexVertices.Contains(vertex)) { if (IsConvex(vertex)) { reflexVertices.Remove(vertex); convexVertices.Add(vertex); Log("Vertex: {0} now convex", vertex); } else { Log("Vertex: {0} still reflex", vertex); } } if (convexVertices.Contains(vertex)) { bool wasEar = earVertices.Contains(vertex); bool isEar = IsEar(vertex); if (wasEar && !isEar) { earVertices.Remove(vertex); Log("Vertex: {0} no longer ear", vertex); } else if (!wasEar && isEar) { earVertices.AddFirst(vertex); Log("Vertex: {0} now ear", vertex); } else { Log("Vertex: {0} still ear", vertex); } } }
private static void FindConvexAndReflexVertices() { for (int i = 0; i < polygonVertices.Count; i++) { Vertex v = polygonVertices[i].Value; if (IsConvex(v)) { convexVertices.Add(v); Log("Convex: {0}", v); } else { reflexVertices.Add(v); Log("Reflex: {0}", v); } } }
/// <summary> /// Cuts a hole into a shape. /// </summary> /// <param name="shapeVerts">An array of vertices for the primary shape.</param> /// <param name="holeVerts">An array of vertices for the hole to be cut. It is assumed that these vertices lie completely within the shape verts.</param> /// <returns>The new array of vertices that can be passed to Triangulate to properly triangulate the shape with the hole.</returns> public static Vector2[] CutHoleInShape(Vector2[] shapeVerts, Vector2[] holeVerts) { Log("\nCutting hole into shape..."); //make sure the shape vertices are wound counter clockwise and the hole vertices clockwise shapeVerts = EnsureWindingOrder(shapeVerts, WindingOrder.CounterClockwise); holeVerts = EnsureWindingOrder(holeVerts, WindingOrder.Clockwise); //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 < shapeVerts.Length; i++) { polygonVertices.AddLast(new Vertex(shapeVerts[i], i)); } CyclicalList <Vertex> holePolygon = new CyclicalList <Vertex>(); for (int i = 0; i < holeVerts.Length; i++) { holePolygon.Add(new Vertex(holeVerts[i], i + polygonVertices.Count)); } #if DEBUG StringBuilder vString = new StringBuilder(); foreach (Vertex v in polygonVertices) { vString.Append(string.Format("{0}, ", v)); } Log("Shape Vertices: {0}", vString); vString = new StringBuilder(); foreach (Vertex v in holePolygon) { vString.Append(string.Format("{0}, ", v)); } Log("Hole Vertices: {0}", vString); #endif FindConvexAndReflexVertices(); FindEarVertices(); //find the hole vertex with the largest X value Vertex rightMostHoleVertex = holePolygon[0]; foreach (Vertex v in holePolygon) { if (v.Position.X > rightMostHoleVertex.Position.X) { rightMostHoleVertex = v; } } //construct a list of all line segments where at least one vertex //is to the right of the rightmost hole vertex with one vertex //above the hole vertex and one below List <LineSegment> segmentsToTest = new List <LineSegment>(); for (int i = 0; i < polygonVertices.Count; i++) { Vertex a = polygonVertices[i].Value; Vertex b = polygonVertices[i + 1].Value; if ((a.Position.X > rightMostHoleVertex.Position.X || b.Position.X > rightMostHoleVertex.Position.X) && (a.Position.Y >= rightMostHoleVertex.Position.Y && b.Position.Y <= rightMostHoleVertex.Position.Y || a.Position.Y <= rightMostHoleVertex.Position.Y && b.Position.Y >= rightMostHoleVertex.Position.Y)) { segmentsToTest.Add(new LineSegment(a, b)); } } //now we try to find the closest intersection point heading to the right from //our hole vertex. float? closestPoint = null; LineSegment closestSegment = new LineSegment(); foreach (LineSegment segment in segmentsToTest) { float?intersection = segment.IntersectsWithRay(rightMostHoleVertex.Position, Vector2.UnitX); if (intersection != null) { if (closestPoint == null || closestPoint.Value > intersection.Value) { closestPoint = intersection; closestSegment = segment; } } } //if closestPoint is null, there were no collisions (likely from improper input data), //but we'll just return without doing anything else if (closestPoint == null) { return(shapeVerts); } //otherwise we can find our mutually visible vertex to split the polygon Vector2 I = rightMostHoleVertex.Position + Vector2.UnitX * closestPoint.Value; Vertex P = closestSegment.A.Position.X > closestSegment.B.Position.X ? closestSegment.A : closestSegment.B; //construct triangle MIP Triangle mip = new Triangle(rightMostHoleVertex, new Vertex(I, 1), P); //see if any of the reflex vertices lie inside of the MIP triangle List <Vertex> interiorReflexVertices = new List <Vertex>(); foreach (Vertex v in reflexVertices) { if (mip.ContainsPoint(v)) { interiorReflexVertices.Add(v); } } //if there are any interior reflex vertices, find the one that, when connected //to our rightMostHoleVertex, forms the line closest to Vector2.UnitX if (interiorReflexVertices.Count > 0) { float closestDot = -1f; foreach (Vertex v in interiorReflexVertices) { //compute the dot product of the vector against the UnitX Vector2 d = Vector2.Normalize(v.Position - rightMostHoleVertex.Position); float dot = Vector2.Dot(Vector2.UnitX, d); //if this line is the closest we've found if (dot > closestDot) { //save the value and save the vertex as P closestDot = dot; P = v; } } } //now we just form our output array by injecting the hole vertices into place //we know we have to inject the hole into the main array after point P going from //rightMostHoleVertex around and then back to P. int mIndex = holePolygon.IndexOf(rightMostHoleVertex); int injectPoint = polygonVertices.IndexOf(P); Log("Inserting hole at injection point {0} starting at hole vertex {1}.", P, rightMostHoleVertex); for (int i = mIndex; i <= mIndex + holePolygon.Count; i++) { Log("Inserting vertex {0} after vertex {1}.", holePolygon[i], polygonVertices[injectPoint].Value); polygonVertices.AddAfter(polygonVertices[injectPoint++], holePolygon[i]); } polygonVertices.AddAfter(polygonVertices[injectPoint], P); #if DEBUG vString = new StringBuilder(); foreach (Vertex v in polygonVertices) { vString.Append(string.Format("{0}, ", v)); } Log("New Shape Vertices: {0}\n", vString); #endif //finally we write out the new polygon vertices and return them out Vector2[] newShapeVerts = new Vector2[polygonVertices.Count]; for (int i = 0; i < polygonVertices.Count; i++) { newShapeVerts[i] = polygonVertices[i].Value.Position; } return(newShapeVerts); }