MuseScore  3.4
Music composition and notation
bsp.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2002-2011 Werner Schweer
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2
9 // as published by the Free Software Foundation and appearing in
10 // the file LICENCE.GPL
11 //
12 // This code is from Qt implementation of QGraphicsItem
13 // Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
14 //=============================================================================
15 
16 #ifndef __BSP_H__
17 #define __BSP_H__
18 
19 namespace Ms {
20 
21 class BspTreeVisitor;
22 class InsertItemBspTreeVisitor;
23 class RemoveItemBspTreeVisitor;
24 class FindItemBspTreeVisitor;
25 
26 class Element;
27 
28 //---------------------------------------------------------
29 // BspTree
30 // binary space partitioning
31 //---------------------------------------------------------
32 
33 class BspTree
34  {
35  public:
36  struct Node {
37  enum class Type : char { HORIZONTAL, VERTICAL, LEAF };
38  union {
39  qreal offset;
40  int leafIndex;
41  };
43  };
44  private:
45  uint depth;
46  void initialize(const QRectF& rect, int depth, int index);
47  void climbTree(BspTreeVisitor* visitor, const QPointF& pos, int index = 0);
48  void climbTree(BspTreeVisitor* visitor, const QRectF& rect, int index = 0);
49 
50  void findItems(QList<Element*>* foundItems, const QRectF& rect, int index);
51  void findItems(QList<Element*>* foundItems, const QPointF& pos, int index);
52  QRectF rectForIndex(int index) const;
53 
54  QVector<Node> nodes;
55  QVector<QList<Element*> > leaves;
56  int leafCnt;
57  QRectF rect;
58 
59  public:
60  BspTree();
61 
62  void initialize(const QRectF& rect, int depth);
63  void clear();
64 
65  void insert(Element* item);
66  void remove(Element* item);
67 
68  QList<Element*> items(const QRectF& rect);
69  QList<Element*> items(const QPointF& pos);
70 
71  int leafCount() const { return leafCnt; }
72  inline int firstChildIndex(int index) const { return index * 2 + 1; }
73 
74  inline int parentIndex(int index) const {
75  return index > 0 ? ((index & 1) ? ((index - 1) / 2) : ((index - 2) / 2)) : -1;
76  }
77 #ifndef NDEBUG
78  QString debug(int index) const;
79 #endif
80  };
81 
82 //---------------------------------------------------------
83 // BspTreeVisitor
84 //---------------------------------------------------------
85 
87  {
88  public:
89  virtual ~BspTreeVisitor() {}
90  virtual void visit(QList<Element*>* items) = 0;
91  };
92 
93 } // namespace Ms
94 #endif
int leafIndex
Definition: bsp.h:40
Definition: bsp.h:33
void initialize(const QRectF &rect, int depth, int index)
Definition: bsp.cpp:205
QRectF rect
Definition: bsp.h:57
QVector< Node > nodes
Definition: bsp.h:54
uint depth
Definition: bsp.h:45
void climbTree(BspTreeVisitor *visitor, const QPointF &pos, int index=0)
Definition: bsp.cpp:256
Base class of score layout elements.
Definition: element.h:158
QString debug(int index) const
Definition: bsp.cpp:173
void findItems(QList< Element *> *foundItems, const QRectF &rect, int index)
QVector< QList< Element * > > leaves
Definition: bsp.h:55
void insert(Element *item)
Definition: bsp.cpp:115
int parentIndex(int index) const
Definition: bsp.h:74
Definition: bsp.h:86
int firstChildIndex(int index) const
Definition: bsp.h:72
QRectF rectForIndex(int index) const
Definition: bsp.cpp:325
Definition: aeolus.cpp:26
virtual ~BspTreeVisitor()
Definition: bsp.h:89
BspTree()
Definition: bsp.cpp:69
Definition: bsp.h:36
QList< Element * > items(const QRectF &rect)
Definition: bsp.cpp:137
void clear()
Definition: bsp.cpp:104
qreal offset
Definition: bsp.h:39
Type type
Definition: bsp.h:42
Type
Definition: bsp.h:37
int leafCnt
Definition: bsp.h:56
int leafCount() const
Definition: bsp.h:71