From 5fa8c4501e8315ef5c25acccc113fcf7fef65c4b Mon Sep 17 00:00:00 2001 From: zzzzrrr Date: Tue, 11 Aug 2009 17:25:57 -0400 Subject: [PATCH] bug fixes --- src/org/poly2tri/cdt/CDT.scala | 62 ++++++++++++++++---------- src/org/poly2tri/shapes/Triangle.scala | 10 +++-- src/org/poly2tri/utils/Util.scala | 2 +- 3 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/org/poly2tri/cdt/CDT.scala b/src/org/poly2tri/cdt/CDT.scala index c3ef4f2..d1d52b8 100644 --- a/src/org/poly2tri/cdt/CDT.scala +++ b/src/org/poly2tri/cdt/CDT.scala @@ -139,8 +139,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian // Process Point event var triangle = pointEvent(point) // Process edge events - if(triangle != null) - point.edges.foreach(e => triangle = edgeEvent(e, triangle)) + point.edges.foreach(e => triangle = edgeEvent(e, triangle)) if(i == CDT.clearPoint) {cleanTri = triangle; mesh.debug += cleanTri} } catch { @@ -256,9 +255,29 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian edgeNeighbors(nTriangles, T1) edgeNeighbors(nTriangles, T2) - // Mark constrained edges - T1.first.mark(point1, point2) - T2.first.mark(point1, point2) + // Select edge triangle + var edgeTri1: Triangle = null + var i = 0 + while(edgeTri1 == null) { + if(T1(i).contains(point1, point2)) + edgeTri1 = T1(i) + i += 1 + } + + // Select edge triangle + var edgeTri2: Triangle = null + i = 0 + while(edgeTri2 == null) { + if(T2(i).contains(point1, point2)) + edgeTri2 = T2(i) + i += 1 + } + + edgeTri1.markNeighbor(edgeTri2) + + // Mark constrained edge + edgeTri1 mark(point1, point2) + edgeTri2 mark(point1, point2) // Double check returning T2.first vs T1.first first @@ -298,29 +317,30 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian // Select edge triangle var edgeTri: Triangle = null - T.foreach(t => - if(t.contains(first.point, node.point)) - edgeTri = t - ) - assert(edgeTri != null) + var i = 0 + while(edgeTri == null) { + if(T(i).contains(first.point, node.point)) + edgeTri = T(i) + i += 1 + } aFront link (first, node, edgeTri) - // Mark constrained edge - edgeTri mark(edge.p, edge.q) - // Update neighbors edgeNeighbors(nTriangles, T) + // Mark constrained edge + edgeTri mark(edge.p, edge.q) + // Return original triangle triangle - } else { - + } else if(firstTriangle.contains(edge.p, edge.q)) { // Mark constrained edge - if(firstTriangle != null) - firstTriangle mark(edge.p, edge.q) + firstTriangle mark(edge.p, edge.q) triangle + } else { + throw new Exception("Triangulation error") } } @@ -447,16 +467,12 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val t2 = node.triangle val point = t1.points(0) - val oPoint = t2 oppositePoint t1 - // Try to avoid creating degenerate triangles - val collinear = t1.collinear(oPoint) || t2.collinear(oPoint, point) - - if(illegal(t1.points(1), oPoint, t1.points(2), t1.points(0)) && !collinear) { + if(illegal(t1.points(1), oPoint, t1.points(2), t1.points(0)) && !t2.finalized) { // Flip edge and rotate everything clockwise - t1.legalize(oPoint) + t1.legalize(oPoint) t2.legalize(oPoint, point) // Copy old neighbors diff --git a/src/org/poly2tri/shapes/Triangle.scala b/src/org/poly2tri/shapes/Triangle.scala index 780eff4..122900a 100644 --- a/src/org/poly2tri/shapes/Triangle.scala +++ b/src/org/poly2tri/shapes/Triangle.scala @@ -48,9 +48,11 @@ class Triangle(val points: Array[Point]) { // Finalization flag var clean = false + var finalized = false + // Update neighbor pointers private def markNeighbor(p1: Point, p2: Point, t: Triangle) { - assert(t != this, "self-pointer error") + if((p1 == points(2) && p2 == points(1)) || (p1 == points(1) && p2 == points(2))) neighbors(0) = t else if((p1 == points(0) && p2 == points(2)) || (p1 == points(2) && p2 == points(0))) @@ -64,7 +66,7 @@ class Triangle(val points: Array[Point]) { /* Exhaustive search to update neighbor pointers */ def markNeighbor(t: Triangle) { - assert(t != this, "self-pointer error") + if (t.contains(points(1), points(2))) { neighbors(0) = t t.markNeighbor(points(1), points(2), this) @@ -93,7 +95,6 @@ class Triangle(val points: Array[Point]) { t.printDebug printDebug println(area + " | " + t.area) - throw new Exception("Point location error, please report") } @@ -308,10 +309,13 @@ class Triangle(val points: Array[Point]) { // Mark edge as constrained private def markEdge(p: Point, q: Point) { if((q == points(0) && p == points(1)) || (q == points(1) && p == points(0))) { + finalized = true edges(2) = true } else if ((q == points(0) && p == points(2)) || (q == points(2) && p == points(0))) { + finalized = true edges(1) = true } else if ((q == points(1) && p == points(2)) || (q == points(2) && p == points(1))){ + finalized = true edges(0) = true } } diff --git a/src/org/poly2tri/utils/Util.scala b/src/org/poly2tri/utils/Util.scala index ddb065e..06f691b 100644 --- a/src/org/poly2tri/utils/Util.scala +++ b/src/org/poly2tri/utils/Util.scala @@ -125,7 +125,7 @@ object Util { return det } else { // Cheat a little bit.... we have a degenerate triangle - val c = pc * 1.00001f + val c = pc * 1.0001f return orient2d(pa, pb, c) }