Permalink
Browse files

Fix axis aligned cpBB segment query test.

  • Loading branch information...
1 parent f5e696c commit d342fb3b6dc693aaeb777d3cd3d71a497e8d1d1f @slembcke committed Oct 3, 2016
Showing with 123 additions and 4 deletions.
  1. +8 −4 include/chipmunk/cpBB.h
  2. +115 −0 xcode/ObjectiveChipmunkTests/MiscTest.m
@@ -117,14 +117,18 @@ static inline cpFloat cpBBSegmentQuery(cpBB bb, cpVect a, cpVect b)
cpVect delta = cpvsub(b, a);
cpFloat tmin = -INFINITY, tmax = INFINITY;
- if(delta.x != 0.0f){
+ if(delta.x == 0.0f){
+ if(a.x < bb.l || bb.r < a.x) return INFINITY;
+ } else {
cpFloat t1 = (bb.l - a.x)/delta.x;
cpFloat t2 = (bb.r - a.x)/delta.x;
tmin = cpfmax(tmin, cpfmin(t1, t2));
tmax = cpfmin(tmax, cpfmax(t1, t2));
}
- if(delta.y != 0.0f){
+ if(delta.y == 0.0f){
+ if(a.y < bb.b || bb.t < a.y) return INFINITY;
+ } else {
cpFloat t1 = (bb.b - a.y)/delta.y;
cpFloat t2 = (bb.t - a.y)/delta.y;
tmin = cpfmax(tmin, cpfmin(t1, t2));
@@ -133,9 +137,9 @@ static inline cpFloat cpBBSegmentQuery(cpBB bb, cpVect a, cpVect b)
if(tmin <= tmax && 0.0f <= tmax && tmin <= 1.0f){
return cpfmax(tmin, 0.0f);
+ } else {
+ return INFINITY;
}
-
- return INFINITY;
}
/// Return true if the bounding box intersects the line segment with ends @c a and @c b.
@@ -132,6 +132,121 @@ -(void)testSlerp {
}
}
+-(void)testBBSegmentQuery
+{
+ { // left
+ cpVect a = cpv(-2, 0);
+ cpVect b = cpv( 0, 0);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqualWithAccuracy(t, 0.5, 1e-5);
+ }
+
+ { // right
+ cpVect a = cpv( 2, 0);
+ cpVect b = cpv( 0, 0);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqualWithAccuracy(t, 0.5, 1e-5);
+ }
+
+ { // bottom
+ cpVect a = cpv(0, -2);
+ cpVect b = cpv(0, 0);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqualWithAccuracy(t, 0.5, 1e-5);
+ }
+
+ { // top
+ cpVect a = cpv(0, 2);
+ cpVect b = cpv(0, 0);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqualWithAccuracy(t, 0.5, 1e-5);
+ }
+
+ { // diagonal corner
+ cpVect a = cpv(-2, -2);
+ cpVect b = cpv( 0, 0);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqualWithAccuracy(t, 0.5, 1e-5);
+ }
+
+ { // diagonal edge
+ cpVect a = cpv(-2, -1);
+ cpVect b = cpv( 0, 1);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqualWithAccuracy(t, 0.5, 1e-5);
+ }
+
+ { // x-aligned low.
+ cpVect a = cpv(-2, -1);
+ cpVect b = cpv( 0, -1);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqual(t, 0.5);
+ }
+
+ { // x-aligned high.
+ cpVect a = cpv(-2, 1);
+ cpVect b = cpv( 0, 1);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqual(t, 0.5);
+ }
+
+ { // y-aligned low.
+ cpVect a = cpv(-1, -2);
+ cpVect b = cpv(-1, 0);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqual(t, 0.5);
+ }
+
+ { // y-aligned high.
+ cpVect a = cpv(1, -2);
+ cpVect b = cpv(1, 0);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqual(t, 0.5);
+ }
+
+ { // Miss, x-aligned low.
+ cpVect a = cpv( 2, -2);
+ cpVect b = cpv( 0, -2);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqual(t, INFINITY);
+ }
+
+ { // Miss, x-aligned high.
+ cpVect a = cpv( 2, 2);
+ cpVect b = cpv( 0, 2);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqual(t, INFINITY);
+ }
+
+ { // Miss, y-aligned low.
+ cpVect a = cpv(-2, 2);
+ cpVect b = cpv(-2, 0);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqual(t, INFINITY);
+ }
+
+ { // Miss, y-aligned high.
+ cpVect a = cpv(2, 2);
+ cpVect b = cpv(2, 0);
+ cpBB bb = cpBBNew(-1, -1, 1, 1);
+ cpFloat t = cpBBSegmentQuery(bb, a, b);
+ XCTAssertEqual(t, INFINITY);
+ }
+}
+
-(void)testImageSamplerLA
{
{

0 comments on commit d342fb3

Please sign in to comment.