From 84598700c479800b02c7815e9b54728fb779b65b Mon Sep 17 00:00:00 2001 From: zzzzrrr Date: Mon, 10 Aug 2009 11:49:44 -0400 Subject: [PATCH] fixed legalization bug --- src/org/poly2tri/cdt/AFront.scala | 3 ++ src/org/poly2tri/cdt/CDT.scala | 13 +++++--- src/org/poly2tri/shapes/Triangle.scala | 44 +++++++++++++++----------- src/org/poly2tri/utils/Util.scala | 4 +-- 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/org/poly2tri/cdt/AFront.scala b/src/org/poly2tri/cdt/AFront.scala index 61ecf51..4c0eaee 100644 --- a/src/org/poly2tri/cdt/AFront.scala +++ b/src/org/poly2tri/cdt/AFront.scala @@ -87,6 +87,9 @@ class AFront(iTriangle: Triangle) { node } + def constrainedEdge() { + + } def -=(tuple: Tuple3[Node, Node, Triangle]) { val (node, kNode, triangle) = tuple diff --git a/src/org/poly2tri/cdt/CDT.scala b/src/org/poly2tri/cdt/CDT.scala index 1b04297..614333c 100644 --- a/src/org/poly2tri/cdt/CDT.scala +++ b/src/org/poly2tri/cdt/CDT.scala @@ -137,7 +137,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian // Process Point event var triangle = pointEvent(point) // Process edge events - 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} } } @@ -407,8 +407,10 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val neighbors = Array(node.triangle, null, node.prev.triangle) val triangle = new Triangle(points, neighbors) // Update neighbor pointers - node.prev.triangle.markNeighbor(triangle.points(0), triangle.points(1), triangle, mesh.debug) - node.triangle.markNeighbor(triangle.points(1), triangle.points(2), triangle, mesh.debug) + //node.prev.triangle.markNeighbor(triangle.points(0), triangle.points(1), triangle) + node.prev.triangle.markNeighbor(triangle) + //node.triangle.markNeighbor(triangle.points(1), triangle.points(2), triangle) + node.triangle.markNeighbor(triangle) mesh.map += triangle aFront -= (node.prev, node, triangle) } @@ -451,12 +453,13 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian if(illegal(t1.points(1), oPoint, t1.points(2), t1.points(0))) { - // Flip edges and rotate everything clockwise + // Flip edge and rotate everything clockwise t1.legalize(oPoint) t2.legalize(oPoint, point) // Update neighbor pointers val neighbors = List(t2.neighbors(0), t2.neighbors(1), t2.neighbors(2)) + t2.clearNeighbors for(n <- neighbors) { if(n != null) { t2.markNeighbor(n) @@ -464,7 +467,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian } } t2.markNeighbor(t1) - + // Update advancing front aFront.insertLegalized(point, t1, node) diff --git a/src/org/poly2tri/shapes/Triangle.scala b/src/org/poly2tri/shapes/Triangle.scala index f81f94c..534c705 100644 --- a/src/org/poly2tri/shapes/Triangle.scala +++ b/src/org/poly2tri/shapes/Triangle.scala @@ -62,7 +62,7 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { } // Update neighbor pointers - def markNeighbor(p1: Point, p2: Point, triangle: Triangle) { + private def markNeighbor(p1: Point, p2: Point, triangle: Triangle) { if((p1 == points(2) && p2 == points(1)) || (p1 == points(1) && p2 == points(2))) neighbors(0) = triangle else if((p1 == points(0) && p2 == points(2)) || (p1 == points(2) && p2 == points(0))) @@ -70,6 +70,9 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { else if((p1 == points(0) && p2 == points(1)) || (p1 == points(1) && p2 == points(0))) neighbors(2) = triangle else { + println("**********************") + triangle.printDebug + printDebug throw new Exception("Neighbor pointer error, please report!") } } @@ -88,6 +91,11 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { } } + def clearNeighbors { + for(i <- 0 until 3) + neighbors(i) = null + } + def oppositePoint(t: Triangle): Point = { if(points(0) == t.points(1)) points(1) @@ -101,6 +109,7 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { printDebug throw new Exception("point location error") } + } def contains(p: Point): Boolean = (p == points(0) || p == points(1) || p == points(2)) @@ -110,11 +119,14 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { // Fast point in triangle test def pointIn(point: Point): Boolean = { + val ij = points(1) - points(0) + val jk = points(2) - points(1) val pab = (point - points(0)).cross(ij) val pbc = (point - points(1)).cross(jk) var sameSign = Math.signum(pab) == Math.signum(pbc) if (!sameSign) return false + val ki = points(0) - points(2) val pca = (point - points(2)).cross(ki) sameSign = Math.signum(pab) == Math.signum(pca) if (!sameSign) return false @@ -124,20 +136,27 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { // Locate first triangle crossed by constrained edge def locateFirst(edge: Segment): Triangle = { + val p = edge.p val q = edge.q val e = p - q if(q == points(0)) { + val ik = points(2) - points(0) + val ij = points(1) - points(0) val sameSign = Math.signum(ik cross e) == Math.signum(ij cross e) if(!sameSign) return this if(neighbors(2) == null) return null return neighbors(2).locateFirst(edge) } else if(q == points(1)) { + val jk = points(2) - points(1) + val ji = points(0) - points(1) val sameSign = Math.signum(jk cross e) == Math.signum(ji cross e) if(!sameSign) return this if(neighbors(0) == null) return null return neighbors(0).locateFirst(edge) } else if(q == points(2)) { + val kj = points(1) - points(2) + val ki = points(0) - points(2) val sameSign = Math.signum(kj cross e) == Math.signum(ki cross e) if(!sameSign) return this if(neighbors(1) == null) return null @@ -229,10 +248,10 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { // Legalized triangle by rotating clockwise around point(0) def legalize(oPoint: Point) { + Util.collinear(points(0), points(2), oPoint) points(1) = points(0) points(0) = points(2) points(2) = oPoint - updateEdges } // Legalize triagnle by rotating clockwise around oPoint @@ -250,7 +269,7 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { points(2) = points(1) points(1) = nPoint } - updateEdges + Util.collinear(points(0), points(2), points(1)) } // Make legalized triangle will not be collinear @@ -288,25 +307,11 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { def printDebug = println(points(0) + "," + points(1) + "," + points(2)) - private var ik, ij , jk, ji, kj, ki: Point = null - updateEdges - - // Update the edges that consitite this triangle - // May change during legalization - private def updateEdges { - ik = points(2) - points(0) - ij = points(1) - points(0) - jk = points(2) - points(1) - ji = points(0) - points(1) - kj = points(1) - points(2) - ki = points(0) - points(2) - } - // Initial mark edges sweep def mark(p: Point, q: Point) { if(contains(p) && contains(q)) { markEdge(p, q) - markNeighbors(p, q) + markNeighborEdge(p, q) } } @@ -322,7 +327,8 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { } // Mark neighbor's edge - private def markNeighbors(p: Point, q: Point) = neighbors.foreach(n => if(n != null) n.markEdge(p, q)) + private def markNeighborEdge(p: Point, q: Point) = + neighbors.foreach(n => if(n != null) n.markEdge(p, q)) // Mark edge as constrained private def markEdge(p: Point, q: Point) { diff --git a/src/org/poly2tri/utils/Util.scala b/src/org/poly2tri/utils/Util.scala index d2dfbdd..f49b3b9 100644 --- a/src/org/poly2tri/utils/Util.scala +++ b/src/org/poly2tri/utils/Util.scala @@ -41,8 +41,8 @@ object Util { def collinear(p1: Point, p2: Point, p3: Point): Boolean = { val d = (p2 - p1) cross (p1 - p3) - - if(Math.abs(d) <= COLLINEAR_SLOP) + + if(d <= COLLINEAR_SLOP) true else false