michael@0: michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkRTree_DEFINED michael@0: #define SkRTree_DEFINED michael@0: michael@0: #include "SkRect.h" michael@0: #include "SkTDArray.h" michael@0: #include "SkChunkAlloc.h" michael@0: #include "SkBBoxHierarchy.h" michael@0: michael@0: /** michael@0: * An R-Tree implementation. In short, it is a balanced n-ary tree containing a hierarchy of michael@0: * bounding rectangles. michael@0: * michael@0: * Much like a B-Tree it maintains balance by enforcing minimum and maximum child counts, and michael@0: * splitting nodes when they become overfull. Unlike B-trees, however, we're using spatial data; so michael@0: * there isn't a canonical ordering to use when choosing insertion locations and splitting michael@0: * distributions. A variety of heuristics have been proposed for these problems; here, we're using michael@0: * something resembling an R*-tree, which attempts to minimize area and overlap during insertion, michael@0: * and aims to minimize a combination of margin, overlap, and area when splitting. michael@0: * michael@0: * One detail that is thus far unimplemented that may improve tree quality is attempting to remove michael@0: * and reinsert nodes when they become full, instead of immediately splitting (nodes that may have michael@0: * been placed well early on may hurt the tree later when more nodes have been added; removing michael@0: * and reinserting nodes generally helps reduce overlap and make a better tree). Deletion of nodes michael@0: * is also unimplemented. michael@0: * michael@0: * For more details see: michael@0: * michael@0: * Beckmann, N.; Kriegel, H. P.; Schneider, R.; Seeger, B. (1990). "The R*-tree: michael@0: * an efficient and robust access method for points and rectangles" michael@0: * michael@0: * It also supports bulk-loading from a batch of bounds and values; if you don't require the tree michael@0: * to be usable in its intermediate states while it is being constructed, this is significantly michael@0: * quicker than individual insertions and produces more consistent trees. michael@0: */ michael@0: class SkRTree : public SkBBoxHierarchy { michael@0: public: michael@0: SK_DECLARE_INST_COUNT(SkRTree) michael@0: michael@0: /** michael@0: * Create a new R-Tree with specified min/max child counts. michael@0: * The child counts are valid iff: michael@0: * - (max + 1) / 2 >= min (splitting an overfull node must be enough to populate 2 nodes) michael@0: * - min < max michael@0: * - min > 0 michael@0: * - max < SK_MaxU16 michael@0: * If you have some prior information about the distribution of bounds you're expecting, you michael@0: * can provide an optional aspect ratio parameter. This allows the bulk-load algorithm to create michael@0: * better proportioned tiles of rectangles. michael@0: */ michael@0: static SkRTree* Create(int minChildren, int maxChildren, SkScalar aspectRatio = 1, michael@0: bool orderWhenBulkLoading = true); michael@0: virtual ~SkRTree(); michael@0: michael@0: /** michael@0: * Insert a node, consisting of bounds and a data value into the tree, if we don't immediately michael@0: * need to use the tree; we may allow the insert to be deferred (this can allow us to bulk-load michael@0: * a large batch of nodes at once, which tends to be faster and produce a better tree). michael@0: * @param data The data value michael@0: * @param bounds The corresponding bounding box michael@0: * @param defer Can this insert be deferred? (this may be ignored) michael@0: */ michael@0: virtual void insert(void* data, const SkIRect& bounds, bool defer = false) SK_OVERRIDE; michael@0: michael@0: /** michael@0: * If any inserts have been deferred, this will add them into the tree michael@0: */ michael@0: virtual void flushDeferredInserts() SK_OVERRIDE; michael@0: michael@0: /** michael@0: * Given a query rectangle, populates the passed-in array with the elements it intersects michael@0: */ michael@0: virtual void search(const SkIRect& query, SkTDArray* results) SK_OVERRIDE; michael@0: michael@0: virtual void clear() SK_OVERRIDE; michael@0: bool isEmpty() const { return 0 == fCount; } michael@0: michael@0: /** michael@0: * Gets the depth of the tree structure michael@0: */ michael@0: virtual int getDepth() const SK_OVERRIDE { michael@0: return this->isEmpty() ? 0 : fRoot.fChild.subtree->fLevel + 1; michael@0: } michael@0: michael@0: /** michael@0: * This gets the insertion count (rather than the node count) michael@0: */ michael@0: virtual int getCount() const SK_OVERRIDE { return fCount; } michael@0: michael@0: virtual void rewindInserts() SK_OVERRIDE; michael@0: michael@0: private: michael@0: michael@0: struct Node; michael@0: michael@0: /** michael@0: * A branch of the tree, this may contain a pointer to another interior node, or a data value michael@0: */ michael@0: struct Branch { michael@0: union { michael@0: Node* subtree; michael@0: void* data; michael@0: } fChild; michael@0: SkIRect fBounds; michael@0: }; michael@0: michael@0: /** michael@0: * A node in the tree, has between fMinChildren and fMaxChildren (the root is a special case) michael@0: */ michael@0: struct Node { michael@0: uint16_t fNumChildren; michael@0: uint16_t fLevel; michael@0: bool isLeaf() { return 0 == fLevel; } michael@0: // Since we want to be able to pick min/max child counts at runtime, we assume the creator michael@0: // has allocated sufficient space directly after us in memory, and index into that space michael@0: Branch* child(size_t index) { michael@0: return reinterpret_cast(this + 1) + index; michael@0: } michael@0: }; michael@0: michael@0: typedef int32_t SkIRect::*SortSide; michael@0: michael@0: // Helper for sorting our children arrays by sides of their rects michael@0: struct RectLessThan { michael@0: RectLessThan(SkRTree::SortSide side) : fSide(side) { } michael@0: bool operator()(const SkRTree::Branch lhs, const SkRTree::Branch rhs) const { michael@0: return lhs.fBounds.*fSide < rhs.fBounds.*fSide; michael@0: } michael@0: private: michael@0: const SkRTree::SortSide fSide; michael@0: }; michael@0: michael@0: struct RectLessX { michael@0: bool operator()(const SkRTree::Branch lhs, const SkRTree::Branch rhs) { michael@0: return ((lhs.fBounds.fRight - lhs.fBounds.fLeft) >> 1) < michael@0: ((rhs.fBounds.fRight - lhs.fBounds.fLeft) >> 1); michael@0: } michael@0: }; michael@0: michael@0: struct RectLessY { michael@0: bool operator()(const SkRTree::Branch lhs, const SkRTree::Branch rhs) { michael@0: return ((lhs.fBounds.fBottom - lhs.fBounds.fTop) >> 1) < michael@0: ((rhs.fBounds.fBottom - lhs.fBounds.fTop) >> 1); michael@0: } michael@0: }; michael@0: michael@0: SkRTree(int minChildren, int maxChildren, SkScalar aspectRatio, bool orderWhenBulkLoading); michael@0: michael@0: /** michael@0: * Recursively descend the tree to find an insertion position for 'branch', updates michael@0: * bounding boxes on the way up. michael@0: */ michael@0: Branch* insert(Node* root, Branch* branch, uint16_t level = 0); michael@0: michael@0: int chooseSubtree(Node* root, Branch* branch); michael@0: SkIRect computeBounds(Node* n); michael@0: int distributeChildren(Branch* children); michael@0: void search(Node* root, const SkIRect query, SkTDArray* results) const; michael@0: michael@0: /** michael@0: * This performs a bottom-up bulk load using the STR (sort-tile-recursive) algorithm, this michael@0: * seems to generally produce better, more consistent trees at significantly lower cost than michael@0: * repeated insertions. michael@0: * michael@0: * This consumes the input array. michael@0: * michael@0: * TODO: Experiment with other bulk-load algorithms (in particular the Hilbert pack variant, michael@0: * which groups rects by position on the Hilbert curve, is probably worth a look). There also michael@0: * exist top-down bulk load variants (VAMSplit, TopDownGreedy, etc). michael@0: */ michael@0: Branch bulkLoad(SkTDArray* branches, int level = 0); michael@0: michael@0: void validate(); michael@0: int validateSubtree(Node* root, SkIRect bounds, bool isRoot = false); michael@0: michael@0: const int fMinChildren; michael@0: const int fMaxChildren; michael@0: const size_t fNodeSize; michael@0: michael@0: // This is the count of data elements (rather than total nodes in the tree) michael@0: int fCount; michael@0: michael@0: Branch fRoot; michael@0: SkChunkAlloc fNodes; michael@0: SkTDArray fDeferredInserts; michael@0: SkScalar fAspectRatio; michael@0: bool fSortWhenBulkLoading; michael@0: michael@0: Node* allocateNode(uint16_t level); michael@0: michael@0: typedef SkBBoxHierarchy INHERITED; michael@0: }; michael@0: michael@0: #endif