Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
7
QT_BEGIN_NAMESPACE
8
9
QBspTree::QBspTree() : depth(6), visited(0) {}
10
11
void
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
28
void
QBspTree
::
destroy
()
29
{
30
leaves.clear();
31
nodes.clear();
32
}
33
34
void
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
42
void
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
67
void
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
71
t = (depth & 1) ?
Node
::
HorizontalPlane
:
Node
::
VerticalPlane
;
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
96
void
QBspTree
::insert(
QList
<
int
> &leaf,
const
QRect &, uint,
QBspTreeData
data)
97
{
98
leaf.append(data.i);
99
}
100
101
void
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
108
QT_END_NAMESPACE
QBspTree
Definition
qbsptree_p.h:26
QBspTree::NodeType
Node::Type NodeType
Definition
qbsptree_p.h:36
QBspTree::QBspTreeData
QBspTree::Data QBspTreeData
Definition
qbsptree_p.h:47
QBspTree::create
void create(int n, int d=-1)
Definition
qbsptree.cpp:11
QBspTree::callback
void callback(QList< int > &leaf, const QRect &area, uint visited, QBspTreeData data)
Definition
qbsptree_p.h:48
QBspTree::climbTree
void climbTree(const QRect &rect, callback *function, QBspTreeData data, int index)
Definition
qbsptree.cpp:42
QBspTree::destroy
void destroy()
Definition
qbsptree.cpp:28
QBspTree::firstChildIndex
int firstChildIndex(int i) const
Definition
qbsptree_p.h:69
QBspTree::climbTree
void climbTree(const QRect &rect, callback *function, QBspTreeData data)
Definition
qbsptree.cpp:34
QBspTree::init
void init(const QRect &area, int depth, NodeType type, int index)
Definition
qbsptree.cpp:67
QBspTree::leaf
QList< int > & leaf(int i)
Definition
qbsptree_p.h:60
QList
Definition
qlist.h:82
QRect
\inmodule QtCore\reentrant
Definition
qrect.h:32
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
QBspTree::Node
Definition
qbsptree_p.h:30
QBspTree::Node::Type
Type
Definition
qbsptree_p.h:31
QBspTree::Node::None
@ None
Definition
qbsptree_p.h:31
QBspTree::Node::HorizontalPlane
@ HorizontalPlane
Definition
qbsptree_p.h:31
QBspTree::Node::Both
@ Both
Definition
qbsptree_p.h:31
QBspTree::Node::VerticalPlane
@ VerticalPlane
Definition
qbsptree_p.h:31
qtbase
src
widgets
itemviews
qbsptree.cpp
Generated on
for Qt by
1.18.0