fixed triangulation bug

This commit is contained in:
Mason 2009-08-15 11:39:29 -04:00
parent 00b91269b1
commit cd7d09ee8a
3 changed files with 21 additions and 16 deletions

View File

@ -87,7 +87,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
val i18 = "data/i.18" val i18 = "data/i.18"
val tank = "data/tank.dat" val tank = "data/tank.dat"
var currentModel = strange var currentModel = nazcaMonkey
var doCDT = true var doCDT = true
var mouseButton = 0 var mouseButton = 0

View File

@ -251,22 +251,22 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
val point1 = if(ahead) edge.q else edge.p val point1 = if(ahead) edge.q else edge.p
val point2 = if(ahead) edge.p else edge.q val point2 = if(ahead) edge.p else edge.q
var node = aFront.locatePoint(point1) var pNode = aFront.locatePoint(point1)
val first = node val first = pNode
val points = new ArrayBuffer[Point] val points = new ArrayBuffer[Point]
// Neighbor triangles // Neighbor triangles
val nTriangles = new ArrayBuffer[Triangle] val nTriangles = new ArrayBuffer[Triangle]
nTriangles += node.triangle nTriangles += pNode.triangle
node = node.next pNode = pNode.next
while(node.point != point2) { while(pNode.point != point2) {
points += node.point points += pNode.point
nTriangles += node.triangle nTriangles += pNode.triangle
node = node.next pNode = pNode.next
} }
// Triangulate empty areas. // Triangulate empty areas.
val T = new ArrayBuffer[Triangle] val T = new ArrayBuffer[Triangle]
triangulate(points.toArray, List(point1, point2), T) triangulate(points.toArray, List(point1, point2), T)
@ -283,17 +283,18 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
} }
// Update advancing front // Update advancing front
aFront link (first, node, edgeTri) aFront link (first, pNode, edgeTri)
// Update neighbors // Update neighbors
edgeNeighbors(nTriangles, T) edgeNeighbors(nTriangles, T)
// Mark constrained edge // Mark constrained edge
edgeTri markEdge(point1, point2) edgeTri markEdge(point1, point2)
} else if(firstTriangle.contains(edge.q, edge.p)) { } else if(firstTriangle.contains(edge.q, edge.p)) {
// Mark constrained edge // Mark constrained edge
firstTriangle markEdge(edge.q, edge.p) firstTriangle markEdge(edge.q, edge.p)
firstTriangle.finalized = true
} else { } else {
throw new Exception("Triangulation error") throw new Exception("Triangulation error")
//null //null
@ -326,7 +327,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
if(P.size > 1) { if(P.size > 1) {
var c = P.first var c = P.first
for(j <- 1 until P.size) { for(j <- 1 until P.size) {
if(illegal(a, P(j), b, c)) { if(illegal(a, c, b, P(j))) {
c = P(j) c = P(j)
i = j i = j
} }
@ -425,9 +426,9 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
val oPoint = t2 oppositePoint t1 val oPoint = t2 oppositePoint t1
val collinear = t1.collinear(oPoint) || t2.collinear(oPoint, point) val collinear = t1.collinear(oPoint) || t2.collinear(oPoint, point)
if(illegal(t1.points(1), oPoint, t1.points(2), t1.points(0)) && !t2.finalized) { if(illegal(t1.points(1), oPoint, t1.points(2), t1.points(0)) && !t2.finalized) {
// Flip edge and rotate everything clockwise // Flip edge and rotate everything clockwise
t1.legalize(oPoint) t1.legalize(oPoint)
t2.legalize(oPoint, point) t2.legalize(oPoint, point)
@ -445,6 +446,10 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
} }
t2.markNeighbor(t1) t2.markNeighbor(t1)
// Don't legalize these triangles again
t2.finalized = true
t1.finalized = true
// Update advancing front // Update advancing front
aFront.insertLegalized(t1.points(1), t1, node) aFront.insertLegalized(t1.points(1), t1, node)

View File

@ -126,7 +126,7 @@ class Triangle(val points: Array[Point]) {
// Locate first triangle crossed by constrained edge // Locate first triangle crossed by constrained edge
def locateFirst(edge: Segment): Triangle = { def locateFirst(edge: Segment): Triangle = {
if(contains(edge)) this
if(edge.q == points(0)) if(edge.q == points(0))
search(points(1), points(2), edge, neighbors(2)) search(points(1), points(2), edge, neighbors(2))
else if(edge.q == points(1)) else if(edge.q == points(1))