1// Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "expandboundingvolumejob_p.h"
5
6#include <Qt3DRender/private/entity_p.h>
7#include <Qt3DRender/private/renderlogging_p.h>
8#include <Qt3DRender/private/sphere_p.h>
9#include <Qt3DRender/private/job_common_p.h>
10#include <Qt3DRender/private/managers_p.h>
11#include <Qt3DRender/private/nodemanagers_p.h>
12
13#include <QThread>
14
15QT_BEGIN_NAMESPACE
16
17namespace Qt3DRender {
18namespace Render {
19
20namespace {
21
22void expandWorldBoundingVolume(NodeManagers *manager, Entity *node)
23{
24 // Go to the nodes that have the most depth
25 const auto &childrenHandles = node->childrenHandles();
26 for (const HEntity &handle : childrenHandles) {
27 Entity *c = manager->renderNodesManager()->data(handle);
28 if (c && c->isEnabled())
29 expandWorldBoundingVolume(manager, node: c);
30 }
31
32 // Then traverse back from leaf to root
33 // Initialize parent bounding volume to be equal to that of the first child
34 if (!childrenHandles.empty()) {
35 Qt3DRender::Render::Sphere *parentBoundingVolume = node->worldBoundingVolumeWithChildren();
36 for (const HEntity &handle : childrenHandles) {
37 Entity *c = manager->renderNodesManager()->data(handle);
38 if (c && c->isEnabled())
39 parentBoundingVolume->expandToContain(sphere: *c->worldBoundingVolumeWithChildren());
40 }
41 }
42}
43
44}
45
46ExpandBoundingVolumeJob::ExpandBoundingVolumeJob()
47 : m_node(nullptr)
48 , m_manager(nullptr)
49{
50 SET_JOB_RUN_STAT_TYPE(this, JobTypes::ExpandBoundingVolume, 0)
51}
52
53void ExpandBoundingVolumeJob::setRoot(Entity *root)
54{
55 m_node = root;
56}
57
58void ExpandBoundingVolumeJob::setManagers(NodeManagers *manager)
59{
60 m_manager = manager;
61}
62
63void ExpandBoundingVolumeJob::run()
64{
65 // Expand worldBoundingVolumeWithChildren of each node that has children by the
66 // bounding volumes of the children.
67
68 // TODO: Implement this using a parallel_for
69 qCDebug(Jobs) << "Entering" << Q_FUNC_INFO << QThread::currentThread();
70 expandWorldBoundingVolume(manager: m_manager, node: m_node);
71 qCDebug(Jobs) << "Exiting" << Q_FUNC_INFO << QThread::currentThread();
72}
73
74} // namespace Render
75} // namespace Qt3DRender
76
77QT_END_NAMESPACE
78

source code of qt3d/src/render/jobs/expandboundingvolumejob.cpp