face_position_cache.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Minetest
  3. Copyright (C) 2015 Nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  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 "face_position_cache.h"
  17. #include "threading/mutex_auto_lock.h"
  18. std::unordered_map<u16, std::vector<v3s16>> FacePositionCache::cache;
  19. std::mutex FacePositionCache::cache_mutex;
  20. // Calculate the borders of a "d-radius" cube
  21. const std::vector<v3s16> &FacePositionCache::getFacePositions(u16 d)
  22. {
  23. MutexAutoLock lock(cache_mutex);
  24. std::unordered_map<u16, std::vector<v3s16>>::const_iterator it = cache.find(d);
  25. if (it != cache.end())
  26. return it->second;
  27. return generateFacePosition(d);
  28. }
  29. const std::vector<v3s16> &FacePositionCache::generateFacePosition(u16 d)
  30. {
  31. cache[d] = std::vector<v3s16>();
  32. std::vector<v3s16> &c = cache[d];
  33. if (d == 0) {
  34. c.emplace_back(0,0,0);
  35. return c;
  36. }
  37. if (d == 1) {
  38. // This is an optimized sequence of coordinates.
  39. c.emplace_back(0, 1, 0); // Top
  40. c.emplace_back(0, 0, 1); // Back
  41. c.emplace_back(-1, 0, 0); // Left
  42. c.emplace_back(1, 0, 0); // Right
  43. c.emplace_back(0, 0,-1); // Front
  44. c.emplace_back(0,-1, 0); // Bottom
  45. // 6
  46. c.emplace_back(-1, 0, 1); // Back left
  47. c.emplace_back(1, 0, 1); // Back right
  48. c.emplace_back(-1, 0,-1); // Front left
  49. c.emplace_back(1, 0,-1); // Front right
  50. c.emplace_back(-1,-1, 0); // Bottom left
  51. c.emplace_back(1,-1, 0); // Bottom right
  52. c.emplace_back(0,-1, 1); // Bottom back
  53. c.emplace_back(0,-1,-1); // Bottom front
  54. c.emplace_back(-1, 1, 0); // Top left
  55. c.emplace_back(1, 1, 0); // Top right
  56. c.emplace_back(0, 1, 1); // Top back
  57. c.emplace_back(0, 1,-1); // Top front
  58. // 18
  59. c.emplace_back(-1, 1, 1); // Top back-left
  60. c.emplace_back(1, 1, 1); // Top back-right
  61. c.emplace_back(-1, 1,-1); // Top front-left
  62. c.emplace_back(1, 1,-1); // Top front-right
  63. c.emplace_back(-1,-1, 1); // Bottom back-left
  64. c.emplace_back(1,-1, 1); // Bottom back-right
  65. c.emplace_back(-1,-1,-1); // Bottom front-left
  66. c.emplace_back(1,-1,-1); // Bottom front-right
  67. // 26
  68. return c;
  69. }
  70. // Take blocks in all sides, starting from y=0 and going +-y
  71. for (s16 y = 0; y <= d - 1; y++) {
  72. // Left and right side, including borders
  73. for (s16 z =- d; z <= d; z++) {
  74. c.emplace_back(d, y, z);
  75. c.emplace_back(-d, y, z);
  76. if (y != 0) {
  77. c.emplace_back(d, -y, z);
  78. c.emplace_back(-d, -y, z);
  79. }
  80. }
  81. // Back and front side, excluding borders
  82. for (s16 x = -d + 1; x <= d - 1; x++) {
  83. c.emplace_back(x, y, d);
  84. c.emplace_back(x, y, -d);
  85. if (y != 0) {
  86. c.emplace_back(x, -y, d);
  87. c.emplace_back(x, -y, -d);
  88. }
  89. }
  90. }
  91. // Take the bottom and top face with borders
  92. // -d < x < d, y = +-d, -d < z < d
  93. for (s16 x = -d; x <= d; x++)
  94. for (s16 z = -d; z <= d; z++) {
  95. c.emplace_back(x, -d, z);
  96. c.emplace_back(x, d, z);
  97. }
  98. return c;
  99. }