From a6b338d88df43fc8316b9d5c765e9d397c66b04f Mon Sep 17 00:00:00 2001 From: zzzzrrr Date: Mon, 10 Aug 2009 12:35:03 -0400 Subject: [PATCH] changed triangle initialization --- src/org/poly2tri/cdt/CDT.scala | 20 ++++++++------------ src/org/poly2tri/shapes/Triangle.scala | 17 +++++------------ 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/org/poly2tri/cdt/CDT.scala b/src/org/poly2tri/cdt/CDT.scala index 614333c..f1b2206 100644 --- a/src/org/poly2tri/cdt/CDT.scala +++ b/src/org/poly2tri/cdt/CDT.scala @@ -72,9 +72,8 @@ object CDT { val segments = initSegments(points) val sortedPoints = pointSort(points) - val noNeighbors = new Array[Triangle](3) val tPoints = Array(sortedPoints(0), p1, p2) - val iTriangle = new Triangle(tPoints, noNeighbors) + val iTriangle = new Triangle(tPoints) new CDT(sortedPoints, segments, iTriangle) } @@ -158,8 +157,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian // Projected point hits advancing front; create new triangle val pts = Array(point, node.point, node.next.point) - val neighbors = Array(node.triangle, null, null) - val triangle = new Triangle(pts, neighbors) + val triangle = new Triangle(pts) mesh.map += triangle // Legalize @@ -367,8 +365,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian if(!P.isEmpty) { val points = Array(a, P.first, b) - val neighbors = new Array[Triangle](3) - T += new Triangle(points, neighbors) + T += new Triangle(points) mesh.map += T.last } } @@ -404,12 +401,9 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val angle = Math.abs(Math.atan2(a cross b, a dot b)) if(angle <= PI_2) { val points = Array(node.prev.point, node.point, node.next.point) - val neighbors = Array(node.triangle, null, node.prev.triangle) - val triangle = new Triangle(points, neighbors) + val triangle = new Triangle(points) // Update neighbor pointers - //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) @@ -456,10 +450,12 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian // Flip edge and rotate everything clockwise t1.legalize(oPoint) t2.legalize(oPoint, point) - - // Update neighbor pointers + + // Copy old neighbors val neighbors = List(t2.neighbors(0), t2.neighbors(1), t2.neighbors(2)) + // Clear old neighbors t2.clearNeighbors + // Update new neighbors for(n <- neighbors) { if(n != null) { t2.markNeighbor(n) diff --git a/src/org/poly2tri/shapes/Triangle.scala b/src/org/poly2tri/shapes/Triangle.scala index 534c705..998aec5 100644 --- a/src/org/poly2tri/shapes/Triangle.scala +++ b/src/org/poly2tri/shapes/Triangle.scala @@ -38,8 +38,10 @@ import utils.Util // Triangle-based data structures are know to have better performance than quad-edge structures // See: J. Shewchuk, "Triangle: Engineering a 2D Quality Mesh Generator and Delaunay Triangulator" // "Triangulations in CGAL" -class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { +class Triangle(val points: Array[Point]) { + // Neighbor pointers + var neighbors = new Array[Triangle](3) // Flags to determine if an edge is the final Delauney edge val edges = Array(false, false, false) @@ -70,9 +72,6 @@ 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!") } } @@ -92,8 +91,7 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { } def clearNeighbors { - for(i <- 0 until 3) - neighbors(i) = null + neighbors = new Array[Triangle](3) } def oppositePoint(t: Triangle): Point = { @@ -104,10 +102,7 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { else if((points(2) == t.points(1) && points(1) == t.points(2)) || (points(1) == t.points(1) && points(2) == t.points(2))) points(0) else { - println("**********************") - t.printDebug - printDebug - throw new Exception("point location error") + throw new Exception("Point location error, please report") } } @@ -248,7 +243,6 @@ 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 @@ -269,7 +263,6 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { points(2) = points(1) points(1) = nPoint } - Util.collinear(points(0), points(2), points(1)) } // Make legalized triangle will not be collinear