OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CC_BASE_RTREE_H_ | 5 #ifndef CC_BASE_RTREE_H_ |
6 #define CC_BASE_RTREE_H_ | 6 #define CC_BASE_RTREE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... |
33 // For more details see: | 33 // For more details see: |
34 // | 34 // |
35 // Beckmann, N.; Kriegel, H. P.; Schneider, R.; Seeger, B. (1990). | 35 // Beckmann, N.; Kriegel, H. P.; Schneider, R.; Seeger, B. (1990). |
36 // "The R*-tree: an efficient and robust access method for points and | 36 // "The R*-tree: an efficient and robust access method for points and |
37 // rectangles" | 37 // rectangles" |
38 class CC_BASE_EXPORT RTree { | 38 class CC_BASE_EXPORT RTree { |
39 public: | 39 public: |
40 RTree(); | 40 RTree(); |
41 ~RTree(); | 41 ~RTree(); |
42 | 42 |
| 43 // Constructs the rtree from a given container of gfx::Rects. Queries using |
| 44 // Search will then return indices into this container. |
| 45 template <typename Container> |
| 46 void Build(const Container& items) { |
| 47 Build(items, [](const gfx::Rect& bounds) { return bounds; }); |
| 48 } |
| 49 |
| 50 // Build helper that takes a container and a function used to get gfx::Rect |
| 51 // from each item. That is, "bounds_getter(items[i]);" should return a |
| 52 // gfx::Rect representing the bounds of items[i] for each i. |
43 template <typename Container, typename Functor> | 53 template <typename Container, typename Functor> |
44 void Build(const Container& items, const Functor& bounds_getter) { | 54 void Build(const Container& items, const Functor& bounds_getter) { |
45 DCHECK_EQ(0u, num_data_elements_); | 55 DCHECK_EQ(0u, num_data_elements_); |
46 | 56 |
47 std::vector<Branch> branches; | 57 std::vector<Branch> branches; |
48 branches.reserve(items.size()); | 58 branches.reserve(items.size()); |
49 | 59 |
50 for (size_t i = 0; i < items.size(); i++) { | 60 for (size_t i = 0; i < items.size(); i++) { |
51 const gfx::Rect& bounds = bounds_getter(items[i]); | 61 const gfx::Rect& bounds = bounds_getter(items[i]); |
52 if (bounds.IsEmpty()) | 62 if (bounds.IsEmpty()) |
(...skipping 26 matching lines...) Expand all Loading... |
79 kMinChildren; | 89 kMinChildren; |
80 nodes_.reserve(node_count); | 90 nodes_.reserve(node_count); |
81 | 91 |
82 root_ = BuildRecursive(&branches, 0); | 92 root_ = BuildRecursive(&branches, 0); |
83 } | 93 } |
84 // We should've wasted at most kMinChildren nodes. | 94 // We should've wasted at most kMinChildren nodes. |
85 DCHECK_LE(nodes_.capacity() - nodes_.size(), | 95 DCHECK_LE(nodes_.capacity() - nodes_.size(), |
86 static_cast<size_t>(kMinChildren)); | 96 static_cast<size_t>(kMinChildren)); |
87 } | 97 } |
88 | 98 |
89 template <typename Container> | 99 // Given a query rect, returns sorted indices of elements that were used to |
90 void Build(const Container& items) { | 100 // construct this rtree. |
91 Build(items, [](const gfx::Rect& bounds) { return bounds; }); | 101 std::vector<size_t> Search(const gfx::Rect& query) const; |
92 } | |
93 | 102 |
94 void Search(const gfx::Rect& query, std::vector<size_t>* results) const; | 103 // Returns the total bounds of all items in this rtree. |
95 | |
96 gfx::Rect GetBounds() const; | 104 gfx::Rect GetBounds() const; |
97 | 105 |
98 private: | 106 private: |
99 // These values were empirically determined to produce reasonable performance | 107 // These values were empirically determined to produce reasonable performance |
100 // in most cases. | 108 // in most cases. |
101 enum { kMinChildren = 6 }; | 109 enum { kMinChildren = 6 }; |
102 enum { kMaxChildren = 11 }; | 110 enum { kMaxChildren = 11 }; |
103 | 111 |
104 struct Node; | 112 struct Node; |
105 struct Branch { | 113 struct Branch { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 size_t num_data_elements_; | 146 size_t num_data_elements_; |
139 Branch root_; | 147 Branch root_; |
140 std::vector<Node> nodes_; | 148 std::vector<Node> nodes_; |
141 | 149 |
142 DISALLOW_COPY_AND_ASSIGN(RTree); | 150 DISALLOW_COPY_AND_ASSIGN(RTree); |
143 }; | 151 }; |
144 | 152 |
145 } // namespace cc | 153 } // namespace cc |
146 | 154 |
147 #endif // CC_BASE_RTREE_H_ | 155 #endif // CC_BASE_RTREE_H_ |
OLD | NEW |