contours.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import {extent, thresholdSturges, tickStep, range} from "d3-array";
  2. import {slice} from "./array";
  3. import ascending from "./ascending";
  4. import area from "./area";
  5. import constant from "./constant";
  6. import contains from "./contains";
  7. import noop from "./noop";
  8. var cases = [
  9. [],
  10. [[[1.0, 1.5], [0.5, 1.0]]],
  11. [[[1.5, 1.0], [1.0, 1.5]]],
  12. [[[1.5, 1.0], [0.5, 1.0]]],
  13. [[[1.0, 0.5], [1.5, 1.0]]],
  14. [[[1.0, 1.5], [0.5, 1.0]], [[1.0, 0.5], [1.5, 1.0]]],
  15. [[[1.0, 0.5], [1.0, 1.5]]],
  16. [[[1.0, 0.5], [0.5, 1.0]]],
  17. [[[0.5, 1.0], [1.0, 0.5]]],
  18. [[[1.0, 1.5], [1.0, 0.5]]],
  19. [[[0.5, 1.0], [1.0, 0.5]], [[1.5, 1.0], [1.0, 1.5]]],
  20. [[[1.5, 1.0], [1.0, 0.5]]],
  21. [[[0.5, 1.0], [1.5, 1.0]]],
  22. [[[1.0, 1.5], [1.5, 1.0]]],
  23. [[[0.5, 1.0], [1.0, 1.5]]],
  24. []
  25. ];
  26. export default function() {
  27. var dx = 1,
  28. dy = 1,
  29. threshold = thresholdSturges,
  30. smooth = smoothLinear;
  31. function contours(values) {
  32. var tz = threshold(values);
  33. // Convert number of thresholds into uniform thresholds.
  34. if (!Array.isArray(tz)) {
  35. var domain = extent(values), start = domain[0], stop = domain[1];
  36. tz = tickStep(start, stop, tz);
  37. tz = range(Math.floor(start / tz) * tz, Math.floor(stop / tz) * tz, tz);
  38. } else {
  39. tz = tz.slice().sort(ascending);
  40. }
  41. return tz.map(function(value) {
  42. return contour(values, value);
  43. });
  44. }
  45. // Accumulate, smooth contour rings, assign holes to exterior rings.
  46. // Based on https://github.com/mbostock/shapefile/blob/v0.6.2/shp/polygon.js
  47. function contour(values, value) {
  48. var polygons = [],
  49. holes = [];
  50. isorings(values, value, function(ring) {
  51. smooth(ring, values, value);
  52. if (area(ring) > 0) polygons.push([ring]);
  53. else holes.push(ring);
  54. });
  55. holes.forEach(function(hole) {
  56. for (var i = 0, n = polygons.length, polygon; i < n; ++i) {
  57. if (contains((polygon = polygons[i])[0], hole) !== -1) {
  58. polygon.push(hole);
  59. return;
  60. }
  61. }
  62. });
  63. return {
  64. type: "MultiPolygon",
  65. value: value,
  66. coordinates: polygons
  67. };
  68. }
  69. // Marching squares with isolines stitched into rings.
  70. // Based on https://github.com/topojson/topojson-client/blob/v3.0.0/src/stitch.js
  71. function isorings(values, value, callback) {
  72. var fragmentByStart = new Array,
  73. fragmentByEnd = new Array,
  74. x, y, t0, t1, t2, t3;
  75. // Special case for the first row (y = -1, t2 = t3 = 0).
  76. x = y = -1;
  77. t1 = values[0] >= value;
  78. cases[t1 << 1].forEach(stitch);
  79. while (++x < dx - 1) {
  80. t0 = t1, t1 = values[x + 1] >= value;
  81. cases[t0 | t1 << 1].forEach(stitch);
  82. }
  83. cases[t1 << 0].forEach(stitch);
  84. // General case for the intermediate rows.
  85. while (++y < dy - 1) {
  86. x = -1;
  87. t1 = values[y * dx + dx] >= value;
  88. t2 = values[y * dx] >= value;
  89. cases[t1 << 1 | t2 << 2].forEach(stitch);
  90. while (++x < dx - 1) {
  91. t0 = t1, t1 = values[y * dx + dx + x + 1] >= value;
  92. t3 = t2, t2 = values[y * dx + x + 1] >= value;
  93. cases[t0 | t1 << 1 | t2 << 2 | t3 << 3].forEach(stitch);
  94. }
  95. cases[t1 | t2 << 3].forEach(stitch);
  96. }
  97. // Special case for the last row (y = dy - 1, t0 = t1 = 0).
  98. x = -1;
  99. t2 = values[y * dx] >= value;
  100. cases[t2 << 2].forEach(stitch);
  101. while (++x < dx - 1) {
  102. t3 = t2, t2 = values[y * dx + x + 1] >= value;
  103. cases[t2 << 2 | t3 << 3].forEach(stitch);
  104. }
  105. cases[t2 << 3].forEach(stitch);
  106. function stitch(line) {
  107. var start = [line[0][0] + x, line[0][1] + y],
  108. end = [line[1][0] + x, line[1][1] + y],
  109. startIndex = index(start),
  110. endIndex = index(end),
  111. f, g;
  112. if (f = fragmentByEnd[startIndex]) {
  113. if (g = fragmentByStart[endIndex]) {
  114. delete fragmentByEnd[f.end];
  115. delete fragmentByStart[g.start];
  116. if (f === g) {
  117. f.ring.push(end);
  118. callback(f.ring);
  119. } else {
  120. fragmentByStart[f.start] = fragmentByEnd[g.end] = {start: f.start, end: g.end, ring: f.ring.concat(g.ring)};
  121. }
  122. } else {
  123. delete fragmentByEnd[f.end];
  124. f.ring.push(end);
  125. fragmentByEnd[f.end = endIndex] = f;
  126. }
  127. } else if (f = fragmentByStart[endIndex]) {
  128. if (g = fragmentByEnd[startIndex]) {
  129. delete fragmentByStart[f.start];
  130. delete fragmentByEnd[g.end];
  131. if (f === g) {
  132. f.ring.push(end);
  133. callback(f.ring);
  134. } else {
  135. fragmentByStart[g.start] = fragmentByEnd[f.end] = {start: g.start, end: f.end, ring: g.ring.concat(f.ring)};
  136. }
  137. } else {
  138. delete fragmentByStart[f.start];
  139. f.ring.unshift(start);
  140. fragmentByStart[f.start = startIndex] = f;
  141. }
  142. } else {
  143. fragmentByStart[startIndex] = fragmentByEnd[endIndex] = {start: startIndex, end: endIndex, ring: [start, end]};
  144. }
  145. }
  146. }
  147. function index(point) {
  148. return point[0] * 2 + point[1] * (dx + 1) * 4;
  149. }
  150. function smoothLinear(ring, values, value) {
  151. ring.forEach(function(point) {
  152. var x = point[0],
  153. y = point[1],
  154. xt = x | 0,
  155. yt = y | 0,
  156. v0,
  157. v1 = values[yt * dx + xt];
  158. if (x > 0 && x < dx && xt === x) {
  159. v0 = values[yt * dx + xt - 1];
  160. point[0] = x + (value - v0) / (v1 - v0) - 0.5;
  161. }
  162. if (y > 0 && y < dy && yt === y) {
  163. v0 = values[(yt - 1) * dx + xt];
  164. point[1] = y + (value - v0) / (v1 - v0) - 0.5;
  165. }
  166. });
  167. }
  168. contours.contour = contour;
  169. contours.size = function(_) {
  170. if (!arguments.length) return [dx, dy];
  171. var _0 = Math.ceil(_[0]), _1 = Math.ceil(_[1]);
  172. if (!(_0 > 0) || !(_1 > 0)) throw new Error("invalid size");
  173. return dx = _0, dy = _1, contours;
  174. };
  175. contours.thresholds = function(_) {
  176. return arguments.length ? (threshold = typeof _ === "function" ? _ : Array.isArray(_) ? constant(slice.call(_)) : constant(_), contours) : threshold;
  177. };
  178. contours.smooth = function(_) {
  179. return arguments.length ? (smooth = _ ? smoothLinear : noop, contours) : smooth === smoothLinear;
  180. };
  181. return contours;
  182. }