bug hunting

This commit is contained in:
zzzzrrr 2009-08-07 13:32:42 -04:00
parent ea65046b07
commit 8c7b86d715
4 changed files with 47 additions and 9 deletions

View File

@ -84,7 +84,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
val strange = "data/strange.dat" val strange = "data/strange.dat"
val i18 = "data/i.18" val i18 = "data/i.18"
var currentModel = strange var currentModel = nazcaMonkey
var mouseButton = 0 var mouseButton = 0
var mousePressed = false var mousePressed = false
@ -264,7 +264,7 @@ class Poly2TriDemo extends BasicGame("Poly2Tri") {
if(c == '5') selectModel(star) if(c == '5') selectModel(star)
if(c == '6') selectModel(i18) if(c == '6') selectModel(i18)
if(c == 's') drawSegs = !drawSegs if(c == 's') drawSegs = !drawSegs
if(c == 'e') {drawEarClip = !drawEarClip; selectModel(currentModel)} //if(c == 'e') {drawEarClip = !drawEarClip; selectModel(currentModel)}
} }
def selectModel(model: String) { def selectModel(model: String) {

View File

@ -126,14 +126,16 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
// Implement sweep-line // Implement sweep-line
private def sweep { private def sweep {
//var cTri: Triangle = null //var cTri: Triangle = null
for(i <- 1 until points.size) { for(i <- 1 until 15 /*points.size*/) {
val point = points(i) val point = points(i)
// Process Point event // Process Point event
var triangle: Triangle = null var triangle: Triangle = null
try { try {
triangle = pointEvent(point) triangle = pointEvent(point)
} catch { } catch {
case e: Exception => println("Offending triangle = " + i) case e: Exception =>
println("Offending triangle = " + i)
//System exit 0
} }
// Process edge events // Process edge events
//point.edges.foreach(e => edgeEvent(e, triangle)) //point.edges.foreach(e => edgeEvent(e, triangle))
@ -147,6 +149,9 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
val node = aFront.locate(point) val node = aFront.locate(point)
// Avoid triangles that are almost collinear
if(!Util.collinear(point, node.point, node.next.point)) {
// Projected point coincides with existing point; create two triangles // Projected point coincides with existing point; create two triangles
if(point.x == node.point.x && node.prev != null) { if(point.x == node.point.x && node.prev != null) {
@ -198,9 +203,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
if(legal) { if(legal) {
newNode = aFront.insert(point, triangle, node) newNode = aFront.insert(point, triangle, node)
// Update neighbors // Update neighbors
println(1)
nTri.updateNeighbors(cwPoint, ccwPoint, triangle, mesh.debug) nTri.updateNeighbors(cwPoint, ccwPoint, triangle, mesh.debug)
println(2)
} else { } else {
newNode = new Node(triangle.points(1), triangle) newNode = new Node(triangle.points(1), triangle)
val rNode = node.next val rNode = node.next
@ -214,6 +217,11 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
scanAFront(newNode) scanAFront(newNode)
newNode.triangle newNode.triangle
} }
} else {
println("bad triangle")
null
}
} }
// EdgeEvent // EdgeEvent
@ -404,9 +412,9 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
val sinA = v1 cross v2 val sinA = v1 cross v2
val sinB = v3 cross v4 val sinB = v3 cross v4
//println((cosA*sinB + sinA*cosB))
// Some small number // Some small number
if((cosA*sinB + sinA*cosB) < -0.1) if((cosA*sinB + sinA*cosB) < -10f)
true true
else else
false false

View File

@ -56,7 +56,7 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) {
mesh += triangle mesh += triangle
println(ccwPoint + "," + cwPoint) println(ccwPoint + "," + cwPoint)
printDebug printDebug
//throw new Exception("Neighbor update error") throw new Exception("Neighbor update error")
} }
} }

View File

@ -7,6 +7,8 @@ import shapes.Point
object Util { object Util {
val COLLINEAR_SLOP = 10f
// From "Scala By Example," by Martin Odersky // From "Scala By Example," by Martin Odersky
def msort[A](less: (A, A) => Boolean)(xs: List[A]): List[A] = { def msort[A](less: (A, A) => Boolean)(xs: List[A]): List[A] = {
def merge(xs1: List[A], xs2: List[A]): List[A] = def merge(xs1: List[A], xs2: List[A]): List[A] =
@ -33,6 +35,34 @@ object Util {
} }
xs xs
} }
// Tests if the given points are collinear
def collinear(p1: Point, p2: Point, p3: Point): Boolean = {
// 3x3 matrix
val a11 = p1.x
val a12 = p1.y
val a13 = 1f
val a21 = p2.x
val a22 = p2.y
val a23 = 1f
val a31 = p3.x
val a32 = p3.y
val a33 = 1f
// Determinant
val d = a11*(a22*a33 - a32*a23) - a12*(a21*a33-a31*a23) + a13*(a21*a32-a31*a22)
println("Determinant = " + d)
if(d <= COLLINEAR_SLOP) {
true
} else {
false
}
}
} }
/** The object <code>Random</code> offers a default implementation /** The object <code>Random</code> offers a default implementation