test_voxelalgorithms.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "test.h"
  17. #include "gamedef.h"
  18. #include "voxelalgorithms.h"
  19. #include "util/numeric.h"
  20. class TestVoxelAlgorithms : public TestBase {
  21. public:
  22. TestVoxelAlgorithms() { TestManager::registerTestModule(this); }
  23. const char *getName() { return "TestVoxelAlgorithms"; }
  24. void runTests(IGameDef *gamedef);
  25. void testVoxelLineIterator(const NodeDefManager *ndef);
  26. };
  27. static TestVoxelAlgorithms g_test_instance;
  28. void TestVoxelAlgorithms::runTests(IGameDef *gamedef)
  29. {
  30. const NodeDefManager *ndef = gamedef->getNodeDefManager();
  31. TEST(testVoxelLineIterator, ndef);
  32. }
  33. ////////////////////////////////////////////////////////////////////////////////
  34. void TestVoxelAlgorithms::testVoxelLineIterator(const NodeDefManager *ndef)
  35. {
  36. // Test some lines
  37. // Do not test lines that start or end on the border of
  38. // two voxels as rounding errors can make the test fail!
  39. std::vector<core::line3d<f32> > lines;
  40. for (f32 x = -9.1; x < 9; x += 3.124) {
  41. for (f32 y = -9.2; y < 9; y += 3.123) {
  42. for (f32 z = -9.3; z < 9; z += 3.122) {
  43. lines.emplace_back(-x, -y, -z, x, y, z);
  44. }
  45. }
  46. }
  47. lines.emplace_back(0, 0, 0, 0, 0, 0);
  48. // Test every line
  49. std::vector<core::line3d<f32> >::iterator it = lines.begin();
  50. for (; it < lines.end(); it++) {
  51. core::line3d<f32> l = *it;
  52. // Initialize test
  53. voxalgo::VoxelLineIterator iterator(l.start, l.getVector());
  54. //Test the first voxel
  55. v3s16 start_voxel = floatToInt(l.start, 1);
  56. UASSERT(iterator.m_current_node_pos == start_voxel);
  57. // Values for testing
  58. v3s16 end_voxel = floatToInt(l.end, 1);
  59. v3s16 voxel_vector = end_voxel - start_voxel;
  60. int nodecount = abs(voxel_vector.X) + abs(voxel_vector.Y)
  61. + abs(voxel_vector.Z);
  62. int actual_nodecount = 0;
  63. v3s16 old_voxel = iterator.m_current_node_pos;
  64. while (iterator.hasNext()) {
  65. iterator.next();
  66. actual_nodecount++;
  67. v3s16 new_voxel = iterator.m_current_node_pos;
  68. // This must be a neighbor of the old voxel
  69. UASSERTEQ(f32, (new_voxel - old_voxel).getLengthSQ(), 1);
  70. // The line must intersect with the voxel
  71. v3f voxel_center = intToFloat(iterator.m_current_node_pos, 1);
  72. aabb3f box(voxel_center - v3f(0.5, 0.5, 0.5),
  73. voxel_center + v3f(0.5, 0.5, 0.5));
  74. UASSERT(box.intersectsWithLine(l));
  75. // Update old voxel
  76. old_voxel = new_voxel;
  77. }
  78. // Test last node
  79. UASSERT(iterator.m_current_node_pos == end_voxel);
  80. // Test node count
  81. UASSERTEQ(int, actual_nodecount, nodecount);
  82. }
  83. }