@pooder/kit 5.3.1 → 5.4.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.
Files changed (90) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/index.d.mts +243 -36
  3. package/dist/index.d.ts +243 -36
  4. package/dist/index.js +2278 -1041
  5. package/dist/index.mjs +2278 -1041
  6. package/package.json +1 -1
  7. package/src/coordinate.ts +106 -106
  8. package/src/extensions/background.ts +323 -230
  9. package/src/extensions/bridgeSelection.ts +17 -17
  10. package/src/extensions/constraints.ts +322 -322
  11. package/src/extensions/dieline.ts +1149 -1076
  12. package/src/extensions/dielineShape.ts +109 -0
  13. package/src/extensions/edgeScale.ts +19 -19
  14. package/src/extensions/feature.ts +1137 -1021
  15. package/src/extensions/featureComplete.ts +46 -46
  16. package/src/extensions/film.ts +266 -194
  17. package/src/extensions/geometry.ts +885 -752
  18. package/src/extensions/image.ts +2054 -1926
  19. package/src/extensions/index.ts +11 -11
  20. package/src/extensions/maskOps.ts +283 -283
  21. package/src/extensions/mirror.ts +128 -128
  22. package/src/extensions/ruler.ts +654 -451
  23. package/src/extensions/sceneLayout.ts +140 -140
  24. package/src/extensions/sceneLayoutModel.ts +364 -352
  25. package/src/extensions/sceneVisibility.ts +64 -71
  26. package/src/extensions/size.ts +389 -389
  27. package/src/extensions/tracer.ts +1019 -1019
  28. package/src/extensions/white-ink.ts +1567 -1514
  29. package/src/extensions/wrappedOffsets.ts +33 -33
  30. package/src/index.ts +2 -2
  31. package/src/services/CanvasService.ts +832 -300
  32. package/src/services/ViewportSystem.ts +95 -95
  33. package/src/services/index.ts +3 -3
  34. package/src/services/renderSpec.ts +53 -18
  35. package/src/units.ts +27 -27
  36. package/tests/run.ts +118 -118
  37. package/tsconfig.test.json +15 -15
  38. package/.test-dist/src/CanvasService.js +0 -249
  39. package/.test-dist/src/ViewportSystem.js +0 -75
  40. package/.test-dist/src/background.js +0 -203
  41. package/.test-dist/src/bridgeSelection.js +0 -20
  42. package/.test-dist/src/constraints.js +0 -237
  43. package/.test-dist/src/coordinate.js +0 -74
  44. package/.test-dist/src/dieline.js +0 -818
  45. package/.test-dist/src/edgeScale.js +0 -12
  46. package/.test-dist/src/extensions/background.js +0 -203
  47. package/.test-dist/src/extensions/bridgeSelection.js +0 -20
  48. package/.test-dist/src/extensions/constraints.js +0 -237
  49. package/.test-dist/src/extensions/dieline.js +0 -828
  50. package/.test-dist/src/extensions/edgeScale.js +0 -12
  51. package/.test-dist/src/extensions/feature.js +0 -825
  52. package/.test-dist/src/extensions/featureComplete.js +0 -32
  53. package/.test-dist/src/extensions/film.js +0 -167
  54. package/.test-dist/src/extensions/geometry.js +0 -545
  55. package/.test-dist/src/extensions/image.js +0 -1529
  56. package/.test-dist/src/extensions/index.js +0 -30
  57. package/.test-dist/src/extensions/maskOps.js +0 -279
  58. package/.test-dist/src/extensions/mirror.js +0 -104
  59. package/.test-dist/src/extensions/ruler.js +0 -345
  60. package/.test-dist/src/extensions/sceneLayout.js +0 -96
  61. package/.test-dist/src/extensions/sceneLayoutModel.js +0 -196
  62. package/.test-dist/src/extensions/sceneVisibility.js +0 -62
  63. package/.test-dist/src/extensions/size.js +0 -331
  64. package/.test-dist/src/extensions/tracer.js +0 -538
  65. package/.test-dist/src/extensions/white-ink.js +0 -1190
  66. package/.test-dist/src/extensions/wrappedOffsets.js +0 -33
  67. package/.test-dist/src/feature.js +0 -826
  68. package/.test-dist/src/featureComplete.js +0 -32
  69. package/.test-dist/src/film.js +0 -167
  70. package/.test-dist/src/geometry.js +0 -506
  71. package/.test-dist/src/image.js +0 -1250
  72. package/.test-dist/src/index.js +0 -18
  73. package/.test-dist/src/maskOps.js +0 -270
  74. package/.test-dist/src/mirror.js +0 -104
  75. package/.test-dist/src/renderSpec.js +0 -2
  76. package/.test-dist/src/ruler.js +0 -343
  77. package/.test-dist/src/sceneLayout.js +0 -99
  78. package/.test-dist/src/sceneLayoutModel.js +0 -196
  79. package/.test-dist/src/sceneView.js +0 -40
  80. package/.test-dist/src/sceneVisibility.js +0 -42
  81. package/.test-dist/src/services/CanvasService.js +0 -249
  82. package/.test-dist/src/services/ViewportSystem.js +0 -76
  83. package/.test-dist/src/services/index.js +0 -24
  84. package/.test-dist/src/services/renderSpec.js +0 -2
  85. package/.test-dist/src/size.js +0 -332
  86. package/.test-dist/src/tracer.js +0 -544
  87. package/.test-dist/src/units.js +0 -30
  88. package/.test-dist/src/white-ink.js +0 -829
  89. package/.test-dist/src/wrappedOffsets.js +0 -33
  90. package/.test-dist/tests/run.js +0 -94
@@ -1,11 +1,11 @@
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";
11
+ export { SceneVisibilityService } from "./sceneVisibility";
@@ -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
+ }