OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 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_BASE_H_ | |
6 #define UI_GFX_GEOMETRY_R_TREE_BASE_H_ | |
7 | |
8 #include <list> | |
9 #include <vector> | |
10 | |
11 #include "base/containers/hash_tables.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/memory/scoped_vector.h" | |
15 #include "ui/gfx/geometry/rect.h" | |
16 #include "ui/gfx/gfx_export.h" | |
17 | |
18 namespace gfx { | |
19 | |
20 class GFX_EXPORT RTreeBase { | |
21 public: | |
22 RTreeBase(size_t min_children, size_t max_children); | |
23 virtual ~RTreeBase(); | |
Peter Kasting
2014/05/16 01:41:05
Can both of these be protected also? In general,
luken
2014/05/21 20:19:36
RTreeBase ctor and dtor can be made protected and
Peter Kasting
2014/05/21 20:26:44
Yeah, but Node is a subclass of NodeBase, so it sh
| |
24 | |
25 protected: | |
26 class RecordBase; | |
27 typedef std::list<const RecordBase*> Records; | |
28 | |
29 // Private data structure class for storing internal nodes or leaves with keys | |
Peter Kasting
2014/05/16 01:41:05
You use "key" three times in this file, all in com
luken
2014/05/21 20:19:36
Done.
| |
30 // of R-Trees. Note that "leaf" Nodes can still have children, the children | |
31 // will all be Records. | |
32 class GFX_EXPORT NodeBase { | |
33 public: | |
34 virtual ~NodeBase(); | |
35 virtual void Query(const Rect& query_rect, Records* records_out) const = 0; | |
Peter Kasting
2014/05/16 01:41:05
This function should have a comment indicating wha
luken
2014/05/21 20:19:36
Done.
| |
36 | |
37 // Recompute our bounds by taking the union of all children rects. Will then | |
Peter Kasting
2014/05/16 01:41:05
Nit: How about:
Recomputes our bounds by taking t
luken
2014/05/21 20:19:36
Done.
| |
38 // call RecomputeBoundsUpToRoot() on our parent for recursive bounds | |
39 // recalculation up to the root. | |
40 void RecomputeBoundsUpToRoot(); | |
41 | |
42 // Returns NULL if no children. Does not recompute bounds. | |
43 virtual scoped_ptr<NodeBase> RemoveAndReturnLastChild() = 0; | |
44 virtual void GetAllValues(Records* records_out) const = 0; | |
Peter Kasting
2014/05/16 01:41:05
Nit: Put a blank line above this if the comment ab
luken
2014/05/21 20:19:36
Done.
| |
45 | |
46 void SetParent(NodeBase* parent) { parent_ = parent; } | |
Peter Kasting
2014/05/16 01:41:05
Based on http://google-styleguide.googlecode.com/s
luken
2014/05/21 20:19:36
Done.
| |
47 | |
48 const Rect& rect() const { return rect_; } | |
49 NodeBase* parent() { return parent_; } | |
50 virtual const int level() const = 0; | |
Peter Kasting
2014/05/16 01:41:05
Virtual functions should never be named unix_hacke
luken
2014/05/21 20:19:36
Done.
| |
51 | |
52 protected: | |
53 friend class RTreeTest; | |
54 friend class RTreeNodeTest; | |
55 | |
56 NodeBase(const Rect& rect, NodeBase* parent); | |
57 | |
58 // Bounds recomputation without calling parents to do the same. | |
59 virtual void RecomputeLocalBounds() = 0; | |
60 | |
61 // This Node's bounding rectangle. | |
62 Rect rect_; | |
Peter Kasting
2014/05/16 01:41:05
From http://google-styleguide.googlecode.com/svn/t
luken
2014/05/21 20:19:36
Done.
| |
63 | |
64 // A weak pointer to our parent Node in the RTree. The root node will have a | |
65 // NULL value for |parent_|. | |
66 NodeBase* parent_; | |
67 }; | |
Peter Kasting
2014/05/16 01:41:05
DISALLOW_COPY_AND_ASSIGN (every class in this file
luken
2014/05/21 20:19:36
Done.
| |
68 | |
69 class GFX_EXPORT RecordBase : public NodeBase { | |
70 public: | |
71 RecordBase(const Rect& rect); | |
72 virtual ~RecordBase(); | |
73 // For record nodes only, to support re-insert, allows setting the rect. | |
74 void SetRect(const Rect& rect); | |
75 virtual void Query(const Rect& query_rect, | |
76 Records* records_out) const OVERRIDE; | |
77 virtual scoped_ptr<NodeBase> RemoveAndReturnLastChild() OVERRIDE; | |
78 virtual const int level() const OVERRIDE; | |
79 | |
80 protected: | |
81 friend class RTreeTest; | |
82 friend class RTreeNodeTest; | |
83 | |
84 virtual void RecomputeLocalBounds() OVERRIDE; | |
85 virtual void GetAllValues(Records* records_out) const OVERRIDE; | |
86 }; | |
87 | |
88 class GFX_EXPORT Node : public NodeBase { | |
Peter Kasting
2014/05/16 01:41:05
Can this class be forward-declared here, and then
luken
2014/05/21 20:19:36
Unfortunately, no. r_tree.h relies on knowing what
Peter Kasting
2014/05/21 20:26:44
I still wonder if there isn't some possibility of
| |
89 public: | |
90 // Constructs an empty Node with |level_| of 0. | |
91 Node(); | |
92 virtual ~Node(); | |
93 | |
94 // Constructs a new Node that is the parent of this Node and already has | |
95 // this Node as its sole child. Valid to call only on root Nodes, meaning | |
96 // Nodes with |parent_| NULL. Note that ownership of this Node is transfered | |
97 // to the returned parent by this function. | |
Peter Kasting
2014/05/16 01:41:05
Nit: returned parent -> parent returned
luken
2014/05/21 20:19:36
Done.
| |
98 scoped_ptr<Node> MakeParent(); | |
Peter Kasting
2014/05/16 01:41:05
Perhaps this function and the next should be calle
luken
2014/05/21 20:19:36
Done.
| |
99 | |
100 // Constucts a new Node that is the sibling of this Node which already | |
101 // has the same |parent_| and |level_| as this Node. Note that the new node | |
102 // will not have been added to the |parent_| as its bounds are still empty. | |
Peter Kasting
2014/05/16 01:41:05
Either it has the same parent or it doesn't have t
luken
2014/05/21 20:19:36
Done.
| |
103 scoped_ptr<Node> MakeSibling(); | |
104 | |
105 // Recursive call to build a set of keys from record Nodes with rects that | |
106 // intersect the |query_rect| in this subtree. Appends to |matches_out| | |
Peter Kasting
2014/05/16 01:41:05
Nit: Maybe:
Appends to |records_out| the set of R
luken
2014/05/21 20:19:36
Done.
| |
107 // without clearing. | |
108 virtual void Query(const Rect& query_rect, | |
109 Records* records_out) const OVERRIDE; | |
110 | |
111 typedef ScopedVector<NodeBase> Nodes; | |
Peter Kasting
2014/05/16 01:41:05
Based on http://google-styleguide.googlecode.com/s
luken
2014/05/21 20:19:36
Done.
| |
112 | |
113 // Removes |number_to_remove| children from this Node, and appends them to | |
114 // the supplied list. Does not repair bounds upon completion. Nodes are | |
115 // selected in the manner suggested in the Beckmann et al. paper, which | |
116 // suggests that the children should be sorted by the distance from the | |
117 // center of their bounding rectangle to their parent's bounding retangle, | |
118 // and then the n closest children should be removed for re-insertion. This | |
119 // removal occurs at most once on each level of the tree when overflowing | |
120 // nodes that have exceeded the maximum number of children during an Insert. | |
121 void RemoveNodesForReinsert(size_t number_to_remove, Nodes* nodes); | |
122 | |
123 // Given a pointer to a child node within this Node, removes it from our | |
124 // list. If that child had any children, appends them to the supplied orphan | |
125 // list. Returns the new count of this node after removal. Does not | |
126 // recompute bounds, as the caller might subsequently remove this node as | |
127 // well, meaning the recomputation would be wasted work. | |
128 size_t RemoveChild(NodeBase* child_node, Nodes* orphans); | |
129 | |
130 // Returns NULL if no children. Does not recompute bounds. | |
131 virtual scoped_ptr<NodeBase> RemoveAndReturnLastChild() OVERRIDE; | |
132 | |
133 // Returns the best parent for insertion of the provided |node| as a child. | |
134 Node* ChooseSubtree(NodeBase* node); | |
135 | |
136 // Adds |node| as a child of this Node, and recomputes the bounds of this | |
137 // node after the addition of the child. Returns the new count of children | |
138 // stored in this Node. This node becomes the owner of |node|. | |
139 size_t AddChild(scoped_ptr<NodeBase> node); | |
140 | |
141 // Returns a sibling to this Node with at least min_children and no greater | |
142 // than max_children of this Node's children assigned to it, and having the | |
143 // same parent. Bounds will be valid on both Nodes after this call. | |
144 Node* Split(size_t min_children, size_t max_children); | |
Peter Kasting
2014/05/16 01:41:05
This seems to return a new Node the caller is expe
luken
2014/05/21 20:19:36
Done.
| |
145 | |
146 virtual const int level() const OVERRIDE; | |
147 const size_t count() const { return children_.size(); } | |
148 | |
149 private: | |
150 typedef std::vector<Rect> Rects; | |
151 Node(int level); | |
Peter Kasting
2014/05/16 01:41:05
Nit: I suggest a blank line above this
luken
2014/05/21 20:19:36
Done.
| |
152 | |
153 // Used by Split to calculate optimal index of split, after determining | |
154 // along which axis to sort and split the children rectangles. Returns the | |
155 // index to the first element in the split children as sorted by the bounds | |
156 // vectors. | |
Peter Kasting
2014/05/16 01:41:05
Sadly, I just don't really understand what this fu
luken
2014/05/21 20:19:36
Reworked the comment.
| |
157 static size_t ChooseSplitIndex(size_t min_children, | |
158 size_t max_children, | |
159 const Rects& low_bounds, | |
160 const Rects& high_bounds); | |
161 | |
162 // Returns the smallest sum of the margins of the pairs of rectangles at the | |
163 // same index within |low_bounds| and |high_bounds|. | |
Peter Kasting
2014/05/16 01:41:05
My brain exploded trying to parse this sentence.
luken
2014/05/21 20:19:36
Done.
| |
164 static int SmallestMarginSum(size_t start_index, | |
165 size_t end_index, | |
166 const Rects& low_bounds, | |
167 const Rects& high_bounds); | |
168 | |
169 // Sort nodes primarily by increasing y coordinates, and secondarily by | |
Peter Kasting
2014/05/16 01:41:05
Nit: Sort -> Sorts, or "Used to sort" (several fun
luken
2014/05/21 20:19:36
Done.
| |
170 // increasing height. | |
171 static bool CompareVertical(NodeBase* a, NodeBase* b); | |
172 | |
173 // Sort nodes primarily by increasing x coordinates, and secondarily by | |
174 // increasing width. | |
175 static bool CompareHorizontal(NodeBase* a, NodeBase* b); | |
176 | |
177 // Sort nodes by the distance of the center of their rectangles to the | |
178 // center of their parent's rectangles. | |
179 static bool CompareCenterDistanceFromParent(NodeBase* a, NodeBase* b); | |
180 | |
181 // Given two vectors of Nodes sorted by vertical or horizontal bounds, this | |
182 // function populates two vectors of Rectangles in which the ith element is | |
Peter Kasting
2014/05/16 01:41:05
Nit: Remove "this function" (also in next comment)
luken
2014/05/21 20:19:36
Done.
| |
183 // the union of all bounding rectangles [0,i] in the associated sorted array | |
184 // of Nodes. | |
185 static void BuildLowBounds(const std::vector<NodeBase*>& vertical_sort, | |
186 const std::vector<NodeBase*>& horizontal_sort, | |
187 Rects* vertical_bounds, | |
188 Rects* horizontal_bounds); | |
189 | |
190 // Given two vectors of Nodes sorted by vertical or horizontal bounds, this | |
191 // function populates two vectors of Rectangles in which the ith element is | |
192 // the union of all bounding rectangles [i, count()) in the associated | |
193 // sorted array of Nodes. | |
194 static void BuildHighBounds(const std::vector<NodeBase*>& vertical_sort, | |
195 const std::vector<NodeBase*>& horizontal_sort, | |
196 Rects* vertical_bounds, | |
197 Rects* horizontal_bounds); | |
198 | |
199 virtual void RecomputeLocalBounds() OVERRIDE; | |
200 | |
201 // Returns all records stored in the subtree rooted at this node. Appends to | |
202 // |matches_out| without clearing. | |
Peter Kasting
2014/05/16 01:41:05
This sort of comment should be on the base class f
luken
2014/05/21 20:19:36
Done.
| |
203 virtual void GetAllValues(Records* matches_out) const OVERRIDE; | |
204 | |
205 // Returns the increase in overlap value, as defined in Beckmann et al. as | |
206 // the sum of the areas of the intersection of all child rectangles | |
207 // (excepting the candidate child) with the argument rectangle. Here the | |
208 // candidate child is indicated by index in |children_|, and expanded_rect | |
Peter Kasting
2014/05/16 01:41:05
Nit: "...Here |candidate| is the index of the cand
luken
2014/05/21 20:19:36
Done.
| |
209 // is the already-computed union of candidate's rect and rect. | |
Peter Kasting
2014/05/16 01:41:05
Nit: candidate's -> the candidate's; also put pipe
luken
2014/05/21 20:19:36
Done.
| |
210 int OverlapIncreaseToAdd(const Rect& rect, | |
211 size_t candidate, | |
212 const Rect& expanded_rect) const; | |
213 | |
214 // Children [0, split_index) within |sorted_children| remain within this | |
215 // node, while children [split_index, count()) are assigned to the new node. | |
Peter Kasting
2014/05/16 01:41:05
Nit: How about:
Returns a new node containing chi
luken
2014/05/21 20:19:36
Done.
| |
216 Node* DivideChildren(const Rects& low_bounds, | |
Peter Kasting
2014/05/16 01:41:05
This seems to return a new Node the caller is expe
luken
2014/05/21 20:19:36
Done.
| |
217 const Rects& high_bounds, | |
218 const std::vector<NodeBase*>& sorted_children, | |
219 size_t split_index); | |
220 | |
221 // Returns a pointer to the child node that will result in the least overlap | |
222 // increase with the addition of node_rect, or NULL if there's a tie found. | |
223 // Requires a precomputed vector of expanded rectangles where the ith | |
224 // rectangle in the vector is the union of |children_|[i] and node_rect. | |
225 // Overlap is defined in Beckmann et al. as the sum of the areas of | |
226 // intersection of all child rectangles with the |node_rect| argument | |
227 // rectangle. This heuristic attempts to choose the node for which adding | |
228 // the new rectangle to their bounding box will result in the least overlap | |
229 // with the other rectangles, thus trying to preserve the usefulness of the | |
230 // bounding rectangle by keeping it from covering too much redundant area. | |
231 Node* LeastOverlapIncrease(const Rect& node_rect, | |
232 const Rects& expanded_rects); | |
233 | |
234 // Returns a pointer to the child node that will result in the least area | |
235 // enlargement if the argument node rectangle were to be added to that | |
236 // nodes' bounding box. Requires a precomputed vector of expanded rectangles | |
Peter Kasting
2014/05/16 01:41:05
Nit: nodes -> node's
luken
2014/05/21 20:19:36
Done.
| |
237 // where the ith rectangle in the vector is the union of children_[i] and | |
238 // |node_rect|. | |
239 Node* LeastAreaEnlargement(const Rect& node_rect, | |
240 const Rects& expanded_rects); | |
241 | |
242 // Level counts from -1 for "record" Nodes, that is Nodes that wrap key | |
Peter Kasting
2014/05/16 01:41:05
This level can't ever be -1, because this class is
luken
2014/05/21 20:19:36
Done.
| |
243 // values, to 0 for leaf Nodes, that is Nodes that only have record | |
244 // children, up to the root Node, which has level equal to the height of the | |
245 // tree. For an R*-Tree to be considered well-formed all branches of the | |
246 // tree must have equal height. | |
247 const int level_; | |
248 | |
249 Nodes children_; | |
250 | |
251 friend class RTreeTest; | |
252 friend class RTreeNodeTest; | |
253 | |
254 DISALLOW_COPY_AND_ASSIGN(Node); | |
255 }; | |
256 | |
257 // Supports re-insertion of Nodes based on the strategies outlined in | |
258 // Beckmann et al. | |
259 void InsertNode(NodeBase* node, int* highset_reinsert_level); | |
Peter Kasting
2014/05/16 01:41:05
Nit: You might want to note what -1 would mean for
luken
2014/05/21 20:19:36
Done.
| |
260 | |
261 // Supports removal of nodes for tree without deletion. | |
262 void RemoveNode(NodeBase* node); | |
263 | |
264 // A pointer to the root node in the RTree. | |
265 scoped_ptr<Node> root_; | |
266 | |
267 // The parameters used to define the shape of the RTree. | |
268 size_t min_children_; | |
269 size_t max_children_; | |
270 | |
271 friend class RTreeTest; | |
272 friend class RTreeNodeTest; | |
273 | |
274 DISALLOW_COPY_AND_ASSIGN(RTreeBase); | |
275 }; | |
276 | |
277 } // namespace gfx | |
278 | |
279 #endif // UI_GFX_GEOMETRY_R_TREE_BASE_H_ | |
OLD | NEW |