@pooder/kit 5.3.0 → 5.3.1
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/.test-dist/src/CanvasService.js +249 -249
- package/.test-dist/src/ViewportSystem.js +75 -75
- package/.test-dist/src/background.js +203 -203
- package/.test-dist/src/bridgeSelection.js +20 -20
- package/.test-dist/src/constraints.js +237 -237
- package/.test-dist/src/dieline.js +818 -818
- package/.test-dist/src/edgeScale.js +12 -12
- package/.test-dist/src/feature.js +826 -826
- package/.test-dist/src/featureComplete.js +32 -32
- package/.test-dist/src/film.js +167 -167
- package/.test-dist/src/geometry.js +506 -506
- package/.test-dist/src/image.js +1250 -1250
- package/.test-dist/src/maskOps.js +270 -270
- package/.test-dist/src/mirror.js +104 -104
- package/.test-dist/src/renderSpec.js +2 -2
- package/.test-dist/src/ruler.js +343 -343
- package/.test-dist/src/sceneLayout.js +99 -99
- package/.test-dist/src/sceneLayoutModel.js +196 -196
- package/.test-dist/src/sceneView.js +40 -40
- package/.test-dist/src/sceneVisibility.js +42 -42
- package/.test-dist/src/size.js +332 -332
- package/.test-dist/src/tracer.js +544 -544
- package/.test-dist/src/white-ink.js +829 -829
- package/.test-dist/src/wrappedOffsets.js +33 -33
- package/CHANGELOG.md +6 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +108 -20
- package/dist/index.mjs +108 -20
- package/package.json +1 -1
- package/src/coordinate.ts +106 -106
- package/src/extensions/background.ts +230 -230
- package/src/extensions/bridgeSelection.ts +17 -17
- package/src/extensions/constraints.ts +322 -322
- package/src/extensions/dieline.ts +46 -0
- package/src/extensions/edgeScale.ts +19 -19
- package/src/extensions/feature.ts +1021 -1021
- package/src/extensions/featureComplete.ts +46 -46
- package/src/extensions/film.ts +194 -194
- package/src/extensions/geometry.ts +752 -719
- package/src/extensions/image.ts +1926 -1924
- package/src/extensions/index.ts +11 -11
- package/src/extensions/maskOps.ts +283 -283
- package/src/extensions/mirror.ts +128 -128
- package/src/extensions/ruler.ts +451 -451
- package/src/extensions/sceneLayout.ts +140 -140
- package/src/extensions/sceneLayoutModel.ts +352 -342
- package/src/extensions/sceneVisibility.ts +71 -71
- package/src/extensions/size.ts +389 -389
- package/src/extensions/tracer.ts +58 -19
- package/src/extensions/white-ink.ts +1400 -1400
- package/src/extensions/wrappedOffsets.ts +33 -33
- package/src/index.ts +2 -2
- package/src/services/CanvasService.ts +300 -300
- package/src/services/ViewportSystem.ts +95 -95
- package/src/services/index.ts +3 -3
- package/src/services/renderSpec.ts +18 -18
- package/src/units.ts +27 -27
- package/tests/run.ts +118 -118
- package/tsconfig.test.json +15 -15
|
@@ -1,270 +1,270 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createMask = createMask;
|
|
4
|
-
exports.circularMorphology = circularMorphology;
|
|
5
|
-
exports.fillHoles = fillHoles;
|
|
6
|
-
exports.countForeground = countForeground;
|
|
7
|
-
exports.isMaskConnected8 = isMaskConnected8;
|
|
8
|
-
exports.findMinimalConnectRadius = findMinimalConnectRadius;
|
|
9
|
-
exports.polygonSignedArea = polygonSignedArea;
|
|
10
|
-
function createMask(imageData, options) {
|
|
11
|
-
const { width, height, data } = imageData;
|
|
12
|
-
const { threshold, padding, paddedWidth, paddedHeight, maskMode = "auto", whiteThreshold = 240, alphaOpaqueCutoff = 250, } = options;
|
|
13
|
-
const resolvedMode = maskMode === "auto" ? inferMaskMode(imageData, alphaOpaqueCutoff) : maskMode;
|
|
14
|
-
const mask = new Uint8Array(paddedWidth * paddedHeight);
|
|
15
|
-
for (let y = 0; y < height; y++) {
|
|
16
|
-
for (let x = 0; x < width; x++) {
|
|
17
|
-
const srcIdx = (y * width + x) * 4;
|
|
18
|
-
const r = data[srcIdx];
|
|
19
|
-
const g = data[srcIdx + 1];
|
|
20
|
-
const b = data[srcIdx + 2];
|
|
21
|
-
const a = data[srcIdx + 3];
|
|
22
|
-
const destIdx = (y + padding) * paddedWidth + (x + padding);
|
|
23
|
-
if (resolvedMode === "alpha") {
|
|
24
|
-
if (a > threshold)
|
|
25
|
-
mask[destIdx] = 1;
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
if (a > threshold &&
|
|
29
|
-
!(r > whiteThreshold && g > whiteThreshold && b > whiteThreshold)) {
|
|
30
|
-
mask[destIdx] = 1;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return mask;
|
|
36
|
-
}
|
|
37
|
-
function inferMaskMode(imageData, alphaOpaqueCutoff) {
|
|
38
|
-
const { data } = imageData;
|
|
39
|
-
const total = data.length / 4;
|
|
40
|
-
let belowOpaque = 0;
|
|
41
|
-
let veryTransparent = 0;
|
|
42
|
-
let minAlpha = 255;
|
|
43
|
-
for (let i = 3; i < data.length; i += 4) {
|
|
44
|
-
const a = data[i];
|
|
45
|
-
if (a < minAlpha)
|
|
46
|
-
minAlpha = a;
|
|
47
|
-
if (a < alphaOpaqueCutoff)
|
|
48
|
-
belowOpaque++;
|
|
49
|
-
if (a < 32)
|
|
50
|
-
veryTransparent++;
|
|
51
|
-
}
|
|
52
|
-
if (minAlpha === 255)
|
|
53
|
-
return "whitebg";
|
|
54
|
-
const belowOpaqueRatio = belowOpaque / total;
|
|
55
|
-
const veryTransparentRatio = veryTransparent / total;
|
|
56
|
-
if (veryTransparentRatio >= 0.0005)
|
|
57
|
-
return "alpha";
|
|
58
|
-
if (belowOpaqueRatio >= 0.01)
|
|
59
|
-
return "alpha";
|
|
60
|
-
return "whitebg";
|
|
61
|
-
}
|
|
62
|
-
function circularMorphology(mask, width, height, radius, op) {
|
|
63
|
-
const dilate = (m, r) => {
|
|
64
|
-
const horizontalDist = new Int32Array(width * height);
|
|
65
|
-
for (let y = 0; y < height; y++) {
|
|
66
|
-
let lastSolid = -r * 2;
|
|
67
|
-
for (let x = 0; x < width; x++) {
|
|
68
|
-
if (m[y * width + x])
|
|
69
|
-
lastSolid = x;
|
|
70
|
-
horizontalDist[y * width + x] = x - lastSolid;
|
|
71
|
-
}
|
|
72
|
-
lastSolid = width + r * 2;
|
|
73
|
-
for (let x = width - 1; x >= 0; x--) {
|
|
74
|
-
if (m[y * width + x])
|
|
75
|
-
lastSolid = x;
|
|
76
|
-
horizontalDist[y * width + x] = Math.min(horizontalDist[y * width + x], lastSolid - x);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
const result = new Uint8Array(width * height);
|
|
80
|
-
const r2 = r * r;
|
|
81
|
-
for (let x = 0; x < width; x++) {
|
|
82
|
-
for (let y = 0; y < height; y++) {
|
|
83
|
-
let found = false;
|
|
84
|
-
const minY = Math.max(0, y - r);
|
|
85
|
-
const maxY = Math.min(height - 1, y + r);
|
|
86
|
-
for (let dy = minY; dy <= maxY; dy++) {
|
|
87
|
-
const dY = dy - y;
|
|
88
|
-
const hDist = horizontalDist[dy * width + x];
|
|
89
|
-
if (hDist * hDist + dY * dY <= r2) {
|
|
90
|
-
found = true;
|
|
91
|
-
break;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
if (found)
|
|
95
|
-
result[y * width + x] = 1;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return result;
|
|
99
|
-
};
|
|
100
|
-
const erode = (m, r) => {
|
|
101
|
-
const inverted = new Uint8Array(m.length);
|
|
102
|
-
for (let i = 0; i < m.length; i++)
|
|
103
|
-
inverted[i] = m[i] ? 0 : 1;
|
|
104
|
-
const dilatedInverted = dilate(inverted, r);
|
|
105
|
-
const result = new Uint8Array(m.length);
|
|
106
|
-
for (let i = 0; i < m.length; i++)
|
|
107
|
-
result[i] = dilatedInverted[i] ? 0 : 1;
|
|
108
|
-
return result;
|
|
109
|
-
};
|
|
110
|
-
switch (op) {
|
|
111
|
-
case "dilate":
|
|
112
|
-
return dilate(mask, radius);
|
|
113
|
-
case "erode":
|
|
114
|
-
return erode(mask, radius);
|
|
115
|
-
case "closing":
|
|
116
|
-
return erode(dilate(mask, radius), radius);
|
|
117
|
-
case "opening":
|
|
118
|
-
return dilate(erode(mask, radius), radius);
|
|
119
|
-
default:
|
|
120
|
-
return mask;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
function fillHoles(mask, width, height) {
|
|
124
|
-
const background = new Uint8Array(width * height);
|
|
125
|
-
const queue = [];
|
|
126
|
-
for (let x = 0; x < width; x++) {
|
|
127
|
-
if (mask[x] === 0) {
|
|
128
|
-
background[x] = 1;
|
|
129
|
-
queue.push(x);
|
|
130
|
-
}
|
|
131
|
-
const lastRowIdx = (height - 1) * width + x;
|
|
132
|
-
if (mask[lastRowIdx] === 0) {
|
|
133
|
-
background[lastRowIdx] = 1;
|
|
134
|
-
queue.push(lastRowIdx);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
for (let y = 1; y < height - 1; y++) {
|
|
138
|
-
const leftIdx = y * width;
|
|
139
|
-
const rightIdx = y * width + (width - 1);
|
|
140
|
-
if (mask[leftIdx] === 0) {
|
|
141
|
-
background[leftIdx] = 1;
|
|
142
|
-
queue.push(leftIdx);
|
|
143
|
-
}
|
|
144
|
-
if (mask[rightIdx] === 0) {
|
|
145
|
-
background[rightIdx] = 1;
|
|
146
|
-
queue.push(rightIdx);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
let head = 0;
|
|
150
|
-
while (head < queue.length) {
|
|
151
|
-
const idx = queue[head++];
|
|
152
|
-
const x = idx % width;
|
|
153
|
-
const y = (idx - x) / width;
|
|
154
|
-
const up = y > 0 ? idx - width : -1;
|
|
155
|
-
const down = y < height - 1 ? idx + width : -1;
|
|
156
|
-
const left = x > 0 ? idx - 1 : -1;
|
|
157
|
-
const right = x < width - 1 ? idx + 1 : -1;
|
|
158
|
-
if (up >= 0 && mask[up] === 0 && background[up] === 0) {
|
|
159
|
-
background[up] = 1;
|
|
160
|
-
queue.push(up);
|
|
161
|
-
}
|
|
162
|
-
if (down >= 0 && mask[down] === 0 && background[down] === 0) {
|
|
163
|
-
background[down] = 1;
|
|
164
|
-
queue.push(down);
|
|
165
|
-
}
|
|
166
|
-
if (left >= 0 && mask[left] === 0 && background[left] === 0) {
|
|
167
|
-
background[left] = 1;
|
|
168
|
-
queue.push(left);
|
|
169
|
-
}
|
|
170
|
-
if (right >= 0 && mask[right] === 0 && background[right] === 0) {
|
|
171
|
-
background[right] = 1;
|
|
172
|
-
queue.push(right);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
const filledMask = new Uint8Array(width * height);
|
|
176
|
-
for (let i = 0; i < width * height; i++) {
|
|
177
|
-
filledMask[i] = background[i] === 0 ? 1 : 0;
|
|
178
|
-
}
|
|
179
|
-
return filledMask;
|
|
180
|
-
}
|
|
181
|
-
function countForeground(mask) {
|
|
182
|
-
let c = 0;
|
|
183
|
-
for (let i = 0; i < mask.length; i++)
|
|
184
|
-
c += mask[i] ? 1 : 0;
|
|
185
|
-
return c;
|
|
186
|
-
}
|
|
187
|
-
function isMaskConnected8(mask, width, height) {
|
|
188
|
-
const total = countForeground(mask);
|
|
189
|
-
if (total === 0)
|
|
190
|
-
return true;
|
|
191
|
-
let start = -1;
|
|
192
|
-
for (let i = 0; i < mask.length; i++) {
|
|
193
|
-
if (mask[i]) {
|
|
194
|
-
start = i;
|
|
195
|
-
break;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
if (start === -1)
|
|
199
|
-
return true;
|
|
200
|
-
const visited = new Uint8Array(mask.length);
|
|
201
|
-
const queue = [start];
|
|
202
|
-
visited[start] = 1;
|
|
203
|
-
let seen = 1;
|
|
204
|
-
let head = 0;
|
|
205
|
-
while (head < queue.length) {
|
|
206
|
-
const idx = queue[head++];
|
|
207
|
-
const x = idx % width;
|
|
208
|
-
const y = (idx - x) / width;
|
|
209
|
-
for (let dy = -1; dy <= 1; dy++) {
|
|
210
|
-
const ny = y + dy;
|
|
211
|
-
if (ny < 0 || ny >= height)
|
|
212
|
-
continue;
|
|
213
|
-
for (let dx = -1; dx <= 1; dx++) {
|
|
214
|
-
if (dx === 0 && dy === 0)
|
|
215
|
-
continue;
|
|
216
|
-
const nx = x + dx;
|
|
217
|
-
if (nx < 0 || nx >= width)
|
|
218
|
-
continue;
|
|
219
|
-
const nidx = ny * width + nx;
|
|
220
|
-
if (mask[nidx] && !visited[nidx]) {
|
|
221
|
-
visited[nidx] = 1;
|
|
222
|
-
queue.push(nidx);
|
|
223
|
-
seen++;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
return seen === total;
|
|
229
|
-
}
|
|
230
|
-
function findMinimalConnectRadius(mask, width, height, maxRadius) {
|
|
231
|
-
if (maxRadius <= 0)
|
|
232
|
-
return 0;
|
|
233
|
-
if (isMaskConnected8(mask, width, height))
|
|
234
|
-
return 0;
|
|
235
|
-
let low = 0;
|
|
236
|
-
let high = 1;
|
|
237
|
-
while (high <= maxRadius) {
|
|
238
|
-
const closed = circularMorphology(mask, width, height, high, "closing");
|
|
239
|
-
if (isMaskConnected8(closed, width, height))
|
|
240
|
-
break;
|
|
241
|
-
high *= 2;
|
|
242
|
-
}
|
|
243
|
-
if (high > maxRadius)
|
|
244
|
-
high = maxRadius;
|
|
245
|
-
if (!isMaskConnected8(circularMorphology(mask, width, height, high, "closing"), width, height)) {
|
|
246
|
-
return high;
|
|
247
|
-
}
|
|
248
|
-
while (low + 1 < high) {
|
|
249
|
-
const mid = Math.floor((low + high) / 2);
|
|
250
|
-
const closed = circularMorphology(mask, width, height, mid, "closing");
|
|
251
|
-
if (isMaskConnected8(closed, width, height)) {
|
|
252
|
-
high = mid;
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
low = mid;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return high;
|
|
259
|
-
}
|
|
260
|
-
function polygonSignedArea(points) {
|
|
261
|
-
if (points.length < 3)
|
|
262
|
-
return 0;
|
|
263
|
-
let sum = 0;
|
|
264
|
-
for (let i = 0; i < points.length; i++) {
|
|
265
|
-
const a = points[i];
|
|
266
|
-
const b = points[(i + 1) % points.length];
|
|
267
|
-
sum += a.x * b.y - b.x * a.y;
|
|
268
|
-
}
|
|
269
|
-
return sum / 2;
|
|
270
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createMask = createMask;
|
|
4
|
+
exports.circularMorphology = circularMorphology;
|
|
5
|
+
exports.fillHoles = fillHoles;
|
|
6
|
+
exports.countForeground = countForeground;
|
|
7
|
+
exports.isMaskConnected8 = isMaskConnected8;
|
|
8
|
+
exports.findMinimalConnectRadius = findMinimalConnectRadius;
|
|
9
|
+
exports.polygonSignedArea = polygonSignedArea;
|
|
10
|
+
function createMask(imageData, options) {
|
|
11
|
+
const { width, height, data } = imageData;
|
|
12
|
+
const { threshold, padding, paddedWidth, paddedHeight, maskMode = "auto", whiteThreshold = 240, alphaOpaqueCutoff = 250, } = options;
|
|
13
|
+
const resolvedMode = maskMode === "auto" ? inferMaskMode(imageData, alphaOpaqueCutoff) : maskMode;
|
|
14
|
+
const mask = new Uint8Array(paddedWidth * paddedHeight);
|
|
15
|
+
for (let y = 0; y < height; y++) {
|
|
16
|
+
for (let x = 0; x < width; x++) {
|
|
17
|
+
const srcIdx = (y * width + x) * 4;
|
|
18
|
+
const r = data[srcIdx];
|
|
19
|
+
const g = data[srcIdx + 1];
|
|
20
|
+
const b = data[srcIdx + 2];
|
|
21
|
+
const a = data[srcIdx + 3];
|
|
22
|
+
const destIdx = (y + padding) * paddedWidth + (x + padding);
|
|
23
|
+
if (resolvedMode === "alpha") {
|
|
24
|
+
if (a > threshold)
|
|
25
|
+
mask[destIdx] = 1;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
if (a > threshold &&
|
|
29
|
+
!(r > whiteThreshold && g > whiteThreshold && b > whiteThreshold)) {
|
|
30
|
+
mask[destIdx] = 1;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return mask;
|
|
36
|
+
}
|
|
37
|
+
function inferMaskMode(imageData, alphaOpaqueCutoff) {
|
|
38
|
+
const { data } = imageData;
|
|
39
|
+
const total = data.length / 4;
|
|
40
|
+
let belowOpaque = 0;
|
|
41
|
+
let veryTransparent = 0;
|
|
42
|
+
let minAlpha = 255;
|
|
43
|
+
for (let i = 3; i < data.length; i += 4) {
|
|
44
|
+
const a = data[i];
|
|
45
|
+
if (a < minAlpha)
|
|
46
|
+
minAlpha = a;
|
|
47
|
+
if (a < alphaOpaqueCutoff)
|
|
48
|
+
belowOpaque++;
|
|
49
|
+
if (a < 32)
|
|
50
|
+
veryTransparent++;
|
|
51
|
+
}
|
|
52
|
+
if (minAlpha === 255)
|
|
53
|
+
return "whitebg";
|
|
54
|
+
const belowOpaqueRatio = belowOpaque / total;
|
|
55
|
+
const veryTransparentRatio = veryTransparent / total;
|
|
56
|
+
if (veryTransparentRatio >= 0.0005)
|
|
57
|
+
return "alpha";
|
|
58
|
+
if (belowOpaqueRatio >= 0.01)
|
|
59
|
+
return "alpha";
|
|
60
|
+
return "whitebg";
|
|
61
|
+
}
|
|
62
|
+
function circularMorphology(mask, width, height, radius, op) {
|
|
63
|
+
const dilate = (m, r) => {
|
|
64
|
+
const horizontalDist = new Int32Array(width * height);
|
|
65
|
+
for (let y = 0; y < height; y++) {
|
|
66
|
+
let lastSolid = -r * 2;
|
|
67
|
+
for (let x = 0; x < width; x++) {
|
|
68
|
+
if (m[y * width + x])
|
|
69
|
+
lastSolid = x;
|
|
70
|
+
horizontalDist[y * width + x] = x - lastSolid;
|
|
71
|
+
}
|
|
72
|
+
lastSolid = width + r * 2;
|
|
73
|
+
for (let x = width - 1; x >= 0; x--) {
|
|
74
|
+
if (m[y * width + x])
|
|
75
|
+
lastSolid = x;
|
|
76
|
+
horizontalDist[y * width + x] = Math.min(horizontalDist[y * width + x], lastSolid - x);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const result = new Uint8Array(width * height);
|
|
80
|
+
const r2 = r * r;
|
|
81
|
+
for (let x = 0; x < width; x++) {
|
|
82
|
+
for (let y = 0; y < height; y++) {
|
|
83
|
+
let found = false;
|
|
84
|
+
const minY = Math.max(0, y - r);
|
|
85
|
+
const maxY = Math.min(height - 1, y + r);
|
|
86
|
+
for (let dy = minY; dy <= maxY; dy++) {
|
|
87
|
+
const dY = dy - y;
|
|
88
|
+
const hDist = horizontalDist[dy * width + x];
|
|
89
|
+
if (hDist * hDist + dY * dY <= r2) {
|
|
90
|
+
found = true;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (found)
|
|
95
|
+
result[y * width + x] = 1;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
};
|
|
100
|
+
const erode = (m, r) => {
|
|
101
|
+
const inverted = new Uint8Array(m.length);
|
|
102
|
+
for (let i = 0; i < m.length; i++)
|
|
103
|
+
inverted[i] = m[i] ? 0 : 1;
|
|
104
|
+
const dilatedInverted = dilate(inverted, r);
|
|
105
|
+
const result = new Uint8Array(m.length);
|
|
106
|
+
for (let i = 0; i < m.length; i++)
|
|
107
|
+
result[i] = dilatedInverted[i] ? 0 : 1;
|
|
108
|
+
return result;
|
|
109
|
+
};
|
|
110
|
+
switch (op) {
|
|
111
|
+
case "dilate":
|
|
112
|
+
return dilate(mask, radius);
|
|
113
|
+
case "erode":
|
|
114
|
+
return erode(mask, radius);
|
|
115
|
+
case "closing":
|
|
116
|
+
return erode(dilate(mask, radius), radius);
|
|
117
|
+
case "opening":
|
|
118
|
+
return dilate(erode(mask, radius), radius);
|
|
119
|
+
default:
|
|
120
|
+
return mask;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function fillHoles(mask, width, height) {
|
|
124
|
+
const background = new Uint8Array(width * height);
|
|
125
|
+
const queue = [];
|
|
126
|
+
for (let x = 0; x < width; x++) {
|
|
127
|
+
if (mask[x] === 0) {
|
|
128
|
+
background[x] = 1;
|
|
129
|
+
queue.push(x);
|
|
130
|
+
}
|
|
131
|
+
const lastRowIdx = (height - 1) * width + x;
|
|
132
|
+
if (mask[lastRowIdx] === 0) {
|
|
133
|
+
background[lastRowIdx] = 1;
|
|
134
|
+
queue.push(lastRowIdx);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
for (let y = 1; y < height - 1; y++) {
|
|
138
|
+
const leftIdx = y * width;
|
|
139
|
+
const rightIdx = y * width + (width - 1);
|
|
140
|
+
if (mask[leftIdx] === 0) {
|
|
141
|
+
background[leftIdx] = 1;
|
|
142
|
+
queue.push(leftIdx);
|
|
143
|
+
}
|
|
144
|
+
if (mask[rightIdx] === 0) {
|
|
145
|
+
background[rightIdx] = 1;
|
|
146
|
+
queue.push(rightIdx);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
let head = 0;
|
|
150
|
+
while (head < queue.length) {
|
|
151
|
+
const idx = queue[head++];
|
|
152
|
+
const x = idx % width;
|
|
153
|
+
const y = (idx - x) / width;
|
|
154
|
+
const up = y > 0 ? idx - width : -1;
|
|
155
|
+
const down = y < height - 1 ? idx + width : -1;
|
|
156
|
+
const left = x > 0 ? idx - 1 : -1;
|
|
157
|
+
const right = x < width - 1 ? idx + 1 : -1;
|
|
158
|
+
if (up >= 0 && mask[up] === 0 && background[up] === 0) {
|
|
159
|
+
background[up] = 1;
|
|
160
|
+
queue.push(up);
|
|
161
|
+
}
|
|
162
|
+
if (down >= 0 && mask[down] === 0 && background[down] === 0) {
|
|
163
|
+
background[down] = 1;
|
|
164
|
+
queue.push(down);
|
|
165
|
+
}
|
|
166
|
+
if (left >= 0 && mask[left] === 0 && background[left] === 0) {
|
|
167
|
+
background[left] = 1;
|
|
168
|
+
queue.push(left);
|
|
169
|
+
}
|
|
170
|
+
if (right >= 0 && mask[right] === 0 && background[right] === 0) {
|
|
171
|
+
background[right] = 1;
|
|
172
|
+
queue.push(right);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const filledMask = new Uint8Array(width * height);
|
|
176
|
+
for (let i = 0; i < width * height; i++) {
|
|
177
|
+
filledMask[i] = background[i] === 0 ? 1 : 0;
|
|
178
|
+
}
|
|
179
|
+
return filledMask;
|
|
180
|
+
}
|
|
181
|
+
function countForeground(mask) {
|
|
182
|
+
let c = 0;
|
|
183
|
+
for (let i = 0; i < mask.length; i++)
|
|
184
|
+
c += mask[i] ? 1 : 0;
|
|
185
|
+
return c;
|
|
186
|
+
}
|
|
187
|
+
function isMaskConnected8(mask, width, height) {
|
|
188
|
+
const total = countForeground(mask);
|
|
189
|
+
if (total === 0)
|
|
190
|
+
return true;
|
|
191
|
+
let start = -1;
|
|
192
|
+
for (let i = 0; i < mask.length; i++) {
|
|
193
|
+
if (mask[i]) {
|
|
194
|
+
start = i;
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (start === -1)
|
|
199
|
+
return true;
|
|
200
|
+
const visited = new Uint8Array(mask.length);
|
|
201
|
+
const queue = [start];
|
|
202
|
+
visited[start] = 1;
|
|
203
|
+
let seen = 1;
|
|
204
|
+
let head = 0;
|
|
205
|
+
while (head < queue.length) {
|
|
206
|
+
const idx = queue[head++];
|
|
207
|
+
const x = idx % width;
|
|
208
|
+
const y = (idx - x) / width;
|
|
209
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
210
|
+
const ny = y + dy;
|
|
211
|
+
if (ny < 0 || ny >= height)
|
|
212
|
+
continue;
|
|
213
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
214
|
+
if (dx === 0 && dy === 0)
|
|
215
|
+
continue;
|
|
216
|
+
const nx = x + dx;
|
|
217
|
+
if (nx < 0 || nx >= width)
|
|
218
|
+
continue;
|
|
219
|
+
const nidx = ny * width + nx;
|
|
220
|
+
if (mask[nidx] && !visited[nidx]) {
|
|
221
|
+
visited[nidx] = 1;
|
|
222
|
+
queue.push(nidx);
|
|
223
|
+
seen++;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return seen === total;
|
|
229
|
+
}
|
|
230
|
+
function findMinimalConnectRadius(mask, width, height, maxRadius) {
|
|
231
|
+
if (maxRadius <= 0)
|
|
232
|
+
return 0;
|
|
233
|
+
if (isMaskConnected8(mask, width, height))
|
|
234
|
+
return 0;
|
|
235
|
+
let low = 0;
|
|
236
|
+
let high = 1;
|
|
237
|
+
while (high <= maxRadius) {
|
|
238
|
+
const closed = circularMorphology(mask, width, height, high, "closing");
|
|
239
|
+
if (isMaskConnected8(closed, width, height))
|
|
240
|
+
break;
|
|
241
|
+
high *= 2;
|
|
242
|
+
}
|
|
243
|
+
if (high > maxRadius)
|
|
244
|
+
high = maxRadius;
|
|
245
|
+
if (!isMaskConnected8(circularMorphology(mask, width, height, high, "closing"), width, height)) {
|
|
246
|
+
return high;
|
|
247
|
+
}
|
|
248
|
+
while (low + 1 < high) {
|
|
249
|
+
const mid = Math.floor((low + high) / 2);
|
|
250
|
+
const closed = circularMorphology(mask, width, height, mid, "closing");
|
|
251
|
+
if (isMaskConnected8(closed, width, height)) {
|
|
252
|
+
high = mid;
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
low = mid;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return high;
|
|
259
|
+
}
|
|
260
|
+
function polygonSignedArea(points) {
|
|
261
|
+
if (points.length < 3)
|
|
262
|
+
return 0;
|
|
263
|
+
let sum = 0;
|
|
264
|
+
for (let i = 0; i < points.length; i++) {
|
|
265
|
+
const a = points[i];
|
|
266
|
+
const b = points[(i + 1) % points.length];
|
|
267
|
+
sum += a.x * b.y - b.x * a.y;
|
|
268
|
+
}
|
|
269
|
+
return sum / 2;
|
|
270
|
+
}
|