calculate-packing 0.0.14 → 0.0.16

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/dist/index.d.ts CHANGED
@@ -100,6 +100,7 @@ declare class BaseSolver {
100
100
  * progress
101
101
  */
102
102
  preview(): GraphicsObject;
103
+ noisySolve(): void;
103
104
  }
104
105
 
105
106
  type Point = {
package/dist/index.js CHANGED
@@ -132,61 +132,42 @@ var rotatePoint = (point, angle, origin = { x: 0, y: 0 }) => {
132
132
  };
133
133
  };
134
134
 
135
- // lib/geometry/getComponentBounds.ts
136
- var getComponentBounds = (component, minGap = 0) => {
137
- const bounds = {
138
- minX: Infinity,
139
- maxX: -Infinity,
140
- minY: Infinity,
141
- maxY: -Infinity
142
- };
143
- component.pads.forEach((pad) => {
144
- const hw = pad.size.x / 2;
145
- const hh = pad.size.y / 2;
135
+ // lib/constructOutlinesFromPackedComponents.ts
136
+ var createPadPolygons = (component, minGap) => {
137
+ return component.pads.map((pad) => {
138
+ const hw = pad.size.x / 2 + minGap;
139
+ const hh = pad.size.y / 2 + minGap;
146
140
  const localCorners = [
147
141
  { x: pad.offset.x - hw, y: pad.offset.y - hh },
148
142
  { x: pad.offset.x + hw, y: pad.offset.y - hh },
149
143
  { x: pad.offset.x + hw, y: pad.offset.y + hh },
150
144
  { x: pad.offset.x - hw, y: pad.offset.y + hh }
151
145
  ];
152
- localCorners.forEach((corner) => {
153
- const world = rotatePoint(
146
+ const worldCorners = localCorners.map((corner) => {
147
+ const rotated = rotatePoint(
154
148
  corner,
155
149
  component.ccwRotationOffset * Math.PI / 180
156
150
  );
157
- const x = world.x + component.center.x;
158
- const y = world.y + component.center.y;
159
- bounds.minX = Math.min(bounds.minX, x);
160
- bounds.maxX = Math.max(bounds.maxX, x);
161
- bounds.minY = Math.min(bounds.minY, y);
162
- bounds.maxY = Math.max(bounds.maxY, y);
151
+ return [
152
+ rotated.x + component.center.x,
153
+ rotated.y + component.center.y
154
+ ];
163
155
  });
156
+ return new Flatten.Polygon(worldCorners);
164
157
  });
165
- return {
166
- minX: bounds.minX - minGap,
167
- maxX: bounds.maxX + minGap,
168
- minY: bounds.minY - minGap,
169
- maxY: bounds.maxY + minGap
170
- };
171
158
  };
172
-
173
- // lib/constructOutlinesFromPackedComponents.ts
174
159
  var constructOutlinesFromPackedComponents = (components, opts = {}) => {
175
160
  const { minGap = 0 } = opts;
176
161
  if (components.length === 0) return [];
177
- const rectPolys = components.map((c) => {
178
- const b = getComponentBounds(c, minGap);
179
- return new Flatten.Polygon([
180
- [b.minX, b.minY],
181
- [b.maxX, b.minY],
182
- [b.maxX, b.maxY],
183
- [b.minX, b.maxY]
184
- ]);
185
- });
186
- if (rectPolys.length === 0) return [];
187
- let union = rectPolys[0];
188
- for (let i = 1; i < rectPolys.length; i++) {
189
- union = Flatten.BooleanOperations.unify(union, rectPolys[i]);
162
+ const allPadPolys = [];
163
+ for (const component of components) {
164
+ const padPolys = createPadPolygons(component, minGap);
165
+ allPadPolys.push(...padPolys);
166
+ }
167
+ if (allPadPolys.length === 0) return [];
168
+ let union = allPadPolys[0];
169
+ for (let i = 1; i < allPadPolys.length; i++) {
170
+ union = Flatten.BooleanOperations.unify(union, allPadPolys[i]);
190
171
  }
191
172
  const outlines = [];
192
173
  for (const face of union.faces) {
@@ -306,6 +287,44 @@ function findOptimalPointOnSegment({
306
287
  };
307
288
  }
308
289
 
290
+ // lib/geometry/getComponentBounds.ts
291
+ var getComponentBounds = (component, minGap = 0) => {
292
+ const bounds = {
293
+ minX: Infinity,
294
+ maxX: -Infinity,
295
+ minY: Infinity,
296
+ maxY: -Infinity
297
+ };
298
+ component.pads.forEach((pad) => {
299
+ const hw = pad.size.x / 2;
300
+ const hh = pad.size.y / 2;
301
+ const localCorners = [
302
+ { x: pad.offset.x - hw, y: pad.offset.y - hh },
303
+ { x: pad.offset.x + hw, y: pad.offset.y - hh },
304
+ { x: pad.offset.x + hw, y: pad.offset.y + hh },
305
+ { x: pad.offset.x - hw, y: pad.offset.y + hh }
306
+ ];
307
+ localCorners.forEach((corner) => {
308
+ const world = rotatePoint(
309
+ corner,
310
+ component.ccwRotationOffset * Math.PI / 180
311
+ );
312
+ const x = world.x + component.center.x;
313
+ const y = world.y + component.center.y;
314
+ bounds.minX = Math.min(bounds.minX, x);
315
+ bounds.maxX = Math.max(bounds.maxX, x);
316
+ bounds.minY = Math.min(bounds.minY, y);
317
+ bounds.maxY = Math.max(bounds.maxY, y);
318
+ });
319
+ });
320
+ return {
321
+ minX: bounds.minX - minGap,
322
+ maxX: bounds.maxX + minGap,
323
+ minY: bounds.minY - minGap,
324
+ maxY: bounds.maxY + minGap
325
+ };
326
+ };
327
+
309
328
  // lib/testing/createColorMapFromStrings.ts
310
329
  var createColorMapFromStrings = (strings) => {
311
330
  const colorMap = {};
@@ -543,6 +562,22 @@ function sortComponentQueue({
543
562
  });
544
563
  }
545
564
 
565
+ // lib/solver-utils/makeNumbersRounded.ts
566
+ var makeNumbersRounded = (obj) => {
567
+ if (typeof obj === "number") {
568
+ return Math.round(obj * 100) / 100;
569
+ }
570
+ if (typeof obj === "object") {
571
+ return Object.fromEntries(
572
+ Object.entries(obj).map(([key, value]) => [
573
+ key,
574
+ makeNumbersRounded(value)
575
+ ])
576
+ );
577
+ }
578
+ return obj;
579
+ };
580
+
546
581
  // lib/solver-utils/BaseSolver.ts
547
582
  var BaseSolver = class {
548
583
  MAX_ITERATIONS = 1e3;
@@ -631,6 +666,15 @@ var BaseSolver = class {
631
666
  circles: []
632
667
  };
633
668
  }
669
+ noisySolve() {
670
+ const startTime = Date.now();
671
+ while (!this.solved && !this.failed) {
672
+ this.step();
673
+ console.log(JSON.stringify(makeNumbersRounded(this.visualize())));
674
+ }
675
+ const endTime = Date.now();
676
+ this.timeToSolve = endTime - startTime;
677
+ }
634
678
  };
635
679
 
636
680
  // lib/PackSolver/PhasedPackSolver.ts
@@ -733,11 +777,16 @@ var PhasedPackSolver = class extends BaseSolver {
733
777
  const padMargins = newPackedComponent.pads.map(
734
778
  (p) => Math.max(p.size.x, p.size.y) / 2
735
779
  );
736
- const additionalGap = Math.max(...padMargins);
737
- const outlines = constructOutlinesFromPackedComponents(
738
- this.packedComponents,
739
- { minGap: minGap + additionalGap }
740
- );
780
+ const maxPadMargin = Math.max(...padMargins);
781
+ const minPadMargin = Math.min(...padMargins);
782
+ const outlines = [
783
+ ...constructOutlinesFromPackedComponents(this.packedComponents, {
784
+ minGap: minGap + minPadMargin
785
+ }),
786
+ ...constructOutlinesFromPackedComponents(this.packedComponents, {
787
+ minGap: minGap + maxPadMargin
788
+ })
789
+ ];
741
790
  this.phaseData.outlines = outlines;
742
791
  const networkIdsInPackedComponents = new Set(
743
792
  this.packedComponents.flatMap((c) => c.pads.map((p) => p.networkId))
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.14",
5
+ "version": "0.0.16",
6
6
  "description": "Calculate a packing layout with support for different strategy configurations",
7
7
  "scripts": {
8
8
  "start": "cosmos",