Chromium Code Reviews| 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 // 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 RecordBase; | |
| 29 typedef std::list<const RecordBase*> Records; | |
|
Peter Kasting
2014/05/29 00:32:26
Are you sure you want a list rather than a vector?
luken
2014/05/30 16:51:04
I don't need any random access. I don't do any ins
Peter Kasting
2014/05/30 18:51:18
I don't think it's clear-cut. A list needs to hea
luken
2014/05/30 19:23:39
I changed it to a vector. Thanks for the detailed
| |
| 30 class NodeBase; | |
| 31 typedef ScopedVector<NodeBase> Nodes; | |
|
Peter Kasting
2014/05/29 00:32:26
Tiny nit: I'd probably place the forward-decls in
luken
2014/05/30 16:51:04
Done.
| |
| 32 | |
| 33 | |
| 34 friend class RTreeTest; | |
| 35 friend class RTreeNodeTest; | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: To match the order in r_tree.h, I'd probably
luken
2014/05/30 16:51:04
Done.
| |
| 36 | |
| 37 RTreeBase(size_t min_children, size_t max_children); | |
| 38 ~RTreeBase(); | |
| 39 | |
| 40 // Private data structure class for storing internal Nodes or leaves with | |
|
Peter Kasting
2014/05/29 00:32:26
This class isn't actually declared "private" in RT
luken
2014/05/30 16:51:04
Done.
| |
| 41 // Records. | |
| 42 class GFX_EXPORT NodeBase { | |
| 43 public: | |
| 44 virtual ~NodeBase(); | |
| 45 | |
| 46 // Appends to |records_out| the set of Records in this subtree with rects | |
| 47 // that intersect |query_rect|. Avoids clearing |records_out| so that it | |
| 48 // can be called recursively. | |
| 49 virtual void Query(const Rect& query_rect, Records* records_out) const = 0; | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: Would it be any clearer to name this AppendIn
luken
2014/05/30 16:51:04
Done.
| |
| 50 | |
| 51 // Returns NULL if no children. Does not recompute bounds. | |
| 52 virtual scoped_ptr<NodeBase> RemoveAndReturnLastChild() = 0; | |
| 53 | |
| 54 // Returns all records stored in the subtree rooted at this node. Appends to | |
| 55 // |matches_out| without clearing. | |
| 56 virtual void GetAllValues(Records* records_out) const = 0; | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: Would it be any clearer to name this AppendAl
luken
2014/05/30 16:51:04
Done.
| |
| 57 | |
| 58 // Level counts from -1 for Records to 0 for leaf Nodes, Nodes that only | |
| 59 // have Record children, up to the root Node, which has level equal to the | |
| 60 // height of the tree. For an R*-Tree to be considered well-formed all | |
| 61 // branches of the tree must have equal height. | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: This is pretty good, I wonder if the followin
luken
2014/05/30 16:51:04
Done.
| |
| 62 virtual const int Level() const = 0; | |
| 63 | |
| 64 // Recomputes our bounds by taking the union of all child rects, then calls | |
| 65 // recursively on our parent so that ultimately all nodes up to the root | |
| 66 // recompute their bounds. | |
| 67 void RecomputeBoundsUpToRoot(); | |
| 68 | |
| 69 NodeBase* parent() { return parent_; } | |
| 70 void set_parent(NodeBase* parent) { parent_ = parent; } | |
| 71 const Rect& rect() const { return rect_; } | |
| 72 void set_rect(const Rect& rect) { rect_ = rect; } | |
| 73 | |
| 74 protected: | |
| 75 friend class RTreeTest; | |
| 76 friend class RTreeNodeTest; | |
| 77 | |
| 78 NodeBase(const Rect& rect, NodeBase* parent); | |
| 79 | |
| 80 // Bounds recomputation without calling parents to do the same. | |
| 81 virtual void RecomputeLocalBounds(); | |
| 82 | |
| 83 private: | |
| 84 // This Node's bounding rectangle. | |
| 85 Rect rect_; | |
| 86 | |
| 87 // A weak pointer to our parent Node in the RTree. The root node will have a | |
| 88 // NULL value for |parent_|. | |
| 89 NodeBase* parent_; | |
| 90 | |
| 91 DISALLOW_COPY_AND_ASSIGN(NodeBase); | |
| 92 }; | |
| 93 | |
| 94 class GFX_EXPORT RecordBase : public NodeBase { | |
| 95 public: | |
| 96 RecordBase(const Rect& rect); | |
|
Peter Kasting
2014/05/29 00:32:26
From http://google-styleguide.googlecode.com/svn/t
luken
2014/05/30 16:51:04
Done.
| |
| 97 virtual ~RecordBase(); | |
| 98 | |
| 99 virtual void Query(const Rect& query_rect, | |
| 100 Records* records_out) const OVERRIDE; | |
| 101 virtual scoped_ptr<NodeBase> RemoveAndReturnLastChild() OVERRIDE; | |
| 102 virtual const int Level() const OVERRIDE; | |
| 103 | |
| 104 protected: | |
| 105 friend class RTreeTest; | |
| 106 friend class RTreeNodeTest; | |
| 107 | |
| 108 virtual void GetAllValues(Records* records_out) const OVERRIDE; | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(RecordBase); | |
|
Peter Kasting
2014/05/29 00:32:26
This declaration must always be private.
luken
2014/05/30 16:51:04
Done.
| |
| 111 }; | |
| 112 | |
| 113 class GFX_EXPORT Node : public NodeBase { | |
| 114 public: | |
| 115 // Constructs an empty Node with |level_| of 0. | |
| 116 Node(); | |
| 117 virtual ~Node(); | |
| 118 | |
| 119 virtual void Query(const Rect& query_rect, | |
| 120 Records* records_out) const OVERRIDE; | |
| 121 virtual scoped_ptr<NodeBase> RemoveAndReturnLastChild() OVERRIDE; | |
| 122 virtual const int Level() const OVERRIDE; | |
| 123 | |
| 124 // Constructs a new Node that is the parent of this Node and already has | |
| 125 // this Node as its sole child. Valid to call only on root Nodes, meaning | |
| 126 // Nodes with |parent_| NULL. Note that ownership of this Node is transfered | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: transferred
luken
2014/05/30 16:51:04
Done.
| |
| 127 // to the parent returned by this function. | |
| 128 scoped_ptr<Node> ConstructParent(); | |
| 129 | |
| 130 // Removes |number_to_remove| children from this Node, and appends them to | |
| 131 // the supplied list. Does not repair bounds upon completion. Nodes are | |
| 132 // selected in the manner suggested in the Beckmann et al. paper, which | |
| 133 // suggests that the children should be sorted by the distance from the | |
| 134 // center of their bounding rectangle to their parent's bounding retangle, | |
| 135 // and then the n closest children should be removed for re-insertion. This | |
| 136 // removal occurs at most once on each level of the tree when overflowing | |
| 137 // nodes that have exceeded the maximum number of children during an Insert. | |
| 138 void RemoveNodesForReinsert(size_t number_to_remove, Nodes* nodes); | |
| 139 | |
| 140 // Given a pointer to a child node within this Node, removes it from our | |
| 141 // list. If that child had any children, appends them to the supplied orphan | |
| 142 // list. Returns the new count of this node after removal. Does not | |
| 143 // recompute bounds, as the caller might subsequently remove this node as | |
| 144 // well, meaning the recomputation would be wasted work. | |
| 145 size_t 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* child(size_t i) const { return children_[i]; } | |
| 162 | |
| 163 private: | |
| 164 typedef std::vector<Rect> Rects; | |
| 165 | |
| 166 Node(int level); | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: explicit
luken
2014/05/30 16:51:04
Done.
| |
| 167 | |
| 168 // Given two arrays of bounds rectangles as computed by BuildLowBounds() | |
| 169 // and BuildHighBounds() returns the index of the element in those arrays | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: Comma before "returns"
luken
2014/05/30 16:51:04
Done.
| |
| 170 // along which a split of the arrays would result in a minimum amount of | |
| 171 // overlap (area of interesection) in the two groups. | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: intersection
luken
2014/05/30 16:51:04
Done.
| |
| 172 static size_t ChooseSplitIndex(size_t start_index, | |
| 173 size_t end_index, | |
| 174 const Rects& low_bounds, | |
| 175 const Rects& high_bounds); | |
| 176 | |
| 177 // R*-Tree attempts to keep groups of rectangles that are roughly square | |
| 178 // in shape. It does this by comparing the "margins" of different bounding | |
| 179 // boxes, where margin is defined as the sum of the length of all four sides | |
| 180 // of a rectangle. For two rectangles of equal area, the one with the | |
| 181 // smallest margin will be the rectangle that has width and height closest, | |
| 182 // to equality, or is the most square. When splitting we decide to split | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: "to equality, or" -> "i.e."
luken
2014/05/30 16:51:04
Done.
| |
| 183 // along an axis chosen from the rectangles either sorted vertically or | |
| 184 // horizontally by finding the axis that would result in the smallest sum of | |
| 185 // margins between the two bounding boxes of the resulting split. Returns | |
| 186 // the smallest sum computed given the sorted bounding boxes and a range to | |
| 187 // look within. | |
| 188 static int SmallestMarginSum(size_t start_index, | |
| 189 size_t end_index, | |
| 190 const Rects& low_bounds, | |
| 191 const Rects& high_bounds); | |
| 192 | |
| 193 // Sorts nodes primarily by increasing y coordinates, and secondarily by | |
| 194 // increasing height. | |
| 195 static bool CompareVertical(NodeBase* a, NodeBase* b); | |
| 196 | |
| 197 // Sorts nodes primarily by increasing x coordinates, and secondarily by | |
| 198 // increasing width. | |
| 199 static bool CompareHorizontal(NodeBase* a, NodeBase* b); | |
| 200 | |
| 201 // Sorts nodes by the distance of the center of their rectangles to the | |
| 202 // center of their parent's rectangles. | |
| 203 static bool CompareCenterDistanceFromParent(NodeBase* a, NodeBase* b); | |
| 204 | |
| 205 // Given two vectors of Nodes sorted by vertical or horizontal bounds, | |
| 206 // populates two vectors of Rectangles in which the ith element is the union | |
| 207 // of all bounding rectangles [0,i] in the associated sorted array of Nodes. | |
| 208 static void BuildLowBounds(const std::vector<NodeBase*>& vertical_sort, | |
| 209 const std::vector<NodeBase*>& horizontal_sort, | |
| 210 Rects* vertical_bounds, | |
| 211 Rects* horizontal_bounds); | |
| 212 | |
| 213 // Given two vectors of Nodes sorted by vertical or horizontal bounds, | |
| 214 // populates two vectors of Rectangles in which the ith element is the | |
| 215 // union of all bounding rectangles [i, count()) in the associated sorted | |
| 216 // array of Nodes. | |
| 217 static void BuildHighBounds(const std::vector<NodeBase*>& vertical_sort, | |
| 218 const std::vector<NodeBase*>& horizontal_sort, | |
| 219 Rects* vertical_bounds, | |
| 220 Rects* horizontal_bounds); | |
| 221 | |
| 222 virtual void RecomputeLocalBounds() OVERRIDE; | |
| 223 | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: I'd remove blank lines between uncommented fu
luken
2014/05/30 16:51:04
Done.
| |
| 224 virtual void GetAllValues(Records* matches_out) const 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| child is indicated by index in |children_|, and | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: This seems slightly out-of-date as the functi
luken
2014/05/30 16:51:04
Done.
| |
| 230 // |expanded_rect| is the already-computed union of the candidate's rect and | |
| 231 // |rect|. | |
| 232 int OverlapIncreaseToAdd(const Rect& rect, | |
| 233 const NodeBase* candidate_node, | |
| 234 const Rect& expanded_rect) const; | |
| 235 | |
| 236 // Returns a new node containing children [split_index, count()) within | |
| 237 // |sorted_children|. Children before |split_index| remain with |this|. | |
| 238 scoped_ptr<NodeBase> DivideChildren( | |
| 239 const Rects& low_bounds, | |
| 240 const Rects& high_bounds, | |
| 241 const std::vector<NodeBase*>& sorted_children, | |
| 242 size_t split_index); | |
| 243 | |
| 244 // Returns a pointer to the child node that will result in the least overlap | |
| 245 // increase with the addition of node_rect, or NULL if there's a tie found. | |
| 246 // Requires a precomputed vector of expanded rectangles where the ith | |
| 247 // rectangle in the vector is the union of |children_|[i] and node_rect. | |
| 248 // Overlap is defined in Beckmann et al. as the sum of the areas of | |
| 249 // intersection of all child rectangles with the |node_rect| argument | |
| 250 // rectangle. This heuristic attempts to choose the node for which adding | |
| 251 // the new rectangle to their bounding box will result in the least overlap | |
| 252 // with the other rectangles, thus trying to preserve the usefulness of the | |
| 253 // bounding rectangle by keeping it from covering too much redundant area. | |
| 254 Node* LeastOverlapIncrease(const Rect& node_rect, | |
| 255 const Rects& expanded_rects); | |
| 256 | |
| 257 // Returns a pointer to the child node that will result in the least area | |
| 258 // enlargement if the argument node rectangle were to be added to that | |
| 259 // node's bounding box. Requires a precomputed vector of expanded rectangles | |
| 260 // where the ith rectangle in the vector is the union of children_[i] and | |
| 261 // |node_rect|. | |
| 262 Node* LeastAreaEnlargement(const Rect& node_rect, | |
| 263 const Rects& expanded_rects); | |
| 264 | |
| 265 const int level_; | |
| 266 | |
| 267 Nodes children_; | |
| 268 | |
| 269 friend class RTreeTest; | |
| 270 friend class RTreeNodeTest; | |
| 271 | |
| 272 DISALLOW_COPY_AND_ASSIGN(Node); | |
| 273 }; | |
| 274 | |
| 275 // Supports re-insertion of Nodes based on the strategies outlined in | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: Maybe:
Re-inserts |node| into the tree. The
luken
2014/05/30 16:51:04
Done.
| |
| 276 // Beckmann et al. A value of -1 for |highest_reinsert_level| means that | |
| 277 // reinserts are permitted for every level of the tree. This is the | |
| 278 // recommended value for all initial calls to InsertNode. | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: Maybe this last sentence should be: "This sho
luken
2014/05/30 16:51:04
Done.
| |
| 279 void InsertNode(NodeBase* node, int* highset_reinsert_level); | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: highest
luken
2014/05/30 16:51:04
Done.
| |
| 280 | |
| 281 // Supports removal of nodes for tree without deletion. | |
|
Peter Kasting
2014/05/29 00:32:26
Nit: Maybe:
Removes |node| from the tree without
luken
2014/05/30 16:51:04
Done.
| |
| 282 void RemoveNode(NodeBase* node); | |
| 283 | |
| 284 // A pointer to the root node in the RTree. | |
| 285 scoped_ptr<Node> root_; | |
|
Peter Kasting
2014/05/29 00:32:26
I don't believe there was a "private:" declaration
luken
2014/05/30 16:51:04
Done.
| |
| 286 | |
| 287 // The parameters used to define the shape of the RTree. | |
| 288 size_t min_children_; | |
| 289 size_t max_children_; | |
| 290 | |
| 291 DISALLOW_COPY_AND_ASSIGN(RTreeBase); | |
| 292 }; | |
| 293 | |
| 294 } // namespace gfx | |
| 295 | |
| 296 #endif // UI_GFX_GEOMETRY_R_TREE_BASE_H_ | |
| OLD | NEW |