Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
areastore.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2015 est31 <mtest31@outlook.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22#include "irr_v3d.h"
23#include "noise.h" // for PcgRandom
24#include <map>
25#include <list>
26#include <vector>
27#include <istream>
28#include "util/container.h"
29#include "util/numeric.h"
30#ifndef ANDROID
31 #include "cmake_config.h"
32#endif
33#if USE_SPATIAL
34 #include <spatialindex/SpatialIndex.h>
35 #include "util/serialize.h"
36#endif
37
38
39struct Area {
40 Area(u32 area_id) : id(area_id) {}
41
42 Area(const v3s16 &mine, const v3s16 &maxe, u32 area_id = U32_MAX) :
43 id(area_id), minedge(mine), maxedge(maxe)
44 {
46 }
47
48 u32 id;
50 std::string data;
51};
52
53
54class AreaStore {
55public:
57 m_res_cache(1000, &cacheMiss, this)
58 {}
59
60 virtual ~AreaStore() = default;
61
63
64 virtual void reserve(size_t count) {};
65 size_t size() const { return areas_map.size(); }
66
70 virtual bool insertArea(Area *a) = 0;
71
74 virtual bool removeArea(u32 id) = 0;
75
78 void getAreasForPos(std::vector<Area *> *result, v3s16 pos);
79
83 virtual void getAreasInArea(std::vector<Area *> *result,
84 v3s16 minedge, v3s16 maxedge, bool accept_overlap) = 0;
85
87 void setCacheParams(bool enabled, u8 block_radius, size_t limit);
88
91 const Area *getArea(u32 id) const;
92
94 void serialize(std::ostream &is) const;
95
100 void deserialize(std::istream &is);
101
102protected:
105 void invalidateCache();
106
109 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0;
110
112 u32 getNextId() const;
113
114 // Note: This can't be an unordered_map, since all
115 // references would be invalidated on rehash.
116 typedef std::map<u32, Area> AreaMap;
118
119private:
121 static void cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *dest);
122
123 bool m_cache_enabled = true;
128};
129
130
132public:
133 virtual void reserve(size_t count) { m_areas.reserve(count); }
134 virtual bool insertArea(Area *a);
135 virtual bool removeArea(u32 id);
136 virtual void getAreasInArea(std::vector<Area *> *result,
137 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
138
139protected:
140 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
141
142private:
143 std::vector<Area *> m_areas;
144};
145
146
147#if USE_SPATIAL
148
150public:
152 virtual ~SpatialAreaStore();
153
154 virtual bool insertArea(Area *a);
155 virtual bool removeArea(u32 id);
156 virtual void getAreasInArea(std::vector<Area *> *result,
157 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
158
159protected:
160 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
161
162private:
163 SpatialIndex::ISpatialIndex *m_tree = nullptr;
164 SpatialIndex::IStorageManager *m_storagemanager = nullptr;
165
166 class VectorResultVisitor : public SpatialIndex::IVisitor {
167 public:
168 VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store) :
169 m_store(store),
170 m_result(result)
171 {}
173
174 virtual void visitNode(const SpatialIndex::INode &in) {}
175
176 virtual void visitData(const SpatialIndex::IData &in)
177 {
178 u32 id = in.getIdentifier();
179
180 std::map<u32, Area>::iterator itr = m_store->areas_map.find(id);
181 assert(itr != m_store->areas_map.end());
182 m_result->push_back(&itr->second);
183 }
184
185 virtual void visitData(std::vector<const SpatialIndex::IData *> &v)
186 {
187 for (size_t i = 0; i < v.size(); i++)
188 visitData(*(v[i]));
189 }
190
191 private:
193 std::vector<Area *> *m_result = nullptr;
194 };
195};
196
197#endif // USE_SPATIAL
Definition: areastore.h:54
AreaMap areas_map
Definition: areastore.h:117
AreaStore()
Definition: areastore.h:56
virtual void reserve(size_t count)
Definition: areastore.h:64
virtual void getAreasForPosImpl(std::vector< Area * > *result, v3s16 pos)=0
Implementation of getAreasForPos.
void setCacheParams(bool enabled, u8 block_radius, size_t limit)
Sets cache parameters.
Definition: areastore.cpp:139
static void cacheMiss(void *data, const v3s16 &mpos, std::vector< Area * > *dest)
Called by the cache when a value isn't found in the cache.
Definition: areastore.cpp:147
void getAreasForPos(std::vector< Area * > *result, v3s16 pos)
Finds areas that the passed position is contained in.
Definition: areastore.cpp:168
u32 getNextId() const
Returns the next area ID and increments it.
Definition: areastore.cpp:126
static AreaStore * getOptimalImplementation()
Definition: areastore.cpp:48
virtual ~AreaStore()=default
std::map< u32, Area > AreaMap
Definition: areastore.h:116
virtual bool insertArea(Area *a)=0
Add an area to the store.
const Area * getArea(u32 id) const
Returns a pointer to the area coresponding to the passed ID, or NULL if it doesn't exist.
Definition: areastore.cpp:57
LRUCache< v3s16, std::vector< Area * > > m_res_cache
Definition: areastore.h:127
virtual bool removeArea(u32 id)=0
Removes an area from the store by ID.
void serialize(std::ostream &is) const
Serializes the store's areas to a binary ostream.
Definition: areastore.cpp:65
virtual void getAreasInArea(std::vector< Area * > *result, v3s16 minedge, v3s16 maxedge, bool accept_overlap)=0
Finds areas that are completely contained inside the area defined by the passed edges.
void deserialize(std::istream &is)
Deserializes the Areas from a binary istream.
Definition: areastore.cpp:89
u8 m_cacheblock_radius
Range, in nodes, of the getAreasForPos cache.
Definition: areastore.h:126
void invalidateCache()
Invalidates the getAreasForPos cache.
Definition: areastore.cpp:119
size_t size() const
Definition: areastore.h:65
bool m_cache_enabled
Definition: areastore.h:123
Definition: container.h:255
Definition: areastore.h:166
~VectorResultVisitor()
Definition: areastore.h:172
virtual void visitData(const SpatialIndex::IData &in)
Definition: areastore.h:176
virtual void visitNode(const SpatialIndex::INode &in)
Definition: areastore.h:174
VectorResultVisitor(std::vector< Area * > *result, SpatialAreaStore *store)
Definition: areastore.h:168
virtual void visitData(std::vector< const SpatialIndex::IData * > &v)
Definition: areastore.h:185
SpatialAreaStore * m_store
Definition: areastore.h:192
std::vector< Area * > * m_result
Definition: areastore.h:193
Definition: areastore.h:149
SpatialIndex::ISpatialIndex * m_tree
Definition: areastore.h:163
virtual bool removeArea(u32 id)
Removes an area from the store by ID.
Definition: areastore.cpp:275
virtual ~SpatialAreaStore()
Definition: areastore.cpp:308
virtual void getAreasForPosImpl(std::vector< Area * > *result, v3s16 pos)
Implementation of getAreasForPos.
Definition: areastore.cpp:290
virtual void getAreasInArea(std::vector< Area * > *result, v3s16 minedge, v3s16 maxedge, bool accept_overlap)
Finds areas that are completely contained inside the area defined by the passed edges.
Definition: areastore.cpp:296
virtual bool insertArea(Area *a)
Add an area to the store.
Definition: areastore.cpp:263
SpatialIndex::IStorageManager * m_storagemanager
Definition: areastore.h:164
SpatialAreaStore()
Definition: areastore.cpp:314
Definition: areastore.h:131
virtual void getAreasInArea(std::vector< Area * > *result, v3s16 minedge, v3s16 maxedge, bool accept_overlap)
Finds areas that are completely contained inside the area defined by the passed edges.
Definition: areastore.cpp:233
virtual bool insertArea(Area *a)
Add an area to the store.
Definition: areastore.cpp:192
virtual void getAreasForPosImpl(std::vector< Area * > *result, v3s16 pos)
Implementation of getAreasForPos.
Definition: areastore.cpp:224
virtual void reserve(size_t count)
Definition: areastore.h:133
virtual bool removeArea(u32 id)
Removes an area from the store by ID.
Definition: areastore.cpp:206
std::vector< Area * > m_areas
Definition: areastore.h:143
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
#define U32_MAX
Definition: irrlichttypes.h:44
void sortBoxVerticies(v3s16 &p1, v3s16 &p2)
Definition: numeric.h:130
Definition: areastore.h:39
v3s16 maxedge
Definition: areastore.h:49
u32 id
Definition: areastore.h:48
Area(u32 area_id)
Definition: areastore.h:40
std::string data
Definition: areastore.h:50
Area(const v3s16 &mine, const v3s16 &maxe, u32 area_id=U32_MAX)
Definition: areastore.h:42
v3s16 minedge
Definition: areastore.h:49