@pooder/kit 5.4.0 → 6.0.0
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/coordinate.js +74 -0
- package/.test-dist/src/extensions/background.js +547 -0
- package/.test-dist/src/extensions/bridgeSelection.js +20 -0
- package/.test-dist/src/extensions/constraints.js +237 -0
- package/.test-dist/src/extensions/dieline.js +931 -0
- package/.test-dist/src/extensions/dielineShape.js +66 -0
- package/.test-dist/src/extensions/edgeScale.js +12 -0
- package/.test-dist/src/extensions/feature.js +910 -0
- package/.test-dist/src/extensions/featureComplete.js +32 -0
- package/.test-dist/src/extensions/film.js +226 -0
- package/.test-dist/src/extensions/geometry.js +609 -0
- package/.test-dist/src/extensions/image.js +1613 -0
- package/.test-dist/src/extensions/index.js +28 -0
- package/.test-dist/src/extensions/maskOps.js +334 -0
- package/.test-dist/src/extensions/mirror.js +104 -0
- package/.test-dist/src/extensions/ruler.js +442 -0
- package/.test-dist/src/extensions/sceneLayout.js +96 -0
- package/.test-dist/src/extensions/sceneLayoutModel.js +202 -0
- package/.test-dist/src/extensions/sceneVisibility.js +55 -0
- package/.test-dist/src/extensions/size.js +331 -0
- package/.test-dist/src/extensions/tracer.js +709 -0
- package/.test-dist/src/extensions/white-ink.js +1200 -0
- package/.test-dist/src/extensions/wrappedOffsets.js +33 -0
- package/.test-dist/src/index.js +18 -0
- package/.test-dist/src/services/CanvasService.js +1011 -0
- package/.test-dist/src/services/ViewportSystem.js +76 -0
- package/.test-dist/src/services/index.js +25 -0
- package/.test-dist/src/services/renderSpec.js +2 -0
- package/.test-dist/src/services/visibility.js +54 -0
- package/.test-dist/src/units.js +30 -0
- package/.test-dist/tests/run.js +148 -0
- package/CHANGELOG.md +6 -0
- package/dist/index.d.mts +150 -62
- package/dist/index.d.ts +150 -62
- package/dist/index.js +2219 -1714
- package/dist/index.mjs +2226 -1718
- package/package.json +1 -1
- package/src/coordinate.ts +106 -106
- package/src/extensions/background.ts +716 -323
- package/src/extensions/bridgeSelection.ts +17 -17
- package/src/extensions/constraints.ts +322 -322
- package/src/extensions/dieline.ts +1169 -1149
- package/src/extensions/dielineShape.ts +109 -109
- package/src/extensions/edgeScale.ts +19 -19
- package/src/extensions/feature.ts +1140 -1137
- package/src/extensions/featureComplete.ts +46 -46
- package/src/extensions/film.ts +270 -266
- package/src/extensions/geometry.ts +851 -885
- package/src/extensions/image.ts +2007 -2054
- package/src/extensions/index.ts +10 -11
- package/src/extensions/maskOps.ts +283 -283
- package/src/extensions/mirror.ts +128 -128
- package/src/extensions/ruler.ts +664 -654
- package/src/extensions/sceneLayout.ts +140 -140
- package/src/extensions/sceneLayoutModel.ts +364 -364
- package/src/extensions/size.ts +389 -389
- package/src/extensions/tracer.ts +1019 -1019
- package/src/extensions/white-ink.ts +1508 -1575
- package/src/extensions/wrappedOffsets.ts +33 -33
- package/src/index.ts +2 -2
- package/src/services/CanvasService.ts +1286 -832
- package/src/services/ViewportSystem.ts +95 -95
- package/src/services/index.ts +4 -3
- package/src/services/renderSpec.ts +83 -53
- package/src/services/visibility.ts +78 -0
- package/src/units.ts +27 -27
- package/tests/run.ts +253 -118
- package/tsconfig.test.json +15 -15
- package/src/extensions/sceneVisibility.ts +0 -64
package/src/extensions/index.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
export * from "./background";
|
|
2
|
-
export * from "./image";
|
|
3
|
-
export * from "./size";
|
|
4
|
-
export * from "./dieline";
|
|
5
|
-
export * from "./feature";
|
|
6
|
-
export * from "./film";
|
|
7
|
-
export * from "./mirror";
|
|
8
|
-
export * from "./ruler";
|
|
9
|
-
export * from "./white-ink";
|
|
10
|
-
export { SceneLayoutService } from "./sceneLayout";
|
|
11
|
-
export { SceneVisibilityService } from "./sceneVisibility";
|
|
1
|
+
export * from "./background";
|
|
2
|
+
export * from "./image";
|
|
3
|
+
export * from "./size";
|
|
4
|
+
export * from "./dieline";
|
|
5
|
+
export * from "./feature";
|
|
6
|
+
export * from "./film";
|
|
7
|
+
export * from "./mirror";
|
|
8
|
+
export * from "./ruler";
|
|
9
|
+
export * from "./white-ink";
|
|
10
|
+
export { SceneLayoutService } from "./sceneLayout";
|
|
@@ -1,104 +1,104 @@
|
|
|
1
|
-
export type MaskMode = "auto" | "alpha" | "whitebg";
|
|
2
|
-
|
|
3
|
-
export interface CreateMaskOptions {
|
|
4
|
-
threshold: number;
|
|
5
|
-
padding: number;
|
|
6
|
-
paddedWidth: number;
|
|
7
|
-
paddedHeight: number;
|
|
8
|
-
maskMode?: MaskMode;
|
|
9
|
-
whiteThreshold?: number;
|
|
10
|
-
alphaOpaqueCutoff?: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface AlphaAnalysis {
|
|
14
|
-
total: number;
|
|
15
|
-
minAlpha: number;
|
|
16
|
-
belowOpaqueRatio: number;
|
|
17
|
-
veryTransparentRatio: number;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function createMask(
|
|
21
|
-
imageData: ImageData,
|
|
22
|
-
options: CreateMaskOptions,
|
|
23
|
-
): Uint8Array {
|
|
24
|
-
const { width, height, data } = imageData;
|
|
25
|
-
const {
|
|
26
|
-
threshold,
|
|
27
|
-
padding,
|
|
28
|
-
paddedWidth,
|
|
29
|
-
paddedHeight,
|
|
30
|
-
maskMode = "auto",
|
|
31
|
-
whiteThreshold = 240,
|
|
32
|
-
alphaOpaqueCutoff = 250,
|
|
33
|
-
} = options;
|
|
34
|
-
|
|
35
|
-
const resolvedMode =
|
|
36
|
-
maskMode === "auto" ? inferMaskMode(imageData, alphaOpaqueCutoff) : maskMode;
|
|
37
|
-
|
|
38
|
-
const mask = new Uint8Array(paddedWidth * paddedHeight);
|
|
39
|
-
|
|
40
|
-
for (let y = 0; y < height; y++) {
|
|
41
|
-
for (let x = 0; x < width; x++) {
|
|
42
|
-
const srcIdx = (y * width + x) * 4;
|
|
43
|
-
const r = data[srcIdx];
|
|
44
|
-
const g = data[srcIdx + 1];
|
|
45
|
-
const b = data[srcIdx + 2];
|
|
46
|
-
const a = data[srcIdx + 3];
|
|
47
|
-
const destIdx = (y + padding) * paddedWidth + (x + padding);
|
|
48
|
-
|
|
49
|
-
if (resolvedMode === "alpha") {
|
|
50
|
-
if (a > threshold) mask[destIdx] = 1;
|
|
51
|
-
} else {
|
|
52
|
-
if (
|
|
53
|
-
a > threshold &&
|
|
54
|
-
!(r > whiteThreshold && g > whiteThreshold && b > whiteThreshold)
|
|
55
|
-
) {
|
|
56
|
-
mask[destIdx] = 1;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return mask;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function inferMaskMode(
|
|
66
|
-
imageData: ImageData,
|
|
67
|
-
alphaOpaqueCutoff: number,
|
|
68
|
-
): MaskMode {
|
|
69
|
-
const analysis = analyzeAlpha(imageData, alphaOpaqueCutoff);
|
|
70
|
-
if (analysis.minAlpha === 255) return "whitebg";
|
|
71
|
-
if (analysis.veryTransparentRatio >= 0.0005) return "alpha";
|
|
72
|
-
if (analysis.belowOpaqueRatio >= 0.01) return "alpha";
|
|
73
|
-
return "whitebg";
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function analyzeAlpha(
|
|
77
|
-
imageData: ImageData,
|
|
78
|
-
alphaOpaqueCutoff: number,
|
|
79
|
-
): AlphaAnalysis {
|
|
80
|
-
const { data } = imageData;
|
|
81
|
-
const total = data.length / 4;
|
|
82
|
-
|
|
83
|
-
let belowOpaque = 0;
|
|
84
|
-
let veryTransparent = 0;
|
|
85
|
-
let minAlpha = 255;
|
|
86
|
-
|
|
87
|
-
for (let i = 3; i < data.length; i += 4) {
|
|
88
|
-
const a = data[i];
|
|
89
|
-
if (a < minAlpha) minAlpha = a;
|
|
90
|
-
if (a < alphaOpaqueCutoff) belowOpaque++;
|
|
91
|
-
if (a < 32) veryTransparent++;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
total,
|
|
96
|
-
minAlpha,
|
|
97
|
-
belowOpaqueRatio: belowOpaque / total,
|
|
98
|
-
veryTransparentRatio: veryTransparent / total,
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
1
|
+
export type MaskMode = "auto" | "alpha" | "whitebg";
|
|
2
|
+
|
|
3
|
+
export interface CreateMaskOptions {
|
|
4
|
+
threshold: number;
|
|
5
|
+
padding: number;
|
|
6
|
+
paddedWidth: number;
|
|
7
|
+
paddedHeight: number;
|
|
8
|
+
maskMode?: MaskMode;
|
|
9
|
+
whiteThreshold?: number;
|
|
10
|
+
alphaOpaqueCutoff?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface AlphaAnalysis {
|
|
14
|
+
total: number;
|
|
15
|
+
minAlpha: number;
|
|
16
|
+
belowOpaqueRatio: number;
|
|
17
|
+
veryTransparentRatio: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createMask(
|
|
21
|
+
imageData: ImageData,
|
|
22
|
+
options: CreateMaskOptions,
|
|
23
|
+
): Uint8Array {
|
|
24
|
+
const { width, height, data } = imageData;
|
|
25
|
+
const {
|
|
26
|
+
threshold,
|
|
27
|
+
padding,
|
|
28
|
+
paddedWidth,
|
|
29
|
+
paddedHeight,
|
|
30
|
+
maskMode = "auto",
|
|
31
|
+
whiteThreshold = 240,
|
|
32
|
+
alphaOpaqueCutoff = 250,
|
|
33
|
+
} = options;
|
|
34
|
+
|
|
35
|
+
const resolvedMode =
|
|
36
|
+
maskMode === "auto" ? inferMaskMode(imageData, alphaOpaqueCutoff) : maskMode;
|
|
37
|
+
|
|
38
|
+
const mask = new Uint8Array(paddedWidth * paddedHeight);
|
|
39
|
+
|
|
40
|
+
for (let y = 0; y < height; y++) {
|
|
41
|
+
for (let x = 0; x < width; x++) {
|
|
42
|
+
const srcIdx = (y * width + x) * 4;
|
|
43
|
+
const r = data[srcIdx];
|
|
44
|
+
const g = data[srcIdx + 1];
|
|
45
|
+
const b = data[srcIdx + 2];
|
|
46
|
+
const a = data[srcIdx + 3];
|
|
47
|
+
const destIdx = (y + padding) * paddedWidth + (x + padding);
|
|
48
|
+
|
|
49
|
+
if (resolvedMode === "alpha") {
|
|
50
|
+
if (a > threshold) mask[destIdx] = 1;
|
|
51
|
+
} else {
|
|
52
|
+
if (
|
|
53
|
+
a > threshold &&
|
|
54
|
+
!(r > whiteThreshold && g > whiteThreshold && b > whiteThreshold)
|
|
55
|
+
) {
|
|
56
|
+
mask[destIdx] = 1;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return mask;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function inferMaskMode(
|
|
66
|
+
imageData: ImageData,
|
|
67
|
+
alphaOpaqueCutoff: number,
|
|
68
|
+
): MaskMode {
|
|
69
|
+
const analysis = analyzeAlpha(imageData, alphaOpaqueCutoff);
|
|
70
|
+
if (analysis.minAlpha === 255) return "whitebg";
|
|
71
|
+
if (analysis.veryTransparentRatio >= 0.0005) return "alpha";
|
|
72
|
+
if (analysis.belowOpaqueRatio >= 0.01) return "alpha";
|
|
73
|
+
return "whitebg";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function analyzeAlpha(
|
|
77
|
+
imageData: ImageData,
|
|
78
|
+
alphaOpaqueCutoff: number,
|
|
79
|
+
): AlphaAnalysis {
|
|
80
|
+
const { data } = imageData;
|
|
81
|
+
const total = data.length / 4;
|
|
82
|
+
|
|
83
|
+
let belowOpaque = 0;
|
|
84
|
+
let veryTransparent = 0;
|
|
85
|
+
let minAlpha = 255;
|
|
86
|
+
|
|
87
|
+
for (let i = 3; i < data.length; i += 4) {
|
|
88
|
+
const a = data[i];
|
|
89
|
+
if (a < minAlpha) minAlpha = a;
|
|
90
|
+
if (a < alphaOpaqueCutoff) belowOpaque++;
|
|
91
|
+
if (a < 32) veryTransparent++;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
total,
|
|
96
|
+
minAlpha,
|
|
97
|
+
belowOpaqueRatio: belowOpaque / total,
|
|
98
|
+
veryTransparentRatio: veryTransparent / total,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
102
|
export function circularMorphology(
|
|
103
103
|
mask: Uint8Array,
|
|
104
104
|
width: number,
|
|
@@ -126,8 +126,8 @@ export function circularMorphology(
|
|
|
126
126
|
horizontalDist[y * width + x] = Math.min(
|
|
127
127
|
horizontalDist[y * width + x],
|
|
128
128
|
lastSolid - x,
|
|
129
|
-
);
|
|
130
|
-
}
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
const result = new Uint8Array(width * height);
|
|
@@ -142,11 +142,11 @@ export function circularMorphology(
|
|
|
142
142
|
const hDist = horizontalDist[dy * width + x];
|
|
143
143
|
if (hDist * hDist + dY * dY <= r2) {
|
|
144
144
|
found = true;
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
if (found) result[y * width + x] = 1;
|
|
149
|
-
}
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (found) result[y * width + x] = 1;
|
|
149
|
+
}
|
|
150
150
|
}
|
|
151
151
|
return result;
|
|
152
152
|
};
|
|
@@ -233,178 +233,178 @@ export function circularMorphology(
|
|
|
233
233
|
return mask;
|
|
234
234
|
}
|
|
235
235
|
}
|
|
236
|
-
|
|
237
|
-
export function fillHoles(
|
|
238
|
-
mask: Uint8Array,
|
|
239
|
-
width: number,
|
|
240
|
-
height: number,
|
|
241
|
-
): Uint8Array {
|
|
242
|
-
const background = new Uint8Array(width * height);
|
|
243
|
-
const queue: number[] = [];
|
|
244
|
-
|
|
245
|
-
for (let x = 0; x < width; x++) {
|
|
246
|
-
if (mask[x] === 0) {
|
|
247
|
-
background[x] = 1;
|
|
248
|
-
queue.push(x);
|
|
249
|
-
}
|
|
250
|
-
const lastRowIdx = (height - 1) * width + x;
|
|
251
|
-
if (mask[lastRowIdx] === 0) {
|
|
252
|
-
background[lastRowIdx] = 1;
|
|
253
|
-
queue.push(lastRowIdx);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
for (let y = 1; y < height - 1; y++) {
|
|
257
|
-
const leftIdx = y * width;
|
|
258
|
-
const rightIdx = y * width + (width - 1);
|
|
259
|
-
if (mask[leftIdx] === 0) {
|
|
260
|
-
background[leftIdx] = 1;
|
|
261
|
-
queue.push(leftIdx);
|
|
262
|
-
}
|
|
263
|
-
if (mask[rightIdx] === 0) {
|
|
264
|
-
background[rightIdx] = 1;
|
|
265
|
-
queue.push(rightIdx);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
let head = 0;
|
|
270
|
-
while (head < queue.length) {
|
|
271
|
-
const idx = queue[head++];
|
|
272
|
-
const x = idx % width;
|
|
273
|
-
const y = (idx - x) / width;
|
|
274
|
-
|
|
275
|
-
const up = y > 0 ? idx - width : -1;
|
|
276
|
-
const down = y < height - 1 ? idx + width : -1;
|
|
277
|
-
const left = x > 0 ? idx - 1 : -1;
|
|
278
|
-
const right = x < width - 1 ? idx + 1 : -1;
|
|
279
|
-
|
|
280
|
-
if (up >= 0 && mask[up] === 0 && background[up] === 0) {
|
|
281
|
-
background[up] = 1;
|
|
282
|
-
queue.push(up);
|
|
283
|
-
}
|
|
284
|
-
if (down >= 0 && mask[down] === 0 && background[down] === 0) {
|
|
285
|
-
background[down] = 1;
|
|
286
|
-
queue.push(down);
|
|
287
|
-
}
|
|
288
|
-
if (left >= 0 && mask[left] === 0 && background[left] === 0) {
|
|
289
|
-
background[left] = 1;
|
|
290
|
-
queue.push(left);
|
|
291
|
-
}
|
|
292
|
-
if (right >= 0 && mask[right] === 0 && background[right] === 0) {
|
|
293
|
-
background[right] = 1;
|
|
294
|
-
queue.push(right);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
const filledMask = new Uint8Array(width * height);
|
|
299
|
-
for (let i = 0; i < width * height; i++) {
|
|
300
|
-
filledMask[i] = background[i] === 0 ? 1 : 0;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
return filledMask;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
export function countForeground(mask: Uint8Array): number {
|
|
307
|
-
let c = 0;
|
|
308
|
-
for (let i = 0; i < mask.length; i++) c += mask[i] ? 1 : 0;
|
|
309
|
-
return c;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
export function isMaskConnected8(
|
|
313
|
-
mask: Uint8Array,
|
|
314
|
-
width: number,
|
|
315
|
-
height: number,
|
|
316
|
-
): boolean {
|
|
317
|
-
const total = countForeground(mask);
|
|
318
|
-
if (total === 0) return true;
|
|
319
|
-
|
|
320
|
-
let start = -1;
|
|
321
|
-
for (let i = 0; i < mask.length; i++) {
|
|
322
|
-
if (mask[i]) {
|
|
323
|
-
start = i;
|
|
324
|
-
break;
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
if (start === -1) return true;
|
|
328
|
-
|
|
329
|
-
const visited = new Uint8Array(mask.length);
|
|
330
|
-
const queue: number[] = [start];
|
|
331
|
-
visited[start] = 1;
|
|
332
|
-
let seen = 1;
|
|
333
|
-
|
|
334
|
-
let head = 0;
|
|
335
|
-
while (head < queue.length) {
|
|
336
|
-
const idx = queue[head++];
|
|
337
|
-
const x = idx % width;
|
|
338
|
-
const y = (idx - x) / width;
|
|
339
|
-
|
|
340
|
-
for (let dy = -1; dy <= 1; dy++) {
|
|
341
|
-
const ny = y + dy;
|
|
342
|
-
if (ny < 0 || ny >= height) continue;
|
|
343
|
-
for (let dx = -1; dx <= 1; dx++) {
|
|
344
|
-
if (dx === 0 && dy === 0) continue;
|
|
345
|
-
const nx = x + dx;
|
|
346
|
-
if (nx < 0 || nx >= width) continue;
|
|
347
|
-
const nidx = ny * width + nx;
|
|
348
|
-
if (mask[nidx] && !visited[nidx]) {
|
|
349
|
-
visited[nidx] = 1;
|
|
350
|
-
queue.push(nidx);
|
|
351
|
-
seen++;
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
return seen === total;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
export function findMinimalConnectRadius(
|
|
361
|
-
mask: Uint8Array,
|
|
362
|
-
width: number,
|
|
363
|
-
height: number,
|
|
364
|
-
maxRadius: number,
|
|
365
|
-
): number {
|
|
366
|
-
if (maxRadius <= 0) return 0;
|
|
367
|
-
if (isMaskConnected8(mask, width, height)) return 0;
|
|
368
|
-
|
|
369
|
-
let low = 0;
|
|
370
|
-
let high = 1;
|
|
371
|
-
while (high <= maxRadius) {
|
|
372
|
-
const closed = circularMorphology(mask, width, height, high, "closing");
|
|
373
|
-
if (isMaskConnected8(closed, width, height)) break;
|
|
374
|
-
high *= 2;
|
|
375
|
-
}
|
|
376
|
-
if (high > maxRadius) high = maxRadius;
|
|
377
|
-
|
|
378
|
-
if (
|
|
379
|
-
!isMaskConnected8(
|
|
380
|
-
circularMorphology(mask, width, height, high, "closing"),
|
|
381
|
-
width,
|
|
382
|
-
height,
|
|
383
|
-
)
|
|
384
|
-
) {
|
|
385
|
-
return high;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
while (low + 1 < high) {
|
|
389
|
-
const mid = Math.floor((low + high) / 2);
|
|
390
|
-
const closed = circularMorphology(mask, width, height, mid, "closing");
|
|
391
|
-
if (isMaskConnected8(closed, width, height)) {
|
|
392
|
-
high = mid;
|
|
393
|
-
} else {
|
|
394
|
-
low = mid;
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
return high;
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
export function polygonSignedArea(points: Array<{ x: number; y: number }>): number {
|
|
402
|
-
if (points.length < 3) return 0;
|
|
403
|
-
let sum = 0;
|
|
404
|
-
for (let i = 0; i < points.length; i++) {
|
|
405
|
-
const a = points[i];
|
|
406
|
-
const b = points[(i + 1) % points.length];
|
|
407
|
-
sum += a.x * b.y - b.x * a.y;
|
|
408
|
-
}
|
|
409
|
-
return sum / 2;
|
|
410
|
-
}
|
|
236
|
+
|
|
237
|
+
export function fillHoles(
|
|
238
|
+
mask: Uint8Array,
|
|
239
|
+
width: number,
|
|
240
|
+
height: number,
|
|
241
|
+
): Uint8Array {
|
|
242
|
+
const background = new Uint8Array(width * height);
|
|
243
|
+
const queue: number[] = [];
|
|
244
|
+
|
|
245
|
+
for (let x = 0; x < width; x++) {
|
|
246
|
+
if (mask[x] === 0) {
|
|
247
|
+
background[x] = 1;
|
|
248
|
+
queue.push(x);
|
|
249
|
+
}
|
|
250
|
+
const lastRowIdx = (height - 1) * width + x;
|
|
251
|
+
if (mask[lastRowIdx] === 0) {
|
|
252
|
+
background[lastRowIdx] = 1;
|
|
253
|
+
queue.push(lastRowIdx);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
for (let y = 1; y < height - 1; y++) {
|
|
257
|
+
const leftIdx = y * width;
|
|
258
|
+
const rightIdx = y * width + (width - 1);
|
|
259
|
+
if (mask[leftIdx] === 0) {
|
|
260
|
+
background[leftIdx] = 1;
|
|
261
|
+
queue.push(leftIdx);
|
|
262
|
+
}
|
|
263
|
+
if (mask[rightIdx] === 0) {
|
|
264
|
+
background[rightIdx] = 1;
|
|
265
|
+
queue.push(rightIdx);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
let head = 0;
|
|
270
|
+
while (head < queue.length) {
|
|
271
|
+
const idx = queue[head++];
|
|
272
|
+
const x = idx % width;
|
|
273
|
+
const y = (idx - x) / width;
|
|
274
|
+
|
|
275
|
+
const up = y > 0 ? idx - width : -1;
|
|
276
|
+
const down = y < height - 1 ? idx + width : -1;
|
|
277
|
+
const left = x > 0 ? idx - 1 : -1;
|
|
278
|
+
const right = x < width - 1 ? idx + 1 : -1;
|
|
279
|
+
|
|
280
|
+
if (up >= 0 && mask[up] === 0 && background[up] === 0) {
|
|
281
|
+
background[up] = 1;
|
|
282
|
+
queue.push(up);
|
|
283
|
+
}
|
|
284
|
+
if (down >= 0 && mask[down] === 0 && background[down] === 0) {
|
|
285
|
+
background[down] = 1;
|
|
286
|
+
queue.push(down);
|
|
287
|
+
}
|
|
288
|
+
if (left >= 0 && mask[left] === 0 && background[left] === 0) {
|
|
289
|
+
background[left] = 1;
|
|
290
|
+
queue.push(left);
|
|
291
|
+
}
|
|
292
|
+
if (right >= 0 && mask[right] === 0 && background[right] === 0) {
|
|
293
|
+
background[right] = 1;
|
|
294
|
+
queue.push(right);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const filledMask = new Uint8Array(width * height);
|
|
299
|
+
for (let i = 0; i < width * height; i++) {
|
|
300
|
+
filledMask[i] = background[i] === 0 ? 1 : 0;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return filledMask;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function countForeground(mask: Uint8Array): number {
|
|
307
|
+
let c = 0;
|
|
308
|
+
for (let i = 0; i < mask.length; i++) c += mask[i] ? 1 : 0;
|
|
309
|
+
return c;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export function isMaskConnected8(
|
|
313
|
+
mask: Uint8Array,
|
|
314
|
+
width: number,
|
|
315
|
+
height: number,
|
|
316
|
+
): boolean {
|
|
317
|
+
const total = countForeground(mask);
|
|
318
|
+
if (total === 0) return true;
|
|
319
|
+
|
|
320
|
+
let start = -1;
|
|
321
|
+
for (let i = 0; i < mask.length; i++) {
|
|
322
|
+
if (mask[i]) {
|
|
323
|
+
start = i;
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (start === -1) return true;
|
|
328
|
+
|
|
329
|
+
const visited = new Uint8Array(mask.length);
|
|
330
|
+
const queue: number[] = [start];
|
|
331
|
+
visited[start] = 1;
|
|
332
|
+
let seen = 1;
|
|
333
|
+
|
|
334
|
+
let head = 0;
|
|
335
|
+
while (head < queue.length) {
|
|
336
|
+
const idx = queue[head++];
|
|
337
|
+
const x = idx % width;
|
|
338
|
+
const y = (idx - x) / width;
|
|
339
|
+
|
|
340
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
341
|
+
const ny = y + dy;
|
|
342
|
+
if (ny < 0 || ny >= height) continue;
|
|
343
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
344
|
+
if (dx === 0 && dy === 0) continue;
|
|
345
|
+
const nx = x + dx;
|
|
346
|
+
if (nx < 0 || nx >= width) continue;
|
|
347
|
+
const nidx = ny * width + nx;
|
|
348
|
+
if (mask[nidx] && !visited[nidx]) {
|
|
349
|
+
visited[nidx] = 1;
|
|
350
|
+
queue.push(nidx);
|
|
351
|
+
seen++;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return seen === total;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export function findMinimalConnectRadius(
|
|
361
|
+
mask: Uint8Array,
|
|
362
|
+
width: number,
|
|
363
|
+
height: number,
|
|
364
|
+
maxRadius: number,
|
|
365
|
+
): number {
|
|
366
|
+
if (maxRadius <= 0) return 0;
|
|
367
|
+
if (isMaskConnected8(mask, width, height)) return 0;
|
|
368
|
+
|
|
369
|
+
let low = 0;
|
|
370
|
+
let high = 1;
|
|
371
|
+
while (high <= maxRadius) {
|
|
372
|
+
const closed = circularMorphology(mask, width, height, high, "closing");
|
|
373
|
+
if (isMaskConnected8(closed, width, height)) break;
|
|
374
|
+
high *= 2;
|
|
375
|
+
}
|
|
376
|
+
if (high > maxRadius) high = maxRadius;
|
|
377
|
+
|
|
378
|
+
if (
|
|
379
|
+
!isMaskConnected8(
|
|
380
|
+
circularMorphology(mask, width, height, high, "closing"),
|
|
381
|
+
width,
|
|
382
|
+
height,
|
|
383
|
+
)
|
|
384
|
+
) {
|
|
385
|
+
return high;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
while (low + 1 < high) {
|
|
389
|
+
const mid = Math.floor((low + high) / 2);
|
|
390
|
+
const closed = circularMorphology(mask, width, height, mid, "closing");
|
|
391
|
+
if (isMaskConnected8(closed, width, height)) {
|
|
392
|
+
high = mid;
|
|
393
|
+
} else {
|
|
394
|
+
low = mid;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return high;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function polygonSignedArea(points: Array<{ x: number; y: number }>): number {
|
|
402
|
+
if (points.length < 3) return 0;
|
|
403
|
+
let sum = 0;
|
|
404
|
+
for (let i = 0; i < points.length; i++) {
|
|
405
|
+
const a = points[i];
|
|
406
|
+
const b = points[(i + 1) % points.length];
|
|
407
|
+
sum += a.x * b.y - b.x * a.y;
|
|
408
|
+
}
|
|
409
|
+
return sum / 2;
|
|
410
|
+
}
|