8 #include <unordered_map>
9 #include <unordered_set>
25 using std::unordered_map;
26 using std::unordered_set;
36 OctTreePartition::OctTreePartition()
57 vector<PartitionTreeNode*>* thisEntityPartitions =
nullptr;
60 thisEntityPartitions = &thisEntityPartitionsIt->second;
62 if (thisEntityPartitions !=
nullptr && thisEntityPartitions->empty() ==
false) {
80 Console::println(
"OctTreePartition::addEntity(): too many entities: " + to_string(uniquePartitionId) +
" >= " + to_string(
visibleEntitiesBitSet.size()));
92 auto minXPartition =
static_cast<int32_t
>(Math::floor(boundingBox->getMin().getX() /
PARTITION_SIZE_MAX));
93 auto minYPartition =
static_cast<int32_t
>(Math::floor(boundingBox->getMin().getY() /
PARTITION_SIZE_MAX));
94 auto minZPartition =
static_cast<int32_t
>(Math::floor(boundingBox->getMin().getZ() /
PARTITION_SIZE_MAX));
95 auto maxXPartition =
static_cast<int32_t
>(Math::floor(boundingBox->getMax().getX() /
PARTITION_SIZE_MAX));
96 auto maxYPartition =
static_cast<int32_t
>(Math::floor(boundingBox->getMax().getY() /
PARTITION_SIZE_MAX));
97 auto maxZPartition =
static_cast<int32_t
>(Math::floor(boundingBox->getMax().getZ() /
PARTITION_SIZE_MAX));
98 for (
auto yPartition = minYPartition; yPartition <= maxYPartition; yPartition++)
99 for (
auto xPartition = minXPartition; xPartition <= maxXPartition; xPartition++)
100 for (
auto zPartition = minZPartition; zPartition <= maxZPartition; zPartition++) {
108 vector<PartitionTreeNode*>* objectPartitionsVector =
nullptr;
111 objectPartitionsVector = &objectPartitionsVectorIt->second;
113 if (objectPartitionsVector ==
nullptr || objectPartitionsVector->empty() ==
true) {
115 "OctTreePartition::removeEntity(): '" +
130 while (objectPartitionsVector->size() > 0) {
131 auto lastIdx = objectPartitionsVector->size() - 1;
132 auto partitionTreeNode = (*objectPartitionsVector)[lastIdx];
133 auto& partitionEntities = partitionTreeNode->partitionEntities;
134 partitionEntities.erase(remove(partitionEntities.begin(), partitionEntities.end(), entity), partitionEntities.end());
135 objectPartitionsVector->erase(objectPartitionsVector->begin() + lastIdx);
136 if (partitionEntities.empty() ==
true) {
137 auto rootPartitionTreeNode = partitionTreeNode;
138 while (rootPartitionTreeNode->parent !=
nullptr) rootPartitionTreeNode = rootPartitionTreeNode->parent;
145 if ((
void*)&treeRootSubNodeIt == (
void*)rootPartitionTreeNode) {
150 tuple<int32_t, int32_t, int32_t> key = {
151 rootPartitionTreeNode->x,
152 rootPartitionTreeNode->y,
153 rootPartitionTreeNode->z
174 for (
auto i = 0; i < indent; i++) Console::print(
"\t");
175 Console::println(to_string(node->
x) +
"/" + to_string(node->
y) +
"/" + to_string(node->
z) +
": ");
177 for (
auto i = 0; i < indent + 1; i++) Console::print(
"\t");
178 Console::println(entity->getId());
185 if (nodeEntity == entity) Console::println(
"OctTreePartition::findEntity(): found entity: " + entity->
getId());
virtual const string & getId()=0
void setUniquePartitionId(int uniquePartitionId)
Set unique partition id.
virtual BoundingBox * getWorldBoundingBox()=0
static constexpr int UNIQUEPARTITIONID_NONE
int getUniquePartitionId()
Oct tree partition implementation.
unordered_map< Entity *, vector< PartitionTreeNode * > > entityPartitionNodes
bool isPartitionNodeEmpty(PartitionTreeNode *node)
Is partition empty.
void removeEntity(Entity *entity) override
Removes a entity.
void dumpNode(PartitionTreeNode *node, int indent)
Dump node to console.
const vector< Entity * > & getVisibleEntities(Frustum *frustum) override
Get visible entities.
vector< int > freeEntityUniquePartitionIds
PartitionTreeNode treeRoot
unordered_map< Entity *, int > entityUniquePartitionIdMapping
void removePartitionNode(PartitionTreeNode *node)
Remove partition node, should be empty.
void addEntity(Entity *entity) override
Adds a entity.
void dump()
Dump oct tree to console.
bitset< 1024 *512 > visibleEntitiesBitSet
void updatePartitionTree(PartitionTreeNode *parent, int32_t x, int32_t y, int32_t z, float partitionSize, Entity *entity)
Update partition tree.
int32_t doPartitionTreeLookUpVisibleObjects(Frustum *frustum, PartitionTreeNode *node)
Do partition tree lookup.
void findEntity(PartitionTreeNode *node, Entity *entity)
Find entity.
static constexpr float PARTITION_SIZE_MAX
void reset() override
Reset.
vector< Entity * > visibleEntities
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
list< PartitionTreeNode > subNodes
unordered_map< tuple< int32_t, int32_t, int32_t >, PartitionTreeNode *, PartitionTreeNodeId_Hash > subNodesByCoordinate
PartitionTreeNode * parent
vector< Entity * > partitionEntities