marchingsquares-isolines.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /*!
  2. * MarchingSquaresJS
  3. * version 1.3.3
  4. * https://github.com/RaumZeit/MarchingSquares.js
  5. *
  6. * @license GNU Affero General Public License.
  7. * Copyright (c) 2015-2019 Ronny Lorenz <ronny@tbi.univie.ac.at>
  8. */
  9. (function (global, factory) {
  10. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  11. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  12. (factory((global.MarchingSquaresJS = global.MarchingSquaresJS || {})));
  13. }(this, (function (exports) { 'use strict';
  14. /*
  15. * Compute the distance of a value 'v' from 'a' through linear interpolation
  16. * between the values of 'a' and 'b'
  17. *
  18. * Note, that we assume that 'a' and 'b' have unit distance (i.e. 1)
  19. */
  20. function linear(a, b, v) {
  21. if (a < b)
  22. return (v - a) / (b - a);
  23. return (a - v) / (a - b);
  24. }
  25. function Options() {
  26. /* Settings common to all implemented algorithms */
  27. this.successCallback = null;
  28. this.verbose = false;
  29. this.polygons = false;
  30. this.polygons_full = false;
  31. this.linearRing = true;
  32. this.noQuadTree = false;
  33. this.noFrame = false;
  34. }
  35. /* Compose settings specific to IsoLines algorithm */
  36. function isoLineOptions(userSettings) {
  37. var i,
  38. key,
  39. val,
  40. lineOptions,
  41. optionKeys;
  42. lineOptions = new Options();
  43. userSettings = userSettings ? userSettings : {};
  44. optionKeys = Object.keys(lineOptions);
  45. for(i = 0; i < optionKeys.length; i++) {
  46. key = optionKeys[i];
  47. val = userSettings[key];
  48. if ((typeof val !== 'undefined') && (val !== null))
  49. lineOptions[key] = val;
  50. }
  51. /* restore compatibility */
  52. lineOptions.polygons_full = !lineOptions.polygons;
  53. /* add interpolation functions (not yet user customizable) */
  54. lineOptions.interpolate = linear;
  55. return lineOptions;
  56. }
  57. function cell2Polygons(cell, x, y, settings) {
  58. var polygons = [];
  59. cell.polygons.forEach(function(p) {
  60. p.forEach(function(pp) {
  61. pp[0] += x;
  62. pp[1] += y;
  63. });
  64. if (settings.linearRing)
  65. p.push(p[0]);
  66. polygons.push(p);
  67. });
  68. return polygons;
  69. }
  70. function entry_coordinate(x, y, mode, path) {
  71. if (mode === 0) { /* down */
  72. x += 1;
  73. y += path[0][1];
  74. } else if (mode === 1) { /* left */
  75. x += path[0][0];
  76. } else if (mode === 2) { /* up */
  77. y += path[0][1];
  78. } else if (mode === 3) { /* right */
  79. x += path[0][0];
  80. y += 1;
  81. }
  82. return [ x, y ];
  83. }
  84. function skip_coordinate(x, y, mode) {
  85. if (mode === 0) { /* down */
  86. x++;
  87. } else if (mode === 1) ; else if (mode === 2) { /* up */
  88. y++;
  89. } else if (mode === 3) { /* right */
  90. x++;
  91. y++;
  92. }
  93. return [ x, y ];
  94. }
  95. function requireLineFrame(data, threshold) {
  96. var frameRequired,
  97. cols,
  98. rows,
  99. i,
  100. j;
  101. frameRequired = true;
  102. cols = data[0].length;
  103. rows = data.length;
  104. for (j = 0; j < rows; j++) {
  105. if ((data[j][0] >= threshold) ||
  106. (data[j][cols - 1] >= threshold)) {
  107. frameRequired = false;
  108. break;
  109. }
  110. }
  111. if ((frameRequired) &&
  112. ((data[rows - 1][0] >= threshold) ||
  113. (data[rows - 1][cols - 1] >= threshold))) {
  114. frameRequired = false;
  115. }
  116. if (frameRequired)
  117. for (i = 0; i < cols - 1; i++) {
  118. if ((data[0][i] >= threshold) ||
  119. (data[rows - 1][i] > threshold)) {
  120. frameRequired = false;
  121. break;
  122. }
  123. }
  124. return frameRequired;
  125. }
  126. function traceLinePaths(data, cellGrid, settings) {
  127. var nextedge,
  128. e,
  129. ee,
  130. cc,
  131. path,
  132. enter,
  133. x,
  134. y,
  135. finalized,
  136. origin,
  137. point,
  138. dir,
  139. count,
  140. found_entry,
  141. ve;
  142. var polygons = [];
  143. var rows = data.length - 1;
  144. var cols = data[0].length - 1;
  145. /*
  146. * directions for out-of-grid moves are:
  147. * 0 ... "down",
  148. * 1 ... "left",
  149. * 2 ... "up",
  150. * 3 ... "right"
  151. */
  152. var valid_entries = [ 'right', /* down */
  153. 'bottom', /* left */
  154. 'left', /* up */
  155. 'top' /* right */
  156. ];
  157. var add_x = [ 0, -1, 0, 1 ];
  158. var add_y = [ -1, 0, 1, 0 ];
  159. var entry_dir = {
  160. bottom: 1,
  161. left: 2,
  162. top: 3,
  163. right: 0
  164. };
  165. /* first, detect whether we need any outer frame */
  166. if (!settings.noFrame)
  167. if (requireLineFrame(data, settings.threshold)) {
  168. if (settings.linearRing)
  169. polygons.push([ [0, 0], [0, rows], [cols, rows], [cols, 0], [0, 0] ]);
  170. else
  171. polygons.push([ [0, 0], [0, rows], [cols, rows], [cols, 0] ]);
  172. }
  173. /* finally, start tracing back first polygon(s) */
  174. cellGrid.forEach(function(a, i) {
  175. a.forEach(function(cell, j) {
  176. nextedge = null;
  177. /* trace paths for all available edges that go through this cell */
  178. for (e = 0; e < 4; e++) {
  179. nextedge = valid_entries[e];
  180. if (typeof cell.edges[nextedge] !== 'object')
  181. continue;
  182. /* start a new, full path */
  183. path = [];
  184. ee = cell.edges[nextedge];
  185. enter = nextedge;
  186. x = i;
  187. y = j;
  188. finalized = false;
  189. origin = [ i + ee.path[0][0], j + ee.path[0][1] ];
  190. /* add start coordinate */
  191. path.push(origin);
  192. /* start traceback */
  193. while (!finalized) {
  194. cc = cellGrid[x][y];
  195. if (typeof cc.edges[enter] !== 'object')
  196. break;
  197. ee = cc.edges[enter];
  198. /* remove edge from cell */
  199. delete cc.edges[enter];
  200. /* add last point of edge to path arra, since we extend a polygon */
  201. point = ee.path[1];
  202. point[0] += x;
  203. point[1] += y;
  204. path.push(point);
  205. enter = ee.move.enter;
  206. x = x + ee.move.x;
  207. y = y + ee.move.y;
  208. /* handle out-of-grid moves */
  209. if ((typeof cellGrid[x] === 'undefined') ||
  210. (typeof cellGrid[x][y] === 'undefined')) {
  211. if (!settings.linearRing)
  212. break;
  213. dir = 0;
  214. count = 0;
  215. if (x === cols) {
  216. x--;
  217. dir = 0; /* move downwards */
  218. } else if (x < 0) {
  219. x++;
  220. dir = 2; /* move upwards */
  221. } else if (y === rows) {
  222. y--;
  223. dir = 3; /* move right */
  224. } else if (y < 0) {
  225. y++;
  226. dir = 1; /* move left */
  227. }
  228. if ((x === i) && (y === j) && (dir === entry_dir[nextedge])) {
  229. finalized = true;
  230. enter = nextedge;
  231. break;
  232. }
  233. while (1) {
  234. found_entry = false;
  235. if (count > 4)
  236. throw new Error('Direction change counter overflow! This should never happen!');
  237. if (!((typeof cellGrid[x] === 'undefined') ||
  238. (typeof cellGrid[x][y] === 'undefined'))) {
  239. cc = cellGrid[x][y];
  240. /* check for re-entry */
  241. ve = valid_entries[dir];
  242. if (typeof cc.edges[ve] === 'object') {
  243. /* found re-entry */
  244. ee = cc.edges[ve];
  245. path.push(entry_coordinate(x, y, dir, ee.path));
  246. enter = ve;
  247. found_entry = true;
  248. break;
  249. }
  250. }
  251. if (found_entry) {
  252. break;
  253. } else {
  254. path.push(skip_coordinate(x, y, dir));
  255. x += add_x[dir];
  256. y += add_y[dir];
  257. /* change direction if we'e moved out of grid again */
  258. if ((typeof cellGrid[x] === 'undefined') ||
  259. (typeof cellGrid[x][y] === 'undefined')) {
  260. if (((dir === 0) && (y < 0)) ||
  261. ((dir === 1) && (x < 0)) ||
  262. ((dir === 2) && (y === rows)) ||
  263. ((dir === 3) && (x === cols))) {
  264. x -= add_x[dir];
  265. y -= add_y[dir];
  266. dir = (dir + 1) % 4;
  267. count++;
  268. }
  269. }
  270. if ((x === i) && (y === j) && (dir === entry_dir[nextedge])) {
  271. /* we are back where we started off, so finalize the polygon */
  272. finalized = true;
  273. enter = nextedge;
  274. break;
  275. }
  276. }
  277. }
  278. }
  279. }
  280. if ((settings.linearRing) &&
  281. ((path[path.length - 1][0] !== origin[0]) ||
  282. (path[path.length - 1][1] !== origin[1])))
  283. path.push(origin);
  284. polygons.push(path);
  285. } /* end forall entry sites */
  286. }); /* end foreach i */
  287. }); /* end foreach j */
  288. return polygons;
  289. }
  290. /* quadTree node constructor */
  291. function TreeNode(data, x, y, dx, dy) {
  292. var dx_tmp = dx,
  293. dy_tmp = dy,
  294. msb_x = 0,
  295. msb_y = 0;
  296. /* left-bottom corner of current quadrant */
  297. this.x = x;
  298. this.y = y;
  299. /* minimum value in subtree under this node */
  300. this.lowerBound = null;
  301. /* maximum value in subtree under this node */
  302. this.upperBound = null;
  303. /*
  304. * child nodes are layed out in the following way:
  305. *
  306. * (x, y + 1) ---- (x + 1, y + 1)
  307. * | | |
  308. * | D | C |
  309. * | | |
  310. * |----------------------------|
  311. * | | |
  312. * | A | B |
  313. * | | |
  314. * (x, y) ------------ (x + 1, y)
  315. */
  316. this.childA = null;
  317. this.childB = null;
  318. this.childC = null;
  319. this.childD = null;
  320. if ((dx === 1) && (dy === 1)) {
  321. /* do not further subdivision */
  322. this.lowerBound = Math.min(
  323. data[y][x],
  324. data[y][x + 1],
  325. data[y + 1][x + 1],
  326. data[y + 1][x]
  327. );
  328. this.upperBound = Math.max(
  329. data[y][x],
  330. data[y][x + 1],
  331. data[y + 1][x + 1],
  332. data[y + 1][x]
  333. );
  334. } else {
  335. /* get most significant bit from dx */
  336. if (dx > 1) {
  337. while (dx_tmp !== 0) {
  338. dx_tmp = dx_tmp >> 1;
  339. msb_x++;
  340. }
  341. if (dx === (1 << (msb_x - 1)))
  342. msb_x--;
  343. dx_tmp = 1 << (msb_x - 1);
  344. }
  345. /* get most significant bit from dx */
  346. if (dy > 1) {
  347. while (dy_tmp !== 0) {
  348. dy_tmp = dy_tmp >> 1;
  349. msb_y++;
  350. }
  351. if (dy === (1 << (msb_y - 1)))
  352. msb_y--;
  353. dy_tmp = 1 << (msb_y - 1);
  354. }
  355. this.childA = new TreeNode(data, x, y, dx_tmp, dy_tmp);
  356. this.lowerBound = this.childA.lowerBound;
  357. this.upperBound = this.childA.upperBound;
  358. if (dx - dx_tmp > 0) {
  359. this.childB = new TreeNode(data, x + dx_tmp, y, dx - dx_tmp, dy_tmp);
  360. this.lowerBound = Math.min(this.lowerBound, this.childB.lowerBound);
  361. this.upperBound = Math.max(this.upperBound, this.childB.upperBound);
  362. if (dy - dy_tmp > 0) {
  363. this.childC = new TreeNode(data, x + dx_tmp, y + dy_tmp, dx - dx_tmp, dy - dy_tmp);
  364. this.lowerBound = Math.min(this.lowerBound, this.childC.lowerBound);
  365. this.upperBound = Math.max(this.upperBound, this.childC.upperBound);
  366. }
  367. }
  368. if (dy - dy_tmp > 0) {
  369. this.childD = new TreeNode(data, x, y + dy_tmp, dx_tmp, dy - dy_tmp);
  370. this.lowerBound = Math.min(this.lowerBound, this.childD.lowerBound);
  371. this.upperBound = Math.max(this.upperBound, this.childD.upperBound);
  372. }
  373. }
  374. }
  375. /**
  376. * Retrieve a list of cells within a particular range of values by
  377. * recursivly traversing the quad tree to it's leaves.
  378. *
  379. * @param subsumed If 'true' include all cells that are completely
  380. * subsumed within the specified range. Otherwise,
  381. * return only cells where at least one corner is
  382. * outside the specified range.
  383. *
  384. * @return An array of objects 'o' where each object has exactly two
  385. * properties: 'o.x' and 'o.y' denoting the left-bottom corner
  386. * of the corresponding cell.
  387. */
  388. TreeNode.prototype.cellsInBand = function(lowerBound, upperBound, subsumed) {
  389. var cells = [];
  390. subsumed = (typeof subsumed === 'undefined') ? true : subsumed;
  391. if ((this.lowerBound > upperBound) || (this.upperBound < lowerBound))
  392. return cells;
  393. if (!(this.childA || this.childB || this.childC || this.childD)) {
  394. if ((subsumed) ||
  395. (this.lowerBound <= lowerBound) ||
  396. (this.upperBound >= upperBound)) {
  397. cells.push({
  398. x: this.x,
  399. y: this.y
  400. });
  401. }
  402. } else {
  403. if (this.childA)
  404. cells = cells.concat(this.childA.cellsInBand(lowerBound, upperBound, subsumed));
  405. if (this.childB)
  406. cells = cells.concat(this.childB.cellsInBand(lowerBound, upperBound, subsumed));
  407. if (this.childD)
  408. cells = cells.concat(this.childD.cellsInBand(lowerBound, upperBound, subsumed));
  409. if (this.childC)
  410. cells = cells.concat(this.childC.cellsInBand(lowerBound, upperBound, subsumed));
  411. }
  412. return cells;
  413. };
  414. TreeNode.prototype.cellsBelowThreshold = function(threshold, subsumed) {
  415. var cells = [];
  416. subsumed = (typeof subsumed === 'undefined') ? true : subsumed;
  417. if (this.lowerBound > threshold)
  418. return cells;
  419. if (!(this.childA || this.childB || this.childC || this.childD)) {
  420. if ((subsumed) ||
  421. (this.upperBound >= threshold)) {
  422. cells.push({
  423. x: this.x,
  424. y: this.y
  425. });
  426. }
  427. } else {
  428. if (this.childA)
  429. cells = cells.concat(this.childA.cellsBelowThreshold(threshold, subsumed));
  430. if (this.childB)
  431. cells = cells.concat(this.childB.cellsBelowThreshold(threshold, subsumed));
  432. if (this.childD)
  433. cells = cells.concat(this.childD.cellsBelowThreshold(threshold, subsumed));
  434. if (this.childC)
  435. cells = cells.concat(this.childC.cellsBelowThreshold(threshold, subsumed));
  436. }
  437. return cells;
  438. };
  439. /*
  440. * Given a scalar field `data` construct a QuadTree
  441. * to efficiently lookup those parts of the scalar
  442. * field where values are within a particular
  443. * range of [lowerbound, upperbound] limits.
  444. */
  445. function QuadTree(data) {
  446. var i, cols;
  447. /* do some input checking */
  448. if (!data)
  449. throw new Error('data is required');
  450. if (!Array.isArray(data) ||
  451. !Array.isArray(data[0]))
  452. throw new Error('data must be scalar field, i.e. array of arrays');
  453. if (data.length < 2)
  454. throw new Error('data must contain at least two rows');
  455. /* check if we've got a regular grid */
  456. cols = data[0].length;
  457. if (cols < 2)
  458. throw new Error('data must contain at least two columns');
  459. for (i = 1; i < data.length; i++) {
  460. if (!Array.isArray(data[i]))
  461. throw new Error('Row ' + i + ' is not an array');
  462. if (data[i].length != cols)
  463. throw new Error('unequal row lengths detected, please provide a regular grid');
  464. }
  465. /* create pre-processing object */
  466. this.data = data;
  467. /* root node, i.e. entry to the data */
  468. this.root = new TreeNode(data, 0, 0, data[0].length - 1, data.length - 1);
  469. }
  470. /* eslint no-console: ["error", { allow: ["log"] }] */
  471. /*
  472. * Compute the iso lines for a scalar 2D field given
  473. * a certain threshold by applying the Marching Squares
  474. * Algorithm. The function returns a list of path coordinates
  475. */
  476. function isoLines(input, threshold, options) {
  477. var settings,
  478. i,
  479. j,
  480. useQuadTree = false,
  481. multiLine = false,
  482. tree = null,
  483. root = null,
  484. data = null,
  485. cellGrid = null,
  486. linePolygons = null,
  487. ret = [];
  488. /* validation */
  489. if (!input) throw new Error('data is required');
  490. if (threshold === undefined || threshold === null) throw new Error('threshold is required');
  491. if ((!!options) && (typeof options !== 'object')) throw new Error('options must be an object');
  492. /* process options */
  493. settings = isoLineOptions(options);
  494. /* check for input data */
  495. if (input instanceof QuadTree) {
  496. tree = input;
  497. root = input.root;
  498. data = input.data;
  499. if (!settings.noQuadTree)
  500. useQuadTree = true;
  501. } else if (Array.isArray(input) && Array.isArray(input[0])) {
  502. data = input;
  503. } else {
  504. throw new Error('input is neither array of arrays nor object retrieved from \'QuadTree()\'');
  505. }
  506. /* check and prepare input threshold(s) */
  507. if (Array.isArray(threshold)) {
  508. multiLine = true;
  509. /* activate QuadTree optimization if not explicitly forbidden by user settings */
  510. if (!settings.noQuadTree)
  511. useQuadTree = true;
  512. /* check if all minV are numbers */
  513. for (i = 0; i < threshold.length; i++)
  514. if (isNaN(+threshold[i]))
  515. throw new Error('threshold[' + i + '] is not a number');
  516. } else {
  517. if (isNaN(+threshold))
  518. throw new Error('threshold must be a number or array of numbers');
  519. threshold = [ threshold ];
  520. }
  521. /* create QuadTree root node if not already present */
  522. if ((useQuadTree) && (!root)) {
  523. tree = new QuadTree(data);
  524. root = tree.root;
  525. data = tree.data;
  526. }
  527. if (settings.verbose) {
  528. if(settings.polygons)
  529. console.log('MarchingSquaresJS-isoLines: returning single lines (polygons) for each grid cell');
  530. else
  531. console.log('MarchingSquaresJS-isoLines: returning line paths (polygons) for entire data grid');
  532. if (multiLine)
  533. console.log('MarchingSquaresJS-isoLines: multiple lines requested, returning array of line paths instead of lines for a single threshold');
  534. }
  535. /* Done with all input validation, now let's start computing stuff */
  536. /* loop over all threhsold values */
  537. threshold.forEach(function(t, i) {
  538. linePolygons = [];
  539. /* store bounds for current computation in settings object */
  540. settings.threshold = t;
  541. if(settings.verbose)
  542. console.log('MarchingSquaresJS-isoLines: computing iso lines for threshold ' + t);
  543. if (settings.polygons) {
  544. /* compose list of polygons for each single cell */
  545. if (useQuadTree) {
  546. /* go through list of cells retrieved from QuadTree */
  547. root
  548. .cellsBelowThreshold(settings.threshold, true)
  549. .forEach(function(c) {
  550. linePolygons = linePolygons.concat(
  551. cell2Polygons(
  552. prepareCell(data,
  553. c.x,
  554. c.y,
  555. settings),
  556. c.x,
  557. c.y,
  558. settings
  559. ));
  560. });
  561. } else {
  562. /* go through entire array of input data */
  563. for (j = 0; j < data.length - 1; ++j) {
  564. for (i = 0; i < data[0].length - 1; ++i)
  565. linePolygons = linePolygons.concat(
  566. cell2Polygons(
  567. prepareCell(data,
  568. i,
  569. j,
  570. settings),
  571. i,
  572. j,
  573. settings
  574. ));
  575. }
  576. }
  577. } else {
  578. /* sparse grid of input data cells */
  579. cellGrid = [];
  580. for (i = 0; i < data[0].length - 1; ++i)
  581. cellGrid[i] = [];
  582. /* compose list of polygons for entire input grid */
  583. if (useQuadTree) {
  584. /* collect the cells */
  585. root
  586. .cellsBelowThreshold(settings.threshold, false)
  587. .forEach(function(c) {
  588. cellGrid[c.x][c.y] = prepareCell(data,
  589. c.x,
  590. c.y,
  591. settings);
  592. });
  593. } else {
  594. /* prepare cells */
  595. for (i = 0; i < data[0].length - 1; ++i) {
  596. for (j = 0; j < data.length - 1; ++j) {
  597. cellGrid[i][j] = prepareCell(data,
  598. i,
  599. j,
  600. settings);
  601. }
  602. }
  603. }
  604. linePolygons = traceLinePaths(data, cellGrid, settings);
  605. }
  606. /* finally, add polygons to output array */
  607. if (multiLine)
  608. ret.push(linePolygons);
  609. else
  610. ret = linePolygons;
  611. if(typeof settings.successCallback === 'function')
  612. settings.successCallback(ret, t);
  613. });
  614. return ret;
  615. }
  616. /*
  617. * Thats all for the public interface, below follows the actual
  618. * implementation
  619. */
  620. /*
  621. * ################################
  622. * Isocontour implementation below
  623. * ################################
  624. */
  625. function prepareCell(grid, x, y, settings) {
  626. var left,
  627. right,
  628. top,
  629. bottom,
  630. average,
  631. cell;
  632. var cval = 0;
  633. var x3 = grid[y + 1][x];
  634. var x2 = grid[y + 1][x + 1];
  635. var x1 = grid[y][x + 1];
  636. var x0 = grid[y][x];
  637. var threshold = settings.threshold;
  638. /*
  639. * Note that missing data within the grid will result
  640. * in horribly failing to trace full polygon paths
  641. */
  642. if(isNaN(x0) || isNaN(x1) || isNaN(x2) || isNaN(x3)) {
  643. return;
  644. }
  645. /*
  646. * Here we detect the type of the cell
  647. *
  648. * x3 ---- x2
  649. * | |
  650. * | |
  651. * x0 ---- x1
  652. *
  653. * with edge points
  654. *
  655. * x0 = (x,y),
  656. * x1 = (x + 1, y),
  657. * x2 = (x + 1, y + 1), and
  658. * x3 = (x, y + 1)
  659. *
  660. * and compute the polygon intersections with the edges
  661. * of the cell. Each edge value may be (i) smaller, or (ii)
  662. * greater or equal to the iso line threshold. We encode
  663. * this property using 1 bit of information, where
  664. *
  665. * 0 ... below,
  666. * 1 ... above or equal
  667. *
  668. * Then we store the cells value as vector
  669. *
  670. * cval = (x0, x1, x2, x3)
  671. *
  672. * where x0 is the least significant bit (0th),
  673. * x1 the 2nd bit, and so on. This essentially
  674. * enables us to work with a single integer number
  675. */
  676. cval |= ((x3 >= threshold) ? 8 : 0);
  677. cval |= ((x2 >= threshold) ? 4 : 0);
  678. cval |= ((x1 >= threshold) ? 2 : 0);
  679. cval |= ((x0 >= threshold) ? 1 : 0);
  680. /* make sure cval is a number */
  681. cval = +cval;
  682. /* compose the cell object */
  683. cell = {
  684. cval: cval,
  685. polygons: [],
  686. edges: {},
  687. x0: x0,
  688. x1: x1,
  689. x2: x2,
  690. x3: x3
  691. };
  692. /*
  693. * Compute interpolated intersections of the polygon(s)
  694. * with the cell borders and (i) add edges for polygon
  695. * trace-back, or (ii) a list of small closed polygons
  696. */
  697. switch (cval) {
  698. case 0:
  699. if (settings.polygons)
  700. cell.polygons.push([ [0, 0], [0, 1], [1, 1], [1, 0] ]);
  701. break;
  702. case 15:
  703. /* cell is outside (above) threshold, no polygons */
  704. break;
  705. case 14: /* 1110 */
  706. left = settings.interpolate(x0, x3, threshold);
  707. bottom = settings.interpolate(x0, x1, threshold);
  708. if (settings.polygons_full) {
  709. cell.edges.left = {
  710. path: [ [0, left], [bottom, 0] ],
  711. move: {
  712. x: 0,
  713. y: -1,
  714. enter: 'top'
  715. }
  716. };
  717. }
  718. if (settings.polygons)
  719. cell.polygons.push([ [0, 0], [0, left], [bottom, 0] ]);
  720. break;
  721. case 13: /* 1101 */
  722. bottom = settings.interpolate(x0, x1, threshold);
  723. right = settings.interpolate(x1, x2, threshold);
  724. if (settings.polygons_full) {
  725. cell.edges.bottom = {
  726. path: [ [bottom, 0], [1, right] ],
  727. move: {
  728. x: 1,
  729. y: 0,
  730. enter: 'left'
  731. }
  732. };
  733. }
  734. if (settings.polygons)
  735. cell.polygons.push([ [bottom, 0], [1, right], [1, 0] ]);
  736. break;
  737. case 11: /* 1011 */
  738. right = settings.interpolate(x1, x2, threshold);
  739. top = settings.interpolate(x3, x2, threshold);
  740. if (settings.polygons_full) {
  741. cell.edges.right = {
  742. path: [ [1, right], [top, 1] ],
  743. move: {
  744. x: 0,
  745. y: 1,
  746. enter: 'bottom'
  747. }
  748. };
  749. }
  750. if (settings.polygons)
  751. cell.polygons.push([ [1, right], [top, 1], [1, 1] ]);
  752. break;
  753. case 7: /* 0111 */
  754. left = settings.interpolate(x0, x3, threshold);
  755. top = settings.interpolate(x3, x2, threshold);
  756. if (settings.polygons_full) {
  757. cell.edges.top = {
  758. path: [ [top, 1], [0, left] ],
  759. move: {
  760. x: -1,
  761. y: 0,
  762. enter: 'right'
  763. }
  764. };
  765. }
  766. if (settings.polygons)
  767. cell.polygons.push([ [top, 1], [0, left], [0, 1] ]);
  768. break;
  769. case 1: /* 0001 */
  770. left = settings.interpolate(x0, x3, threshold);
  771. bottom = settings.interpolate(x0, x1, threshold);
  772. if (settings.polygons_full) {
  773. cell.edges.bottom = {
  774. path: [ [bottom, 0], [0, left] ],
  775. move: {
  776. x: -1,
  777. y: 0,
  778. enter: 'right'
  779. }
  780. };
  781. }
  782. if (settings.polygons)
  783. cell.polygons.push([ [bottom, 0], [0, left], [0, 1], [1, 1], [1, 0] ]);
  784. break;
  785. case 2: /* 0010 */
  786. bottom = settings.interpolate(x0, x1, threshold);
  787. right = settings.interpolate(x1, x2, threshold);
  788. if (settings.polygons_full) {
  789. cell.edges.right = {
  790. path: [ [1, right], [bottom, 0] ],
  791. move: {
  792. x: 0,
  793. y: -1,
  794. enter: 'top'
  795. }
  796. };
  797. }
  798. if (settings.polygons)
  799. cell.polygons.push([ [0, 0], [0, 1], [1, 1], [1, right], [bottom, 0] ]);
  800. break;
  801. case 4: /* 0100 */
  802. right = settings.interpolate(x1, x2, threshold);
  803. top = settings.interpolate(x3, x2, threshold);
  804. if (settings.polygons_full) {
  805. cell.edges.top = {
  806. path: [ [top, 1], [1, right] ],
  807. move: {
  808. x: 1,
  809. y: 0,
  810. enter: 'left'
  811. }
  812. };
  813. }
  814. if (settings.polygons)
  815. cell.polygons.push([ [0, 0], [0, 1], [top, 1], [1, right], [1, 0] ]);
  816. break;
  817. case 8: /* 1000 */
  818. left = settings.interpolate(x0, x3, threshold);
  819. top = settings.interpolate(x3, x2, threshold);
  820. if (settings.polygons_full) {
  821. cell.edges.left = {
  822. path: [ [0, left], [top, 1] ],
  823. move: {
  824. x: 0,
  825. y: 1,
  826. enter: 'bottom'
  827. }
  828. };
  829. }
  830. if (settings.polygons)
  831. cell.polygons.push([ [0, 0], [0, left], [top, 1], [1, 1], [1, 0] ]);
  832. break;
  833. case 12: /* 1100 */
  834. left = settings.interpolate(x0, x3, threshold);
  835. right = settings.interpolate(x1, x2, threshold);
  836. if (settings.polygons_full) {
  837. cell.edges.left = {
  838. path: [ [0, left], [1, right] ],
  839. move: {
  840. x: 1,
  841. y: 0,
  842. enter: 'left'
  843. }
  844. };
  845. }
  846. if (settings.polygons)
  847. cell.polygons.push([ [0, 0], [0, left], [1, right], [1, 0] ]);
  848. break;
  849. case 9: /* 1001 */
  850. bottom = settings.interpolate(x0, x1, threshold);
  851. top = settings.interpolate(x3, x2, threshold);
  852. if (settings.polygons_full) {
  853. cell.edges.bottom = {
  854. path: [ [bottom, 0], [top, 1] ],
  855. move: {
  856. x: 0,
  857. y: 1,
  858. enter: 'bottom'
  859. }
  860. };
  861. }
  862. if (settings.polygons)
  863. cell.polygons.push([ [bottom, 0], [top, 1], [1, 1], [1, 0] ]);
  864. break;
  865. case 3: /* 0011 */
  866. left = settings.interpolate(x0, x3, threshold);
  867. right = settings.interpolate(x1, x2, threshold);
  868. if (settings.polygons_full) {
  869. cell.edges.right = {
  870. path: [ [1, right], [0, left] ],
  871. move: {
  872. x: -1,
  873. y: 0,
  874. enter: 'right'
  875. }
  876. };
  877. }
  878. if (settings.polygons)
  879. cell.polygons.push([ [0, left], [0, 1], [1, 1], [1, right] ]);
  880. break;
  881. case 6: /* 0110 */
  882. bottom = settings.interpolate(x0, x1, threshold);
  883. top = settings.interpolate(x3, x2, threshold);
  884. if (settings.polygons_full) {
  885. cell.edges.top = {
  886. path: [ [top, 1], [bottom, 0] ],
  887. move: {
  888. x: 0,
  889. y: -1,
  890. enter: 'top'
  891. }
  892. };
  893. }
  894. if (settings.polygons)
  895. cell.polygons.push([ [0, 0], [0, 1], [top, 1], [bottom, 0] ]);
  896. break;
  897. case 10: /* 1010 */
  898. left = settings.interpolate(x0, x3, threshold);
  899. right = settings.interpolate(x1, x2, threshold);
  900. bottom = settings.interpolate(x0, x1, threshold);
  901. top = settings.interpolate(x3, x2, threshold);
  902. average = (x0 + x1 + x2 + x3) / 4;
  903. if (settings.polygons_full) {
  904. if (average < threshold) {
  905. cell.edges.left = {
  906. path: [ [0, left], [top, 1] ],
  907. move: {
  908. x: 0,
  909. y: 1,
  910. enter: 'bottom'
  911. }
  912. };
  913. cell.edges.right = {
  914. path: [ [1, right], [bottom, 0] ],
  915. move: {
  916. x: 0,
  917. y: -1,
  918. enter: 'top'
  919. }
  920. };
  921. } else {
  922. cell.edges.right = {
  923. path: [ [1, right], [top, 1] ],
  924. move: {
  925. x: 0,
  926. y: 1,
  927. enter: 'bottom'
  928. }
  929. };
  930. cell.edges.left = {
  931. path: [ [0, left], [bottom, 0] ],
  932. move: {
  933. x: 0,
  934. y: -1,
  935. enter: 'top'
  936. }
  937. };
  938. }
  939. }
  940. if (settings.polygons) {
  941. if (average < threshold) {
  942. cell.polygons.push([ [0, 0], [0, left], [top, 1], [1, 1], [1, right], [bottom, 0] ]);
  943. } else {
  944. cell.polygons.push([ [0, 0], [0, left], [bottom, 0] ]);
  945. cell.polygons.push([ [top, 1], [1, 1], [1, right] ]);
  946. }
  947. }
  948. break;
  949. case 5: /* 0101 */
  950. left = settings.interpolate(x0, x3, threshold);
  951. right = settings.interpolate(x1, x2, threshold);
  952. bottom = settings.interpolate(x0, x1, threshold);
  953. top = settings.interpolate(x3, x2, threshold);
  954. average = (x0 + x1 + x2 + x3) / 4;
  955. if (settings.polygons_full) {
  956. if (average < threshold) {
  957. cell.edges.bottom = {
  958. path: [ [bottom, 0], [0, left] ],
  959. move: {
  960. x: -1,
  961. y: 0,
  962. enter: 'right'
  963. }
  964. };
  965. cell.edges.top = {
  966. path: [ [top, 1], [1, right] ],
  967. move: {
  968. x: 1,
  969. y: 0,
  970. enter: 'left'
  971. }
  972. };
  973. } else {
  974. cell.edges.top = {
  975. path: [ [top, 1], [0, left] ],
  976. move: {
  977. x: -1,
  978. y: 0,
  979. enter: 'right'
  980. }
  981. };
  982. cell.edges.bottom = {
  983. path: [ [bottom, 0], [1, right] ],
  984. move: {
  985. x: 1,
  986. y: 0,
  987. enter: 'left'
  988. }
  989. };
  990. }
  991. }
  992. if (settings.polygons) {
  993. if (average < threshold) {
  994. cell.polygons.push([ [0, left], [0, 1], [top, 1], [1, right], [1, 0], [bottom, 0] ]);
  995. } else {
  996. cell.polygons.push([ [0, left], [0, 1], [top, 1] ]);
  997. cell.polygons.push([ [bottom, 0], [1, right], [1, 0] ]);
  998. }
  999. }
  1000. break;
  1001. }
  1002. return cell;
  1003. }
  1004. exports.isoLines = isoLines;
  1005. exports.isoContours = isoLines;
  1006. Object.defineProperty(exports, '__esModule', { value: true });
  1007. })));