1// Copyright (C) 2017 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 "computefilteredboundingvolumejob_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,
23 Qt3DRender::Render::Sphere *sphere,
24 Qt3DRender::Render::Entity *node,
25 Qt3DRender::Render::Entity *excludeSubTree)
26{
27 Qt3DRender::Render::Sphere childSphere(*node->worldBoundingVolume());
28 // Go to the nodes that have the most depth
29 const auto &childrenHandles = node->childrenHandles();
30 for (const HEntity &handle : childrenHandles) {
31 Entity *c = manager->renderNodesManager()->data(handle);
32 if (c && c != excludeSubTree)
33 expandWorldBoundingVolume(manager, sphere: &childSphere, node: c, excludeSubTree);
34 }
35 sphere->expandToContain(sphere: childSphere);
36}
37
38} // namespace
39
40class ComputeFilteredBoundingVolumeJobPrivate : public Qt3DCore::QAspectJobPrivate
41{
42public:
43 ComputeFilteredBoundingVolumeJobPrivate(ComputeFilteredBoundingVolumeJob *job) : Qt3DCore::QAspectJobPrivate(), m_job(job) {}
44 ~ComputeFilteredBoundingVolumeJobPrivate() {}
45
46 void postFrame(Qt3DCore::QAspectManager *aspectManager) override;
47
48 ComputeFilteredBoundingVolumeJob *m_job;
49 Qt3DRender::Render::Sphere m_sphere;
50};
51
52ComputeFilteredBoundingVolumeJob::ComputeFilteredBoundingVolumeJob()
53 : Qt3DCore::QAspectJob(*new ComputeFilteredBoundingVolumeJobPrivate(this))
54 , m_root(nullptr)
55 , m_ignoreSubTree(nullptr)
56 , m_manager(nullptr)
57{
58 SET_JOB_RUN_STAT_TYPE(this, JobTypes::ExpandBoundingVolume, 0)
59}
60
61void ComputeFilteredBoundingVolumeJob::setRoot(Entity *root)
62{
63 m_root = root;
64}
65
66void ComputeFilteredBoundingVolumeJob::setManagers(NodeManagers *manager)
67{
68 m_manager = manager;
69}
70
71void ComputeFilteredBoundingVolumeJob::ignoreSubTree(Entity *node)
72{
73 m_ignoreSubTree = node;
74}
75
76void ComputeFilteredBoundingVolumeJob::run()
77{
78 qCDebug(Jobs) << "Entering" << Q_FUNC_INFO << QThread::currentThread();
79 Q_D(ComputeFilteredBoundingVolumeJob);
80 d->m_sphere = {};
81
82 if (!m_root)
83 return;
84 if (!m_ignoreSubTree) {
85 d->m_sphere = *m_root->worldBoundingVolumeWithChildren();
86 return;
87 }
88
89 bool isFilterChildOfRoot = false;
90 Entity *parent = m_ignoreSubTree->parent();
91 while (parent) {
92 if (parent == m_root) {
93 isFilterChildOfRoot = true;
94 break;
95 }
96 parent = parent->parent();
97 }
98 if (!isFilterChildOfRoot) {
99 d->m_sphere = *m_root->worldBoundingVolumeWithChildren();
100 return;
101 }
102
103 expandWorldBoundingVolume(manager: m_manager, sphere: &d->m_sphere, node: m_root, excludeSubTree: m_ignoreSubTree);
104
105 qCDebug(Jobs) << "Exiting" << Q_FUNC_INFO << QThread::currentThread();
106}
107
108void ComputeFilteredBoundingVolumeJob::finished(Qt3DCore::QAspectManager *aspectManager, const Qt3DRender::Render::Sphere &sphere)
109{
110 Q_UNUSED(aspectManager);
111 Q_UNUSED(sphere);
112}
113
114void ComputeFilteredBoundingVolumeJobPrivate::postFrame(Qt3DCore::QAspectManager *aspectManager)
115{
116 m_job->finished(aspectManager, sphere: m_sphere);
117}
118
119} // namespace Render
120} // namespace Qt3DRender
121
122QT_END_NAMESPACE
123

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