calculate-packing 0.0.32 → 0.0.33

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 (2) hide show
  1. package/dist/index.js +61 -13
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -250,12 +250,22 @@ var createPadPolygons = (component, minGap) => {
250
250
  corner,
251
251
  component.ccwRotationOffset * Math.PI / 180
252
252
  );
253
- return [
254
- rotated.x + component.center.x,
255
- rotated.y + component.center.y
256
- ];
253
+ return {
254
+ x: rotated.x + component.center.x,
255
+ y: rotated.y + component.center.y
256
+ };
257
257
  });
258
- return new Flatten.Polygon(worldCorners);
258
+ const arr = worldCorners.map(({ x, y }) => [x, y]);
259
+ const poly = new Flatten.Polygon(arr);
260
+ const xs = worldCorners.map((p) => p.x);
261
+ const ys = worldCorners.map((p) => p.y);
262
+ const bbox = {
263
+ minX: Math.min(...xs),
264
+ minY: Math.min(...ys),
265
+ maxX: Math.max(...xs),
266
+ maxY: Math.max(...ys)
267
+ };
268
+ return { poly, bbox };
259
269
  });
260
270
  };
261
271
  var constructOutlinesFromPackedComponents = (components, opts = {}) => {
@@ -264,23 +274,61 @@ var constructOutlinesFromPackedComponents = (components, opts = {}) => {
264
274
  const bounds = combineBounds(
265
275
  components.map((c) => getComponentBounds(c, minGap))
266
276
  );
267
- const allPadPolys = [];
277
+ const allPadShapes = [];
268
278
  for (const component of components) {
269
- const padPolys = createPadPolygons(component, minGap);
270
- allPadPolys.push(...padPolys);
279
+ const padShapes = createPadPolygons(component, minGap);
280
+ allPadShapes.push(...padShapes);
281
+ }
282
+ if (allPadShapes.length === 0) return [];
283
+ const areaOfBox = (b) => Math.max(0, b.maxX - b.minX) * Math.max(0, b.maxY - b.minY);
284
+ const containsBox = (outer, inner, eps = 1e-9) => outer.minX - eps <= inner.minX && outer.minY - eps <= inner.minY && outer.maxX + eps >= inner.maxX && outer.maxY + eps >= inner.maxY;
285
+ const sortedByAreaDesc = [...allPadShapes].sort(
286
+ (a, b) => areaOfBox(b.bbox) - areaOfBox(a.bbox)
287
+ );
288
+ const filteredPadShapes = [];
289
+ for (const shape of sortedByAreaDesc) {
290
+ const w = shape.bbox.maxX - shape.bbox.minX;
291
+ const h = shape.bbox.maxY - shape.bbox.minY;
292
+ if (!(w > 1e-12 && h > 1e-12)) continue;
293
+ let contained = false;
294
+ for (const kept of filteredPadShapes) {
295
+ if (containsBox(kept.bbox, shape.bbox)) {
296
+ contained = true;
297
+ break;
298
+ }
299
+ }
300
+ if (!contained) filteredPadShapes.push(shape);
271
301
  }
272
- if (allPadPolys.length === 0) return [];
302
+ const keptPadPolys = filteredPadShapes.map((s) => s.poly);
273
303
  let A = new Flatten.Polygon(
274
304
  new Flatten.Box(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY)
275
305
  );
276
- let B = A.clone();
277
- for (let i = 0; i < allPadPolys.length; i++) {
306
+ const B = A.clone();
307
+ for (let i = 0; i < keptPadPolys.length; i++) {
278
308
  try {
279
- A = Flatten.BooleanOperations.subtract(A, allPadPolys[i]);
309
+ A = Flatten.BooleanOperations.subtract(A, keptPadPolys[i]);
280
310
  } catch (e) {
281
311
  }
282
312
  }
283
- const union = Flatten.BooleanOperations.subtract(B, A);
313
+ let union = null;
314
+ try {
315
+ union = Flatten.BooleanOperations.subtract(B, A);
316
+ } catch (e) {
317
+ try {
318
+ if (keptPadPolys.length > 0) {
319
+ let U = keptPadPolys[0];
320
+ for (let i = 1; i < keptPadPolys.length; i++) {
321
+ try {
322
+ U = Flatten.BooleanOperations.unify(U, keptPadPolys[i]);
323
+ } catch {
324
+ }
325
+ }
326
+ union = U;
327
+ }
328
+ } catch {
329
+ union = null;
330
+ }
331
+ }
284
332
  const outlines = [];
285
333
  for (const face of union.faces) {
286
334
  if (face.isHole) continue;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "calculate-packing",
3
3
  "main": "dist/index.js",
4
4
  "type": "module",
5
- "version": "0.0.32",
5
+ "version": "0.0.33",
6
6
  "description": "Calculate a packing layout with support for different strategy configurations",
7
7
  "scripts": {
8
8
  "start": "cosmos",