summaryrefslogtreecommitdiff
path: root/test/test-geo.cpp
blob: 7404d3206774b4aa382ec63ad189d304bb4613fb (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#define BOOST_TEST_MODULE test_geo

#include "testHelpers.h"
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>
#include <stream_support.h>

#include <game/geoData.h>
#include <lib/ray.h>

struct TestGeoData : public GeoData {
	TestGeoData() : GeoData {{{-10, -5}, {30, 40}}, 5.F} { }
};

namespace std {
	std::ostream &
	operator<<(std::ostream & s, const Ray & r)
	{
		return (s << r.start << "->" << r.direction);
	}
}

BOOST_FIXTURE_TEST_SUITE(tgd, TestGeoData)

BOOST_AUTO_TEST_CASE(initialize)
{
	BOOST_CHECK_EQUAL(limit.first, glm::ivec2(-10, -5));
	BOOST_CHECK_EQUAL(limit.second, glm::ivec2(30, 40));
	BOOST_CHECK_EQUAL(scale, 5.F);
	BOOST_CHECK_EQUAL(size, glm::uvec2(41, 46));
	BOOST_CHECK_EQUAL(nodes.size(), 1886);
	BOOST_CHECK(std::all_of(nodes.begin(), nodes.end(), [](const auto & n) {
		return n.height == -1500.F;
	}));
}

BOOST_AUTO_TEST_CASE(coords)
{
	BOOST_CHECK_EQUAL(at(-10, -5), 0);
	BOOST_CHECK_EQUAL(at(-9, -5), 1);
	BOOST_CHECK_EQUAL(at(0, -5), 10);
	BOOST_CHECK_EQUAL(at(30, -5), 40);
	BOOST_CHECK_EQUAL(at(30, 40), 1885);
}

BOOST_AUTO_TEST_CASE(coords_bad)
{
	BOOST_CHECK_THROW(std::ignore = at(-11, -5), std::range_error);
	BOOST_CHECK_THROW(std::ignore = at(-10, -6), std::range_error);
	BOOST_CHECK_THROW(std::ignore = at(-11, -6), std::range_error);
	BOOST_CHECK_THROW(std::ignore = at(31, 40), std::range_error);
	BOOST_CHECK_THROW(std::ignore = at(30, 41), std::range_error);
	BOOST_CHECK_THROW(std::ignore = at(31, 41), std::range_error);
}

BOOST_AUTO_TEST_CASE(gen_random)
{
	// Can only really its sanity
	generateRandom();
	// Some terrain above sea level
	BOOST_CHECK(std::any_of(nodes.begin(), nodes.end(), [](const auto & n) {
		return n.height > 0;
	}));
	// Still an island
	for (int x = limit.first.x; x <= limit.second.x; x += 1) {
		BOOST_CHECK_EQUAL(nodes[at(x, limit.first.y)].height, -1500.F);
		BOOST_CHECK_EQUAL(nodes[at(x, limit.second.y)].height, -1500.F);
	}
	for (int y = limit.first.y; y <= limit.second.y; y += 1) {
		BOOST_CHECK_EQUAL(nodes[at(limit.first.x, y)].height, -1500.F);
		BOOST_CHECK_EQUAL(nodes[at(limit.second.x, y)].height, -1500.F);
	}
}

BOOST_AUTO_TEST_CASE(load_uk_heightmap)
{
	loadFromImages(FIXTURESDIR "/height/V0txo.jpg", 100.F);
	// Some terrain above sea level
	BOOST_CHECK(std::any_of(nodes.begin(), nodes.end(), [](const auto & n) {
		return n.height > 0;
	}));
}

BOOST_AUTO_TEST_CASE(get_height_at)
{
	// at(x,y) is index based
	nodes[at(0, 0)].height = 1;
	nodes[at(1, 0)].height = 2;
	nodes[at(0, 1)].height = 3;
	nodes[at(1, 1)].height = 4;

	// positionAt(x,y) is world based
	// Corners
	BOOST_CHECK_EQUAL(positionAt({0, 0}), glm::vec3(0, 0, 1));
	BOOST_CHECK_EQUAL(positionAt({5, 0}), glm::vec3(5, 0, 2));
	BOOST_CHECK_EQUAL(positionAt({0, 5}), glm::vec3(0, 5, 3));
	BOOST_CHECK_EQUAL(positionAt({5, 5}), glm::vec3(5, 5, 4));

	// Edge midpoints
	BOOST_CHECK_EQUAL(positionAt({0, 2.5F}), glm::vec3(0, 2.5F, 2));
	BOOST_CHECK_EQUAL(positionAt({5, 2.5F}), glm::vec3(5, 2.5F, 3));
	BOOST_CHECK_EQUAL(positionAt({2.5F, 0}), glm::vec3(2.5F, 0, 1.5F));
	BOOST_CHECK_EQUAL(positionAt({2.5F, 5}), glm::vec3(2.5F, 5, 3.5F));

	// Centre
	BOOST_CHECK_EQUAL(positionAt({2.5F, 2.5F}), glm::vec3(2.5F, 2.5F, 2.5F));
}

using TestRayTracerData = std::tuple<glm::vec2, glm::vec2, glm::vec2::value_type, std::vector<glm::vec2>>;
BOOST_TEST_DECORATOR(*boost::unit_test::timeout(1))

BOOST_DATA_TEST_CASE(raytracer,
		boost::unit_test::data::make<TestRayTracerData>({
				{{1, 2}, {4, 5}, 4,
						{
								{0, 0},
								{0, 4},
								{4, 4},
								{4, 8},
								{8, 8},
								{8, 12},
								{12, 12},
								{12, 16},
								{12, 20},
								{16, 20},
						}},
				{{-1, -1}, {-4, -5}, 5,
						{
								{-5, -5},
								{-5, -10},
								{-10, -10},
								{-10, -15},
								{-15, -15},
								{-15, -20},
								{-20, -20},
								{-20, -25},
						}},
		}),
		start, dir, scale, points)
{
	GeoData::RayTracer rt {start / scale, glm::normalize(dir)};
	for (const auto & point : points) {
		BOOST_CHECK_CLOSE_VEC(point, rt.next() * scale);
	}
}

using TestRayData = std::tuple<glm::vec3, glm::vec3, glm::vec3>;
BOOST_TEST_DECORATOR(*boost::unit_test::timeout(1))

BOOST_DATA_TEST_CASE(intersect_ray,
		boost::unit_test::data::make<TestRayData>({
				{{-1, -1, 1.0}, {1, 1, 0}, {0, 0, 1}},
				{{-1, -1, 2.5}, {1, 1, 0}, {2.5F, 2.5F, 2.5F}},
				{{-20, -20, 2.5}, {1, 1, 0}, {2.5F, 2.5F, 2.5F}},
				// outside the map looking in
				{{-205, -205, 4}, {1, 1, 0}, {5, 5, 4}},
				{{-205, 5, 4}, {1, 0, 0}, {5, 5, 4}},
				{{-205, 215, 4}, {1, -1, 0}, {5, 5, 4}},
				{{215, -205, 4}, {-1, 1, 0}, {5, 5, 4}},
				{{215, 5, 4}, {-1, 0, 0}, {5, 5, 4}},
				{{215, 215, 4}, {-1, -1, 0}, {5, 5, 4}},
				{{5, 215, 4}, {0, -1, 0}, {5, 5, 4}},
				{{5, -205, 4}, {0, 1, 0}, {5, 5, 4}},
		}),
		start, dir, pos)
{
	// at(x,y) is index based
	nodes[at(0, 0)].height = 1;
	nodes[at(1, 0)].height = 2;
	nodes[at(0, 1)].height = 3;
	nodes[at(1, 1)].height = 4;

	const auto intersect = intersectRay({start, glm::normalize(dir)});

	BOOST_CHECK_IF(has_intersect, intersect) {
		BOOST_CHECK_CLOSE_VEC(*intersect, pos);
	}
}

auto xs = boost::unit_test::data::xrange(-20.F, 0.F, 0.6F), ys = boost::unit_test::data::xrange(-20.F, 0.F, 0.7F);
auto targetsx = boost::unit_test::data::xrange(0.2F, 4.9F, 1.3F),
	 targetsy = boost::unit_test::data::xrange(0.3F, 4.9F, 1.3F);
BOOST_TEST_DECORATOR(*boost::unit_test::timeout(1))

BOOST_DATA_TEST_CASE(intersect_ray_many, xs * ys * targetsx * targetsy, x, y, targetx, targety)
{
	// at(x,y) is index based
	nodes[at(0, 0)].height = 1;
	nodes[at(1, 0)].height = 2;
	nodes[at(0, 1)].height = 3;
	nodes[at(1, 1)].height = 4;

	const glm::vec3 start {x, y, 10};
	const auto target {this->positionAt({targetx, targety})};
	const Ray ray {start, glm::normalize(target - start)};

	BOOST_TEST_CONTEXT(ray) {
		const auto intersect = intersectRay(ray);

		BOOST_CHECK_IF(has_intersect, intersect) {
			BOOST_CHECK_CLOSE_VEC(*intersect, target);
		}
	}
}

BOOST_TEST_DECORATOR(*boost::unit_test::timeout(1))

BOOST_DATA_TEST_CASE(intersect_ray_miss,
		boost::unit_test::data::make<Ray>({
				{{3, 3, 5}, {-1, -1, 0}},
				{{0, 0, 5}, {0, 0, 1}},
				{{0, 0, 5}, {0, 0, -1}},
		}),
		ray)
{
	// at(x,y) is index based
	nodes[at(0, 0)].height = 1;
	nodes[at(1, 0)].height = 2;
	nodes[at(0, 1)].height = 3;
	nodes[at(1, 1)].height = 4;

	BOOST_REQUIRE(!intersectRay(ray));
}

BOOST_AUTO_TEST_SUITE_END()