diff options
| author | Mikhail Romanko <me@blankhex.com> | 2025-02-24 09:37:22 +0300 |
|---|---|---|
| committer | Mikhail Romanko <me@blankhex.com> | 2025-02-24 09:37:22 +0300 |
| commit | 67e7582d6314029cacbbfdb20378720827a678de (patch) | |
| tree | 24f3896bafba89c060ec8e3ff758c512129db85c /test/src/TestRay3f.c | |
| parent | be16daaecf4bd9ceebeff0f5bd89d48c688cb0cd (diff) | |
| download | bhlib-67e7582d6314029cacbbfdb20378720827a678de.tar.gz | |
Add line, plane, ray and segments, split math unit test
Added some basic geometric primitives such as planes, rays, segments
and lines (plus some extra functions like xProject, xBarycentric, Lerpf),
as well as some intersection tests between them.
Additionally, I split massive math test into smaller ones and tweaked
unit test library (testing no longer stops after first failure).
Diffstat (limited to 'test/src/TestRay3f.c')
| -rw-r--r-- | test/src/TestRay3f.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/test/src/TestRay3f.c b/test/src/TestRay3f.c new file mode 100644 index 0000000..6a80270 --- /dev/null +++ b/test/src/TestRay3f.c @@ -0,0 +1,99 @@ +#include <BH/Math.h> +#include <BH/Unit.h> + + +#define ACCEPTABLE_DELTA 0.0001f + + +BH_UNIT_TEST(RayIntersectTriangle) +{ + float a[3], b[3], c[3], p[3], d[3], out[3], t; + + a[0] =-3.0000f; a[1] = 1.0000f; a[2] = 2.0000f; + b[0] =-5.0000f; b[1] =-2.0000f; b[2] = 0.0000f; + c[0] =-6.0000f; c[1] = 2.5000f; c[2] =-1.0000f; + + p[0] =-1.5000f; p[1] = 1.0000f; p[2] = 1.0000f; + d[0] =-1.0000f; d[1] = 0.0000f; d[2] = 0.0000f; + + BH_VERIFY(BH_Ray3fIntersectTriangle(p, d, a, b, c, &t, out) == BH_OK); + + BH_VERIFY_DELTA(t, 2.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(out[0],-4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(out[1], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(out[2], 1.0000f, ACCEPTABLE_DELTA); + + return 0; +} + + +BH_UNIT_TEST(SegmentIntersectTriangle) +{ + float a[3], b[3], c[3], d[3], f[3], out[3], t; + + a[0] =-3.0000f; a[1] = 1.0000f; a[2] = 2.0000f; + b[0] =-5.0000f; b[1] =-2.0000f; b[2] = 0.0000f; + c[0] =-6.0000f; c[1] = 2.5000f; c[2] =-1.0000f; + + d[0] =-1.5000f; d[1] = 1.0000f; d[2] = 1.0000f; + f[0] =-6.0000f; f[1] = 1.0000f; f[2] = 1.0000f; + + BH_VERIFY(BH_Segment3fIntersectTriangle(d, f, a, b, c, &t, out) == BH_OK); + BH_VERIFY_DELTA(out[0],-4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(out[1], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(out[2], 1.0000f, ACCEPTABLE_DELTA); + + BH_Vec3fLerp(d, f, t, out); + BH_VERIFY_DELTA(out[0],-4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(out[1], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(out[2], 1.0000f, ACCEPTABLE_DELTA); + + return 0; +} + + +BH_UNIT_TEST(Barycentric) +{ + float a[3], b[3], c[3], p[3], d[3], out[3], t; + + a[0] =-3.0000f; a[1] = 1.0000f; a[2] = 2.0000f; + b[0] =-5.0000f; b[1] =-2.0000f; b[2] = 0.0000f; + c[0] =-6.0000f; c[1] = 2.5000f; c[2] =-1.0000f; + + p[0] =-1.5000f; p[1] = 1.0000f; p[2] = 1.0000f; + d[0] =-1.0000f; d[1] = 0.0000f; d[2] = 0.0000f; + + BH_VERIFY(BH_Ray3fIntersectTriangle(p, d, a, b, c, &t, out) == BH_OK); + (void)t; + + BH_Triangle3fBarycentric(a, b, c, out, out); + BH_VERIFY(out[0] >= 0.0f); + BH_VERIFY(out[1] >= 0.0f); + BH_VERIFY(out[2] >= 0.0f); + + BH_VERIFY(out[0] <= 1.0f); + BH_VERIFY(out[1] <= 1.0f); + BH_VERIFY(out[2] <= 1.0f); + + BH_VERIFY_DELTA(out[0] + out[1] + out[2], 1.0000f, ACCEPTABLE_DELTA); + + BH_Vec3fBarycentric(a, b, c, out[1], out[2], out); + BH_VERIFY_DELTA(out[0],-4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(out[1], 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(out[2], 1.0000f, ACCEPTABLE_DELTA); + + return 0; +} + + +int main(int argc, char **argv) +{ + (void)argc; + (void)argv; + + BH_UNIT_ADD(RayIntersectTriangle); + BH_UNIT_ADD(SegmentIntersectTriangle); + BH_UNIT_ADD(Barycentric); + + return BH_UnitRun(); +} |
