载入中...
搜索中...
未找到
Spatial.cpp
浏览该文件的文档.
1#include "spatial/Spatial.h"
2
3#include "spatial/BSPTree2D.h"
4#include "spatial/BSPTree3D.h"
5#include "spatial/Octree.h"
6#include "spatial/QuadTree.h"
9
10#include <simplesquirrel/simplesquirrel.hpp>
11
12namespace eve::spatial {
13
15
16QuadTree *Spatial::newQuadTree(float minX, float minY, float maxX, float maxY, int maxDepth,
17 int maxPerNode) {
18 return new QuadTree(minX, minY, maxX, maxY, maxDepth, maxPerNode);
19}
20
21Octree *Spatial::newOctree(float minX, float minY, float minZ, float maxX, float maxY, float maxZ,
22 int maxDepth, int maxPerNode) {
23 return new Octree(minX, minY, minZ, maxX, maxY, maxZ, maxDepth, maxPerNode);
24}
25
26SpatialHash2D *Spatial::newSpatialHash2D(float cellSize) { return new SpatialHash2D(cellSize); }
27
28SpatialHash3D *Spatial::newSpatialHash3D(float cellSize) { return new SpatialHash3D(cellSize); }
29
30BSPTree2D *Spatial::newBSPTree2D(float minX, float minY, float maxX, float maxY, int maxDepth,
31 int maxPerNode) {
32 return new BSPTree2D(minX, minY, maxX, maxY, maxDepth, maxPerNode);
33}
34
35BSPTree3D *Spatial::newBSPTree3D(float minX, float minY, float minZ, float maxX, float maxY,
36 float maxZ, int maxDepth, int maxPerNode) {
37 return new BSPTree3D(minX, minY, minZ, maxX, maxY, maxZ, maxDepth, maxPerNode);
38}
39
40void Spatial::expose(ssq::Table &table) {
41 auto cls = table.addClass(name, Spatial::create, false);
42 expose(cls);
43
44 auto qt = table.addClass<QuadTree>(
45 "QuadTree",
46 std::function<QuadTree *()>([]() -> QuadTree * { return nullptr; }), true);
47 qt.addFunc("clear", &QuadTree::clear);
48 qt.addFunc("insert", &QuadTree::insert);
49 qt.addFunc("remove", &QuadTree::remove);
50 qt.addFunc("update", &QuadTree::update);
51 qt.addFunc("contains", &QuadTree::contains);
52 qt.addFunc("getCount", &QuadTree::getCount);
53 qt.addFunc("queryPoint", &QuadTree::queryPoint);
54 qt.addFunc("queryRect", &QuadTree::queryRect);
55 qt.addFunc("queryCircle", &QuadTree::queryCircle);
56 qt.addFunc("getResultCount", &QuadTree::getResultCount);
57 qt.addFunc("getResultId", &QuadTree::getResultId);
58 qt.addFunc("getMinX", &QuadTree::getMinX);
59 qt.addFunc("getMinY", &QuadTree::getMinY);
60 qt.addFunc("getMaxX", &QuadTree::getMaxX);
61 qt.addFunc("getMaxY", &QuadTree::getMaxY);
62 qt.addFunc("getMaxDepth", &QuadTree::getMaxDepth);
63 qt.addFunc("getMaxPerNode", &QuadTree::getMaxPerNode);
64
65 auto ot = table.addClass<Octree>(
66 "Octree", std::function<Octree *()>([]() -> Octree * { return nullptr; }), true);
67 ot.addFunc("clear", &Octree::clear);
68 ot.addFunc("insert", &Octree::insert);
69 ot.addFunc("remove", &Octree::remove);
70 ot.addFunc("update", &Octree::update);
71 ot.addFunc("contains", &Octree::contains);
72 ot.addFunc("getCount", &Octree::getCount);
73 ot.addFunc("queryPoint", &Octree::queryPoint);
74 ot.addFunc("queryAABB", &Octree::queryAABB);
75 ot.addFunc("querySphere", &Octree::querySphere);
76 ot.addFunc("getResultCount", &Octree::getResultCount);
77 ot.addFunc("getResultId", &Octree::getResultId);
78 ot.addFunc("getMinX", &Octree::getMinX);
79 ot.addFunc("getMinY", &Octree::getMinY);
80 ot.addFunc("getMinZ", &Octree::getMinZ);
81 ot.addFunc("getMaxX", &Octree::getMaxX);
82 ot.addFunc("getMaxY", &Octree::getMaxY);
83 ot.addFunc("getMaxZ", &Octree::getMaxZ);
84 ot.addFunc("getMaxDepth", &Octree::getMaxDepth);
85 ot.addFunc("getMaxPerNode", &Octree::getMaxPerNode);
86
87 auto h2 = table.addClass<SpatialHash2D>(
88 "SpatialHash2D",
89 std::function<SpatialHash2D *()>([]() -> SpatialHash2D * { return nullptr; }), true);
90 h2.addFunc("clear", &SpatialHash2D::clear);
91 h2.addFunc("setCellSize", &SpatialHash2D::setCellSize);
92 h2.addFunc("getCellSize", &SpatialHash2D::getCellSize);
93 h2.addFunc("insert", &SpatialHash2D::insert);
94 h2.addFunc("remove", &SpatialHash2D::remove);
95 h2.addFunc("update", &SpatialHash2D::update);
96 h2.addFunc("contains", &SpatialHash2D::contains);
97 h2.addFunc("getCount", &SpatialHash2D::getCount);
98 h2.addFunc("queryPoint", &SpatialHash2D::queryPoint);
99 h2.addFunc("queryRect", &SpatialHash2D::queryRect);
100 h2.addFunc("queryCircle", &SpatialHash2D::queryCircle);
101 h2.addFunc("getResultCount", &SpatialHash2D::getResultCount);
102 h2.addFunc("getResultId", &SpatialHash2D::getResultId);
103
104 auto h3 = table.addClass<SpatialHash3D>(
105 "SpatialHash3D",
106 std::function<SpatialHash3D *()>([]() -> SpatialHash3D * { return nullptr; }), true);
107 h3.addFunc("clear", &SpatialHash3D::clear);
108 h3.addFunc("setCellSize", &SpatialHash3D::setCellSize);
109 h3.addFunc("getCellSize", &SpatialHash3D::getCellSize);
110 h3.addFunc("insert", &SpatialHash3D::insert);
111 h3.addFunc("remove", &SpatialHash3D::remove);
112 h3.addFunc("update", &SpatialHash3D::update);
113 h3.addFunc("contains", &SpatialHash3D::contains);
114 h3.addFunc("getCount", &SpatialHash3D::getCount);
115 h3.addFunc("queryPoint", &SpatialHash3D::queryPoint);
116 h3.addFunc("queryAABB", &SpatialHash3D::queryAABB);
117 h3.addFunc("querySphere", &SpatialHash3D::querySphere);
118 h3.addFunc("getResultCount", &SpatialHash3D::getResultCount);
119 h3.addFunc("getResultId", &SpatialHash3D::getResultId);
120
121 auto b2 = table.addClass<BSPTree2D>(
122 "BSPTree2D",
123 std::function<BSPTree2D *()>([]() -> BSPTree2D * { return nullptr; }), true);
124 b2.addFunc("clear", &BSPTree2D::clear);
125 b2.addFunc("insert", &BSPTree2D::insert);
126 b2.addFunc("remove", &BSPTree2D::remove);
127 b2.addFunc("update", &BSPTree2D::update);
128 b2.addFunc("contains", &BSPTree2D::contains);
129 b2.addFunc("getCount", &BSPTree2D::getCount);
130 b2.addFunc("queryPoint", &BSPTree2D::queryPoint);
131 b2.addFunc("queryRect", &BSPTree2D::queryRect);
132 b2.addFunc("queryCircle", &BSPTree2D::queryCircle);
133 b2.addFunc("getResultCount", &BSPTree2D::getResultCount);
134 b2.addFunc("getResultId", &BSPTree2D::getResultId);
135 b2.addFunc("getMinX", &BSPTree2D::getMinX);
136 b2.addFunc("getMinY", &BSPTree2D::getMinY);
137 b2.addFunc("getMaxX", &BSPTree2D::getMaxX);
138 b2.addFunc("getMaxY", &BSPTree2D::getMaxY);
139 b2.addFunc("getMaxDepth", &BSPTree2D::getMaxDepth);
140 b2.addFunc("getMaxPerNode", &BSPTree2D::getMaxPerNode);
141
142 auto b3 = table.addClass<BSPTree3D>(
143 "BSPTree3D",
144 std::function<BSPTree3D *()>([]() -> BSPTree3D * { return nullptr; }), true);
145 b3.addFunc("clear", &BSPTree3D::clear);
146 b3.addFunc("insert", &BSPTree3D::insert);
147 b3.addFunc("remove", &BSPTree3D::remove);
148 b3.addFunc("update", &BSPTree3D::update);
149 b3.addFunc("contains", &BSPTree3D::contains);
150 b3.addFunc("getCount", &BSPTree3D::getCount);
151 b3.addFunc("queryPoint", &BSPTree3D::queryPoint);
152 b3.addFunc("queryAABB", &BSPTree3D::queryAABB);
153 b3.addFunc("querySphere", &BSPTree3D::querySphere);
154 b3.addFunc("getResultCount", &BSPTree3D::getResultCount);
155 b3.addFunc("getResultId", &BSPTree3D::getResultId);
156 b3.addFunc("getMinX", &BSPTree3D::getMinX);
157 b3.addFunc("getMinY", &BSPTree3D::getMinY);
158 b3.addFunc("getMinZ", &BSPTree3D::getMinZ);
159 b3.addFunc("getMaxX", &BSPTree3D::getMaxX);
160 b3.addFunc("getMaxY", &BSPTree3D::getMaxY);
161 b3.addFunc("getMaxZ", &BSPTree3D::getMaxZ);
162 b3.addFunc("getMaxDepth", &BSPTree3D::getMaxDepth);
163 b3.addFunc("getMaxPerNode", &BSPTree3D::getMaxPerNode);
164}
165
166void Spatial::expose(ssq::Class &cls) {
167 cls.addFunc("getName", &Spatial::getName);
168 cls.addFunc("newQuadTree", &Spatial::newQuadTree);
169 cls.addFunc("newOctree", &Spatial::newOctree);
170 cls.addFunc("newSpatialHash2D", &Spatial::newSpatialHash2D);
171 cls.addFunc("newSpatialHash3D", &Spatial::newSpatialHash3D);
172 cls.addFunc("newBSPTree2D", &Spatial::newBSPTree2D);
173 cls.addFunc("newBSPTree3D", &Spatial::newBSPTree3D);
174}
175
176} // namespace eve::spatial
HSQOBJECT cls
Definition ECS.cpp:21
std::string name
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:26
virtual std::string getName() const =0
Returns the name.
Binary space partition tree (kd-style AABB splits) for 2D culling. Alternating X/Y splits at node mid...
Definition BSPTree2D.h:18
void clear()
Removes all stored entries.
Definition BSPTree2D.cpp:27
bool update(int id, float minX, float minY, float maxX, float maxY)
Moves an existing item to a new AABB; false if unknown or out of bounds.
Definition BSPTree2D.cpp:54
float getMinX() const
Root/world minimum X.
Definition BSPTree2D.h:55
int queryPoint(float x, float y)
Finds items overlapping a point; fills the result buffer.
int getResultId(int index) const
Hit id at dense index from the last query*, or -1.
Definition BSPTree2D.h:52
bool contains(int id) const
True if the id is currently stored.
Definition BSPTree2D.cpp:34
int queryRect(float minX, float minY, float maxX, float maxY)
Finds items overlapping an AABB; fills the result buffer.
int queryCircle(float cx, float cy, float radius)
Finds items overlapping a circle; fills the result buffer.
int getCount() const
Number of stored ids.
Definition BSPTree2D.h:40
float getMaxY() const
Root/world maximum Y.
Definition BSPTree2D.h:61
int getMaxDepth() const
Maximum subdivision depth.
Definition BSPTree2D.h:63
bool remove(int id)
Removes an item by id; false if unknown.
Definition BSPTree2D.cpp:47
bool insert(int id, float minX, float minY, float maxX, float maxY)
Inserts an item AABB; false if out of bounds or id already present.
Definition BSPTree2D.cpp:36
float getMaxX() const
Root/world maximum X.
Definition BSPTree2D.h:59
int getMaxPerNode() const
Item capacity before a node splits.
Definition BSPTree2D.h:65
int getResultCount() const
Number of hits from the last query*.
Definition BSPTree2D.h:50
float getMinY() const
Root/world minimum Y.
Definition BSPTree2D.h:57
Binary space partition tree (kd-style AABB splits) for 3D culling. Alternating X/Y/Z splits at node m...
Definition BSPTree3D.h:18
float getMinY() const
Root/world minimum Y.
Definition BSPTree3D.h:57
bool contains(int id) const
True if the id is currently stored.
Definition BSPTree3D.cpp:36
float getMinX() const
Root/world minimum X.
Definition BSPTree3D.h:55
bool remove(int id)
Removes an item by id; false if unknown.
Definition BSPTree3D.cpp:50
void clear()
Removes all stored entries.
Definition BSPTree3D.cpp:29
float getMinZ() const
Root/world minimum Z.
Definition BSPTree3D.h:59
int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Finds items overlapping an AABB; fills the result buffer.
float getMaxZ() const
Root/world maximum Z.
Definition BSPTree3D.h:65
float getMaxY() const
Root/world maximum Y.
Definition BSPTree3D.h:63
int getResultCount() const
Number of hits from the last query*.
Definition BSPTree3D.h:50
int querySphere(float cx, float cy, float cz, float radius)
Finds items overlapping a sphere; fills the result buffer.
int queryPoint(float x, float y, float z)
Finds items overlapping a point; fills the result buffer.
int getCount() const
Number of stored ids.
Definition BSPTree3D.h:40
int getResultId(int index) const
Hit id at dense index from the last query*, or -1.
Definition BSPTree3D.h:52
int getMaxPerNode() const
Item capacity before a node splits.
Definition BSPTree3D.h:69
bool update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Moves an existing item to a new AABB; false if unknown or out of bounds.
Definition BSPTree3D.cpp:57
bool insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Inserts an item AABB; false if out of bounds or id already present.
Definition BSPTree3D.cpp:38
float getMaxX() const
Root/world maximum X.
Definition BSPTree3D.h:61
int getMaxDepth() const
Maximum subdivision depth.
Definition BSPTree3D.h:67
Region octree for 3D AABB broad-phase / scene culling. Same storage rules as QuadTree (smallest fully...
Definition Octree.h:18
bool insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Inserts an item AABB; false if out of bounds or id already present.
Definition Octree.cpp:37
float getMaxZ() const
Root/world maximum Z.
Definition Octree.h:65
int queryPoint(float x, float y, float z)
Finds items overlapping a point; fills the result buffer.
Definition Octree.cpp:161
int getMaxPerNode() const
Item capacity before a node splits.
Definition Octree.h:69
int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Finds items overlapping an AABB; fills the result buffer.
Definition Octree.cpp:167
bool update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Moves an existing item to a new AABB; false if unknown or out of bounds.
Definition Octree.cpp:56
int querySphere(float cx, float cy, float cz, float radius)
Finds items overlapping a sphere; fills the result buffer.
Definition Octree.cpp:174
float getMinZ() const
Root/world minimum Z.
Definition Octree.h:59
float getMinX() const
Root/world minimum X.
Definition Octree.h:55
float getMaxY() const
Root/world maximum Y.
Definition Octree.h:63
bool contains(int id) const
True if the id is currently stored.
Definition Octree.cpp:35
void clear()
Removes all stored entries.
Definition Octree.cpp:28
int getResultCount() const
Number of hits from the last query*.
Definition Octree.h:50
int getMaxDepth() const
Maximum subdivision depth.
Definition Octree.h:67
float getMaxX() const
Root/world maximum X.
Definition Octree.h:61
int getResultId(int index) const
Hit id at dense index from the last query*, or -1.
Definition Octree.h:52
float getMinY() const
Root/world minimum Y.
Definition Octree.h:57
int getCount() const
Number of stored ids.
Definition Octree.h:40
bool remove(int id)
Removes an item by id; false if unknown.
Definition Octree.cpp:49
Region quadtree for 2D AABB broad-phase / map culling. Items are stored in the smallest node that ful...
Definition QuadTree.h:19
float getMaxY() const
Root/world maximum Y.
Definition QuadTree.h:62
bool contains(int id) const
True if the id is currently stored.
Definition QuadTree.cpp:33
bool insert(int id, float minX, float minY, float maxX, float maxY)
Inserts an item AABB; false if out of bounds or id already present.
Definition QuadTree.cpp:35
int getCount() const
Number of stored ids.
Definition QuadTree.h:41
bool update(int id, float minX, float minY, float maxX, float maxY)
Moves an existing item to a new AABB; false if unknown or out of bounds.
Definition QuadTree.cpp:56
int queryPoint(float x, float y)
Finds items overlapping a point; fills the result buffer.
Definition QuadTree.cpp:157
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
int queryRect(float minX, float minY, float maxX, float maxY)
Finds items overlapping an AABB; fills the result buffer.
Definition QuadTree.cpp:163
void clear()
Removes all stored entries.
Definition QuadTree.cpp:26
int queryCircle(float cx, float cy, float radius)
Finds items overlapping a circle; fills the result buffer.
Definition QuadTree.cpp:170
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
bool remove(int id)
Removes an item by id; false if unknown.
Definition QuadTree.cpp:49
Uniform-grid spatial hash for 2D AABB queries (map / particle / entity culling). Items are registered...
int getResultId(int index) const
Hit id at dense index from the last query*, or -1.
void clear()
Removes all stored entries.
bool remove(int id)
Removes an item by id; false if unknown.
int getCount() const
Number of stored ids.
bool contains(int id) const
True if the id is currently stored.
bool update(int id, float minX, float minY, float maxX, float maxY)
Moves an existing item to a new AABB; false if unknown or out of bounds.
void setCellSize(float cellSize)
Sets cell size and clears existing entries.
float getCellSize() const
Current uniform cell size.
int queryPoint(float x, float y)
Finds items overlapping a point; fills the result buffer.
int getResultCount() const
Number of hits from the last query*.
int queryRect(float minX, float minY, float maxX, float maxY)
Finds items overlapping an AABB; fills the result buffer.
int queryCircle(float cx, float cy, float radius)
Finds items overlapping a circle; fills the result buffer.
bool insert(int id, float minX, float minY, float maxX, float maxY)
Inserts an item AABB; false if out of bounds or id already present.
Uniform-grid spatial hash for 3D AABB / sphere queries.
void clear()
Removes all stored entries.
bool insert(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Inserts an item AABB; false if out of bounds or id already present.
int querySphere(float cx, float cy, float cz, float radius)
Finds items overlapping a sphere; fills the result buffer.
int getCount() const
Number of stored ids.
void setCellSize(float cellSize)
Sets cell size and clears existing entries.
bool update(int id, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Moves an existing item to a new AABB; false if unknown or out of bounds.
int getResultId(int index) const
Hit id at dense index from the last query*, or -1.
bool contains(int id) const
True if the id is currently stored.
float getCellSize() const
Current uniform cell size.
bool remove(int id)
Removes an item by id; false if unknown.
int queryAABB(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
Finds items overlapping an AABB; fills the result buffer.
int queryPoint(float x, float y, float z)
Finds items overlapping a point; fills the result buffer.
int getResultCount() const
Number of hits from the last query*.
Spatial index module — broad-phase / map culling structures. Script: spatial <- eve....
Definition Spatial.h:21
BSPTree2D * newBSPTree2D(float minX, float minY, float maxX, float maxY, int maxDepth=12, int maxPerNode=8)
Creates a 2D BSP/kd tree over the given bounds. @ownership Caller deletes.
Definition Spatial.cpp:30
Octree * newOctree(float minX, float minY, float minZ, float maxX, float maxY, float maxZ, int maxDepth=8, int maxPerNode=8)
Creates an Octree over the given 3D bounds. @ownership Caller deletes.
Definition Spatial.cpp:21
SpatialHash3D * newSpatialHash3D(float cellSize=64.f)
Creates a 3D spatial hash with the given cell size. @ownership Caller deletes.
Definition Spatial.cpp:28
SpatialHash2D * newSpatialHash2D(float cellSize=64.f)
Creates a 2D spatial hash with the given cell size. @ownership Caller deletes.
Definition Spatial.cpp:26
QuadTree * newQuadTree(float minX, float minY, float maxX, float maxY, int maxDepth=8, int maxPerNode=8)
Creates a QuadTree over the given 2D bounds. @ownership Caller deletes.
Definition Spatial.cpp:16
BSPTree3D * newBSPTree3D(float minX, float minY, float minZ, float maxX, float maxY, float maxZ, int maxDepth=12, int maxPerNode=8)
Creates a 3D BSP/kd tree over the given bounds. @ownership Caller deletes.
Definition Spatial.cpp:35
std::unordered_map< std::string, SkillDefinition > & table()
Definition Skill.cpp:65