summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2024-01-15 19:27:53 +0100
committerStanislaw Halik <sthalik@misaki.pl>2024-01-15 19:27:53 +0100
commit785293f4bf1beec65d23be0612e545e4c26ec366 (patch)
treef250c34d82977116498b8049c8055fc3981478ab /src
parenta5acc700d6a3a9b050864cf78a0f9f2305babdff (diff)
b
Diffstat (limited to 'src')
-rw-r--r--src/RTree-fwd.h5
-rw-r--r--src/RTree.h18
-rw-r--r--src/RTree.hpp27
-rw-r--r--src/chunk-collision.cpp16
-rw-r--r--src/chunk.cpp8
-rw-r--r--src/chunk.hpp4
6 files changed, 42 insertions, 36 deletions
diff --git a/src/RTree-fwd.h b/src/RTree-fwd.h
new file mode 100644
index 00000000..a93ec168
--- /dev/null
+++ b/src/RTree-fwd.h
@@ -0,0 +1,5 @@
+#pragma once
+
+template<class DATATYPE, class ELEMTYPE, int NUMDIMS,
+ class ELEMTYPEREAL = ELEMTYPE, int TMAXNODES = 8, int TMINNODES = TMAXNODES / 2>
+class RTree;
diff --git a/src/RTree.h b/src/RTree.h
index 95784409..7b8eeb7c 100644
--- a/src/RTree.h
+++ b/src/RTree.h
@@ -1,6 +1,5 @@
-#ifndef RTREE_H
-#define RTREE_H
-
+#pragma once
+#include "RTree-fwd.h"
// NOTE This file compiles under MSVC 6 SP5 and MSVC .Net 2003 it may not work on other compilers without modification.
// NOTE These next few lines may be win32 specific, you may need to modify them to compile on other platform
@@ -11,8 +10,6 @@
#include <stdio.h>
#endif
-#include <vector>
-
//
// RTree.h
//
@@ -71,10 +68,9 @@ private:
/// array similar to MFC CArray or STL Vector for returning search query result.
///
template<class DATATYPE, class ELEMTYPE, int NUMDIMS,
- class ELEMTYPEREAL = ELEMTYPE, int TMAXNODES = 8, int TMINNODES = TMAXNODES / 2>
+ class ELEMTYPEREAL, int TMAXNODES, int TMINNODES>
class RTree final
{
-
public:
struct Node; // Fwd decl. Used by other internal structs and iterator
@@ -88,8 +84,6 @@ public:
MINNODES = TMINNODES, ///< Min elements in node
};
-public:
-
RTree();
RTree(const RTree& other);
~RTree() noexcept;
@@ -302,9 +296,11 @@ protected:
Node* m_root; ///< Root of tree
ELEMTYPEREAL m_unitSphereVolume; ///< Unit sphere constant for required number of dimensions
+ template<typename T> using Array = ::Corrade::Containers::Array<T>;
+
public:
// return all the AABBs that form the RTree
- void ListTree(std::vector<Rect>& vec, std::vector<Node*>& temp) const;
+ void ListTree(Array<Rect>& vec, Array<Node*>& temp) const;
};
@@ -315,5 +311,3 @@ extern template class RTree<floormat::uint64_t, float, 2, float>;
#endif
//#undef RTREE_TEMPLATE
//#undef RTREE_QUAL
-
-#endif //RTREE_H
diff --git a/src/RTree.hpp b/src/RTree.hpp
index 01ce9b5b..18ae3b77 100644
--- a/src/RTree.hpp
+++ b/src/RTree.hpp
@@ -6,6 +6,7 @@
#include <cmath>
#include <cstddef>
#include <algorithm>
+#include <Corrade/Containers/GrowableArray.h>
#include <Corrade/Utility/Math.h>
#ifdef __GNUG__
@@ -1247,29 +1248,31 @@ void RTREE_QUAL::ReInsert(Node* a_node, ListNode** a_listNode)
RTREE_TEMPLATE
-void RTREE_QUAL::ListTree(std::vector<Rect>& treeList, std::vector<Node*>& toVisit) const
+void RTREE_QUAL::ListTree(Array<Rect>& treeList, Array<Node*>& toVisit) const
{
fm_assert(m_root);
fm_assert(m_root->m_level >= 0);
+ using namespace ::Corrade::Containers;
+
std::size_t count = (std::size_t)Count();
- treeList.clear();
- treeList.reserve(count);
- toVisit.clear();
- toVisit.reserve(count);
+ arrayResize(treeList, 0);
+ arrayResize(toVisit, 0);
+ arrayReserve(treeList, count);
+ arrayReserve(toVisit, count);
- toVisit.push_back(m_root);
+ arrayAppend(toVisit, m_root);
- while (!toVisit.empty()) {
+ while (!toVisit.isEmpty()) {
Node* a_node = toVisit.back();
- toVisit.pop_back();
+ arrayRemove(toVisit, toVisit.size()-1);
if(a_node->IsInternalNode())
{
// This is an internal node in the tree
for(int index=0; index < a_node->m_count; ++index)
{
- treeList.push_back(a_node->m_branch[index].m_rect);
- toVisit.push_back(a_node->m_branch[index].m_child);
+ arrayAppend(treeList, a_node->m_branch[index].m_rect);
+ arrayAppend(toVisit, a_node->m_branch[index].m_child);
}
}
else
@@ -1277,12 +1280,12 @@ void RTREE_QUAL::ListTree(std::vector<Rect>& treeList, std::vector<Node*>& toVis
// This is a leaf node
for(int index=0; index < a_node->m_count; ++index)
{
- treeList.push_back(a_node->m_branch[index].m_rect);
+ arrayAppend(treeList, a_node->m_branch[index].m_rect);
}
}
}
- toVisit.clear();
+ arrayResize(toVisit, 0);
}
RTREE_TEMPLATE
diff --git a/src/chunk-collision.cpp b/src/chunk-collision.cpp
index a6fd379d..5ffbf0ec 100644
--- a/src/chunk-collision.cpp
+++ b/src/chunk-collision.cpp
@@ -11,7 +11,7 @@
namespace floormat {
-chunk::RTree* chunk::rtree() noexcept { ensure_passability(); return &_rtree; }
+chunk::RTree* chunk::rtree() noexcept { ensure_passability(); return &*_rtree; }
namespace {
@@ -30,7 +30,7 @@ void chunk::ensure_passability() noexcept
return;
_pass_modified = false;
- _rtree.RemoveAll();
+ _rtree->RemoveAll();
for (const std::shared_ptr<object>& s : objects())
{
@@ -45,7 +45,7 @@ void chunk::ensure_passability() noexcept
{
auto [min, max] = whole_tile(i);
auto id = make_id(collision_type::geometry, atlas->pass_mode(), i+1);
- _rtree.Insert(min.data(), max.data(), id);
+ _rtree->Insert(min.data(), max.data(), id);
}
}
for (auto i = 0uz; i < TILE_COUNT; i++)
@@ -55,13 +55,13 @@ void chunk::ensure_passability() noexcept
{
auto [min, max] = wall_north(i, atlas->info().depth);
auto id = make_id(collision_type::geometry, atlas->info().passability, TILE_COUNT+i+1);
- _rtree.Insert(min.data(), max.data(), id);
+ _rtree->Insert(min.data(), max.data(), id);
}
if (const auto* atlas = tile.wall_west_atlas().get())
{
auto [min, max] = wall_west(i, atlas->info().depth);
auto id = make_id(collision_type::geometry, atlas->info().passability, TILE_COUNT*2+i+1);
- _rtree.Insert(min.data(), max.data(), id);
+ _rtree->Insert(min.data(), max.data(), id);
}
}
}
@@ -82,13 +82,13 @@ bool chunk::_bbox_for_scenery(const object& s, bbox& value) noexcept
void chunk::_remove_bbox(const bbox& x)
{
auto start = Vector2(x.start), end = Vector2(x.end);
- _rtree.Remove(start.data(), end.data(), x.id);
+ _rtree->Remove(start.data(), end.data(), x.id);
}
void chunk::_add_bbox(const bbox& x)
{
auto start = Vector2(x.start), end = Vector2(x.end);
- _rtree.Insert(start.data(), end.data(), x.id);
+ _rtree->Insert(start.data(), end.data(), x.id);
}
void chunk::_replace_bbox(const bbox& x0, const bbox& x1, bool b0, bool b1)
@@ -144,7 +144,7 @@ bool chunk::can_place_object(const object_proto& proto, local_coords pos)
const auto center = Vector2(pos)*TILE_SIZE2 + Vector2(proto.offset) + Vector2(proto.bbox_offset),
min = center - Vector2(bbox_size)*.5f, max = min + Vector2(bbox_size);
bool ret = true;
- _rtree.Search(min.data(), max.data(), [&](uint64_t data, const auto&) {
+ _rtree->Search(min.data(), max.data(), [&](uint64_t data, const auto&) {
[[maybe_unused]] auto x = std::bit_cast<collision_data>(data);
if (x.pass == (uint64_t)pass_mode::pass || x.pass == (uint64_t)pass_mode::shoot_through)
return true;
diff --git a/src/chunk.cpp b/src/chunk.cpp
index a819de6c..19d79b3a 100644
--- a/src/chunk.cpp
+++ b/src/chunk.cpp
@@ -2,6 +2,7 @@
#include "object.hpp"
#include "world.hpp"
#include "tile-iterator.hpp"
+#include "RTree.h"
#include <algorithm>
#include <Corrade/Containers/GrowableArray.h>
#include <Magnum/GL/Context.h>
@@ -120,7 +121,10 @@ void chunk::mark_modified() noexcept
mark_passability_modified();
}
-chunk::chunk(class world& w, chunk_coords_ ch) noexcept : _world{&w}, _coord{ch}
+chunk::chunk(class world& w, chunk_coords_ ch) noexcept :
+ _world{&w},
+ _rtree{InPlaceInit},
+ _coord{ch}
{
}
@@ -129,7 +133,7 @@ chunk::~chunk() noexcept
_teardown = true;
arrayResize(_objects, 0);
arrayShrink(_objects);
- _rtree.RemoveAll();
+ _rtree->RemoveAll();
}
chunk::chunk(chunk&&) noexcept = default;
diff --git a/src/chunk.hpp b/src/chunk.hpp
index 56afb6a5..064a16e7 100644
--- a/src/chunk.hpp
+++ b/src/chunk.hpp
@@ -2,7 +2,7 @@
#include "object-id.hpp"
#include "tile.hpp"
#include "local-coords.hpp"
-#include "src/RTree.h"
+#include "src/RTree-fwd.h"
#include "global-coords.hpp"
#include "wall-defs.hpp"
#include <type_traits>
@@ -150,7 +150,7 @@ private:
Array<std::shared_ptr<object>> _objects;
class world* _world;
GL::Mesh ground_mesh{NoCreate}, wall_mesh{NoCreate}, scenery_mesh{NoCreate};
- RTree _rtree;
+ Pointer<RTree> _rtree;
chunk_coords_ _coord;
mutable bool _maybe_empty : 1 = true,