@thi.ng/geom-isoline 2.1.86 → 2.1.88
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/CHANGELOG.md +1 -1
- package/index.js +125 -125
- package/package.json +9 -7
package/CHANGELOG.md
CHANGED
package/index.js
CHANGED
|
@@ -1,144 +1,144 @@
|
|
|
1
1
|
import { range2d } from "@thi.ng/transducers/range2d";
|
|
2
|
-
// flattened [to, clear] tuples
|
|
3
|
-
// all positive values are given as times 2
|
|
4
|
-
// prettier-ignore
|
|
5
2
|
const EDGE_INDEX = [
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
-1,
|
|
4
|
+
-1,
|
|
5
|
+
4,
|
|
6
|
+
0,
|
|
7
|
+
2,
|
|
8
|
+
0,
|
|
9
|
+
2,
|
|
10
|
+
0,
|
|
11
|
+
0,
|
|
12
|
+
0,
|
|
13
|
+
-1,
|
|
14
|
+
-1,
|
|
15
|
+
0,
|
|
16
|
+
0,
|
|
17
|
+
0,
|
|
18
|
+
0,
|
|
19
|
+
6,
|
|
20
|
+
0,
|
|
21
|
+
4,
|
|
22
|
+
0,
|
|
23
|
+
-1,
|
|
24
|
+
-1,
|
|
25
|
+
2,
|
|
26
|
+
0,
|
|
27
|
+
6,
|
|
28
|
+
0,
|
|
29
|
+
4,
|
|
30
|
+
0,
|
|
31
|
+
6,
|
|
32
|
+
0,
|
|
33
|
+
-1,
|
|
34
|
+
-1
|
|
8
35
|
];
|
|
9
|
-
// flattened coord offsets [x,y] tuples
|
|
10
36
|
const NEXT_EDGES = [0, -1, 1, 0, 0, 1, -1, 0];
|
|
11
|
-
// flattened [to,clear] tuples (all values times 2)
|
|
12
37
|
const S5 = [4, 8, 0, 2, 0, 26, 4, 14];
|
|
13
38
|
const S10 = [6, 4, 2, 16, 6, 22, 2, 28];
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
39
|
+
const setBorder = (src, w, h, val) => {
|
|
40
|
+
const w1 = w - 1;
|
|
41
|
+
const h1 = h - 1;
|
|
42
|
+
const idxH1 = h1 * w;
|
|
43
|
+
for (let x = 0; x < w; x++) {
|
|
44
|
+
src[x] = src[idxH1 + x] = val;
|
|
45
|
+
}
|
|
46
|
+
for (let y = 0; y < h; y++) {
|
|
47
|
+
const yy = y * w;
|
|
48
|
+
src[yy] = src[w1 + yy] = val;
|
|
49
|
+
}
|
|
50
|
+
return src;
|
|
26
51
|
};
|
|
27
52
|
const encodeCrossings = (src, w, h, iso) => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
(src[i] < iso ? 16 : 0) |
|
|
35
|
-
(src[i + 1] < iso ? 8 : 0) |
|
|
36
|
-
(src[i + 1 + w] < iso ? 4 : 0) |
|
|
37
|
-
(src[i + w] < iso ? 2 : 0);
|
|
38
|
-
}
|
|
39
|
-
i++;
|
|
53
|
+
const out = new Uint8Array(src.length);
|
|
54
|
+
const w1 = w - 1;
|
|
55
|
+
const h1 = h - 1;
|
|
56
|
+
for (let y = 0, i = 0; y < h1; y++) {
|
|
57
|
+
for (let x = 0; x < w1; i++, x++) {
|
|
58
|
+
out[i] = (src[i] < iso ? 16 : 0) | (src[i + 1] < iso ? 8 : 0) | (src[i + 1 + w] < iso ? 4 : 0) | (src[i + w] < iso ? 2 : 0);
|
|
40
59
|
}
|
|
41
|
-
|
|
60
|
+
i++;
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
42
63
|
};
|
|
43
64
|
const cellValue = (src, w, idx) => {
|
|
44
|
-
|
|
65
|
+
return (src[idx] + src[idx + 1] + src[idx + w] + src[idx + w + 1]) * 0.25;
|
|
45
66
|
};
|
|
46
67
|
const mix = (src, w, x1, y1, x2, y2, iso) => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
68
|
+
const a = src[y1 * w + x1];
|
|
69
|
+
const b = src[y2 * w + x2];
|
|
70
|
+
return a === b ? 0 : (a - iso) / (a - b);
|
|
50
71
|
};
|
|
51
|
-
// prettier-ignore
|
|
52
72
|
const contourVertex = [
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
73
|
+
(src, w, x, y, iso) => [x + mix(src, w, x, y, x + 1, y, iso), y],
|
|
74
|
+
(src, w, x, y, iso) => [x + 1, y + mix(src, w, x + 1, y, x + 1, y + 1, iso)],
|
|
75
|
+
(src, w, x, y, iso) => [x + mix(src, w, x, y + 1, x + 1, y + 1, iso), y + 1],
|
|
76
|
+
(src, w, x, y, iso) => [x, y + mix(src, w, x, y, x, y + 1, iso)]
|
|
57
77
|
];
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
const i = y * w + x;
|
|
100
|
-
const id = coded[i]; // * 2
|
|
101
|
-
if (id === 10) {
|
|
102
|
-
idx = (cellValue(src, w, i) > iso ? 0 : 4) + (from === 6 ? 0 : 2);
|
|
103
|
-
to = S5[idx];
|
|
104
|
-
clear = S5[idx + 1];
|
|
105
|
-
}
|
|
106
|
-
else if (id === 20) {
|
|
107
|
-
idx =
|
|
108
|
-
cellValue(src, w, i) > iso
|
|
109
|
-
? from === 0
|
|
110
|
-
? 0
|
|
111
|
-
: 2
|
|
112
|
-
: from === 4
|
|
113
|
-
? 4
|
|
114
|
-
: 6;
|
|
115
|
-
to = S10[idx];
|
|
116
|
-
clear = S10[idx + 1];
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
to = EDGE_INDEX[id];
|
|
120
|
-
clear = EDGE_INDEX[id + 1];
|
|
121
|
-
}
|
|
122
|
-
if (from === -1 && to > -1 && curr.length > 0) {
|
|
123
|
-
yield curr;
|
|
124
|
-
curr = [];
|
|
125
|
-
}
|
|
126
|
-
if (clear !== -1) {
|
|
127
|
-
coded[i] = clear;
|
|
128
|
-
}
|
|
129
|
-
if (to >= 0) {
|
|
130
|
-
const p = contourVertex[to >> 1](src, w, x, y, iso);
|
|
131
|
-
p[0] = (p[0] + 0.5) * sx;
|
|
132
|
-
p[1] = (p[1] + 0.5) * sy;
|
|
133
|
-
curr.push(p);
|
|
134
|
-
x += NEXT_EDGES[to];
|
|
135
|
-
y += NEXT_EDGES[to + 1];
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
next = true;
|
|
139
|
-
}
|
|
78
|
+
function* isolines(src, w, h, iso, scale = 1) {
|
|
79
|
+
const coded = encodeCrossings(src, w, h, iso);
|
|
80
|
+
let curr = [];
|
|
81
|
+
let from;
|
|
82
|
+
let to = -1;
|
|
83
|
+
let clear;
|
|
84
|
+
let x;
|
|
85
|
+
let y;
|
|
86
|
+
const w1 = w - 1;
|
|
87
|
+
const h1 = h - 1;
|
|
88
|
+
const [sx, sy] = typeof scale === "number" ? [scale, scale] : scale;
|
|
89
|
+
const cells = range2d(h, w);
|
|
90
|
+
let next = true;
|
|
91
|
+
let idx;
|
|
92
|
+
while (true) {
|
|
93
|
+
from = to;
|
|
94
|
+
if (next) {
|
|
95
|
+
const c = cells.next();
|
|
96
|
+
if (c.done)
|
|
97
|
+
break;
|
|
98
|
+
[y, x] = c.value;
|
|
99
|
+
next = false;
|
|
100
|
+
}
|
|
101
|
+
if (x >= w1 || y >= h1) {
|
|
102
|
+
next = true;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const i = y * w + x;
|
|
106
|
+
const id = coded[i];
|
|
107
|
+
if (id === 10) {
|
|
108
|
+
idx = (cellValue(src, w, i) > iso ? 0 : 4) + (from === 6 ? 0 : 2);
|
|
109
|
+
to = S5[idx];
|
|
110
|
+
clear = S5[idx + 1];
|
|
111
|
+
} else if (id === 20) {
|
|
112
|
+
idx = cellValue(src, w, i) > iso ? from === 0 ? 0 : 2 : from === 4 ? 4 : 6;
|
|
113
|
+
to = S10[idx];
|
|
114
|
+
clear = S10[idx + 1];
|
|
115
|
+
} else {
|
|
116
|
+
to = EDGE_INDEX[id];
|
|
117
|
+
clear = EDGE_INDEX[id + 1];
|
|
140
118
|
}
|
|
141
|
-
if (curr.length > 0) {
|
|
142
|
-
|
|
119
|
+
if (from === -1 && to > -1 && curr.length > 0) {
|
|
120
|
+
yield curr;
|
|
121
|
+
curr = [];
|
|
143
122
|
}
|
|
123
|
+
if (clear !== -1) {
|
|
124
|
+
coded[i] = clear;
|
|
125
|
+
}
|
|
126
|
+
if (to >= 0) {
|
|
127
|
+
const p = contourVertex[to >> 1](src, w, x, y, iso);
|
|
128
|
+
p[0] = (p[0] + 0.5) * sx;
|
|
129
|
+
p[1] = (p[1] + 0.5) * sy;
|
|
130
|
+
curr.push(p);
|
|
131
|
+
x += NEXT_EDGES[to];
|
|
132
|
+
y += NEXT_EDGES[to + 1];
|
|
133
|
+
} else {
|
|
134
|
+
next = true;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (curr.length > 0) {
|
|
138
|
+
yield curr;
|
|
139
|
+
}
|
|
144
140
|
}
|
|
141
|
+
export {
|
|
142
|
+
isolines,
|
|
143
|
+
setBorder
|
|
144
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/geom-isoline",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.88",
|
|
4
4
|
"description": "Fast 2D contour line extraction / generation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
28
30
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -33,13 +35,13 @@
|
|
|
33
35
|
"test": "bun test"
|
|
34
36
|
},
|
|
35
37
|
"dependencies": {
|
|
36
|
-
"@thi.ng/api": "^8.9.
|
|
37
|
-
"@thi.ng/transducers": "^8.8.
|
|
38
|
-
"@thi.ng/vectors": "^7.8.
|
|
38
|
+
"@thi.ng/api": "^8.9.12",
|
|
39
|
+
"@thi.ng/transducers": "^8.8.15",
|
|
40
|
+
"@thi.ng/vectors": "^7.8.9"
|
|
39
41
|
},
|
|
40
42
|
"devDependencies": {
|
|
41
43
|
"@microsoft/api-extractor": "^7.38.3",
|
|
42
|
-
"
|
|
44
|
+
"esbuild": "^0.19.8",
|
|
43
45
|
"rimraf": "^5.0.5",
|
|
44
46
|
"tools": "^0.0.1",
|
|
45
47
|
"typedoc": "^0.25.4",
|
|
@@ -76,5 +78,5 @@
|
|
|
76
78
|
"parent": "@thi.ng/geom",
|
|
77
79
|
"year": 2015
|
|
78
80
|
},
|
|
79
|
-
"gitHead": "
|
|
81
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
|
|
80
82
|
}
|