10#include "ecs/component/core/hierarchy.h"
11#include "ecs/component/core/name.h"
12#include "ecs/component/core/transform.h"
14namespace Vkm::Engine::HierarchyOperations {
28void setParent(Scene& scene, EntityId child, EntityId parent, EntityId before = {});
38void removeFromParent(Scene& scene, EntityId entity);
49glm::mat4 computeWorldMatrix(
const Scene& scene, EntityId entity);
57constexpr uint32_t MAX_DEPTH = 32;
68void warnWalkBound(
const char* walk,
size_t bound);
85 SearchQueue(
const SearchQueue& other) =
delete;
86 SearchQueue& operator=(
const SearchQueue& other) =
delete;
88 SearchQueue(SearchQueue && other) =
delete;
89 SearchQueue& operator=(SearchQueue && other) =
delete;
92 std::vector<EntityId>& entries() {
return m_queue; }
95 std::vector<EntityId> m_queue;
112void forEachChild(
const Scene& scene, EntityId parent, Fn&& fn) {
113 const Hierarchy* node = scene.tryGet<Hierarchy>(parent);
116 const size_t bound = scene.entityCount();
118 for (EntityId child = node->firstChild; child; ++step) {
120 warnWalkBound(
"sibling-list walk", bound);
124 const Hierarchy* held = scene.tryGet<Hierarchy>(child);
125 const EntityId next = held ? held->nextSibling : EntityId{};
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);
151 const Hierarchy* node = scene.tryGet<Hierarchy>(at);
168template<
typename Match>
169EntityId findInSelfOrDescendantsIf(
const Scene& scene, EntityId root, Match match) {
170 if (!root)
return {};
171 if (match(root))
return root;
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);
183 forEachChild(scene, pending[i], [&](EntityId child) {
184 if (!found && match(child)) found = child;
185 pending.push_back(child);
187 if (found)
return found;
203EntityId findInSelfOrDescendants(
const Scene& scene, EntityId root) {
204 return findInSelfOrDescendantsIf(scene, root, [&](EntityId
id) {
return scene.has<T>(id); });
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;
234EntityId findInSelfOrAncestors(
const Scene& scene, EntityId leaf) {
236 forSelfAndAncestors(scene, leaf, [&](EntityId at) {
237 if (scene.has<T>(at)) found = at;
254inline bool isAncestorOf(
const Scene& scene, EntityId ancestor, EntityId node) {
255 if (!ancestor || !node || ancestor == node)
return false;
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);
273void destroyHierarchy(Scene& scene, EntityId entity);
287std::vector<EntityId> collectSubtree(
const Scene& scene, EntityId root);