Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(235)

Side by Side Diff: ui/gfx/geometry/r_tree.h

Issue 245763002: Adds an RTree data structure to gfx/geometry (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ready for review, thanks Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef UI_GFX_GEOMETRY_R_TREE_H_
6 #define UI_GFX_GEOMETRY_R_TREE_H_
7
8 #include <map>
9 #include <set>
10 #include <vector>
11
12 #include "base/containers/linked_list.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/macros.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/gfx_export.h"
19
20 namespace gfx {
21
22 // Defines a heirarchical bounding rectangle data structure for Rect objects,
23 // associated with a generic unique key K for efficient spatial queries. The
24 // R*-tree algorithm is used to build the trees. Based on the papers:
25 //
26 // A Guttman 'R-trees: a dynamic index structure for spatial searching', Proc
27 // ACM SIGMOD Int Conf on Management of Data, 47-57, 1984
28 //
29 // N Beckmann, H-P Kriegel, R Schneider, B Seeger 'The R*-tree: an efficient and
30 // robust access method for points and rectangles', Proc ACM SIGMOD Int Conf on
31 // Management of Data, 322-331, 1990
32 class GFX_EXPORT RTree {
33 public:
34 RTree(size_t min_children, size_t max_children);
35 ~RTree();
36
37 // Insert a new rect into the tree, associated with provided key. Note that if
38 // rect is empty, this rect will not actually be inserted. Duplicate keys
39 // overwrite old entries.
40 void Insert(const Rect& rect, const void* key);
41
42 // If present, remove the supplied key from the tree.
43 void Remove(const void* key);
44
45 // Fills a supplied list matches_out with all keys having bounding rects
46 // intersecting query_rect.
47 void Query(const Rect& query_rect, std::set<const void*>* matches_out) const;
48
49 // Removes all objects from the tree.
50 void Clear();
51
52 private:
53 // Private data structure class for storing internal nodes or leaves with keys
54 // of R-Trees. Note that "leaf" nodes can still have children, the children
55 // will all be Nodes with non-NULL record pointers.
56 class GFX_EXPORT Node {
57 public:
58 // Level counts from -1 for "record" Nodes, that is Nodes that wrap key
59 // values, to 0 for leaf Nodes, that is Nodes that only have record
60 // children, up to the root Node, which has level equal to the height of the
61 // tree.
62 explicit Node(int level);
63
64 // Builds a new record Node.
65 Node(const Rect& rect, const void* key);
66
67 virtual ~Node();
68
69 // Deletes all children and any attached record.
70 void Clear();
71
72 // Recursive call to build a list of rects that intersect the query_rect.
73 void Query(const Rect& query_rect,
74 std::set<const void*>* matches_out) const;
75
76 // Recompute our bounds by taking the union of all children rects. Will then
77 // call RecomputeBounds() on our parent for recursive bounds recalculation
78 // up to the root.
79 void RecomputeBounds();
80
81 // Removes number_to_remove nodes from this Node, and appends them to the
82 // supplied list. Does not repair bounds upon completion.
83 void RemoveNodesForReinsert(size_t number_to_remove,
84 std::vector<Node*>* nodes);
85
86 // Given a pointer to a child node within this Node, remove it from our
87 // list. If that child had any children, append them to the supplied orphan
88 // list. Returns the new count of this node after removal. Does not
89 // recompute bounds, as this node itself may be removed if it now has too
90 // few children.
91 size_t RemoveChild(Node* child_node, std::vector<Node*>* orphans);
92
93 // Does what it says on the tin. Returns NULL if no children. Does not
94 // recompute bounds.
95 Node* RemoveAndReturnLastChild();
96
97 // Given a node, returns the best fit node for insertion of that node at
98 // the nodes level().
99 Node* ChooseSubtree(Node* node);
100
101 // Adds the provided node to this Node. Returns the new count of records
102 // stored in the Node. Will recompute the bounds of this node after
103 // addition.
104 size_t AddChild(Node* node);
105
106 // Returns a sibling to this Node with at least min_children and no greater
107 // than max_children of this Node's children assigned to it, and having the
108 // same parent. Bounds will be valid on both Nodes after this call.
109 Node* Split(size_t min_children, size_t max_children);
110
111 // For record nodes only, to support re-insert, allows setting the rect.
112 void SetRect(const Rect& rect);
113
114 // Returns a pointer to the parent of this Node, or NULL if no parent.
115 Node* parent() const { return parent_; }
116
117 // 0 level() would mean that this Node is a leaf. 1 would mean that this
118 // Node has children that are leaves. Calling level() on root_ returns the
119 // height of the tree - 1. A level of -1 means that this is a Record node.
120 int level() const { return level_; }
121
122 const Rect& rect() const { return rect_; }
123
124 size_t count() const { return children_.size(); }
125
126 const void* key() const { return key_; }
127
128 private:
129 // Returns all records stored in this node and its children.
130 void GetAllValues(std::set<const void*>* matches_out) const;
131
132 // Used for sorting Nodes along vertical and horizontal axes
133 static bool CompareVertical(Node* a, Node* b);
134
135 static bool CompareHorizontal(Node* a, Node* b);
136
137 static bool CompareCenterDistanceFromParent(Node* a, Node* b);
138
139 // Returns the increase in overlap value, as defined in Beckmann et al as
140 // the sum of the areas of the intersection of each |children_| rectangle
141 // (excepting the candidate child) with the argument rectangle. The
142 // expanded_rect argument is computed as the union of the candidate child
143 // rect and the argument rect, and is included here to avoid recomputation.
144 // Here the candidate child is indicated by index in |children_|, and
145 // expanded_rect is the alread-computed union of candidate's rect and
146 // rect.
147 int OverlapIncreaseToAdd(const Rect& rect,
148 size_t candidate,
149 const Rect& expanded_rect);
150
151 // Bounds recomputation without calling parents to do the same.
152 void RecomputeBoundsNoParents();
153
154 // Split() helper methods.
155 //
156 // Given two vectors of Nodes sorted by vertical or horizontal bounds, this
157 // function populates two vectors of Rectangles in which the ith element is
158 // the Union of all bounding rectangles [0,i] in the associated sorted array
159 // of Nodes.
160 static void BuildLowBounds(const std::vector<Node*>& vertical_sort,
161 const std::vector<Node*>& horizontal_sort,
162 std::vector<Rect>* vertical_bounds,
163 std::vector<Rect>* horizontal_bounds);
164
165 // Given two vectors of Nodes sorted by vertical or horizontal bounds, this
166 // function populates two vectors of Rectangles in which the ith element is
167 // the Union of all bounding rectangles [i, count()) in the associated
168 // sorted array of Nodes.
169 static void BuildHighBounds(const std::vector<Node*>& vertical_sort,
170 const std::vector<Node*>& horizontal_sort,
171 std::vector<Rect>* vertical_bounds,
172 std::vector<Rect>* horizontal_bounds);
173
174 // Returns true if this is a vertical split, false if a horizontal one.
175 // Based on ChooseSplitAxis algorithm in Beckmann et al. Chooses the axis
176 // with the lowest sum of margin values of bounding boxes.
177 static bool ChooseSplitAxis(const std::vector<Rect>& low_vertical_bounds,
178 const std::vector<Rect>& high_vertical_bounds,
179 const std::vector<Rect>& low_horizontal_bounds,
180 const std::vector<Rect>& high_horizontal_bounds,
181 size_t min_children,
182 size_t max_children);
183
184 // Used by SplitNode to calculate optimal index of split, after determining
185 // along which axis to sort and split the children rectangles. Returns the
186 // index to the first element in the split children as sorted by the bounds
187 // vectors.
188 static size_t ChooseSplitIndex(size_t min_children,
189 size_t max_children,
190 const std::vector<Rect>& low_bounds,
191 const std::vector<Rect>& high_bounds);
192
193 // Takes our children_ and divides them into a new node, starting at index
194 // split_index in sorted_children.
195 Node* DivideChildren(const std::vector<Rect>& low_bounds,
196 const std::vector<Rect>& high_bounds,
197 const std::vector<Node*>& sorted_children,
198 size_t split_index);
199
200 // Returns a pointer to the child node that will result in the least overlap
201 // increase, as defined in the Beckmann et al paper, or NULL if there's a
202 // tie found.
203 Node* LeastOverlapIncrease(Node* node);
204
205 // Returns a pointer to the child node that will result in the least area
206 // enlargement if the argument node rectangle were to be added to that
207 // nodes' bounding box.
208 Node* LeastAreaEnlargement(Node* node);
209
210 // This Node's bounding rectangle.
211 Rect rect_;
212
213 // The height of the node in the tree, counting from -1 at the record node
214 // to 0 at the leaf up to the root node which has level equal to the height
215 // of the tree.
216 int level_;
217
218 // Pointers to all of our children Nodes.
219 ScopedVector<Node> children_;
220
221 // A weak pointer to our parent Node in the RTree. The root node will have a
222 // NULL value for |parent_|.
223 Node* parent_;
224
225 // If this is a record Node, then |key_| will be non-NULL and will contain
226 // the key data. Otherwise, NULL.
227 const void* key_;
228
229 friend class RTreeTest;
230 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeBuildHighBounds);
231 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeBuildLowBounds);
232 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeChooseSplitAxisAndIndex);
233 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeChooseSubtree);
234 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeCompareCenterDistanceFromParent);
235 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeCompareHorizontal);
236 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeCompareVertical);
237 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeDivideChildren);
238 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeLeastAreaEnlargement);
239 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeLeastOverlapIncrease);
240 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeOverlapIncreaseToAdd);
241 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeRemoveAndReturnLastChild);
242 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeRemoveChildNoOrphans);
243 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeRemoveChildOrphans);
244 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeRemoveNodesForReinsert);
245 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeSplit);
246
247 DISALLOW_COPY_AND_ASSIGN(Node);
248 };
249
250 // Supports re-insertion of Nodes based on the strategies outlined in
251 // Beckmann et al.
252 void InsertNode(Node* node, int* highset_reinsert_level);
253
254 // Supports removal of nodes for tree without deletion.
255 void RemoveNode(Node* node);
256
257 // A pointer to the root node in the RTree.
258 scoped_ptr<Node> root_;
259
260 // The parameters used to define the shape of the RTree.
261 size_t min_children_;
262 size_t max_children_;
263
264 // A map of supplied keys to their Node representation within the RTree, for
265 // efficient retrieval of keys without requiring a bounding rect.
266 std::map<const void*, Node*> record_map_;
267
268 friend class RTreeTest;
269 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeBuildHighBounds);
270 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeBuildLowBounds);
271 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeChooseSplitAxisAndIndex);
272 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeChooseSubtree);
273 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeCompareCenterDistanceFromParent);
274 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeCompareHorizontal);
275 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeCompareVertical);
276 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeDivideChildren);
277 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeLeastAreaEnlargement);
278 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeLeastOverlapIncrease);
279 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeOverlapIncreaseToAdd);
280 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeRemoveAndReturnLastChild);
281 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeRemoveChildNoOrphans);
282 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeRemoveChildOrphans);
283 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeRemoveNodesForReinsert);
284 FRIEND_TEST_ALL_PREFIXES(RTreeTest, NodeSplit);
285
286 DISALLOW_COPY_AND_ASSIGN(RTree);
287 };
288
289 } // namespace gfx
290
291 #endif // UI_GFX_GEOMETRY_R_TREE_H_
OLDNEW
« no previous file with comments | « ui/gfx/OWNERS ('k') | ui/gfx/geometry/r_tree.cc » ('j') | ui/gfx/geometry/r_tree.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698