From 41e40241a15ea3affefc7d6906b5026806e259de Mon Sep 17 00:00:00 2001 From: zzzzrrr Date: Fri, 14 Aug 2009 16:16:54 -0400 Subject: [PATCH] fixed orientation bug --- src/org/poly2tri/Poly2Tri.scala | 2 +- src/org/poly2tri/cdt/AFront.scala | 27 +++++++++++++++++++++------ src/org/poly2tri/cdt/CDT.scala | 27 +++++++++++---------------- 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/org/poly2tri/Poly2Tri.scala b/src/org/poly2tri/Poly2Tri.scala index 51a3593..1ec77c5 100644 --- a/src/org/poly2tri/Poly2Tri.scala +++ b/src/org/poly2tri/Poly2Tri.scala @@ -87,7 +87,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") { val i18 = "data/i.18" val tank = "data/tank.dat" - var currentModel = strange + var currentModel = star var doCDT = true var mouseButton = 0 diff --git a/src/org/poly2tri/cdt/AFront.scala b/src/org/poly2tri/cdt/AFront.scala index 8771bd0..d2e7f6d 100644 --- a/src/org/poly2tri/cdt/AFront.scala +++ b/src/org/poly2tri/cdt/AFront.scala @@ -90,25 +90,39 @@ class AFront(iTriangle: Triangle) { } // Update advancing front with constrained edge triangles - def constrainedEdge(sNode: Node, eNode: Node, T2: ArrayBuffer[Triangle], edge: Segment): Triangle = { + def constrainedEdge(sNode: Node, eNode: Node, T1: ArrayBuffer[Triangle], + T2: ArrayBuffer[Triangle], edge: Segment) { var node = sNode val point1 = edge.q val point2 = edge.p - var edgeTri: Triangle = null + var edgeTri1: Triangle = null + var edgeTri2: Triangle = null var marked = false // Scan the advancing front and update Node triangle pointers - while(node != eNode) { + while(node != null && node != eNode.next) { T2.foreach(t => { if(t.contains(node.point, node.next.point)) node.triangle = t if(!marked && t.contains(point1, point2)) { - edgeTri = t - edgeTri markEdge(point1, point2) + edgeTri2 = t + edgeTri2 markEdge(point1, point2) + marked = true + } + }) + + marked = false + + T1.foreach(t => { + if(t.contains(node.point, node.next.point)) + node.triangle = t + if(!marked && t.contains(point1, point2)) { + edgeTri1 = t + edgeTri1 markEdge(point1, point2) marked = true } }) @@ -116,7 +130,8 @@ class AFront(iTriangle: Triangle) { node = node.next } - edgeTri + edgeTri1.markNeighbor(edgeTri2) + } def -=(tuple: Tuple3[Node, Node, Triangle]) { diff --git a/src/org/poly2tri/cdt/CDT.scala b/src/org/poly2tri/cdt/CDT.scala index 165fe4a..f614116 100644 --- a/src/org/poly2tri/cdt/CDT.scala +++ b/src/org/poly2tri/cdt/CDT.scala @@ -130,10 +130,10 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian try { val point = points(i) // Process Point event - var triangle = pointEvent(point) + val node = pointEvent(point) + if(i == CDT.clearPoint) {cleanTri = node.triangle; mesh.debug += cleanTri} // Process edge events - point.edges.foreach(e => triangle = edgeEvent(e, triangle)) - if(i == CDT.clearPoint) {cleanTri = triangle; mesh.debug += cleanTri} + point.edges.foreach(e => edgeEvent(e, node)) } catch { case e: Exception => @@ -153,7 +153,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian } // Point event - private def pointEvent(point: Point): Triangle = { + private def pointEvent(point: Point): Node = { val node = aFront.locate(point) @@ -167,15 +167,15 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val newNode = legalization(triangle, node) // Fill in adjacent triangles if required scanAFront(newNode) - newNode.triangle + newNode } // EdgeEvent - private def edgeEvent(edge: Segment, triangle: Triangle): Triangle = { - + private def edgeEvent(edge: Segment, node: Node) { + // Locate the first intersected triangle - val firstTriangle = triangle.locateFirst(edge) + val firstTriangle = node.triangle.locateFirst(edge) // Remove intersected triangles if(firstTriangle != null && !firstTriangle.contains(edge)) { @@ -237,13 +237,11 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val sNode = aFront.locate(point1) val eNode = aFront.locate(point2) - val eTri = aFront.constrainedEdge(sNode, eNode, T2, edge) + aFront.constrainedEdge(sNode, eNode, T1, T2, edge) // Mark edge triangle neighbors //T1.foreach(t => t.markNeighbor(eTri)) - eTri - } else if(firstTriangle == null) { // No triangles are intersected by the edge; edge must lie outside the mesh @@ -293,13 +291,9 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian // Mark constrained edge edgeTri markEdge(point1, point2) - // Return original triangle - triangle - } else if(firstTriangle.contains(edge.q, edge.p)) { // Mark constrained edge firstTriangle markEdge(edge.q, edge.p) - triangle } else { throw new Exception("Triangulation error") //null @@ -344,7 +338,8 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian } if(!P.isEmpty) { - val points = Array(a, P(i), b) + val ccw = Util.orient2d(a, P(i), b) > 0 + val points = if(ccw) Array(a, P(i), b) else Array(a, b, P(i)) T += new Triangle(points) T.last.finalized = true mesh.map += T.last