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