+ m_document_dirty = true;
+ return new_index;
+}
+
+void Document::OverlayQuadChildren(QuadTreeIndex orig_parent, QuadTreeIndex parent, QuadTreeNodeChildren type)
+{
+ PROFILE_SCOPE("Document::OverlayQuadChildren()");
+ 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, true});
+
+ 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;
+ m_document_dirty = true;
+}
+
+void Document::OverlayQuadParent(QuadTreeIndex orig_child, QuadTreeIndex child, QuadTreeNodeChildren type)
+{
+ PROFILE_SCOPE("Document::OverlayQuadParent()");
+ 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, -1, true});
+
+ m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size();
+ m_quadtree.nodes[new_index].object_dirty = m_objects.bounds.size();
+ for (QuadTreeIndex overlay = child; overlay != -1; overlay = m_quadtree.nodes[overlay].next_overlay)
+ {
+ for (unsigned i = m_quadtree.nodes[overlay].object_begin; i < m_quadtree.nodes[overlay].object_end; ++i)
+ {
+ Rect new_bounds = TransformFromQuadChild(m_objects.bounds[i], type);
+ // If the object is too small to be seen, discard it.
+ if (!new_bounds.w || !new_bounds.h) continue;
+ m_objects.bounds.push_back(new_bounds);
+ 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();
+ QuadTreeIndex orig_node = m_quadtree.nodes[orig_child].parent;
+ 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;
+ m_quadtree.nodes[prev_overlay].next_overlay = new_index;
+ Debug("OverlayQuadParent(%d, %d, %d) = %d", orig_child, child, type, new_index);
+
+ // Recurse into any extant parent.
+ if (m_quadtree.nodes[orig_node].parent != -1)
+ OverlayQuadParent(orig_node, new_index, m_quadtree.nodes[orig_node].child_type);
+
+ m_quadtree.nodes[new_index].object_dirty = m_quadtree.nodes[new_index].object_end;
+ m_quadtree.nodes[new_index].next_overlay = -1;
+ m_document_dirty = true;
+}
+
+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 our parent, should we have any.
+ if (m_quadtree.nodes[node].parent != -1)
+ OverlayQuadParent(node, overlay, m_quadtree.nodes[node].child_type);
+ // 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;
+ }
+}
+
+// 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, -1, true});
+
+ m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size();
+ for (QuadTreeIndex overlay = child; overlay != -1; overlay = m_quadtree.nodes[overlay].next_overlay)
+ {
+ for (unsigned i = m_quadtree.nodes[overlay].object_begin; i < m_quadtree.nodes[overlay].object_end; ++i)
+ {
+ Rect new_bounds = TransformFromQuadChild(m_objects.bounds[i], type);
+ // If the object is too small to be seen, discard it.
+ if (!new_bounds.w || !new_bounds.h) continue;
+ m_objects.bounds.push_back(new_bounds);
+ 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();
+ m_quadtree.nodes[new_index].object_dirty = 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!");
+ }