@turf/isolines 7.0.0-alpha.1 → 7.0.0-alpha.110

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/js/index.js DELETED
@@ -1,122 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- const bbox_1 = tslib_1.__importDefault(require("@turf/bbox"));
5
- const meta_1 = require("@turf/meta");
6
- const invariant_1 = require("@turf/invariant");
7
- const helpers_1 = require("@turf/helpers");
8
- const marchingsquares_isocontours_1 = tslib_1.__importDefault(require("./lib/marchingsquares-isocontours"));
9
- const grid_to_matrix_1 = tslib_1.__importDefault(require("./lib/grid-to-matrix"));
10
- /**
11
- * Takes a grid {@link FeatureCollection} of {@link Point} features with z-values and an array of
12
- * value breaks and generates [isolines](https://en.wikipedia.org/wiki/Contour_line).
13
- *
14
- * @name isolines
15
- * @param {FeatureCollection<Point>} pointGrid input points
16
- * @param {Array<number>} breaks values of `zProperty` where to draw isolines
17
- * @param {Object} [options={}] Optional parameters
18
- * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
19
- * @param {Object} [options.commonProperties={}] GeoJSON properties passed to ALL isolines
20
- * @param {Array<Object>} [options.breaksProperties=[]] GeoJSON properties passed, in order, to the correspondent isoline;
21
- * the breaks array will define the order in which the isolines are created
22
- * @returns {FeatureCollection<MultiLineString>} a FeatureCollection of {@link MultiLineString} features representing isolines
23
- * @example
24
- * // create a grid of points with random z-values in their properties
25
- * var extent = [0, 30, 20, 50];
26
- * var cellWidth = 100;
27
- * var pointGrid = turf.pointGrid(extent, cellWidth, {units: 'miles'});
28
- *
29
- * for (var i = 0; i < pointGrid.features.length; i++) {
30
- * pointGrid.features[i].properties.temperature = Math.random() * 10;
31
- * }
32
- * var breaks = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
33
- *
34
- * var lines = turf.isolines(pointGrid, breaks, {zProperty: 'temperature'});
35
- *
36
- * //addToMap
37
- * var addToMap = [lines];
38
- */
39
- function isolines(pointGrid, breaks, options) {
40
- // Optional parameters
41
- options = options || {};
42
- if (!helpers_1.isObject(options))
43
- throw new Error("options is invalid");
44
- const zProperty = options.zProperty || "elevation";
45
- const commonProperties = options.commonProperties || {};
46
- const breaksProperties = options.breaksProperties || [];
47
- // Input validation
48
- invariant_1.collectionOf(pointGrid, "Point", "Input must contain Points");
49
- if (!breaks)
50
- throw new Error("breaks is required");
51
- if (!Array.isArray(breaks))
52
- throw new Error("breaks must be an Array");
53
- if (!helpers_1.isObject(commonProperties))
54
- throw new Error("commonProperties must be an Object");
55
- if (!Array.isArray(breaksProperties))
56
- throw new Error("breaksProperties must be an Array");
57
- // Isoline methods
58
- const matrix = grid_to_matrix_1.default(pointGrid, { zProperty: zProperty, flip: true });
59
- const createdIsoLines = createIsoLines(matrix, breaks, zProperty, commonProperties, breaksProperties);
60
- const scaledIsolines = rescaleIsolines(createdIsoLines, matrix, pointGrid);
61
- return helpers_1.featureCollection(scaledIsolines);
62
- }
63
- /**
64
- * Creates the isolines lines (featuresCollection of MultiLineString features) from the 2D data grid
65
- *
66
- * Marchingsquares process the grid data as a 3D representation of a function on a 2D plane, therefore it
67
- * assumes the points (x-y coordinates) are one 'unit' distance. The result of the isolines function needs to be
68
- * rescaled, with turfjs, to the original area and proportions on the map
69
- *
70
- * @private
71
- * @param {Array<Array<number>>} matrix Grid Data
72
- * @param {Array<number>} breaks BreakProps
73
- * @param {string} zProperty name of the z-values property
74
- * @param {Object} [commonProperties={}] GeoJSON properties passed to ALL isolines
75
- * @param {Object} [breaksProperties=[]] GeoJSON properties passed to the correspondent isoline
76
- * @returns {Array<MultiLineString>} isolines
77
- */
78
- function createIsoLines(matrix, breaks, zProperty, commonProperties, breaksProperties) {
79
- const results = [];
80
- for (let i = 1; i < breaks.length; i++) {
81
- const threshold = +breaks[i]; // make sure it's a number
82
- const properties = Object.assign(Object.assign({}, commonProperties), breaksProperties[i]);
83
- properties[zProperty] = threshold;
84
- const isoline = helpers_1.multiLineString(marchingsquares_isocontours_1.default(matrix, threshold), properties);
85
- results.push(isoline);
86
- }
87
- return results;
88
- }
89
- /**
90
- * Translates and scales isolines
91
- *
92
- * @private
93
- * @param {Array<MultiLineString>} createdIsoLines to be rescaled
94
- * @param {Array<Array<number>>} matrix Grid Data
95
- * @param {Object} points Points by Latitude
96
- * @returns {Array<MultiLineString>} isolines
97
- */
98
- function rescaleIsolines(createdIsoLines, matrix, points) {
99
- // get dimensions (on the map) of the original grid
100
- const gridBbox = bbox_1.default(points); // [ minX, minY, maxX, maxY ]
101
- const originalWidth = gridBbox[2] - gridBbox[0];
102
- const originalHeigth = gridBbox[3] - gridBbox[1];
103
- // get origin, which is the first point of the last row on the rectangular data on the map
104
- const x0 = gridBbox[0];
105
- const y0 = gridBbox[1];
106
- // get number of cells per side
107
- const matrixWidth = matrix[0].length - 1;
108
- const matrixHeight = matrix.length - 1;
109
- // calculate the scaling factor between matrix and rectangular grid on the map
110
- const scaleX = originalWidth / matrixWidth;
111
- const scaleY = originalHeigth / matrixHeight;
112
- const resize = (point) => {
113
- point[0] = point[0] * scaleX + x0;
114
- point[1] = point[1] * scaleY + y0;
115
- };
116
- // resize and shift each point/line of the createdIsoLines
117
- createdIsoLines.forEach((isoline) => {
118
- meta_1.coordEach(isoline, resize);
119
- });
120
- return createdIsoLines;
121
- }
122
- exports.default = isolines;
@@ -1,37 +0,0 @@
1
- /**
2
- * Takes a {@link Point} grid and returns a correspondent matrix {Array<Array<number>>}
3
- * of the 'property' values
4
- *
5
- * @name gridToMatrix
6
- * @param {FeatureCollection<Point>} grid of points
7
- * @param {Object} [options={}] Optional parameters
8
- * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
9
- * @param {boolean} [options.flip=false] returns the matrix upside-down
10
- * @param {boolean} [options.flags=false] flags, adding a `matrixPosition` array field ([row, column]) to its properties,
11
- * the grid points with coordinates on the matrix
12
- * @returns {Array<Array<number>>} matrix of property values
13
- * @example
14
- * var extent = [-70.823364, -33.553984, -70.473175, -33.302986];
15
- * var cellSize = 3;
16
- * var grid = turf.pointGrid(extent, cellSize);
17
- * // add a random property to each point between 0 and 60
18
- * for (var i = 0; i < grid.features.length; i++) {
19
- * grid.features[i].properties.elevation = (Math.random() * 60);
20
- * }
21
- * gridToMatrix(grid);
22
- * //= [
23
- * [ 1, 13, 10, 9, 10, 13, 18],
24
- * [34, 8, 5, 4, 5, 8, 13],
25
- * [10, 5, 2, 1, 2, 5, 4],
26
- * [ 0, 4, 56, 19, 1, 4, 9],
27
- * [10, 5, 2, 1, 2, 5, 10],
28
- * [57, 8, 5, 4, 5, 0, 57],
29
- * [ 3, 13, 10, 9, 5, 13, 18],
30
- * [18, 13, 10, 9, 78, 13, 18]
31
- * ]
32
- */
33
- export default function gridToMatrix(grid: any, options?: {
34
- zProperty?: string;
35
- flip?: boolean;
36
- flags?: boolean;
37
- } | undefined): number[][];
@@ -1,104 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const invariant_1 = require("@turf/invariant");
4
- const meta_1 = require("@turf/meta");
5
- const helpers_1 = require("@turf/helpers");
6
- /**
7
- * Takes a {@link Point} grid and returns a correspondent matrix {Array<Array<number>>}
8
- * of the 'property' values
9
- *
10
- * @name gridToMatrix
11
- * @param {FeatureCollection<Point>} grid of points
12
- * @param {Object} [options={}] Optional parameters
13
- * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
14
- * @param {boolean} [options.flip=false] returns the matrix upside-down
15
- * @param {boolean} [options.flags=false] flags, adding a `matrixPosition` array field ([row, column]) to its properties,
16
- * the grid points with coordinates on the matrix
17
- * @returns {Array<Array<number>>} matrix of property values
18
- * @example
19
- * var extent = [-70.823364, -33.553984, -70.473175, -33.302986];
20
- * var cellSize = 3;
21
- * var grid = turf.pointGrid(extent, cellSize);
22
- * // add a random property to each point between 0 and 60
23
- * for (var i = 0; i < grid.features.length; i++) {
24
- * grid.features[i].properties.elevation = (Math.random() * 60);
25
- * }
26
- * gridToMatrix(grid);
27
- * //= [
28
- * [ 1, 13, 10, 9, 10, 13, 18],
29
- * [34, 8, 5, 4, 5, 8, 13],
30
- * [10, 5, 2, 1, 2, 5, 4],
31
- * [ 0, 4, 56, 19, 1, 4, 9],
32
- * [10, 5, 2, 1, 2, 5, 10],
33
- * [57, 8, 5, 4, 5, 0, 57],
34
- * [ 3, 13, 10, 9, 5, 13, 18],
35
- * [18, 13, 10, 9, 78, 13, 18]
36
- * ]
37
- */
38
- function gridToMatrix(grid, options) {
39
- // Optional parameters
40
- options = options || {};
41
- if (!helpers_1.isObject(options))
42
- throw new Error("options is invalid");
43
- var zProperty = options.zProperty || "elevation";
44
- var flip = options.flip;
45
- var flags = options.flags;
46
- // validation
47
- invariant_1.collectionOf(grid, "Point", "input must contain Points");
48
- var pointsMatrix = sortPointsByLatLng(grid, flip);
49
- var matrix = [];
50
- // create property matrix from sorted points
51
- // looping order matters here
52
- for (var r = 0; r < pointsMatrix.length; r++) {
53
- var pointRow = pointsMatrix[r];
54
- var row = [];
55
- for (var c = 0; c < pointRow.length; c++) {
56
- var point = pointRow[c];
57
- // Check if zProperty exist
58
- if (point.properties[zProperty])
59
- row.push(point.properties[zProperty]);
60
- else
61
- row.push(0);
62
- // add flags
63
- if (flags === true)
64
- point.properties.matrixPosition = [r, c];
65
- }
66
- matrix.push(row);
67
- }
68
- return matrix;
69
- }
70
- exports.default = gridToMatrix;
71
- /**
72
- * Sorts points by latitude and longitude, creating a 2-dimensional array of points
73
- *
74
- * @private
75
- * @param {FeatureCollection<Point>} points GeoJSON Point features
76
- * @param {boolean} [flip=false] returns the matrix upside-down
77
- * @returns {Array<Array<Point>>} points ordered by latitude and longitude
78
- */
79
- function sortPointsByLatLng(points, flip) {
80
- var pointsByLatitude = {};
81
- // divide points by rows with the same latitude
82
- meta_1.featureEach(points, function (point) {
83
- var lat = invariant_1.getCoords(point)[1];
84
- if (!pointsByLatitude[lat])
85
- pointsByLatitude[lat] = [];
86
- pointsByLatitude[lat].push(point);
87
- });
88
- // sort points (with the same latitude) by longitude
89
- var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function (lat) {
90
- var row = pointsByLatitude[lat];
91
- var rowOrderedByLongitude = row.sort(function (a, b) {
92
- return invariant_1.getCoords(a)[0] - invariant_1.getCoords(b)[0];
93
- });
94
- return rowOrderedByLongitude;
95
- });
96
- // sort rows (of points with the same latitude) by latitude
97
- var pointMatrix = orderedRowsByLatitude.sort(function (a, b) {
98
- if (flip)
99
- return invariant_1.getCoords(a[0])[1] - invariant_1.getCoords(b[0])[1];
100
- else
101
- return invariant_1.getCoords(b[0])[1] - invariant_1.getCoords(a[0])[1];
102
- });
103
- return pointMatrix;
104
- }
@@ -1 +0,0 @@
1
- export default function isoContours(data: any, threshold: any, options: any): any[];
@@ -1,388 +0,0 @@
1
- "use strict";
2
- /**
3
- * @license GNU Affero General Public License.
4
- * Copyright (c) 2015, 2015 Ronny Lorenz <ronny@tbi.univie.ac.at>
5
- * v. 1.2.0
6
- * https://github.com/RaumZeit/MarchingSquares.js
7
- *
8
- * MarchingSquaresJS is free software: you can redistribute it and/or modify
9
- * it under the terms of the GNU Affero General Public License as published by
10
- * the Free Software Foundation, either version 3 of the License, or
11
- * (at your option) any later version.
12
- *
13
- * MarchingSquaresJS is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
- * GNU Affero General Public License for more details.
17
- *
18
- * As additional permission under GNU Affero General Public License version 3
19
- * section 7, third-party projects (personal or commercial) may distribute,
20
- * include, or link against UNMODIFIED VERSIONS of MarchingSquaresJS without the
21
- * requirement that said third-party project for that reason alone becomes
22
- * subject to any requirement of the GNU Affero General Public License version 3.
23
- * Any modifications to MarchingSquaresJS, however, must be shared with the public
24
- * and made available.
25
- *
26
- * In summary this:
27
- * - allows you to use MarchingSquaresJS at no cost
28
- * - allows you to use MarchingSquaresJS for both personal and commercial purposes
29
- * - allows you to distribute UNMODIFIED VERSIONS of MarchingSquaresJS under any
30
- * license as long as this license notice is included
31
- * - enables you to keep the source code of your program that uses MarchingSquaresJS
32
- * undisclosed
33
- * - forces you to share any modifications you have made to MarchingSquaresJS,
34
- * e.g. bug-fixes
35
- *
36
- * You should have received a copy of the GNU Affero General Public License
37
- * along with MarchingSquaresJS. If not, see <http://www.gnu.org/licenses/>.
38
- */
39
- Object.defineProperty(exports, "__esModule", { value: true });
40
- /**
41
- * Compute the isocontour(s) of a scalar 2D field given
42
- * a certain threshold by applying the Marching Squares
43
- * Algorithm. The function returns a list of path coordinates
44
- */
45
- var defaultSettings = {
46
- successCallback: null,
47
- verbose: false,
48
- };
49
- var settings = {};
50
- function isoContours(data, threshold, options) {
51
- /* process options */
52
- options = options ? options : {};
53
- var optionKeys = Object.keys(defaultSettings);
54
- for (var i = 0; i < optionKeys.length; i++) {
55
- var key = optionKeys[i];
56
- var val = options[key];
57
- val =
58
- typeof val !== "undefined" && val !== null ? val : defaultSettings[key];
59
- settings[key] = val;
60
- }
61
- if (settings.verbose)
62
- console.log("MarchingSquaresJS-isoContours: computing isocontour for " + threshold);
63
- var ret = contourGrid2Paths(computeContourGrid(data, threshold));
64
- if (typeof settings.successCallback === "function")
65
- settings.successCallback(ret);
66
- return ret;
67
- }
68
- exports.default = isoContours;
69
- /*
70
- Thats all for the public interface, below follows the actual
71
- implementation
72
- */
73
- /*
74
- ################################
75
- Isocontour implementation below
76
- ################################
77
- */
78
- /* assume that x1 == 1 && x0 == 0 */
79
- function interpolateX(y, y0, y1) {
80
- return (y - y0) / (y1 - y0);
81
- }
82
- /* compute the isocontour 4-bit grid */
83
- function computeContourGrid(data, threshold) {
84
- var rows = data.length - 1;
85
- var cols = data[0].length - 1;
86
- var ContourGrid = { rows: rows, cols: cols, cells: [] };
87
- for (var j = 0; j < rows; ++j) {
88
- ContourGrid.cells[j] = [];
89
- for (var i = 0; i < cols; ++i) {
90
- /* compose the 4-bit corner representation */
91
- var cval = 0;
92
- var tl = data[j + 1][i];
93
- var tr = data[j + 1][i + 1];
94
- var br = data[j][i + 1];
95
- var bl = data[j][i];
96
- if (isNaN(tl) || isNaN(tr) || isNaN(br) || isNaN(bl)) {
97
- continue;
98
- }
99
- cval |= tl >= threshold ? 8 : 0;
100
- cval |= tr >= threshold ? 4 : 0;
101
- cval |= br >= threshold ? 2 : 0;
102
- cval |= bl >= threshold ? 1 : 0;
103
- /* resolve ambiguity for cval == 5 || 10 via averaging */
104
- var flipped = false;
105
- if (cval === 5 || cval === 10) {
106
- var average = (tl + tr + br + bl) / 4;
107
- if (cval === 5 && average < threshold) {
108
- cval = 10;
109
- flipped = true;
110
- }
111
- else if (cval === 10 && average < threshold) {
112
- cval = 5;
113
- flipped = true;
114
- }
115
- }
116
- /* add cell to ContourGrid if it contains edges */
117
- if (cval !== 0 && cval !== 15) {
118
- var top, bottom, left, right;
119
- top = bottom = left = right = 0.5;
120
- /* interpolate edges of cell */
121
- if (cval === 1) {
122
- left = 1 - interpolateX(threshold, tl, bl);
123
- bottom = 1 - interpolateX(threshold, br, bl);
124
- }
125
- else if (cval === 2) {
126
- bottom = interpolateX(threshold, bl, br);
127
- right = 1 - interpolateX(threshold, tr, br);
128
- }
129
- else if (cval === 3) {
130
- left = 1 - interpolateX(threshold, tl, bl);
131
- right = 1 - interpolateX(threshold, tr, br);
132
- }
133
- else if (cval === 4) {
134
- top = interpolateX(threshold, tl, tr);
135
- right = interpolateX(threshold, br, tr);
136
- }
137
- else if (cval === 5) {
138
- top = interpolateX(threshold, tl, tr);
139
- right = interpolateX(threshold, br, tr);
140
- bottom = 1 - interpolateX(threshold, br, bl);
141
- left = 1 - interpolateX(threshold, tl, bl);
142
- }
143
- else if (cval === 6) {
144
- bottom = interpolateX(threshold, bl, br);
145
- top = interpolateX(threshold, tl, tr);
146
- }
147
- else if (cval === 7) {
148
- left = 1 - interpolateX(threshold, tl, bl);
149
- top = interpolateX(threshold, tl, tr);
150
- }
151
- else if (cval === 8) {
152
- left = interpolateX(threshold, bl, tl);
153
- top = 1 - interpolateX(threshold, tr, tl);
154
- }
155
- else if (cval === 9) {
156
- bottom = 1 - interpolateX(threshold, br, bl);
157
- top = 1 - interpolateX(threshold, tr, tl);
158
- }
159
- else if (cval === 10) {
160
- top = 1 - interpolateX(threshold, tr, tl);
161
- right = 1 - interpolateX(threshold, tr, br);
162
- bottom = interpolateX(threshold, bl, br);
163
- left = interpolateX(threshold, bl, tl);
164
- }
165
- else if (cval === 11) {
166
- top = 1 - interpolateX(threshold, tr, tl);
167
- right = 1 - interpolateX(threshold, tr, br);
168
- }
169
- else if (cval === 12) {
170
- left = interpolateX(threshold, bl, tl);
171
- right = interpolateX(threshold, br, tr);
172
- }
173
- else if (cval === 13) {
174
- bottom = 1 - interpolateX(threshold, br, bl);
175
- right = interpolateX(threshold, br, tr);
176
- }
177
- else if (cval === 14) {
178
- left = interpolateX(threshold, bl, tl);
179
- bottom = interpolateX(threshold, bl, br);
180
- }
181
- else {
182
- console.log("MarchingSquaresJS-isoContours: Illegal cval detected: " + cval);
183
- }
184
- ContourGrid.cells[j][i] = {
185
- cval: cval,
186
- flipped: flipped,
187
- top: top,
188
- right: right,
189
- bottom: bottom,
190
- left: left,
191
- };
192
- }
193
- }
194
- }
195
- return ContourGrid;
196
- }
197
- function isSaddle(cell) {
198
- return cell.cval === 5 || cell.cval === 10;
199
- }
200
- function isTrivial(cell) {
201
- return cell.cval === 0 || cell.cval === 15;
202
- }
203
- function clearCell(cell) {
204
- if (!isTrivial(cell) && cell.cval !== 5 && cell.cval !== 10) {
205
- cell.cval = 15;
206
- }
207
- }
208
- function getXY(cell, edge) {
209
- if (edge === "top") {
210
- return [cell.top, 1.0];
211
- }
212
- else if (edge === "bottom") {
213
- return [cell.bottom, 0.0];
214
- }
215
- else if (edge === "right") {
216
- return [1.0, cell.right];
217
- }
218
- else if (edge === "left") {
219
- return [0.0, cell.left];
220
- }
221
- }
222
- function contourGrid2Paths(grid) {
223
- var paths = [];
224
- var path_idx = 0;
225
- var epsilon = 1e-7;
226
- grid.cells.forEach(function (g, j) {
227
- g.forEach(function (gg, i) {
228
- if (typeof gg !== "undefined" && !isSaddle(gg) && !isTrivial(gg)) {
229
- var p = tracePath(grid.cells, j, i);
230
- var merged = false;
231
- /* we may try to merge paths at this point */
232
- if (p.info === "mergeable") {
233
- /*
234
- search backwards through the path array to find an entry
235
- that starts with where the current path ends...
236
- */
237
- var x = p.path[p.path.length - 1][0], y = p.path[p.path.length - 1][1];
238
- for (var k = path_idx - 1; k >= 0; k--) {
239
- if (Math.abs(paths[k][0][0] - x) <= epsilon &&
240
- Math.abs(paths[k][0][1] - y) <= epsilon) {
241
- for (var l = p.path.length - 2; l >= 0; --l) {
242
- paths[k].unshift(p.path[l]);
243
- }
244
- merged = true;
245
- break;
246
- }
247
- }
248
- }
249
- if (!merged)
250
- paths[path_idx++] = p.path;
251
- }
252
- });
253
- });
254
- return paths;
255
- }
256
- /*
257
- construct consecutive line segments from starting cell by
258
- walking arround the enclosed area clock-wise
259
- */
260
- function tracePath(grid, j, i) {
261
- var maxj = grid.length;
262
- var p = [];
263
- var dxContour = [0, 0, 1, 1, 0, 0, 0, 0, -1, 0, 1, 1, -1, 0, -1, 0];
264
- var dyContour = [0, -1, 0, 0, 1, 1, 1, 1, 0, -1, 0, 0, 0, -1, 0, 0];
265
- var dx, dy;
266
- var startEdge = [
267
- "none",
268
- "left",
269
- "bottom",
270
- "left",
271
- "right",
272
- "none",
273
- "bottom",
274
- "left",
275
- "top",
276
- "top",
277
- "none",
278
- "top",
279
- "right",
280
- "right",
281
- "bottom",
282
- "none",
283
- ];
284
- var nextEdge = [
285
- "none",
286
- "bottom",
287
- "right",
288
- "right",
289
- "top",
290
- "top",
291
- "top",
292
- "top",
293
- "left",
294
- "bottom",
295
- "right",
296
- "right",
297
- "left",
298
- "bottom",
299
- "left",
300
- "none",
301
- ];
302
- var edge;
303
- var currentCell = grid[j][i];
304
- var cval = currentCell.cval;
305
- var edge = startEdge[cval];
306
- var pt = getXY(currentCell, edge);
307
- /* push initial segment */
308
- p.push([i + pt[0], j + pt[1]]);
309
- edge = nextEdge[cval];
310
- pt = getXY(currentCell, edge);
311
- p.push([i + pt[0], j + pt[1]]);
312
- clearCell(currentCell);
313
- /* now walk arround the enclosed area in clockwise-direction */
314
- var k = i + dxContour[cval];
315
- var l = j + dyContour[cval];
316
- var prev_cval = cval;
317
- while (k >= 0 && l >= 0 && l < maxj && (k != i || l != j)) {
318
- currentCell = grid[l][k];
319
- if (typeof currentCell === "undefined") {
320
- /* path ends here */
321
- //console.log(k + " " + l + " is undefined, stopping path!");
322
- break;
323
- }
324
- cval = currentCell.cval;
325
- if (cval === 0 || cval === 15) {
326
- return { path: p, info: "mergeable" };
327
- }
328
- edge = nextEdge[cval];
329
- dx = dxContour[cval];
330
- dy = dyContour[cval];
331
- if (cval === 5 || cval === 10) {
332
- /* select upper or lower band, depending on previous cells cval */
333
- if (cval === 5) {
334
- if (currentCell.flipped) {
335
- /* this is actually a flipped case 10 */
336
- if (dyContour[prev_cval] === -1) {
337
- edge = "left";
338
- dx = -1;
339
- dy = 0;
340
- }
341
- else {
342
- edge = "right";
343
- dx = 1;
344
- dy = 0;
345
- }
346
- }
347
- else {
348
- /* real case 5 */
349
- if (dxContour[prev_cval] === -1) {
350
- edge = "bottom";
351
- dx = 0;
352
- dy = -1;
353
- }
354
- }
355
- }
356
- else if (cval === 10) {
357
- if (currentCell.flipped) {
358
- /* this is actually a flipped case 5 */
359
- if (dxContour[prev_cval] === -1) {
360
- edge = "top";
361
- dx = 0;
362
- dy = 1;
363
- }
364
- else {
365
- edge = "bottom";
366
- dx = 0;
367
- dy = -1;
368
- }
369
- }
370
- else {
371
- /* real case 10 */
372
- if (dyContour[prev_cval] === 1) {
373
- edge = "left";
374
- dx = -1;
375
- dy = 0;
376
- }
377
- }
378
- }
379
- }
380
- pt = getXY(currentCell, edge);
381
- p.push([k + pt[0], l + pt[1]]);
382
- clearCell(currentCell);
383
- k += dx;
384
- l += dy;
385
- prev_cval = cval;
386
- }
387
- return { path: p, info: "closed" };
388
- }