source: trunk/src/gui/itemviews/qbsptree.cpp@ 112

Last change on this file since 112 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 4.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qbsptree_p.h"
43
44QT_BEGIN_NAMESPACE
45
46QBspTree::QBspTree() : depth(6), visited(0) {}
47
48void QBspTree::create(int n, int d)
49{
50 // simple heuristics to find the best tree depth
51 if (d == -1) {
52 int c;
53 for (c = 0; n; ++c)
54 n = n / 10;
55 depth = c << 1;
56 } else {
57 depth = d;
58 }
59 depth = qMax(depth, uint(1));
60
61 nodes.resize((1 << depth) - 1); // resize to number of nodes
62 leaves.resize(1 << depth); // resize to number of leaves
63}
64
65void QBspTree::destroy()
66{
67 leaves.clear();
68 nodes.clear();
69}
70
71void QBspTree::climbTree(const QRect &rect, callback *function, QBspTreeData data)
72{
73 if (nodes.isEmpty())
74 return;
75 ++visited;
76 climbTree(rect, function, data, 0);
77}
78
79void QBspTree::climbTree(const QRect &area, callback *function, QBspTreeData data, int index)
80{
81 if (index >= nodes.count()) { // the index points to a leaf
82 Q_ASSERT(!nodes.isEmpty());
83 function(leaf(index - nodes.count()), area, visited, data);
84 return;
85 }
86
87 Node::Type t = (Node::Type) nodes.at(index).type;
88
89 int pos = nodes.at(index).pos;
90 int idx = firstChildIndex(index);
91 if (t == Node::VerticalPlane) {
92 if (area.left() < pos)
93 climbTree(area, function, data, idx); // back
94 if (area.right() >= pos)
95 climbTree(area, function, data, idx + 1); // front
96 } else {
97 if (area.top() < pos)
98 climbTree(area, function, data, idx); // back
99 if (area.bottom() >= pos)
100 climbTree(area, function, data, idx + 1); // front
101 }
102}
103
104void QBspTree::init(const QRect &area, int depth, NodeType type, int index)
105{
106 Node::Type t = Node::None; // t should never have this value
107 if (type == Node::Both) // if both planes are specified, use 2d bsp
108 t = (depth & 1) ? Node::HorizontalPlane : Node::VerticalPlane;
109 else
110 t = type;
111 QPoint center = area.center();
112 nodes[index].pos = (t == Node::VerticalPlane ? center.x() : center.y());
113 nodes[index].type = t;
114
115 QRect front = area;
116 QRect back = area;
117
118 if (t == Node::VerticalPlane) {
119 front.setLeft(center.x());
120 back.setRight(center.x() - 1); // front includes the center
121 } else { // t == Node::HorizontalPlane
122 front.setTop(center.y());
123 back.setBottom(center.y() - 1);
124 }
125
126 int idx = firstChildIndex(index);
127 if (--depth) {
128 init(back, depth, type, idx);
129 init(front, depth, type, idx + 1);
130 }
131}
132
133void QBspTree::insert(QVector<int> &leaf, const QRect &, uint, QBspTreeData data)
134{
135 leaf.append(data.i);
136}
137
138void QBspTree::remove(QVector<int> &leaf, const QRect &, uint, QBspTreeData data)
139{
140 int i = leaf.indexOf(data.i);
141 if (i != -1)
142 leaf.remove(i);
143}
144
145QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.