]> git.lizzy.rs Git - minetest.git/blob - src/util/areastore.cpp
Use true pitch/yaw/roll rotations without loss of precision by pgimeno (#8019)
[minetest.git] / src / util / areastore.cpp
1 /*
2 Minetest
3 Copyright (C) 2015 est31 <mtest31@outlook.com>
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 "util/areastore.h"
21 #include "util/serialize.h"
22 #include "util/container.h"
23
24 #if USE_SPATIAL
25         #include <spatialindex/SpatialIndex.h>
26         #include <spatialindex/RTree.h>
27         #include <spatialindex/Point.h>
28 #endif
29
30 #define AST_SMALLER_EQ_AS(p, q) (((p).X <= (q).X) && ((p).Y <= (q).Y) && ((p).Z <= (q).Z))
31
32 #define AST_OVERLAPS_IN_DIMENSION(amine, amaxe, b, d) \
33         (!(((amine).d > (b)->maxedge.d) || ((amaxe).d < (b)->minedge.d)))
34
35 #define AST_CONTAINS_PT(a, p) (AST_SMALLER_EQ_AS((a)->minedge, (p)) && \
36         AST_SMALLER_EQ_AS((p), (a)->maxedge))
37
38 #define AST_CONTAINS_AREA(amine, amaxe, b)         \
39         (AST_SMALLER_EQ_AS((amine), (b)->minedge) \
40         && AST_SMALLER_EQ_AS((b)->maxedge, (amaxe)))
41
42 #define AST_AREAS_OVERLAP(amine, amaxe, b)                \
43         (AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), X) && \
44         AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), Y) &&  \
45         AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), Z))
46
47
48 AreaStore *AreaStore::getOptimalImplementation()
49 {
50 #if USE_SPATIAL
51         return new SpatialAreaStore();
52 #else
53         return new VectorAreaStore();
54 #endif
55 }
56
57 const Area *AreaStore::getArea(u32 id) const
58 {
59         AreaMap::const_iterator it = areas_map.find(id);
60         if (it == areas_map.end())
61                 return nullptr;
62         return &it->second;
63 }
64
65 void AreaStore::serialize(std::ostream &os) const
66 {
67         writeU8(os, 0); // Serialisation version
68
69         // TODO: Compression?
70         writeU16(os, areas_map.size());
71         for (const auto &it : areas_map) {
72                 const Area &a = it.second;
73                 writeV3S16(os, a.minedge);
74                 writeV3S16(os, a.maxedge);
75                 writeU16(os, a.data.size());
76                 os.write(a.data.data(), a.data.size());
77         }
78 }
79
80 void AreaStore::deserialize(std::istream &is)
81 {
82         u8 ver = readU8(is);
83         if (ver != 0)
84                 throw SerializationError("Unknown AreaStore "
85                                 "serialization version!");
86
87         u16 num_areas = readU16(is);
88         for (u32 i = 0; i < num_areas; ++i) {
89                 Area a;
90                 a.minedge = readV3S16(is);
91                 a.maxedge = readV3S16(is);
92                 u16 data_len = readU16(is);
93                 char *data = new char[data_len];
94                 is.read(data, data_len);
95                 a.data = std::string(data, data_len);
96                 insertArea(&a);
97                 delete [] data;
98         }
99 }
100
101 void AreaStore::invalidateCache()
102 {
103         if (m_cache_enabled) {
104                 m_res_cache.invalidate();
105         }
106 }
107
108 void AreaStore::setCacheParams(bool enabled, u8 block_radius, size_t limit)
109 {
110         m_cache_enabled = enabled;
111         m_cacheblock_radius = MYMAX(block_radius, 16);
112         m_res_cache.setLimit(MYMAX(limit, 20));
113         invalidateCache();
114 }
115
116 void AreaStore::cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *dest)
117 {
118         AreaStore *as = (AreaStore *)data;
119         u8 r = as->m_cacheblock_radius;
120
121         // get the points at the edges of the mapblock
122         v3s16 minedge(mpos.X * r, mpos.Y * r, mpos.Z * r);
123         v3s16 maxedge(
124                 minedge.X + r - 1,
125                 minedge.Y + r - 1,
126                 minedge.Z + r - 1);
127
128         as->getAreasInArea(dest, minedge, maxedge, true);
129
130         /* infostream << "Cache miss with " << dest->size() << " areas, between ("
131                         << minedge.X << ", " << minedge.Y << ", " << minedge.Z
132                         << ") and ("
133                         << maxedge.X << ", " << maxedge.Y << ", " << maxedge.Z
134                         << ")" << std::endl; // */
135 }
136
137 void AreaStore::getAreasForPos(std::vector<Area *> *result, v3s16 pos)
138 {
139         if (m_cache_enabled) {
140                 v3s16 mblock = getContainerPos(pos, m_cacheblock_radius);
141                 const std::vector<Area *> *pre_list = m_res_cache.lookupCache(mblock);
142
143                 size_t s_p_l = pre_list->size();
144                 for (size_t i = 0; i < s_p_l; i++) {
145                         Area *b = (*pre_list)[i];
146                         if (AST_CONTAINS_PT(b, pos)) {
147                                 result->push_back(b);
148                         }
149                 }
150         } else {
151                 return getAreasForPosImpl(result, pos);
152         }
153 }
154
155
156 ////
157 // VectorAreaStore
158 ////
159
160
161 bool VectorAreaStore::insertArea(Area *a)
162 {
163         if (a->id == U32_MAX)
164                 a->id = getNextId();
165         std::pair<AreaMap::iterator, bool> res =
166                         areas_map.insert(std::make_pair(a->id, *a));
167         if (!res.second)
168                 // ID is not unique
169                 return false;
170         m_areas.push_back(&res.first->second);
171         invalidateCache();
172         return true;
173 }
174
175 bool VectorAreaStore::removeArea(u32 id)
176 {
177         AreaMap::iterator it = areas_map.find(id);
178         if (it == areas_map.end())
179                 return false;
180         Area *a = &it->second;
181         for (std::vector<Area *>::iterator v_it = m_areas.begin();
182                         v_it != m_areas.end(); ++v_it) {
183                 if (*v_it == a) {
184                         m_areas.erase(v_it);
185                         break;
186                 }
187         }
188         areas_map.erase(it);
189         invalidateCache();
190         return true;
191 }
192
193 void VectorAreaStore::getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos)
194 {
195         for (Area *area : m_areas) {
196                 if (AST_CONTAINS_PT(area, pos)) {
197                         result->push_back(area);
198                 }
199         }
200 }
201
202 void VectorAreaStore::getAreasInArea(std::vector<Area *> *result,
203                 v3s16 minedge, v3s16 maxedge, bool accept_overlap)
204 {
205         for (Area *area : m_areas) {
206                 if (accept_overlap ? AST_AREAS_OVERLAP(minedge, maxedge, area) :
207                                 AST_CONTAINS_AREA(minedge, maxedge, area)) {
208                         result->push_back(area);
209                 }
210         }
211 }
212
213 #if USE_SPATIAL
214
215 static inline SpatialIndex::Region get_spatial_region(const v3s16 minedge,
216                 const v3s16 maxedge)
217 {
218         const double p_low[] = {(double)minedge.X,
219                 (double)minedge.Y, (double)minedge.Z};
220         const double p_high[] = {(double)maxedge.X, (double)maxedge.Y,
221                 (double)maxedge.Z};
222         return SpatialIndex::Region(p_low, p_high, 3);
223 }
224
225 static inline SpatialIndex::Point get_spatial_point(const v3s16 pos)
226 {
227         const double p[] = {(double)pos.X, (double)pos.Y, (double)pos.Z};
228         return SpatialIndex::Point(p, 3);
229 }
230
231
232 bool SpatialAreaStore::insertArea(Area *a)
233 {
234         if (a->id == U32_MAX)
235                 a->id = getNextId();
236         if (!areas_map.insert(std::make_pair(a->id, *a)).second)
237                 // ID is not unique
238                 return false;
239         m_tree->insertData(0, nullptr, get_spatial_region(a->minedge, a->maxedge), a->id);
240         invalidateCache();
241         return true;
242 }
243
244 bool SpatialAreaStore::removeArea(u32 id)
245 {
246         std::map<u32, Area>::iterator itr = areas_map.find(id);
247         if (itr != areas_map.end()) {
248                 Area *a = &itr->second;
249                 bool result = m_tree->deleteData(get_spatial_region(a->minedge,
250                         a->maxedge), id);
251                 areas_map.erase(itr);
252                 invalidateCache();
253                 return result;
254         } else {
255                 return false;
256         }
257 }
258
259 void SpatialAreaStore::getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos)
260 {
261         VectorResultVisitor visitor(result, this);
262         m_tree->pointLocationQuery(get_spatial_point(pos), visitor);
263 }
264
265 void SpatialAreaStore::getAreasInArea(std::vector<Area *> *result,
266                 v3s16 minedge, v3s16 maxedge, bool accept_overlap)
267 {
268         VectorResultVisitor visitor(result, this);
269         if (accept_overlap) {
270                 m_tree->intersectsWithQuery(get_spatial_region(minedge, maxedge),
271                         visitor);
272         } else {
273                 m_tree->containsWhatQuery(get_spatial_region(minedge, maxedge), visitor);
274         }
275 }
276
277 SpatialAreaStore::~SpatialAreaStore()
278 {
279         delete m_tree;
280 }
281
282 SpatialAreaStore::SpatialAreaStore()
283 {
284         m_storagemanager =
285                 SpatialIndex::StorageManager::createNewMemoryStorageManager();
286         SpatialIndex::id_type id;
287         m_tree = SpatialIndex::RTree::createNewRTree(
288                 *m_storagemanager,
289                 .7, // Fill factor
290                 100, // Index capacity
291                 100, // Leaf capacity
292                 3, // dimension :)
293                 SpatialIndex::RTree::RV_RSTAR,
294                 id);
295 }
296
297 #endif