@thi.ng/geom-isoline 2.1.87 → 2.1.89

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/index.js +125 -125
  3. package/package.json +9 -6
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-12-09T19:12:03Z
3
+ - **Last updated**: 2023-12-11T10:07:09Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
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
- -1, -1, 4, 0, 2, 0, 2, 0, 0, 0, -1, -1, 0, 0, 0, 0,
7
- 6, 0, 4, 0, -1, -1, 2, 0, 6, 0, 4, 0, 6, 0, -1, -1
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
- export const setBorder = (src, w, h, val) => {
15
- const w1 = w - 1;
16
- const h1 = h - 1;
17
- const idxH1 = h1 * w;
18
- for (let x = 0; x < w; x++) {
19
- src[x] = src[idxH1 + x] = val;
20
- }
21
- for (let y = 0; y < h; y++) {
22
- const yy = y * w;
23
- src[yy] = src[w1 + yy] = val;
24
- }
25
- return src;
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
- const out = new Uint8Array(src.length);
29
- const w1 = w - 1;
30
- const h1 = h - 1;
31
- for (let y = 0, i = 0; y < h1; y++) {
32
- for (let x = 0; x < w1; i++, x++) {
33
- out[i] =
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
- return out;
60
+ i++;
61
+ }
62
+ return out;
42
63
  };
43
64
  const cellValue = (src, w, idx) => {
44
- return (src[idx] + src[idx + 1] + src[idx + w] + src[idx + w + 1]) * 0.25;
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
- const a = src[y1 * w + x1];
48
- const b = src[y2 * w + x2];
49
- return a === b ? 0 : (a - iso) / (a - b);
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
- (src, w, x, y, iso) => [x + mix(src, w, x, y, x + 1, y, iso), y],
54
- (src, w, x, y, iso) => [x + 1, y + mix(src, w, x + 1, y, x + 1, y + 1, iso)],
55
- (src, w, x, y, iso) => [x + mix(src, w, x, y + 1, x + 1, y + 1, iso), y + 1],
56
- (src, w, x, y, iso) => [x, y + mix(src, w, x, y, x, y + 1, iso)]
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
- * Takes an linearized 2d input field of scalars (i.e. `src` array), its
60
- * `width`, `height` and computes iso lines (contours) for given `iso` threshold
61
- * value. The computation is implemented as a generator, yielding 1 point array
62
- * per found contour. The returned points optionally can be scaled using `scale`
63
- * factor or vector. The default scale of 1.0 will result in coords in these
64
- * ranges: [0.5 .. w-0.5] (for X) and [0.5 .. h-0.5] (for Y).
65
- *
66
- * @param src -
67
- * @param w -
68
- * @param h -
69
- * @param iso -
70
- * @param scale -
71
- */
72
- export function* isolines(src, w, h, iso, scale = 1) {
73
- const coded = encodeCrossings(src, w, h, iso);
74
- let curr = [];
75
- let from;
76
- let to = -1;
77
- let clear;
78
- let x;
79
- let y;
80
- const w1 = w - 1;
81
- const h1 = h - 1;
82
- const [sx, sy] = typeof scale === "number" ? [scale, scale] : scale;
83
- const cells = range2d(h, w);
84
- let next = true;
85
- let idx;
86
- while (true) {
87
- from = to;
88
- if (next) {
89
- const c = cells.next();
90
- if (c.done)
91
- break;
92
- [y, x] = c.value;
93
- next = false;
94
- }
95
- if (x >= w1 || y >= h1) {
96
- next = true;
97
- continue;
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
- yield curr;
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.87",
3
+ "version": "2.1.89",
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 clean && tsc --declaration",
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,12 +35,13 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/api": "^8.9.11",
37
- "@thi.ng/transducers": "^8.8.14",
38
- "@thi.ng/vectors": "^7.8.8"
38
+ "@thi.ng/api": "^8.9.12",
39
+ "@thi.ng/transducers": "^8.8.15",
40
+ "@thi.ng/vectors": "^7.8.10"
39
41
  },
40
42
  "devDependencies": {
41
43
  "@microsoft/api-extractor": "^7.38.3",
44
+ "esbuild": "^0.19.8",
42
45
  "rimraf": "^5.0.5",
43
46
  "tools": "^0.0.1",
44
47
  "typedoc": "^0.25.4",
@@ -75,5 +78,5 @@
75
78
  "parent": "@thi.ng/geom",
76
79
  "year": 2015
77
80
  },
78
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
81
+ "gitHead": "22e36fa838e5431d40165384918b395603bbd92f\n"
79
82
  }