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 testVoxelLineIterator(const NodeDefManager *ndef);
36 static TestVoxelAlgorithms g_test_instance;
38 void TestVoxelAlgorithms::runTests(IGameDef *gamedef)
40 const NodeDefManager *ndef = gamedef->getNodeDefManager();
42 TEST(testVoxelLineIterator, ndef);
45 ////////////////////////////////////////////////////////////////////////////////
47 void TestVoxelAlgorithms::testVoxelLineIterator(const NodeDefManager *ndef)
50 // Do not test lines that start or end on the border of
51 // two voxels as rounding errors can make the test fail!
52 std::vector<core::line3d<f32> > lines;
53 for (f32 x = -9.1; x < 9; x += 3.124) {
54 for (f32 y = -9.2; y < 9; y += 3.123) {
55 for (f32 z = -9.3; z < 9; z += 3.122) {
56 lines.emplace_back(-x, -y, -z, x, y, z);
60 lines.emplace_back(0, 0, 0, 0, 0, 0);
62 std::vector<core::line3d<f32> >::iterator it = lines.begin();
63 for (; it < lines.end(); it++) {
64 core::line3d<f32> l = *it;
67 voxalgo::VoxelLineIterator iterator(l.start, l.getVector());
69 //Test the first voxel
70 v3s16 start_voxel = floatToInt(l.start, 1);
71 UASSERT(iterator.m_current_node_pos == start_voxel);
74 v3s16 end_voxel = floatToInt(l.end, 1);
75 v3s16 voxel_vector = end_voxel - start_voxel;
76 int nodecount = abs(voxel_vector.X) + abs(voxel_vector.Y)
77 + abs(voxel_vector.Z);
78 int actual_nodecount = 0;
79 v3s16 old_voxel = iterator.m_current_node_pos;
81 while (iterator.hasNext()) {
84 v3s16 new_voxel = iterator.m_current_node_pos;
85 // This must be a neighbor of the old voxel
86 UASSERTEQ(f32, (new_voxel - old_voxel).getLengthSQ(), 1);
87 // The line must intersect with the voxel
88 v3f voxel_center = intToFloat(iterator.m_current_node_pos, 1);
89 aabb3f box(voxel_center - v3f(0.5, 0.5, 0.5),
90 voxel_center + v3f(0.5, 0.5, 0.5));
91 UASSERT(box.intersectsWithLine(l));
93 old_voxel = new_voxel;
97 UASSERT(iterator.m_current_node_pos == end_voxel);
99 UASSERTEQ(int, actual_nodecount, nodecount);