Qt 4.8
Classes | Public Types | Public Functions | Protected Functions | Static Protected Functions | Properties | List of all members
QBspTree Class Reference

#include <qbsptree_p.h>

Classes

struct  Data
 
struct  Node
 

Public Types

typedef void callback(QVector< int > &leaf, const QRect &area, uint visited, QBspTreeData data)
 
typedef Node::Type NodeType
 
typedef QBspTree::Data QBspTreeData
 

Public Functions

void climbTree (const QRect &rect, callback *function, QBspTreeData data)
 
void create (int n, int d=-1)
 
void destroy ()
 
void init (const QRect &area, NodeType type)
 
void insertLeaf (const QRect &r, int i)
 
QVector< int > & leaf (int i)
 
int leafCount () const
 
 QBspTree ()
 
void removeLeaf (const QRect &r, int i)
 

Protected Functions

void climbTree (const QRect &rect, callback *function, QBspTreeData data, int index)
 
int firstChildIndex (int i) const
 
void init (const QRect &area, int depth, NodeType type, int index)
 
int parentIndex (int i) const
 

Static Protected Functions

static void insert (QVector< int > &leaf, const QRect &area, uint visited, QBspTreeData data)
 
static void remove (QVector< int > &leaf, const QRect &area, uint visited, QBspTreeData data)
 

Properties

uint depth
 
QVector< QVector< int > > leaves
 
QVector< Nodenodes
 
uint visited
 

Detailed Description

Definition at line 61 of file qbsptree_p.h.

Typedefs

◆ callback

typedef void QBspTree::callback(QVector< int > &leaf, const QRect &area, uint visited, QBspTreeData data)

Definition at line 84 of file qbsptree_p.h.

◆ NodeType

Definition at line 72 of file qbsptree_p.h.

◆ QBspTreeData

Definition at line 83 of file qbsptree_p.h.

Constructors and Destructors

◆ QBspTree()

QBspTree::QBspTree ( )

Definition at line 46 of file qbsptree.cpp.

46 : depth(6), visited(0) {}
uint depth
Definition: qbsptree_p.h:111
uint visited
Definition: qbsptree_p.h:112

Functions

◆ climbTree() [1/2]

void QBspTree::climbTree ( const QRect rect,
callback function,
QBspTreeData  data 
)

Definition at line 71 of file qbsptree.cpp.

Referenced by climbTree(), init(), insertLeaf(), QIconModeViewBase::intersectingSet(), and removeLeaf().

72 {
73  if (nodes.isEmpty())
74  return;
75  ++visited;
76  climbTree(rect, function, data, 0);
77 }
static const char * data(const QByteArray &arr)
void climbTree(const QRect &rect, callback *function, QBspTreeData data)
Definition: qbsptree.cpp:71
QVector< Node > nodes
Definition: qbsptree_p.h:113
uint visited
Definition: qbsptree_p.h:112

◆ climbTree() [2/2]

void QBspTree::climbTree ( const QRect rect,
callback function,
QBspTreeData  data,
int  index 
)
protected

Definition at line 79 of file qbsptree.cpp.

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 }
QVector< int > & leaf(int i)
Definition: qbsptree_p.h:96
int firstChildIndex(int i) const
Definition: qbsptree_p.h:105
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
static const char * data(const QByteArray &arr)
void climbTree(const QRect &rect, callback *function, QBspTreeData data)
Definition: qbsptree.cpp:71
QVector< Node > nodes
Definition: qbsptree_p.h:113
uint visited
Definition: qbsptree_p.h:112
quint16 index
static int area(const QSize &s)
Definition: qicon.cpp:155

◆ create()

void QBspTree::create ( int  n,
int  d = -1 
)

Definition at line 48 of file qbsptree.cpp.

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 }
double d
Definition: qnumeric_p.h:62
unsigned char c[8]
Definition: qnumeric_p.h:62
QVector< QVector< int > > leaves
Definition: qbsptree_p.h:114
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
uint depth
Definition: qbsptree_p.h:111
unsigned int uint
Definition: qglobal.h:996
QVector< Node > nodes
Definition: qbsptree_p.h:113

◆ destroy()

void QBspTree::destroy ( )

Definition at line 65 of file qbsptree.cpp.

66 {
67  leaves.clear();
68  nodes.clear();
69 }
QVector< QVector< int > > leaves
Definition: qbsptree_p.h:114
void clear()
Removes all the elements from the vector and releases the memory used by the vector.
Definition: qvector.h:347
QVector< Node > nodes
Definition: qbsptree_p.h:113

◆ firstChildIndex()

int QBspTree::firstChildIndex ( int  i) const
inlineprotected

Definition at line 105 of file qbsptree_p.h.

Referenced by climbTree(), and init().

105 { return ((i * 2) + 1); }

◆ init() [1/2]

