aboutsummaryrefslogtreecommitdiff
path: root/test/src/TestRay3f.c
blob: edb8997c22a1067af06b7349351e2d276e337753 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <BH/Math/Ray3f.h>
#include <BH/Math/Vec3f.h>
#include <BH/Math/Box3f.h>
#include <BH/Math/Plane.h>
#include <BH/Math/Misc.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;
}


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;
    (void)argv;

    BH_UNIT_ADD(RayIntersectTriangle);
    BH_UNIT_ADD(SegmentIntersectTriangle);
    BH_UNIT_ADD(Barycentric);
    BH_UNIT_ADD(RayBox);
    BH_UNIT_ADD(SegmentBox);

    return BH_UnitRun();
}