d3-octree.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. // https://github.com/vasturiano/d3-octree v0.2.0 Copyright 2021 Vasco Asturiano
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
  6. }(this, (function (exports) { 'use strict';
  7. function tree_add(d) {
  8. var x = +this._x.call(null, d),
  9. y = +this._y.call(null, d),
  10. z = +this._z.call(null, d);
  11. return add(this.cover(x, y, z), x, y, z, d);
  12. }
  13. function add(tree, x, y, z, d) {
  14. if (isNaN(x) || isNaN(y) || isNaN(z)) return tree; // ignore invalid points
  15. var parent,
  16. node = tree._root,
  17. leaf = {data: d},
  18. x0 = tree._x0,
  19. y0 = tree._y0,
  20. z0 = tree._z0,
  21. x1 = tree._x1,
  22. y1 = tree._y1,
  23. z1 = tree._z1,
  24. xm,
  25. ym,
  26. zm,
  27. xp,
  28. yp,
  29. zp,
  30. right,
  31. bottom,
  32. deep,
  33. i,
  34. j;
  35. // If the tree is empty, initialize the root as a leaf.
  36. if (!node) return tree._root = leaf, tree;
  37. // Find the existing leaf for the new point, or add it.
  38. while (node.length) {
  39. if (right = x >= (xm = (x0 + x1) / 2)) x0 = xm; else x1 = xm;
  40. if (bottom = y >= (ym = (y0 + y1) / 2)) y0 = ym; else y1 = ym;
  41. if (deep = z >= (zm = (z0 + z1) / 2)) z0 = zm; else z1 = zm;
  42. if (parent = node, !(node = node[i = deep << 2 | bottom << 1 | right])) return parent[i] = leaf, tree;
  43. }
  44. // Is the new point is exactly coincident with the existing point?
  45. xp = +tree._x.call(null, node.data);
  46. yp = +tree._y.call(null, node.data);
  47. zp = +tree._z.call(null, node.data);
  48. if (x === xp && y === yp && z === zp) return leaf.next = node, parent ? parent[i] = leaf : tree._root = leaf, tree;
  49. // Otherwise, split the leaf node until the old and new point are separated.
  50. do {
  51. parent = parent ? parent[i] = new Array(8) : tree._root = new Array(8);
  52. if (right = x >= (xm = (x0 + x1) / 2)) x0 = xm; else x1 = xm;
  53. if (bottom = y >= (ym = (y0 + y1) / 2)) y0 = ym; else y1 = ym;
  54. if (deep = z >= (zm = (z0 + z1) / 2)) z0 = zm; else z1 = zm;
  55. } while ((i = deep << 2 | bottom << 1 | right) === (j = (zp >= zm) << 2 | (yp >= ym) << 1 | (xp >= xm)));
  56. return parent[j] = node, parent[i] = leaf, tree;
  57. }
  58. function addAll(data) {
  59. var d, i, n = data.length,
  60. x,
  61. y,
  62. z,
  63. xz = new Array(n),
  64. yz = new Array(n),
  65. zz = new Array(n),
  66. x0 = Infinity,
  67. y0 = Infinity,
  68. z0 = Infinity,
  69. x1 = -Infinity,
  70. y1 = -Infinity,
  71. z1 = -Infinity;
  72. // Compute the points and their extent.
  73. for (i = 0; i < n; ++i) {
  74. if (isNaN(x = +this._x.call(null, d = data[i])) || isNaN(y = +this._y.call(null, d)) || isNaN(z = +this._z.call(null, d))) continue;
  75. xz[i] = x;
  76. yz[i] = y;
  77. zz[i] = z;
  78. if (x < x0) x0 = x;
  79. if (x > x1) x1 = x;
  80. if (y < y0) y0 = y;
  81. if (y > y1) y1 = y;
  82. if (z < z0) z0 = z;
  83. if (z > z1) z1 = z;
  84. }
  85. // If there were no (valid) points, abort.
  86. if (x0 > x1 || y0 > y1 || z0 > z1) return this;
  87. // Expand the tree to cover the new points.
  88. this.cover(x0, y0, z0).cover(x1, y1, z1);
  89. // Add the new points.
  90. for (i = 0; i < n; ++i) {
  91. add(this, xz[i], yz[i], zz[i], data[i]);
  92. }
  93. return this;
  94. }
  95. function tree_cover(x, y, z) {
  96. if (isNaN(x = +x) || isNaN(y = +y) || isNaN(z = +z)) return this; // ignore invalid points
  97. var x0 = this._x0,
  98. y0 = this._y0,
  99. z0 = this._z0,
  100. x1 = this._x1,
  101. y1 = this._y1,
  102. z1 = this._z1;
  103. // If the octree has no extent, initialize them.
  104. // Integer extent are necessary so that if we later double the extent,
  105. // the existing octant boundaries don’t change due to floating point error!
  106. if (isNaN(x0)) {
  107. x1 = (x0 = Math.floor(x)) + 1;
  108. y1 = (y0 = Math.floor(y)) + 1;
  109. z1 = (z0 = Math.floor(z)) + 1;
  110. }
  111. // Otherwise, double repeatedly to cover.
  112. else {
  113. var t = x1 - x0 || 1,
  114. node = this._root,
  115. parent,
  116. i;
  117. while (x0 > x || x >= x1 || y0 > y || y >= y1 || z0 > z || z >= z1) {
  118. i = (z < z0) << 2 | (y < y0) << 1 | (x < x0);
  119. parent = new Array(8), parent[i] = node, node = parent, t *= 2;
  120. switch (i) {
  121. case 0: x1 = x0 + t, y1 = y0 + t, z1 = z0 + t; break;
  122. case 1: x0 = x1 - t, y1 = y0 + t, z1 = z0 + t; break;
  123. case 2: x1 = x0 + t, y0 = y1 - t, z1 = z0 + t; break;
  124. case 3: x0 = x1 - t, y0 = y1 - t, z1 = z0 + t; break;
  125. case 4: x1 = x0 + t, y1 = y0 + t, z0 = z1 - t; break;
  126. case 5: x0 = x1 - t, y1 = y0 + t, z0 = z1 - t; break;
  127. case 6: x1 = x0 + t, y0 = y1 - t, z0 = z1 - t; break;
  128. case 7: x0 = x1 - t, y0 = y1 - t, z0 = z1 - t; break;
  129. }
  130. }
  131. if (this._root && this._root.length) this._root = node;
  132. }
  133. this._x0 = x0;
  134. this._y0 = y0;
  135. this._z0 = z0;
  136. this._x1 = x1;
  137. this._y1 = y1;
  138. this._z1 = z1;
  139. return this;
  140. }
  141. function tree_data() {
  142. var data = [];
  143. this.visit(function(node) {
  144. if (!node.length) do data.push(node.data); while (node = node.next)
  145. });
  146. return data;
  147. }
  148. function tree_extent(_) {
  149. return arguments.length
  150. ? this.cover(+_[0][0], +_[0][1], +_[0][2]).cover(+_[1][0], +_[1][1], +_[1][2])
  151. : isNaN(this._x0) ? undefined : [[this._x0, this._y0, this._z0], [this._x1, this._y1, this._z1]];
  152. }
  153. function Octant(node, x0, y0, z0, x1, y1, z1) {
  154. this.node = node;
  155. this.x0 = x0;
  156. this.y0 = y0;
  157. this.z0 = z0;
  158. this.x1 = x1;
  159. this.y1 = y1;
  160. this.z1 = z1;
  161. }
  162. function tree_find(x, y, z, radius) {
  163. var data,
  164. x0 = this._x0,
  165. y0 = this._y0,
  166. z0 = this._z0,
  167. x1,
  168. y1,
  169. z1,
  170. x2,
  171. y2,
  172. z2,
  173. x3 = this._x1,
  174. y3 = this._y1,
  175. z3 = this._z1,
  176. octs = [],
  177. node = this._root,
  178. q,
  179. i;
  180. if (node) octs.push(new Octant(node, x0, y0, z0, x3, y3, z3));
  181. if (radius == null) radius = Infinity;
  182. else {
  183. x0 = x - radius, y0 = y - radius, z0 = z - radius;
  184. x3 = x + radius, y3 = y + radius, z3 = z + radius;
  185. radius *= radius;
  186. }
  187. while (q = octs.pop()) {
  188. // Stop searching if this octant can’t contain a closer node.
  189. if (!(node = q.node)
  190. || (x1 = q.x0) > x3
  191. || (y1 = q.y0) > y3
  192. || (z1 = q.z0) > z3
  193. || (x2 = q.x1) < x0
  194. || (y2 = q.y1) < y0
  195. || (z2 = q.z1) < z0) continue;
  196. // Bisect the current octant.
  197. if (node.length) {
  198. var xm = (x1 + x2) / 2,
  199. ym = (y1 + y2) / 2,
  200. zm = (z1 + z2) / 2;
  201. octs.push(
  202. new Octant(node[7], xm, ym, zm, x2, y2, z2),
  203. new Octant(node[6], x1, ym, zm, xm, y2, z2),
  204. new Octant(node[5], xm, y1, zm, x2, ym, z2),
  205. new Octant(node[4], x1, y1, zm, xm, ym, z2),
  206. new Octant(node[3], xm, ym, z1, x2, y2, zm),
  207. new Octant(node[2], x1, ym, z1, xm, y2, zm),
  208. new Octant(node[1], xm, y1, z1, x2, ym, zm),
  209. new Octant(node[0], x1, y1, z1, xm, ym, zm)
  210. );
  211. // Visit the closest octant first.
  212. if (i = (z >= zm) << 2 | (y >= ym) << 1 | (x >= xm)) {
  213. q = octs[octs.length - 1];
  214. octs[octs.length - 1] = octs[octs.length - 1 - i];
  215. octs[octs.length - 1 - i] = q;
  216. }
  217. }
  218. // Visit this point. (Visiting coincident points isn’t necessary!)
  219. else {
  220. var dx = x - +this._x.call(null, node.data),
  221. dy = y - +this._y.call(null, node.data),
  222. dz = z - +this._z.call(null, node.data),
  223. d2 = dx * dx + dy * dy + dz * dz;
  224. if (d2 < radius) {
  225. var d = Math.sqrt(radius = d2);
  226. x0 = x - d, y0 = y - d, z0 = z - d;
  227. x3 = x + d, y3 = y + d, z3 = z + d;
  228. data = node.data;
  229. }
  230. }
  231. }
  232. return data;
  233. }
  234. function tree_remove(d) {
  235. if (isNaN(x = +this._x.call(null, d)) || isNaN(y = +this._y.call(null, d)) || isNaN(z = +this._z.call(null, d))) return this; // ignore invalid points
  236. var parent,
  237. node = this._root,
  238. retainer,
  239. previous,
  240. next,
  241. x0 = this._x0,
  242. y0 = this._y0,
  243. z0 = this._z0,
  244. x1 = this._x1,
  245. y1 = this._y1,
  246. z1 = this._z1,
  247. x,
  248. y,
  249. z,
  250. xm,
  251. ym,
  252. zm,
  253. right,
  254. bottom,
  255. deep,
  256. i,
  257. j;
  258. // If the tree is empty, initialize the root as a leaf.
  259. if (!node) return this;
  260. // Find the leaf node for the point.
  261. // While descending, also retain the deepest parent with a non-removed sibling.
  262. if (node.length) while (true) {
  263. if (right = x >= (xm = (x0 + x1) / 2)) x0 = xm; else x1 = xm;
  264. if (bottom = y >= (ym = (y0 + y1) / 2)) y0 = ym; else y1 = ym;
  265. if (deep = z >= (zm = (z0 + z1) / 2)) z0 = zm; else z1 = zm;
  266. if (!(parent = node, node = node[i = deep << 2 | bottom << 1 | right])) return this;
  267. if (!node.length) break;
  268. if (parent[(i + 1) & 7] || parent[(i + 2) & 7] || parent[(i + 3) & 7] || parent[(i + 4) & 7] || parent[(i + 5) & 7] || parent[(i + 6) & 7] || parent[(i + 7) & 7]) retainer = parent, j = i;
  269. }
  270. // Find the point to remove.
  271. while (node.data !== d) if (!(previous = node, node = node.next)) return this;
  272. if (next = node.next) delete node.next;
  273. // If there are multiple coincident points, remove just the point.
  274. if (previous) return (next ? previous.next = next : delete previous.next), this;
  275. // If this is the root point, remove it.
  276. if (!parent) return this._root = next, this;
  277. // Remove this leaf.
  278. next ? parent[i] = next : delete parent[i];
  279. // If the parent now contains exactly one leaf, collapse superfluous parents.
  280. if ((node = parent[0] || parent[1] || parent[2] || parent[3] || parent[4] || parent[5] || parent[6] || parent[7])
  281. && node === (parent[7] || parent[6] || parent[5] || parent[4] || parent[3] || parent[2] || parent[1] || parent[0])
  282. && !node.length) {
  283. if (retainer) retainer[j] = node;
  284. else this._root = node;
  285. }
  286. return this;
  287. }
  288. function removeAll(data) {
  289. for (var i = 0, n = data.length; i < n; ++i) this.remove(data[i]);
  290. return this;
  291. }
  292. function tree_root() {
  293. return this._root;
  294. }
  295. function tree_size() {
  296. var size = 0;
  297. this.visit(function(node) {
  298. if (!node.length) do ++size; while (node = node.next)
  299. });
  300. return size;
  301. }
  302. function tree_visit(callback) {
  303. var octs = [], q, node = this._root, child, x0, y0, z0, x1, y1, z1;
  304. if (node) octs.push(new Octant(node, this._x0, this._y0, this._z0, this._x1, this._y1, this._z1));
  305. while (q = octs.pop()) {
  306. if (!callback(node = q.node, x0 = q.x0, y0 = q.y0, z0 = q.z0, x1 = q.x1, y1 = q.y1, z1 = q.z1) && node.length) {
  307. var xm = (x0 + x1) / 2, ym = (y0 + y1) / 2, zm = (z0 + z1) / 2;
  308. if (child = node[7]) octs.push(new Octant(child, xm, ym, zm, x1, y1, z1));
  309. if (child = node[6]) octs.push(new Octant(child, x0, ym, zm, xm, y1, z1));
  310. if (child = node[5]) octs.push(new Octant(child, xm, y0, zm, x1, ym, z1));
  311. if (child = node[4]) octs.push(new Octant(child, x0, y0, zm, xm, ym, z1));
  312. if (child = node[3]) octs.push(new Octant(child, xm, ym, z0, x1, y1, zm));
  313. if (child = node[2]) octs.push(new Octant(child, x0, ym, z0, xm, y1, zm));
  314. if (child = node[1]) octs.push(new Octant(child, xm, y0, z0, x1, ym, zm));
  315. if (child = node[0]) octs.push(new Octant(child, x0, y0, z0, xm, ym, zm));
  316. }
  317. }
  318. return this;
  319. }
  320. function tree_visitAfter(callback) {
  321. var octs = [], next = [], q;
  322. if (this._root) octs.push(new Octant(this._root, this._x0, this._y0, this._z0, this._x1, this._y1, this._z1));
  323. while (q = octs.pop()) {
  324. var node = q.node;
  325. if (node.length) {
  326. var child, x0 = q.x0, y0 = q.y0, z0 = q.z0, x1 = q.x1, y1 = q.y1, z1 = q.z1, xm = (x0 + x1) / 2, ym = (y0 + y1) / 2, zm = (z0 + z1) / 2;
  327. if (child = node[0]) octs.push(new Octant(child, x0, y0, z0, xm, ym, zm));
  328. if (child = node[1]) octs.push(new Octant(child, xm, y0, z0, x1, ym, zm));
  329. if (child = node[2]) octs.push(new Octant(child, x0, ym, z0, xm, y1, zm));
  330. if (child = node[3]) octs.push(new Octant(child, xm, ym, z0, x1, y1, zm));
  331. if (child = node[4]) octs.push(new Octant(child, x0, y0, zm, xm, ym, z1));
  332. if (child = node[5]) octs.push(new Octant(child, xm, y0, zm, x1, ym, z1));
  333. if (child = node[6]) octs.push(new Octant(child, x0, ym, zm, xm, y1, z1));
  334. if (child = node[7]) octs.push(new Octant(child, xm, ym, zm, x1, y1, z1));
  335. }
  336. next.push(q);
  337. }
  338. while (q = next.pop()) {
  339. callback(q.node, q.x0, q.y0, q.z0, q.x1, q.y1, q.z1);
  340. }
  341. return this;
  342. }
  343. function defaultX(d) {
  344. return d[0];
  345. }
  346. function tree_x(_) {
  347. return arguments.length ? (this._x = _, this) : this._x;
  348. }
  349. function defaultY(d) {
  350. return d[1];
  351. }
  352. function tree_y(_) {
  353. return arguments.length ? (this._y = _, this) : this._y;
  354. }
  355. function defaultZ(d) {
  356. return d[2];
  357. }
  358. function tree_z(_) {
  359. return arguments.length ? (this._z = _, this) : this._z;
  360. }
  361. function octree(nodes, x, y, z) {
  362. var tree = new Octree(x == null ? defaultX : x, y == null ? defaultY : y, z == null ? defaultZ : z, NaN, NaN, NaN, NaN, NaN, NaN);
  363. return nodes == null ? tree : tree.addAll(nodes);
  364. }
  365. function Octree(x, y, z, x0, y0, z0, x1, y1, z1) {
  366. this._x = x;
  367. this._y = y;
  368. this._z = z;
  369. this._x0 = x0;
  370. this._y0 = y0;
  371. this._z0 = z0;
  372. this._x1 = x1;
  373. this._y1 = y1;
  374. this._z1 = z1;
  375. this._root = undefined;
  376. }
  377. function leaf_copy(leaf) {
  378. var copy = {data: leaf.data}, next = copy;
  379. while (leaf = leaf.next) next = next.next = {data: leaf.data};
  380. return copy;
  381. }
  382. var treeProto = octree.prototype = Octree.prototype;
  383. treeProto.copy = function() {
  384. var copy = new Octree(this._x, this._y, this._z, this._x0, this._y0, this._z0, this._x1, this._y1, this._z1),
  385. node = this._root,
  386. nodes,
  387. child;
  388. if (!node) return copy;
  389. if (!node.length) return copy._root = leaf_copy(node), copy;
  390. nodes = [{source: node, target: copy._root = new Array(8)}];
  391. while (node = nodes.pop()) {
  392. for (var i = 0; i < 8; ++i) {
  393. if (child = node.source[i]) {
  394. if (child.length) nodes.push({source: child, target: node.target[i] = new Array(8)});
  395. else node.target[i] = leaf_copy(child);
  396. }
  397. }
  398. }
  399. return copy;
  400. };
  401. treeProto.add = tree_add;
  402. treeProto.addAll = addAll;
  403. treeProto.cover = tree_cover;
  404. treeProto.data = tree_data;
  405. treeProto.extent = tree_extent;
  406. treeProto.find = tree_find;
  407. treeProto.remove = tree_remove;
  408. treeProto.removeAll = removeAll;
  409. treeProto.root = tree_root;
  410. treeProto.size = tree_size;
  411. treeProto.visit = tree_visit;
  412. treeProto.visitAfter = tree_visitAfter;
  413. treeProto.x = tree_x;
  414. treeProto.y = tree_y;
  415. treeProto.z = tree_z;
  416. exports.octree = octree;
  417. Object.defineProperty(exports, '__esModule', { value: true });
  418. })));