void QBspTree::init ( const QRect area,
NodeType  type 
)
inline

Definition at line 91 of file qbsptree_p.h.

Referenced by init(), and removeLeaf().

91 { init(area, depth, type, 0); }
int type
Definition: qmetatype.cpp:239
void init(const QRect &area, NodeType type)
Definition: qbsptree_p.h:91
uint depth
Definition: qbsptree_p.h:111

◆ init() [2/2]

void QBspTree::init ( const QRect area,
int  depth,
NodeType  type,
int  index 
)
protected

Definition at line 104 of file qbsptree.cpp.

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
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 }
int type
Definition: qmetatype.cpp:239
void setBottom(int pos)
Sets the bottom edge of the rectangle to the given y coordinate.
Definition: qrect.h:267
void init(const QRect &area, NodeType type)
Definition: qbsptree_p.h:91
int firstChildIndex(int i) const
Definition: qbsptree_p.h:105
void setTop(int pos)
Sets the top edge of the rectangle to the given y coordinate.
Definition: qrect.h:261
void setRight(int pos)
Sets the right edge of the rectangle to the given x coordinate.
Definition: qrect.h:264
uint depth
Definition: qbsptree_p.h:111
QVector< Node > nodes
Definition: qbsptree_p.h:113
Q_CORE_EXPORT QTextStream & center(QTextStream &s)
QPoint center() const
Returns the center point of the rectangle.
Definition: qrect.h:300
void setLeft(int pos)
Sets the left edge of the rectangle to the given x coordinate.
Definition: qrect.h:258
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
quint16 index
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
static int area(const QSize &s)
Definition: qicon.cpp:155

◆ insert()

void QBspTree::insert ( QVector< int > &  leaf,
const QRect area,
uint  visited,
QBspTreeData  data 
)
staticprotected

Definition at line 133 of file qbsptree.cpp.

Referenced by firstChildIndex(), and insertLeaf().

134 {
135  leaf.append(data.i);
136 }
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
static const char * data(const QByteArray &arr)

◆ insertLeaf()

void QBspTree::insertLeaf ( const QRect r,
int  i 
)
inline

Definition at line 97 of file qbsptree_p.h.

97 { climbTree(r, &insert, i, 0); }
static void insert(QVector< int > &leaf, const QRect &area, uint visited, QBspTreeData data)
Definition: qbsptree.cpp:133
void climbTree(const QRect &rect, callback *function, QBspTreeData data)
Definition: qbsptree.cpp:71

◆ leaf()

QVector<int>& QBspTree::leaf ( int  i)
inline

Definition at line 96 of file qbsptree_p.h.

Referenced by climbTree(), and firstChildIndex().

96 { return leaves[i]; }
QVector< QVector< int > > leaves
Definition: qbsptree_p.h:114

◆ leafCount()

int QBspTree::leafCount ( ) const
inline

Definition at line 95 of file qbsptree_p.h.

95 { return leaves.count(); }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< QVector< int > > leaves
Definition: qbsptree_p.h:114

◆ parentIndex()

int QBspTree::parentIndex ( int  i) const
inlineprotected

Definition at line 104 of file qbsptree_p.h.

104 { return (i & 1) ? ((i - 1) / 2) : ((i - 2) / 2); }

◆ remove()

void QBspTree::remove ( QVector< int > &  leaf,
const QRect area,
uint  visited,
QBspTreeData  data 
)
staticprotected

Definition at line 138 of file qbsptree.cpp.

139 {
140  int i = leaf.indexOf(data.i);
141  if (i != -1)
142  leaf.remove(i);
143 }
void remove(int i)
Removes the element at index position i.
Definition: qvector.h:374
static const char * data(const QByteArray &arr)
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the vector, searching forward from ind...
Definition: qvector.h:698

◆ removeLeaf()

void QBspTree::removeLeaf ( const QRect r,
int  i 
)
inline

Definition at line 98 of file qbsptree_p.h.

98 { climbTree(r, &remove, i, 0); }
void climbTree(const QRect &rect, callback *function, QBspTreeData data)
Definition: qbsptree.cpp:71

Properties

◆ depth

uint QBspTree::depth
private

Definition at line 111 of file qbsptree_p.h.

Referenced by create(), init(), and removeLeaf().

◆ leaves

QVector< QVector<int> > QBspTree::leaves
mutableprivate

Definition at line 114 of file qbsptree_p.h.

Referenced by create(), destroy(), leaf(), and leafCount().

◆ nodes

QVector<Node> QBspTree::nodes
private

Definition at line 113 of file qbsptree_p.h.

Referenced by climbTree(), create(), destroy(), and init().

◆ visited

uint QBspTree::visited
mutableprivate

Definition at line 112 of file qbsptree_p.h.

Referenced by climbTree(), and firstChildIndex().


The documentation for this class was generated from the following files: