code cleanup

This commit is contained in:
zzzzrrr 2009-08-11 12:39:13 -04:00
parent 260e7935b9
commit c4d577ec06
3 changed files with 41 additions and 43 deletions

View File

@ -123,43 +123,43 @@ 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 = {
val p = edge.p
val q = edge.q
val e = p - q
val ik = points(2) - points(0) if(edge.q == points(0))
val ij = points(1) - points(0) search(points(1), points(2), edge, neighbors(2))
val jk = points(2) - points(1) else if(edge.q == points(1))
val ji = points(0) - points(1) search(points(0), points(2), edge, neighbors(0))
val kj = points(1) - points(2) else if(edge.q == points(2))
val ki = points(0) - points(2) search(points(1), points(0), edge, neighbors(1))
else
throw new Exception("Point not found")
}
def search(p1: Point, p2: Point, edge: Segment, neighbor: Triangle): Triangle = {
val o1 = Util.orient2d(edge.q, p1, edge.p)
val o2 = Util.orient2d(edge.q, p2, edge.p)
val sameSign = Math.signum(o1) == Math.signum(o2)
// Edge crosses this triangle
if(!sameSign)
return this
// Look at neighbor
if(neighbor == null)
null
else
neighbor.locateFirst(edge)
if(q == points(0)) {
val sameSign = Math.signum(ik cross e) == Math.signum(ij cross e)
if(!sameSign) return this
if(neighbors(2) == null) return null
return neighbors(2).locateFirst(edge)
} else if(q == points(1)) {
val sameSign = Math.signum(jk cross e) == Math.signum(ji cross e)
if(!sameSign) return this
if(neighbors(0) == null) return null
return neighbors(0).locateFirst(edge)
} else if(q == points(2)) {
val sameSign = Math.signum(kj cross e) == Math.signum(ki cross e)
if(!sameSign) return this
if(neighbors(1) == null) return null
return neighbors(1).locateFirst(edge)
}
throw new Exception("Point not found")
} }
// Locate next triangle crossed by edge // Locate next triangle crossed by edge
def findNeighbor(e: Point): Triangle = { def findNeighbor(e: Point): Triangle = {
if(Util.orient2d(points(0), points(1), e) > 0) if(Util.orient2d(points(0), points(1), e) < 0)
return neighbors(2) return neighbors(2)
else if(Util.orient2d(points(1), points(2), e) > 0) else if(Util.orient2d(points(1), points(2), e) < 0)
return neighbors(0) return neighbors(0)
else if(Util.orient2d(points(2), points(0), e) > 0) else if(Util.orient2d(points(2), points(0), e) < 0)
return neighbors(1) return neighbors(1)
else else
// Point must reside inside this triangle // Point must reside inside this triangle
@ -249,10 +249,10 @@ class Triangle(val points: Array[Point]) {
} }
// Make legalized triangle will not be collinear // Check if legalized triangle will be collinear
def collinear(oPoint: Point): Boolean = Util.collinear(points(1), points(0), oPoint) def collinear(oPoint: Point): Boolean = Util.collinear(points(1), points(0), oPoint)
// Make sure legalized triangle will not be collinear // Check if legalized triangle will be collinear
def collinear(oPoint: Point, nPoint: Point): Boolean = { def collinear(oPoint: Point, nPoint: Point): Boolean = {
if(oPoint == points(0)) { if(oPoint == points(0)) {
Util.collinear(points(0), points(2), nPoint) Util.collinear(points(0), points(2), nPoint)

View File

@ -43,7 +43,7 @@ object Util {
// Tests if the given points are collinear // Tests if the given points are collinear
def collinear(p1: Point, p2: Point, p3: Point): Boolean = { def collinear(p1: Point, p2: Point, p3: Point): Boolean = {
val d = (p2 - p1) cross (p1 - p3) val d = orient2d(p1, p2, p3)
if(Math.abs(d) <= COLLINEAR_SLOP) if(Math.abs(d) <= COLLINEAR_SLOP)
true true
@ -93,7 +93,7 @@ object Util {
} }
// Adaptive exact 2D orientation test. Robust. By Jonathan Shewchuk // Adaptive exact 2D orientation test. Robust. By Jonathan Shewchuk
// Return: positive if point a, b, and c are counterclockwise // Return: positive if point a, b, and c are counterclockwise
// negative if point a, b, and c are clockwise // negative if point a, b, and c are clockwise
// zero if points are collinear // zero if points are collinear
// See: http://www-2.cs.cmu.edu/~quake/robust.html // See: http://www-2.cs.cmu.edu/~quake/robust.html
@ -124,15 +124,13 @@ object Util {
if ((det >= errbound) || (-det >= errbound)) { if ((det >= errbound) || (-det >= errbound)) {
return det return det
} else { } else {
println(pa + "," + pb + "," + pc)
println(detleft + "," + detright) // Cheat a little bit.... we have a degenerate triangle
println("Det = " + det + " , errbound = " + errbound) val c = pc * 1.00001f
throw new Exception("Degenerate triangle") return orient2d(pa, pb, c)
} }
// Not implemented;
// http://www.cs.cmu.edu/afs/cs/project/quake/public/code/predicates.c
// return orient2dadapt(pa, pb, pc, detsum)
} }
} }