aboutsummaryrefslogtreecommitdiff
path: root/test/src/TestRay3f.c
blob: 6a80270d6c5ca7371f311dc332a2c7166293f7b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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();
}