From e827825e47572a8ead96a198caa02b77ae9cd407 Mon Sep 17 00:00:00 2001 From: zzzzrrr Date: Wed, 12 Aug 2009 17:04:33 -0400 Subject: [PATCH] code cleanup --- src/org/poly2tri/Poly2Tri.scala | 4 +- src/org/poly2tri/cdt/AFront.scala | 75 +++++++------------------- src/org/poly2tri/cdt/CDT.scala | 25 ++++----- src/org/poly2tri/shapes/Triangle.scala | 2 +- src/org/poly2tri/utils/Util.scala | 2 +- 5 files changed, 33 insertions(+), 75 deletions(-) diff --git a/src/org/poly2tri/Poly2Tri.scala b/src/org/poly2tri/Poly2Tri.scala index 7eba85c..51a3593 100644 --- a/src/org/poly2tri/Poly2Tri.scala +++ b/src/org/poly2tri/Poly2Tri.scala @@ -305,8 +305,8 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") { CDT.clearPoint = 7 loadModel(i18, 20f, Point(600f, 500f), 20) case "data/nazca_heron.dat" => - doCDT = false; drawCDT = false; drawcdtMesh = false - CDT.clearPoint = 7 + //doCDT = false; drawCDT = false; drawcdtMesh = false + CDT.clearPoint = 100 loadModel(nazcaHeron, 4.2f, Point(400f, 300f), 1500) case "data/tank.dat" => //doCDT = false; drawCDT = false; drawcdtMesh = false diff --git a/src/org/poly2tri/cdt/AFront.scala b/src/org/poly2tri/cdt/AFront.scala index 8d82029..171fde6 100644 --- a/src/org/poly2tri/cdt/AFront.scala +++ b/src/org/poly2tri/cdt/AFront.scala @@ -61,7 +61,7 @@ class AFront(iTriangle: Triangle) { // Locate node containing given point def locatePoint(point: Point): Node = { var node = head - while(node != tail) { + while(node != null) { if(point == node.point) return node node = node.next @@ -90,72 +90,33 @@ class AFront(iTriangle: Triangle) { } // Update advancing front with constrained edge triangles - def constrainedEdge(sNode: Node, eNode: Node, T1: ArrayBuffer[Triangle], - T2: ArrayBuffer[Triangle], edge: Segment): Triangle = { + def constrainedEdge(sNode: Node, eNode: Node, T2: ArrayBuffer[Triangle], edge: Segment): Triangle = { var node = sNode - var t1r, t2r = false - - // Scan the advancing front and update Node triangle pointers - // Either T1 OR T2 - while(node != eNode) { - - T2.foreach(t => { - if(t.contains(node.point, node.next.point)) { - node.triangle = t - t2r = true - } - }) - - if(!t2r) - T1.foreach(t => { - if(t.contains(node.point, node.next.point)) { - node.triangle = t - t1r = true - } - }) - - node = node.next - } val point1 = edge.q val point2 = edge.p - // Select edge triangles + var edgeTri: Triangle = null + var marked = false - var edgeTri1: Triangle = null - var i = 0 - while(edgeTri1 == null) { - if(T1(i).contains(point1, point2)) - edgeTri1 = T1(i) - i += 1 - } - - // Mark constrained edge - edgeTri1 markEdge(point1, point2) - - var edgeTri2: Triangle = null - i = 0 - while(edgeTri2 == null) { - if(T2(i).contains(point1, point2)) - edgeTri2 = T2(i) - i += 1 - } + // Scan the advancing front and update Node triangle pointers + while(node != eNode) { - // Mark constrained edge - edgeTri2 markEdge(point1, point2) - - // Update neighbor pointer - edgeTri1.markNeighbor(edgeTri2) - - if(t1r && !t2r) - edgeTri1 - else if(t2r && !t1r) { - edgeTri2 - } else { - throw new Exception("edge insertion error") + 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) + marked = true + } + }) + + node = node.next } + edgeTri } def -=(tuple: Tuple3[Node, Node, Triangle]) { diff --git a/src/org/poly2tri/cdt/CDT.scala b/src/org/poly2tri/cdt/CDT.scala index 74836a8..5058f5e 100644 --- a/src/org/poly2tri/cdt/CDT.scala +++ b/src/org/poly2tri/cdt/CDT.scala @@ -240,13 +240,11 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val sNode = aFront.locate(point1) val eNode = aFront.locate(point2) - val first = aFront.constrainedEdge(sNode, eNode, T1, T2, edge) - // Update neighbors edgeNeighbors(nTriangles, T1) edgeNeighbors(nTriangles, T2) - first + aFront.constrainedEdge(sNode, eNode, T2, edge) } else if(firstTriangle == null) { @@ -257,7 +255,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val point1 = if(ahead) edge.q else edge.p val point2 = if(ahead) edge.p else edge.q - var node = aFront.locate(point1) + var node = aFront.locatePoint(point1) val first = node val points = new ArrayBuffer[Point] @@ -277,8 +275,6 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val T = new ArrayBuffer[Triangle] triangulate(points.toArray, List(edge.q, edge.p), T) - // Update advancing front - // Select edge triangle var edgeTri: Triangle = null var i = 0 @@ -288,6 +284,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian i += 1 } + // Update advancing front aFront link (first, node, edgeTri) // Update neighbors @@ -299,9 +296,9 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian // Return original triangle triangle - } else if(firstTriangle.contains(edge.p, edge.q)) { + } else if(firstTriangle.contains(edge.q, edge.p)) { // Mark constrained edge - firstTriangle markEdge(edge.p, edge.q) + firstTriangle markEdge(edge.q, edge.p) triangle } else { throw new Exception("Triangulation error") @@ -347,10 +344,10 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian } if(!P.isEmpty) { - val ccw = Util.orient2d(a, b, P(i)) > 0 - val pB = if(ccw) P(i) else b - val pC = if(ccw) b else P(i) - val points = Array(a, pB, pC) + //val ccw = Util.orient2d(a, b, P(i)) > 0 + //val pB = if(ccw) P(i) else b + //val pC = if(ccw) b else P(i) + val points = Array(a, P(i), b) T += new Triangle(points) mesh.map += T.last } @@ -447,8 +444,8 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian // Update new neighbors for(n <- neighbors) { if(n != null) { - t2.markNeighbor(n) - t1.markNeighbor(n) + t1.markNeighbor(n) + t2.markNeighbor(n) } } t2.markNeighbor(t1) diff --git a/src/org/poly2tri/shapes/Triangle.scala b/src/org/poly2tri/shapes/Triangle.scala index 60ebe2d..da39935 100644 --- a/src/org/poly2tri/shapes/Triangle.scala +++ b/src/org/poly2tri/shapes/Triangle.scala @@ -132,7 +132,7 @@ class Triangle(val points: Array[Point]) { else if(edge.q == points(1)) search(points(0), points(2), edge, neighbors(0)) else if(edge.q == points(2)) - search(points(1), points(0), edge, neighbors(1)) + search(points(0), points(1), edge, neighbors(1)) else throw new Exception("Point not found") diff --git a/src/org/poly2tri/utils/Util.scala b/src/org/poly2tri/utils/Util.scala index 16e77ba..5c5e2fc 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.0001f + val c = pc * 0.1e-5f return orient2d(pa, pb, c) }