/// <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 TSVector2[] CutHoleInShape(TSVector2[] shapeVerts, TSVector2[] 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], (short)i)); } CyclicalList <Vertex> holePolygon = new CyclicalList <Vertex>(); for (int i = 0; i < holeVerts.Length; i++) { holePolygon.Add(new Vertex(holeVerts[i], (short)(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. FP? closestPoint = null; LineSegment closestSegment = new LineSegment(); foreach (LineSegment segment in segmentsToTest) { FP?intersection = segment.IntersectsWithRay(rightMostHoleVertex.Position, TSVector2.right); 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 TSVector2 I = rightMostHoleVertex.Position + TSVector2.right * 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) { FP closestDot = -1f; foreach (Vertex v in interiorReflexVertices) { //compute the dot product of the vector against the UnitX TSVector2 d = TSVector2.Normalize(v.Position - rightMostHoleVertex.Position); FP dot = TSVector2.Dot(TSVector2.right, 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 TSVector2[] newShapeVerts = new TSVector2[polygonVertices.Count]; for (int i = 0; i < polygonVertices.Count; i++) { newShapeVerts[i] = polygonVertices[i].Value.Position; } return(newShapeVerts); }
/// <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) { //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)); } 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; } } Debug.Log("rightMost to cut out is " + rightMostHoleVertex); Debug.Log("rightMost to cut out isdskdl;skdl;ksal;dkl;sakd;lksa;ldk;sakd;lsakdl;ksa;ldfdgdfgfdsgfdgfdgdgfd\n \n\n\n "); //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, new Vector2(1f, 0f)); 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 + new Vector2(1f, 0f) * 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 = v.Position - rightMostHoleVertex.Position; d.Normalize(); float dot = Vector2.Dot(new Vector2(1f, 0), 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); for (int i = mIndex; i <= mIndex + holePolygon.Count; i++) { polygonVertices.AddAfter(polygonVertices[injectPoint++], holePolygon[i]); } polygonVertices.AddAfter(polygonVertices[injectPoint], P); //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); }