blob: 4021c0980bd02d2982ada114a630bab3bfaaab3f [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;
tomhudson158fcaa2014-11-19 10:41:14 -080046 virtual size_t bytesUsed() const SK_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
mtkleina06a9532014-11-18 09:27:49 -080055 // These values were empirically determined to produce reasonable performance in most cases.
56 static const int kMinChildren = 6,
57 kMaxChildren = 11;
tomhudson158fcaa2014-11-19 10:41:14 -080058
rileya@google.com1f45e932012-09-05 16:10:59 +000059private:
rileya@google.com1f45e932012-09-05 16:10:59 +000060 struct Node;
61
rileya@google.com1f45e932012-09-05 16:10:59 +000062 struct Branch {
63 union {
mtkleina06a9532014-11-18 09:27:49 -080064 Node* fSubtree;
65 unsigned fOpIndex;
66 };
67 SkRect fBounds;
rileya@google.com1f45e932012-09-05 16:10:59 +000068 };
skia.committer@gmail.com6c778162012-09-06 02:01:13 +000069
rileya@google.com1f45e932012-09-05 16:10:59 +000070 struct Node {
71 uint16_t fNumChildren;
72 uint16_t fLevel;
mtkleina06a9532014-11-18 09:27:49 -080073 Branch fChildren[kMaxChildren];
rileya@google.com1f45e932012-09-05 16:10:59 +000074 };
75
mtkleina06a9532014-11-18 09:27:49 -080076 void search(Node* root, const SkRect& query, SkTDArray<unsigned>* results) const;
rileya@google.com1f45e932012-09-05 16:10:59 +000077
mtkleina06a9532014-11-18 09:27:49 -080078 // Consumes the input array.
rileya@google.com1f45e932012-09-05 16:10:59 +000079 Branch bulkLoad(SkTDArray<Branch>* branches, int level = 0);
80
mtkleina06a9532014-11-18 09:27:49 -080081 // How many times will bulkLoad() call allocateNodeAtLevel()?
82 static int CountNodes(int branches, SkScalar aspectRatio);
rileya@google.com1f45e932012-09-05 16:10:59 +000083
mtkleina06a9532014-11-18 09:27:49 -080084 Node* allocateNodeAtLevel(uint16_t level);
rileya@google.com1f45e932012-09-05 16:10:59 +000085
86 // This is the count of data elements (rather than total nodes in the tree)
robertphillips@google.comadacc702013-10-14 21:53:24 +000087 int fCount;
rileya@google.comb839f0f2012-09-10 17:31:05 +000088 SkScalar fAspectRatio;
mtkleina06a9532014-11-18 09:27:49 -080089 Branch fRoot;
90 SkTDArray<Node> fNodes;
rileya@google.com1f45e932012-09-05 16:10:59 +000091
rileya@google.com48134582012-09-11 15:41:50 +000092 typedef SkBBoxHierarchy INHERITED;
rileya@google.com1f45e932012-09-05 16:10:59 +000093};
94
95#endif