test_voxelalgorithms.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 testPropogateSunlight(INodeDefManager *ndef);
  26. void testClearLightAndCollectSources(INodeDefManager *ndef);
  27. void testVoxelLineIterator(INodeDefManager *ndef);
  28. };
  29. static TestVoxelAlgorithms g_test_instance;
  30. void TestVoxelAlgorithms::runTests(IGameDef *gamedef)
  31. {
  32. INodeDefManager *ndef = gamedef->getNodeDefManager();
  33. TEST(testPropogateSunlight, ndef);
  34. TEST(testClearLightAndCollectSources, ndef);
  35. TEST(testVoxelLineIterator, ndef);
  36. }
  37. ////////////////////////////////////////////////////////////////////////////////
  38. void TestVoxelAlgorithms::testPropogateSunlight(INodeDefManager *ndef)
  39. {
  40. VoxelManipulator v;
  41. for (u16 z = 0; z < 3; z++)
  42. for (u16 y = 0; y < 3; y++)
  43. for (u16 x = 0; x < 3; x++) {
  44. v3s16 p(x,y,z);
  45. v.setNodeNoRef(p, MapNode(CONTENT_AIR));
  46. }
  47. VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
  48. {
  49. std::set<v3s16> light_sources;
  50. voxalgo::setLight(v, a, 0, ndef);
  51. voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
  52. v, a, true, light_sources, ndef);
  53. //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
  54. UASSERT(res.bottom_sunlight_valid == true);
  55. UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
  56. == LIGHT_SUN);
  57. }
  58. v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
  59. {
  60. std::set<v3s16> light_sources;
  61. voxalgo::setLight(v, a, 0, ndef);
  62. voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
  63. v, a, true, light_sources, ndef);
  64. UASSERT(res.bottom_sunlight_valid == true);
  65. UASSERT(v.getNode(v3s16(1,1,1)).getLight(LIGHTBANK_DAY, ndef)
  66. == LIGHT_SUN);
  67. }
  68. {
  69. std::set<v3s16> light_sources;
  70. voxalgo::setLight(v, a, 0, ndef);
  71. voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
  72. v, a, false, light_sources, ndef);
  73. UASSERT(res.bottom_sunlight_valid == true);
  74. UASSERT(v.getNode(v3s16(2,0,2)).getLight(LIGHTBANK_DAY, ndef)
  75. == 0);
  76. }
  77. v.setNodeNoRef(v3s16(1,3,2), MapNode(t_CONTENT_STONE));
  78. {
  79. std::set<v3s16> light_sources;
  80. voxalgo::setLight(v, a, 0, ndef);
  81. voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
  82. v, a, true, light_sources, ndef);
  83. UASSERT(res.bottom_sunlight_valid == true);
  84. UASSERT(v.getNode(v3s16(1,1,2)).getLight(LIGHTBANK_DAY, ndef)
  85. == 0);
  86. }
  87. {
  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(1,0,2)).getLight(LIGHTBANK_DAY, ndef)
  94. == 0);
  95. }
  96. {
  97. MapNode n(CONTENT_AIR);
  98. n.setLight(LIGHTBANK_DAY, 10, ndef);
  99. v.setNodeNoRef(v3s16(1,-1,2), n);
  100. }
  101. {
  102. std::set<v3s16> light_sources;
  103. voxalgo::setLight(v, a, 0, ndef);
  104. voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
  105. v, a, true, light_sources, ndef);
  106. UASSERT(res.bottom_sunlight_valid == true);
  107. }
  108. {
  109. std::set<v3s16> light_sources;
  110. voxalgo::setLight(v, a, 0, ndef);
  111. voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
  112. v, a, false, light_sources, ndef);
  113. UASSERT(res.bottom_sunlight_valid == true);
  114. }
  115. {
  116. MapNode n(CONTENT_AIR);
  117. n.setLight(LIGHTBANK_DAY, LIGHT_SUN, ndef);
  118. v.setNodeNoRef(v3s16(1,-1,2), n);
  119. }
  120. {
  121. std::set<v3s16> light_sources;
  122. voxalgo::setLight(v, a, 0, ndef);
  123. voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
  124. v, a, true, light_sources, ndef);
  125. UASSERT(res.bottom_sunlight_valid == false);
  126. }
  127. {
  128. std::set<v3s16> light_sources;
  129. voxalgo::setLight(v, a, 0, ndef);
  130. voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
  131. v, a, false, light_sources, ndef);
  132. UASSERT(res.bottom_sunlight_valid == false);
  133. }
  134. v.setNodeNoRef(v3s16(1,3,2), MapNode(CONTENT_IGNORE));
  135. {
  136. std::set<v3s16> light_sources;
  137. voxalgo::setLight(v, a, 0, ndef);
  138. voxalgo::SunlightPropagateResult res = voxalgo::propagateSunlight(
  139. v, a, true, light_sources, ndef);
  140. UASSERT(res.bottom_sunlight_valid == true);
  141. }
  142. }
  143. void TestVoxelAlgorithms::testClearLightAndCollectSources(INodeDefManager *ndef)
  144. {
  145. VoxelManipulator v;
  146. for (u16 z = 0; z < 3; z++)
  147. for (u16 y = 0; y < 3; y++)
  148. for (u16 x = 0; x < 3; x++) {
  149. v3s16 p(x,y,z);
  150. v.setNode(p, MapNode(CONTENT_AIR));
  151. }
  152. VoxelArea a(v3s16(0,0,0), v3s16(2,2,2));
  153. v.setNodeNoRef(v3s16(0,0,0), MapNode(t_CONTENT_STONE));
  154. v.setNodeNoRef(v3s16(1,1,1), MapNode(t_CONTENT_TORCH));
  155. {
  156. MapNode n(CONTENT_AIR);
  157. n.setLight(LIGHTBANK_DAY, 1, ndef);
  158. v.setNode(v3s16(1,1,2), n);
  159. }
  160. {
  161. std::set<v3s16> light_sources;
  162. std::map<v3s16, u8> unlight_from;
  163. voxalgo::clearLightAndCollectSources(v, a, LIGHTBANK_DAY,
  164. ndef, light_sources, unlight_from);
  165. //v.print(dstream, ndef, VOXELPRINT_LIGHT_DAY);
  166. UASSERT(v.getNode(v3s16(0,1,1)).getLight(LIGHTBANK_DAY, ndef) == 0);
  167. UASSERT(light_sources.find(v3s16(1,1,1)) != light_sources.end());
  168. UASSERT(light_sources.size() == 1);
  169. UASSERT(unlight_from.find(v3s16(1,1,2)) != unlight_from.end());
  170. UASSERT(unlight_from.size() == 1);
  171. }
  172. }
  173. void TestVoxelAlgorithms::testVoxelLineIterator(INodeDefManager *ndef)
  174. {
  175. // Test some lines
  176. // Do not test lines that start or end on the border of
  177. // two voxels as rounding errors can make the test fail!
  178. std::vector<core::line3d<f32> > lines;
  179. for (f32 x = -9.1; x < 9; x += 3.124) {
  180. for (f32 y = -9.2; y < 9; y += 3.123) {
  181. for (f32 z = -9.3; z < 9; z += 3.122) {
  182. lines.emplace_back(-x, -y, -z, x, y, z);
  183. }
  184. }
  185. }
  186. lines.emplace_back(0, 0, 0, 0, 0, 0);
  187. // Test every line
  188. std::vector<core::line3d<f32> >::iterator it = lines.begin();
  189. for (; it < lines.end(); it++) {
  190. core::line3d<f32> l = *it;
  191. // Initialize test
  192. voxalgo::VoxelLineIterator iterator(l.start, l.getVector());
  193. //Test the first voxel
  194. v3s16 start_voxel = floatToInt(l.start, 1);
  195. UASSERT(iterator.m_current_node_pos == start_voxel);
  196. // Values for testing
  197. v3s16 end_voxel = floatToInt(l.end, 1);
  198. v3s16 voxel_vector = end_voxel - start_voxel;
  199. int nodecount = abs(voxel_vector.X) + abs(voxel_vector.Y)
  200. + abs(voxel_vector.Z);
  201. int actual_nodecount = 0;
  202. v3s16 old_voxel = iterator.m_current_node_pos;
  203. while (iterator.hasNext()) {
  204. iterator.next();
  205. actual_nodecount++;
  206. v3s16 new_voxel = iterator.m_current_node_pos;
  207. // This must be a neighbor of the old voxel
  208. UASSERTEQ(f32, (new_voxel - old_voxel).getLengthSQ(), 1);
  209. // The line must intersect with the voxel
  210. v3f voxel_center = intToFloat(iterator.m_current_node_pos, 1);
  211. aabb3f box(voxel_center - v3f(0.5, 0.5, 0.5),
  212. voxel_center + v3f(0.5, 0.5, 0.5));
  213. UASSERT(box.intersectsWithLine(l));
  214. // Update old voxel
  215. old_voxel = new_voxel;
  216. }
  217. // Test last node
  218. UASSERT(iterator.m_current_node_pos == end_voxel);
  219. // Test node count
  220. UASSERTEQ(int, actual_nodecount, nodecount);
  221. }
  222. }