Cogs.Core
PointVisualizationSystem.cpp
1#include "PointVisualizationSystem.h"
2
3#include "Context.h"
4
5#include "ExtensionRegistry.h"
6#include "Resources/MeshManager.h"
7#include "Resources/MaterialManager.h"
8#include "Components/Core/MeshComponent.h"
9#include "Components/Core/MeshRenderComponent.h"
10#include "Components/Core/SceneComponent.h"
11#include "Components/PointDataComponent.h"
12
13#include "Foundation/ComponentModel/Entity.h"
14#include "Foundation/Memory/MemoryBuffer.h"
15
17{
19
20 for (auto & pvizComp : pool) {
21 auto & pvizData = getData(&pvizComp);
22
23 auto * sceneComp = pvizComp.getComponent<SceneComponent>();
24 if (!sceneComp || sceneComp->visible == false) continue;
25
26 if (!pvizComp.pointData) continue;
27 auto pointComp = pvizComp.pointData->getComponent<PointDataComponent>();
28 if (pointComp == nullptr) continue;
29
30 if ((pvizData.pointPositionGeneration != pointComp->positions.getGeneration())
31 || (pvizData.pointDataGeneration != pointComp->auxData0.getGeneration())
32 || sceneComp->hasFieldChanged(&SceneComponent::visible)) {
33 pvizData.needsUpdate = true;
34 }
35
36 if (pointComp->hasChanged()) {
37 auto mrndComp = pvizComp.getComponent<MeshRenderComponent>();
38 mrndComp->material = pvizComp.material;
39 pvizData.needsUpdate = true;
40 }
41
42 if (pvizData.needsUpdate) {
43 if (!HandleIsValid(pvizData.mesh)) {
44 pvizData.mesh = context->meshManager->create();
45
46 auto meshComp = pvizComp.getComponent<MeshComponent>();
47 meshComp->meshHandle = pvizData.mesh;
48 }
49
50 if (!pointComp->positions || pointComp->positions.empty()) {
51 pvizData.mesh->clear();
52 } else {
53 const auto Nv = pointComp->positions.size();
54 auto & mesh = pvizData.mesh;
55
56 if (!pointComp->data.empty()) {
57 auto bbox = Cogs::Geometry::makeEmptyBoundingBox<Cogs::Geometry::BoundingBox>();
58
59 tmp.resize(Nv, false);
60 for (size_t i = 0; i < Nv; i++) {
61 const auto p = pointComp->positions[i];
62 const auto w = pointComp->data[i];
63 bbox.min = glm::min(bbox.min, p);
64 bbox.max = glm::max(bbox.max, p);
65 tmp[i] = glm::vec4(p, w);
66 }
67
68 mesh->setVertexData(tmp.data(), Nv, VertexFormats::Pos4f);
69 mesh->primitiveType = PrimitiveType::PointList;
70 if ((pointComp->extentMin.x <= pointComp->extentMax.x) &&
71 (pointComp->extentMin.y <= pointComp->extentMax.y) &&
72 (pointComp->extentMin.z <= pointComp->extentMax.z)) {
73 auto bbox_ = Cogs::Geometry::makeEmptyBoundingBox<Cogs::Geometry::BoundingBox>();
74 bbox_.min = pointComp->extentMin;
75 bbox_.max = pointComp->extentMax;
76 mesh->setBounds(bbox_);
77 }
78 } else {
79 mesh->setVertexData(pointComp->positions.data(), Nv, VertexFormats::Pos3f);
80 mesh->primitiveType = PrimitiveType::PointList;
81 if ((pointComp->extentMin.x <= pointComp->extentMax.x) &&
82 (pointComp->extentMin.y <= pointComp->extentMax.y) &&
83 (pointComp->extentMin.z <= pointComp->extentMax.z)) {
84 auto bbox = Cogs::Geometry::makeEmptyBoundingBox<Cogs::Geometry::BoundingBox>();
85 bbox.min = pointComp->extentMin;
86 bbox.max = pointComp->extentMax;
87 mesh->setBounds(bbox);
88 }
89 }
90 }
91 }
92 }
93}
ComponentType * getComponent() const
Definition: Component.h:159
Context * context
Pointer to the Context instance the system lives in.
void update()
Updates the system state to that of the current frame.
ComponentPool< ComponentType > pool
Pool of components managed by the system.
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Contains a handle to a Mesh resource to use when rendering using the MeshRenderComponent.
Definition: MeshComponent.h:15
MeshHandle meshHandle
Handle to a Mesh resource to use when rendering.
Definition: MeshComponent.h:29
Renders the contents of a MeshComponent using the given materials.
MaterialInstanceHandle material
Material used to render the mesh.
Contains information on how the entity behaves in the scene.
bool visible
If the entity this component is a member of should be visible.
bool HandleIsValid(const ResourceHandle_t< T > &handle)
Check if the given resource is valid, that is not equal to NoHandle or InvalidHandle.
void clear()
Clear all data from the Mesh, returning it to its initial non-indexed state with no streams and no su...
Definition: Mesh.cpp:27
@ PointList
List of points.
Definition: Common.h:124