@tscircuit/rectdiff 0.0.4 → 0.0.6

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 (31) hide show
  1. package/dist/index.d.ts +112 -3
  2. package/dist/index.js +869 -142
  3. package/lib/solvers/RectDiffSolver.ts +144 -32
  4. package/lib/solvers/rectdiff/candidates.ts +150 -104
  5. package/lib/solvers/rectdiff/engine.ts +72 -53
  6. package/lib/solvers/rectdiff/gapfill/detection/deduplicateGaps.ts +28 -0
  7. package/lib/solvers/rectdiff/gapfill/detection/findAllGaps.ts +83 -0
  8. package/lib/solvers/rectdiff/gapfill/detection/findGapsOnLayer.ts +100 -0
  9. package/lib/solvers/rectdiff/gapfill/detection/mergeUncoveredCells.ts +75 -0
  10. package/lib/solvers/rectdiff/gapfill/detection.ts +3 -0
  11. package/lib/solvers/rectdiff/gapfill/engine/addPlacement.ts +27 -0
  12. package/lib/solvers/rectdiff/gapfill/engine/calculateCoverage.ts +44 -0
  13. package/lib/solvers/rectdiff/gapfill/engine/findUncoveredPoints.ts +43 -0
  14. package/lib/solvers/rectdiff/gapfill/engine/getGapFillProgress.ts +42 -0
  15. package/lib/solvers/rectdiff/gapfill/engine/initGapFillState.ts +57 -0
  16. package/lib/solvers/rectdiff/gapfill/engine/stepGapFill.ts +128 -0
  17. package/lib/solvers/rectdiff/gapfill/engine/tryExpandGap.ts +78 -0
  18. package/lib/solvers/rectdiff/gapfill/engine.ts +7 -0
  19. package/lib/solvers/rectdiff/gapfill/types.ts +60 -0
  20. package/lib/solvers/rectdiff/geometry.ts +23 -11
  21. package/lib/solvers/rectdiff/subsolvers/GapFillSubSolver.ts +253 -0
  22. package/lib/solvers/rectdiff/types.ts +1 -1
  23. package/package.json +1 -1
  24. package/pages/board-with-cutout.page.tsx +11 -0
  25. package/test-assets/board-with-cutout.json +148 -0
  26. package/tests/obstacle-extra-layers.test.ts +1 -1
  27. package/tests/obstacle-zlayers.test.ts +1 -1
  28. package/tests/rect-diff-solver.test.ts +1 -4
  29. package/utils/README.md +21 -0
  30. package/utils/rectsEqual.ts +18 -0
  31. package/utils/rectsOverlap.ts +18 -0
package/dist/index.d.ts CHANGED
@@ -69,6 +69,12 @@ interface CapacityMeshNode {
69
69
  _parent?: CapacityMeshNode;
70
70
  }
71
71
 
