OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkQuadTree.h" | 8 #include "SkQuadTree.h" |
9 #include "SkTSort.h" | 9 #include "SkTSort.h" |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 class SkQuadTree::QuadTreeNode { | 13 static const int kSplitThreshold = 8; |
14 public: | 14 static const int kMinDimensions = 128; |
15 struct Data { | |
16 Data(const SkIRect& bounds, void* data) : fBounds(bounds), fInnerBounds( bounds), fData(data) {} | |
17 SkIRect fBounds; | |
18 SkIRect fInnerBounds; | |
19 void* fData; | |
20 }; | |
21 | |
22 QuadTreeNode(const SkIRect& bounds) | |
23 : fBounds(bounds) | |
24 , fTopLeft(NULL) | |
25 , fTopRight(NULL) | |
26 , fBottomLeft(NULL) | |
27 , fBottomRight(NULL) | |
28 , fCanSubdivide((fBounds.width() * fBounds.height()) > 0) {} | |
29 | |
30 ~QuadTreeNode() { | |
31 clear(); | |
32 } | |
33 | |
34 void clear() { | |
35 SkDELETE(fTopLeft); | |
36 fTopLeft = NULL; | |
37 SkDELETE(fTopRight); | |
38 fTopRight = NULL; | |
39 SkDELETE(fBottomLeft); | |
40 fBottomLeft = NULL; | |
41 SkDELETE(fBottomRight); | |
42 fBottomRight = NULL; | |
43 fData.reset(); | |
44 } | |
45 | |
46 const SkIRect& getBounds() const { return fBounds; } | |
47 | |
48 // Insert data into the QuadTreeNode | |
49 bool insert(Data& data) { | |
50 // Ignore objects which do not belong in this quad tree | |
51 return data.fInnerBounds.intersect(fBounds) && doInsert(data); | |
52 } | |
53 | |
54 // Find all data which appear within a range | |
55 void queryRange(const SkIRect& range, SkTDArray<void*>* dataInRange) const { | |
56 // Automatically abort if the range does not collide with this quad | |
57 if (!SkIRect::Intersects(fBounds, range)) { | |
58 return; // nothing added to the list | |
59 } | |
60 | |
61 // Check objects at this quad level | |
62 for (int i = 0; i < fData.count(); ++i) { | |
63 if (SkIRect::Intersects(fData[i].fBounds, range)) { | |
64 dataInRange->push(fData[i].fData); | |
65 } | |
66 } | |
67 | |
68 // Terminate here, if there are no children | |
69 if (!hasChildren()) { | |
70 return; | |
71 } | |
72 | |
73 // Otherwise, add the data from the children | |
74 fTopLeft->queryRange(range, dataInRange); | |
75 fTopRight->queryRange(range, dataInRange); | |
76 fBottomLeft->queryRange(range, dataInRange); | |
77 fBottomRight->queryRange(range, dataInRange); | |
78 } | |
79 | |
80 int getDepth(int i = 1) const { | |
81 if (hasChildren()) { | |
82 int depthTL = fTopLeft->getDepth(++i); | |
83 int depthTR = fTopRight->getDepth(i); | |
84 int depthBL = fBottomLeft->getDepth(i); | |
85 int depthBR = fBottomRight->getDepth(i); | |
86 return SkTMax(SkTMax(depthTL, depthTR), SkTMax(depthBL, depthBR)); | |
87 } | |
88 return i; | |
89 } | |
90 | |
91 void rewindInserts(SkBBoxHierarchyClient* client) { | |
92 for (int i = fData.count() - 1; i >= 0; --i) { | |
93 if (client->shouldRewind(fData[i].fData)) { | |
94 fData.remove(i); | |
95 } | |
96 } | |
97 if (hasChildren()) { | |
98 fTopLeft->rewindInserts(client); | |
99 fTopRight->rewindInserts(client); | |
100 fBottomLeft->rewindInserts(client); | |
101 fBottomRight->rewindInserts(client); | |
102 } | |
103 } | |
104 | |
105 private: | |
106 // create four children which fully divide this quad into four quads of equa l area | |
107 void subdivide() { | |
108 if (!hasChildren() && fCanSubdivide) { | |
109 SkIPoint center = SkIPoint::Make(fBounds.centerX(), fBounds.centerY( )); | |
110 fTopLeft = SkNEW_ARGS(QuadTreeNode, (SkIRect::MakeLTRB( | |
111 fBounds.fLeft, fBounds.fTop, center.fX, center.fY))); | |
112 fTopRight = SkNEW_ARGS(QuadTreeNode, (SkIRect::MakeLTRB( | |
113 center.fX, fBounds.fTop, fBounds.fRight, center.fY))); | |
114 fBottomLeft = SkNEW_ARGS(QuadTreeNode, (SkIRect::MakeLTRB( | |
115 fBounds.fLeft, center.fY, center.fX, fBounds.fBottom))); | |
116 fBottomRight = SkNEW_ARGS(QuadTreeNode, (SkIRect::MakeLTRB( | |
117 center.fX, center.fY, fBounds.fRight, fBounds.fBottom))); | |
118 | |
119 // If any of the data can fit entirely into a subregion, move it dow n now | |
120 for (int i = fData.count() - 1; i >= 0; --i) { | |
121 // If the data fits entirely into one of the 4 subregions, move that data | |
122 // down to that subregion. | |
123 if (fTopLeft->doInsert(fData[i]) || | |
124 fTopRight->doInsert(fData[i]) || | |
125 fBottomLeft->doInsert(fData[i]) || | |
126 fBottomRight->doInsert(fData[i])) { | |
127 fData.remove(i); | |
128 } | |
129 } | |
130 } | |
131 } | |
132 | |
133 bool doInsert(const Data& data) { | |
134 if (!fBounds.contains(data.fInnerBounds)) { | |
135 return false; | |
136 } | |
137 | |
138 if (fData.count() > kQuadTreeNodeCapacity) { | |
139 subdivide(); | |
140 } | |
141 | |
142 // If there is space in this quad tree, add the object here | |
143 // If this quadtree can't be subdivided, we have no choice but to add it here | |
144 if ((fData.count() <= kQuadTreeNodeCapacity) || !fCanSubdivide) { | |
145 if (fData.isEmpty()) { | |
146 fData.setReserve(kQuadTreeNodeCapacity); | |
147 } | |
148 fData.push(data); | |
149 } else if (!fTopLeft->doInsert(data) && !fTopRight->doInsert(data) && | |
150 !fBottomLeft->doInsert(data) && !fBottomRight->doInsert(data) ) { | |
151 // Can't be pushed down to children ? keep it here | |
152 fData.push(data); | |
153 } | |
154 | |
155 return true; | |
156 } | |
157 | |
158 bool hasChildren() const { | |
159 return (NULL != fTopLeft); | |
160 } | |
161 | |
162 // Arbitrary constant to indicate how many elements can be stored in this qu ad tree node | |
163 enum { kQuadTreeNodeCapacity = 4 }; | |
164 | |
165 // Bounds of this quad tree | |
166 SkIRect fBounds; | |
167 | |
168 // Data in this quad tree node | |
169 SkTDArray<Data> fData; | |
170 | |
171 // Children | |
172 QuadTreeNode* fTopLeft; | |
173 QuadTreeNode* fTopRight; | |
174 QuadTreeNode* fBottomLeft; | |
175 QuadTreeNode* fBottomRight; | |
176 | |
177 // Whether or not this node can have children | |
178 bool fCanSubdivide; | |
179 }; | |
180 | |
181 //////////////////////////////////////////////////////////////////////////////// /////////////////// | |
182 | 15 |
183 SkQuadTree* SkQuadTree::Create(const SkIRect& bounds) { | 16 SkQuadTree* SkQuadTree::Create(const SkIRect& bounds) { |
184 return new SkQuadTree(bounds); | 17 return new SkQuadTree(bounds); |
185 } | 18 } |
186 | 19 |
187 SkQuadTree::SkQuadTree(const SkIRect& bounds) | 20 SkQuadTree::SkQuadTree(const SkIRect& bounds) |
188 : fCount(0) | 21 : fEntryCount(0) |
189 , fRoot(SkNEW_ARGS(QuadTreeNode, (bounds))) { | 22 , fRoot(NULL) { |
190 SkASSERT((bounds.width() * bounds.height()) > 0); | 23 SkASSERT((bounds.width() * bounds.height()) > 0); |
24 fRoot = fAllNodes.pop(); | |
25 fRoot->fBounds = bounds; | |
191 } | 26 } |
192 | 27 |
193 SkQuadTree::~SkQuadTree() { | 28 SkQuadTree::~SkQuadTree() { |
194 SkDELETE(fRoot); | 29 } |
30 | |
31 SkQuadTree::Node* SkQuadTree::pickChild(Node* node, const SkIRect& bounds) const { | |
tomhudson
2014/03/05 11:56:56
IIRC in conversation you said you had support for
iancottrell
2014/03/05 12:11:16
Yes, I worry that web pages might have common path
| |
32 // is it entirely to the left? | |
33 int index = 0; | |
34 if (bounds.fRight < node->fCenter.fX) { | |
35 // Inside the left side | |
36 } else if(bounds.fLeft >= node->fCenter.fX) { | |
37 // Inside the right side | |
38 index |= 1; | |
39 } else { | |
40 // Not inside any children | |
41 return NULL; | |
42 } | |
43 if (bounds.fBottom < node->fCenter.fY) { | |
44 // Inside the top side | |
45 } else if(bounds.fTop >= node->fCenter.fY) { | |
46 // Inside the bottom side | |
47 index |= 2; | |
48 } else { | |
49 // Not inside any children | |
50 return NULL; | |
51 } | |
52 SkASSERT(node->fChildren[index]->fBounds.contains(bounds)); | |
53 return node->fChildren[index]; | |
54 } | |
55 | |
56 void SkQuadTree::insert(Node* node, Entry* entry) { | |
57 // does it belong in a child? | |
58 if (node->fChildren[0] != NULL) { | |
59 Node* child = pickChild(node, entry->fBounds); | |
60 if (child != NULL) { | |
61 insert(child, entry); | |
62 } else { | |
63 node->fEntries.push(entry); | |
64 } | |
65 return; | |
66 } | |
67 // No children yet, add to this node | |
68 node->fEntries.push(entry); | |
69 // should I split? | |
70 if (node->fEntries.getCount() < kSplitThreshold) { | |
71 return; | |
72 } | |
73 | |
74 if ((node->fBounds.width() < kMinDimensions) || | |
75 (node->fBounds.height() < kMinDimensions)) { | |
76 return; | |
77 } | |
78 | |
79 // Build all the children | |
80 node->fCenter = SkIPoint::Make(node->fBounds.centerX(), node->fBounds.center Y()); | |
81 for(int index=0; index<kChildCount; ++index) { | |
82 node->fChildren[index] = fAllNodes.pop(); | |
83 } | |
84 node->fChildren[0]->fBounds = SkIRect::MakeLTRB( | |
85 node->fBounds.fLeft, node->fBounds.fTop, node->fCenter.fX, nod e->fCenter.fY); | |
86 node->fChildren[1]->fBounds = SkIRect::MakeLTRB( | |
87 node->fCenter.fX, node->fBounds.fTop, node->fBounds.fRight, nod e->fCenter.fY); | |
88 node->fChildren[2]->fBounds = SkIRect::MakeLTRB( | |
89 node->fBounds.fLeft, node->fCenter.fY, node->fCenter.fX, nod e->fBounds.fBottom); | |
90 node->fChildren[3]->fBounds = SkIRect::MakeLTRB( | |
91 node->fCenter.fX, node->fCenter.fY, node->fBounds.fRight, nod e->fBounds.fBottom); | |
92 // reinsert all the entries of this node to allow child trickle | |
93 SkSList<Entry> entries; | |
94 entries.takeAll(node->fEntries); | |
95 while(!entries.isEmpty()) { | |
96 insert(node, entries.pop()); | |
97 } | |
98 } | |
99 | |
100 void SkQuadTree::search(Node* node, const SkIRect& query, SkTDArray<void*>* resu lts) const { | |
101 if (!SkIRect::IntersectsNoEmptyCheck(node->fBounds, query)) { | |
102 return; // nothing added to the list | |
103 } | |
104 for (Entry* entry = node->fEntries.head(); entry != NULL; entry = entry->fNe xt) { | |
105 if (SkIRect::IntersectsNoEmptyCheck(entry->fBounds, query)) { | |
106 results->push(entry->fData); | |
107 } | |
108 } | |
109 if (node->fChildren[0] != NULL) { | |
110 for(int index=0; index<kChildCount; ++index) { | |
111 search(node->fChildren[index], query, results); | |
112 } | |
113 } | |
114 } | |
115 | |
116 void SkQuadTree::clear(Node* node) { | |
117 // first clear the entries of this node | |
118 fAllEntries.takeAll(node->fEntries); | |
119 // recurse into and clear all child nodes | |
120 for(int index=0; index<kChildCount; ++index) { | |
121 Node* child = node->fChildren[index]; | |
122 node->fChildren[index] = NULL; | |
123 if (child != NULL) { | |
124 clear(child); | |
125 fAllNodes.push(child); | |
126 } | |
127 } | |
128 } | |
129 | |
130 int SkQuadTree::getDepth(Node* node) const { | |
131 int maxDepth = 0; | |
132 if (node->fChildren[0] != NULL) { | |
133 for(int index=0; index<kChildCount; ++index) { | |
134 maxDepth = SkMax32(maxDepth, getDepth(node->fChildren[index])); | |
135 } | |
136 } | |
137 return maxDepth + 1; | |
195 } | 138 } |
196 | 139 |
197 void SkQuadTree::insert(void* data, const SkIRect& bounds, bool) { | 140 void SkQuadTree::insert(void* data, const SkIRect& bounds, bool) { |
198 if (bounds.isEmpty()) { | 141 if (bounds.isEmpty()) { |
199 SkASSERT(false); | 142 SkASSERT(false); |
200 return; | 143 return; |
201 } | 144 } |
202 | 145 Entry* entry = fAllEntries.pop(); |
203 QuadTreeNode::Data quadTreeData(bounds, data); | 146 entry->fData = data; |
204 fRoot->insert(quadTreeData); | 147 entry->fBounds = bounds; |
205 ++fCount; | 148 ++fEntryCount; |
149 //if (fRoot->fEntries.isEmpty() && fRoot->fChildren[0] == NULL) { | |
150 // fDeferred.push(entry); | |
151 //} else { | |
152 insert(fRoot, entry); | |
153 //} | |
206 } | 154 } |
207 | 155 |
208 void SkQuadTree::search(const SkIRect& query, SkTDArray<void*>* results) { | 156 void SkQuadTree::search(const SkIRect& query, SkTDArray<void*>* results) { |
209 SkASSERT(NULL != results); | 157 SkASSERT(NULL != results); |
210 fRoot->queryRange(query, results); | 158 if (!query.isEmpty()) { |
159 search(fRoot, query, results); | |
160 } | |
211 } | 161 } |
212 | 162 |
213 void SkQuadTree::clear() { | 163 void SkQuadTree::clear() { |
214 fCount = 0; | 164 fEntryCount = 0; |
215 fRoot->clear(); | 165 clear(fRoot); |
216 } | 166 } |
217 | 167 |
218 int SkQuadTree::getDepth() const { | 168 int SkQuadTree::getDepth() const { |
219 return fRoot->getDepth(); | 169 return getDepth(fRoot); |
220 } | 170 } |
221 | 171 |
222 void SkQuadTree::rewindInserts() { | 172 void SkQuadTree::rewindInserts() { |
223 SkASSERT(fClient); | 173 SkASSERT(fClient); |
224 fRoot->rewindInserts(fClient); | 174 // Currently only supports deferred inserts |
175 SkASSERT(fRoot->fEntries.isEmpty() && fRoot->fChildren[0] == NULL); | |
176 SkSList<Entry> entries; | |
177 entries.takeAll(fDeferred); | |
178 while(!entries.isEmpty()) { | |
179 Entry* entry = entries.pop(); | |
180 if (fClient->shouldRewind(entry->fData)) { | |
181 entry->fData = NULL; | |
182 fAllEntries.push(entry); | |
183 --fEntryCount; | |
184 } else { | |
185 fDeferred.push(entry); | |
186 } | |
187 } | |
225 } | 188 } |
189 | |
190 void SkQuadTree::flushDeferredInserts() { | |
191 while(!fDeferred.isEmpty()) { | |
192 insert(fRoot, fDeferred.pop()); | |
193 } | |
194 } | |
OLD | NEW |