fixed orientation bug

This commit is contained in:
zzzzrrr 2009-08-05 11:25:56 -04:00
parent 456a6a8d15
commit ae5cf98122
2 changed files with 36 additions and 11 deletions

View File

@ -124,7 +124,7 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
// Implement sweep-line
private def sweep {
for(i <- 1 until 10 /*points.size*/) {
for(i <- 1 until points.size) {
val point = points(i)
// Process Point event
val triangle = pointEvent(point)
@ -334,9 +334,21 @@ class CDT(val points: List[Point], val segments: List[Segment], iTriangle: Trian
val v2 = p1 - p2
val v3 = p1 - p4
val v4 = p3 - p4
if((v1 dot v2) < 0 && (v3 dot v4) < 0)
true
else
val cosA = v1 dot v2
val cosB = v3 dot v4
if(cosA < 0 && cosB < 0)
return true
else if(cosA > 0 && cosB > 0)
return false
val sinA = v1 cross v2
val sinB = v3 cross v4
if(cosA*sinB + sinA*cosB < -0.01f)
true
else
false
}

View File

@ -118,13 +118,26 @@ class Triangle(val points: Array[Point], val neighbors: Array[Triangle]) {
// Locate next triangle crossed by constraied edge
def findNeighbor(e: Point): Triangle = {
var sameSign = Math.signum(ik cross e) == Math.signum(ij cross e)
if(!sameSign) return neighbors(0)
sameSign = Math.signum(jk cross e) == Math.signum(ji cross e)
if(!sameSign) return neighbors(1)
sameSign = Math.signum(kj cross e) == Math.signum(ki cross e)
if(!sameSign) return neighbors(2)
this
if(orient(points(0), points(1), e) > 0)
return neighbors(2)
if(orient(points(1), points(2), e) > 0)
return neighbors(0)
if(orient(points(2), points(0), e) > 0)
return neighbors(1)
else
// Point must reside inside this triangle
this
}
// Return: positive if point p is left of ab
// negative if point p is right of ab
// zero if points are colinear
def orient(b: Point, a: Point, p: Point): Float = {
val acx = a.x - p.x
val bcx = b.x - p.x
val acy = a.y - p.y
val bcy = b.y - p.y
acx * bcy - acy * bcx
}
// The neighbor CW to given point