@pooder/kit 4.3.1 → 5.0.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 -0
- package/.test-dist/src/ViewportSystem.js +75 -0
- package/.test-dist/src/background.js +203 -0
- package/.test-dist/src/bridgeSelection.js +20 -0
- package/.test-dist/src/constraints.js +237 -0
- package/.test-dist/src/coordinate.js +74 -0
- package/.test-dist/src/dieline.js +818 -0
- package/.test-dist/src/edgeScale.js +12 -0
- package/.test-dist/src/feature.js +754 -0
- package/.test-dist/src/featureComplete.js +32 -0
- package/.test-dist/src/film.js +167 -0
- package/.test-dist/src/geometry.js +506 -0
- package/.test-dist/src/image.js +1234 -0
- package/.test-dist/src/index.js +35 -0
- package/.test-dist/src/maskOps.js +270 -0
- package/.test-dist/src/mirror.js +104 -0
- package/.test-dist/src/renderSpec.js +2 -0
- package/.test-dist/src/ruler.js +343 -0
- package/.test-dist/src/sceneLayout.js +99 -0
- package/.test-dist/src/sceneLayoutModel.js +196 -0
- package/.test-dist/src/sceneView.js +40 -0
- package/.test-dist/src/sceneVisibility.js +42 -0
- package/.test-dist/src/size.js +332 -0
- package/.test-dist/src/tracer.js +544 -0
- package/.test-dist/src/units.js +30 -0
- package/.test-dist/src/white-ink.js +829 -0
- package/.test-dist/src/wrappedOffsets.js +33 -0
- package/.test-dist/tests/run.js +94 -0
- package/CHANGELOG.md +17 -0
- package/dist/index.d.mts +342 -37
- package/dist/index.d.ts +342 -37
- package/dist/index.js +3679 -865
- package/dist/index.mjs +3673 -868
- package/package.json +2 -2
- package/src/CanvasService.ts +300 -96
- package/src/ViewportSystem.ts +92 -92
- package/src/background.ts +230 -230
- package/src/bridgeSelection.ts +17 -0
- package/src/coordinate.ts +106 -106
- package/src/dieline.ts +1005 -973
- package/src/edgeScale.ts +19 -0
- package/src/feature.ts +83 -30
- package/src/film.ts +194 -194
- package/src/geometry.ts +242 -84
- package/src/image.ts +1582 -512
- package/src/index.ts +14 -10
- package/src/maskOps.ts +326 -0
- package/src/mirror.ts +128 -128
- package/src/renderSpec.ts +18 -0
- package/src/ruler.ts +449 -508
- package/src/sceneLayout.ts +121 -0
- package/src/sceneLayoutModel.ts +335 -0
- package/src/sceneVisibility.ts +49 -0
- package/src/size.ts +379 -0
- package/src/tracer.ts +719 -570
- package/src/units.ts +27 -27
- package/src/white-ink.ts +1018 -373
- package/src/wrappedOffsets.ts +33 -0
- package/tests/run.ts +118 -0
- package/tsconfig.test.json +15 -15
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Image Tracer Utility
|
|
4
|
+
* Converts raster images (URL/Base64) to SVG Path Data using Marching Squares algorithm.
|
|
5
|
+
*/
|
|
6
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
7
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
8
|
+
};
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.ImageTracer = void 0;
|
|
11
|
+
const paper_1 = __importDefault(require("paper"));
|
|
12
|
+
const maskOps_1 = require("./maskOps");
|
|
13
|
+
class ImageTracer {
|
|
14
|
+
/**
|
|
15
|
+
* Main entry point: Traces an image URL to an SVG path string.
|
|
16
|
+
* @param imageUrl The URL or Base64 string of the image.
|
|
17
|
+
* @param options Configuration options.
|
|
18
|
+
*/
|
|
19
|
+
static async trace(imageUrl, options = {}) {
|
|
20
|
+
const { pathData } = await this.traceWithBounds(imageUrl, options);
|
|
21
|
+
return pathData;
|
|
22
|
+
}
|
|
23
|
+
static async traceWithBounds(imageUrl, options = {}) {
|
|
24
|
+
const img = await this.loadImage(imageUrl);
|
|
25
|
+
const width = img.width;
|
|
26
|
+
const height = img.height;
|
|
27
|
+
const debug = options.debug === true;
|
|
28
|
+
const debugLog = (message, payload) => {
|
|
29
|
+
if (!debug)
|
|
30
|
+
return;
|
|
31
|
+
if (payload) {
|
|
32
|
+
console.info(`[ImageTracer] ${message}`, payload);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
console.info(`[ImageTracer] ${message}`);
|
|
36
|
+
};
|
|
37
|
+
// 1. Draw to canvas and get pixel data
|
|
38
|
+
const canvas = document.createElement("canvas");
|
|
39
|
+
canvas.width = width;
|
|
40
|
+
canvas.height = height;
|
|
41
|
+
const ctx = canvas.getContext("2d");
|
|
42
|
+
if (!ctx)
|
|
43
|
+
throw new Error("Could not get 2D context");
|
|
44
|
+
ctx.drawImage(img, 0, 0);
|
|
45
|
+
const imageData = ctx.getImageData(0, 0, width, height);
|
|
46
|
+
// 2. Morphology processing
|
|
47
|
+
const threshold = options.threshold ?? 10;
|
|
48
|
+
// Adaptive radius: 3% of the image's largest dimension, at least 5px
|
|
49
|
+
const adaptiveRadius = Math.max(5, Math.floor(Math.max(width, height) * 0.02));
|
|
50
|
+
const radius = options.morphologyRadius ?? adaptiveRadius;
|
|
51
|
+
const expand = options.expand ?? 0;
|
|
52
|
+
const noChannels = options.noChannels !== false;
|
|
53
|
+
debugLog("traceWithBounds:start", {
|
|
54
|
+
width,
|
|
55
|
+
height,
|
|
56
|
+
threshold,
|
|
57
|
+
radius,
|
|
58
|
+
expand,
|
|
59
|
+
noChannels,
|
|
60
|
+
simplifyTolerance: options.simplifyTolerance ?? 2.5,
|
|
61
|
+
smoothing: options.smoothing !== false,
|
|
62
|
+
});
|
|
63
|
+
// Add padding to the processing canvas to avoid edge clipping during dilation
|
|
64
|
+
// Padding should be at least the radius + expansion size
|
|
65
|
+
const padding = radius + expand + 2;
|
|
66
|
+
const paddedWidth = width + padding * 2;
|
|
67
|
+
const paddedHeight = height + padding * 2;
|
|
68
|
+
let mask = (0, maskOps_1.createMask)(imageData, {
|
|
69
|
+
threshold,
|
|
70
|
+
padding,
|
|
71
|
+
paddedWidth,
|
|
72
|
+
paddedHeight,
|
|
73
|
+
maskMode: options.maskMode,
|
|
74
|
+
whiteThreshold: options.whiteThreshold,
|
|
75
|
+
alphaOpaqueCutoff: options.alphaOpaqueCutoff,
|
|
76
|
+
});
|
|
77
|
+
const connectRadiusMax = options.connectRadiusMax ?? Math.max(10, Math.floor(Math.max(width, height) * 0.12));
|
|
78
|
+
const rConnect = (0, maskOps_1.findMinimalConnectRadius)(mask, paddedWidth, paddedHeight, connectRadiusMax);
|
|
79
|
+
if (rConnect > 0) {
|
|
80
|
+
mask = (0, maskOps_1.circularMorphology)(mask, paddedWidth, paddedHeight, rConnect, "closing");
|
|
81
|
+
}
|
|
82
|
+
if (radius > 0) {
|
|
83
|
+
mask = (0, maskOps_1.circularMorphology)(mask, paddedWidth, paddedHeight, radius, "closing");
|
|
84
|
+
}
|
|
85
|
+
if (noChannels) {
|
|
86
|
+
mask = (0, maskOps_1.fillHoles)(mask, paddedWidth, paddedHeight);
|
|
87
|
+
}
|
|
88
|
+
if (radius > 0) {
|
|
89
|
+
const smoothRadius = Math.max(2, Math.floor(radius * 0.3));
|
|
90
|
+
mask = (0, maskOps_1.circularMorphology)(mask, paddedWidth, paddedHeight, smoothRadius, "closing");
|
|
91
|
+
}
|
|
92
|
+
const baseMask = mask;
|
|
93
|
+
const baseContour = this.pickPrimaryContour(this.traceAllContours(baseMask, paddedWidth, paddedHeight));
|
|
94
|
+
if (!baseContour) {
|
|
95
|
+
// Fallback: Return a rectangular outline matching dimensions
|
|
96
|
+
const w = options.scaleToWidth ?? width;
|
|
97
|
+
const h = options.scaleToHeight ?? height;
|
|
98
|
+
debugLog("fallback:no-base-contour", { width: w, height: h });
|
|
99
|
+
return {
|
|
100
|
+
pathData: `M 0 0 L ${w} 0 L ${w} ${h} L 0 ${h} Z`,
|
|
101
|
+
baseBounds: { x: 0, y: 0, width: w, height: h },
|
|
102
|
+
bounds: { x: 0, y: 0, width: w, height: h },
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const baseUnpadded = baseContour.map(p => ({
|
|
106
|
+
x: p.x - padding,
|
|
107
|
+
y: p.y - padding,
|
|
108
|
+
}));
|
|
109
|
+
let baseBounds = this.boundsFromPoints(baseUnpadded);
|
|
110
|
+
let maskExpanded = baseMask;
|
|
111
|
+
if (expand > 0) {
|
|
112
|
+
maskExpanded = (0, maskOps_1.circularMorphology)(baseMask, paddedWidth, paddedHeight, expand, "dilate");
|
|
113
|
+
}
|
|
114
|
+
const expandedContour = this.pickPrimaryContour(this.traceAllContours(maskExpanded, paddedWidth, paddedHeight));
|
|
115
|
+
if (!expandedContour) {
|
|
116
|
+
debugLog("fallback:no-expanded-contour", {
|
|
117
|
+
baseBounds,
|
|
118
|
+
width,
|
|
119
|
+
height,
|
|
120
|
+
expand,
|
|
121
|
+
});
|
|
122
|
+
return {
|
|
123
|
+
pathData: `M 0 0 L ${width} 0 L ${width} ${height} L 0 ${height} Z`,
|
|
124
|
+
baseBounds,
|
|
125
|
+
bounds: baseBounds,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
const expandedUnpadded = expandedContour.map(p => ({
|
|
129
|
+
x: p.x - padding,
|
|
130
|
+
y: p.y - padding,
|
|
131
|
+
}));
|
|
132
|
+
let globalBounds = this.boundsFromPoints(expandedUnpadded);
|
|
133
|
+
// 9. Post-processing (Scale)
|
|
134
|
+
let finalPoints = expandedUnpadded;
|
|
135
|
+
if (options.scaleToWidth && options.scaleToHeight) {
|
|
136
|
+
finalPoints = this.scalePoints(expandedUnpadded, options.scaleToWidth, options.scaleToHeight, globalBounds);
|
|
137
|
+
globalBounds = this.boundsFromPoints(finalPoints);
|
|
138
|
+
const baseScaled = this.scalePoints(baseUnpadded, options.scaleToWidth, options.scaleToHeight, baseBounds);
|
|
139
|
+
baseBounds = this.boundsFromPoints(baseScaled);
|
|
140
|
+
}
|
|
141
|
+
// 10. Simplify and Generate SVG
|
|
142
|
+
const useSmoothing = options.smoothing !== false; // Default true
|
|
143
|
+
debugLog("traceWithBounds:contours", {
|
|
144
|
+
baseBounds,
|
|
145
|
+
expandedBounds: globalBounds,
|
|
146
|
+
expandedDeltaX: globalBounds.width - baseBounds.width,
|
|
147
|
+
expandedDeltaY: globalBounds.height - baseBounds.height,
|
|
148
|
+
useSmoothing,
|
|
149
|
+
});
|
|
150
|
+
if (useSmoothing) {
|
|
151
|
+
return {
|
|
152
|
+
pathData: this.pointsToSVGPaper(finalPoints, options.simplifyTolerance ?? 2.5),
|
|
153
|
+
baseBounds,
|
|
154
|
+
bounds: globalBounds,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
const simplifiedPoints = this.douglasPeucker(finalPoints, options.simplifyTolerance ?? 2.0);
|
|
159
|
+
return {
|
|
160
|
+
pathData: this.pointsToSVG(simplifiedPoints),
|
|
161
|
+
baseBounds,
|
|
162
|
+
bounds: globalBounds,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
static pickPrimaryContour(contours) {
|
|
167
|
+
if (contours.length === 0)
|
|
168
|
+
return null;
|
|
169
|
+
return contours.reduce((best, cur) => {
|
|
170
|
+
if (!best)
|
|
171
|
+
return cur;
|
|
172
|
+
const bestArea = Math.abs((0, maskOps_1.polygonSignedArea)(best));
|
|
173
|
+
const curArea = Math.abs((0, maskOps_1.polygonSignedArea)(cur));
|
|
174
|
+
if (curArea !== bestArea)
|
|
175
|
+
return curArea > bestArea ? cur : best;
|
|
176
|
+
return cur.length > best.length ? cur : best;
|
|
177
|
+
}, contours[0]);
|
|
178
|
+
}
|
|
179
|
+
static boundsFromPoints(points) {
|
|
180
|
+
let minX = Infinity;
|
|
181
|
+
let minY = Infinity;
|
|
182
|
+
let maxX = -Infinity;
|
|
183
|
+
let maxY = -Infinity;
|
|
184
|
+
for (const p of points) {
|
|
185
|
+
if (p.x < minX)
|
|
186
|
+
minX = p.x;
|
|
187
|
+
if (p.y < minY)
|
|
188
|
+
minY = p.y;
|
|
189
|
+
if (p.x > maxX)
|
|
190
|
+
maxX = p.x;
|
|
191
|
+
if (p.y > maxY)
|
|
192
|
+
maxY = p.y;
|
|
193
|
+
}
|
|
194
|
+
if (!Number.isFinite(minX) || !Number.isFinite(minY)) {
|
|
195
|
+
return { x: 0, y: 0, width: 0, height: 0 };
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
x: minX,
|
|
199
|
+
y: minY,
|
|
200
|
+
width: maxX - minX,
|
|
201
|
+
height: maxY - minY,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
static createMask(imageData, threshold, padding, paddedWidth, paddedHeight) {
|
|
205
|
+
const { width, height, data } = imageData;
|
|
206
|
+
const mask = new Uint8Array(paddedWidth * paddedHeight);
|
|
207
|
+
// 1. Detect if the image has transparency (any pixel with alpha < 255)
|
|
208
|
+
let hasTransparency = false;
|
|
209
|
+
for (let i = 3; i < data.length; i += 4) {
|
|
210
|
+
if (data[i] < 255) {
|
|
211
|
+
hasTransparency = true;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
// 2. Binarize based on alpha or luminance
|
|
216
|
+
for (let y = 0; y < height; y++) {
|
|
217
|
+
for (let x = 0; x < width; x++) {
|
|
218
|
+
const srcIdx = (y * width + x) * 4;
|
|
219
|
+
const r = data[srcIdx];
|
|
220
|
+
const g = data[srcIdx + 1];
|
|
221
|
+
const b = data[srcIdx + 2];
|
|
222
|
+
const a = data[srcIdx + 3];
|
|
223
|
+
const destIdx = (y + padding) * paddedWidth + (x + padding);
|
|
224
|
+
if (hasTransparency) {
|
|
225
|
+
if (a > threshold) {
|
|
226
|
+
mask[destIdx] = 1;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
if (!(r > 240 && g > 240 && b > 240)) {
|
|
231
|
+
mask[destIdx] = 1;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return mask;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Fast circular morphology using a distance-transform inspired separable approach.
|
|
240
|
+
* O(N * R) complexity, where R is the radius.
|
|
241
|
+
*/
|
|
242
|
+
static circularMorphology(mask, width, height, radius, op) {
|
|
243
|
+
const dilate = (m, r) => {
|
|
244
|
+
const horizontalDist = new Int32Array(width * height);
|
|
245
|
+
// Horizontal pass: dist to nearest solid pixel in row
|
|
246
|
+
for (let y = 0; y < height; y++) {
|
|
247
|
+
let lastSolid = -r * 2;
|
|
248
|
+
for (let x = 0; x < width; x++) {
|
|
249
|
+
if (m[y * width + x])
|
|
250
|
+
lastSolid = x;
|
|
251
|
+
horizontalDist[y * width + x] = x - lastSolid;
|
|
252
|
+
}
|
|
253
|
+
lastSolid = width + r * 2;
|
|
254
|
+
for (let x = width - 1; x >= 0; x--) {
|
|
255
|
+
if (m[y * width + x])
|
|
256
|
+
lastSolid = x;
|
|
257
|
+
horizontalDist[y * width + x] = Math.min(horizontalDist[y * width + x], lastSolid - x);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
const result = new Uint8Array(width * height);
|
|
261
|
+
const r2 = r * r;
|
|
262
|
+
// Vertical pass: check Euclidean distance using precomputed horizontal distances
|
|
263
|
+
for (let x = 0; x < width; x++) {
|
|
264
|
+
for (let y = 0; y < height; y++) {
|
|
265
|
+
let found = false;
|
|
266
|
+
const minY = Math.max(0, y - r);
|
|
267
|
+
const maxY = Math.min(height - 1, y + r);
|
|
268
|
+
for (let dy = minY; dy <= maxY; dy++) {
|
|
269
|
+
const dY = dy - y;
|
|
270
|
+
const hDist = horizontalDist[dy * width + x];
|
|
271
|
+
if (hDist * hDist + dY * dY <= r2) {
|
|
272
|
+
found = true;
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (found)
|
|
277
|
+
result[y * width + x] = 1;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return result;
|
|
281
|
+
};
|
|
282
|
+
const erode = (m, r) => {
|
|
283
|
+
// Erosion is dilation of the inverted mask
|
|
284
|
+
const inverted = new Uint8Array(m.length);
|
|
285
|
+
for (let i = 0; i < m.length; i++)
|
|
286
|
+
inverted[i] = m[i] ? 0 : 1;
|
|
287
|
+
const dilatedInverted = dilate(inverted, r);
|
|
288
|
+
const result = new Uint8Array(m.length);
|
|
289
|
+
for (let i = 0; i < m.length; i++)
|
|
290
|
+
result[i] = dilatedInverted[i] ? 0 : 1;
|
|
291
|
+
return result;
|
|
292
|
+
};
|
|
293
|
+
switch (op) {
|
|
294
|
+
case "dilate":
|
|
295
|
+
return dilate(mask, radius);
|
|
296
|
+
case "erode":
|
|
297
|
+
return erode(mask, radius);
|
|
298
|
+
case "closing":
|
|
299
|
+
return erode(dilate(mask, radius), radius);
|
|
300
|
+
case "opening":
|
|
301
|
+
return dilate(erode(mask, radius), radius);
|
|
302
|
+
default:
|
|
303
|
+
return mask;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Fills internal holes in the binary mask using flood fill from edges.
|
|
308
|
+
*/
|
|
309
|
+
static fillHoles(mask, width, height) {
|
|
310
|
+
const background = new Uint8Array(width * height);
|
|
311
|
+
const queue = [];
|
|
312
|
+
// Add all edge pixels that are 0 to the queue
|
|
313
|
+
for (let x = 0; x < width; x++) {
|
|
314
|
+
if (mask[x] === 0) {
|
|
315
|
+
background[x] = 1;
|
|
316
|
+
queue.push([x, 0]);
|
|
317
|
+
}
|
|
318
|
+
const lastRow = (height - 1) * width + x;
|
|
319
|
+
if (mask[lastRow] === 0) {
|
|
320
|
+
background[lastRow] = 1;
|
|
321
|
+
queue.push([x, height - 1]);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
for (let y = 1; y < height - 1; y++) {
|
|
325
|
+
if (mask[y * width] === 0) {
|
|
326
|
+
background[y * width] = 1;
|
|
327
|
+
queue.push([0, y]);
|
|
328
|
+
}
|
|
329
|
+
if (mask[y * width + width - 1] === 0) {
|
|
330
|
+
background[y * width + width - 1] = 1;
|
|
331
|
+
queue.push([width - 1, y]);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// Flood fill from the edges to find all background pixels
|
|
335
|
+
const dirs = [
|
|
336
|
+
[0, 1],
|
|
337
|
+
[0, -1],
|
|
338
|
+
[1, 0],
|
|
339
|
+
[-1, 0],
|
|
340
|
+
];
|
|
341
|
+
let head = 0;
|
|
342
|
+
while (head < queue.length) {
|
|
343
|
+
const [cx, cy] = queue[head++];
|
|
344
|
+
for (const [dx, dy] of dirs) {
|
|
345
|
+
const nx = cx + dx;
|
|
346
|
+
const ny = cy + dy;
|
|
347
|
+
if (nx >= 0 && nx < width && ny >= 0 && ny < height) {
|
|
348
|
+
const nidx = ny * width + nx;
|
|
349
|
+
if (mask[nidx] === 0 && background[nidx] === 0) {
|
|
350
|
+
background[nidx] = 1;
|
|
351
|
+
queue.push([nx, ny]);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
// Any pixel that is NOT reachable from the background is part of the "filled" mask
|
|
357
|
+
const filledMask = new Uint8Array(width * height);
|
|
358
|
+
for (let i = 0; i < width * height; i++) {
|
|
359
|
+
filledMask[i] = background[i] === 0 ? 1 : 0;
|
|
360
|
+
}
|
|
361
|
+
return filledMask;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Traces all contours in the mask with optimized start-point detection
|
|
365
|
+
*/
|
|
366
|
+
static traceAllContours(mask, width, height) {
|
|
367
|
+
const visited = new Uint8Array(width * height);
|
|
368
|
+
const allContours = [];
|
|
369
|
+
for (let y = 0; y < height; y++) {
|
|
370
|
+
for (let x = 0; x < width; x++) {
|
|
371
|
+
const idx = y * width + x;
|
|
372
|
+
if (mask[idx] && !visited[idx]) {
|
|
373
|
+
// Only start a new trace if it's a potential outer boundary (left edge)
|
|
374
|
+
const isLeftEdge = x === 0 || mask[idx - 1] === 0;
|
|
375
|
+
if (isLeftEdge) {
|
|
376
|
+
const contour = this.marchingSquares(mask, visited, x, y, width, height);
|
|
377
|
+
if (contour.length > 2) {
|
|
378
|
+
allContours.push(contour);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return allContours;
|
|
385
|
+
}
|
|
386
|
+
static loadImage(url) {
|
|
387
|
+
return new Promise((resolve, reject) => {
|
|
388
|
+
const img = new Image();
|
|
389
|
+
img.crossOrigin = "Anonymous";
|
|
390
|
+
img.onload = () => resolve(img);
|
|
391
|
+
img.onerror = (e) => reject(e);
|
|
392
|
+
img.src = url;
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Moore-Neighbor Tracing Algorithm
|
|
397
|
+
* More robust for irregular shapes than simple Marching Squares walker.
|
|
398
|
+
*/
|
|
399
|
+
static marchingSquares(mask, visited, startX, startY, width, height) {
|
|
400
|
+
const isSolid = (x, y) => {
|
|
401
|
+
if (x < 0 || x >= width || y < 0 || y >= height)
|
|
402
|
+
return false;
|
|
403
|
+
return mask[y * width + x] === 1;
|
|
404
|
+
};
|
|
405
|
+
const points = [];
|
|
406
|
+
// Moore-Neighbor Tracing
|
|
407
|
+
// We enter from the Left (since we scan Left->Right), so "backtrack" is Left.
|
|
408
|
+
// B = (startX - 1, startY)
|
|
409
|
+
// P = (startX, startY)
|
|
410
|
+
let cx = startX;
|
|
411
|
+
let cy = startY;
|
|
412
|
+
// Start backtrack direction: Left (since we found it scanning from left)
|
|
413
|
+
// Directions: 0=Up, 1=UpRight, 2=Right, 3=DownRight, 4=Down, 5=DownLeft, 6=Left, 7=UpLeft
|
|
414
|
+
// Offsets for 8 neighbors starting from Up (0,-1) clockwise
|
|
415
|
+
const neighbors = [
|
|
416
|
+
{ x: 0, y: -1 },
|
|
417
|
+
{ x: 1, y: -1 },
|
|
418
|
+
{ x: 1, y: 0 },
|
|
419
|
+
{ x: 1, y: 1 },
|
|
420
|
+
{ x: 0, y: 1 },
|
|
421
|
+
{ x: -1, y: 1 },
|
|
422
|
+
{ x: -1, y: 0 },
|
|
423
|
+
{ x: -1, y: -1 },
|
|
424
|
+
];
|
|
425
|
+
// Backtrack is Left -> Index 6.
|
|
426
|
+
let backtrack = 6;
|
|
427
|
+
const maxSteps = width * height * 3;
|
|
428
|
+
let steps = 0;
|
|
429
|
+
do {
|
|
430
|
+
points.push({ x: cx, y: cy });
|
|
431
|
+
visited[cy * width + cx] = 1; // Mark as visited to avoid re-starting here
|
|
432
|
+
// Search for next solid neighbor in clockwise order, starting from backtrack
|
|
433
|
+
let found = false;
|
|
434
|
+
for (let i = 0; i < 8; i++) {
|
|
435
|
+
const idx = (backtrack + 1 + i) % 8;
|
|
436
|
+
const nx = cx + neighbors[idx].x;
|
|
437
|
+
const ny = cy + neighbors[idx].y;
|
|
438
|
+
if (isSolid(nx, ny)) {
|
|
439
|
+
cx = nx;
|
|
440
|
+
cy = ny;
|
|
441
|
+
backtrack = (idx + 4 + 1) % 8;
|
|
442
|
+
found = true;
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (!found)
|
|
447
|
+
break;
|
|
448
|
+
steps++;
|
|
449
|
+
} while ((cx !== startX || cy !== startY) && steps < maxSteps);
|
|
450
|
+
return points;
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Douglas-Peucker Line Simplification
|
|
454
|
+
*/
|
|
455
|
+
static douglasPeucker(points, tolerance) {
|
|
456
|
+
if (points.length <= 2)
|
|
457
|
+
return points;
|
|
458
|
+
const sqTolerance = tolerance * tolerance;
|
|
459
|
+
let maxSqDist = 0;
|
|
460
|
+
let index = 0;
|
|
461
|
+
const first = points[0];
|
|
462
|
+
const last = points[points.length - 1];
|
|
463
|
+
for (let i = 1; i < points.length - 1; i++) {
|
|
464
|
+
const sqDist = this.getSqSegDist(points[i], first, last);
|
|
465
|
+
if (sqDist > maxSqDist) {
|
|
466
|
+
index = i;
|
|
467
|
+
maxSqDist = sqDist;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (maxSqDist > sqTolerance) {
|
|
471
|
+
// Check if closed loop?
|
|
472
|
+
// If closed loop, we shouldn't simplify start/end connection too much?
|
|
473
|
+
// Douglas-Peucker works on segments.
|
|
474
|
+
const left = this.douglasPeucker(points.slice(0, index + 1), tolerance);
|
|
475
|
+
const right = this.douglasPeucker(points.slice(index), tolerance);
|
|
476
|
+
return left.slice(0, left.length - 1).concat(right);
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
return [first, last];
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
static getSqSegDist(p, p1, p2) {
|
|
483
|
+
let x = p1.x;
|
|
484
|
+
let y = p1.y;
|
|
485
|
+
let dx = p2.x - x;
|
|
486
|
+
let dy = p2.y - y;
|
|
487
|
+
if (dx !== 0 || dy !== 0) {
|
|
488
|
+
const t = ((p.x - x) * dx + (p.y - y) * dy) / (dx * dx + dy * dy);
|
|
489
|
+
if (t > 1) {
|
|
490
|
+
x = p2.x;
|
|
491
|
+
y = p2.y;
|
|
492
|
+
}
|
|
493
|
+
else if (t > 0) {
|
|
494
|
+
x += dx * t;
|
|
495
|
+
y += dy * t;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
dx = p.x - x;
|
|
499
|
+
dy = p.y - y;
|
|
500
|
+
return dx * dx + dy * dy;
|
|
501
|
+
}
|
|
502
|
+
static scalePoints(points, targetWidth, targetHeight, bounds) {
|
|
503
|
+
if (points.length === 0)
|
|
504
|
+
return points;
|
|
505
|
+
if (bounds.width === 0 || bounds.height === 0)
|
|
506
|
+
return points;
|
|
507
|
+
const scaleX = targetWidth / bounds.width;
|
|
508
|
+
const scaleY = targetHeight / bounds.height;
|
|
509
|
+
return points.map((p) => ({
|
|
510
|
+
x: (p.x - bounds.x) * scaleX,
|
|
511
|
+
y: (p.y - bounds.y) * scaleY,
|
|
512
|
+
}));
|
|
513
|
+
}
|
|
514
|
+
static pointsToSVG(points) {
|
|
515
|
+
if (points.length === 0)
|
|
516
|
+
return "";
|
|
517
|
+
const head = points[0];
|
|
518
|
+
const tail = points.slice(1);
|
|
519
|
+
return (`M ${head.x} ${head.y} ` +
|
|
520
|
+
tail.map((p) => `L ${p.x} ${p.y}`).join(" ") +
|
|
521
|
+
" Z");
|
|
522
|
+
}
|
|
523
|
+
static ensurePaper() {
|
|
524
|
+
if (!paper_1.default.project) {
|
|
525
|
+
paper_1.default.setup(new paper_1.default.Size(100, 100));
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
static pointsToSVGPaper(points, tolerance) {
|
|
529
|
+
if (points.length < 3)
|
|
530
|
+
return this.pointsToSVG(points);
|
|
531
|
+
this.ensurePaper();
|
|
532
|
+
// Create Path
|
|
533
|
+
const path = new paper_1.default.Path({
|
|
534
|
+
segments: points.map(p => [p.x, p.y]),
|
|
535
|
+
closed: true
|
|
536
|
+
});
|
|
537
|
+
// Simplify
|
|
538
|
+
path.simplify(tolerance);
|
|
539
|
+
const data = path.pathData;
|
|
540
|
+
path.remove();
|
|
541
|
+
return data;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
exports.ImageTracer = ImageTracer;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseLengthToMm = parseLengthToMm;
|
|
4
|
+
exports.formatMm = formatMm;
|
|
5
|
+
const coordinate_1 = require("./coordinate");
|
|
6
|
+
function parseLengthToMm(input, defaultUnit) {
|
|
7
|
+
if (typeof input === "number") {
|
|
8
|
+
if (!Number.isFinite(input))
|
|
9
|
+
return 0;
|
|
10
|
+
return coordinate_1.Coordinate.convertUnit(input, defaultUnit, "mm");
|
|
11
|
+
}
|
|
12
|
+
const raw = input.trim();
|
|
13
|
+
if (!raw)
|
|
14
|
+
return 0;
|
|
15
|
+
const match = raw.match(/^([+-]?\d+(?:\.\d+)?)\s*(px|mm|cm|in)?$/i);
|
|
16
|
+
if (!match)
|
|
17
|
+
return 0;
|
|
18
|
+
const value = Number(match[1]);
|
|
19
|
+
if (!Number.isFinite(value))
|
|
20
|
+
return 0;
|
|
21
|
+
const unit = match[2]?.toLowerCase() ?? defaultUnit;
|
|
22
|
+
return coordinate_1.Coordinate.convertUnit(value, unit, "mm");
|
|
23
|
+
}
|
|
24
|
+
function formatMm(valueMm, displayUnit, fractionDigits = 2) {
|
|
25
|
+
if (!Number.isFinite(valueMm))
|
|
26
|
+
return "0";
|
|
27
|
+
const value = coordinate_1.Coordinate.convertUnit(valueMm, "mm", displayUnit);
|
|
28
|
+
const rounded = Number(value.toFixed(fractionDigits));
|
|
29
|
+
return rounded.toString();
|
|
30
|
+
}
|