3 Copyright (C) 2015 est31 <mtest31@outlook.com>
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.
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.
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.
24 #include "noise.h" // for PcgRandom
29 #include "util/container.h"
30 #include "util/numeric.h"
32 #include "cmake_config.h"
35 #include <spatialindex/SpatialIndex.h>
36 #include "util/serialize.h"
42 Area(const v3s16 &mine, const v3s16 &maxe) :
43 minedge(mine), maxedge(maxe)
45 sortBoxVerticies(minedge, maxedge);
49 v3s16 minedge, maxedge;
57 m_cache_enabled(true),
58 m_cacheblock_radius(64),
59 m_res_cache(1000, &cacheMiss, this),
63 virtual ~AreaStore() {}
65 static AreaStore *getOptimalImplementation();
67 virtual void reserve(size_t count) {};
68 size_t size() const { return areas_map.size(); }
70 /// Add an area to the store.
71 /// Updates the area's ID.
72 virtual bool insertArea(Area *a) = 0;
74 /// Removes an area from the store by ID.
75 /// @return Whether the area was in the store and removed.
76 virtual bool removeArea(u32 id) = 0;
78 /// Finds areas that the passed position is contained in.
79 /// Stores output in passed vector.
80 void getAreasForPos(std::vector<Area *> *result, v3s16 pos);
82 /// Finds areas that are completely contained inside the area defined
83 /// by the passed edges. If @p accept_overlap is true this finds any
84 /// areas that intersect with the passed area at any point.
85 virtual void getAreasInArea(std::vector<Area *> *result,
86 v3s16 minedge, v3s16 maxedge, bool accept_overlap) = 0;
88 /// Sets cache parameters.
89 void setCacheParams(bool enabled, u8 block_radius, size_t limit);
91 /// Returns a pointer to the area coresponding to the passed ID,
92 /// or NULL if it doesn't exist.
93 const Area *getArea(u32 id) const;
96 typedef bool (*ForEachCallback)(const Area *a, void *arg);
97 /// Calls a passed function for every stored area, until the
98 /// callback returns true. If that happens, it returns true,
99 /// if the search is exhausted, it returns false.
100 virtual bool forEach(ForEachCallback, void *arg=NULL) const = 0;
102 void serialize(std::ostream &is) const;
103 bool deserialize(std::istream &is);
107 /// Invalidates the getAreasForPos cache.
108 /// Call after adding or removing an area.
109 void invalidateCache();
111 /// Implementation of getAreasForPos.
112 /// getAreasForPos calls this if the cache is disabled.
113 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) = 0;
115 /// Returns the next area ID and increments it.
116 u32 getNextId() { return m_next_id++; }
118 // Note: This can't be an unordered_map, since all
119 // references would be invalidated on rehash.
120 typedef std::map<u32, Area> AreaMap;
124 /// Called by the cache when a value isn't found in the cache.
125 static void cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *dest);
127 bool m_cache_enabled;
128 /// Range, in nodes, of the getAreasForPos cache.
129 /// If you modify this, call invalidateCache()
130 u8 m_cacheblock_radius;
131 LRUCache<v3s16, std::vector<Area *> > m_res_cache;
137 class VectorAreaStore : public AreaStore {
139 virtual void reserve(size_t count) { m_areas.reserve(count); }
140 virtual bool insertArea(Area *a);
141 virtual bool removeArea(u32 id);
142 virtual void getAreasInArea(std::vector<Area *> *result,
143 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
144 //virtual bool forEach(ForEachCallback, void *arg) const;
147 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
150 std::vector<Area *> m_areas;
156 class SpatialAreaStore : public AreaStore {
159 virtual ~SpatialAreaStore();
161 virtual bool insertArea(Area *a);
162 virtual bool removeArea(u32 id);
163 virtual void getAreasInArea(std::vector<Area *> *result,
164 v3s16 minedge, v3s16 maxedge, bool accept_overlap);
165 //virtual bool forEach(ForEachCallback, void *arg) const;
168 virtual void getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos);
171 SpatialIndex::ISpatialIndex *m_tree;
172 SpatialIndex::IStorageManager *m_storagemanager;
174 class VectorResultVisitor : public SpatialIndex::IVisitor {
176 VectorResultVisitor(std::vector<Area *> *result, SpatialAreaStore *store) :
180 ~VectorResultVisitor() {}
182 virtual void visitNode(const SpatialIndex::INode &in) {}
184 virtual void visitData(const SpatialIndex::IData &in)
186 u32 id = in.getIdentifier();
188 std::map<u32, Area>::iterator itr = m_store->areas_map.find(id);
189 assert(itr != m_store->areas_map.end());
190 m_result->push_back(&itr->second);
193 virtual void visitData(std::vector<const SpatialIndex::IData *> &v)
195 for (size_t i = 0; i < v.size(); i++)
200 SpatialAreaStore *m_store;
201 std::vector<Area *> *m_result;
205 #endif // USE_SPATIAL
207 #endif // AREA_STORE_H_