72
+ type XYRect = {
73
+ x: number;
74
+ y: number;
75
+ width: number;
76
+ height: number;
77
+ };
72
78
  type GridFill3DOptions = {
73
79
  gridSizes?: number[];
74
80
  initialCellRatio?: number;
@@ -85,26 +91,129 @@ type GridFill3DOptions = {
85
91
  preferMultiLayer?: boolean;
86
92
  maxMultiLayerSpan?: number;
87
93
  };
94
+ type Placed3D = {
95
+ rect: XYRect;
96
+ zLayers: number[];
97
+ };
98
+
99
+ interface GapFillOptions {
100
+ /** Minimum width for gap-fill rectangles (can be smaller than main solver) */
101
+ minWidth: number;
102
+ /** Minimum height for gap-fill rectangles */
103
+ minHeight: number;
104
+ /** Maximum iterations to prevent infinite loops */
105
+ maxIterations: number;
106
+ /** Target coverage percentage (0-1) to stop early */
107
+ targetCoverage: number;
108
+ /** Grid resolution for gap detection */
109
+ scanResolution: number;
110
+ }
111
+ /** Context for layer-based operations shared across gap fill functions */
112
+ interface LayerContext {
113
+ bounds: XYRect;
114
+ layerCount: number;
115
+ obstaclesByLayer: XYRect[][];
116
+ placedByLayer: XYRect[][];
117
+ }
118
+
119
+ /**
120
+ * A sub-solver that fills empty spaces (gaps) left by the main grid-based
121
+ * placement algorithm.
122
+ *
123
+ * The preceding grid-based placement is fast but can leave irregular un-placed
124
+ * areas. This solver maximizes board coverage by finding and filling these
125
+ * gaps, which is critical for producing a high-quality capacity mesh.
126
+ *
127
+ * The core of the algorithm is its gap-detection phase. It works by first
128
+ * collecting all unique x and y-coordinates from the edges of existing
129
+ * obstacles and placed rectangles. This set of coordinates is supplemented by a
130
+ * uniform grid based on the `scanResolution` parameter. Together, these form a
131
+ * non-uniform grid of cells. The solver then tests the center of each cell for
132
+ * coverage. Contiguous uncovered cells are merged into larger, maximal
133
+ * rectangles, which become the candidate gaps to be filled.
134
+ *
135
+ * Once a prioritized list of gaps is generated (favoring larger, multi-layer
136
+ * gaps), the solver iteratively attempts to fill each one by expanding a new
137
+ * rectangle from a seed point until it collides with an existing boundary.
138
+ *
139
+ * The time complexity is dominated by the gap detection, which is approximately
140
+ * O((N+1/R)^2 * B), where N is the number of objects, R is the scan
141
+ * resolution, and B is the number of blockers. The algorithm's performance is
142
+ * therefore highly dependent on the `scanResolution`. It is a heuristic
143
+ * designed to be "fast enough" by avoiding a brute-force search, instead
144
+ * relying on this grid-based cell checking to find significant gaps.
145
+ */
146
+ declare class GapFillSubSolver extends BaseSolver {
147
+ private state;
148
+ private layerCtx;
149
+ constructor(params: {
150
+ placed: Placed3D[];
151
+ options?: Partial<GapFillOptions>;
152
+ layerCtx: LayerContext;
153
+ });
154
+ /**
155
+ * Execute one step of the gap fill algorithm.
156
+ * Each gap goes through four stages: scan for gaps, select a target gap,
157
+ * expand a rectangle from seed point, then place the final result.
158
+ */
159
+ _step(): void;
160
+ /**
161
+ * Calculate progress as a value between 0 and 1.
162
+ * Accounts for iterations, gaps processed, and current stage within each gap.
163
+ */
164
+ computeProgress(): number;
165
+ /**
166
+ * Get all placed rectangles including original ones plus newly created gap-fill rectangles.
167
+ */
168
+ getPlaced(): Placed3D[];
169
+ /**
170
+ * Get placed rectangles organized by Z-layer for efficient layer-based operations.
171
+ */
172
+ getPlacedByLayer(): XYRect[][];
173
+ getOutput(): {
174
+ placed: Placed3D[];
175
+ placedByLayer: XYRect[][];
176
+ filledCount: number;
177
+ };
178
+ /** Zen visualization: show four-stage gap filling process. */
179
+ visualize(): GraphicsObject;
180
+ }
88
181
 
182
+ /**
183
+ * A streaming, one-step-per-iteration solver for capacity mesh generation.
184
+ */
89
185
  declare class RectDiffSolver extends BaseSolver {
90
186
  private srj;
91
- private mode;
92
187
  private gridOptions;
188
+ private gapFillOptions;
93
189
  private state;
94
190
  private _meshNodes;
191
+ /** Active subsolver for GAP_FILL phases. */
192
+ activeSubSolver: GapFillSubSolver | null;
95
193
  constructor(opts: {
96
194
  simpleRouteJson: SimpleRouteJson;
97
- mode?: "grid" | "exact";
98
195
  gridOptions?: Partial<GridFill3DOptions>;
196
+ gapFillOptions?: Partial<GapFillOptions>;
99
197
  });
100
198
  _setup(): void;
101
- /** IMPORTANT: exactly ONE small step per call */
199
+ /** Exactly ONE small step per call. */
102
200
  _step(): void;
201
+ /** Compute solver progress (0 to 1). */
103
202
  computeProgress(): number;
104
203
  getOutput(): {
105
204
  meshNodes: CapacityMeshNode[];
106
205
  };
206
+ /** Get coverage percentage (0-1). */
207
+ getCoverage(sampleResolution?: number): number;
208
+ /** Find uncovered points for debugging gaps. */
209
+ getUncoveredPoints(sampleResolution?: number): Array<{
210
+ x: number;
211
+ y: number;
212
+ z: number;
213
+ }>;
214
+ /** Get color based on z layer for visualization. */
107
215
  private getColorForZLayer;
216
+ /** Streaming visualization: board + obstacles + current placements. */
108
217
  visualize(): GraphicsObject;
109
218
  }
110
219