vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
hierarchy_operations.h
1#pragma once
2
3#include <cstddef>
4#include <string_view>
5#include <vector>
6
7#include <glm/glm.hpp>
8
9#include "ecs/scene.h"
10#include "ecs/component/core/hierarchy.h"
11#include "ecs/component/core/name.h"
12#include "ecs/component/core/transform.h"
13
14namespace Vkm::Engine::HierarchyOperations {
15
28void setParent(Scene& scene, EntityId child, EntityId parent, EntityId before = {});
29
38void removeFromParent(Scene& scene, EntityId entity);
39
49glm::mat4 computeWorldMatrix(const Scene& scene, EntityId entity);
50
57constexpr uint32_t MAX_DEPTH = 32;
58
68void warnWalkBound(const char* walk, size_t bound);
69
70namespace detail {
71
80class SearchQueue {
81 public:
82 SearchQueue();
83 ~SearchQueue();
84
85 SearchQueue(const SearchQueue& other) = delete;
86 SearchQueue& operator=(const SearchQueue& other) = delete;
87
88 SearchQueue(SearchQueue && other) = delete;
89 SearchQueue& operator=(SearchQueue && other) = delete;
90
91 public:
92 std::vector<EntityId>& entries() { return m_queue; }
93
94 private:
95 std::vector<EntityId> m_queue;
96};
97
98} // namespace detail
99
111template<typename Fn>
112void forEachChild(const Scene& scene, EntityId parent, Fn&& fn) {
113 const Hierarchy* node = scene.tryGet<Hierarchy>(parent);
114 if (!node) return;
115
116 const size_t bound = scene.entityCount();
117 size_t step = 0;
118 for (EntityId child = node->firstChild; child; ++step) {
119 if (step >= bound) {
120 warnWalkBound("sibling-list walk", bound);
121 return;
122 }
123 // Read before fn, which may remove this child's Hierarchy.
124 const Hierarchy* held = scene.tryGet<Hierarchy>(child);
125 const EntityId next = held ? held->nextSibling : EntityId{};
126 fn(child);
127 child = next;
128 }
129}
130
142template<typename Fn>
143void forSelfAndAncestors(const Scene& scene, EntityId entity, Fn&& fn) {
144 EntityId at = entity;
145 for (uint32_t step = 0; at; ++step) {
146 if (step >= MAX_DEPTH) {
147 warnWalkBound("ancestor walk", MAX_DEPTH);
148 return;
149 }
150 if (!fn(at)) return;
151 const Hierarchy* node = scene.tryGet<Hierarchy>(at);
152 if (!node) return;
153 at = node->parent;
154 }
155}
156
168template<typename Match>
169EntityId findInSelfOrDescendantsIf(const Scene& scene, EntityId root, Match match) {
170 if (!root) return {};
171 if (match(root)) return root;
172
173 detail::SearchQueue queue;
174 std::vector<EntityId>& pending = queue.entries();
175 pending.push_back(root);
176 const size_t bound = scene.entityCount();
177 for (size_t i = 0; i < pending.size(); ++i) {
178 if (pending.size() > bound) {
179 warnWalkBound("breadth-first search", bound);
180 return {};
181 }
182 EntityId found{};
183 forEachChild(scene, pending[i], [&](EntityId child) {
184 if (!found && match(child)) found = child;
185 pending.push_back(child);
186 });
187 if (found) return found;
188 }
189 return {};
190}
191
202template<typename T>
203EntityId findInSelfOrDescendants(const Scene& scene, EntityId root) {
204 return findInSelfOrDescendantsIf(scene, root, [&](EntityId id) { return scene.has<T>(id); });
205}
206
218inline EntityId findNamed(const Scene& scene, EntityId root, std::string_view name) {
219 return findInSelfOrDescendantsIf(scene, root, [&](EntityId id) {
220 const Name* named = scene.tryGet<Name>(id);
221 return named && name == named->value;
222 });
223}
224
233template<typename T>
234EntityId findInSelfOrAncestors(const Scene& scene, EntityId leaf) {
235 EntityId found{};
236 forSelfAndAncestors(scene, leaf, [&](EntityId at) {
237 if (scene.has<T>(at)) found = at;
238 return !found;
239 });
240 return found;
241}
242
254inline bool isAncestorOf(const Scene& scene, EntityId ancestor, EntityId node) {
255 if (!ancestor || !node || ancestor == node) return false;
256
257 const Hierarchy* at = scene.tryGet<Hierarchy>(node);
258 for (size_t step = 0; at && at->parent && step < scene.entityCount(); ++step) {
259 if (at->parent == ancestor) return true;
260 at = scene.tryGet<Hierarchy>(at->parent);
261 }
262 return false;
263}
264
273void destroyHierarchy(Scene& scene, EntityId entity);
274
287std::vector<EntityId> collectSubtree(const Scene& scene, EntityId root);
288
289} // namespace Vkm::Engine::HierarchyOperations