bool remove(const Bounds& area, const Bounds& b) { if (m_leaf) return area.contains(m_data.item); int cdel = 0; for (int i = 0; i < 4; ++i) { Node** c = m_data.children + i; if (!*c) continue; Bounds cb = b.quadrant(i); if (area.intersects(cb) && (*c)->remove(area, cb)) { delete *c; * c = 0; ++cdel; } } return cdel == 4; // true if all sub-nodes were removed }
void iterate(QuadTree::Iteration& iter, const Bounds& area, const Bounds& b) const { if (m_leaf && b.contains(m_data.item)) iter.process(m_data.item); else for (int i = 0; i < 4; ++i) { if (m_data.children[i]) { Bounds cb = b.quadrant(i); if (area.intersects(cb)) m_data.children[i]->iterate(iter, area, cb); } } }
void insert(const QuadTree::Item& item, Bounds& b) { if (m_leaf) { m_leaf = false; QuadTree::Item prev_item = m_data.item; std::memset(m_data.children, 0, sizeof(m_data.children)); insert(prev_item, b); // Split } std::pair<int, Bounds> bq = b.quadrant(item); Node** c = m_data.children + bq.first; if (*c) (*c)->insert(item, bq.second); // Branch else *c = new Node(item); // New leaf }