/// <summary> /// This implementation will use simple node traversal algorithm to find a point on the front /// </summary> public AdvancingFrontNode LocatePoint(TriangulationPoint point) { double px = point.X; AdvancingFrontNode node = _search; double nx = node.Point.X; // ReSharper disable CompareOfFloatsByEqualityOperator if (px == nx) // ReSharper restore CompareOfFloatsByEqualityOperator { if (!point.Equals(node.Point)) { // We might have two nodes with same x value for a short time if (point.Equals(node.Prev.Point)) { node = node.Prev; } else if (point.Equals(node.Next.Point)) { node = node.Next; } else { throw new Exception("Failed to find Node for given afront point"); } } } else if (px < nx) { while ((node = node.Prev) != null) { if (point.Equals(node.Point)) { break; } } } else { while ((node = node.Next) != null) { if (point.Equals(node.Point)) { break; } } } _search = node; return(node); }
private static void FlipEdgeEvent(DTSweepContext tcx, TriangulationPoint ep, TriangulationPoint eq, DelaunayTriangle t, TriangulationPoint p) { DelaunayTriangle ot = t.NeighborAcrossFrom(p); TriangulationPoint op = ot.OppositePoint(t, p); if (ot == null) { // If we want to integrate the fillEdgeEvent do it here // With current implementation we should never get here throw new InvalidOperationException("[BUG:FIXME] FLIP failed due to missing triangle"); } if (tcx.IsDebugEnabled) { tcx.DebugContext.PrimaryTriangle = t; tcx.DebugContext.SecondaryTriangle = ot; } // TODO: remove bool inScanArea = TriangulationUtil.InScanArea(p, t.PointCCWFrom(p), t.PointCWFrom(p), op); if (inScanArea) { // Lets rotate shared edge one vertex CW RotateTrianglePair(t, p, ot, op); tcx.MapTriangleToNodes(t); tcx.MapTriangleToNodes(ot); if (p.Equals(eq) && op.Equals(ep)) { if (eq.Equals(tcx.EdgeEvent.ConstrainedEdge.Q) && ep.Equals(tcx.EdgeEvent.ConstrainedEdge.P)) { if (tcx.IsDebugEnabled) { Console.WriteLine("[FLIP] - constrained edge done"); // TODO: remove } t.MarkConstrainedEdge(ep, eq); ot.MarkConstrainedEdge(ep, eq); Legalize(tcx, t); Legalize(tcx, ot); } else { if (tcx.IsDebugEnabled) { Console.WriteLine("[FLIP] - subedge done"); // TODO: remove } // XXX: I think one of the triangles should be legalized here? } } else { if (tcx.IsDebugEnabled) { Console.WriteLine("[FLIP] - flipping and continuing with triangle still crossing edge"); // TODO: remove } Orientation o = TriangulationUtil.Orient2d(eq, op, ep); t = NextFlipTriangle(tcx, o, t, ot, p, op); FlipEdgeEvent(tcx, ep, eq, t, p); } } else { TriangulationPoint newP; if (NextFlipPoint(ep, eq, ot, op, out newP)) { FlipScanEdgeEvent(tcx, ep, eq, t, ot, newP); EdgeEvent(tcx, ep, eq, t, p); } } }
/// <summary> /// If this is a Delaunay Triangulation of a pointset we need to fill so the triangle mesh gets a ConvexHull /// </summary> private static void FinalizationConvexHull(DTSweepContext tcx) { DelaunayTriangle t1, t2; AdvancingFrontNode n1 = tcx.Front.Head.Next; AdvancingFrontNode n2 = n1.Next; TurnAdvancingFrontConvex(tcx, n1, n2); // Lets remove triangles connected to the two "algorithm" points // XXX: When the first three nodes are points in a triangle we need to do a flip before // removing triangles or we will lose a valid triangle. // Same for last three nodes! // !!! If I implement ConvexHull for lower right and left boundary this fix should not be // needed and the removed triangles will be added again by default n1 = tcx.Front.Tail.Prev; if (n1.Triangle.Contains(n1.Next.Point) && n1.Triangle.Contains(n1.Prev.Point)) { t1 = n1.Triangle.NeighborAcrossFrom(n1.Point); RotateTrianglePair(n1.Triangle, n1.Point, t1, t1.OppositePoint(n1.Triangle, n1.Point)); tcx.MapTriangleToNodes(n1.Triangle); tcx.MapTriangleToNodes(t1); } n1 = tcx.Front.Head.Next; if (n1.Triangle.Contains(n1.Prev.Point) && n1.Triangle.Contains(n1.Next.Point)) { t1 = n1.Triangle.NeighborAcrossFrom(n1.Point); RotateTrianglePair(n1.Triangle, n1.Point, t1, t1.OppositePoint(n1.Triangle, n1.Point)); tcx.MapTriangleToNodes(n1.Triangle); tcx.MapTriangleToNodes(t1); } // Lower right boundary TriangulationPoint first = tcx.Front.Head.Point; n2 = tcx.Front.Tail.Prev; t1 = n2.Triangle; TriangulationPoint p1 = n2.Point; n2.Triangle = null; do { tcx.RemoveFromList(t1); p1 = t1.PointCCWFrom(p1); if (p1.Equals(first)) { break; } t2 = t1.NeighborCCWFrom(p1); t1.Clear(); t1 = t2; } while (true); // Lower left boundary first = tcx.Front.Head.Next.Point; p1 = t1.PointCWFrom(tcx.Front.Head.Point); t2 = t1.NeighborCWFrom(tcx.Front.Head.Point); t1.Clear(); t1 = t2; while (p1.Equals(first)) { tcx.RemoveFromList(t1); p1 = t1.PointCCWFrom(p1); t2 = t1.NeighborCCWFrom(p1); t1.Clear(); t1 = t2; } // Remove current head and tail node now that we have removed all triangles attached // to them. Then set new head and tail node points tcx.Front.Head = tcx.Front.Head.Next; tcx.Front.Head.Prev = null; tcx.Front.Tail = tcx.Front.Tail.Prev; tcx.Front.Tail.Next = null; }