Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "qbsptree_p.h"
5
7
8QBspTree::QBspTree() : depth(6), visited(0) {}
9
10void QBspTree::create(int n, int d)
11{
12 // simple heuristics to find the best tree depth
13 if (d == -1) {
14 int c;
15 for (c = 0; n; ++c)
16 n = n / 10;
17 depth = c << 1;
18 } else {
19 depth = d;
20 }
21 depth = qMax(depth, uint(1));
22
23 nodes.resize((1ll << depth) - 1); // resize to number of nodes
24 leaves.resize(1ll << depth); // resize to number of leaves
25}
26
28{
29 leaves.clear();
30 nodes.clear();
31}
32
33void QBspTree::climbTree(const QRect &rect, callback *function, QBspTreeData data)
34{
35 if (nodes.isEmpty())
36 return;
37 ++visited;
38 climbTree(rect, function, data, 0);
39}
40
41void QBspTree::climbTree(const QRect &area, callback *function, QBspTreeData data, int index)
42{
43 if (index >= nodes.size()) { // the index points to a leaf
44 Q_ASSERT(!nodes.isEmpty());
45 function(leaf(index - nodes.size()), area, visited, data);
46 return;
47 }
48
49 Node::Type t = (Node::Type) nodes.at(index).type;
50
51 int pos = nodes.at(index).pos;
52 int idx = firstChildIndex(index);
53 if (t == Node::VerticalPlane) {
54 if (area.left() < pos)
55 climbTree(area, function, data, idx); // back
56 if (area.right() >= pos)
57 climbTree(area, function, data, idx + 1); // front
58 } else {
59 if (area.top() < pos)
60 climbTree(area, function, data, idx); // back
61 if (area.bottom() >= pos)
62 climbTree(area, function, data, idx + 1); // front
63 }
64}
65
67{
68 Node::Type t = Node::None; // t should never have this value
69 if (type == Node::Both) // if both planes are specified, use 2d bsp
71 else
72 t = type;
73 QPoint center = area.center();
74 nodes[index].pos = (t == Node::VerticalPlane ? center.x() : center.y());
75 nodes[index].type = t;
76
77 QRect front = area;
78 QRect back = area;
79
80 if (t == Node::VerticalPlane) {
81 front.setLeft(center.x());
82 back.setRight(center.x() - 1); // front includes the center
83 } else { // t == Node::HorizontalPlane
84 front.setTop(center.y());
85 back.setBottom(center.y() - 1);
86 }
87
88 int idx = firstChildIndex(index);
89 if (--depth) {
90 init(back, depth, type, idx);
91 init(front, depth, type, idx + 1);
92 }
93}
94
95void QBspTree::insert(QList<int> &leaf, const QRect &, uint, QBspTreeData data)
96{
97 leaf.append(data.i);
98}
99
100void QBspTree::remove(QList<int> &leaf, const QRect &, uint, QBspTreeData data)
101{
102 int i = leaf.indexOf(data.i);
103 if (i != -1)
104 leaf.remove(i);
105}
106
static void remove(QList< int > &leaf, const QRect &area, uint visited, QBspTreeData data)
Definition qbsptree.cpp:100
void create(int n, int d=-1)
Definition qbsptree.cpp:10
void destroy()
Definition qbsptree.cpp:27
int firstChildIndex(int i) const
Definition qbsptree_p.h:68
void climbTree(const QRect &rect, callback *function, QBspTreeData data)
Definition qbsptree.cpp:33
void init(const QRect &area, NodeType type)
Definition qbsptree_p.h:54
static void insert(QList< int > &leaf, const QRect &area, uint visited, QBspTreeData data)
Definition qbsptree.cpp:95
QList< int > & leaf(int i)
Definition qbsptree_p.h:59
void remove(qsizetype i, qsizetype n=1)
Definition qlist.h:794
void resize(qsizetype size)
Definition qlist.h:403
void append(parameter_type t)
Definition qlist.h:458
void clear()
Definition qlist.h:434
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr void setRight(int pos) noexcept
Sets the right edge of the rectangle to the given x coordinate.
Definition qrect.h:197
constexpr void setBottom(int pos) noexcept
Sets the bottom edge of the rectangle to the given y coordinate.
Definition qrect.h:200
constexpr void setLeft(int pos) noexcept
Sets the left edge of the rectangle to the given x coordinate.
Definition qrect.h:191
constexpr void setTop(int pos) noexcept
Sets the top edge of the rectangle to the given y coordinate.
Definition qrect.h:194
rect
[4]
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction function
static int area(const QSize &s)
Definition qicon.cpp:153
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLint GLenum GLsizei GLsizei GLsizei depth
GLuint index
[2]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLfloat n
const GLubyte * c
GLdouble GLdouble t
Definition qopenglext.h:243
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
unsigned int uint
Definition qtypes.h:34
qsizetype indexOf(const AT &t, qsizetype from=0) const noexcept
Definition qlist.h:962