diff --git a/src/org/poly2tri/cdt/CDT.scala b/src/org/poly2tri/cdt/CDT.scala index f0d6ad4..5a3d638 100644 --- a/src/org/poly2tri/cdt/CDT.scala +++ b/src/org/poly2tri/cdt/CDT.scala @@ -157,7 +157,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian mesh.map += lTriangle mesh.map += rTriangle - // No need for legalization here + // Skip legalization // Update neighbors node.triangle.updateNeighbors(rTriangle.points(1), rTriangle.points(2), rTriangle, mesh.debug) @@ -268,7 +268,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val dEdge = new Segment(point1, point2) T1.first markEdge dEdge T2.first markEdge dEdge - + } else if(firstTriangle == null) { // No triangles are intersected by the edge; edge must lie outside the mesh @@ -412,10 +412,24 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian val oPoint = t2 oppositePoint t1 if(illegal(t1.points(1), oPoint, t1.points(2), t1.points(0))) { - // Flip edges and rotate everything clockwise + + // Flip edges and rotate everything clockwise val point = t1.points(0) t1.legalize(oPoint) t2.legalize(oPoint, point) + + // Update neighbor pointers + val ccwNeighbor = t2.neighborCCW(oPoint) + + if(ccwNeighbor != null) { + ccwNeighbor.updateNeighbors(t1.points(2), t1.points(0), t1, mesh.debug) + t1.neighbors(1) = ccwNeighbor + } + + t2.rotateNeighborsCW(oPoint, t1) + t1.neighbors(0) = t2 + t1.neighbors(2) = null + false } else { true diff --git a/src/org/poly2tri/shapes/Triangle.scala b/src/org/poly2tri/shapes/Triangle.scala index a23dfb7..7b2eaed 100644 --- a/src/org/poly2tri/shapes/Triangle.scala +++ b/src/org/poly2tri/shapes/Triangle.scala @@ -88,7 +88,6 @@ 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 - if(contains(p)) return this val q = edge.q val e = p - q if(q == points(0)) { @@ -107,16 +106,16 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { if(neighbors(1) == null) return null return neighbors(1).locateFirst(edge) } - null + throw new Exception("Point not found") } // Locate next triangle crossed by edge def findNeighbor(e: Point): Triangle = { if(orient(points(0), points(1), e) >= 0) return neighbors(2) - if(orient(points(1), points(2), e) >= 0) + else if(orient(points(1), points(2), e) >= 0) return neighbors(0) - if(orient(points(2), points(0), e) >= 0) + else if(orient(points(2), points(0), e) >= 0) return neighbors(1) else // Point must reside inside this triangle @@ -155,6 +154,16 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { neighbors(0) } + // The neighbor clockwise to given point + def neighborAcross(point: Point): Triangle = { + if(point == points(0)) { + neighbors(0) + }else if(point == points(1)) { + neighbors(1) + } else + neighbors(2) + } + // The point counter-clockwise to given point def pointCCW(point: Point): Point = { if(point == points(0)) { @@ -207,6 +216,25 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) { updateEdges } + // Rotate neighbors clockwise around give point. Share diagnal with triangle + def rotateNeighborsCW(oPoint: Point, triangle: Triangle) { + if(oPoint == points(0)) { + neighbors(2) = neighbors(1) + neighbors(1) = null + neighbors(0) = triangle + } else if (oPoint == points(1)) { + neighbors(0) = neighbors(2) + neighbors(2) = null + neighbors(1) = triangle + } else if (oPoint == points(2)) { + neighbors(1) = neighbors(0) + neighbors(0) = null + neighbors(2) = triangle + } else { + throw new Exception("pointer bug") + } + } + def printDebug = println(points(0) + "," + points(1) + "," + points(2)) private var ik, ij , jk, ji, kj, ki: Point = null