+void Document::OverlayQuadChildren(QuadTreeIndex orig_parent, QuadTreeIndex parent, QuadTreeNodeChildren type)
+{
+ QuadTreeIndex new_index = m_quadtree.nodes.size();
+ Debug("-------------- Generating Quadtree Node %d (orig %d parent %d, type %d) ----------------------", new_index, orig_parent, parent, type);
+ m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, orig_parent, type, 0, 0, -1});
+
+ m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size();
+ for (unsigned i = m_quadtree.nodes[parent].object_dirty; i < m_quadtree.nodes[parent].object_end; ++i)
+ {
+ if (IntersectsQuadChild(m_objects.bounds[i], type))
+ {
+ m_count += ClipObjectToQuadChild(i, type);
+ }
+ }
+ m_quadtree.nodes[new_index].object_end = m_objects.bounds.size();
+ QuadTreeIndex orig_node = -1;
+ switch (type)
+ {
+ case QTC_TOP_LEFT:
+ orig_node = m_quadtree.nodes[orig_parent].top_left;
+ break;
+ case QTC_TOP_RIGHT:
+ orig_node = m_quadtree.nodes[orig_parent].top_right;
+ break;
+ case QTC_BOTTOM_LEFT:
+ orig_node = m_quadtree.nodes[orig_parent].bottom_left;
+ break;
+ case QTC_BOTTOM_RIGHT:
+ orig_node = m_quadtree.nodes[orig_parent].bottom_right;
+ break;
+ default:
+ Fatal("Tried to overlay a QuadTree child of invalid type!");
+ }
+ if (orig_node == -1)
+ Fatal("Tried to overlay a QuadTree child that didn't exist!");
+
+ // Add us to the node's overlay linked list.
+ QuadTreeIndex prev_overlay = orig_node;
+ while (m_quadtree.nodes[prev_overlay].next_overlay != -1) prev_overlay = m_quadtree.nodes[prev_overlay].next_overlay;
+ Debug("- Original node %d, Previous overlay %d, new overlay %d", orig_node, prev_overlay, new_index);
+ m_quadtree.nodes[prev_overlay].next_overlay = new_index;
+
+ // Recurse into any extant children.
+ if (m_quadtree.nodes[orig_node].top_left != -1)
+ OverlayQuadChildren(orig_node, new_index, QTC_TOP_LEFT);
+ if (m_quadtree.nodes[orig_node].top_right != -1)
+ OverlayQuadChildren(orig_node, new_index, QTC_TOP_RIGHT);
+ if (m_quadtree.nodes[orig_node].bottom_left != -1)
+ OverlayQuadChildren(orig_node, new_index, QTC_BOTTOM_LEFT);
+ if (m_quadtree.nodes[orig_node].bottom_right != -1)
+ OverlayQuadChildren(orig_node, new_index, QTC_BOTTOM_RIGHT);
+
+ m_quadtree.nodes[new_index].object_dirty = m_quadtree.nodes[new_index].object_end;
+ m_quadtree.nodes[new_index].next_overlay = -1;
+}
+
+void Document::PropagateQuadChanges(QuadTreeIndex node)
+{
+ for(QuadTreeIndex overlay = node; overlay != -1; overlay = m_quadtree.nodes[overlay].next_overlay)
+ {
+ // We don't care about clean overlays.
+ if (m_quadtree.nodes[overlay].object_dirty == m_quadtree.nodes[overlay].object_end) continue;
+ // Recurse into any extant children.
+ if (m_quadtree.nodes[node].top_left != -1)
+ OverlayQuadChildren(node, overlay, QTC_TOP_LEFT);
+ if (m_quadtree.nodes[node].top_right != -1)
+ OverlayQuadChildren(node, overlay, QTC_TOP_RIGHT);
+ if (m_quadtree.nodes[node].bottom_left != -1)
+ OverlayQuadChildren(node, overlay, QTC_BOTTOM_LEFT);
+ if (m_quadtree.nodes[node].bottom_right != -1)
+ OverlayQuadChildren(node, overlay, QTC_BOTTOM_RIGHT);
+
+ m_quadtree.nodes[overlay].object_dirty = m_quadtree.nodes[overlay].object_end;
+ }
+}
+