Luanti 5.19.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 <map>
9#include <vector>
10#include <istream>
11#include "util/container.h"
12#include "util/numeric.h"
13#ifndef ANDROID
14 #include "cmake_config.h"
15#endif
16#if USE_SPATIAL
17 #include <spatialindex/SpatialIndex.h>
18#endif
19
20
21struct Area {
22 Area(u32 area_id) : id(area_id) {}
23
24 Area(const v3s16 &mine, const v3s16 &maxe, u32 area_id = U32_MAX) :
25 id(area_id), minedge(mine), maxedge(maxe)
26 {
28 }
29
30 u32 id;
32 std::string data;
33};
34
35
36class AreaStore {
37public:
39 m_res_cache(1000, &cacheMiss, this)
40 {}
41
42 virtual ~AreaStore() = default;
43
45
46 virtual void reserve(size_t count) {};
47 size_t size() const { return areas_map.size(); }
48
52 virtual bool insertArea(Area *a) = 0;
53
56 virtual bool removeArea(u32 id) = 0;
57
60 void getAreasForPos(std::vector<Area *> *result, v3s16 pos);
61
64 virtual void getOverlappingAreas(std::vector<Area *> *result,
65 v3s16 minedge, v3s16 maxedge) = 0;
66
69 virtual void getContainingAreas(std::vector<Area *> *result,
70 v3s16 minedge, v3s16 maxedge) = 0;
71
73 void setCacheParams(bool enabled, u8 block_radius, size_t limit);
74
77 const Area *getArea(u32 id) const;
78
80 void serialize(std::ostream &is) const;
81
86 void deserialize(std::istream &is);
87
88protected:
91 void invalidateCache();
92
95 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0;
96
98 u32 getNextId() const;
99
100 // Note: This can't be an unordered_map, since all
101 // references would be invalidated on rehash.
102 typedef std::map<u32, Area> AreaMap;
104
105private:
107 static void cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *dest);
108
109 bool m_cache_enabled = true;
114};
115
116
118public:
119 void reserve(size_t count) override { m_areas.reserve(count); }
120 bool insertArea(Area *a) override;
121 bool removeArea(u32 id) override;
122 void getOverlappingAreas(std::vector<Area *> *result, v3s16 minedge, v3s16 maxedge) override;
123 void getContainingAreas(std::vector<Area *> *result, v3s16 minedge, v3s16 maxedge) override;
124
125protected:
126 void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) override;
127
128private:
129 std::vector<Area *> m_areas;
130};
131
132
133#if USE_SPATIAL
134
136public:
138 virtual ~SpatialAreaStore();
139
140 bool insertArea(Area *a) override;
141 bool removeArea(u32 id) override;
142 void getOverlappingAreas(std::vector<Area *> *result, v3s16 minedge, v3s16 maxedge) override;
143 void getContainingAreas(std::vector<Area *> *result, v3s16 minedge, v3s16 maxedge) override;
144
145protected:
146 void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) override;
147
148private:
149 SpatialIndex::ISpatialIndex *m_tree = nullptr;
150 SpatialIndex::IStorageManager *m_storagemanager = nullptr;
151
152 class VectorResultVisitor : public SpatialIndex::IVisitor {
153 public:
154 VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store) :
155 m_store(store),
156 m_result(result)
157 {}
159
160 virtual void visitNode(const SpatialIndex::INode &in) {}
161
162 virtual void visitData(const SpatialIndex::IData &in)
163 {
164 u32 id = in.getIdentifier();
165
166 auto itr = m_store->areas_map.find(id);
167 assert(itr != m_store->areas_map.end());
168 m_result->push_back(&itr->second);
169 }
170
171 virtual void visitData(std::vector<const SpatialIndex::IData *> &v)
172 {
173 for (size_t i = 0; i < v.size(); i++)
174 visitData(*(v[i]));
175 }
176
177 private:
179 std::vector<Area *> *m_result = nullptr;
180 };
181};
182
183#endif // USE_SPATIAL
AreaMap areas_map
Definition areastore.h:103
AreaStore()
Definition areastore.h:38
virtual void reserve(size_t count)
Definition areastore.h:46
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:102
virtual bool insertArea(Area *a)=0
Add an area to the store.
const Area * getArea(u32 id) const
Returns a pointer to the area corresponding 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:113
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
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:112
void invalidateCache()
Invalidates the getAreasForPos cache.
Definition areastore.cpp:104
size_t size() const
Definition areastore.h:47
virtual void getOverlappingAreas(std::vector< Area * > *result, v3s16 minedge, v3s16 maxedge)=0
Finds areas that are overlapping the area defined by the passed edges.
virtual void getContainingAreas(std::vector< Area * > *result, v3s16 minedge, v3s16 maxedge)=0
Finds areas that are completely contained inside the area defined by the passed edges.
bool m_cache_enabled
Definition areastore.h:109
Definition container.h:284
~VectorResultVisitor()
Definition areastore.h:158
virtual void visitData(const SpatialIndex::IData &in)
Definition areastore.h:162
virtual void visitNode(const SpatialIndex::INode &in)
Definition areastore.h:160
VectorResultVisitor(std::vector< Area * > *result, SpatialAreaStore *store)
Definition areastore.h:154
virtual void visitData(std::vector< const SpatialIndex::IData * > &v)
Definition areastore.h:171
SpatialAreaStore * m_store
Definition areastore.h:178
std::vector< Area * > * m_result
Definition areastore.h:179
SpatialIndex::ISpatialIndex * m_tree
Definition areastore.h:149
void getContainingAreas(std::vector< Area * > *result, v3s16 minedge, v3s16 maxedge) override
Finds areas that are completely contained inside the area defined by the passed edges.
Definition areastore.cpp:294
void getOverlappingAreas(std::vector< Area * > *result, v3s16 minedge, v3s16 maxedge) override
Finds areas that are overlapping the area defined by the passed edges.
Definition areastore.cpp:287
bool removeArea(u32 id) override
Removes an area from the store by ID.
Definition areastore.cpp:266
SpatialIndex::IStorageManager * m_storagemanager
Definition areastore.h:150
void getAreasForPosImpl(std::vector< Area * > *result, v3s16 pos) override
Implementation of getAreasForPos.
Definition areastore.cpp:281
bool insertArea(Area *a) override
Add an area to the store.
Definition areastore.cpp:254
SpatialAreaStore()
Definition areastore.cpp:307
Definition areastore.h:117
void getContainingAreas(std::vector< Area * > *result, v3s16 minedge, v3s16 maxedge) override
Finds areas that are completely contained inside the area defined by the passed edges.
Definition areastore.cpp:226
void getAreasForPosImpl(std::vector< Area * > *result, v3s16 pos) override
Implementation of getAreasForPos.
Definition areastore.cpp:208
void getOverlappingAreas(std::vector< Area * > *result, v3s16 minedge, v3s16 maxedge) override
Finds areas that are overlapping the area defined by the passed edges.
Definition areastore.cpp:217
void reserve(size_t count) override
Definition areastore.h:119
bool insertArea(Area *a) override
Add an area to the store.
Definition areastore.cpp:177
std::vector< Area * > m_areas
Definition areastore.h:129
bool removeArea(u32 id) override
Removes an area from the store by ID.
Definition areastore.cpp:190
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
#define U32_MAX
Definition irrlichttypes.h:23
void sortBoxVerticies(core::vector3d< T > &p1, core::vector3d< T > &p2)
Definition numeric.h:118
Definition areastore.h:21
v3s16 maxedge
Definition areastore.h:31
u32 id
Definition areastore.h:30
Area(u32 area_id)
Definition areastore.h:22
std::string data
Definition areastore.h:32
Area(const v3s16 &mine, const v3s16 &maxe, u32 area_id=U32_MAX)
Definition areastore.h:24
v3s16 minedge
Definition areastore.h:31