calculate-packing 0.0.22 → 0.0.23

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 +60 -41
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -195,6 +195,53 @@ function simplifyCollinearSegments(outline, tolerance = 1e-10) {
195
195
  return simplified;
196
196
  }
197
197
 
198
+ // lib/geometry/getComponentBounds.ts
199
+ var getComponentBounds = (component, minGap = 0) => {
200
+ const bounds = {
201
+ minX: Infinity,
202
+ maxX: -Infinity,
203
+ minY: Infinity,
204
+ maxY: -Infinity
205
+ };
206
+ component.pads.forEach((pad) => {
207
+ const hw = pad.size.x / 2;
208
+ const hh = pad.size.y / 2;
209
+ const localCorners = [
210
+ { x: pad.offset.x - hw, y: pad.offset.y - hh },
211
+ { x: pad.offset.x + hw, y: pad.offset.y - hh },
212
+ { x: pad.offset.x + hw, y: pad.offset.y + hh },
213
+ { x: pad.offset.x - hw, y: pad.offset.y + hh }
214
+ ];
215
+ localCorners.forEach((corner) => {
216
+ const world = rotatePoint(
217
+ corner,
218
+ component.ccwRotationOffset * Math.PI / 180
219
+ );
220
+ const x = world.x + component.center.x;
221
+ const y = world.y + component.center.y;
222
+ bounds.minX = Math.min(bounds.minX, x);
223
+ bounds.maxX = Math.max(bounds.maxX, x);
224
+ bounds.minY = Math.min(bounds.minY, y);
225
+ bounds.maxY = Math.max(bounds.maxY, y);
226
+ });
227
+ });
228
+ return {
229
+ minX: bounds.minX - minGap,
230
+ maxX: bounds.maxX + minGap,
231
+ minY: bounds.minY - minGap,
232
+ maxY: bounds.maxY + minGap
233
+ };
234
+ };
235
+
236
+ // lib/geometry/combineBounds.ts
237
+ var combineBounds = (bounds) => {
238
+ const minX = Math.min(...bounds.map((b) => b.minX));
239
+ const minY = Math.min(...bounds.map((b) => b.minY));
240
+ const maxX = Math.max(...bounds.map((b) => b.maxX));
241
+ const maxY = Math.max(...bounds.map((b) => b.maxY));
242
+ return { minX, minY, maxX, maxY };
243
+ };
244
+
198
245
  // lib/constructOutlinesFromPackedComponents.ts
199
246
  var createPadPolygons = (component, minGap) => {
200
247
  return component.pads.map((pad) => {
@@ -222,16 +269,26 @@ var createPadPolygons = (component, minGap) => {
222
269
  var constructOutlinesFromPackedComponents = (components, opts = {}) => {
223
270
  const { minGap = 0 } = opts;
224
271
  if (components.length === 0) return [];
272
+ const bounds = combineBounds(
273
+ components.map((c) => getComponentBounds(c, minGap))
274
+ );
225
275
  const allPadPolys = [];
226
276
  for (const component of components) {
227
277
  const padPolys = createPadPolygons(component, minGap);
228
278
  allPadPolys.push(...padPolys);
229
279
  }
230
280
  if (allPadPolys.length === 0) return [];
231
- let union = allPadPolys[0];
232
- for (let i = 1; i < allPadPolys.length; i++) {
233
- union = Flatten.BooleanOperations.unify(union, allPadPolys[i]);
281
+ let A = new Flatten.Polygon(
282
+ new Flatten.Box(bounds.minX, bounds.minY, bounds.maxX, bounds.maxY)
283
+ );
284
+ let B = A.clone();
285
+ for (let i = 0; i < allPadPolys.length; i++) {
286
+ try {
287
+ A = Flatten.BooleanOperations.subtract(A, allPadPolys[i]);
288
+ } catch (e) {
289
+ }
234
290
  }
291
+ const union = Flatten.BooleanOperations.subtract(B, A);
235
292
  const outlines = [];
236
293
  for (const face of union.faces) {
237
294
  if (face.isHole) continue;
@@ -351,44 +408,6 @@ function findOptimalPointOnSegment({
351
408
  };
352
409
  }
353
410
 
354
- // lib/geometry/getComponentBounds.ts
355
- var getComponentBounds = (component, minGap = 0) => {
356
- const bounds = {
357
- minX: Infinity,
358
- maxX: -Infinity,
359
- minY: Infinity,
360
- maxY: -Infinity
361
- };
362
- component.pads.forEach((pad) => {
363
- const hw = pad.size.x / 2;
364
- const hh = pad.size.y / 2;
365
- const localCorners = [
366
- { x: pad.offset.x - hw, y: pad.offset.y - hh },
367
- { x: pad.offset.x + hw, y: pad.offset.y - hh },
368
- { x: pad.offset.x + hw, y: pad.offset.y + hh },
369
- { x: pad.offset.x - hw, y: pad.offset.y + hh }
370
- ];
371
- localCorners.forEach((corner) => {
372
- const world = rotatePoint(
373
- corner,
374
- component.ccwRotationOffset * Math.PI / 180
375
- );
376
- const x = world.x + component.center.x;
377
- const y = world.y + component.center.y;
378
- bounds.minX = Math.min(bounds.minX, x);
379
- bounds.maxX = Math.max(bounds.maxX, x);
380
- bounds.minY = Math.min(bounds.minY, y);
381
- bounds.maxY = Math.max(bounds.maxY, y);
382
- });
383
- });
384
- return {
385
- minX: bounds.minX - minGap,
386
- maxX: bounds.maxX + minGap,
387
- minY: bounds.minY - minGap,
388
- maxY: bounds.maxY + minGap
389
- };
390
- };
391
-
392
411
  // lib/testing/createColorMapFromStrings.ts
393
412
  var createColorMapFromStrings = (strings) => {
394
413
  const colorMap = {};
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.22",
5
+ "version": "0.0.23",
6
6
  "description": "Calculate a packing layout with support for different strategy configurations",
7
7
  "scripts": {
8
8
  "start": "cosmos",