Node-Red configuration
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

d3-contour.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // https://d3js.org/d3-contour/ v4.0.2 Copyright 2012-2023 Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('d3-array')) :
  4. typeof define === 'function' && define.amd ? define(['exports', 'd3-array'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}, global.d3));
  6. })(this, (function (exports, d3Array) { 'use strict';
  7. var array = Array.prototype;
  8. var slice = array.slice;
  9. function ascending(a, b) {
  10. return a - b;
  11. }
  12. function area(ring) {
  13. var i = 0, n = ring.length, area = ring[n - 1][1] * ring[0][0] - ring[n - 1][0] * ring[0][1];
  14. while (++i < n) area += ring[i - 1][1] * ring[i][0] - ring[i - 1][0] * ring[i][1];
  15. return area;
  16. }
  17. var constant = x => () => x;
  18. function contains(ring, hole) {
  19. var i = -1, n = hole.length, c;
  20. while (++i < n) if (c = ringContains(ring, hole[i])) return c;
  21. return 0;
  22. }
  23. function ringContains(ring, point) {
  24. var x = point[0], y = point[1], contains = -1;
  25. for (var i = 0, n = ring.length, j = n - 1; i < n; j = i++) {
  26. var pi = ring[i], xi = pi[0], yi = pi[1], pj = ring[j], xj = pj[0], yj = pj[1];
  27. if (segmentContains(pi, pj, point)) return 0;
  28. if (((yi > y) !== (yj > y)) && ((x < (xj - xi) * (y - yi) / (yj - yi) + xi))) contains = -contains;
  29. }
  30. return contains;
  31. }
  32. function segmentContains(a, b, c) {
  33. var i; return collinear(a, b, c) && within(a[i = +(a[0] === b[0])], c[i], b[i]);
  34. }
  35. function collinear(a, b, c) {
  36. return (b[0] - a[0]) * (c[1] - a[1]) === (c[0] - a[0]) * (b[1] - a[1]);
  37. }
  38. function within(p, q, r) {
  39. return p <= q && q <= r || r <= q && q <= p;
  40. }
  41. function noop() {}
  42. var cases = [
  43. [],
  44. [[[1.0, 1.5], [0.5, 1.0]]],
  45. [[[1.5, 1.0], [1.0, 1.5]]],
  46. [[[1.5, 1.0], [0.5, 1.0]]],
  47. [[[1.0, 0.5], [1.5, 1.0]]],
  48. [[[1.0, 1.5], [0.5, 1.0]], [[1.0, 0.5], [1.5, 1.0]]],
  49. [[[1.0, 0.5], [1.0, 1.5]]],
  50. [[[1.0, 0.5], [0.5, 1.0]]],
  51. [[[0.5, 1.0], [1.0, 0.5]]],
  52. [[[1.0, 1.5], [1.0, 0.5]]],
  53. [[[0.5, 1.0], [1.0, 0.5]], [[1.5, 1.0], [1.0, 1.5]]],
  54. [[[1.5, 1.0], [1.0, 0.5]]],
  55. [[[0.5, 1.0], [1.5, 1.0]]],
  56. [[[1.0, 1.5], [1.5, 1.0]]],
  57. [[[0.5, 1.0], [1.0, 1.5]]],
  58. []
  59. ];
  60. function Contours() {
  61. var dx = 1,
  62. dy = 1,
  63. threshold = d3Array.thresholdSturges,
  64. smooth = smoothLinear;
  65. function contours(values) {
  66. var tz = threshold(values);
  67. // Convert number of thresholds into uniform thresholds.
  68. if (!Array.isArray(tz)) {
  69. const e = d3Array.extent(values, finite);
  70. tz = d3Array.ticks(...d3Array.nice(e[0], e[1], tz), tz);
  71. while (tz[tz.length - 1] >= e[1]) tz.pop();
  72. while (tz[1] < e[0]) tz.shift();
  73. } else {
  74. tz = tz.slice().sort(ascending);
  75. }
  76. return tz.map(value => contour(values, value));
  77. }
  78. // Accumulate, smooth contour rings, assign holes to exterior rings.
  79. // Based on https://github.com/mbostock/shapefile/blob/v0.6.2/shp/polygon.js
  80. function contour(values, value) {
  81. const v = value == null ? NaN : +value;
  82. if (isNaN(v)) throw new Error(`invalid value: ${value}`);
  83. var polygons = [],
  84. holes = [];
  85. isorings(values, v, function(ring) {
  86. smooth(ring, values, v);
  87. if (area(ring) > 0) polygons.push([ring]);
  88. else holes.push(ring);
  89. });
  90. holes.forEach(function(hole) {
  91. for (var i = 0, n = polygons.length, polygon; i < n; ++i) {
  92. if (contains((polygon = polygons[i])[0], hole) !== -1) {
  93. polygon.push(hole);
  94. return;
  95. }
  96. }
  97. });
  98. return {
  99. type: "MultiPolygon",
  100. value: value,
  101. coordinates: polygons
  102. };
  103. }
  104. // Marching squares with isolines stitched into rings.
  105. // Based on https://github.com/topojson/topojson-client/blob/v3.0.0/src/stitch.js
  106. function isorings(values, value, callback) {
  107. var fragmentByStart = new Array,
  108. fragmentByEnd = new Array,
  109. x, y, t0, t1, t2, t3;
  110. // Special case for the first row (y = -1, t2 = t3 = 0).
  111. x = y = -1;
  112. t1 = above(values[0], value);
  113. cases[t1 << 1].forEach(stitch);
  114. while (++x < dx - 1) {
  115. t0 = t1, t1 = above(values[x + 1], value);
  116. cases[t0 | t1 << 1].forEach(stitch);
  117. }
  118. cases[t1 << 0].forEach(stitch);
  119. // General case for the intermediate rows.
  120. while (++y < dy - 1) {
  121. x = -1;
  122. t1 = above(values[y * dx + dx], value);
  123. t2 = above(values[y * dx], value);
  124. cases[t1 << 1 | t2 << 2].forEach(stitch);
  125. while (++x < dx - 1) {
  126. t0 = t1, t1 = above(values[y * dx + dx + x + 1], value);
  127. t3 = t2, t2 = above(values[y * dx + x + 1], value);
  128. cases[t0 | t1 << 1 | t2 << 2 | t3 << 3].forEach(stitch);
  129. }
  130. cases[t1 | t2 << 3].forEach(stitch);
  131. }
  132. // Special case for the last row (y = dy - 1, t0 = t1 = 0).
  133. x = -1;
  134. t2 = values[y * dx] >= value;
  135. cases[t2 << 2].forEach(stitch);
  136. while (++x < dx - 1) {
  137. t3 = t2, t2 = above(values[y * dx + x + 1], value);
  138. cases[t2 << 2 | t3 << 3].forEach(stitch);
  139. }
  140. cases[t2 << 3].forEach(stitch);
  141. function stitch(line) {
  142. var start = [line[0][0] + x, line[0][1] + y],
  143. end = [line[1][0] + x, line[1][1] + y],
  144. startIndex = index(start),
  145. endIndex = index(end),
  146. f, g;
  147. if (f = fragmentByEnd[startIndex]) {
  148. if (g = fragmentByStart[endIndex]) {
  149. delete fragmentByEnd[f.end];
  150. delete fragmentByStart[g.start];
  151. if (f === g) {
  152. f.ring.push(end);
  153. callback(f.ring);
  154. } else {
  155. fragmentByStart[f.start] = fragmentByEnd[g.end] = {start: f.start, end: g.end, ring: f.ring.concat(g.ring)};
  156. }
  157. } else {
  158. delete fragmentByEnd[f.end];
  159. f.ring.push(end);
  160. fragmentByEnd[f.end = endIndex] = f;
  161. }
  162. } else if (f = fragmentByStart[endIndex]) {
  163. if (g = fragmentByEnd[startIndex]) {
  164. delete fragmentByStart[f.start];
  165. delete fragmentByEnd[g.end];
  166. if (f === g) {
  167. f.ring.push(end);
  168. callback(f.ring);
  169. } else {
  170. fragmentByStart[g.start] = fragmentByEnd[f.end] = {start: g.start, end: f.end, ring: g.ring.concat(f.ring)};
  171. }
  172. } else {
  173. delete fragmentByStart[f.start];
  174. f.ring.unshift(start);
  175. fragmentByStart[f.start = startIndex] = f;
  176. }
  177. } else {
  178. fragmentByStart[startIndex] = fragmentByEnd[endIndex] = {start: startIndex, end: endIndex, ring: [start, end]};
  179. }
  180. }
  181. }
  182. function index(point) {
  183. return point[0] * 2 + point[1] * (dx + 1) * 4;
  184. }
  185. function smoothLinear(ring, values, value) {
  186. ring.forEach(function(point) {
  187. var x = point[0],
  188. y = point[1],
  189. xt = x | 0,
  190. yt = y | 0,
  191. v1 = valid(values[yt * dx + xt]);
  192. if (x > 0 && x < dx && xt === x) {
  193. point[0] = smooth1(x, valid(values[yt * dx + xt - 1]), v1, value);
  194. }
  195. if (y > 0 && y < dy && yt === y) {
  196. point[1] = smooth1(y, valid(values[(yt - 1) * dx + xt]), v1, value);
  197. }
  198. });
  199. }
  200. contours.contour = contour;
  201. contours.size = function(_) {
  202. if (!arguments.length) return [dx, dy];
  203. var _0 = Math.floor(_[0]), _1 = Math.floor(_[1]);
  204. if (!(_0 >= 0 && _1 >= 0)) throw new Error("invalid size");
  205. return dx = _0, dy = _1, contours;
  206. };
  207. contours.thresholds = function(_) {
  208. return arguments.length ? (threshold = typeof _ === "function" ? _ : Array.isArray(_) ? constant(slice.call(_)) : constant(_), contours) : threshold;
  209. };
  210. contours.smooth = function(_) {
  211. return arguments.length ? (smooth = _ ? smoothLinear : noop, contours) : smooth === smoothLinear;
  212. };
  213. return contours;
  214. }
  215. // When computing the extent, ignore infinite values (as well as invalid ones).
  216. function finite(x) {
  217. return isFinite(x) ? x : NaN;
  218. }
  219. // Is the (possibly invalid) x greater than or equal to the (known valid) value?
  220. // Treat any invalid value as below negative infinity.
  221. function above(x, value) {
  222. return x == null ? false : +x >= value;
  223. }
  224. // During smoothing, treat any invalid value as negative infinity.
  225. function valid(v) {
  226. return v == null || isNaN(v = +v) ? -Infinity : v;
  227. }
  228. function smooth1(x, v0, v1, value) {
  229. const a = value - v0;
  230. const b = v1 - v0;
  231. const d = isFinite(a) || isFinite(b) ? a / b : Math.sign(a) / Math.sign(b);
  232. return isNaN(d) ? x : x + d - 0.5;
  233. }
  234. function defaultX(d) {
  235. return d[0];
  236. }
  237. function defaultY(d) {
  238. return d[1];
  239. }
  240. function defaultWeight() {
  241. return 1;
  242. }
  243. function density() {
  244. var x = defaultX,
  245. y = defaultY,
  246. weight = defaultWeight,
  247. dx = 960,
  248. dy = 500,
  249. r = 20, // blur radius
  250. k = 2, // log2(grid cell size)
  251. o = r * 3, // grid offset, to pad for blur
  252. n = (dx + o * 2) >> k, // grid width
  253. m = (dy + o * 2) >> k, // grid height
  254. threshold = constant(20);
  255. function grid(data) {
  256. var values = new Float32Array(n * m),
  257. pow2k = Math.pow(2, -k),
  258. i = -1;
  259. for (const d of data) {
  260. var xi = (x(d, ++i, data) + o) * pow2k,
  261. yi = (y(d, i, data) + o) * pow2k,
  262. wi = +weight(d, i, data);
  263. if (wi && xi >= 0 && xi < n && yi >= 0 && yi < m) {
  264. var x0 = Math.floor(xi),
  265. y0 = Math.floor(yi),
  266. xt = xi - x0 - 0.5,
  267. yt = yi - y0 - 0.5;
  268. values[x0 + y0 * n] += (1 - xt) * (1 - yt) * wi;
  269. values[x0 + 1 + y0 * n] += xt * (1 - yt) * wi;
  270. values[x0 + 1 + (y0 + 1) * n] += xt * yt * wi;
  271. values[x0 + (y0 + 1) * n] += (1 - xt) * yt * wi;
  272. }
  273. }
  274. d3Array.blur2({data: values, width: n, height: m}, r * pow2k);
  275. return values;
  276. }
  277. function density(data) {
  278. var values = grid(data),
  279. tz = threshold(values),
  280. pow4k = Math.pow(2, 2 * k);
  281. // Convert number of thresholds into uniform thresholds.
  282. if (!Array.isArray(tz)) {
  283. tz = d3Array.ticks(Number.MIN_VALUE, d3Array.max(values) / pow4k, tz);
  284. }
  285. return Contours()
  286. .size([n, m])
  287. .thresholds(tz.map(d => d * pow4k))
  288. (values)
  289. .map((c, i) => (c.value = +tz[i], transform(c)));
  290. }
  291. density.contours = function(data) {
  292. var values = grid(data),
  293. contours = Contours().size([n, m]),
  294. pow4k = Math.pow(2, 2 * k),
  295. contour = value => {
  296. value = +value;
  297. var c = transform(contours.contour(values, value * pow4k));
  298. c.value = value; // preserve exact threshold value
  299. return c;
  300. };
  301. Object.defineProperty(contour, "max", {get: () => d3Array.max(values) / pow4k});
  302. return contour;
  303. };
  304. function transform(geometry) {
  305. geometry.coordinates.forEach(transformPolygon);
  306. return geometry;
  307. }
  308. function transformPolygon(coordinates) {
  309. coordinates.forEach(transformRing);
  310. }
  311. function transformRing(coordinates) {
  312. coordinates.forEach(transformPoint);
  313. }
  314. // TODO Optimize.
  315. function transformPoint(coordinates) {
  316. coordinates[0] = coordinates[0] * Math.pow(2, k) - o;
  317. coordinates[1] = coordinates[1] * Math.pow(2, k) - o;
  318. }
  319. function resize() {
  320. o = r * 3;
  321. n = (dx + o * 2) >> k;
  322. m = (dy + o * 2) >> k;
  323. return density;
  324. }
  325. density.x = function(_) {
  326. return arguments.length ? (x = typeof _ === "function" ? _ : constant(+_), density) : x;
  327. };
  328. density.y = function(_) {
  329. return arguments.length ? (y = typeof _ === "function" ? _ : constant(+_), density) : y;
  330. };
  331. density.weight = function(_) {
  332. return arguments.length ? (weight = typeof _ === "function" ? _ : constant(+_), density) : weight;
  333. };
  334. density.size = function(_) {
  335. if (!arguments.length) return [dx, dy];
  336. var _0 = +_[0], _1 = +_[1];
  337. if (!(_0 >= 0 && _1 >= 0)) throw new Error("invalid size");
  338. return dx = _0, dy = _1, resize();
  339. };
  340. density.cellSize = function(_) {
  341. if (!arguments.length) return 1 << k;
  342. if (!((_ = +_) >= 1)) throw new Error("invalid cell size");
  343. return k = Math.floor(Math.log(_) / Math.LN2), resize();
  344. };
  345. density.thresholds = function(_) {
  346. return arguments.length ? (threshold = typeof _ === "function" ? _ : Array.isArray(_) ? constant(slice.call(_)) : constant(_), density) : threshold;
  347. };
  348. density.bandwidth = function(_) {
  349. if (!arguments.length) return Math.sqrt(r * (r + 1));
  350. if (!((_ = +_) >= 0)) throw new Error("invalid bandwidth");
  351. return r = (Math.sqrt(4 * _ * _ + 1) - 1) / 2, resize();
  352. };
  353. return density;
  354. }
  355. exports.contourDensity = density;
  356. exports.contours = Contours;
  357. }));