calculate-packing 0.0.12 → 0.0.13

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 (3) hide show
  1. package/dist/index.d.ts +49 -28
  2. package/dist/index.js +1090 -736
  3. package/package.json +2 -1
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { GraphicsObject, Point } from 'graphics-debug';
1
+ import { GraphicsObject, Point as Point$1 } from 'graphics-debug';
2
2
  import { CircuitJson } from 'circuit-json';
3
3
 
4
4
  type ComponentId = string;
@@ -36,6 +36,7 @@ interface PackedComponent extends InputComponent {
36
36
  x: number;
37
37
  y: number;
38
38
  };
39
+ /** Rotation in degrees (counterclockwise) */
39
40
  ccwRotationOffset: number;
40
41
  pads: OutputPad[];
41
42
  }
@@ -43,7 +44,7 @@ interface PackInput {
43
44
  components: InputComponent[];
44
45
  minGap: number;
45
46
  packOrderStrategy: "largest_to_smallest";
46
- packPlacementStrategy: "shortest_connection_along_outline" | "minimum_sum_distance_to_network";
47
+ packPlacementStrategy: "shortest_connection_along_outline" | "minimum_sum_distance_to_network" | "minimum_sum_squared_distance_to_network";
47
48
  disconnectedPackDirection?: "left" | "right" | "up" | "down" | "nearest_to_center";
48
49
  packFirst?: ComponentId[];
49
50
  }
@@ -101,46 +102,66 @@ declare class BaseSolver {
101
102
  preview(): GraphicsObject;
102
103
  }
103
104
 
104
- /**
105
- * The pack algorithm performs the following steps:
106
- * 1. Sort the components using the packOrderStrategy
107
- * 2. Select the next component to pack
108
- * 3. If the first component, pack at center (0,0) and go to step 2
109
- * 4. Compute the outline of all packed components with a gap of minGap + max(pad.width, pad.height)/2
110
- * 5. Find the point along the outline that minimizes the distance of the pad
111
- * centers within a networkId. If no shared pads, pack to the defaultPackDirection
112
- * 6. Add the component at the selected point, with it's pad at the position
113
- * minimizing the distance between the pad centers
114
- * 7. To determine the component rotation, find the minimum distance between pad
115
- * centers for the remaining pads at each possible rotation (making sure that
116
- * we never pack such that two pads overlap)
117
- * 8. Go to step 2 until all components are packed
118
- */
119
- declare class PackSolver extends BaseSolver {
105
+ type Point = {
106
+ x: number;
107
+ y: number;
108
+ };
109
+ type Segment = [Point, Point];
110
+
111
+ type PackingPhase = "idle" | "show_candidate_points" | "show_rotations" | "show_final_placement";
112
+ interface RotationTrial extends PackedComponent {
113
+ cost: number;
114
+ anchorType: "pad" | "center";
115
+ anchorPadId?: string;
116
+ hasOverlap: boolean;
117
+ }
118
+ declare class PhasedPackSolver extends BaseSolver {
120
119
  packInput: PackInput;
121
120
  unpackedComponentQueue: InputComponent[];
122
121
  packedComponents: PackedComponent[];
122
+ currentPhase: PackingPhase;
123
+ currentComponent?: InputComponent;
124
+ phaseData: {
125
+ candidatePoints?: Array<Point$1 & {
126
+ networkId: NetworkId;
127
+ distance: number;
128
+ }>;
129
+ goodCandidates?: Array<Point$1 & {
130
+ networkId: NetworkId;
131
+ }>;
132
+ bestDistance?: number;
133
+ rotationTrials?: Array<RotationTrial>;
134
+ selectedRotation?: PackedComponent;
135
+ outlines?: Segment[][];
136
+ };
123
137
  lastBestPointsResult?: {
124
- bestPoints: (Point & {
138
+ goodCandidates: (Point$1 & {
125
139
  networkId: NetworkId;
126
140
  })[];
127
141
  distance: number;
128
142
  };
129
143
  lastEvaluatedPositionShadows?: Array<PackedComponent>;
144
+ lastCandidatePoints?: Array<Point$1 & {
145
+ networkId: NetworkId;
146
+ distance: number;
147
+ }>;
130
148
  constructor(input: PackInput);
131
149
  _setup(): void;
132
150
  _step(): void;
151
+ private placeFirstComponent;
152
+ private computeCandidatePoints;
153
+ private computeRotationTrials;
154
+ private selectBestRotation;
155
+ private finalizeComponentPlacement;
156
+ /** Visualize the current packing state based on the current phase */
157
+ visualize(): GraphicsObject;
158
+ private visualizeCandidatePoints;
159
+ private visualizeRotationTrials;
160
+ private visualizeFinalPlacement;
133
161
  getConstructorParams(): PackInput[];
162
+ getResult(): PackedComponent[];
134
163
  private getCandidateAngles;
135
164
  private checkOverlapWithPackedComponents;
136
- private computeGlobalCenter;
137
- private findBestPointForDisconnected;
138
- private placeComponentAtPoint;
139
- private placeComponentDisconnected;
140
- /** Visualize the current packing state – components are omitted, only the outline is shown. */
141
- visualize(): GraphicsObject;
142
- getResult(): PackedComponent[];
143
- private computeSumDistanceForPosition;
144
165
  }
145
166
 
146
167
  declare const convertCircuitJsonToPackOutput: (circuitJson: CircuitJson, opts?: {
@@ -164,4 +185,4 @@ declare const getGraphicsFromPackOutput: (packOutput: PackOutput) => GraphicsObj
164
185
  */
165
186
  declare const convertPackOutputToPackInput: (packed: PackOutput) => PackInput;
166
187
 
167
- export { type ComponentId, type InputComponent, type InputPad, type NetworkId, type OutputPad, type PackInput, type PackOutput, PackSolver, type PackedComponent, type PadId, convertCircuitJsonToPackOutput, convertPackOutputToPackInput, getGraphicsFromPackOutput, pack };
188
+ export { type ComponentId, type InputComponent, type InputPad, type NetworkId, type OutputPad, type PackInput, type PackOutput, type PackedComponent, type PadId, PhasedPackSolver, convertCircuitJsonToPackOutput, convertPackOutputToPackInput, getGraphicsFromPackOutput, pack };