guillaume 2026.6.1
A C++ UI framework designed for modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
measure_mesh.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include <utility/graphic/model.hpp>
24#include <utility/ressource_provider.hpp>
25
26#include "guillaume/systems/measure_mesh.hpp"
27
28namespace guillaume::systems
29{
31 std::shared_ptr<utility::RessourceProvider> ressourceProvider,
32 utility::Engine &engine)
33 : ecs::SystemFiller<components::Mesh, components::Bound,
34 components::Transform>(ecs::Phase::Measure)
35 , _ressourceProvider(ressourceProvider)
36 , _engine(engine)
37 {
38 }
39
43
45 {
46 }
47
49 {
50 }
51
52 void MeasureMesh::update(const ecs::Entity::Identifier &entityIdentifier)
53 {
54 getLogger().debug()
55 << "Updating MeasureMesh system for entity " << entityIdentifier;
56 if (!requireComponent<components::Mesh>(entityIdentifier)
57 || !requireComponent<components::Bound>(entityIdentifier)
58 || !requireComponent<components::Transform>(entityIdentifier)) {
59 return;
60 }
61
62 const auto &modelComponent =
63 getComponent<components::Mesh>(entityIdentifier);
64 auto &boundComponent =
65 getComponent<components::Bound>(entityIdentifier);
66 auto &transformComponent =
68
69 MeasureMeshCacheKey cacheKey { modelComponent.getMeshPath() };
70
71 getLogger().debug() << "Measuring model '" << cacheKey.meshPath
72 << "' for entity " << entityIdentifier;
73
74 if (contains(cacheKey)) {
75 getLogger().debug() << "Mesh '" << cacheKey.meshPath
76 << "' already measured, skipping.";
77 const auto &cacheValue = get(cacheKey);
78 if (cacheValue.has_value()) {
79 const float scale = modelComponent.scaleFor(
80 cacheValue->getWidth(), cacheValue->getHeight());
81 getComponent<components::Bound>(entityIdentifier)
82 .setWidth(cacheValue->getWidth() * scale)
83 .setHeight(cacheValue->getHeight() * scale);
84 }
85 return;
86 }
87
88 auto model = _ressourceProvider->loadModel(cacheKey.meshPath);
89 if (model == nullptr) {
90 getLogger().warning()
91 << "Failed to load model '" << cacheKey.meshPath
92 << "' for entity " << entityIdentifier;
93 return;
94 }
95 auto boundingBox = model->computeBoundingSize();
96 const float scale = modelComponent.scaleFor(boundingBox.getWidth(),
97 boundingBox.getHeight());
98
99 getComponent<components::Bound>(entityIdentifier)
100 .setWidth(boundingBox.getWidth() * scale)
101 .setHeight(boundingBox.getHeight() * scale);
102
103 put(cacheKey, boundingBox);
104 }
105
106} // namespace guillaume::systems
std::size_t Identifier
The key under which an entity's components are stored. Systems and components refer to entities by th...
Definition entity.hpp:86
ComponentType & getComponent(const ecs::Entity::Identifier &entityIdentifier)
Get a component of an entity from the bound registry, to read or modify it.
bool requireComponent(const ecs::Entity::Identifier &entityIdentifier)
Tell whether an entity has a component, and log a warning naming the entity and the component when it...
virtual void update(const ecs::Entity::Identifier &entityIdentifier) override
Write the size of one model into its components::Bound, loading and measuring the file when it is not...
void prepare(void) override
Does nothing: the system needs no per-frame setup.
~MeasureMesh(void)
Destroy the system and its cache.
void cleanup(void) override
Does nothing: the cache is kept from one frame to the next.
MeasureMesh(std::shared_ptr< utility::RessourceProvider > ressourceProvider, utility::Engine &engine)
Create the system in the Measure phase, with an empty cache.
State. Every class here is plain data attached to an entity.
The entity-component-system core, independent of any widget.
Definition component.hpp:28
Rules. Every class here runs each frame on the entities that have the components it declares.
Key of the size cache of MeasureMesh: the path of the model file.
std::string meshPath
Path of the model file that was measured.