Translated using Weblate (Russian)
[oweals/minetest.git] / src / unittest / test_voxelalgorithms.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "test.h"
21
22 #include "gamedef.h"
23 #include "voxelalgorithms.h"
24 #include "util/numeric.h"
25
26 class TestVoxelAlgorithms : public TestBase {
27 public:
28         TestVoxelAlgorithms() { TestManager::registerTestModule(this); }
29         const char *getName() { return "TestVoxelAlgorithms"; }
30
31         void runTests(IGameDef *gamedef);
32
33         void testVoxelLineIterator(const NodeDefManager *ndef);
34 };
35
36 static TestVoxelAlgorithms g_test_instance;
37
38 void TestVoxelAlgorithms::runTests(IGameDef *gamedef)
39 {
40         const NodeDefManager *ndef = gamedef->getNodeDefManager();
41
42         TEST(testVoxelLineIterator, ndef);
43 }
44
45 ////////////////////////////////////////////////////////////////////////////////
46
47 void TestVoxelAlgorithms::testVoxelLineIterator(const NodeDefManager *ndef)
48 {
49         // Test some lines
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);
57         }
58         }
59         }
60         lines.emplace_back(0, 0, 0, 0, 0, 0);
61         // Test every line
62         std::vector<core::line3d<f32> >::iterator it = lines.begin();
63         for (; it < lines.end(); it++) {
64                 core::line3d<f32> l = *it;
65
66                 // Initialize test
67                 voxalgo::VoxelLineIterator iterator(l.start, l.getVector());
68
69                 //Test the first voxel
70                 v3s16 start_voxel = floatToInt(l.start, 1);
71                 UASSERT(iterator.m_current_node_pos == start_voxel);
72
73                 // Values for testing
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;
80
81                 while (iterator.hasNext()) {
82                         iterator.next();
83                         actual_nodecount++;
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));
92                         // Update old voxel
93                         old_voxel = new_voxel;
94                 }
95
96                 // Test last node
97                 UASSERT(iterator.m_current_node_pos == end_voxel);
98                 // Test node count
99                 UASSERTEQ(int, actual_nodecount, nodecount);
100         }
101 }