3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "voxelalgorithms.h"
24 #include "util/numeric.h"
26 class TestVoxelAlgorithms : public TestBase {
28 TestVoxelAlgorithms() { TestManager::registerTestModule(this); }
29 const char *getName() { return "TestVoxelAlgorithms"; }
31 void runTests(IGameDef *gamedef);
33 void testPropogateSunlight(INodeDefManager *ndef);
34 void testClearLightAndCollectSources(INodeDefManager *ndef);
35 void testVoxelLineIterator(INodeDefManager *ndef);
38 static TestVoxelAlgorithms g_test_instance;
40 void TestVoxelAlgorithms::runTests(IGameDef *gamedef)
42 INodeDefManager *ndef = gamedef->getNodeDefManager();
44 TEST(testPropogateSunlight, ndef);
45 TEST(testClearLightAndCollectSources, ndef);
46 TEST(testVoxelLineIterator, ndef);
49 ////////////////////////////////////////////////////////////////////////////////
51 void TestVoxelAlgorithms::testPropogateSunlight(INodeDefManager *ndef)
55 for (u16 z = 0; z < 3; z++)
56 for (u16 y = 0; y < 3; y++)
57 for (u16 x = 0; x < 3; x++) {
59 v.setNodeNoRef(p, MapNode(CONTENT_AIR));
62 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
65 std::set<v3s16> light_sources;
66 voxalgo::setLight(v, a, 0, ndef);
67 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
68 v, a, true, light_sources, ndef);
69 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
70 UASSERT(res.bottom_sunlight_valid == true);
71 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
75 v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
78 std::set<v3s16> light_sources;
79 voxalgo::setLight(v, a, 0, ndef);
80 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
81 v, a, true, light_sources, ndef);
82 UASSERT(res.bottom_sunlight_valid == true);
83 UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
88 std::set<v3s16> light_sources;
89 voxalgo::setLight(v, a, 0, ndef);
90 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
91 v, a, false, light_sources, ndef);
92 UASSERT(res.bottom_sunlight_valid == true);
93 UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
97 v.setNodeNoRef(v3s16(1,3,2), MapNode(t_CONTENT_STONE));
100 std::set<v3s16> light_sources;
101 voxalgo::setLight(v, a, 0, ndef);
102 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
103 v, a, true, light_sources, ndef);
104 UASSERT(res.bottom_sunlight_valid == true);
105 UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
110 std::set<v3s16> light_sources;
111 voxalgo::setLight(v, a, 0, ndef);
112 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
113 v, a, false, light_sources, ndef);
114 UASSERT(res.bottom_sunlight_valid == true);
115 UASSERT(v.getNode(v3s16(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
120 MapNode n(CONTENT_AIR);
121 n.setLight(LIGHTBANK_DAY, 10, ndef);
122 v.setNodeNoRef(v3s16(1,-1,2), n);
126 std::set<v3s16> light_sources;
127 voxalgo::setLight(v, a, 0, ndef);
128 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
129 v, a, true, light_sources, ndef);
130 UASSERT(res.bottom_sunlight_valid == true);
134 std::set<v3s16> light_sources;
135 voxalgo::setLight(v, a, 0, ndef);
136 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
137 v, a, false, light_sources, ndef);
138 UASSERT(res.bottom_sunlight_valid == true);
142 MapNode n(CONTENT_AIR);
143 n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
144 v.setNodeNoRef(v3s16(1,-1,2), n);
148 std::set<v3s16> light_sources;
149 voxalgo::setLight(v, a, 0, ndef);
150 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
151 v, a, true, light_sources, ndef);
152 UASSERT(res.bottom_sunlight_valid == false);
156 std::set<v3s16> light_sources;
157 voxalgo::setLight(v, a, 0, ndef);
158 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
159 v, a, false, light_sources, ndef);
160 UASSERT(res.bottom_sunlight_valid == false);
163 v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
166 std::set<v3s16> light_sources;
167 voxalgo::setLight(v, a, 0, ndef);
168 voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
169 v, a, true, light_sources, ndef);
170 UASSERT(res.bottom_sunlight_valid == true);
174 void TestVoxelAlgorithms::testClearLightAndCollectSources(INodeDefManager *ndef)
178 for (u16 z = 0; z < 3; z++)
179 for (u16 y = 0; y < 3; y++)
180 for (u16 x = 0; x < 3; x++) {
182 v.setNode(p, MapNode(CONTENT_AIR));
185 VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
186 v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
187 v.setNodeNoRef(v3s16(1,1,1), MapNode(t_CONTENT_TORCH));
190 MapNode n(CONTENT_AIR);
191 n.setLight(LIGHTBANK_DAY, 1, ndef);
192 v.setNode(v3s16(1,1,2), n);
196 std::set<v3s16> light_sources;
197 std::map<v3s16, u8> unlight_from;
198 voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
199 ndef, light_sources, unlight_from);
200 //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
201 UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef) == 0);
202 UASSERT(light_sources.find(v3s16(1,1,1)) != light_sources.end());
203 UASSERT(light_sources.size() == 1);
204 UASSERT(unlight_from.find(v3s16(1,1,2)) != unlight_from.end());
205 UASSERT(unlight_from.size() == 1);
209 void TestVoxelAlgorithms::testVoxelLineIterator(INodeDefManager *ndef)
212 // Do not test lines that start or end on the border of
213 // two voxels as rounding errors can make the test fail!
214 std::vector<core::line3d<f32> > lines;
215 for (f32 x = -9.1; x < 9; x += 3.124) {
216 for (f32 y = -9.2; y < 9; y += 3.123) {
217 for (f32 z = -9.3; z < 9; z += 3.122) {
218 lines.push_back(core::line3d<f32>(-x, -y, -z, x, y, z));
222 lines.push_back(core::line3d<f32>(0, 0, 0, 0, 0, 0));
224 std::vector<core::line3d<f32> >::iterator it = lines.begin();
225 for (; it < lines.end(); it++) {
226 core::line3d<f32> l = *it;
229 voxalgo::VoxelLineIterator iterator(l.start, l.getVector());
231 //Test the first voxel
232 v3s16 start_voxel = floatToInt(l.start, 1);
233 UASSERT(iterator.m_current_node_pos == start_voxel);
235 // Values for testing
236 v3s16 end_voxel = floatToInt(l.end, 1);
237 v3s16 voxel_vector = end_voxel - start_voxel;
238 int nodecount = abs(voxel_vector.X) + abs(voxel_vector.Y)
239 + abs(voxel_vector.Z);
240 int actual_nodecount = 0;
241 v3s16 old_voxel = iterator.m_current_node_pos;
243 while (iterator.hasNext()) {
246 v3s16 new_voxel = iterator.m_current_node_pos;
247 // This must be a neighbor of the old voxel
248 UASSERTEQ(f32, (new_voxel - old_voxel).getLengthSQ(), 1);
249 // The line must intersect with the voxel
250 v3f voxel_center = intToFloat(iterator.m_current_node_pos, 1);
251 aabb3f box(voxel_center - v3f(0.5, 0.5, 0.5),
252 voxel_center + v3f(0.5, 0.5, 0.5));
253 UASSERT(box.intersectsWithLine(l));
255 old_voxel = new_voxel;
259 UASSERT(iterator.m_current_node_pos == end_voxel);
261 UASSERTEQ(int, actual_nodecount, nodecount);