-/*\r
- * Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors\r
- * http://code.google.com/p/poly2tri/\r
- *\r
- * All rights reserved.\r
- *\r
- * Redistribution and use in source and binary forms, with or without modification,\r
- * are permitted provided that the following conditions are met:\r
- *\r
- * * Redistributions of source code must retain the above copyright notice,\r
- * this list of conditions and the following disclaimer.\r
- * * Redistributions in binary form must reproduce the above copyright notice,\r
- * this list of conditions and the following disclaimer in the documentation\r
- * and/or other materials provided with the distribution.\r
- * * Neither the name of Poly2Tri nor the names of its contributors may be\r
- * used to endorse or promote products derived from this software without specific\r
- * prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
- */\r
-#include <stdexcept>\r
-#include "sweep.h"\r
-#include "sweep_context.h"\r
-#include "advancing_front.h"\r
-#include "../common/utils.h"\r
-\r
-namespace p2t {\r
-\r
-// Triangulate simple polygon with holes\r
-void Sweep::Triangulate(SweepContext& tcx)\r
-{\r
- tcx.InitTriangulation();\r
- tcx.CreateAdvancingFront(nodes_);\r
- // Sweep points; build mesh\r
- SweepPoints(tcx);\r
- // Clean up\r
- FinalizationPolygon(tcx);\r
-}\r
-\r
-void Sweep::SweepPoints(SweepContext& tcx)\r
-{\r
- for (int i = 1; i < tcx.point_count(); i++) {\r
- Point& point = *tcx.GetPoint(i);\r
- Node* node = &PointEvent(tcx, point);\r
- for (unsigned int i = 0; i < point.edge_list.size(); i++) {\r
- EdgeEvent(tcx, point.edge_list[i], node);\r
- }\r
- }\r
-}\r
-\r
-void Sweep::FinalizationPolygon(SweepContext& tcx)\r
-{\r
- // Get an Internal triangle to start with\r
- Triangle* t = tcx.front()->head()->next->triangle;\r
- Point* p = tcx.front()->head()->next->point;\r
- while (!t->GetConstrainedEdgeCW(*p)) {\r
- t = t->NeighborCCW(*p);\r
- }\r
-\r
- // Collect interior triangles constrained by edges\r
- tcx.MeshClean(*t);\r
-}\r
-\r
-Node& Sweep::PointEvent(SweepContext& tcx, Point& point)\r
-{\r
- Node& node = tcx.LocateNode(point);\r
- Node& new_node = NewFrontTriangle(tcx, point, node);\r
-\r
- // Only need to check +epsilon since point never have smaller\r
- // x value than node due to how we fetch nodes from the front\r
- if (point.x <= node.point->x + EPSILON) {\r
- Fill(tcx, node);\r
- }\r
-\r
- //tcx.AddNode(new_node);\r
-\r
- FillAdvancingFront(tcx, new_node);\r
- return new_node;\r
-}\r
-\r
-void Sweep::EdgeEvent(SweepContext& tcx, Edge* edge, Node* node)\r
-{\r
- tcx.edge_event.constrained_edge = edge;\r
- tcx.edge_event.right = (edge->p->x > edge->q->x);\r
-\r
- if (IsEdgeSideOfTriangle(*node->triangle, *edge->p, *edge->q)) {\r
- return;\r
- }\r
-\r
- // For now we will do all needed filling\r
- // TODO: integrate with flip process might give some better performance\r
- // but for now this avoid the issue with cases that needs both flips and fills\r
- FillEdgeEvent(tcx, edge, node);\r
- EdgeEvent(tcx, *edge->p, *edge->q, node->triangle, *edge->q);\r
-}\r
-\r
-void Sweep::EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangle, Point& point)\r
-{\r
- if (IsEdgeSideOfTriangle(*triangle, ep, eq)) {\r
- return;\r
- }\r
-\r
- Point* p1 = triangle->PointCCW(point);\r
- Orientation o1 = Orient2d(eq, *p1, ep);\r
- if (o1 == COLLINEAR) {\r
- // ASSIMP_CHANGE (aramis_acg)\r
- throw std::runtime_error("EdgeEvent - collinear points not supported");\r
- if( triangle->Contains(&eq, p1)) {\r
- triangle->MarkConstrainedEdge(&eq, p1 );\r
- // We are modifying the constraint maybe it would be better to \r
- // not change the given constraint and just keep a variable for the new constraint\r
- tcx.edge_event.constrained_edge->q = p1;\r
- triangle = &triangle->NeighborAcross(point);\r
- EdgeEvent( tcx, ep, *p1, triangle, *p1 );\r
- } else {\r
- // ASSIMP_CHANGE (aramis_acg)\r
- std::runtime_error("EdgeEvent - collinear points not supported");\r
- }\r
- return;\r
- }\r
-\r
- Point* p2 = triangle->PointCW(point);\r
- Orientation o2 = Orient2d(eq, *p2, ep);\r
- if (o2 == COLLINEAR) {\r
- // ASSIMP_CHANGE (aramis_acg)\r
- throw std::runtime_error("EdgeEvent - collinear points not supported");\r
-\r
- if( triangle->Contains(&eq, p2)) {\r
- triangle->MarkConstrainedEdge(&eq, p2 );\r
- // We are modifying the constraint maybe it would be better to \r
- // not change the given constraint and just keep a variable for the new constraint\r
- tcx.edge_event.constrained_edge->q = p2;\r
- triangle = &triangle->NeighborAcross(point);\r
- EdgeEvent( tcx, ep, *p2, triangle, *p2 );\r
- } else {\r
- // ASSIMP_CHANGE (aramis_acg)\r
- throw std::runtime_error("EdgeEvent - collinear points not supported");\r
- }\r
- return;\r
- }\r
-\r
- if (o1 == o2) {\r
- // Need to decide if we are rotating CW or CCW to get to a triangle\r
- // that will cross edge\r
- if (o1 == CW) {\r
- triangle = triangle->NeighborCCW(point);\r
- } else{\r
- triangle = triangle->NeighborCW(point);\r
- }\r
- EdgeEvent(tcx, ep, eq, triangle, point);\r
- } else {\r
- // This triangle crosses constraint so lets flippin start!\r
- FlipEdgeEvent(tcx, ep, eq, triangle, point);\r
- }\r
-}\r
-\r
-bool Sweep::IsEdgeSideOfTriangle(Triangle& triangle, Point& ep, Point& eq)\r
-{\r
- int index = triangle.EdgeIndex(&ep, &eq);\r
-\r
- if (index != -1) {\r
- triangle.MarkConstrainedEdge(index);\r
- Triangle* t = triangle.GetNeighbor(index);\r
- if (t) {\r
- t->MarkConstrainedEdge(&ep, &eq);\r
- }\r
- return true;\r
- }\r
- return false;\r
-}\r
-\r
-Node& Sweep::NewFrontTriangle(SweepContext& tcx, Point& point, Node& node)\r
-{\r
- Triangle* triangle = new Triangle(point, *node.point, *node.next->point);\r
-\r
- triangle->MarkNeighbor(*node.triangle);\r
- tcx.AddToMap(triangle);\r
-\r
- Node* new_node = new Node(point);\r
- nodes_.push_back(new_node);\r
-\r
- new_node->next = node.next;\r
- new_node->prev = &node;\r
- node.next->prev = new_node;\r
- node.next = new_node;\r
-\r
- if (!Legalize(tcx, *triangle)) {\r
- tcx.MapTriangleToNodes(*triangle);\r
- }\r
-\r
- return *new_node;\r
-}\r
-\r
-void Sweep::Fill(SweepContext& tcx, Node& node)\r
-{\r
- Triangle* triangle = new Triangle(*node.prev->point, *node.point, *node.next->point);\r
-\r
- // TODO: should copy the constrained_edge value from neighbor triangles\r
- // for now constrained_edge values are copied during the legalize\r
- triangle->MarkNeighbor(*node.prev->triangle);\r
- triangle->MarkNeighbor(*node.triangle);\r
-\r
- tcx.AddToMap(triangle);\r
-\r
- // Update the advancing front\r
- node.prev->next = node.next;\r
- node.next->prev = node.prev;\r
-\r
- // If it was legalized the triangle has already been mapped\r
- if (!Legalize(tcx, *triangle)) {\r
- tcx.MapTriangleToNodes(*triangle);\r
- }\r
-\r
-}\r
-\r
-void Sweep::FillAdvancingFront(SweepContext& tcx, Node& n)\r
-{\r
-\r
- // Fill right holes\r
- Node* node = n.next;\r
-\r
- while (node->next) {\r
- double angle = HoleAngle(*node);\r
- if (angle > PI_2 || angle < -PI_2) break;\r
- Fill(tcx, *node);\r
- node = node->next;\r
- }\r
-\r
- // Fill left holes\r
- node = n.prev;\r
-\r
- while (node->prev) {\r
- double angle = HoleAngle(*node);\r
- if (angle > PI_2 || angle < -PI_2) break;\r
- Fill(tcx, *node);\r
- node = node->prev;\r
- }\r
-\r
- // Fill right basins\r
- if (n.next && n.next->next) {\r
- double angle = BasinAngle(n);\r
- if (angle < PI_3div4) {\r
- FillBasin(tcx, n);\r
- }\r
- }\r
-}\r
-\r
-double Sweep::BasinAngle(Node& node)\r
-{\r
- double ax = node.point->x - node.next->next->point->x;\r
- double ay = node.point->y - node.next->next->point->y;\r
- return atan2(ay, ax);\r
-}\r
-\r
-double Sweep::HoleAngle(Node& node)\r
-{\r
- /* Complex plane\r
- * ab = cosA +i*sinA\r
- * ab = (ax + ay*i)(bx + by*i) = (ax*bx + ay*by) + i(ax*by-ay*bx)\r
- * atan2(y,x) computes the principal value of the argument function\r
- * applied to the complex number x+iy\r
- * Where x = ax*bx + ay*by\r
- * y = ax*by - ay*bx\r
- */\r
- double ax = node.next->point->x - node.point->x;\r
- double ay = node.next->point->y - node.point->y;\r
- double bx = node.prev->point->x - node.point->x;\r
- double by = node.prev->point->y - node.point->y;\r
- return atan2(ax * by - ay * bx, ax * bx + ay * by);\r
-}\r
-\r
-bool Sweep::Legalize(SweepContext& tcx, Triangle& t)\r
-{\r
- // To legalize a triangle we start by finding if any of the three edges\r
- // violate the Delaunay condition\r
- for (int i = 0; i < 3; i++) {\r
- if (t.delaunay_edge[i])\r
- continue;\r
-\r
- Triangle* ot = t.GetNeighbor(i);\r
-\r
- if (ot) {\r
- Point* p = t.GetPoint(i);\r
- Point* op = ot->OppositePoint(t, *p);\r
- int oi = ot->Index(op);\r
-\r
- // If this is a Constrained Edge or a Delaunay Edge(only during recursive legalization)\r
- // then we should not try to legalize\r
- if (ot->constrained_edge[oi] || ot->delaunay_edge[oi]) {\r
- t.constrained_edge[i] = ot->constrained_edge[oi];\r
- continue;\r
- }\r
-\r
- bool inside = Incircle(*p, *t.PointCCW(*p), *t.PointCW(*p), *op);\r
-\r
- if (inside) {\r
- // Lets mark this shared edge as Delaunay\r
- t.delaunay_edge[i] = true;\r
- ot->delaunay_edge[oi] = true;\r
-\r
- // Lets rotate shared edge one vertex CW to legalize it\r
- RotateTrianglePair(t, *p, *ot, *op);\r
-\r
- // We now got one valid Delaunay Edge shared by two triangles\r
- // This gives us 4 new edges to check for Delaunay\r
-\r
- // Make sure that triangle to node mapping is done only one time for a specific triangle\r
- bool not_legalized = !Legalize(tcx, t);\r
- if (not_legalized) {\r
- tcx.MapTriangleToNodes(t);\r
- }\r
-\r
- not_legalized = !Legalize(tcx, *ot);\r
- if (not_legalized)\r
- tcx.MapTriangleToNodes(*ot);\r
-\r
- // Reset the Delaunay edges, since they only are valid Delaunay edges\r
- // until we add a new triangle or point.\r
- // XXX: need to think about this. Can these edges be tried after we\r
- // return to previous recursive level?\r
- t.delaunay_edge[i] = false;\r
- ot->delaunay_edge[oi] = false;\r
-\r
- // If triangle have been legalized no need to check the other edges since\r
- // the recursive legalization will handles those so we can end here.\r
- return true;\r
- }\r
- }\r
- }\r
- return false;\r
-}\r
-\r
-bool Sweep::Incircle(Point& pa, Point& pb, Point& pc, Point& pd)\r
-{\r
- double adx = pa.x - pd.x;\r
- double ady = pa.y - pd.y;\r
- double bdx = pb.x - pd.x;\r
- double bdy = pb.y - pd.y;\r
-\r
- double adxbdy = adx * bdy;\r
- double bdxady = bdx * ady;\r
- double oabd = adxbdy - bdxady;\r
-\r
- if (oabd <= 0)\r
- return false;\r
-\r
- double cdx = pc.x - pd.x;\r
- double cdy = pc.y - pd.y;\r
-\r
- double cdxady = cdx * ady;\r
- double adxcdy = adx * cdy;\r
- double ocad = cdxady - adxcdy;\r
-\r
- if (ocad <= 0)\r
- return false;\r
-\r
- double bdxcdy = bdx * cdy;\r
- double cdxbdy = cdx * bdy;\r
-\r
- double alift = adx * adx + ady * ady;\r
- double blift = bdx * bdx + bdy * bdy;\r
- double clift = cdx * cdx + cdy * cdy;\r
-\r
- double det = alift * (bdxcdy - cdxbdy) + blift * ocad + clift * oabd;\r
-\r
- return det > 0;\r
-}\r
-\r
-void Sweep::RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op)\r
-{\r
- Triangle* n1, *n2, *n3, *n4;\r
- n1 = t.NeighborCCW(p);\r
- n2 = t.NeighborCW(p);\r
- n3 = ot.NeighborCCW(op);\r
- n4 = ot.NeighborCW(op);\r
-\r
- bool ce1, ce2, ce3, ce4;\r
- ce1 = t.GetConstrainedEdgeCCW(p);\r
- ce2 = t.GetConstrainedEdgeCW(p);\r
- ce3 = ot.GetConstrainedEdgeCCW(op);\r
- ce4 = ot.GetConstrainedEdgeCW(op);\r
-\r
- bool de1, de2, de3, de4;\r
- de1 = t.GetDelunayEdgeCCW(p);\r
- de2 = t.GetDelunayEdgeCW(p);\r
- de3 = ot.GetDelunayEdgeCCW(op);\r
- de4 = ot.GetDelunayEdgeCW(op);\r
-\r
- t.Legalize(p, op);\r
- ot.Legalize(op, p);\r
-\r
- // Remap delaunay_edge\r
- ot.SetDelunayEdgeCCW(p, de1);\r
- t.SetDelunayEdgeCW(p, de2);\r
- t.SetDelunayEdgeCCW(op, de3);\r
- ot.SetDelunayEdgeCW(op, de4);\r
-\r
- // Remap constrained_edge\r
- ot.SetConstrainedEdgeCCW(p, ce1);\r
- t.SetConstrainedEdgeCW(p, ce2);\r
- t.SetConstrainedEdgeCCW(op, ce3);\r
- ot.SetConstrainedEdgeCW(op, ce4);\r
-\r
- // Remap neighbors\r
- // XXX: might optimize the markNeighbor by keeping track of\r
- // what side should be assigned to what neighbor after the\r
- // rotation. Now mark neighbor does lots of testing to find\r
- // the right side.\r
- t.ClearNeighbors();\r
- ot.ClearNeighbors();\r
- if (n1) ot.MarkNeighbor(*n1);\r
- if (n2) t.MarkNeighbor(*n2);\r
- if (n3) t.MarkNeighbor(*n3);\r
- if (n4) ot.MarkNeighbor(*n4);\r
- t.MarkNeighbor(ot);\r
-}\r
-\r
-void Sweep::FillBasin(SweepContext& tcx, Node& node)\r
-{\r
- if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) {\r
- tcx.basin.left_node = node.next->next;\r
- } else {\r
- tcx.basin.left_node = node.next;\r
- }\r
-\r
- // Find the bottom and right node\r
- tcx.basin.bottom_node = tcx.basin.left_node;\r
- while (tcx.basin.bottom_node->next\r
- && tcx.basin.bottom_node->point->y >= tcx.basin.bottom_node->next->point->y) {\r
- tcx.basin.bottom_node = tcx.basin.bottom_node->next;\r
- }\r
- if (tcx.basin.bottom_node == tcx.basin.left_node) {\r
- // No valid basin\r
- return;\r
- }\r
-\r
- tcx.basin.right_node = tcx.basin.bottom_node;\r
- while (tcx.basin.right_node->next\r
- && tcx.basin.right_node->point->y < tcx.basin.right_node->next->point->y) {\r
- tcx.basin.right_node = tcx.basin.right_node->next;\r
- }\r
- if (tcx.basin.right_node == tcx.basin.bottom_node) {\r
- // No valid basins\r
- return;\r
- }\r
-\r
- tcx.basin.width = tcx.basin.right_node->point->x - tcx.basin.left_node->point->x;\r
- tcx.basin.left_highest = tcx.basin.left_node->point->y > tcx.basin.right_node->point->y;\r
-\r
- FillBasinReq(tcx, tcx.basin.bottom_node);\r
-}\r
-\r
-void Sweep::FillBasinReq(SweepContext& tcx, Node* node)\r
-{\r
- // if shallow stop filling\r
- if (IsShallow(tcx, *node)) {\r
- return;\r
- }\r
-\r
- Fill(tcx, *node);\r
-\r
- if (node->prev == tcx.basin.left_node && node->next == tcx.basin.right_node) {\r
- return;\r
- } else if (node->prev == tcx.basin.left_node) {\r
- Orientation o = Orient2d(*node->point, *node->next->point, *node->next->next->point);\r
- if (o == CW) {\r
- return;\r
- }\r
- node = node->next;\r
- } else if (node->next == tcx.basin.right_node) {\r
- Orientation o = Orient2d(*node->point, *node->prev->point, *node->prev->prev->point);\r
- if (o == CCW) {\r
- return;\r
- }\r
- node = node->prev;\r
- } else {\r
- // Continue with the neighbor node with lowest Y value\r
- if (node->prev->point->y < node->next->point->y) {\r
- node = node->prev;\r
- } else {\r
- node = node->next;\r
- }\r
- }\r
-\r
- FillBasinReq(tcx, node);\r
-}\r
-\r
-bool Sweep::IsShallow(SweepContext& tcx, Node& node)\r
-{\r
- double height;\r
-\r
- if (tcx.basin.left_highest) {\r
- height = tcx.basin.left_node->point->y - node.point->y;\r
- } else {\r
- height = tcx.basin.right_node->point->y - node.point->y;\r
- }\r
-\r
- // if shallow stop filling\r
- if (tcx.basin.width > height) {\r
- return true;\r
- }\r
- return false;\r
-}\r
-\r
-void Sweep::FillEdgeEvent(SweepContext& tcx, Edge* edge, Node* node)\r
-{\r
- if (tcx.edge_event.right) {\r
- FillRightAboveEdgeEvent(tcx, edge, node);\r
- } else {\r
- FillLeftAboveEdgeEvent(tcx, edge, node);\r
- }\r
-}\r
-\r
-void Sweep::FillRightAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node* node)\r
-{\r
- while (node->next->point->x < edge->p->x) {\r
- // Check if next node is below the edge\r
- if (Orient2d(*edge->q, *node->next->point, *edge->p) == CCW) {\r
- FillRightBelowEdgeEvent(tcx, edge, *node);\r
- } else {\r
- node = node->next;\r
- }\r
- }\r
-}\r
-\r
-void Sweep::FillRightBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)\r
-{\r
- if (node.point->x < edge->p->x) {\r
- if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) {\r
- // Concave\r
- FillRightConcaveEdgeEvent(tcx, edge, node);\r
- } else{\r
- // Convex\r
- FillRightConvexEdgeEvent(tcx, edge, node);\r
- // Retry this one\r
- FillRightBelowEdgeEvent(tcx, edge, node);\r
- }\r
- }\r
-}\r
-\r
-void Sweep::FillRightConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)\r
-{\r
- Fill(tcx, *node.next);\r
- if (node.next->point != edge->p) {\r
- // Next above or below edge?\r
- if (Orient2d(*edge->q, *node.next->point, *edge->p) == CCW) {\r
- // Below\r
- if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) {\r
- // Next is concave\r
- FillRightConcaveEdgeEvent(tcx, edge, node);\r
- } else {\r
- // Next is convex\r
- }\r
- }\r
- }\r
-\r
-}\r
-\r
-void Sweep::FillRightConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)\r
-{\r
- // Next concave or convex?\r
- if (Orient2d(*node.next->point, *node.next->next->point, *node.next->next->next->point) == CCW) {\r
- // Concave\r
- FillRightConcaveEdgeEvent(tcx, edge, *node.next);\r
- } else{\r
- // Convex\r
- // Next above or below edge?\r
- if (Orient2d(*edge->q, *node.next->next->point, *edge->p) == CCW) {\r
- // Below\r
- FillRightConvexEdgeEvent(tcx, edge, *node.next);\r
- } else{\r
- // Above\r
- }\r
- }\r
-}\r
-\r
-void Sweep::FillLeftAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node* node)\r
-{\r
- while (node->prev->point->x > edge->p->x) {\r
- // Check if next node is below the edge\r
- if (Orient2d(*edge->q, *node->prev->point, *edge->p) == CW) {\r
- FillLeftBelowEdgeEvent(tcx, edge, *node);\r
- } else {\r
- node = node->prev;\r
- }\r
- }\r
-}\r
-\r
-void Sweep::FillLeftBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)\r
-{\r
- if (node.point->x > edge->p->x) {\r
- if (Orient2d(*node.point, *node.prev->point, *node.prev->prev->point) == CW) {\r
- // Concave\r
- FillLeftConcaveEdgeEvent(tcx, edge, node);\r
- } else {\r
- // Convex\r
- FillLeftConvexEdgeEvent(tcx, edge, node);\r
- // Retry this one\r
- FillLeftBelowEdgeEvent(tcx, edge, node);\r
- }\r
- }\r
-}\r
-\r
-void Sweep::FillLeftConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)\r
-{\r
- // Next concave or convex?\r
- if (Orient2d(*node.prev->point, *node.prev->prev->point, *node.prev->prev->prev->point) == CW) {\r
- // Concave\r
- FillLeftConcaveEdgeEvent(tcx, edge, *node.prev);\r
- } else{\r
- // Convex\r
- // Next above or below edge?\r
- if (Orient2d(*edge->q, *node.prev->prev->point, *edge->p) == CW) {\r
- // Below\r
- FillLeftConvexEdgeEvent(tcx, edge, *node.prev);\r
- } else{\r
- // Above\r
- }\r
- }\r
-}\r
-\r
-void Sweep::FillLeftConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)\r
-{\r
- Fill(tcx, *node.prev);\r
- if (node.prev->point != edge->p) {\r
- // Next above or below edge?\r
- if (Orient2d(*edge->q, *node.prev->point, *edge->p) == CW) {\r
- // Below\r
- if (Orient2d(*node.point, *node.prev->point, *node.prev->prev->point) == CW) {\r
- // Next is concave\r
- FillLeftConcaveEdgeEvent(tcx, edge, node);\r
- } else{\r
- // Next is convex\r
- }\r
- }\r
- }\r
-\r
-}\r
-\r
-void Sweep::FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* t, Point& p)\r
-{\r
- Triangle& ot = t->NeighborAcross(p);\r
- Point& op = *ot.OppositePoint(*t, p);\r
-\r
- if (InScanArea(p, *t->PointCCW(p), *t->PointCW(p), op)) {\r
- // Lets rotate shared edge one vertex CW\r
- RotateTrianglePair(*t, p, ot, op);\r
- tcx.MapTriangleToNodes(*t);\r
- tcx.MapTriangleToNodes(ot);\r
-\r
- if (p == eq && op == ep) {\r
- if (eq == *tcx.edge_event.constrained_edge->q && ep == *tcx.edge_event.constrained_edge->p) {\r
- t->MarkConstrainedEdge(&ep, &eq);\r
- ot.MarkConstrainedEdge(&ep, &eq);\r
- Legalize(tcx, *t);\r
- Legalize(tcx, ot);\r
- } else {\r
- // XXX: I think one of the triangles should be legalized here?\r
- }\r
- } else {\r
- Orientation o = Orient2d(eq, op, ep);\r
- t = &NextFlipTriangle(tcx, (int)o, *t, ot, p, op);\r
- FlipEdgeEvent(tcx, ep, eq, t, p);\r
- }\r
- } else {\r
- Point& newP = NextFlipPoint(ep, eq, ot, op);\r
- FlipScanEdgeEvent(tcx, ep, eq, *t, ot, newP);\r
- EdgeEvent(tcx, ep, eq, t, p);\r
- }\r
-}\r
-\r
-Triangle& Sweep::NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triangle& ot, Point& p, Point& op)\r
-{\r
- if (o == CCW) {\r
- // ot is not crossing edge after flip\r
- int edge_index = ot.EdgeIndex(&p, &op);\r
- ot.delaunay_edge[edge_index] = true;\r
- Legalize(tcx, ot);\r
- ot.ClearDelunayEdges();\r
- return t;\r
- }\r
-\r
- // t is not crossing edge after flip\r
- int edge_index = t.EdgeIndex(&p, &op);\r
-\r
- t.delaunay_edge[edge_index] = true;\r
- Legalize(tcx, t);\r
- t.ClearDelunayEdges();\r
- return ot;\r
-}\r
-\r
-Point& Sweep::NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op)\r
-{\r
- Orientation o2d = Orient2d(eq, op, ep);\r
- if (o2d == CW) {\r
- // Right\r
- return *ot.PointCCW(op);\r
- } else if (o2d == CCW) {\r
- // Left\r
- return *ot.PointCW(op);\r
- } else{\r
- //throw new RuntimeException("[Unsupported] Opposing point on constrained edge");\r
- // ASSIMP_CHANGE (aramis_acg)\r
- throw std::runtime_error("[Unsupported] Opposing point on constrained edge");\r
- }\r
-}\r
-\r
-void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle,\r
- Triangle& t, Point& p)\r
-{\r
- Triangle& ot = t.NeighborAcross(p);\r
- Point& op = *ot.OppositePoint(t, p);\r
-\r
- if (InScanArea(eq, *flip_triangle.PointCCW(eq), *flip_triangle.PointCW(eq), op)) {\r
- // flip with new edge op->eq\r
- FlipEdgeEvent(tcx, eq, op, &ot, op);\r
- // TODO: Actually I just figured out that it should be possible to\r
- // improve this by getting the next ot and op before the the above\r
- // flip and continue the flipScanEdgeEvent here\r
- // set new ot and op here and loop back to inScanArea test\r
- // also need to set a new flip_triangle first\r
- // Turns out at first glance that this is somewhat complicated\r
- // so it will have to wait.\r
- } else{\r
- Point& newP = NextFlipPoint(ep, eq, ot, op);\r
- FlipScanEdgeEvent(tcx, ep, eq, flip_triangle, ot, newP);\r
- }\r
-}\r
-\r
-Sweep::~Sweep() {\r
-\r
- // Clean up memory\r
- for(unsigned int i = 0; i < nodes_.size(); i++) {\r
- delete nodes_[i];\r
- }\r
-\r
-}\r
-\r
-}\r
-\r
+/*
+ * Poly2Tri Copyright (c) 2009-2010, Poly2Tri Contributors
+ * http://code.google.com/p/poly2tri/
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * * Neither the name of Poly2Tri nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <stdexcept>
+#include "sweep.h"
+#include "sweep_context.h"
+#include "advancing_front.h"
+#include "../common/utils.h"
+
+namespace p2t {
+
+// Triangulate simple polygon with holes
+void Sweep::Triangulate(SweepContext& tcx)
+{
+ tcx.InitTriangulation();
+ tcx.CreateAdvancingFront(nodes_);
+ // Sweep points; build mesh
+ SweepPoints(tcx);
+ // Clean up
+ FinalizationPolygon(tcx);
+}
+
+void Sweep::SweepPoints(SweepContext& tcx)
+{
+ for (int i = 1; i < tcx.point_count(); i++) {
+ Point& point = *tcx.GetPoint(i);
+ Node* node = &PointEvent(tcx, point);
+ for (unsigned int i = 0; i < point.edge_list.size(); i++) {
+ EdgeEvent(tcx, point.edge_list[i], node);
+ }
+ }
+}
+
+void Sweep::FinalizationPolygon(SweepContext& tcx)
+{
+ // Get an Internal triangle to start with
+ Triangle* t = tcx.front()->head()->next->triangle;
+ Point* p = tcx.front()->head()->next->point;
+ while (!t->GetConstrainedEdgeCW(*p)) {
+ t = t->NeighborCCW(*p);
+ }
+
+ // Collect interior triangles constrained by edges
+ tcx.MeshClean(*t);
+}
+
+Node& Sweep::PointEvent(SweepContext& tcx, Point& point)
+{
+ Node& node = tcx.LocateNode(point);
+ Node& new_node = NewFrontTriangle(tcx, point, node);
+
+ // Only need to check +epsilon since point never have smaller
+ // x value than node due to how we fetch nodes from the front
+ if (point.x <= node.point->x + EPSILON) {
+ Fill(tcx, node);
+ }
+
+ //tcx.AddNode(new_node);
+
+ FillAdvancingFront(tcx, new_node);
+ return new_node;
+}
+
+void Sweep::EdgeEvent(SweepContext& tcx, Edge* edge, Node* node)
+{
+ tcx.edge_event.constrained_edge = edge;
+ tcx.edge_event.right = (edge->p->x > edge->q->x);
+
+ if (IsEdgeSideOfTriangle(*node->triangle, *edge->p, *edge->q)) {
+ return;
+ }
+
+ // For now we will do all needed filling
+ // TODO: integrate with flip process might give some better performance
+ // but for now this avoid the issue with cases that needs both flips and fills
+ FillEdgeEvent(tcx, edge, node);
+ EdgeEvent(tcx, *edge->p, *edge->q, node->triangle, *edge->q);
+}
+
+void Sweep::EdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* triangle, Point& point)
+{
+ if (IsEdgeSideOfTriangle(*triangle, ep, eq)) {
+ return;
+ }
+
+ Point* p1 = triangle->PointCCW(point);
+ Orientation o1 = Orient2d(eq, *p1, ep);
+ if (o1 == COLLINEAR) {
+ // ASSIMP_CHANGE (aramis_acg)
+ throw std::runtime_error("EdgeEvent - collinear points not supported");
+ if( triangle->Contains(&eq, p1)) {
+ triangle->MarkConstrainedEdge(&eq, p1 );
+ // We are modifying the constraint maybe it would be better to
+ // not change the given constraint and just keep a variable for the new constraint
+ tcx.edge_event.constrained_edge->q = p1;
+ triangle = &triangle->NeighborAcross(point);
+ EdgeEvent( tcx, ep, *p1, triangle, *p1 );
+ } else {
+ // ASSIMP_CHANGE (aramis_acg)
+ std::runtime_error("EdgeEvent - collinear points not supported");
+ }
+ return;
+ }
+
+ Point* p2 = triangle->PointCW(point);
+ Orientation o2 = Orient2d(eq, *p2, ep);
+ if (o2 == COLLINEAR) {
+ // ASSIMP_CHANGE (aramis_acg)
+ throw std::runtime_error("EdgeEvent - collinear points not supported");
+
+ if( triangle->Contains(&eq, p2)) {
+ triangle->MarkConstrainedEdge(&eq, p2 );
+ // We are modifying the constraint maybe it would be better to
+ // not change the given constraint and just keep a variable for the new constraint
+ tcx.edge_event.constrained_edge->q = p2;
+ triangle = &triangle->NeighborAcross(point);
+ EdgeEvent( tcx, ep, *p2, triangle, *p2 );
+ } else {
+ // ASSIMP_CHANGE (aramis_acg)
+ throw std::runtime_error("EdgeEvent - collinear points not supported");
+ }
+ return;
+ }
+
+ if (o1 == o2) {
+ // Need to decide if we are rotating CW or CCW to get to a triangle
+ // that will cross edge
+ if (o1 == CW) {
+ triangle = triangle->NeighborCCW(point);
+ } else{
+ triangle = triangle->NeighborCW(point);
+ }
+ EdgeEvent(tcx, ep, eq, triangle, point);
+ } else {
+ // This triangle crosses constraint so lets flippin start!
+ FlipEdgeEvent(tcx, ep, eq, triangle, point);
+ }
+}
+
+bool Sweep::IsEdgeSideOfTriangle(Triangle& triangle, Point& ep, Point& eq)
+{
+ int index = triangle.EdgeIndex(&ep, &eq);
+
+ if (index != -1) {
+ triangle.MarkConstrainedEdge(index);
+ Triangle* t = triangle.GetNeighbor(index);
+ if (t) {
+ t->MarkConstrainedEdge(&ep, &eq);
+ }
+ return true;
+ }
+ return false;
+}
+
+Node& Sweep::NewFrontTriangle(SweepContext& tcx, Point& point, Node& node)
+{
+ Triangle* triangle = new Triangle(point, *node.point, *node.next->point);
+
+ triangle->MarkNeighbor(*node.triangle);
+ tcx.AddToMap(triangle);
+
+ Node* new_node = new Node(point);
+ nodes_.push_back(new_node);
+
+ new_node->next = node.next;
+ new_node->prev = &node;
+ node.next->prev = new_node;
+ node.next = new_node;
+
+ if (!Legalize(tcx, *triangle)) {
+ tcx.MapTriangleToNodes(*triangle);
+ }
+
+ return *new_node;
+}
+
+void Sweep::Fill(SweepContext& tcx, Node& node)
+{
+ Triangle* triangle = new Triangle(*node.prev->point, *node.point, *node.next->point);
+
+ // TODO: should copy the constrained_edge value from neighbor triangles
+ // for now constrained_edge values are copied during the legalize
+ triangle->MarkNeighbor(*node.prev->triangle);
+ triangle->MarkNeighbor(*node.triangle);
+
+ tcx.AddToMap(triangle);
+
+ // Update the advancing front
+ node.prev->next = node.next;
+ node.next->prev = node.prev;
+
+ // If it was legalized the triangle has already been mapped
+ if (!Legalize(tcx, *triangle)) {
+ tcx.MapTriangleToNodes(*triangle);
+ }
+
+}
+
+void Sweep::FillAdvancingFront(SweepContext& tcx, Node& n)
+{
+
+ // Fill right holes
+ Node* node = n.next;
+
+ while (node->next) {
+ double angle = HoleAngle(*node);
+ if (angle > PI_2 || angle < -PI_2) break;
+ Fill(tcx, *node);
+ node = node->next;
+ }
+
+ // Fill left holes
+ node = n.prev;
+
+ while (node->prev) {
+ double angle = HoleAngle(*node);
+ if (angle > PI_2 || angle < -PI_2) break;
+ Fill(tcx, *node);
+ node = node->prev;
+ }
+
+ // Fill right basins
+ if (n.next && n.next->next) {
+ double angle = BasinAngle(n);
+ if (angle < PI_3div4) {
+ FillBasin(tcx, n);
+ }
+ }
+}
+
+double Sweep::BasinAngle(Node& node)
+{
+ double ax = node.point->x - node.next->next->point->x;
+ double ay = node.point->y - node.next->next->point->y;
+ return atan2(ay, ax);
+}
+
+double Sweep::HoleAngle(Node& node)
+{
+ /* Complex plane
+ * ab = cosA +i*sinA
+ * ab = (ax + ay*i)(bx + by*i) = (ax*bx + ay*by) + i(ax*by-ay*bx)
+ * atan2(y,x) computes the principal value of the argument function
+ * applied to the complex number x+iy
+ * Where x = ax*bx + ay*by
+ * y = ax*by - ay*bx
+ */
+ double ax = node.next->point->x - node.point->x;
+ double ay = node.next->point->y - node.point->y;
+ double bx = node.prev->point->x - node.point->x;
+ double by = node.prev->point->y - node.point->y;
+ return atan2(ax * by - ay * bx, ax * bx + ay * by);
+}
+
+bool Sweep::Legalize(SweepContext& tcx, Triangle& t)
+{
+ // To legalize a triangle we start by finding if any of the three edges
+ // violate the Delaunay condition
+ for (int i = 0; i < 3; i++) {
+ if (t.delaunay_edge[i])
+ continue;
+
+ Triangle* ot = t.GetNeighbor(i);
+
+ if (ot) {
+ Point* p = t.GetPoint(i);
+ Point* op = ot->OppositePoint(t, *p);
+ int oi = ot->Index(op);
+
+ // If this is a Constrained Edge or a Delaunay Edge(only during recursive legalization)
+ // then we should not try to legalize
+ if (ot->constrained_edge[oi] || ot->delaunay_edge[oi]) {
+ t.constrained_edge[i] = ot->constrained_edge[oi];
+ continue;
+ }
+
+ bool inside = Incircle(*p, *t.PointCCW(*p), *t.PointCW(*p), *op);
+
+ if (inside) {
+ // Lets mark this shared edge as Delaunay
+ t.delaunay_edge[i] = true;
+ ot->delaunay_edge[oi] = true;
+
+ // Lets rotate shared edge one vertex CW to legalize it
+ RotateTrianglePair(t, *p, *ot, *op);
+
+ // We now got one valid Delaunay Edge shared by two triangles
+ // This gives us 4 new edges to check for Delaunay
+
+ // Make sure that triangle to node mapping is done only one time for a specific triangle
+ bool not_legalized = !Legalize(tcx, t);
+ if (not_legalized) {
+ tcx.MapTriangleToNodes(t);
+ }
+
+ not_legalized = !Legalize(tcx, *ot);
+ if (not_legalized)
+ tcx.MapTriangleToNodes(*ot);
+
+ // Reset the Delaunay edges, since they only are valid Delaunay edges
+ // until we add a new triangle or point.
+ // XXX: need to think about this. Can these edges be tried after we
+ // return to previous recursive level?
+ t.delaunay_edge[i] = false;
+ ot->delaunay_edge[oi] = false;
+
+ // If triangle have been legalized no need to check the other edges since
+ // the recursive legalization will handles those so we can end here.
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+bool Sweep::Incircle(Point& pa, Point& pb, Point& pc, Point& pd)
+{
+ double adx = pa.x - pd.x;
+ double ady = pa.y - pd.y;
+ double bdx = pb.x - pd.x;
+ double bdy = pb.y - pd.y;
+
+ double adxbdy = adx * bdy;
+ double bdxady = bdx * ady;
+ double oabd = adxbdy - bdxady;
+
+ if (oabd <= 0)
+ return false;
+
+ double cdx = pc.x - pd.x;
+ double cdy = pc.y - pd.y;
+
+ double cdxady = cdx * ady;
+ double adxcdy = adx * cdy;
+ double ocad = cdxady - adxcdy;
+
+ if (ocad <= 0)
+ return false;
+
+ double bdxcdy = bdx * cdy;
+ double cdxbdy = cdx * bdy;
+
+ double alift = adx * adx + ady * ady;
+ double blift = bdx * bdx + bdy * bdy;
+ double clift = cdx * cdx + cdy * cdy;
+
+ double det = alift * (bdxcdy - cdxbdy) + blift * ocad + clift * oabd;
+
+ return det > 0;
+}
+
+void Sweep::RotateTrianglePair(Triangle& t, Point& p, Triangle& ot, Point& op)
+{
+ Triangle* n1, *n2, *n3, *n4;
+ n1 = t.NeighborCCW(p);
+ n2 = t.NeighborCW(p);
+ n3 = ot.NeighborCCW(op);
+ n4 = ot.NeighborCW(op);
+
+ bool ce1, ce2, ce3, ce4;
+ ce1 = t.GetConstrainedEdgeCCW(p);
+ ce2 = t.GetConstrainedEdgeCW(p);
+ ce3 = ot.GetConstrainedEdgeCCW(op);
+ ce4 = ot.GetConstrainedEdgeCW(op);
+
+ bool de1, de2, de3, de4;
+ de1 = t.GetDelunayEdgeCCW(p);
+ de2 = t.GetDelunayEdgeCW(p);
+ de3 = ot.GetDelunayEdgeCCW(op);
+ de4 = ot.GetDelunayEdgeCW(op);
+
+ t.Legalize(p, op);
+ ot.Legalize(op, p);
+
+ // Remap delaunay_edge
+ ot.SetDelunayEdgeCCW(p, de1);
+ t.SetDelunayEdgeCW(p, de2);
+ t.SetDelunayEdgeCCW(op, de3);
+ ot.SetDelunayEdgeCW(op, de4);
+
+ // Remap constrained_edge
+ ot.SetConstrainedEdgeCCW(p, ce1);
+ t.SetConstrainedEdgeCW(p, ce2);
+ t.SetConstrainedEdgeCCW(op, ce3);
+ ot.SetConstrainedEdgeCW(op, ce4);
+
+ // Remap neighbors
+ // XXX: might optimize the markNeighbor by keeping track of
+ // what side should be assigned to what neighbor after the
+ // rotation. Now mark neighbor does lots of testing to find
+ // the right side.
+ t.ClearNeighbors();
+ ot.ClearNeighbors();
+ if (n1) ot.MarkNeighbor(*n1);
+ if (n2) t.MarkNeighbor(*n2);
+ if (n3) t.MarkNeighbor(*n3);
+ if (n4) ot.MarkNeighbor(*n4);
+ t.MarkNeighbor(ot);
+}
+
+void Sweep::FillBasin(SweepContext& tcx, Node& node)
+{
+ if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) {
+ tcx.basin.left_node = node.next->next;
+ } else {
+ tcx.basin.left_node = node.next;
+ }
+
+ // Find the bottom and right node
+ tcx.basin.bottom_node = tcx.basin.left_node;
+ while (tcx.basin.bottom_node->next
+ && tcx.basin.bottom_node->point->y >= tcx.basin.bottom_node->next->point->y) {
+ tcx.basin.bottom_node = tcx.basin.bottom_node->next;
+ }
+ if (tcx.basin.bottom_node == tcx.basin.left_node) {
+ // No valid basin
+ return;
+ }
+
+ tcx.basin.right_node = tcx.basin.bottom_node;
+ while (tcx.basin.right_node->next
+ && tcx.basin.right_node->point->y < tcx.basin.right_node->next->point->y) {
+ tcx.basin.right_node = tcx.basin.right_node->next;
+ }
+ if (tcx.basin.right_node == tcx.basin.bottom_node) {
+ // No valid basins
+ return;
+ }
+
+ tcx.basin.width = tcx.basin.right_node->point->x - tcx.basin.left_node->point->x;
+ tcx.basin.left_highest = tcx.basin.left_node->point->y > tcx.basin.right_node->point->y;
+
+ FillBasinReq(tcx, tcx.basin.bottom_node);
+}
+
+void Sweep::FillBasinReq(SweepContext& tcx, Node* node)
+{
+ // if shallow stop filling
+ if (IsShallow(tcx, *node)) {
+ return;
+ }
+
+ Fill(tcx, *node);
+
+ if (node->prev == tcx.basin.left_node && node->next == tcx.basin.right_node) {
+ return;
+ } else if (node->prev == tcx.basin.left_node) {
+ Orientation o = Orient2d(*node->point, *node->next->point, *node->next->next->point);
+ if (o == CW) {
+ return;
+ }
+ node = node->next;
+ } else if (node->next == tcx.basin.right_node) {
+ Orientation o = Orient2d(*node->point, *node->prev->point, *node->prev->prev->point);
+ if (o == CCW) {
+ return;
+ }
+ node = node->prev;
+ } else {
+ // Continue with the neighbor node with lowest Y value
+ if (node->prev->point->y < node->next->point->y) {
+ node = node->prev;
+ } else {
+ node = node->next;
+ }
+ }
+
+ FillBasinReq(tcx, node);
+}
+
+bool Sweep::IsShallow(SweepContext& tcx, Node& node)
+{
+ double height;
+
+ if (tcx.basin.left_highest) {
+ height = tcx.basin.left_node->point->y - node.point->y;
+ } else {
+ height = tcx.basin.right_node->point->y - node.point->y;
+ }
+
+ // if shallow stop filling
+ if (tcx.basin.width > height) {
+ return true;
+ }
+ return false;
+}
+
+void Sweep::FillEdgeEvent(SweepContext& tcx, Edge* edge, Node* node)
+{
+ if (tcx.edge_event.right) {
+ FillRightAboveEdgeEvent(tcx, edge, node);
+ } else {
+ FillLeftAboveEdgeEvent(tcx, edge, node);
+ }
+}
+
+void Sweep::FillRightAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node* node)
+{
+ while (node->next->point->x < edge->p->x) {
+ // Check if next node is below the edge
+ if (Orient2d(*edge->q, *node->next->point, *edge->p) == CCW) {
+ FillRightBelowEdgeEvent(tcx, edge, *node);
+ } else {
+ node = node->next;
+ }
+ }
+}
+
+void Sweep::FillRightBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
+{
+ if (node.point->x < edge->p->x) {
+ if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) {
+ // Concave
+ FillRightConcaveEdgeEvent(tcx, edge, node);
+ } else{
+ // Convex
+ FillRightConvexEdgeEvent(tcx, edge, node);
+ // Retry this one
+ FillRightBelowEdgeEvent(tcx, edge, node);
+ }
+ }
+}
+
+void Sweep::FillRightConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
+{
+ Fill(tcx, *node.next);
+ if (node.next->point != edge->p) {
+ // Next above or below edge?
+ if (Orient2d(*edge->q, *node.next->point, *edge->p) == CCW) {
+ // Below
+ if (Orient2d(*node.point, *node.next->point, *node.next->next->point) == CCW) {
+ // Next is concave
+ FillRightConcaveEdgeEvent(tcx, edge, node);
+ } else {
+ // Next is convex
+ }
+ }
+ }
+
+}
+
+void Sweep::FillRightConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
+{
+ // Next concave or convex?
+ if (Orient2d(*node.next->point, *node.next->next->point, *node.next->next->next->point) == CCW) {
+ // Concave
+ FillRightConcaveEdgeEvent(tcx, edge, *node.next);
+ } else{
+ // Convex
+ // Next above or below edge?
+ if (Orient2d(*edge->q, *node.next->next->point, *edge->p) == CCW) {
+ // Below
+ FillRightConvexEdgeEvent(tcx, edge, *node.next);
+ } else{
+ // Above
+ }
+ }
+}
+
+void Sweep::FillLeftAboveEdgeEvent(SweepContext& tcx, Edge* edge, Node* node)
+{
+ while (node->prev->point->x > edge->p->x) {
+ // Check if next node is below the edge
+ if (Orient2d(*edge->q, *node->prev->point, *edge->p) == CW) {
+ FillLeftBelowEdgeEvent(tcx, edge, *node);
+ } else {
+ node = node->prev;
+ }
+ }
+}
+
+void Sweep::FillLeftBelowEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
+{
+ if (node.point->x > edge->p->x) {
+ if (Orient2d(*node.point, *node.prev->point, *node.prev->prev->point) == CW) {
+ // Concave
+ FillLeftConcaveEdgeEvent(tcx, edge, node);
+ } else {
+ // Convex
+ FillLeftConvexEdgeEvent(tcx, edge, node);
+ // Retry this one
+ FillLeftBelowEdgeEvent(tcx, edge, node);
+ }
+ }
+}
+
+void Sweep::FillLeftConvexEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
+{
+ // Next concave or convex?
+ if (Orient2d(*node.prev->point, *node.prev->prev->point, *node.prev->prev->prev->point) == CW) {
+ // Concave
+ FillLeftConcaveEdgeEvent(tcx, edge, *node.prev);
+ } else{
+ // Convex
+ // Next above or below edge?
+ if (Orient2d(*edge->q, *node.prev->prev->point, *edge->p) == CW) {
+ // Below
+ FillLeftConvexEdgeEvent(tcx, edge, *node.prev);
+ } else{
+ // Above
+ }
+ }
+}
+
+void Sweep::FillLeftConcaveEdgeEvent(SweepContext& tcx, Edge* edge, Node& node)
+{
+ Fill(tcx, *node.prev);
+ if (node.prev->point != edge->p) {
+ // Next above or below edge?
+ if (Orient2d(*edge->q, *node.prev->point, *edge->p) == CW) {
+ // Below
+ if (Orient2d(*node.point, *node.prev->point, *node.prev->prev->point) == CW) {
+ // Next is concave
+ FillLeftConcaveEdgeEvent(tcx, edge, node);
+ } else{
+ // Next is convex
+ }
+ }
+ }
+
+}
+
+void Sweep::FlipEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle* t, Point& p)
+{
+ Triangle& ot = t->NeighborAcross(p);
+ Point& op = *ot.OppositePoint(*t, p);
+
+ if (InScanArea(p, *t->PointCCW(p), *t->PointCW(p), op)) {
+ // Lets rotate shared edge one vertex CW
+ RotateTrianglePair(*t, p, ot, op);
+ tcx.MapTriangleToNodes(*t);
+ tcx.MapTriangleToNodes(ot);
+
+ if (p == eq && op == ep) {
+ if (eq == *tcx.edge_event.constrained_edge->q && ep == *tcx.edge_event.constrained_edge->p) {
+ t->MarkConstrainedEdge(&ep, &eq);
+ ot.MarkConstrainedEdge(&ep, &eq);
+ Legalize(tcx, *t);
+ Legalize(tcx, ot);
+ } else {
+ // XXX: I think one of the triangles should be legalized here?
+ }
+ } else {
+ Orientation o = Orient2d(eq, op, ep);
+ t = &NextFlipTriangle(tcx, (int)o, *t, ot, p, op);
+ FlipEdgeEvent(tcx, ep, eq, t, p);
+ }
+ } else {
+ Point& newP = NextFlipPoint(ep, eq, ot, op);
+ FlipScanEdgeEvent(tcx, ep, eq, *t, ot, newP);
+ EdgeEvent(tcx, ep, eq, t, p);
+ }
+}
+
+Triangle& Sweep::NextFlipTriangle(SweepContext& tcx, int o, Triangle& t, Triangle& ot, Point& p, Point& op)
+{
+ if (o == CCW) {
+ // ot is not crossing edge after flip
+ int edge_index = ot.EdgeIndex(&p, &op);
+ ot.delaunay_edge[edge_index] = true;
+ Legalize(tcx, ot);
+ ot.ClearDelunayEdges();
+ return t;
+ }
+
+ // t is not crossing edge after flip
+ int edge_index = t.EdgeIndex(&p, &op);
+
+ t.delaunay_edge[edge_index] = true;
+ Legalize(tcx, t);
+ t.ClearDelunayEdges();
+ return ot;
+}
+
+Point& Sweep::NextFlipPoint(Point& ep, Point& eq, Triangle& ot, Point& op)
+{
+ Orientation o2d = Orient2d(eq, op, ep);
+ if (o2d == CW) {
+ // Right
+ return *ot.PointCCW(op);
+ } else if (o2d == CCW) {
+ // Left
+ return *ot.PointCW(op);
+ } else{
+ //throw new RuntimeException("[Unsupported] Opposing point on constrained edge");
+ // ASSIMP_CHANGE (aramis_acg)
+ throw std::runtime_error("[Unsupported] Opposing point on constrained edge");
+ }
+}
+
+void Sweep::FlipScanEdgeEvent(SweepContext& tcx, Point& ep, Point& eq, Triangle& flip_triangle,
+ Triangle& t, Point& p)
+{
+ Triangle& ot = t.NeighborAcross(p);
+ Point& op = *ot.OppositePoint(t, p);
+
+ if (InScanArea(eq, *flip_triangle.PointCCW(eq), *flip_triangle.PointCW(eq), op)) {
+ // flip with new edge op->eq
+ FlipEdgeEvent(tcx, eq, op, &ot, op);
+ // TODO: Actually I just figured out that it should be possible to
+ // improve this by getting the next ot and op before the the above
+ // flip and continue the flipScanEdgeEvent here
+ // set new ot and op here and loop back to inScanArea test
+ // also need to set a new flip_triangle first
+ // Turns out at first glance that this is somewhat complicated
+ // so it will have to wait.
+ } else{
+ Point& newP = NextFlipPoint(ep, eq, ot, op);
+ FlipScanEdgeEvent(tcx, ep, eq, flip_triangle, ot, newP);
+ }
+}
+
+Sweep::~Sweep() {
+
+ // Clean up memory
+ for(unsigned int i = 0; i < nodes_.size(); i++) {
+ delete nodes_[i];
+ }
+
+}
+
+}
+