blob: 440411d9b0b96f9d035f5840f065f4f3aa038d07 [file] [log] [blame]
rileya@google.com1f45e932012-09-05 16:10:59 +00001
2/*
3 * Copyright 2012 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#ifndef SkRTree_DEFINED
10#define SkRTree_DEFINED
11
mtkleina06a9532014-11-18 09:27:49 -080012#include "SkBBoxHierarchy.h"
rileya@google.com1f45e932012-09-05 16:10:59 +000013#include "SkRect.h"
14#include "SkTDArray.h"
rileya@google.com1f45e932012-09-05 16:10:59 +000015
16/**
17 * An R-Tree implementation. In short, it is a balanced n-ary tree containing a hierarchy of
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000018 * bounding rectangles.
19 *
mtkleina06a9532014-11-18 09:27:49 -080020 * It only supports bulk-loading, i.e. creation from a batch of bounding rectangles.
21 * This performs a bottom-up bulk load using the STR (sort-tile-recursive) algorithm.
rileya@google.com1f45e932012-09-05 16:10:59 +000022 *
mtkleina06a9532014-11-18 09:27:49 -080023 * TODO: Experiment with other bulk-load algorithms (in particular the Hilbert pack variant,
24 * which groups rects by position on the Hilbert curve, is probably worth a look). There also
25 * exist top-down bulk load variants (VAMSplit, TopDownGreedy, etc).
rileya@google.com1f45e932012-09-05 16:10:59 +000026 *
27 * For more details see:
28 *
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000029 * Beckmann, N.; Kriegel, H. P.; Schneider, R.; Seeger, B. (1990). "The R*-tree:
rileya@google.com1f45e932012-09-05 16:10:59 +000030 * an efficient and robust access method for points and rectangles"
rileya@google.com1f45e932012-09-05 16:10:59 +000031 */
32class SkRTree : public SkBBoxHierarchy {
33public:
rileya@google.com48134582012-09-11 15:41:50 +000034 SK_DECLARE_INST_COUNT(SkRTree)
rileya@google.com1f45e932012-09-05 16:10:59 +000035
36 /**
rileya@google.comb839f0f2012-09-10 17:31:05 +000037 * If you have some prior information about the distribution of bounds you're expecting, you
mtkleina06a9532014-11-18 09:27:49 -080038 * can provide an optional aspect ratio parameter. This allows the bulk-load algorithm to
39 * create better proportioned tiles of rectangles.
rileya@google.com1f45e932012-09-05 16:10:59 +000040 */
mtkleina06a9532014-11-18 09:27:49 -080041 explicit SkRTree(SkScalar aspectRatio = 1);
42 virtual ~SkRTree() {}
rileya@google.com1f45e932012-09-05 16:10:59 +000043
mtklein4477c3c2014-10-27 10:27:10 -070044 virtual void insert(SkAutoTMalloc<SkRect>* boundsArray, int N) SK_OVERRIDE;
mtklein6bd41962014-10-02 07:41:56 -070045 virtual void search(const SkRect& query, SkTDArray<unsigned>* results) const SK_OVERRIDE;
rileya@google.com1f45e932012-09-05 16:10:59 +000046
mtkleina06a9532014-11-18 09:27:49 -080047 // Methods and constants below here are only public for tests.
48
mtklein8f8c25e2014-10-02 09:53:04 -070049 // Return the depth of the tree structure.
mtkleina06a9532014-11-18 09:27:49 -080050 int getDepth() const { return fCount ? fRoot.fSubtree->fLevel + 1 : 0; }
mtklein8f8c25e2014-10-02 09:53:04 -070051 // Insertion count (not overall node count, which may be greater).
52 int getCount() const { return fCount; }
rileya@google.com1f45e932012-09-05 16:10:59 +000053
mtkleina06a9532014-11-18 09:27:49 -080054 // These values were empirically determined to produce reasonable performance in most cases.
55 static const int kMinChildren = 6,
56 kMaxChildren = 11;
rileya@google.com1f45e932012-09-05 16:10:59 +000057private:
rileya@google.com1f45e932012-09-05 16:10:59 +000058 struct Node;
59
rileya@google.com1f45e932012-09-05 16:10:59 +000060 struct Branch {
61 union {
mtkleina06a9532014-11-18 09:27:49 -080062 Node* fSubtree;
63 unsigned fOpIndex;
64 };
65 SkRect fBounds;
rileya@google.com1f45e932012-09-05 16:10:59 +000066 };
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000067
rileya@google.com1f45e932012-09-05 16:10:59 +000068 struct Node {
69 uint16_t fNumChildren;
70 uint16_t fLevel;
mtkleina06a9532014-11-18 09:27:49 -080071 Branch fChildren[kMaxChildren];
rileya@google.com1f45e932012-09-05 16:10:59 +000072 };
73
mtkleina06a9532014-11-18 09:27:49 -080074 void search(Node* root, const SkRect& query, SkTDArray<unsigned>* results) const;
rileya@google.com1f45e932012-09-05 16:10:59 +000075
mtkleina06a9532014-11-18 09:27:49 -080076 // Consumes the input array.
rileya@google.com1f45e932012-09-05 16:10:59 +000077 Branch bulkLoad(SkTDArray<Branch>* branches, int level = 0);
78
mtkleina06a9532014-11-18 09:27:49 -080079 // How many times will bulkLoad() call allocateNodeAtLevel()?
80 static int CountNodes(int branches, SkScalar aspectRatio);
rileya@google.com1f45e932012-09-05 16:10:59 +000081
mtkleina06a9532014-11-18 09:27:49 -080082 Node* allocateNodeAtLevel(uint16_t level);
rileya@google.com1f45e932012-09-05 16:10:59 +000083
84 // This is the count of data elements (rather than total nodes in the tree)
robertphillips@google.comadacc702013-10-14 21:53:24 +000085 int fCount;
rileya@google.comb839f0f2012-09-10 17:31:05 +000086 SkScalar fAspectRatio;
mtkleina06a9532014-11-18 09:27:49 -080087 Branch fRoot;
88 SkTDArray<Node> fNodes;
rileya@google.com1f45e932012-09-05 16:10:59 +000089
rileya@google.com48134582012-09-11 15:41:50 +000090 typedef SkBBoxHierarchy INHERITED;
rileya@google.com1f45e932012-09-05 16:10:59 +000091};
92
93#endif