Add 2D/3D boxes/AABBs and intersection tests.

Added support for 2D/3D boxes (or AABBs) and intersection tests between
them and rays/segments.
This commit is contained in:
2025-02-24 22:56:16 +03:00
parent 67e7582d63
commit cae66889a1
6 changed files with 1020 additions and 123 deletions

98
test/src/TestBox2f.c Normal file
View File

@@ -0,0 +1,98 @@
#include <BH/Math.h>
#include <BH/Unit.h>
#define ACCEPTABLE_DELTA 0.0001f
BH_UNIT_TEST(Union)
{
float aMin[2], aMax[2], bMin[2], bMax[2], rMin[2], rMax[2];
aMin[0] = 1.0f; aMin[1] = 2.0f;
aMax[0] = 5.0f; aMax[1] = 5.0f;
bMin[0] = 0.0f; bMin[1] = 0.0f;
bMax[0] = 4.0f; bMax[1] = 4.0f;
BH_Box2fUnion(aMin, aMax, bMin, bMax, rMin, rMax);
BH_VERIFY_DELTA(rMin[0], 0.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMin[1], 0.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[0], 5.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[1], 5.0000f, ACCEPTABLE_DELTA);
return 0;
}
BH_UNIT_TEST(Intersect)
{
float aMin[2], aMax[2], bMin[2], bMax[2], rMin[2], rMax[2];
aMin[0] = 1.0f; aMin[1] = 2.0f;
aMax[0] = 5.0f; aMax[1] = 5.0f;
bMin[0] = 0.0f; bMin[1] = 0.0f;
bMax[0] = 4.0f; bMax[1] = 4.0f;
BH_VERIFY(BH_Box2fIntersect(aMin, aMax, bMin, bMax, rMin, rMax) == BH_OK);
BH_VERIFY_DELTA(rMin[0], 1.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMin[1], 2.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[0], 4.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[1], 4.0000f, ACCEPTABLE_DELTA);
return 0;
}
BH_UNIT_TEST(Contains)
{
float aMin[2], aMax[2], point[2];
aMin[0] = 1.0f; aMin[1] = 2.0f;
aMax[0] = 5.0f; aMax[1] = 5.0f;
point[0] = 0.0f; point[1] = 0.0f;
BH_VERIFY(BH_Box2fContains(aMin, aMax, point) != BH_OK);
point[0] = 1.0f; point[1] = 2.0f;
BH_VERIFY(BH_Box2fContains(aMin, aMax, point) == BH_OK);
point[0] = 4.0f; point[1] = 4.0f;
BH_VERIFY(BH_Box2fContains(aMin, aMax, point) == BH_OK);
point[0] = 6.0f; point[1] = 6.0f;
BH_VERIFY(BH_Box2fContains(aMin, aMax, point) != BH_OK);
return 0;
}
BH_UNIT_TEST(Enclose)
{
float points[4], rMin[2], rMax[2];
points[0] = 5.0f; points[1] = 5.0f;
points[2] = 1.0f; points[3] = 2.0f;
BH_VERIFY(BH_Box2fEnclose(NULL, 0, NULL, NULL) != BH_OK);
BH_VERIFY(BH_Box2fEnclose(points, 2, rMin, rMax) == BH_OK);
BH_VERIFY_DELTA(rMin[0], 1.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMin[1], 2.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[0], 5.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[1], 5.0000f, ACCEPTABLE_DELTA);
return 0;
}
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
BH_UNIT_ADD(Union);
BH_UNIT_ADD(Intersect);
BH_UNIT_ADD(Contains);
BH_UNIT_ADD(Enclose);
return BH_UnitRun();
}

104
test/src/TestBox3f.c Normal file
View File

