From 8428c47904bf84a090bfaf9934124ba4fdb82d57 Mon Sep 17 00:00:00 2001 From: zzzzrrr Date: Sun, 9 Aug 2009 01:53:58 -0400 Subject: [PATCH] fixed AFront issue --- src/org/poly2tri/Poly2Tri.scala | 2 +- src/org/poly2tri/cdt/AFront.scala | 11 ++++ src/org/poly2tri/cdt/CDT.scala | 80 ++++++++++++-------------- src/org/poly2tri/shapes/Triangle.scala | 2 +- 4 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/org/poly2tri/Poly2Tri.scala b/src/org/poly2tri/Poly2Tri.scala index 17d0ba0..cdfea10 100644 --- a/src/org/poly2tri/Poly2Tri.scala +++ b/src/org/poly2tri/Poly2Tri.scala @@ -85,7 +85,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") { val strange = "data/strange.dat" val i18 = "data/i.18" - var currentModel = i18 + var currentModel = strange var mouseButton = 0 var mousePressed = false diff --git a/src/org/poly2tri/cdt/AFront.scala b/src/org/poly2tri/cdt/AFront.scala index 2eb6c0c..318bb37 100644 --- a/src/org/poly2tri/cdt/AFront.scala +++ b/src/org/poly2tri/cdt/AFront.scala @@ -56,6 +56,17 @@ class AFront(iTriangle: Triangle) { null } + // Locate node containing given point + def locatePoint(point: Point): Node = { + var node = head + while(node != tail) { + if(point == node.point) + return node + node = node.next + } + null + } + def insert(tuple: Tuple3[Point, Triangle, Node]) = { val (point, triangle, nNode) = tuple val node = new Node(point, triangle) diff --git a/src/org/poly2tri/cdt/CDT.scala b/src/org/poly2tri/cdt/CDT.scala index ea601c6..b2735ec 100644 --- a/src/org/poly2tri/cdt/CDT.scala +++ b/src/org/poly2tri/cdt/CDT.scala @@ -132,16 +132,11 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian for(i <- 1 until points.size) { val point = points(i) // Process Point event - var triangle: Triangle = null - try { - triangle = pointEvent(point) - } catch { - case e: Exception => - println("Offending triangle = " + i) - } + var triangle = pointEvent(point) // Process edge events - point.edges.foreach(e => edgeEvent(e, triangle)) + point.edges.foreach(e => triangle = edgeEvent(e, triangle)) if(i == 7) {cleanTri = triangle; mesh.debug += cleanTri} + // strange = 13; star & i18 = 7 } } @@ -226,16 +221,14 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian } // EdgeEvent - private def edgeEvent(edge: Segment, triangle: Triangle) { + private def edgeEvent(edge: Segment, triangle: Triangle): Triangle = { // STEP 1: Locate the first intersected triangle val firstTriangle = triangle.locateFirst(edge) - val contains = if(firstTriangle != null) firstTriangle.contains(edge) else false - // STEP 2: Remove intersected triangles - if(firstTriangle != null && !contains) { - + if(firstTriangle != null && !firstTriangle.contains(edge)) { + // Collect intersected triangles val tList = new ArrayBuffer[Triangle] tList += firstTriangle @@ -252,9 +245,8 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian // Remove old triangles; collect neighbor triangles tList.foreach(t => { - t.neighbors.foreach(n => if(n != null) nTriangles += n) + t.neighbors.foreach(n => if(n != null && !tList.contains(n)) nTriangles += n) mesh.map -= t - //mesh.debug += t }) val lPoints = new ArrayBuffer[Point] @@ -268,7 +260,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian tList.foreach(t => { t.points.foreach(p => { if(p != edge.q && p != edge.p) { - if(t.orient(point1, point2, p) >= 0 ) { + if(t.orient(point1, point2, p) > 0 ) { // Keep duplicate points out if(!lPoints.contains(p)) { lPoints += p @@ -290,56 +282,50 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian // Update advancing front - val qNode = aFront.locate(edge.q) - val pNode = aFront.locate(edge.p) - println(tList.size) + val qNode = aFront.locatePoint(edge.q) + val pNode = aFront.locatePoint(edge.p) for(t <- tList) { if(qNode.triangle == t) { - println("node") val p1 = qNode.point val p2 = qNode.next.point T1.foreach(tri => if(tri.contains(p1, p2)) qNode.triangle = tri) T2.foreach(tri => if(tri.contains(p1, p2)) qNode.triangle = tri) } else if(qNode.prev.triangle == t) { - println("prev node") val p1 = qNode.prev.point val p2 = qNode.point T1.foreach(tri => if(tri.contains(p1, p2)) qNode.prev.triangle = tri) T2.foreach(tri => if(tri.contains(p1, p2)) qNode.prev.triangle = tri) - } else if(qNode.next.triangle == t) { - println("next node") - } else + } - if(pNode.triangle == t) { - println("Pnode") - val p1 = pNode.point - val p2 = pNode.next.point - T1.foreach(tri => if(tri.contains(p1, p2)) pNode.triangle = tri) - T2.foreach(tri => if(tri.contains(p1, p2)) pNode.triangle = tri) - } else if(pNode.prev.triangle == t) { - println("prev Pnode") - val p1 = pNode.point - val p2 = pNode.prev.point - T1.foreach(tri => if(tri.contains(p1, p2)) pNode.prev.triangle = tri) - T2.foreach(tri => if(tri.contains(p1, p2)) pNode.prev.triangle = tri) - } else if(pNode.next.triangle == t) { - println("next Pnode") + if(pNode != null) { + if(pNode.triangle == t) { + val p1 = pNode.point + val p2 = pNode.next.point + T1.foreach(tri => if(tri.contains(p1, p2)) pNode.triangle = tri) + T2.foreach(tri => if(tri.contains(p1, p2)) pNode.triangle = tri) + } else if(pNode.prev.triangle == t) { + val p1 = pNode.point + val p2 = pNode.prev.point + T1.foreach(tri => if(tri.contains(p1, p2)) pNode.prev.triangle = tri) + T2.foreach(tri => if(tri.contains(p1, p2)) pNode.prev.triangle = tri) + } } + } // Update neighbors - //updateNeighbors(nTriangles, T1) + updateNeighbors(nTriangles, T1) updateNeighbors(nTriangles, T2) - //nTriangles.foreach(n => n.printDebug) // Mark constrained edges T1.first.mark(point1, point2) T2.first.mark(point1, point2) - - //TODO update neighbor pointers + // Double check returning T2.first vs T1.first + T2.first + } else if(firstTriangle == null) { // No triangles are intersected by the edge; edge must lie outside the mesh @@ -354,8 +340,9 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val nTriangles = new ArrayBuffer[Triangle] var node = aFront.locate(point1) + val first = node - nTriangles += first.triangle + nTriangles += node.triangle node = node.next @@ -372,6 +359,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian triangulate(points.toArray, endPoints, T) // Update advancing front + aFront -= (first, node.prev, T.first) // Update neighbors @@ -379,11 +367,15 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian // Mark constrained edge T.first mark(edge.p, edge.q) - + + triangle + } else { // Mark constrained edge firstTriangle mark(edge.p, edge.q) + triangle } + } // Update neigbor pointers for edge event diff --git a/src/org/poly2tri/shapes/Triangle.scala b/src/org/poly2tri/shapes/Triangle.scala index 2ccac29..0ff8a10 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) { + 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)))