5#include <unordered_map>
6#include <unordered_set>
23using std::unordered_map;
24using std::unordered_set;
78 void reset()
override;
96 string key = to_string(x) +
"," + to_string(y) +
"," + to_string(z);
98 auto storedNode = storedNodeIt != parent->
subNodesByCoordinate.end()?storedNodeIt->second:
nullptr;
101 if (storedNode ==
nullptr) {
115 x * partitionSize + partitionSize,
116 y * partitionSize + partitionSize,
117 z * partitionSize + partitionSize
122 storedNode = &parent->
subNodes.back();
132 auto minXPartition =
static_cast<int32_t
>((Math::floor(boundingBox->getMin().getX() - x * partitionSize) / (partitionSize / 2.0f)));
133 auto minYPartition =
static_cast<int32_t
>((Math::floor(boundingBox->getMin().getY() - y * partitionSize) / (partitionSize / 2.0f)));
134 auto minZPartition =
static_cast<int32_t
>((Math::floor(boundingBox->getMin().getZ() - z * partitionSize) / (partitionSize / 2.0f)));
135 auto maxXPartition =
static_cast<int32_t
>((Math::floor(boundingBox->getMax().getX() - x * partitionSize) / (partitionSize / 2.0f)));
136 auto maxYPartition =
static_cast<int32_t
>((Math::floor(boundingBox->getMax().getY() - y * partitionSize) / (partitionSize / 2.0f)));
137 auto maxZPartition =
static_cast<int32_t
>((Math::floor(boundingBox->getMax().getZ() - z * partitionSize) / (partitionSize / 2.0f)));
138 minXPartition = Math::clamp(minXPartition, 0, 1);
139 minYPartition = Math::clamp(minYPartition, 0, 1);
140 minZPartition = Math::clamp(minZPartition, 0, 1);
141 maxXPartition = Math::clamp(maxXPartition, 0, 1);
142 maxYPartition = Math::clamp(maxYPartition, 0, 1);
143 maxZPartition = Math::clamp(maxZPartition, 0, 1);
144 for (
auto yPartition = minYPartition; yPartition <= maxYPartition; yPartition++)
145 for (
auto xPartition = minXPartition; xPartition <= maxXPartition; xPartition++)
146 for (
auto zPartition = minZPartition; zPartition <= maxZPartition; zPartition++) {
149 static_cast<int32_t
>((x * partitionSize) / (partitionSize / 2.0f) + xPartition),
150 static_cast<int32_t
>((y * partitionSize) / (partitionSize / 2.0f) + yPartition),
151 static_cast<int32_t
>((z * partitionSize) / (partitionSize / 2.0f) + zPartition),
152 partitionSize / 2.0f, entity
156 storedNode->partitionEntities.push_back(entity);
171 for (
auto& subNode: node->
subNodes) {
187 Console::println(
"OctTreePartition::removePartitionNode(): partition has objects attached!!!");
191 for (
auto& subNode: node->
subNodes) {
213 for (
auto& subNode: node->
subNodes) {
225 if (frustum->
isVisible(entity->getBoundingBoxTransformed()) ==
false)
continue;
246 void dumpNode(PartitionTreeNode* node,
int indent);
virtual const string & getId()=0
virtual BoundingBox * getBoundingBoxTransformed()=0
bool isVisible(const Vector3 &vertex)
Checks if given vertex is in frustum.
Oct tree partition implementation.
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.
PartitionTreeNode treeRoot
void removePartitionNode(PartitionTreeNode *node)
Remove partition node, should be empty.
void updateEntity(Entity *entity) override
Updates a entity.
unordered_set< Entity * > visibleEntitiesSet
static constexpr float PARTITION_SIZE_MIN
void addEntity(Entity *entity) override
Adds a entity.
void dump()
Dump oct tree to console.
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.
unordered_map< string, vector< PartitionTreeNode * > > entityPartitionNodes
static constexpr float PARTITION_SIZE_MAX
bool isVisibleEntity(Entity *entity) override
Check if entity is visible.
void reset() override
Reset.
OctTreePartition()
Public constructor.
VectorIteratorMultiple< Entity * > entityIterator
vector< Entity * > visibleEntities
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
void update()
Updates this bounding box.
Vector3 & set(float x, float y, float z)
Set up vector.
Vector iterator template to be used to iterate multiple vectors at a single invocation.
list< PartitionTreeNode > subNodes
unordered_map< string, PartitionTreeNode * > subNodesByCoordinate
PartitionTreeNode * parent
vector< Entity * > partitionEntities