fixed Math.abs collinear bug

This commit is contained in:
Mason 2009-08-07 15:39:29 -04:00
parent 709d8b17ca
commit 96713f6595
4 changed files with 49 additions and 34 deletions

View File

@ -75,7 +75,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
var drawSegs = true var drawSegs = true
var hiLighter = 0 var hiLighter = 0
var drawEarClip = false var drawEarClip = false
var drawCDT = false var drawCDT = true
val nazcaMonkey = "data/nazca_monkey.dat" val nazcaMonkey = "data/nazca_monkey.dat"
val bird = "data/bird.dat" val bird = "data/bird.dat"

View File

@ -276,7 +276,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
val dEdge = new Segment(point1, point2) val dEdge = new Segment(point1, point2)
T1.first markEdge dEdge T1.first markEdge dEdge
T2.first markEdge dEdge T2.first markEdge dEdge
println("cut")
} else if(firstTriangle == null) { } else if(firstTriangle == null) {
// No triangles are intersected by the edge; edge must lie outside the mesh // No triangles are intersected by the edge; edge must lie outside the mesh
@ -308,7 +308,8 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
val dEdge = new Segment(point1, point2) val dEdge = new Segment(point1, point2)
T.first markEdge dEdge T.first markEdge dEdge
} else if(firstTriangle.contains(edge)) { } else {
// Triangle must contain the edge
// Mark constrained edge // Mark constrained edge
firstTriangle markEdge edge firstTriangle markEdge edge
} }
@ -420,39 +421,38 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
val oPoint = t2 oppositePoint t1 val oPoint = t2 oppositePoint t1
// Prevent creation of collinear traingles if(illegal(t1.points(1), oPoint, t1.points(2), t1.points(0))) {
val c1 = Util.collinear(t1.points(0), t1.points(1), oPoint)
val c2 = Util.collinear(t2.points(0), t2.points(1), oPoint)
val c3 = Util.collinear(t2.points(1), t2.points(2), oPoint)
val c4 = Util.collinear(t2.points(0), t2.points(2), oPoint)
val collinear = (c1 || c2 || c3 || c4)
if(illegal(t1.points(1), oPoint, t1.points(2), t1.points(0)) && !collinear) {
println("legalize") // Prevent creation of collinear traingles
// Update neighbor pointers val c1 = t1.collinear(oPoint)
val ccwNeighbor = t2.neighborCCW(oPoint) val c2 = t2.collinear(oPoint, t1.points(0))
if(ccwNeighbor != null) { if(!c1 && !c2) {
val point = if(t1.points(0).x > oPoint.x) t1.points(1) else t1.points(2)
ccwNeighbor.updateNeighbors(oPoint, point, t1, mesh.debug)
t1.neighbors(1) = ccwNeighbor
}
t2.rotateNeighborsCW(oPoint, t1) // Update neighbor pointers
val ccwNeighbor = t2.neighborCCW(oPoint)
t1.neighbors(0) = t2
t1.neighbors(2) = null if(ccwNeighbor != null) {
//val point = if(t1.points(0).x > oPoint.x) t1.points(1) else t1.points(2)
// Flip edges and rotate everything clockwise ccwNeighbor.updateNeighbors(oPoint, t1.points(2), t1, mesh.debug)
val point = t1.points(0) t1.neighbors(1) = ccwNeighbor
t1.legalize(oPoint) }
t2.legalize(oPoint, point)
t2.rotateNeighborsCW(oPoint, t1)
false
t1.neighbors(0) = t2
t1.neighbors(2) = null
// Flip edges and rotate everything clockwise
val point = t1.points(0)
t1.legalize(oPoint)
t2.legalize(oPoint, point)
return false
}
} else { } else {
true true
} }
true
} }
// Final step in the sweep-line CDT algo // Final step in the sweep-line CDT algo

View File

@ -33,6 +33,8 @@ package org.poly2tri.shapes
import scala.collection.mutable.HashSet import scala.collection.mutable.HashSet
import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer
import utils.Util
// Triangle-based data structures are know to have better performance than quad-edge structures // 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" // See: J. Shewchuk, "Triangle: Engineering a 2D Quality Mesh Generator and Delaunay Triangulator"
// "Triangulations in CGAL" // "Triangulations in CGAL"
@ -54,8 +56,7 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) {
neighbors(2) = triangle neighbors(2) = triangle
else { else {
debug += triangle debug += triangle
debug += this //throw new Exception("Neighbor pointer error, please report!")
throw new Exception("Neighbor pointer error, please report!")
} }
} }
@ -217,6 +218,20 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) {
updateEdges updateEdges
} }
// Make legalized triangle will not be collinear
def collinear(oPoint: Point): Boolean = Util.collinear(points(0), points(1), oPoint)
// Make sure legalized triangle will not be collinear
def collinear(oPoint: Point, nPoint: Point): Boolean = {
if(oPoint == points(0)) {
Util.collinear(points(0), points(2), nPoint)
} else if (oPoint == points(1)) {
Util.collinear(points(0), points(1), nPoint)
} else {
Util.collinear(points(2), points(1), nPoint)
}
}
// Rotate neighbors clockwise around give point. Share diagnal with triangle // Rotate neighbors clockwise around give point. Share diagnal with triangle
def rotateNeighborsCW(oPoint: Point, triangle: Triangle) { def rotateNeighborsCW(oPoint: Point, triangle: Triangle) {
if(oPoint == points(0)) { if(oPoint == points(0)) {

View File

@ -51,7 +51,7 @@ object Util {
// Determinant // Determinant
val d = a11*(a22-a32) - a12*(a21-a31) + (a21*a32-a31*a22) val d = a11*(a22-a32) - a12*(a21-a31) + (a21*a32-a31*a22)
if(Math.abs(d) <= COLLINEAR_SLOP) if(d <= COLLINEAR_SLOP)
true true
else else
false false