private static void InWhichOrderAreVerticesAdded(List <ClipVertex> polyList) { ClipVertex thisVertex = polyList[0]; float size = 0.01f; int safety = 0; while (true) { Gizmos.color = Color.red; //Gizmos.DrawWireSphere(new Vector3(thisVertex.coordinate.x, 0f, thisVertex.coordinate.y), size); size += 0.01f; thisVertex = thisVertex.next; //We are back at the first vertex if (thisVertex.Equals(polyList[0])) { break; } safety += 1; if (safety > 100000) { Debug.Log("Endless loop in debug in which orders are vertices added"); break; } } }
// // Debug // private static void DebugEntryExit(List <ClipVertex> polyList) { ClipVertex thisVertex = polyList[0]; Gizmos.color = Color.green; //Gizmos.DrawWireSphere(new Vector3(thisVertex.coordinate.x, 0f, thisVertex.coordinate.y), 0.1f); float size = 0.02f; int safety = 0; while (true) { if (thisVertex.isIntersection) { //Gizmos.color = Color.white; //Debug.Log(thisVertex.neighbor); //Gizmos.DrawWireSphere(new Vector3(thisVertex.neighbor.coordinate.x, 0f, thisVertex.neighbor.coordinate.y), 0.05f); Gizmos.color = Color.yellow; if (thisVertex.isEntry) { //Gizmos.color = Color.white; //Gizmos.DrawWireSphere(new Vector3(thisVertex.next.coordinate.x, 0f, thisVertex.next.coordinate.y), 0.04f); Gizmos.color = Color.red; } //Gizmos.DrawWireSphere(new Vector3(thisVertex.coordinate.x, 0f, thisVertex.coordinate.y), size); size += 0.005f; } thisVertex = thisVertex.next; //We are back at the first vertex if (thisVertex.Equals(polyList[0])) { break; } safety += 1; if (safety > 100000) { Debug.Log("Endless loop in debug entry exit"); break; } } }
//Mark entry exit points private static void MarkEntryExit(List <ClipVertex> poly, List <MyVector2> clipPolyVector) { //First see if the first vertex starts inside or outside (we can use the original list) bool isInside = _Intersections.PointPolygon(clipPolyVector, poly[0].coordinate); //Debug.Log(isInside); ClipVertex currentVertex = poly[0]; ClipVertex firstVertex = currentVertex; int safety = 0; while (true) { if (currentVertex.isIntersection) { //If we were outside, this is an entry currentVertex.isEntry = isInside ? false : true; //Now we know we are either inside or outside isInside = !isInside; } currentVertex = currentVertex.next; //We have travelled around the entire polygon if (currentVertex.Equals(firstVertex)) { break; } safety += 1; if (safety > 100000) { Debug.Log("Endless loop in mark entry exit"); break; } } }
//Find the the first entry vertex in a polygon private static ClipVertex FindFirstEntryVertex(List <ClipVertex> poly) { ClipVertex thisVertex = poly[0]; ClipVertex firstVertex = thisVertex; int safety = 0; while (true) { //Is this an available entry vertex? if (thisVertex.isIntersection && thisVertex.isEntry && !thisVertex.isTakenByFinalPolygon) { //We have found the first vertex break; } thisVertex = thisVertex.next; //We have travelled the entire polygon without finding an available entry vertex if (thisVertex.Equals(firstVertex)) { thisVertex = null; break; } safety += 1; if (safety > 100000) { Debug.Log("Endless loop in find first entry vertex"); break; } } return(thisVertex); }
//Reset vertices before we find the final polygon(s) private static void ResetVertices(List <ClipVertex> poly) { ClipVertex resetVertex = poly[0]; int safety = 0; while (true) { //Reset resetVertex.isTakenByFinalPolygon = false; resetVertex.nextPoly = null; //Dont forget to reset the neighbor if (resetVertex.isIntersection) { resetVertex.neighbor.isTakenByFinalPolygon = false; } resetVertex = resetVertex.next; //All vertices are reset if (resetVertex.Equals(poly[0])) { break; } safety += 1; if (safety > 100000) { Debug.Log("Endless loop in reset vertices"); break; } } }
//Get the clipped polygons: either the intersection or the !intersection //We might end up with more than one polygon and they are connected via clipvertex nextpoly //To get the intersection, we should //Traverese the polygon until an intersection is encountered. Add this intersection to the clipped polygon. //Traversal direction is determined by the entry/exit bool. // - If the intersection is an entry, move forward along the current polygon // - If the intersection is an exit, then change polygon, proceed in the backward direction of the other polygon // - Change polygon if a new intersection is found //If we want to the !intersection, we just travel in the reverse direction from the first intersection vertex private static List <ClipVertex> GetClippedPolygon(List <ClipVertex> poly, bool getIntersectionPolygon) { List <ClipVertex> finalPolygon = new List <ClipVertex>(); //First we have to reset in case we are repeating this method several times depending on the type of boolean operation ResetVertices(poly); //Find the first intersection point which is always where we start ClipVertex thisVertex = FindFirstEntryVertex(poly); //Save this so we know when to stop the algortihm ClipVertex firstVertex = thisVertex; finalPolygon.Add(thisVertex); thisVertex.isTakenByFinalPolygon = true; thisVertex.neighbor.isTakenByFinalPolygon = true; //These rows is the only part thats different if we want to get the intersection or the !intersection //Are needed once again if there are more than one polygon bool isMovingForward = getIntersectionPolygon ? true : false; thisVertex = getIntersectionPolygon ? thisVertex.next : thisVertex.prev; int safety = 0; while (true) { if (thisVertex.Equals(firstVertex) || (thisVertex.neighbor != null && thisVertex.neighbor.Equals(firstVertex))) { //Debug.Log("Found a polygon with: " + finalPolygon.Count + " vertices"); //Debug.Log("Intersection vertices: " + intersectionVertices.Count); //Try to find the next intersection point in case we end up with more than one polygon ClipVertex nextVertex = FindFirstEntryVertex(poly); //Stop if we are out of intersection vertices if (nextVertex == null) { //Debug.Log("No more polygons can be found"); break; } //Find an entry vertex and start over with another polygon else { //Debug.Log("Find another polygon"); //Debug.Log(thisVertex.nextPoly); //Need to connect the polygons finalPolygon[finalPolygon.Count - 1].nextPoly = nextVertex; //Change to a new polygon thisVertex = nextVertex; firstVertex = nextVertex; finalPolygon.Add(thisVertex); thisVertex.isTakenByFinalPolygon = true; thisVertex.neighbor.isTakenByFinalPolygon = true; //Do we want to get the intersection or the !intersection isMovingForward = getIntersectionPolygon ? true : false; thisVertex = getIntersectionPolygon ? thisVertex.next : thisVertex.prev; } } //If this is not an intersection, then just add it if (!thisVertex.isIntersection) { finalPolygon.Add(thisVertex); //And move in the direction we are moving thisVertex = isMovingForward ? thisVertex.next : thisVertex.prev; } else { thisVertex.isTakenByFinalPolygon = true; thisVertex.neighbor.isTakenByFinalPolygon = true; //Jump to the other polygon thisVertex = thisVertex.neighbor; finalPolygon.Add(thisVertex); //Move forward/ back depending on if this is an entry / exit vertex and if we want to find the intersection or not if (getIntersectionPolygon) { isMovingForward = thisVertex.isEntry ? true : false; thisVertex = thisVertex.isEntry ? thisVertex.next : thisVertex.prev; } else { isMovingForward = !isMovingForward; thisVertex = isMovingForward ? thisVertex.next : thisVertex.prev; } } safety += 1; if (safety > 100000) { Debug.Log("Endless loop when creating clipped polygon"); //Debug.Log(isMovingForward); break; } } //Debug //float size = 0.02f; //for (int i = 0; i < finalPolygon.Count; i++) //{ // int iMinusOne = MathUtility.ClampListIndex(i - 1, finalPolygon.Count); // Vector3 v1 = new Vector3(finalPolygon[i].coordinate.x, 0f, finalPolygon[i].coordinate.y); // Vector3 v2 = new Vector3(finalPolygon[iMinusOne].coordinate.x, 0f, finalPolygon[iMinusOne].coordinate.y); // Gizmos.color = Color.white; // Gizmos.DrawWireSphere(v1, size); // Gizmos.DrawLine(v1, v2); // size += 0.005f; //} //Debug.Log(finalPolygon.Count); return(finalPolygon); }