@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.
Files changed (60) hide show
  1. package/.test-dist/src/CanvasService.js +249 -249
  2. package/.test-dist/src/ViewportSystem.js +75 -75
  3. package/.test-dist/src/background.js +203 -203
  4. package/.test-dist/src/bridgeSelection.js +20 -20
  5. package/.test-dist/src/constraints.js +237 -237
  6. package/.test-dist/src/dieline.js +818 -818
  7. package/.test-dist/src/edgeScale.js +12 -12
  8. package/.test-dist/src/feature.js +826 -826
  9. package/.test-dist/src/featureComplete.js +32 -32
  10. package/.test-dist/src/film.js +167 -167
  11. package/.test-dist/src/geometry.js +506 -506
  12. package/.test-dist/src/image.js +1250 -1250
  13. package/.test-dist/src/maskOps.js +270 -270
  14. package/.test-dist/src/mirror.js +104 -104
  15. package/.test-dist/src/renderSpec.js +2 -2
  16. package/.test-dist/src/ruler.js +343 -343
  17. package/.test-dist/src/sceneLayout.js +99 -99
  18. package/.test-dist/src/sceneLayoutModel.js +196 -196
  19. package/.test-dist/src/sceneView.js +40 -40
  20. package/.test-dist/src/sceneVisibility.js +42 -42
  21. package/.test-dist/src/size.js +332 -332
  22. package/.test-dist/src/tracer.js +544 -544
  23. package/.test-dist/src/white-ink.js +829 -829
  24. package/.test-dist/src/wrappedOffsets.js +33 -33
  25. package/CHANGELOG.md +6 -0
  26. package/dist/index.d.mts +6 -0
  27. package/dist/index.d.ts +6 -0
  28. package/dist/index.js +108 -20
  29. package/dist/index.mjs +108 -20
  30. package/package.json +1 -1
  31. package/src/coordinate.ts +106 -106
  32. package/src/extensions/background.ts +230 -230
  33. package/src/extensions/bridgeSelection.ts +17 -17
  34. package/src/extensions/constraints.ts +322 -322
  35. package/src/extensions/dieline.ts +46 -0
  36. package/src/extensions/edgeScale.ts +19 -19
  37. package/src/extensions/feature.ts +1021 -1021
  38. package/src/extensions/featureComplete.ts +46 -46
  39. package/src/extensions/film.ts +194 -194
  40. package/src/extensions/geometry.ts +752 -719
  41. package/src/extensions/image.ts +1926 -1924
  42. package/src/extensions/index.ts +11 -11
  43. package/src/extensions/maskOps.ts +283 -283
  44. package/src/extensions/mirror.ts +128 -128
  45. package/src/extensions/ruler.ts +451 -451
  46. package/src/extensions/sceneLayout.ts +140 -140
  47. package/src/extensions/sceneLayoutModel.ts +352 -342
  48. package/src/extensions/sceneVisibility.ts +71 -71
  49. package/src/extensions/size.ts +389 -389
  50. package/src/extensions/tracer.ts +58 -19
  51. package/src/extensions/white-ink.ts +1400 -1400
  52. package/src/extensions/wrappedOffsets.ts +33 -33
  53. package/src/index.ts +2 -2
  54. package/src/services/CanvasService.ts +300 -300
  55. package/src/services/ViewportSystem.ts +95 -95
  56. package/src/services/index.ts +3 -3
  57. package/src/services/renderSpec.ts +18 -18
  58. package/src/units.ts +27 -27
  59. package/tests/run.ts +118 -118
  60. package/tsconfig.test.json +15 -15
@@ -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
+ }