47 #define RTREE_REINSERT_P 0.30 48 #define RTREE_CHOOSE_SUBTREE_P 32 51 #define RSTAR_TEMPLATE 55 template <
typename BoundedItem,
typename LeafType>
63 template <
typename BoundedItem>
65 std::vector< BoundedItem* >
items;
85 std::size_t dimensions, std::size_t min_child_items, std::size_t max_child_items
106 static_assert(1 <= min_child_items && min_child_items <= max_child_items/2,
107 "Wrong parameters in RStarTree template.");
127 Leaf * newLeaf =
new Leaf();
128 newLeaf->bound = bound;
135 m_root->hasLeaves =
true;
138 m_root->items.reserve(min_child_items);
139 m_root->items.push_back(newLeaf);
140 m_root->bound = bound;
144 InsertInternal(newLeaf, m_root);
214 template <
typename Acceptor,
typename Visitor>
219 QueryFunctor<Acceptor, Visitor> query(accept, visitor);
244 template <
typename Acceptor,
typename LeafRemover>
245 void Remove(
const Acceptor &accept, LeafRemover leafRemover)
247 std::list<Leaf*> itemsToReinsert;
252 RemoveFunctor<Acceptor, LeafRemover>
remove(accept, leafRemover, &itemsToReinsert, &m_size);
253 remove(m_root,
true);
255 if (!itemsToReinsert.empty())
264 for(;it !=
end; it++)
265 InsertInternal(*it, m_root);
272 Remove(AcceptEnclosing(bound), RemoveLeaf());
277 void RemoveItem(
const LeafType &item,
bool removeDuplicates =
true )
279 Remove( AcceptAny(), RemoveSpecificLeaf(item, removeDuplicates));
283 std::size_t
GetSize()
const {
return m_size; }
295 if (static_cast<Node*>(node->
items[0])->hasLeaves)
323 return static_cast<Node*
>(* std::min_element(node->
items.begin(), node->
items.end(),
335 return static_cast<Node*
>(* std::min_element( node->
items.begin(), node->
items.end(),
348 node->bound.stretch(leaf->bound);
355 node->
items.push_back(leaf);
365 Node * tmp_node = InsertInternal( leaf, ChooseSubtree(node, &leaf->bound), firstInsert );
371 node->
items.push_back(tmp_node);
377 if (node->
items.size() > max_child_items )
385 return OverflowTreatment(node, firstInsert);
398 if (level != m_root && firstInsert)
404 Node * splitItem = Split(level);
409 Node * newRoot =
new Node();
413 newRoot->
items.reserve(min_child_items);
414 newRoot->
items.push_back(m_root);
415 newRoot->
items.push_back(splitItem);
418 newRoot->bound.reset();
438 Node * newNode =
new Node();
441 const std::size_t n_items = node->
items.size();
442 const std::size_t distribution_count = n_items - 2*min_child_items + 1;
444 std::size_t split_axis = dimensions+1, split_edge = 0, split_index = 0;
445 int split_margin = 0;
450 assert(n_items == max_child_items + 1);
451 assert(distribution_count > 0);
452 assert(min_child_items + distribution_count-1 <= n_items);
463 for (std::size_t axis = 0; axis < dimensions; axis++)
467 double overlap = 0, dist_area, dist_overlap;
468 std::size_t dist_edge = 0, dist_index = 0;
480 for (std::size_t edge = 0; edge < 2; edge++)
491 for (std::size_t k = 0; k < distribution_count; k++)
513 if (overlap < dist_overlap || (overlap == dist_overlap && area < dist_area))
517 dist_index = min_child_items+k;
518 dist_overlap = overlap;
525 if (split_axis == dimensions+1 || split_margin > margin )
528 split_margin = margin;
529 split_edge = dist_edge;
530 split_index = dist_index;
544 else if (split_axis != dimensions-1)
548 newNode->
items.assign(node->
items.begin() + split_index, node->
items.end());
549 node->
items.erase(node->
items.begin() + split_index, node->
items.end());
555 newNode->bound.reset();
565 std::vector< BoundedItem* > removed_items;
567 const std::size_t n_items = node->
items.size();
573 assert(n_items == max_child_items + 1);
577 std::partial_sort(node->
items.begin(), node->
items.end() - p, node->
items.end(),
581 removed_items.assign(node->
items.end() - p, node->
items.end());
592 InsertInternal( static_cast<Leaf*>(*it), m_root,
false);
601 template <
typename Acceptor,
typename Visitor>
602 struct VisitFunctor : std::unary_function< const BoundingBox *, void > {
611 Leaf *
leaf =
static_cast<Leaf*
>(item);
620 template <
typename Acceptor,
typename Visitor>
629 Node * node =
static_cast<Node*
>(item);
636 for_each(node->
items.begin(), node->
items.end(), *
this);
652 template <
typename Acceptor,
typename LeafRemover>
654 std::unary_function< const BoundingBox *, bool >
661 accept(a), remove(r), size(s) {}
664 Leaf *
leaf =
static_cast<Leaf *
>(item);
666 if (accept(leaf) &&
remove(
leaf))
678 template <
typename Acceptor,
typename LeafRemover>
680 std::unary_function< const BoundedItem *, bool >
691 explicit RemoveFunctor(
const Acceptor &na, LeafRemover &lr, std::list<Leaf*>* ir, std::size_t * size)
692 : accept(na), remove(lr), itemsToReinsert(ir), m_size(size) {}
696 Node * node =
static_cast<Node*
>(item);
704 node->
items.erase(std::remove_if(node->
items.begin(), node->
items.end(), *
this), node->
items.end() );
708 if (node->
items.empty())
714 else if (node->
items.size() < min_child_items)
717 QueueItemsToReinsert(node);
721 else if (node->
items.empty())
746 for(; it !=
end; it++)
747 itemsToReinsert->push_back(static_cast<Leaf*>(*it));
750 for (; it !=
end; it++)
751 QueueItemsToReinsert(static_cast<Node*>(*it));
764 #undef RSTAR_TEMPLATE 767 #undef RTREE_REINSERT_P 768 #undef RTREE_CHOOSE_SUBTREE_P
Node * ChooseSubtree(Node *node, const BoundingBox *bound)
void operator()(BoundedItem *item)
void Remove(const Acceptor &accept, LeafRemover leafRemover)
Removes item(s) from the tree.
std::size_t GetDimensions() const
#define RTREE_CHOOSE_SUBTREE_P
double overlap(const RStarBoundingBox< dimensions > &bb) const
Implementation of an RTree with an R* index.
void Insert(LeafType leaf, const BoundingBox &bound)
std::list< Leaf * > * itemsToReinsert
RStarAcceptOverlapping< Node, Leaf > AcceptOverlapping
std::vector< BoundedItem * > items
void Reinsert(Node *node)
RStarLeaf< BoundedItem, LeafType > Leaf
RemoveFunctor(const Acceptor &na, LeafRemover &lr, std::list< Leaf * > *ir, std::size_t *size)
BoundedItem::BoundingBox BoundingBox
std::size_t GetSize() const
bool operator()(BoundedItem *item, bool isRoot=false)
RStarRemoveLeaf< Leaf > RemoveLeaf
void QueueItemsToReinsert(Node *node)
RStarAcceptAny< Node, Leaf > AcceptAny
VisitFunctor(const Acceptor &a, Visitor &v)
RStarBoundedItem< dimensions > BoundedItem
RStarAcceptEnclosing< Node, Leaf > AcceptEnclosing
RStarRemoveSpecificLeaf< Leaf > RemoveSpecificLeaf
bool operator()(BoundedItem *item) const
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
void operator()(BoundedItem *item)
RemoveLeafFunctor(const Acceptor &a, LeafRemover &r, std::size_t *s)
Visitor Query(const Acceptor &accept, Visitor visitor)
Touches each node using the visitor pattern.
QueryFunctor(const Acceptor &a, Visitor &v)
Node * OverflowTreatment(Node *level, bool firstInsert)
void RemoveItem(const LeafType &item, bool removeDuplicates=true)
void RemoveBoundedArea(const BoundingBox &bound)
RStarNode< BoundedItem > Node
Node * InsertInternal(Leaf *leaf, Node *node, bool firstInsert=true)