-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube_test.cc
104 lines (82 loc) · 3.76 KB
/
cube_test.cc
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
#include "cube.hh"
#include "intersection.hh"
#include "mat3.hh"
#include "ray.hh"
#include "shape.hh"
#include "tform4.hh"
#include <gtest/gtest.h>
#include <spdlog/spdlog.h>
#include <algorithm> // sort
using namespace wt;
struct CubeTest {
shape s{cube{}};
};
struct CubeIntersectionTest : CubeTest {
std::vector<intersection> world_isecs;
};
struct CubeHitIntersectionTestParams {
pnt3 origin;
vec3 direction;
float t1;
float t2;
};
struct CubeHitIntersectionTest : CubeIntersectionTest,
testing::TestWithParam<CubeHitIntersectionTestParams> {};
TEST_P(CubeHitIntersectionTest, Hit) {
ray r{GetParam().origin, normalize(GetParam().direction)};
intersect(s, r, 0, world_isecs);
std::sort(world_isecs.begin(), world_isecs.end());
ASSERT_EQ(world_isecs.size(), 2);
EXPECT_EQ(world_isecs[0].t, GetParam().t1);
EXPECT_EQ(world_isecs[1].t, GetParam().t2);
}
INSTANTIATE_TEST_SUITE_P(
foobar, CubeHitIntersectionTest,
testing::Values(CubeHitIntersectionTestParams{{5, .5, 0}, {-1, 0, 0}, 4, 6}, // +x
CubeHitIntersectionTestParams{{-5, .5, 0}, {1, 0, 0}, 4, 6}, // -x
CubeHitIntersectionTestParams{{.5, 5, 0}, {0, -1, 0}, 4, 6}, // +y
CubeHitIntersectionTestParams{{.5, -5, 0}, {0, 1, 0}, 4, 6}, // -y
CubeHitIntersectionTestParams{{.5, 0, 5}, {0, 0, -1}, 4, 6}, // +z
CubeHitIntersectionTestParams{{.5, 0, -5}, {0, 0, 1}, 4, 6}, // -z
CubeHitIntersectionTestParams{{0, .5, 0}, {0, 0, 1}, -1, 1} // inside
));
struct CubeMissIntersectionTestParams {
pnt3 origin;
vec3 direction;
};
struct CubeMissIntersectionTest : CubeIntersectionTest,
testing::TestWithParam<CubeMissIntersectionTestParams> {};
TEST_P(CubeMissIntersectionTest, Miss) {
ray r{GetParam().origin, normalize(GetParam().direction)};
intersect(s, r, 0, world_isecs);
std::sort(world_isecs.begin(), world_isecs.end());
EXPECT_EQ(world_isecs.size(), 0);
}
INSTANTIATE_TEST_SUITE_P(
foobar, CubeMissIntersectionTest,
testing::Values(CubeMissIntersectionTestParams{{-2, 0, 0}, {.2673, .5345, .8018}},
CubeMissIntersectionTestParams{{0, -2, 0}, {.8018, .2673, .5345}},
CubeMissIntersectionTestParams{{0, 0, -2}, {.5345, .8018, .2673}},
CubeMissIntersectionTestParams{{2, 0, 2}, {0, 0, -1}},
CubeMissIntersectionTestParams{{0, 2, 2}, {0, -1, 0}},
CubeMissIntersectionTestParams{{2, 2, 0}, {-1, 0, 0}}));
struct CubeNormalTestParams {
pnt3 point;
vec3 normal;
};
struct CubeNormalTest : CubeTest, testing::TestWithParam<CubeNormalTestParams> {};
TEST_P(CubeNormalTest, Normal) {
vec3 normal = normal_at(s, GetParam().point, inv_tform(s));
EXPECT_NEAR(normal.x, GetParam().normal.x, 1e-5f);
EXPECT_NEAR(normal.y, GetParam().normal.y, 1e-5f);
EXPECT_NEAR(normal.z, GetParam().normal.z, 1e-5f);
}
INSTANTIATE_TEST_SUITE_P(foobar, CubeNormalTest,
testing::Values(CubeNormalTestParams{{1, .5, -.8}, {1, 0, 0}},
CubeNormalTestParams{{-1, -.2, .9}, {-1, 0, 0}},
CubeNormalTestParams{{-.4, 1, -.1}, {0, 1, 0}},
CubeNormalTestParams{{.3, -1, -.7}, {0, -1, 0}},
CubeNormalTestParams{{-.6, .3, 1}, {0, 0, 1}},
CubeNormalTestParams{{.4, .4, -1}, {0, 0, -1}},
CubeNormalTestParams{{1, 1, 1}, {1, 0, 0}},
CubeNormalTestParams{{-1, -1, -1}, {-1, 0, 0}}));