@@ -0,0 +1,104 @@
#include <BH/Math.h>
#include <BH/Unit.h>
#define ACCEPTABLE_DELTA 0.0001f
BH_UNIT_TEST(Union)
{
float aMin[3], aMax[3], bMin[3], bMax[3], rMin[3], rMax[3];
aMin[0] = 1.0f; aMin[1] = 2.0f; aMin[2] = 3.0f;
aMax[0] = 5.0f; aMax[1] = 5.0f; aMax[2] = 5.0f;
bMin[0] = 0.0f; bMin[1] = 0.0f; bMin[2] = 0.0f;
bMax[0] = 4.0f; bMax[1] = 4.0f; bMax[2] = 4.0f;
BH_Box3fUnion(aMin, aMax, bMin, bMax, rMin, rMax);
BH_VERIFY_DELTA(rMin[0], 0.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMin[1], 0.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMin[2], 0.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[0], 5.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[1], 5.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[2], 5.0000f, ACCEPTABLE_DELTA);
return 0;
}
BH_UNIT_TEST(Intersect)
{
float aMin[3], aMax[3], bMin[3], bMax[3], rMin[3], rMax[3];
aMin[0] = 1.0f; aMin[1] = 2.0f; aMin[2] = 3.0f;
aMax[0] = 5.0f; aMax[1] = 5.0f; aMax[2] = 5.0f;
bMin[0] = 0.0f; bMin[1] = 0.0f; bMin[2] = 0.0f;
bMax[0] = 4.0f; bMax[1] = 4.0f; bMax[2] = 4.0f;
BH_VERIFY(BH_Box3fIntersect(aMin, aMax, bMin, bMax, rMin, rMax) == BH_OK);
BH_VERIFY_DELTA(rMin[0], 1.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMin[1], 2.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMin[2], 3.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[0], 4.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[1], 4.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[2], 4.0000f, ACCEPTABLE_DELTA);
return 0;
}
BH_UNIT_TEST(Contains)
{
float aMin[3], aMax[3], point[3];
aMin[0] = 1.0f; aMin[1] = 2.0f; aMin[2] = 3.0f;
aMax[0] = 5.0f; aMax[1] = 5.0f; aMax[2] = 5.0f;
point[0] = 0.0f; point[1] = 0.0f; point[2] = 0.0f;
BH_VERIFY(BH_Box3fContains(aMin, aMax, point) != BH_OK);
point[0] = 1.0f; point[1] = 2.0f; point[2] = 3.0f;
BH_VERIFY(BH_Box3fContains(aMin, aMax, point) == BH_OK);
point[0] = 4.0f; point[1] = 4.0f; point[2] = 4.0f;
BH_VERIFY(BH_Box3fContains(aMin, aMax, point) == BH_OK);
point[0] = 6.0f; point[1] = 6.0f; point[2] = 5.0f;
BH_VERIFY(BH_Box3fContains(aMin, aMax, point) != BH_OK);
return 0;
}
BH_UNIT_TEST(Enclose)
{
float points[6], rMin[3], rMax[3];
points[0] = 5.0f; points[1] = 5.0f; points[2] = 5.0f;
points[3] = 1.0f; points[4] = 2.0f; points[5] = 3.0f;
BH_VERIFY(BH_Box3fEnclose(NULL, 0, NULL, NULL) != BH_OK);
BH_VERIFY(BH_Box3fEnclose(points, 2, rMin, rMax) == BH_OK);
BH_VERIFY_DELTA(rMin[0], 1.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMin[1], 2.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMin[2], 3.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[0], 5.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[1], 5.0000f, ACCEPTABLE_DELTA);
BH_VERIFY_DELTA(rMax[2], 5.0000f, ACCEPTABLE_DELTA);
return 0;
}
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
BH_UNIT_ADD(Union);
BH_UNIT_ADD(Intersect);
BH_UNIT_ADD(Contains);
BH_UNIT_ADD(Enclose);
return BH_UnitRun();
}

View File

