blob: 499f7a5d7dcb4872d3b5162043ba7f1447e78a0c [file] [log] [blame]
rileya@google.com1f45e932012-09-05 16:10:59 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkRTree_DEFINED
9#define SkRTree_DEFINED
10
mtkleina06a9532014-11-18 09:27:49 -080011#include "SkBBoxHierarchy.h"
rileya@google.com1f45e932012-09-05 16:10:59 +000012#include "SkRect.h"
13#include "SkTDArray.h"
rileya@google.com1f45e932012-09-05 16:10:59 +000014
15/**
16 * 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 +000017 * bounding rectangles.
18 *
mtkleina06a9532014-11-18 09:27:49 -080019 * It only supports bulk-loading, i.e. creation from a batch of bounding rectangles.
20 * This performs a bottom-up bulk load using the STR (sort-tile-recursive) algorithm.
rileya@google.com1f45e932012-09-05 16:10:59 +000021 *
mtkleina06a9532014-11-18 09:27:49 -080022 * TODO: Experiment with other bulk-load algorithms (in particular the Hilbert pack variant,
23 * which groups rects by position on the Hilbert curve, is probably worth a look). There also
24 * exist top-down bulk load variants (VAMSplit, TopDownGreedy, etc).
rileya@google.com1f45e932012-09-05 16:10:59 +000025 *
26 * For more details see:
27 *
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000028 * Beckmann, N.; Kriegel, H. P.; Schneider, R.; Seeger, B. (1990). "The R*-tree:
rileya@google.com1f45e932012-09-05 16:10:59 +000029 * an efficient and robust access method for points and rectangles"
rileya@google.com1f45e932012-09-05 16:10:59 +000030 */
31class SkRTree : public SkBBoxHierarchy {
32public:
mtkleinc6ad06a2015-08-19 09:51:00 -070033
rileya@google.com1f45e932012-09-05 16:10:59 +000034
35 /**
rileya@google.comb839f0f2012-09-10 17:31:05 +000036 * If you have some prior information about the distribution of bounds you're expecting, you
mtkleina06a9532014-11-18 09:27:49 -080037 * can provide an optional aspect ratio parameter. This allows the bulk-load algorithm to
38 * create better proportioned tiles of rectangles.
rileya@google.com1f45e932012-09-05 16:10:59 +000039 */
mtkleina06a9532014-11-18 09:27:49 -080040 explicit SkRTree(SkScalar aspectRatio = 1);
41 virtual ~SkRTree() {}
rileya@google.com1f45e932012-09-05 16:10:59 +000042
mtklein36352bf2015-03-25 18:17:31 -070043 void insert(const SkRect[], int N) override;
mtkleinc6ad06a2015-08-19 09:51:00 -070044 void search(const SkRect& query, SkTDArray<int>* results) const override;
mtklein36352bf2015-03-25 18:17:31 -070045 size_t bytesUsed() const 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
schenney23d85932015-03-06 16:20:28 -080054 // Get the root bound.
mtklein36352bf2015-03-25 18:17:31 -070055 SkRect getRootBound() const override;
schenney23d85932015-03-06 16:20:28 -080056
mtkleina06a9532014-11-18 09:27:49 -080057 // These values were empirically determined to produce reasonable performance in most cases.
58 static const int kMinChildren = 6,
59 kMaxChildren = 11;
tomhudson158fcaa2014-11-19 10:41:14 -080060
rileya@google.com1f45e932012-09-05 16:10:59 +000061private:
rileya@google.com1f45e932012-09-05 16:10:59 +000062 struct Node;
63
rileya@google.com1f45e932012-09-05 16:10:59 +000064 struct Branch {
65 union {
mtkleina06a9532014-11-18 09:27:49 -080066 Node* fSubtree;
mtkleinc6ad06a2015-08-19 09:51:00 -070067 int fOpIndex;
mtkleina06a9532014-11-18 09:27:49 -080068 };
69 SkRect fBounds;
rileya@google.com1f45e932012-09-05 16:10:59 +000070 };
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000071
rileya@google.com1f45e932012-09-05 16:10:59 +000072 struct Node {
73 uint16_t fNumChildren;
74 uint16_t fLevel;
mtkleina06a9532014-11-18 09:27:49 -080075 Branch fChildren[kMaxChildren];
rileya@google.com1f45e932012-09-05 16:10:59 +000076 };
77
mtkleinc6ad06a2015-08-19 09:51:00 -070078 void search(Node* root, const SkRect& query, SkTDArray<int>* results) const;
rileya@google.com1f45e932012-09-05 16:10:59 +000079
mtkleina06a9532014-11-18 09:27:49 -080080 // Consumes the input array.
rileya@google.com1f45e932012-09-05 16:10:59 +000081 Branch bulkLoad(SkTDArray<Branch>* branches, int level = 0);
82
mtkleina06a9532014-11-18 09:27:49 -080083 // How many times will bulkLoad() call allocateNodeAtLevel()?
84 static int CountNodes(int branches, SkScalar aspectRatio);
rileya@google.com1f45e932012-09-05 16:10:59 +000085
mtkleina06a9532014-11-18 09:27:49 -080086 Node* allocateNodeAtLevel(uint16_t level);
rileya@google.com1f45e932012-09-05 16:10:59 +000087
88 // This is the count of data elements (rather than total nodes in the tree)
robertphillips@google.comadacc702013-10-14 21:53:24 +000089 int fCount;
rileya@google.comb839f0f2012-09-10 17:31:05 +000090 SkScalar fAspectRatio;
mtkleina06a9532014-11-18 09:27:49 -080091 Branch fRoot;
92 SkTDArray<Node> fNodes;
rileya@google.com1f45e932012-09-05 16:10:59 +000093
rileya@google.com48134582012-09-11 15:41:50 +000094 typedef SkBBoxHierarchy INHERITED;
rileya@google.com1f45e932012-09-05 16:10:59 +000095};
96
97#endif