Some QuadTree fixes.
authorDavid Gow <[email protected]>
Wed, 6 Aug 2014 16:09:23 +0000 (00:09 +0800)
committerDavid Gow <[email protected]>
Wed, 6 Aug 2014 16:09:23 +0000 (00:09 +0800)
src/Makefile
src/document.cpp
src/document.h
src/quadtree.cpp

index f0733d4..75781a9 100644 (file)
@@ -42,7 +42,7 @@ single : $(BIN)
 double : REAL = 1
 double : $(BIN)
 
-DEF = -DREAL=$(REALTYPE) -DQUADTREE_REMOVED
+DEF = -DREAL=$(REALTYPE) -DQUADTREE_DISABLED
 
 demo : $(BIN) ../tools/stream_plot.py
        mkdir -p ../data/
index 7c49074..b229baf 100644 (file)
@@ -92,10 +92,11 @@ void Document::GenBaseQuadtree()
 {
        m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QTC_UNKNOWN, 0, ObjectCount()});
        m_quadtree.root_id = 0;
-       GenQuadNode(0, QTC_TOP_LEFT);
+       GenQuadChild(0, QTC_TOP_LEFT);
+       GenQuadParent(0, QTC_BOTTOM_RIGHT);
 }
 
-QuadTreeIndex Document::GenQuadNode(QuadTreeIndex parent, QuadTreeNodeChildren type)
+QuadTreeIndex Document::GenQuadChild(QuadTreeIndex parent, QuadTreeNodeChildren type)
 {
        QuadTreeIndex new_index = m_quadtree.nodes.size();
        m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, parent, type, 0, 0});
@@ -132,6 +133,41 @@ QuadTreeIndex Document::GenQuadNode(QuadTreeIndex parent, QuadTreeNodeChildren t
        return new_index;
 }
 
+// Reparent a quadtree node, making it the "type" child of a new node.
+QuadTreeIndex Document::GenQuadParent(QuadTreeIndex child, QuadTreeNodeChildren type)
+{
+       QuadTreeIndex new_index = m_quadtree.nodes.size();
+       m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, -1, QTC_UNKNOWN, 0, 0});
+
+       m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size();
+       for (unsigned i = m_quadtree.nodes[child].object_begin; i < m_quadtree.nodes[child].object_end; ++i)
+       {
+               m_objects.bounds.push_back(TransformFromQuadChild(m_objects.bounds[i], type));
+               m_objects.types.push_back(m_objects.types[i]);
+               m_objects.data_indices.push_back(m_objects.data_indices[i]);
+               m_count++;
+       }
+       m_quadtree.nodes[new_index].object_end = m_objects.bounds.size();
+       switch (type)
+       {
+               case QTC_TOP_LEFT:
+                       m_quadtree.nodes[new_index].top_left = child;
+                       break;
+               case QTC_TOP_RIGHT:
+                       m_quadtree.nodes[new_index].top_right = child;
+                       break;
+               case QTC_BOTTOM_LEFT:
+                       m_quadtree.nodes[new_index].bottom_left = child;
+                       break;
+               case QTC_BOTTOM_RIGHT:
+                       m_quadtree.nodes[new_index].bottom_right = child;
+                       break;
+               default:
+                       Fatal("Tried to add a QuadTree child of invalid type!");
+       }
+       return new_index;
+}
+
 #endif
 
 void Document::Load(const string & filename)
index 07dc33b..14a87d8 100644 (file)
@@ -31,7 +31,8 @@ namespace IPDF
 
 #ifndef QUADTREE_DISABLED
                        inline const QuadTree& GetQuadTree() { if (m_quadtree.root_id == QUADTREE_EMPTY) { GenBaseQuadtree(); } return m_quadtree; }
-                       QuadTreeIndex GenQuadNode(QuadTreeIndex parent, QuadTreeNodeChildren type);
+                       QuadTreeIndex GenQuadChild(QuadTreeIndex parent, QuadTreeNodeChildren type);
+                       QuadTreeIndex GenQuadParent(QuadTreeIndex child, QuadTreeNodeChildren mytype);
 #endif
 
                private:
index e93a0f0..04630b7 100644 (file)
@@ -12,11 +12,11 @@ Rect TransformToQuadChild(const Rect& src, QuadTreeNodeChildren child_type)
        dst.h *= 2;
        if (child_type == QTC_BOTTOM_LEFT || child_type == QTC_BOTTOM_RIGHT)
        {
-               dst.x -= 1;
+               dst.y -= 1;
        }
        if (child_type == QTC_TOP_RIGHT || child_type == QTC_BOTTOM_RIGHT)
        {
-               dst.y -= 1;
+               dst.x -= 1;
        }
        return dst;
 }
@@ -30,11 +30,11 @@ Rect TransformFromQuadChild(const Rect& src, QuadTreeNodeChildren child_type)
        dst.h *= 0.5;
        if (child_type == QTC_BOTTOM_LEFT || child_type == QTC_BOTTOM_RIGHT)
        {
-               dst.x += 1;
+               dst.y += 1;
        }
        if (child_type == QTC_TOP_RIGHT || child_type == QTC_BOTTOM_RIGHT)
        {
-               dst.y += 1;
+               dst.x += 1;
        }
        return dst;
 }

UCC git Repository :: git.ucc.asn.au