source: node_modules/d3-contour/src/density.js

Last change on this file was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[e4c61dd]1import {blur2, max, ticks} from "d3-array";
2import {slice} from "./array.js";
3import constant from "./constant.js";
4import Contours from "./contours.js";
5
6function defaultX(d) {
7 return d[0];
8}
9
10function defaultY(d) {
11 return d[1];
12}
13
14function defaultWeight() {
15 return 1;
16}
17
18export default function() {
19 var x = defaultX,
20 y = defaultY,
21 weight = defaultWeight,
22 dx = 960,
23 dy = 500,
24 r = 20, // blur radius
25 k = 2, // log2(grid cell size)
26 o = r * 3, // grid offset, to pad for blur
27 n = (dx + o * 2) >> k, // grid width
28 m = (dy + o * 2) >> k, // grid height
29 threshold = constant(20);
30
31 function grid(data) {
32 var values = new Float32Array(n * m),
33 pow2k = Math.pow(2, -k),
34 i = -1;
35
36 for (const d of data) {
37 var xi = (x(d, ++i, data) + o) * pow2k,
38 yi = (y(d, i, data) + o) * pow2k,
39 wi = +weight(d, i, data);
40 if (wi && xi >= 0 && xi < n && yi >= 0 && yi < m) {
41 var x0 = Math.floor(xi),
42 y0 = Math.floor(yi),
43 xt = xi - x0 - 0.5,
44 yt = yi - y0 - 0.5;
45 values[x0 + y0 * n] += (1 - xt) * (1 - yt) * wi;
46 values[x0 + 1 + y0 * n] += xt * (1 - yt) * wi;
47 values[x0 + 1 + (y0 + 1) * n] += xt * yt * wi;
48 values[x0 + (y0 + 1) * n] += (1 - xt) * yt * wi;
49 }
50 }
51
52 blur2({data: values, width: n, height: m}, r * pow2k);
53 return values;
54 }
55
56 function density(data) {
57 var values = grid(data),
58 tz = threshold(values),
59 pow4k = Math.pow(2, 2 * k);
60
61 // Convert number of thresholds into uniform thresholds.
62 if (!Array.isArray(tz)) {
63 tz = ticks(Number.MIN_VALUE, max(values) / pow4k, tz);
64 }
65
66 return Contours()
67 .size([n, m])
68 .thresholds(tz.map(d => d * pow4k))
69 (values)
70 .map((c, i) => (c.value = +tz[i], transform(c)));
71 }
72
73 density.contours = function(data) {
74 var values = grid(data),
75 contours = Contours().size([n, m]),
76 pow4k = Math.pow(2, 2 * k),
77 contour = value => {
78 value = +value;
79 var c = transform(contours.contour(values, value * pow4k));
80 c.value = value; // preserve exact threshold value
81 return c;
82 };
83 Object.defineProperty(contour, "max", {get: () => max(values) / pow4k});
84 return contour;
85 };
86
87 function transform(geometry) {
88 geometry.coordinates.forEach(transformPolygon);
89 return geometry;
90 }
91
92 function transformPolygon(coordinates) {
93 coordinates.forEach(transformRing);
94 }
95
96 function transformRing(coordinates) {
97 coordinates.forEach(transformPoint);
98 }
99
100 // TODO Optimize.
101 function transformPoint(coordinates) {
102 coordinates[0] = coordinates[0] * Math.pow(2, k) - o;
103 coordinates[1] = coordinates[1] * Math.pow(2, k) - o;
104 }
105
106 function resize() {
107 o = r * 3;
108 n = (dx + o * 2) >> k;
109 m = (dy + o * 2) >> k;
110 return density;
111 }
112
113 density.x = function(_) {
114 return arguments.length ? (x = typeof _ === "function" ? _ : constant(+_), density) : x;
115 };
116
117 density.y = function(_) {
118 return arguments.length ? (y = typeof _ === "function" ? _ : constant(+_), density) : y;
119 };
120
121 density.weight = function(_) {
122 return arguments.length ? (weight = typeof _ === "function" ? _ : constant(+_), density) : weight;
123 };
124
125 density.size = function(_) {
126 if (!arguments.length) return [dx, dy];
127 var _0 = +_[0], _1 = +_[1];
128 if (!(_0 >= 0 && _1 >= 0)) throw new Error("invalid size");
129 return dx = _0, dy = _1, resize();
130 };
131
132 density.cellSize = function(_) {
133 if (!arguments.length) return 1 << k;
134 if (!((_ = +_) >= 1)) throw new Error("invalid cell size");
135 return k = Math.floor(Math.log(_) / Math.LN2), resize();
136 };
137
138 density.thresholds = function(_) {
139 return arguments.length ? (threshold = typeof _ === "function" ? _ : Array.isArray(_) ? constant(slice.call(_)) : constant(_), density) : threshold;
140 };
141
142 density.bandwidth = function(_) {
143 if (!arguments.length) return Math.sqrt(r * (r + 1));
144 if (!((_ = +_) >= 0)) throw new Error("invalid bandwidth");
145 return r = (Math.sqrt(4 * _ * _ + 1) - 1) / 2, resize();
146 };
147
148 return density;
149}
Note: See TracBrowser for help on using the repository browser.