@@ -180,6 +180,72 @@ BH_UNIT_TEST(Time)
}
BH_UNIT_TEST(RayBox)
{
float start[2], direction[2], bMin[2], bMax[2], r[2];
float time;
bMin[0] =-2.0f; bMin[1] =-2.0f;
bMax[0] = 3.0f; bMax[1] = 3.0f;
start[0] = 0.0f; start[1] = 0.0f;
direction[0] = 1.0f; direction[1] = 0.0f;
BH_VERIFY(BH_Ray2fIntersectBox2f(start, direction, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box2fContains(bMin, bMax, r) == BH_OK);
start[0] = -4.0f; start[1] = 0.0f;
BH_VERIFY(BH_Ray2fIntersectBox2f(start, direction, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box2fContains(bMin, bMax, r) == BH_OK);
start[0] = 4.0f; start[1] = 0.0f;
BH_VERIFY(BH_Ray2fIntersectBox2f(start, direction, bMin, bMax, &time, r) != BH_OK);
start[0] = 4.0f; start[1] = 0.0f;
direction[0] = -1.0f; direction[1] = 0.0f;
BH_VERIFY(BH_Ray2fIntersectBox2f(start, direction, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box2fContains(bMin, bMax, r) == BH_OK);
start[0] = 4.0f; start[1] = 4.0f;
direction[0] = -1.0f; direction[1] = 0.0f;
BH_VERIFY(BH_Ray2fIntersectBox2f(start, direction, bMin, bMax, &time, r) != BH_OK);
return 0;
}
BH_UNIT_TEST(SegmentBox)
{
float start[2], end[2], bMin[2], bMax[2], r[2];
float time;
bMin[0] =-2.0f; bMin[1] =-2.0f;
bMax[0] = 3.0f; bMax[1] = 3.0f;
start[0] = 0.0f; start[1] = 0.0f;
end[0] = 5.0f; end[1] = 0.0f;
BH_VERIFY(BH_Segment2fIntersectBox2f(start, end, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box2fContains(bMin, bMax, r) == BH_OK);
start[0] = -4.0f; start[1] = 0.0f;
BH_VERIFY(BH_Segment2fIntersectBox2f(start, end, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box2fContains(bMin, bMax, r) == BH_OK);
start[0] = 4.0f; start[1] = 0.0f;
BH_VERIFY(BH_Segment2fIntersectBox2f(start, end, bMin, bMax, &time, r) != BH_OK);
start[0] = 4.0f; start[1] = 0.0f;
end[0] = -5.0f; end[1] = 0.0f;
BH_VERIFY(BH_Segment2fIntersectBox2f(start, end, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box2fContains(bMin, bMax, r) == BH_OK);
start[0] = 4.0f; start[1] = 4.0f;
end[0] = -5.0f; end[1] = 4.0f;
BH_VERIFY(BH_Segment2fIntersectBox2f(start, end, bMin, bMax, &time, r) != BH_OK);
return 0;
}
int main(int argc, char **argv)
{
(void)argc;
@@ -191,6 +257,8 @@ int main(int argc, char **argv)
BH_UNIT_ADD(SegmentIntersectLine);
BH_UNIT_ADD(SegmentIntersectSegment);
BH_UNIT_ADD(Time);
BH_UNIT_ADD(RayBox);
BH_UNIT_ADD(SegmentBox);
return BH_UnitRun();
}

View File

@@ -86,6 +86,72 @@ BH_UNIT_TEST(Barycentric)
}
BH_UNIT_TEST(RayBox)
{
float start[3], direction[3], bMin[3], bMax[3], r[3];
float time;
bMin[0] =-2.0f; bMin[1] =-2.0f; bMin[2] =-2.0f;
bMax[0] = 3.0f; bMax[1] = 3.0f; bMax[2] = 3.0f;
start[0] = 0.0f; start[1] = 0.0f; start[2] = 0.0f;
direction[0] = 1.0f; direction[1] = 0.0f; direction[2] = 0.0f;
BH_VERIFY(BH_Ray3fIntersectBox3f(start, direction, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box3fContains(bMin, bMax, r) == BH_OK);
start[0] = -4.0f; start[1] = 0.0f; start[2] = 0.0f;
BH_VERIFY(BH_Ray3fIntersectBox3f(start, direction, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box3fContains(bMin, bMax, r) == BH_OK);
start[0] = 4.0f; start[1] = 0.0f; start[2] = 0.0f;
BH_VERIFY(BH_Ray3fIntersectBox3f(start, direction, bMin, bMax, &time, r) != BH_OK);
start[0] = 4.0f; start[1] = 0.0f; start[2] = 0.0f;
direction[0] = -1.0f; direction[1] = 0.0f; direction[2] = 0.0f;
BH_VERIFY(BH_Ray3fIntersectBox3f(start, direction, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box3fContains(bMin, bMax, r) == BH_OK);
start[0] = 4.0f; start[1] = 4.0f; start[2] = 4.0f;
direction[0] = -1.0f; direction[1] = 0.0f; direction[2] = 0.0f;
BH_VERIFY(BH_Ray3fIntersectBox3f(start, direction, bMin, bMax, &time, r) != BH_OK);
return 0;
}
BH_UNIT_TEST(SegmentBox)
{
float start[3], end[3], bMin[3], bMax[3], r[3];
float time;
bMin[0] =-2.0f; bMin[1] =-2.0f; bMin[2] =-2.0f;
bMax[0] = 3.0f; bMax[1] = 3.0f; bMax[2] = 3.0f;
start[0] = 0.0f; start[1] = 0.0f; start[2] = 0.0f;
end[0] = 5.0f; end[1] = 0.0f; end[2] = 0.0f;
BH_VERIFY(BH_Segment3fIntersectBox3f(start, end, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box3fContains(bMin, bMax, r) == BH_OK);
start[0] = -4.0f; start[1] = 0.0f; start[2] = 0.0f;
BH_VERIFY(BH_Segment3fIntersectBox3f(start, end, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box3fContains(bMin, bMax, r) == BH_OK);
start[0] = 4.0f; start[1] = 0.0f; start[2] = 0.0f;
BH_VERIFY(BH_Segment3fIntersectBox3f(start, end, bMin, bMax, &time, r) != BH_OK);
start[0] = 4.0f; start[1] = 0.0f; start[2] = 0.0f;
end[0] = -5.0f; end[1] = 0.0f; end[2] = 0.0f;
BH_VERIFY(BH_Segment3fIntersectBox3f(start, end, bMin, bMax, &time, r) == BH_OK);
BH_VERIFY(BH_Box3fContains(bMin, bMax, r) == BH_OK);
start[0] = 4.0f; start[1] = 4.0f; start[2] = 4.0f;
end[0] = -5.0f; end[1] = 4.0f; end[2] = 0.0f;
BH_VERIFY(BH_Segment3fIntersectBox3f(start, end, bMin, bMax, &time, r) != BH_OK);
return 0;
}
int main(int argc, char **argv)
{
(void)argc;
@@ -94,6 +160,8 @@ int main(int argc, char **argv)
BH_UNIT_ADD(RayIntersectTriangle);
BH_UNIT_ADD(SegmentIntersectTriangle);
BH_UNIT_ADD(Barycentric);
BH_UNIT_ADD(RayBox);
BH_UNIT_ADD(SegmentBox);
return BH_UnitRun();
}