blob: 557817189d182e9063dff7a242561139df74588b [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:
mtkleinc6ad06a2015-08-19 09:51:00 -070034
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
mtklein36352bf2015-03-25 18:17:31 -070044 void insert(const SkRect[], int N) override;
mtkleinc6ad06a2015-08-19 09:51:00 -070045 void search(const SkRect& query, SkTDArray<int>* results) const override;
mtklein36352bf2015-03-25 18:17:31 -070046 size_t bytesUsed() const override;
rileya@google.com1f45e932012-09-05 16:10:59 +000047
mtkleina06a9532014-11-18 09:27:49 -080048 // Methods and constants below here are only public for tests.
49
mtklein8f8c25e2014-10-02 09:53:04 -070050 // Return the depth of the tree structure.
mtkleina06a9532014-11-18 09:27:49 -080051 int getDepth() const { return fCount ? fRoot.fSubtree->fLevel + 1 : 0; }
mtklein8f8c25e2014-10-02 09:53:04 -070052 // Insertion count (not overall node count, which may be greater).
53 int getCount() const { return fCount; }
rileya@google.com1f45e932012-09-05 16:10:59 +000054
schenney23d85932015-03-06 16:20:28 -080055 // Get the root bound.
mtklein36352bf2015-03-25 18:17:31 -070056 SkRect getRootBound() const override;
schenney23d85932015-03-06 16:20:28 -080057
mtkleina06a9532014-11-18 09:27:49 -080058 // These values were empirically determined to produce reasonable performance in most cases.
59 static const int kMinChildren = 6,
60 kMaxChildren = 11;
tomhudson158fcaa2014-11-19 10:41:14 -080061
rileya@google.com1f45e932012-09-05 16:10:59 +000062private:
rileya@google.com1f45e932012-09-05 16:10:59 +000063 struct Node;
64
rileya@google.com1f45e932012-09-05 16:10:59 +000065 struct Branch {
66 union {
mtkleina06a9532014-11-18 09:27:49 -080067 Node* fSubtree;
mtkleinc6ad06a2015-08-19 09:51:00 -070068 int fOpIndex;
mtkleina06a9532014-11-18 09:27:49 -080069 };
70 SkRect fBounds;
rileya@google.com1f45e932012-09-05 16:10:59 +000071 };
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000072
rileya@google.com1f45e932012-09-05 16:10:59 +000073 struct Node {
74 uint16_t fNumChildren;
75 uint16_t fLevel;
mtkleina06a9532014-11-18 09:27:49 -080076 Branch fChildren[kMaxChildren];
rileya@google.com1f45e932012-09-05 16:10:59 +000077 };
78
mtkleinc6ad06a2015-08-19 09:51:00 -070079 void search(Node* root, const SkRect& query, SkTDArray<int>* results) const;
rileya@google.com1f45e932012-09-05 16:10:59 +000080
mtkleina06a9532014-11-18 09:27:49 -080081 // Consumes the input array.
rileya@google.com1f45e932012-09-05 16:10:59 +000082 Branch bulkLoad(SkTDArray<Branch>* branches, int level = 0);
83
mtkleina06a9532014-11-18 09:27:49 -080084 // How many times will bulkLoad() call allocateNodeAtLevel()?
85 static int CountNodes(int branches, SkScalar aspectRatio);
rileya@google.com1f45e932012-09-05 16:10:59 +000086
mtkleina06a9532014-11-18 09:27:49 -080087 Node* allocateNodeAtLevel(uint16_t level);
rileya@google.com1f45e932012-09-05 16:10:59 +000088
89 // This is the count of data elements (rather than total nodes in the tree)
robertphillips@google.comadacc702013-10-14 21:53:24 +000090 int fCount;
rileya@google.comb839f0f2012-09-10 17:31:05 +000091 SkScalar fAspectRatio;
mtkleina06a9532014-11-18 09:27:49 -080092 Branch fRoot;
93 SkTDArray<Node> fNodes;
rileya@google.com1f45e932012-09-05 16:10:59 +000094
rileya@google.com48134582012-09-11 15:41:50 +000095 typedef SkBBoxHierarchy INHERITED;
rileya@google.com1f45e932012-09-05 16:10:59 +000096};
97
98#endif