-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
RBush.js
220 lines (200 loc) · 5.06 KB
/
RBush.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* @module ol/structs/RBush
*/
import RBush_ from 'rbush';
import {createOrUpdate, equals} from '../extent.js';
import {getUid} from '../util.js';
import {isEmpty} from '../obj.js';
/**
* @typedef {import("rbush").BBox & {value: T}} Entry
* @template T
*/
/**
* @classdesc
* Wrapper around the RBush by Vladimir Agafonkin.
* See https://github.com/mourner/rbush.
*
* @template {Object} T
*/
class RBush {
/**
* @param {number} [maxEntries] Max entries.
*/
constructor(maxEntries) {
/**
* @private
* @type {RBush_<Entry<T>>}
*/
this.rbush_ = new RBush_(maxEntries);
/**
* A mapping between the objects added to this rbush wrapper
* and the objects that are actually added to the internal rbush.
* @private
* @type {Object<string, Entry<T>>}
*/
this.items_ = {};
}
/**
* Insert a value into the RBush.
* @param {import("../extent.js").Extent} extent Extent.
* @param {T} value Value.
*/
insert(extent, value) {
/** @type {Entry<T>} */
const item = {
minX: extent[0],
minY: extent[1],
maxX: extent[2],
maxY: extent[3],
value: value,
};
this.rbush_.insert(item);
this.items_[getUid(value)] = item;
}
/**
* Bulk-insert values into the RBush.
* @param {Array<import("../extent.js").Extent>} extents Extents.
* @param {Array<T>} values Values.
*/
load(extents, values) {
const items = new Array(values.length);
for (let i = 0, l = values.length; i < l; i++) {
const extent = extents[i];
const value = values[i];
/** @type {Entry<T>} */
const item = {
minX: extent[0],
minY: extent[1],
maxX: extent[2],
maxY: extent[3],
value: value,
};
items[i] = item;
this.items_[getUid(value)] = item;
}
this.rbush_.load(items);
}
/**
* Remove a value from the RBush.
* @param {T} value Value.
* @return {boolean} Removed.
*/
remove(value) {
const uid = getUid(value);
// get the object in which the value was wrapped when adding to the
// internal rbush. then use that object to do the removal.
const item = this.items_[uid];
delete this.items_[uid];
return this.rbush_.remove(item) !== null;
}
/**
* Update the extent of a value in the RBush.
* @param {import("../extent.js").Extent} extent Extent.
* @param {T} value Value.
*/
update(extent, value) {
const item = this.items_[getUid(value)];
const bbox = [item.minX, item.minY, item.maxX, item.maxY];
if (!equals(bbox, extent)) {
this.remove(value);
this.insert(extent, value);
}
}
/**
* Return all values in the RBush.
* @return {Array<T>} All.
*/
getAll() {
const items = this.rbush_.all();
return items.map(function (item) {
return item.value;
});
}
/**
* Return all values in the given extent.
* @param {import("../extent.js").Extent} extent Extent.
* @return {Array<T>} All in extent.
*/
getInExtent(extent) {
/** @type {import("rbush").BBox} */
const bbox = {
minX: extent[0],
minY: extent[1],
maxX: extent[2],
maxY: extent[3],
};
const items = this.rbush_.search(bbox);
return items.map(function (item) {
return item.value;
});
}
/**
* Calls a callback function with each value in the tree.
* If the callback returns a truthy value, this value is returned without
* checking the rest of the tree.
* @param {function(T): R} callback Callback.
* @return {R|undefined} Callback return value.
* @template R
*/
forEach(callback) {
return this.forEach_(this.getAll(), callback);
}
/**
* Calls a callback function with each value in the provided extent.
* @param {import("../extent.js").Extent} extent Extent.
* @param {function(T): R} callback Callback.
* @return {R|undefined} Callback return value.
* @template R
*/
forEachInExtent(extent, callback) {
return this.forEach_(this.getInExtent(extent), callback);
}
/**
* @param {Array<T>} values Values.
* @param {function(T): R} callback Callback.
* @return {R|undefined} Callback return value.
* @template R
* @private
*/
forEach_(values, callback) {
let result;
for (let i = 0, l = values.length; i < l; i++) {
result = callback(values[i]);
if (result) {
return result;
}
}
return result;
}
/**
* @return {boolean} Is empty.
*/
isEmpty() {
return isEmpty(this.items_);
}
/**
* Remove all values from the RBush.
*/
clear() {
this.rbush_.clear();
this.items_ = {};
}
/**
* @param {import("../extent.js").Extent} [extent] Extent.
* @return {import("../extent.js").Extent} Extent.
*/
getExtent(extent) {
const data = this.rbush_.toJSON();
return createOrUpdate(data.minX, data.minY, data.maxX, data.maxY, extent);
}
/**
* @param {RBush<T>} rbush R-Tree.
*/
concat(rbush) {
this.rbush_.load(rbush.rbush_.all());
for (const i in rbush.items_) {
this.items_[i] = rbush.items_[i];
}
}
}
export default RBush;