SkRTree.h revision 1f45e934b68a5985b2127ec871ff593c3bfc7c2e
11f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 21f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com/* 31f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * Copyright 2012 Google Inc. 41f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * 51f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * Use of this source code is governed by a BSD-style license that can be 61f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * found in the LICENSE file. 71f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com */ 81f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 91f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com#ifndef SkRTree_DEFINED 101f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com#define SkRTree_DEFINED 111f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 121f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com#include "SkRect.h" 131f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com#include "SkTDArray.h" 141f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com#include "SkChunkAlloc.h" 151f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com#include "SkBBoxHierarchy.h" 161f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 171f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com/** 181f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * An R-Tree implementation. In short, it is a balanced n-ary tree containing a hierarchy of 191f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * bounding rectangles. 201f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * 211f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * Much like a B-Tree it maintains balance by enforcing minimum and maximum child counts, and 221f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * splitting nodes when they become overfull. Unlike B-trees, however, we're using spatial data; so 231f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * there isn't a canonical ordering to use when choosing insertion locations and splitting 241f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * distributions. A variety of heuristics have been proposed for these problems; here, we're using 251f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * something resembling an R*-tree, which attempts to minimize area and overlap during insertion, 261f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * and aims to minimize a combination of margin, overlap, and area when splitting. 271f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * 281f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * One detail that is thus far unimplemented that may improve tree quality is attempting to remove 291f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * and reinsert nodes when they become full, instead of immediately splitting (nodes that may have 301f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * been placed well early on may hurt the tree later when more nodes have been added; removing 311f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * and reinserting nodes generally helps reduce overlap and make a better tree). Deletion of nodes 321f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * is also unimplemented. 331f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * 341f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * For more details see: 351f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * 361f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * Beckmann, N.; Kriegel, H. P.; Schneider, R.; Seeger, B. (1990). "The R*-tree: 371f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * an efficient and robust access method for points and rectangles" 381f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * 391f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * It also supports bulk-loading from a batch of bounds and values; if you don't require the tree 401f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * to be usable in its intermediate states while it is being constructed, this is significantly 411f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * quicker than individual insertions and produces more consistent trees. 421f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com */ 431f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.comclass SkRTree : public SkBBoxHierarchy { 441f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.compublic: 451f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 461f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com /** 471f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * Create a new R-Tree with specified min/max child counts. 481f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * The child counts are valid iff: 491f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * - (max + 1) / 2 >= min (splitting an overfull node must be enough to populate 2 nodes) 501f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * - min < max 511f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * - min > 0 521f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * - max < SK_MaxU16 531f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com */ 541f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com static SkRTree* Create(int minChildren, int maxChildren); 551f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com virtual ~SkRTree(); 561f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 571f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com /** 581f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * Insert a node, consisting of bounds and a data value into the tree, if we don't immediately 591f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * need to use the tree; we may allow the insert to be deferred (this can allow us to bulk-load 601f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * a large batch of nodes at once, which tends to be faster and produce a better tree). 611f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * @param data The data value 621f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * @param bounds The corresponding bounding box 631f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * @param defer Can this insert be deferred? (this may be ignored) 641f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com */ 651f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com virtual void insert(void* data, const SkIRect& bounds, bool defer = false); 661f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 671f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com /** 681f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * If any inserts have been deferred, this will add them into the tree 691f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com */ 701f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com virtual void flushDeferredInserts(); 711f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 721f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com /** 731f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * Given a query rectangle, populates the passed-in array with the elements it intersects 741f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com */ 751f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com virtual void search(const SkIRect& query, SkTDArray<void*>* results); 761f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 771f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com virtual void clear(); 781f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com bool isEmpty() const { return 0 == fCount; } 791f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com int getDepth() const { return this->isEmpty() ? 0 : fRoot.fChild.subtree->fLevel + 1; } 801f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 811f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com /** 821f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * This gets the insertion count (rather than the node count) 831f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com */ 841f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com virtual int getCount() const { return fCount; } 851f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 861f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.comprivate: 871f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 881f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com struct Node; 891f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 901f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com /** 911f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * A branch of the tree, this may contain a pointer to another interior node, or a data value 921f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com */ 931f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com struct Branch { 941f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com union { 951f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com Node* subtree; 961f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com void* data; 971f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com } fChild; 981f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com SkIRect fBounds; 991f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com }; 1001f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1011f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com /** 1021f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * A node in the tree, has between fMinChildren and fMaxChildren (the root is a special case) 1031f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com */ 1041f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com struct Node { 1051f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com uint16_t fNumChildren; 1061f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com uint16_t fLevel; 1071f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com bool isLeaf() { return 0 == fLevel; } 1081f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com // Since we want to be able to pick min/max child counts at runtime, we assume the creator 1091f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com // has allocated sufficient space directly after us in memory, and index into that space 1101f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com Branch* child(size_t index) { 1111f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com return reinterpret_cast<Branch*>(this + 1) + index; 1121f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com } 1131f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com }; 1141f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1151f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com typedef int32_t SkIRect::*SortSide; 1161f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1171f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com // Helper for sorting our children arrays by sides of their rects 1181f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com static bool RectLessThan(SortSide const& side, const Branch lhs, const Branch rhs) { 1191f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com return lhs.fBounds.*side < rhs.fBounds.*side; 1201f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com } 1211f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1221f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com static bool RectLessX(int&, const Branch lhs, const Branch rhs) { 1231f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com return ((lhs.fBounds.fRight - lhs.fBounds.fLeft) >> 1) < 1241f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com ((rhs.fBounds.fRight - lhs.fBounds.fLeft) >> 1); 1251f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com } 1261f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1271f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com static bool RectLessY(int&, const Branch lhs, const Branch rhs) { 1281f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com return ((lhs.fBounds.fBottom - lhs.fBounds.fTop) >> 1) < 1291f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com ((rhs.fBounds.fBottom - lhs.fBounds.fTop) >> 1); 1301f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com } 1311f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1321f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com SkRTree(int minChildren, int maxChildren); 1331f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1341f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com /** 1351f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * Recursively descend the tree to find an insertion position for 'branch', updates 1361f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * bounding boxes on the way up. 1371f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com */ 1381f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com Branch* insert(Node* root, Branch* branch, uint16_t level = 0); 1391f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1401f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com int chooseSubtree(Node* root, Branch* branch); 1411f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com SkIRect computeBounds(Node* n); 1421f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com int distributeChildren(Branch* children); 1431f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com void search(Node* root, const SkIRect query, SkTDArray<void*>* results) const; 1441f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1451f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com /** 1461f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * This performs a bottom-up bulk load using the STR (sort-tile-recursive) algorithm, this 1471f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * seems to generally produce better, more consistent trees at significantly lower cost than 1481f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * repeated insertions. 1491f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * 1501f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * This consumes the input array. 1511f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * 1521f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * TODO: Experiment with other bulk-load algorithms (in particular the Hilbert pack variant, 1531f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * which groups rects by position on the Hilbert curve, is probably worth a look). There also 1541f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com * exist top-down bulk load variants (VAMSplit, TopDownGreedy, etc). 1551f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com */ 1561f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com Branch bulkLoad(SkTDArray<Branch>* branches, int level = 0); 1571f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1581f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com void validate(); 1591f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com int validateSubtree(Node* root, SkIRect bounds, bool isRoot = false); 1601f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1611f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com const int fMinChildren; 1621f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com const int fMaxChildren; 1631f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com const size_t fNodeSize; 1641f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1651f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com // This is the count of data elements (rather than total nodes in the tree) 1661f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com size_t fCount; 1671f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1681f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com Branch fRoot; 1691f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com SkChunkAlloc fNodes; 1701f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com SkTDArray<Branch> fDeferredInserts; 1711f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1721f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com Node* allocateNode(uint16_t level); 1731f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1741f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com}; 1751f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 1761f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com#endif 1771f45e934b68a5985b2127ec871ff593c3bfc7c2erileya@google.com 178