Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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