载入中...
搜索中...
未找到
QuadTree.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include "spatial/Bounds.h"
6#include "spatial/QueryIds.h"
7
8#include <memory>
9#include <unordered_map>
10#include <vector>
11
12namespace eve::spatial {
13
20public:
22 QuadTree(float minX, float minY, float maxX, float maxY, int maxDepth = 8,
23 int maxPerNode = 8);
25 ~QuadTree() = default;
26
27 QuadTree(const QuadTree &) = delete;
28 QuadTree &operator=(const QuadTree &) = delete;
29
31 void clear();
33 bool insert(int id, float minX, float minY, float maxX, float maxY);
35 bool remove(int id);
37 bool update(int id, float minX, float minY, float maxX, float maxY);
39 bool contains(int id) const;
41 int getCount() const { return static_cast<int>(items_.size()); }
42
44 int queryPoint(float x, float y);
46 int queryRect(float minX, float minY, float maxX, float maxY);
48 int queryCircle(float cx, float cy, float radius);
49
51 int getResultCount() const { return results_.getCount(); }
53 int getResultId(int index) const { return results_.getId(index); }
54
56 float getMinX() const { return rootBounds_.minX; }
58 float getMinY() const { return rootBounds_.minY; }
60 float getMaxX() const { return rootBounds_.maxX; }
62 float getMaxY() const { return rootBounds_.maxY; }
64 int getMaxDepth() const { return maxDepth_; }
66 int getMaxPerNode() const { return maxPerNode_; }
67
68private:
69 struct Node {
71 int depth = 0;
72 std::vector<int> itemIds;
73 std::unique_ptr<Node> children[4];
74 bool isLeaf() const { return children[0] == nullptr; }
75 };
76
77 void rebuild();
78 bool insertInto(Node &node, int id, const AABB2 &bounds);
79 void split(Node &node);
80 void collect(Node &node, const AABB2 *rect, float cx, float cy, float radius,
81 bool useCircle);
82 Node *ensureRoot();
83
84 AABB2 rootBounds_;
85 int maxDepth_ = 8;
86 int maxPerNode_ = 8;
87 std::unique_ptr<Node> root_;
88 std::unordered_map<int, AABB2> items_;
89 QueryIds results_;
90};
91
92} // namespace eve::spatial
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float cx
Definition CardTypes.cpp:33
float cy
Definition CardTypes.cpp:34
bool split
Definition CaveMesh.cpp:123
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
float radius
const RoadNode * node
int children
Definition TreeMesh.cpp:295
uint32_t index
std::uint32_t depth
Region quadtree for 2D AABB broad-phase / map culling. Items are stored in the smallest node that ful...
Definition QuadTree.h:19
QuadTree & operator=(const QuadTree &)=delete
float getMaxY() const
Root/world maximum Y.
Definition QuadTree.h:62
int getCount() const
Number of stored ids.
Definition QuadTree.h:41
int getResultId(int index) const
Hit id at dense index from the last query*, or -1.
Definition QuadTree.h:53
int getMaxPerNode() const
Item capacity before a node splits.
Definition QuadTree.h:66
float getMinY() const
Root/world minimum Y.
Definition QuadTree.h:58
float getMaxX() const
Root/world maximum X.
Definition QuadTree.h:60
~QuadTree()=default
Releases tree nodes.
QuadTree(const QuadTree &)=delete
int getResultCount() const
Number of hits from the last query*.
Definition QuadTree.h:51
float getMinX() const
Root/world minimum X.
Definition QuadTree.h:56
int getMaxDepth() const
Maximum subdivision depth.
Definition QuadTree.h:64
Axis-aligned 2D bounding box (min/max inclusive).
Definition Bounds.h:10
glm::vec4 bounds