3 Copyright (C) 2015 Nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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.
20 #include "face_position_cache.h"
21 #include "threading/mutex_auto_lock.h"
24 std::unordered_map<u16, std::vector<v3s16>> FacePositionCache::cache;
25 std::mutex FacePositionCache::cache_mutex;
27 // Calculate the borders of a "d-radius" cube
28 const std::vector<v3s16> &FacePositionCache::getFacePositions(u16 d)
30 MutexAutoLock lock(cache_mutex);
31 std::unordered_map<u16, std::vector<v3s16>>::const_iterator it = cache.find(d);
32 if (it != cache.end())
35 return generateFacePosition(d);
38 const std::vector<v3s16> &FacePositionCache::generateFacePosition(u16 d)
40 cache[d] = std::vector<v3s16>();
41 std::vector<v3s16> &c = cache[d];
43 c.push_back(v3s16(0,0,0));
47 // This is an optimized sequence of coordinates.
48 c.push_back(v3s16( 0, 1, 0)); // Top
49 c.push_back(v3s16( 0, 0, 1)); // Back
50 c.push_back(v3s16(-1, 0, 0)); // Left
51 c.push_back(v3s16( 1, 0, 0)); // Right
52 c.push_back(v3s16( 0, 0,-1)); // Front
53 c.push_back(v3s16( 0,-1, 0)); // Bottom
55 c.push_back(v3s16(-1, 0, 1)); // Back left
56 c.push_back(v3s16( 1, 0, 1)); // Back right
57 c.push_back(v3s16(-1, 0,-1)); // Front left
58 c.push_back(v3s16( 1, 0,-1)); // Front right
59 c.push_back(v3s16(-1,-1, 0)); // Bottom left
60 c.push_back(v3s16( 1,-1, 0)); // Bottom right
61 c.push_back(v3s16( 0,-1, 1)); // Bottom back
62 c.push_back(v3s16( 0,-1,-1)); // Bottom front
63 c.push_back(v3s16(-1, 1, 0)); // Top left
64 c.push_back(v3s16( 1, 1, 0)); // Top right
65 c.push_back(v3s16( 0, 1, 1)); // Top back
66 c.push_back(v3s16( 0, 1,-1)); // Top front
68 c.push_back(v3s16(-1, 1, 1)); // Top back-left
69 c.push_back(v3s16( 1, 1, 1)); // Top back-right
70 c.push_back(v3s16(-1, 1,-1)); // Top front-left
71 c.push_back(v3s16( 1, 1,-1)); // Top front-right
72 c.push_back(v3s16(-1,-1, 1)); // Bottom back-left
73 c.push_back(v3s16( 1,-1, 1)); // Bottom back-right
74 c.push_back(v3s16(-1,-1,-1)); // Bottom front-left
75 c.push_back(v3s16( 1,-1,-1)); // Bottom front-right
80 // Take blocks in all sides, starting from y=0 and going +-y
81 for (s16 y = 0; y <= d - 1; y++) {
82 // Left and right side, including borders
83 for (s16 z =- d; z <= d; z++) {
84 c.push_back(v3s16( d, y, z));
85 c.push_back(v3s16(-d, y, z));
87 c.push_back(v3s16( d, -y, z));
88 c.push_back(v3s16(-d, -y, z));
91 // Back and front side, excluding borders
92 for (s16 x = -d + 1; x <= d - 1; x++) {
93 c.push_back(v3s16(x, y, d));
94 c.push_back(v3s16(x, y, -d));
96 c.push_back(v3s16(x, -y, d));
97 c.push_back(v3s16(x, -y, -d));
102 // Take the bottom and top face with borders
103 // -d < x < d, y = +-d, -d < z < d
104 for (s16 x = -d; x <= d; x++)
105 for (s16 z = -d; z <= d; z++) {
106 c.push_back(v3s16(x, -d, z));
107 c.push_back(v3s16(x, d, z));