Cogs.Core
PotreeBounds.cpp
1#include "Context.h"
2#include "Systems/Core/TransformSystem.h"
3
4#include "PotreeSystem.h"
5
6bool Cogs::Core::PotreeBounds::getBounds(Context* context, const PotreeComponent& poComp, Cogs::Geometry::BoundingBox& bounds, bool ignoreVisibility) const
7{
8 if (!ignoreVisibility && !poComp.isVisible()) return false;
9
10 Cogs::Core::PotreeDataHolder& poDataHolder = poSystem->getData(&poComp);
11 Cogs::Core::PotreeData* poData = poDataHolder.poData.get();
12 assert(poData);
13
14 // Force an update of the octree frame if origin has changed and this is called before
15 // view is invoked (e.g. like figuring out depth bounds in the camera system).
16 poSystem->updateOcttreeFrame(context, *poData, poComp);
17
18 const glm::vec3& tbmin = poData->octtreeFrame.tightBBoxMin;
19 const glm::vec3& tbmax = poData->octtreeFrame.tightBBoxMax;
20 if (poData->state == PotreeState::Running && tbmin.x < tbmax.x) {
21
22 glm::vec3 min_ws = bounds.min;
23 glm::vec3 max_ws = bounds.max;
24
25 const glm::mat4& worldFromOcttreeFrame = poData->worldFromOcttreeFrame;
26 for (unsigned i = 0; i < 8; i++) {
27 glm::vec4 p = worldFromOcttreeFrame * glm::vec4(((i >> 0) & 1) ? tbmin.x : tbmax.x,
28 ((i >> 1) & 1) ? tbmin.y : tbmax.y,
29 ((i >> 2) & 1) ? tbmin.z : tbmax.z,
30 1.f);
31 const glm::vec3 q = (1.f / p.w) * glm::vec3(p);
32 min_ws = glm::min(min_ws, q);
33 max_ws = glm::max(max_ws, q);
34 }
35
36 bounds.min = glm::min(bounds.min, min_ws);
37 bounds.max = glm::max(bounds.max, max_ws);
38 return true;
39 }
40
41 return false;
42}
43
44void Cogs::Core::PotreeBounds::getBounds(Context* context, Cogs::Geometry::BoundingBox& bounds)
45{
46 CpuInstrumentationScope(SCOPE_POTREE, "getBounds");
47 assert(poSystem);
48
49 for (auto& poComp : poSystem->pool) {
50 getBounds(context, poComp, bounds, false);
51 }
52}
53
54bool Cogs::Core::PotreeBounds::getBounds(Context* context, const ComponentModel::Entity* entity, Cogs::Geometry::BoundingBox& bounds, bool ignoreVisibility) const
55{
56 auto poCompPtr = entity->getComponent<PotreeComponent>();
57 if (!poCompPtr) return false;
58 return getBounds(context, *poCompPtr, bounds, ignoreVisibility);
59}
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
T * getComponent() const
Get a pointer to the first component implementing the given type in the entity.
Definition: Entity.h:35
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
void getBounds(Context *context, Cogs::Geometry::BoundingBox &bounds) override
Expand bounds including bounds of all entities in this system in world coordinates.
Component for Point Cloud Display.