00001 #ifndef QUADTREE_H 00002 #define QUADTREE_H 00003 00004 #include <cmath> 00005 #include "QuadTreeNode.h" 00006 #include "Point.h" 00007 #include <QRectF> 00008 #include <QVector2D> 00009 #include <QGraphicsItem> 00010 00011 namespace Elve { 00012 00013 constexpr unsigned long QUADTREESIZE = (1UL+2UL*2UL+4UL*4UL+8UL*8UL+16UL*16UL+32UL*32UL+64UL*64UL+128UL*128UL); 00014 00015 class QuadTree 00016 { 00017 public: 00018 QuadTree(const QRectF& bounds); 00019 QuadTree& operator=(QuadTree&& other); 00020 bool addPoint(const Point* m); 00021 void removePoint(const Point* r); 00022 void movePoint(const Point* r); 00023 void clear(); 00024 00025 void reinsertAll(); 00026 00027 QVector2D gravityFor(const Point &m) const; 00028 00029 void setBounds(const QRectF& bounds); 00030 00031 QuadTreeNode* rootNode() const; 00032 void reset(); 00033 QRectF bounds() const; 00034 void debug(QPainter* p) const; 00035 ~QuadTree(); 00036 private: 00037 bool init(const QRectF& bounds); 00038 bool _addPoint(const Point* m); 00039 bool initLite(const QRectF& bounds); 00040 bool initNodesPos(const QRectF& bounds); 00041 QRectF computeMinRect(); 00042 QuadTreeParams mParams; 00043 QuadTreeNode* mNodes; 00044 QuadTreeNode* mLevels[8]; 00045 QVector2D mPos; 00046 qreal mRadius; 00047 qreal mInverseRadius; 00048 std::set<const Point*> mPoints; 00049 }; 00050 00051 inline QRectF unionRectVec(const QRectF& rect, const QVector2D& pos) { 00052 qreal left = std::min((float)rect.left(),pos.x()); 00053 qreal top = std::min((float)rect.top(),pos.y()); 00054 qreal right = std::max((float)rect.right(),pos.x()); 00055 qreal bottom = std::max((float)rect.bottom(),pos.y()); 00056 return QRectF( 00057 left, 00058 top, 00059 right-left, 00060 bottom-top 00061 ); 00062 } 00063 00064 } 00065 00066 #endif // INSTANTQUADTREE_H