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

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

Issue 269513002: readability review for luken (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: switch to no owners Created 6 years, 6 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 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 // Provides an implementation the parts of the RTree data structure that don't
6 // require knowledge of the generic key type. Don't use these objects directly,
7 // rather specialize the RTree<> object in r_tree.h. This file defines the
8 // internal objects of an RTree, namely Nodes (internal nodes of the tree) and
9 // Records, which hold (key, rectangle) pairs.
10
11 #ifndef UI_GFX_GEOMETRY_R_TREE_BASE_H_
12 #define UI_GFX_GEOMETRY_R_TREE_BASE_H_
13
14 #include <list>
15 #include <vector>
16
17 #include "base/containers/hash_tables.h"
18 #include "base/macros.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/memory/scoped_vector.h"
21 #include "ui/gfx/geometry/rect.h"
22 #include "ui/gfx/gfx_export.h"
23
24 namespace gfx {
25
26 class GFX_EXPORT RTreeBase {
27 protected:
28 class NodeBase;
29 class RecordBase;
30
31 typedef std::vector<const RecordBase*> Records;
32 typedef ScopedVector<NodeBase> Nodes;
33
34 RTreeBase(size_t min_children, size_t max_children);
35 ~RTreeBase();
36
37 // Protected data structure class for storing internal Nodes or leaves with
38 // Records.
39 class GFX_EXPORT NodeBase {
40 public:
41 virtual ~NodeBase();
42
43 // Appends to |records_out| the set of Records in this subtree with rects
44 // that intersect |query_rect|. Avoids clearing |records_out| so that it
45 // can be called recursively.
46 virtual void AppendIntersectingRecords(
47 const Rect& query_rect, Records* records_out) const = 0;
48
49 // Returns all records stored in the subtree rooted at this node. Appends to
50 // |matches_out| without clearing.
51 virtual void AppendAllRecords(Records* records_out) const = 0;
52
53 // Returns NULL if no children. Does not recompute bounds.
54 virtual scoped_ptr<NodeBase> RemoveAndReturnLastChild() = 0;
55
56 // Returns -1 for Records, or the height of this subtree for Nodes. The
57 // height of a leaf Node (a Node containing only Records) is 0, a leaf's
58 // parent is 1, etc. Note that in an R*-Tree, all branches from the root
59 // Node will be the same height.
60 virtual const int Level() const = 0;
61
62 // Recomputes our bounds by taking the union of all child rects, then calls
63 // recursively on our parent so that ultimately all nodes up to the root
64 // recompute their bounds.
65 void RecomputeBoundsUpToRoot();
66
67 NodeBase* parent() { return parent_; }
68 const NodeBase* const_parent() const { return parent_; }
Peter Kasting 2014/06/03 23:52:18 From http://google-styleguide.googlecode.com/svn/t
luken 2014/06/04 18:12:03 Cool, I like your construction better. Question -
Peter Kasting 2014/06/04 18:21:40 Nope, overloading is OK when it doesn't cause conf
69 void set_parent(NodeBase* parent) { parent_ = parent; }
70 const Rect& rect() const { return rect_; }
71 void set_rect(const Rect& rect) { rect_ = rect; }
72
73 protected:
74 friend class RTreeTest;
75 friend class RTreeNodeTest;
Peter Kasting 2014/06/03 23:52:18 From http://google-styleguide.googlecode.com/svn/t
luken 2014/06/04 18:12:03 Done.
76
77 NodeBase(const Rect& rect, NodeBase* parent);
78
79 // Bounds recomputation without calling parents to do the same.
80 virtual void RecomputeLocalBounds();
81
82 private:
83 // This Node's bounding rectangle.
84 Rect rect_;
85
86 // A weak pointer to our parent Node in the RTree. The root node will have a
87 // NULL value for |parent_|.
88 NodeBase* parent_;
89
90 DISALLOW_COPY_AND_ASSIGN(NodeBase);
91 };
92
93 class GFX_EXPORT RecordBase : public NodeBase {
94 public:
95 explicit RecordBase(const Rect& rect);
96 virtual ~RecordBase();
97
98 virtual void AppendIntersectingRecords(
99 const Rect& query_rect, Records* records_out) const OVERRIDE;
100 virtual void AppendAllRecords(Records* records_out) const OVERRIDE;
101 virtual scoped_ptr<NodeBase> RemoveAndReturnLastChild() OVERRIDE;
102 virtual const int Level() const OVERRIDE;
103
104 private:
105 friend class RTreeTest;
106 friend class RTreeNodeTest;
107
108 DISALLOW_COPY_AND_ASSIGN(RecordBase);
109 };
110
111 class GFX_EXPORT Node : public NodeBase {
112 public:
113 // Constructs an empty Node with |level_| of 0.
114 Node();
115 virtual ~Node();
116
117 virtual void AppendIntersectingRecords(
118 const Rect& query_rect, Records* records_out) const OVERRIDE;
119 virtual scoped_ptr<NodeBase> RemoveAndReturnLastChild() OVERRIDE;
120 virtual const int Level() const OVERRIDE;
121 virtual void AppendAllRecords(Records* matches_out) const OVERRIDE;
122
123 // Constructs a new Node that is the parent of this Node and already has
124 // this Node as its sole child. Valid to call only on root Nodes, meaning
125 // Nodes with |parent_| NULL. Note that ownership of this Node is
126 // transferred to the parent returned by this function.
127 scoped_ptr<Node> ConstructParent();
128
129 // Removes |number_to_remove| children from this Node, and appends them to
130 // the supplied list. Does not repair bounds upon completion. Nodes are
131 // selected in the manner suggested in the Beckmann et al. paper, which
132 // suggests that the children should be sorted by the distance from the
133 // center of their bounding rectangle to their parent's bounding rectangle,
134 // and then the n closest children should be removed for re-insertion. This
135 // removal occurs at most once on each level of the tree when overflowing
136 // nodes that have exceeded the maximum number of children during an Insert.
137 void RemoveNodesForReinsert(size_t number_to_remove, Nodes* nodes);
138
139 // Given a pointer to a child node within this Node, removes it from our
140 // list. If that child had any children, appends them to the supplied orphan
141 // list. Returns the |child_node| in a scoped_ptr as the node is
Peter Kasting 2014/06/03 23:52:18 Nit: This sentence can just be "Returns the remove
luken 2014/06/04 18:12:03 Done.
142 // relinquishing ownership of it. Does not recompute bounds, as the caller
143 // might subsequently remove this node as well, meaning the recomputation
144 // would be wasted work.
145 scoped_ptr<NodeBase> RemoveChild(NodeBase* child_node, Nodes* orphans);
146
147 // Returns the best parent for insertion of the provided |node| as a child.
148 Node* ChooseSubtree(NodeBase* node);
149
150 // Adds |node| as a child of this Node, and recomputes the bounds of this
151 // node after the addition of the child. Returns the new count of children
152 // stored in this Node. This node becomes the owner of |node|.
153 size_t AddChild(scoped_ptr<NodeBase> node);
154
155 // Returns a sibling to this Node with at least min_children and no greater
156 // than max_children of this Node's children assigned to it, and having the
157 // same parent. Bounds will be valid on both Nodes after this call.
158 scoped_ptr<NodeBase> Split(size_t min_children, size_t max_children);
159
160 const size_t count() const { return children_.size(); }
161 const NodeBase* const_child(size_t i) const { return children_[i]; }
Peter Kasting 2014/06/03 23:52:18 Nit: See comment above
luken 2014/06/04 18:12:03 Done.
162 NodeBase* child(size_t i) { return children_[i]; }
163
164 private:
165 typedef std::vector<Rect> Rects;
166
167 explicit Node(int level);
168
169 // Given two arrays of bounds rectangles as computed by BuildLowBounds()
170 // and BuildHighBounds(), returns the index of the element in those arrays
171 // along which a split of the arrays would result in a minimum amount of
172 // overlap (area of intersection) in the two groups.
173 static size_t ChooseSplitIndex(size_t start_index,
174 size_t end_index,
175 const Rects& low_bounds,
176 const Rects& high_bounds);
177
178 // R*-Tree attempts to keep groups of rectangles that are roughly square
179 // in shape. It does this by comparing the "margins" of different bounding
180 // boxes, where margin is defined as the sum of the length of all four sides
181 // of a rectangle. For two rectangles of equal area, the one with the
182 // smallest margin will be the rectangle that has width and height closest
183 // to equality, or is the most square. When splitting we decide to split
Peter Kasting 2014/06/03 23:52:18 Nit: I would still substitute "i.e." for "or" sinc
luken 2014/06/04 18:12:03 Done.
184 // along an axis chosen from the rectangles either sorted vertically or
185 // horizontally by finding the axis that would result in the smallest sum of
186 // margins between the two bounding boxes of the resulting split. Returns
187 // the smallest sum computed given the sorted bounding boxes and a range to
188 // look within.
189 static int SmallestMarginSum(size_t start_index,
190 size_t end_index,
191 const Rects& low_bounds,
192 const Rects& high_bounds);
193
194 // Sorts nodes primarily by increasing y coordinates, and secondarily by
195 // increasing height.
196 static bool CompareVertical(const NodeBase* a, const NodeBase* b);
197
198 // Sorts nodes primarily by increasing x coordinates, and secondarily by
199 // increasing width.
200 static bool CompareHorizontal(const NodeBase* a, const NodeBase* b);
201
202 // Sorts nodes by the distance of the center of their rectangles to the
203 // center of their parent's rectangles.
204 static bool CompareCenterDistanceFromParent(
205 const NodeBase* a, const NodeBase* b);
206
207 // Given two vectors of Nodes sorted by vertical or horizontal bounds,
208 // populates two vectors of Rectangles in which the ith element is the union
209 // of all bounding rectangles [0,i] in the associated sorted array of Nodes.
210 static void BuildLowBounds(const std::vector<NodeBase*>& vertical_sort,
211 const std::vector<NodeBase*>& horizontal_sort,
212 Rects* vertical_bounds,
213 Rects* horizontal_bounds);
214
215 // Given two vectors of Nodes sorted by vertical or horizontal bounds,
216 // populates two vectors of Rectangles in which the ith element is the
217 // union of all bounding rectangles [i, count()) in the associated sorted
218 // array of Nodes.
219 static void BuildHighBounds(const std::vector<NodeBase*>& vertical_sort,
220 const std::vector<NodeBase*>& horizontal_sort,
221 Rects* vertical_bounds,
222 Rects* horizontal_bounds);
223
224 virtual void RecomputeLocalBounds() OVERRIDE;
225
226 // Returns the increase in overlap value, as defined in Beckmann et al. as
227 // the sum of the areas of the intersection of all child rectangles
228 // (excepting the candidate child) with the argument rectangle. Here the
229 // |candidate_node| is one of our |children_|, and |expanded_rect| is the
230 // already-computed union of the candidate's rect and |rect|.
231 int OverlapIncreaseToAdd(const Rect& rect,
232 const NodeBase* candidate_node,
233 const Rect& expanded_rect) const;
234
235 // Returns a new node containing children [split_index, count()) within
236 // |sorted_children|. Children before |split_index| remain with |this|.
237 scoped_ptr<NodeBase> DivideChildren(
238 const Rects& low_bounds,
239 const Rects& high_bounds,
240 const std::vector<NodeBase*>& sorted_children,
241 size_t split_index);
242
243 // Returns a pointer to the child node that will result in the least overlap
244 // increase with the addition of node_rect, or NULL if there's a tie found.
245 // Requires a precomputed vector of expanded rectangles where the ith
246 // rectangle in the vector is the union of |children_|[i] and node_rect.
247 // Overlap is defined in Beckmann et al. as the sum of the areas of
248 // intersection of all child rectangles with the |node_rect| argument
249 // rectangle. This heuristic attempts to choose the node for which adding
250 // the new rectangle to their bounding box will result in the least overlap
251 // with the other rectangles, thus trying to preserve the usefulness of the
252 // bounding rectangle by keeping it from covering too much redundant area.
253 Node* LeastOverlapIncrease(const Rect& node_rect,
254 const Rects& expanded_rects);
255
256 // Returns a pointer to the child node that will result in the least area
257 // enlargement if the argument node rectangle were to be added to that
258 // node's bounding box. Requires a precomputed vector of expanded rectangles
259 // where the ith rectangle in the vector is the union of children_[i] and
260 // |node_rect|.
261 Node* LeastAreaEnlargement(const Rect& node_rect,
262 const Rects& expanded_rects);
263
264 const int level_;
265
266 Nodes children_;
267
268 friend class RTreeTest;
269 friend class RTreeNodeTest;
270
271 DISALLOW_COPY_AND_ASSIGN(Node);
272 };
273
274 // Inserts |node| into the tree. The |highest_reinsert_level| supports
275 // re-insertion as described by Beckmann et al. As Node overflows progagate
276 // up the tree the algorithm performs a reinsertion of the overflow Nodes
277 // (instead of a split) at most once per level of the tree. A starting value
278 // of -1 for |highest_reinsert_level| means that reinserts are permitted for
279 // every level of the tree. This should always be set to -1 except by
280 // recursive calls from within InsertNode().
281 void InsertNode(scoped_ptr<NodeBase> node, int* highest_reinsert_level);
282
283 // Removes |node| from the tree without deleting it.
284 scoped_ptr<NodeBase> RemoveNode(NodeBase* node);
285
286 // Deletes the |root_| Node and replaces it with its only descendant child.
287 void PruneRoot();
288
289 // Deletes the current |root_| and replaces it with an empty Node.
Peter Kasting 2014/06/03 23:52:18 This has the side effect of deleting the entire ex
luken 2014/06/04 18:12:03 Done.
290 void ResetRoot();
291
292 const Node* root() const { return root_.get(); }
293
294 private:
295 friend class RTreeTest;
296 friend class RTreeNodeTest;
297
298 // A pointer to the root node in the RTree.
299 scoped_ptr<Node> root_;
300
301 // The parameters used to define the shape of the RTree.
302 const size_t min_children_;
303 const size_t max_children_;
304
305 DISALLOW_COPY_AND_ASSIGN(RTreeBase);
306 };
307
308 } // namespace gfx
309
310 #endif // UI_GFX_GEOMETRY_R_TREE_BASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698