diff options
Diffstat (limited to 'src/util/areastore.cpp')
-rw-r--r-- | src/util/areastore.cpp | 90 |
1 files changed, 51 insertions, 39 deletions
diff --git a/src/util/areastore.cpp b/src/util/areastore.cpp index 9754f96eb..cea526336 100644 --- a/src/util/areastore.cpp +++ b/src/util/areastore.cpp @@ -22,27 +22,28 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/container.h" #if USE_SPATIAL -#include <spatialindex/SpatialIndex.h> -#include <spatialindex/RTree.h> -#include <spatialindex/Point.h> + #include <spatialindex/SpatialIndex.h> + #include <spatialindex/RTree.h> + #include <spatialindex/Point.h> #endif #define AST_SMALLER_EQ_AS(p, q) (((p).X <= (q).X) && ((p).Y <= (q).Y) && ((p).Z <= (q).Z)) -#define AST_OVERLAPS_IN_DIMENSION(amine, amaxe, b, d) \ +#define AST_OVERLAPS_IN_DIMENSION(amine, amaxe, b, d) \ (!(((amine).d > (b)->maxedge.d) || ((amaxe).d < (b)->minedge.d))) -#define AST_CONTAINS_PT(a, p) \ - (AST_SMALLER_EQ_AS((a)->minedge, (p)) && AST_SMALLER_EQ_AS((p), (a)->maxedge)) +#define AST_CONTAINS_PT(a, p) (AST_SMALLER_EQ_AS((a)->minedge, (p)) && \ + AST_SMALLER_EQ_AS((p), (a)->maxedge)) -#define AST_CONTAINS_AREA(amine, amaxe, b) \ - (AST_SMALLER_EQ_AS((amine), (b)->minedge) && \ - AST_SMALLER_EQ_AS((b)->maxedge, (amaxe))) +#define AST_CONTAINS_AREA(amine, amaxe, b) \ + (AST_SMALLER_EQ_AS((amine), (b)->minedge) \ + && AST_SMALLER_EQ_AS((b)->maxedge, (amaxe))) + +#define AST_AREAS_OVERLAP(amine, amaxe, b) \ + (AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), X) && \ + AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), Y) && \ + AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), Z)) -#define AST_AREAS_OVERLAP(amine, amaxe, b) \ - (AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), X) && \ - AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), Y) && \ - AST_OVERLAPS_IN_DIMENSION((amine), (amaxe), (b), Z)) AreaStore *AreaStore::getOptimalImplementation() { @@ -91,7 +92,7 @@ void AreaStore::deserialize(std::istream &is) // Assume forwards-compatibility before version 5 if (ver >= 5) throw SerializationError("Unknown AreaStore " - "serialization version!"); + "serialization version!"); u16 num_areas = readU16(is); std::vector<Area> areas; @@ -104,7 +105,7 @@ void AreaStore::deserialize(std::istream &is) is.read(data, data_len); a.data = std::string(data, data_len); areas.emplace_back(a); - delete[] data; + delete [] data; } bool read_ids = is.good(); // EOF for old formats @@ -151,7 +152,10 @@ void AreaStore::cacheMiss(void *data, const v3s16 &mpos, std::vector<Area *> *de // get the points at the edges of the mapblock v3s16 minedge(mpos.X * r, mpos.Y * r, mpos.Z * r); - v3s16 maxedge(minedge.X + r - 1, minedge.Y + r - 1, minedge.Z + r - 1); + v3s16 maxedge( + minedge.X + r - 1, + minedge.Y + r - 1, + minedge.Z + r - 1); as->getAreasInArea(dest, minedge, maxedge, true); @@ -180,10 +184,12 @@ void AreaStore::getAreasForPos(std::vector<Area *> *result, v3s16 pos) } } + //// // VectorAreaStore //// + bool VectorAreaStore::insertArea(Area *a) { if (a->id == U32_MAX) @@ -204,8 +210,8 @@ bool VectorAreaStore::removeArea(u32 id) if (it == areas_map.end()) return false; Area *a = &it->second; - for (std::vector<Area *>::iterator v_it = m_areas.begin(); v_it != m_areas.end(); - ++v_it) { + for (std::vector<Area *>::iterator v_it = m_areas.begin(); + v_it != m_areas.end(); ++v_it) { if (*v_it == a) { m_areas.erase(v_it); break; @@ -225,12 +231,12 @@ void VectorAreaStore::getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos) } } -void VectorAreaStore::getAreasInArea(std::vector<Area *> *result, v3s16 minedge, - v3s16 maxedge, bool accept_overlap) +void VectorAreaStore::getAreasInArea(std::vector<Area *> *result, + v3s16 minedge, v3s16 maxedge, bool accept_overlap) { for (Area *area : m_areas) { - if (accept_overlap ? AST_AREAS_OVERLAP(minedge, maxedge, area) - : AST_CONTAINS_AREA(minedge, maxedge, area)) { + if (accept_overlap ? AST_AREAS_OVERLAP(minedge, maxedge, area) : + AST_CONTAINS_AREA(minedge, maxedge, area)) { result->push_back(area); } } @@ -238,11 +244,13 @@ void VectorAreaStore::getAreasInArea(std::vector<Area *> *result, v3s16 minedge, #if USE_SPATIAL -static inline SpatialIndex::Region get_spatial_region( - const v3s16 minedge, const v3s16 maxedge) +static inline SpatialIndex::Region get_spatial_region(const v3s16 minedge, + const v3s16 maxedge) { - const double p_low[] = {(double)minedge.X, (double)minedge.Y, (double)minedge.Z}; - const double p_high[] = {(double)maxedge.X, (double)maxedge.Y, (double)maxedge.Z}; + const double p_low[] = {(double)minedge.X, + (double)minedge.Y, (double)minedge.Z}; + const double p_high[] = {(double)maxedge.X, (double)maxedge.Y, + (double)maxedge.Z}; return SpatialIndex::Region(p_low, p_high, 3); } @@ -252,6 +260,7 @@ static inline SpatialIndex::Point get_spatial_point(const v3s16 pos) return SpatialIndex::Point(p, 3); } + bool SpatialAreaStore::insertArea(Area *a) { if (a->id == U32_MAX) @@ -269,8 +278,8 @@ bool SpatialAreaStore::removeArea(u32 id) std::map<u32, Area>::iterator itr = areas_map.find(id); if (itr != areas_map.end()) { Area *a = &itr->second; - bool result = m_tree->deleteData( - get_spatial_region(a->minedge, a->maxedge), id); + bool result = m_tree->deleteData(get_spatial_region(a->minedge, + a->maxedge), id); areas_map.erase(itr); invalidateCache(); return result; @@ -285,13 +294,13 @@ void SpatialAreaStore::getAreasForPosImpl(std::vector<Area *> *result, v3s16 pos m_tree->pointLocationQuery(get_spatial_point(pos), visitor); } -void SpatialAreaStore::getAreasInArea(std::vector<Area *> *result, v3s16 minedge, - v3s16 maxedge, bool accept_overlap) +void SpatialAreaStore::getAreasInArea(std::vector<Area *> *result, + v3s16 minedge, v3s16 maxedge, bool accept_overlap) { VectorResultVisitor visitor(result, this); if (accept_overlap) { - m_tree->intersectsWithQuery( - get_spatial_region(minedge, maxedge), visitor); + m_tree->intersectsWithQuery(get_spatial_region(minedge, maxedge), + visitor); } else { m_tree->containsWhatQuery(get_spatial_region(minedge, maxedge), visitor); } @@ -304,14 +313,17 @@ SpatialAreaStore::~SpatialAreaStore() SpatialAreaStore::SpatialAreaStore() { - m_storagemanager = SpatialIndex::StorageManager::createNewMemoryStorageManager(); + m_storagemanager = + SpatialIndex::StorageManager::createNewMemoryStorageManager(); SpatialIndex::id_type id; - m_tree = SpatialIndex::RTree::createNewRTree(*m_storagemanager, - .7, // Fill factor - 100, // Index capacity - 100, // Leaf capacity - 3, // dimension :) - SpatialIndex::RTree::RV_RSTAR, id); + m_tree = SpatialIndex::RTree::createNewRTree( + *m_storagemanager, + .7, // Fill factor + 100, // Index capacity + 100, // Leaf capacity + 3, // dimension :) + SpatialIndex::RTree::RV_RSTAR, + id); } #endif |