Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
areastore.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2015 est31 <mtest31@outlook.com>
4
5#pragma once
6
7#include "irr_v3d.h"
8#include "noise.h" // for PcgRandom
9#include <map>
10#include <list>
11#include <vector>
12#include <istream>
13#include "util/container.h"
14#include "util/numeric.h"
15#ifndef ANDROID
16 #include "cmake_config.h"
17#endif
18#if USE_SPATIAL
19 #include <spatialindex/SpatialIndex.h>
20 #include "util/serialize.h"
21#endif
22
23
24struct Area {
25 Area(u32 area_id) : id(area_id) {}
26
27 Area(const v3s16 &mine, const v3s16 &maxe, u32 area_id = U32_MAX) :
28 id(area_id), minedge(mine), maxedge(maxe)
29 {
31 }
32
33 u32 id;
35 std::string data;
36};
37
38
39class AreaStore {
40public:
42 m_res_cache(1000, &cacheMiss, this)
43 {}
44
45 virtual ~AreaStore() = default;
46
48
49 virtual void reserve(size_t count) {};
50 size_t size() const { return areas_map.size(); }
51
55 virtual bool insertArea(Area *a) = 0;
56
59 virtual bool removeArea(u32 id) = 0;
60
63 void getAreasForPos(std::vector<Area *> *result, v3s16 pos);
64
68 virtual void getAreasInArea(std::vector<Area *> *result,
69 v3s16 minedge, v3s16 maxedge, bool accept_overlap) = 0;
70
72 void setCacheParams(bool enabled, u8 block_radius, size_t limit);
73
76 const Area *getArea(u32 id) const;
77
79 void serialize(std::ostream &is) const;
80
85 void deserialize(std::istream &is);
86
87protected:
90 void invalidateCache();
91
94 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0;
95
97 u32 getNextId() const;
98
99 // Note: This can't be an unordered_map, since all
100 // references would be invalidated on rehash.
101 typedef std::map<u32, Area> AreaMap;
103
104private:
106 static void cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *dest);
107
108 bool m_cache_enabled = true;
113};
114
115
117public:
118 virtual void reserve(size_t count) { m_areas.reserve(count); }
119 virtual bool insertArea(Area *a);
120 virtual bool removeArea(u32 id);
121 virtual void getAreasInArea(std::vector<Area *> *result,
122 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
123
124protected:
125 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
126
127private:
128 std::vector<Area *> m_areas;
129};
130
131
132#if USE_SPATIAL
133
135public:
137 virtual ~SpatialAreaStore();
138
139 virtual bool insertArea(Area *a);
140 virtual bool removeArea(u32 id);
141 virtual void getAreasInArea(std::vector<Area *> *result,
142 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
143
144protected:
145 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
146
147private:
148 SpatialIndex::ISpatialIndex *m_tree = nullptr;
149 SpatialIndex::IStorageManager *m_storagemanager = nullptr;
150
151 class VectorResultVisitor : public SpatialIndex::IVisitor {
152 public:
153 VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store) :
154 m_store(store),
155 m_result(result)
156 {}
158
159 virtual void visitNode(const SpatialIndex::INode &in) {}
160
161 virtual void visitData(const SpatialIndex::IData &in)
162 {
163 u32 id = in.getIdentifier();
164
165 std::map<u32, Area>::iterator itr = m_store->areas_map.find(id);
166 assert(itr != m_store->areas_map.end());
167 m_result->push_back(&itr->second);
168 }
169
170 virtual void visitData(std::vector<const SpatialIndex::IData *> &v)
171 {
172 for (size_t i = 0; i < v.size(); i++)
173 visitData(*(v[i]));
174 }
175
176 private:
178 std::vector<Area *> *m_result = nullptr;
179 };
180};
181
182#endif // USE_SPATIAL
Definition areastore.h:39
AreaMap areas_map
Definition areastore.h:102
AreaStore()
Definition areastore.h:41
virtual void reserve(size_t count)
Definition areastore.h:49
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:124
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:132
void getAreasForPos(std::vector< Area * > *result, v3s16 pos)
Finds areas that the passed position is contained in.
Definition areastore.cpp:153
u32 getNextId() const
Returns the next area ID and increments it.
Definition areastore.cpp:111
static AreaStore * getOptimalImplementation()
Definition areastore.cpp:33
virtual ~AreaStore()=default
std::map< u32, Area > AreaMap
Definition areastore.h:101
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:42
LRUCache< v3s16, std::vector< Area * > > m_res_cache
Definition areastore.h:112
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:50
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:74
u8 m_cacheblock_radius
Range, in nodes, of the getAreasForPos cache.
Definition areastore.h:111
void invalidateCache()
Invalidates the getAreasForPos cache.
Definition areastore.cpp:104
size_t size() const
Definition areastore.h:50
bool m_cache_enabled
Definition areastore.h:108
Definition container.h:240
Definition areastore.h:151
~VectorResultVisitor()
Definition areastore.h:157
virtual void visitData(const SpatialIndex::IData &in)
Definition areastore.h:161
virtual void visitNode(const SpatialIndex::INode &in)
Definition areastore.h:159
VectorResultVisitor(std::vector< Area * > *result, SpatialAreaStore *store)
Definition areastore.h:153
virtual void visitData(std::vector< const SpatialIndex::IData * > &v)
Definition areastore.h:170
SpatialAreaStore * m_store
Definition areastore.h:177
std::vector< Area * > * m_result
Definition areastore.h:178
Definition areastore.h:134
SpatialIndex::ISpatialIndex * m_tree
Definition areastore.h:148
virtual bool removeArea(u32 id)
Removes an area from the store by ID.
Definition areastore.cpp:260
virtual ~SpatialAreaStore()
Definition areastore.cpp:293
virtual void getAreasForPosImpl(std::vector< Area * > *result, v3s16 pos)
Implementation of getAreasForPos.
Definition areastore.cpp:275
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:281
virtual bool insertArea(Area *a)
Add an area to the store.
Definition areastore.cpp:248
SpatialIndex::IStorageManager * m_storagemanager
Definition areastore.h:149
SpatialAreaStore()
Definition areastore.cpp:299
Definition areastore.h:116
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:218
virtual bool insertArea(Area *a)
Add an area to the store.
Definition areastore.cpp:177
virtual void getAreasForPosImpl(std::vector< Area * > *result, v3s16 pos)
Implementation of getAreasForPos.
Definition areastore.cpp:209
virtual void reserve(size_t count)
Definition areastore.h:118
virtual bool removeArea(u32 id)
Removes an area from the store by ID.
Definition areastore.cpp:191
std::vector< Area * > m_areas
Definition areastore.h:128
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
#define U32_MAX
Definition irrlichttypes.h:29
void sortBoxVerticies(v3s16 &p1, v3s16 &p2)
Definition numeric.h:116
Definition areastore.h:24
v3s16 maxedge
Definition areastore.h:34
u32 id
Definition areastore.h:33
Area(u32 area_id)
Definition areastore.h:25
std::string data
Definition areastore.h:35
Area(const v3s16 &mine, const v3s16 &maxe, u32 area_id=U32_MAX)
Definition areastore.h:27
v3s16 minedge
Definition areastore.h:34