Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbsptree.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qbsptree_p.h"
6
8
9QBspTree::QBspTree() : depth(6), visited(0) {}
10
11void QBspTree::create(int n, int d)
12{
13 // simple heuristics to find the best tree depth
14 if (d == -1) {
15 int c;
16 for (c = 0; n; ++c)
17 n = n / 10;
18 depth = c << 1;
19 } else {
20 depth = d;
21 }
22 depth = qMax(depth, uint(1));
23
24 nodes.resize((1ll << depth) - 1); // resize to number of nodes
25 leaves.resize(1ll << depth); // resize to number of leaves
26}
27
29{
30 leaves.clear();
31 nodes.clear();
32}
33
34void QBspTree::climbTree(const QRect &rect, callback *function, QBspTreeData data)
35{
36 if (nodes.isEmpty())
37 return;
38 ++visited;
39 climbTree(rect, function, data, 0);
40}
41
42void QBspTree::climbTree(const QRect &area, callback *function, QBspTreeData data, int index)
43{
44 if (index >= nodes.size()) { // the index points to a leaf
45 Q_ASSERT(!nodes.isEmpty());
46 function(leaf(index - nodes.size()), area, visited, data);
47 return;
48 }
49
50 Node::Type t = (Node::Type) nodes.at(index).type;
51
52 int pos = nodes.at(index).pos;
53 int idx = firstChildIndex(index);
54 if (t == Node::VerticalPlane) {
55 if (area.left() < pos)
56 climbTree(area, function, data, idx); // back
57 if (area.right() >= pos)
58 climbTree(area, function, data, idx + 1); // front
59 } else {
60 if (area.top() < pos)
61 climbTree(area, function, data, idx); // back
62 if (area.bottom() >= pos)
63 climbTree(area, function, data, idx + 1); // front
64 }
65}
66
67void QBspTree::init(const QRect &area, int depth, NodeType type, int index)
68{
69 Node::Type t = Node::None; // t should never have this value
70 if (type == Node::Both) // if both planes are specified, use 2d bsp
72 else
73 t = type;
74 QPoint center = area.center();
75 nodes[index].pos = (t == Node::VerticalPlane ? center.x() : center.y());
76 nodes[index].type = t;
77
78 QRect front = area;
79 QRect back = area;
80
81 if (t == Node::VerticalPlane) {
82 front.setLeft(center.x());
83 back.setRight(center.x() - 1); // front includes the center
84 } else { // t == Node::HorizontalPlane
85 front.setTop(center.y());
86 back.setBottom(center.y() - 1);
87 }
88
89 int idx = firstChildIndex(index);
90 if (--depth) {
91 init(back, depth, type, idx);
92 init(front, depth, type, idx + 1);
93 }
94}
95
96void QBspTree::insert(QList<int> &leaf, const QRect &, uint, QBspTreeData data)
97{
98 leaf.append(data.i);
99}
100
101void QBspTree::remove(QList<int> &leaf, const QRect &, uint, QBspTreeData data)
102{
103 int i = leaf.indexOf(data.i);
104 if (i != -1)
105 leaf.remove(i);
106}
107
108QT_END_NAMESPACE
Node::Type NodeType
Definition qbsptree_p.h:36
QBspTree::Data QBspTreeData
Definition qbsptree_p.h:47
void create(int n, int d=-1)
Definition qbsptree.cpp:11
void callback(QList< int > &leaf, const QRect &area, uint visited, QBspTreeData data)
Definition qbsptree_p.h:48
void climbTree(const QRect &rect, callback *function, QBspTreeData data, int index)
Definition qbsptree.cpp:42
void destroy()
Definition qbsptree.cpp:28
int firstChildIndex(int i) const
Definition qbsptree_p.h:69
void climbTree(const QRect &rect, callback *function, QBspTreeData data)
Definition qbsptree.cpp:34
void init(const QRect &area, int depth, NodeType type, int index)
Definition qbsptree.cpp:67
Definition qlist.h:80
\inmodule QtCore\reentrant
Definition qrect.h:31