Skip to content

Commit

Permalink
Fix axis aligned cpBB segment query test.
Browse files Browse the repository at this point in the history
  • Loading branch information
slembcke committed Oct 3, 2016
1 parent f5e696c commit d342fb3
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 4 deletions.
12 changes: 8 additions & 4 deletions include/chipmunk/cpBB.h
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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.
Expand Down
115 changes: 115 additions & 0 deletions xcode/ObjectiveChipmunkTests/MiscTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
{
Expand Down

0 comments on commit d342fb3

Please sign in to comment.