@tscircuit/circuit-json-util 0.0.82 → 0.0.84
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/README.md +37 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +350 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -47,6 +47,7 @@ It reduces the amount of code to retrieve or join elements from circuit json, it
|
|
|
47
47
|
| `getStringFromCircuitJsonTree` | [`lib/getStringFromCircuitJsonTree.ts`](./lib/getStringFromCircuitJsonTree.ts) | Renders a circuit-json tree as text for debugging. |
|
|
48
48
|
| `getMinimumFlexContainer` | [`lib/get-minimum-flex-container.ts`](./lib/get-minimum-flex-container.ts) | Computes a minimum flex container from layout constraints. |
|
|
49
49
|
| `getElementRenderLayers` | [`lib/get-element-render-layers.ts`](./lib/get-element-render-layers.ts) | Returns schematic/PCB render layers used for an element. |
|
|
50
|
+
| `computeGapBetweenCopper` | [`lib/compute-gap-between-copper.ts`](./lib/compute-gap-between-copper.ts) | Computes the minimum copper-to-copper gap between two circuit elements by decomposing them into primitive shapes. |
|
|
50
51
|
|
|
51
52
|
## Standard Usage
|
|
52
53
|
|
|
@@ -134,3 +135,39 @@ repositionSchematicGroupTo(circuitJson, "g1", { x: 20, y: 15 })
|
|
|
134
135
|
```
|
|
135
136
|
|
|
136
137
|
|
|
138
|
+
|
|
139
|
+
## Compute Copper Gap
|
|
140
|
+
|
|
141
|
+
Use `computeGapBetweenCopper` to calculate the minimum copper clearance between two PCB elements.
|
|
142
|
+
|
|
143
|
+
It decomposes each element into primitive shapes (`circle`, `rect`, `polygon`) and returns the minimum distance across all shape-pair combinations. If an element does not produce any supported copper shapes, the function returns `Infinity`.
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import { computeGapBetweenCopper } from "@tscircuit/circuit-json-util"
|
|
147
|
+
|
|
148
|
+
const gap = computeGapBetweenCopper(
|
|
149
|
+
{
|
|
150
|
+
type: "pcb_smtpad",
|
|
151
|
+
pcb_smtpad_id: "pad1",
|
|
152
|
+
shape: "circle",
|
|
153
|
+
x: 0,
|
|
154
|
+
y: 0,
|
|
155
|
+
radius: 0.5,
|
|
156
|
+
layer: "top",
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
type: "pcb_smtpad",
|
|
160
|
+
pcb_smtpad_id: "pad2",
|
|
161
|
+
shape: "rect",
|
|
162
|
+
x: 2,
|
|
163
|
+
y: 0,
|
|
164
|
+
width: 1,
|
|
165
|
+
height: 1,
|
|
166
|
+
layer: "top",
|
|
167
|
+
},
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
console.log(gap) // 1
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Currently supported decomposition targets include `pcb_smtpad` (circle/rect/polygon), `pcb_trace` wire segments, `pcb_via`, and `pcb_plated_hole`.
|
package/dist/index.d.ts
CHANGED
|
@@ -16619,4 +16619,6 @@ declare function getMinimumFlexContainer(children: Array<{
|
|
|
16619
16619
|
|
|
16620
16620
|
declare function getElementRenderLayers(element: AnyCircuitElement): PcbRenderLayer[];
|
|
16621
16621
|
|
|
16622
|
-
|
|
16622
|
+
declare const computeGapBetweenCopper: (elm1: AnyCircuitElement, elm2: AnyCircuitElement) => number;
|
|
16623
|
+
|
|
16624
|
+
export { type BoardBounds, type CircuitJsonInputUtilObjects, type CircuitJsonOps, type CircuitJsonTreeNode, type CircuitJsonUtilObjects, type CircuitJsonUtilOptions, type GetCircuitJsonUtilFn, type GetIndexedCircuitJsonUtilFn, type IndexedCircuitJsonUtilOptions, type Size, type SubtreeOptions, applySelector, applySelectorAST, buildSubtree, cju, cjuIndexed, computeGapBetweenCopper, directionToVec, findBoundsAndCenter, getBoardBounds, getBoundsOfPcbElements, getCircuitJsonTree, getElementById, getElementId, getElementRenderLayers, getMinimumFlexContainer, getPrimaryId, getReadableNameForElement, getReadableNameForPcbPort, getReadableNameForPcbSmtpad, getReadableNameForPcbTrace, getStringFromCircuitJsonTree, oppositeDirection, oppositeSide, repositionPcbComponentTo, repositionPcbGroupTo, repositionSchematicComponentTo, repositionSchematicGroupTo, rotateClockwise, rotateCounterClockwise, rotateDirection, su, transformPCBElement, transformPCBElements, transformPCBElement as transformPcbElement, transformPCBElements as transformPcbElements, transformSchematicElement, transformSchematicElements, vecToDirection };
|
package/dist/index.js
CHANGED
|
@@ -1215,6 +1215,43 @@ var getReadableNameForElement = (soup, elm) => {
|
|
|
1215
1215
|
};
|
|
1216
1216
|
|
|
1217
1217
|
// lib/get-bounds-of-pcb-elements.ts
|
|
1218
|
+
var mergeBounds = (currentBounds, nextBounds) => ({
|
|
1219
|
+
minX: Math.min(currentBounds.minX, nextBounds.minX),
|
|
1220
|
+
minY: Math.min(currentBounds.minY, nextBounds.minY),
|
|
1221
|
+
maxX: Math.max(currentBounds.maxX, nextBounds.maxX),
|
|
1222
|
+
maxY: Math.max(currentBounds.maxY, nextBounds.maxY)
|
|
1223
|
+
});
|
|
1224
|
+
var getCircleBounds = (x, y, diameter) => {
|
|
1225
|
+
const radius = diameter / 2;
|
|
1226
|
+
return {
|
|
1227
|
+
minX: x - radius,
|
|
1228
|
+
minY: y - radius,
|
|
1229
|
+
maxX: x + radius,
|
|
1230
|
+
maxY: y + radius
|
|
1231
|
+
};
|
|
1232
|
+
};
|
|
1233
|
+
var getRotatedRectBounds = (x, y, width, height, rotationDegrees) => {
|
|
1234
|
+
const halfWidth = width / 2;
|
|
1235
|
+
const halfHeight = height / 2;
|
|
1236
|
+
const theta = rotationDegrees * Math.PI / 180;
|
|
1237
|
+
const cosTheta = Math.cos(theta);
|
|
1238
|
+
const sinTheta = Math.sin(theta);
|
|
1239
|
+
const corners = [
|
|
1240
|
+
{ x: -halfWidth, y: -halfHeight },
|
|
1241
|
+
{ x: halfWidth, y: -halfHeight },
|
|
1242
|
+
{ x: halfWidth, y: halfHeight },
|
|
1243
|
+
{ x: -halfWidth, y: halfHeight }
|
|
1244
|
+
].map((corner) => ({
|
|
1245
|
+
x: x + corner.x * cosTheta - corner.y * sinTheta,
|
|
1246
|
+
y: y + corner.x * sinTheta + corner.y * cosTheta
|
|
1247
|
+
}));
|
|
1248
|
+
return {
|
|
1249
|
+
minX: Math.min(...corners.map((corner) => corner.x)),
|
|
1250
|
+
minY: Math.min(...corners.map((corner) => corner.y)),
|
|
1251
|
+
maxX: Math.max(...corners.map((corner) => corner.x)),
|
|
1252
|
+
maxY: Math.max(...corners.map((corner) => corner.y))
|
|
1253
|
+
};
|
|
1254
|
+
};
|
|
1218
1255
|
var getBoundsOfPcbElements = (elements) => {
|
|
1219
1256
|
let minX = Number.POSITIVE_INFINITY;
|
|
1220
1257
|
let minY = Number.POSITIVE_INFINITY;
|
|
@@ -1231,6 +1268,47 @@ var getBoundsOfPcbElements = (elements) => {
|
|
|
1231
1268
|
}
|
|
1232
1269
|
continue;
|
|
1233
1270
|
}
|
|
1271
|
+
if (elm.type === "pcb_hole" && elm.hole_shape === "circle") {
|
|
1272
|
+
const holeBounds = getCircleBounds(elm.x, elm.y, elm.hole_diameter);
|
|
1273
|
+
minX = Math.min(minX, holeBounds.minX);
|
|
1274
|
+
minY = Math.min(minY, holeBounds.minY);
|
|
1275
|
+
maxX = Math.max(maxX, holeBounds.maxX);
|
|
1276
|
+
maxY = Math.max(maxY, holeBounds.maxY);
|
|
1277
|
+
continue;
|
|
1278
|
+
}
|
|
1279
|
+
if (elm.type === "pcb_plated_hole") {
|
|
1280
|
+
let platedHoleBounds;
|
|
1281
|
+
if ("outer_diameter" in elm && typeof elm.outer_diameter === "number") {
|
|
1282
|
+
platedHoleBounds = getCircleBounds(elm.x, elm.y, elm.outer_diameter);
|
|
1283
|
+
} else if ("hole_diameter" in elm && typeof elm.hole_diameter === "number") {
|
|
1284
|
+
platedHoleBounds = getCircleBounds(elm.x, elm.y, elm.hole_diameter);
|
|
1285
|
+
}
|
|
1286
|
+
if ("rect_pad_width" in elm && typeof elm.rect_pad_width === "number" && "rect_pad_height" in elm && typeof elm.rect_pad_height === "number") {
|
|
1287
|
+
const rectBounds = getRotatedRectBounds(
|
|
1288
|
+
elm.x,
|
|
1289
|
+
elm.y,
|
|
1290
|
+
elm.rect_pad_width,
|
|
1291
|
+
elm.rect_pad_height,
|
|
1292
|
+
"rect_ccw_rotation" in elm ? elm.rect_ccw_rotation ?? 0 : 0
|
|
1293
|
+
);
|
|
1294
|
+
platedHoleBounds = platedHoleBounds ? mergeBounds(platedHoleBounds, rectBounds) : rectBounds;
|
|
1295
|
+
}
|
|
1296
|
+
if ("hole_diameter" in elm && typeof elm.hole_diameter === "number") {
|
|
1297
|
+
const drillBounds = getCircleBounds(
|
|
1298
|
+
elm.x + ("hole_offset_x" in elm ? elm.hole_offset_x ?? 0 : 0),
|
|
1299
|
+
elm.y + ("hole_offset_y" in elm ? elm.hole_offset_y ?? 0 : 0),
|
|
1300
|
+
elm.hole_diameter
|
|
1301
|
+
);
|
|
1302
|
+
platedHoleBounds = platedHoleBounds ? mergeBounds(platedHoleBounds, drillBounds) : drillBounds;
|
|
1303
|
+
}
|
|
1304
|
+
if (platedHoleBounds) {
|
|
1305
|
+
minX = Math.min(minX, platedHoleBounds.minX);
|
|
1306
|
+
minY = Math.min(minY, platedHoleBounds.minY);
|
|
1307
|
+
maxX = Math.max(maxX, platedHoleBounds.maxX);
|
|
1308
|
+
maxY = Math.max(maxY, platedHoleBounds.maxY);
|
|
1309
|
+
continue;
|
|
1310
|
+
}
|
|
1311
|
+
}
|
|
1234
1312
|
let centerX;
|
|
1235
1313
|
let centerY;
|
|
1236
1314
|
let width;
|
|
@@ -1892,12 +1970,284 @@ function getElementRenderLayers(element) {
|
|
|
1892
1970
|
}
|
|
1893
1971
|
return [];
|
|
1894
1972
|
}
|
|
1973
|
+
|
|
1974
|
+
// lib/shape-distances/geometry.ts
|
|
1975
|
+
var EPSILON = 1e-9;
|
|
1976
|
+
var toRadians = (degrees) => degrees * Math.PI / 180;
|
|
1977
|
+
var clamp = (value, min, max) => Math.max(min, Math.min(max, value));
|
|
1978
|
+
var distanceBetweenPoints = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
|
|
1979
|
+
var distanceBetweenPointAndSegment = (point, a, b) => {
|
|
1980
|
+
const abX = b.x - a.x;
|
|
1981
|
+
const abY = b.y - a.y;
|
|
1982
|
+
const lengthSquared = abX * abX + abY * abY;
|
|
1983
|
+
if (lengthSquared <= EPSILON) return distanceBetweenPoints(point, a);
|
|
1984
|
+
const t = clamp(
|
|
1985
|
+
((point.x - a.x) * abX + (point.y - a.y) * abY) / lengthSquared,
|
|
1986
|
+
0,
|
|
1987
|
+
1
|
|
1988
|
+
);
|
|
1989
|
+
return Math.hypot(point.x - (a.x + t * abX), point.y - (a.y + t * abY));
|
|
1990
|
+
};
|
|
1991
|
+
var cross = (a, b, c) => (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
|
|
1992
|
+
var isPointOnSegment = (point, a, b) => {
|
|
1993
|
+
if (Math.abs(cross(a, b, point)) > EPSILON) return false;
|
|
1994
|
+
return point.x >= Math.min(a.x, b.x) - EPSILON && point.x <= Math.max(a.x, b.x) + EPSILON && point.y >= Math.min(a.y, b.y) - EPSILON && point.y <= Math.max(a.y, b.y) + EPSILON;
|
|
1995
|
+
};
|
|
1996
|
+
var segmentsIntersect = (a1, a2, b1, b2) => {
|
|
1997
|
+
const c1 = cross(a1, a2, b1);
|
|
1998
|
+
const c2 = cross(a1, a2, b2);
|
|
1999
|
+
const c3 = cross(b1, b2, a1);
|
|
2000
|
+
const c4 = cross(b1, b2, a2);
|
|
2001
|
+
if ((c1 > EPSILON && c2 < -EPSILON || c1 < -EPSILON && c2 > EPSILON) && (c3 > EPSILON && c4 < -EPSILON || c3 < -EPSILON && c4 > EPSILON)) {
|
|
2002
|
+
return true;
|
|
2003
|
+
}
|
|
2004
|
+
return isPointOnSegment(b1, a1, a2) || isPointOnSegment(b2, a1, a2) || isPointOnSegment(a1, b1, b2) || isPointOnSegment(a2, b1, b2);
|
|
2005
|
+
};
|
|
2006
|
+
var getPolygonEdges = (polygon) => {
|
|
2007
|
+
const edges = [];
|
|
2008
|
+
if (polygon.points.length < 2) return edges;
|
|
2009
|
+
for (let i = 0; i < polygon.points.length; i += 1) {
|
|
2010
|
+
const a = polygon.points[i];
|
|
2011
|
+
const b = polygon.points[(i + 1) % polygon.points.length];
|
|
2012
|
+
if (!a || !b) continue;
|
|
2013
|
+
edges.push([a, b]);
|
|
2014
|
+
}
|
|
2015
|
+
return edges;
|
|
2016
|
+
};
|
|
2017
|
+
var isPointInPolygon = (point, polygon) => {
|
|
2018
|
+
for (const [a, b] of getPolygonEdges(polygon)) {
|
|
2019
|
+
if (isPointOnSegment(point, a, b)) return true;
|
|
2020
|
+
}
|
|
2021
|
+
let inside = false;
|
|
2022
|
+
for (let i = 0, j = polygon.points.length - 1; i < polygon.points.length; j = i++) {
|
|
2023
|
+
const pi = polygon.points[i];
|
|
2024
|
+
const pj = polygon.points[j];
|
|
2025
|
+
if (!pi || !pj) continue;
|
|
2026
|
+
const intersects = pi.y > point.y !== pj.y > point.y && point.x < (pj.x - pi.x) * (point.y - pi.y) / (pj.y - pi.y) + pi.x;
|
|
2027
|
+
if (intersects) inside = !inside;
|
|
2028
|
+
}
|
|
2029
|
+
return inside;
|
|
2030
|
+
};
|
|
2031
|
+
var rotatePoint = (point, angleRadians) => ({
|
|
2032
|
+
x: point.x * Math.cos(angleRadians) - point.y * Math.sin(angleRadians),
|
|
2033
|
+
y: point.x * Math.sin(angleRadians) + point.y * Math.cos(angleRadians)
|
|
2034
|
+
});
|
|
2035
|
+
var rectToPolygon = (rect) => {
|
|
2036
|
+
const halfWidth = rect.width / 2;
|
|
2037
|
+
const halfHeight = rect.height / 2;
|
|
2038
|
+
const angle = toRadians(rect.rotationDegrees);
|
|
2039
|
+
return {
|
|
2040
|
+
kind: "polygon",
|
|
2041
|
+
points: [
|
|
2042
|
+
{ x: -halfWidth, y: -halfHeight },
|
|
2043
|
+
{ x: halfWidth, y: -halfHeight },
|
|
2044
|
+
{ x: halfWidth, y: halfHeight },
|
|
2045
|
+
{ x: -halfWidth, y: halfHeight }
|
|
2046
|
+
].map((localPoint) => {
|
|
2047
|
+
const rotated = rotatePoint(localPoint, angle);
|
|
2048
|
+
return { x: rect.centerX + rotated.x, y: rect.centerY + rotated.y };
|
|
2049
|
+
})
|
|
2050
|
+
};
|
|
2051
|
+
};
|
|
2052
|
+
|
|
2053
|
+
// lib/shape-distances/decompose-copper-into-shapes.ts
|
|
2054
|
+
var isFiniteNumber = (value) => typeof value === "number" && Number.isFinite(value);
|
|
2055
|
+
var createWireSegmentRect = (start, end, width) => ({
|
|
2056
|
+
kind: "rect",
|
|
2057
|
+
centerX: (start.x + end.x) / 2,
|
|
2058
|
+
centerY: (start.y + end.y) / 2,
|
|
2059
|
+
width: Math.hypot(end.x - start.x, end.y - start.y),
|
|
2060
|
+
height: width,
|
|
2061
|
+
rotationDegrees: Math.atan2(end.y - start.y, end.x - start.x) * 180 / Math.PI
|
|
2062
|
+
});
|
|
2063
|
+
var decomposeCopperIntoShapes = (element) => {
|
|
2064
|
+
const elm = element;
|
|
2065
|
+
const shapes = [];
|
|
2066
|
+
if (elm.type === "pcb_smtpad") {
|
|
2067
|
+
if (elm.shape === "circle" && isFiniteNumber(elm.radius)) {
|
|
2068
|
+
shapes.push({ kind: "circle", x: elm.x, y: elm.y, radius: elm.radius });
|
|
2069
|
+
}
|
|
2070
|
+
if (elm.shape === "rect" && isFiniteNumber(elm.width) && isFiniteNumber(elm.height)) {
|
|
2071
|
+
shapes.push({
|
|
2072
|
+
kind: "rect",
|
|
2073
|
+
centerX: elm.x,
|
|
2074
|
+
centerY: elm.y,
|
|
2075
|
+
width: elm.width,
|
|
2076
|
+
height: elm.height,
|
|
2077
|
+
rotationDegrees: isFiniteNumber(elm.ccw_rotation) ? elm.ccw_rotation : 0
|
|
2078
|
+
});
|
|
2079
|
+
}
|
|
2080
|
+
if (elm.shape === "polygon" && Array.isArray(elm.points)) {
|
|
2081
|
+
const points = elm.points.filter(
|
|
2082
|
+
(point) => isFiniteNumber(point?.x) && isFiniteNumber(point?.y)
|
|
2083
|
+
);
|
|
2084
|
+
if (points.length >= 3) {
|
|
2085
|
+
shapes.push({ kind: "polygon", points });
|
|
2086
|
+
}
|
|
2087
|
+
}
|
|
2088
|
+
return shapes;
|
|
2089
|
+
}
|
|
2090
|
+
if (elm.type === "pcb_trace" && Array.isArray(elm.route)) {
|
|
2091
|
+
for (let i = 0; i < elm.route.length - 1; i += 1) {
|
|
2092
|
+
const start = elm.route[i];
|
|
2093
|
+
const end = elm.route[i + 1];
|
|
2094
|
+
if (!start || !end || start.route_type !== "wire" || end.route_type !== "wire" || !isFiniteNumber(start.x) || !isFiniteNumber(start.y) || !isFiniteNumber(end.x) || !isFiniteNumber(end.y)) {
|
|
2095
|
+
continue;
|
|
2096
|
+
}
|
|
2097
|
+
const width = isFiniteNumber(start.width) ? Math.max(0, start.width) : 0;
|
|
2098
|
+
const radius = width / 2;
|
|
2099
|
+
shapes.push({ kind: "circle", x: start.x, y: start.y, radius });
|
|
2100
|
+
shapes.push({ kind: "circle", x: end.x, y: end.y, radius });
|
|
2101
|
+
if (Math.hypot(end.x - start.x, end.y - start.y) > EPSILON) {
|
|
2102
|
+
shapes.push(createWireSegmentRect(start, end, width));
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
return shapes;
|
|
2106
|
+
}
|
|
2107
|
+
if (elm.type === "pcb_via" && isFiniteNumber(elm.outer_diameter)) {
|
|
2108
|
+
shapes.push({
|
|
2109
|
+
kind: "circle",
|
|
2110
|
+
x: elm.x,
|
|
2111
|
+
y: elm.y,
|
|
2112
|
+
radius: elm.outer_diameter / 2
|
|
2113
|
+
});
|
|
2114
|
+
return shapes;
|
|
2115
|
+
}
|
|
2116
|
+
if (elm.type === "pcb_plated_hole") {
|
|
2117
|
+
if (isFiniteNumber(elm.outer_diameter)) {
|
|
2118
|
+
shapes.push({
|
|
2119
|
+
kind: "circle",
|
|
2120
|
+
x: elm.x,
|
|
2121
|
+
y: elm.y,
|
|
2122
|
+
radius: elm.outer_diameter / 2
|
|
2123
|
+
});
|
|
2124
|
+
}
|
|
2125
|
+
if (isFiniteNumber(elm.rect_pad_width) && isFiniteNumber(elm.rect_pad_height)) {
|
|
2126
|
+
shapes.push({
|
|
2127
|
+
kind: "rect",
|
|
2128
|
+
centerX: elm.x,
|
|
2129
|
+
centerY: elm.y,
|
|
2130
|
+
width: elm.rect_pad_width,
|
|
2131
|
+
height: elm.rect_pad_height,
|
|
2132
|
+
rotationDegrees: isFiniteNumber(elm.rect_ccw_rotation) ? elm.rect_ccw_rotation : 0
|
|
2133
|
+
});
|
|
2134
|
+
}
|
|
2135
|
+
if (isFiniteNumber(elm.outer_width) && isFiniteNumber(elm.outer_height)) {
|
|
2136
|
+
shapes.push({
|
|
2137
|
+
kind: "rect",
|
|
2138
|
+
centerX: elm.x,
|
|
2139
|
+
centerY: elm.y,
|
|
2140
|
+
width: elm.outer_width,
|
|
2141
|
+
height: elm.outer_height,
|
|
2142
|
+
rotationDegrees: 0
|
|
2143
|
+
});
|
|
2144
|
+
}
|
|
2145
|
+
return shapes;
|
|
2146
|
+
}
|
|
2147
|
+
return shapes;
|
|
2148
|
+
};
|
|
2149
|
+
|
|
2150
|
+
// lib/shape-distances/distance-between-shapes.ts
|
|
2151
|
+
var distanceBetweenCircleAndCircle = (a, b) => Math.max(0, Math.hypot(a.x - b.x, a.y - b.y) - a.radius - b.radius);
|
|
2152
|
+
var distanceBetweenPolygonAndPolygon = (a, b) => {
|
|
2153
|
+
if (a.points.length < 3 || b.points.length < 3) {
|
|
2154
|
+
return Number.POSITIVE_INFINITY;
|
|
2155
|
+
}
|
|
2156
|
+
for (const [a1, a2] of getPolygonEdges(a)) {
|
|
2157
|
+
for (const [b1, b2] of getPolygonEdges(b)) {
|
|
2158
|
+
if (segmentsIntersect(a1, a2, b1, b2)) {
|
|
2159
|
+
return 0;
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
const aPoint = a.points[0];
|
|
2164
|
+
const bPoint = b.points[0];
|
|
2165
|
+
if (aPoint && isPointInPolygon(aPoint, b) || bPoint && isPointInPolygon(bPoint, a)) {
|
|
2166
|
+
return 0;
|
|
2167
|
+
}
|
|
2168
|
+
let minDistance = Number.POSITIVE_INFINITY;
|
|
2169
|
+
for (const [a1, a2] of getPolygonEdges(a)) {
|
|
2170
|
+
for (const [b1, b2] of getPolygonEdges(b)) {
|
|
2171
|
+
minDistance = Math.min(
|
|
2172
|
+
minDistance,
|
|
2173
|
+
distanceBetweenPointAndSegment(a1, b1, b2),
|
|
2174
|
+
distanceBetweenPointAndSegment(a2, b1, b2),
|
|
2175
|
+
distanceBetweenPointAndSegment(b1, a1, a2),
|
|
2176
|
+
distanceBetweenPointAndSegment(b2, a1, a2)
|
|
2177
|
+
);
|
|
2178
|
+
}
|
|
2179
|
+
}
|
|
2180
|
+
return minDistance;
|
|
2181
|
+
};
|
|
2182
|
+
var distanceBetweenCircleAndPolygon = (circle, polygon) => {
|
|
2183
|
+
if (polygon.points.length < 3) {
|
|
2184
|
+
return Number.POSITIVE_INFINITY;
|
|
2185
|
+
}
|
|
2186
|
+
if (isPointInPolygon({ x: circle.x, y: circle.y }, polygon)) {
|
|
2187
|
+
return 0;
|
|
2188
|
+
}
|
|
2189
|
+
let minDistanceToEdge = Number.POSITIVE_INFINITY;
|
|
2190
|
+
for (const [start, end] of getPolygonEdges(polygon)) {
|
|
2191
|
+
minDistanceToEdge = Math.min(
|
|
2192
|
+
minDistanceToEdge,
|
|
2193
|
+
distanceBetweenPointAndSegment({ x: circle.x, y: circle.y }, start, end)
|
|
2194
|
+
);
|
|
2195
|
+
}
|
|
2196
|
+
return Math.max(0, minDistanceToEdge - circle.radius);
|
|
2197
|
+
};
|
|
2198
|
+
var distanceBetweenShapes = (a, b) => {
|
|
2199
|
+
if (a.kind === "circle" && b.kind === "circle") {
|
|
2200
|
+
return distanceBetweenCircleAndCircle(a, b);
|
|
2201
|
+
}
|
|
2202
|
+
if (a.kind === "rect" && b.kind === "rect") {
|
|
2203
|
+
return distanceBetweenPolygonAndPolygon(rectToPolygon(a), rectToPolygon(b));
|
|
2204
|
+
}
|
|
2205
|
+
if (a.kind === "circle" && b.kind === "rect") {
|
|
2206
|
+
return distanceBetweenCircleAndPolygon(a, rectToPolygon(b));
|
|
2207
|
+
}
|
|
2208
|
+
if (a.kind === "rect" && b.kind === "circle") {
|
|
2209
|
+
return distanceBetweenCircleAndPolygon(b, rectToPolygon(a));
|
|
2210
|
+
}
|
|
2211
|
+
if (a.kind === "circle" && b.kind === "polygon") {
|
|
2212
|
+
return distanceBetweenCircleAndPolygon(a, b);
|
|
2213
|
+
}
|
|
2214
|
+
if (a.kind === "polygon" && b.kind === "circle") {
|
|
2215
|
+
return distanceBetweenCircleAndPolygon(b, a);
|
|
2216
|
+
}
|
|
2217
|
+
if (a.kind === "rect" && b.kind === "polygon") {
|
|
2218
|
+
return distanceBetweenPolygonAndPolygon(rectToPolygon(a), b);
|
|
2219
|
+
}
|
|
2220
|
+
if (a.kind === "polygon" && b.kind === "rect") {
|
|
2221
|
+
return distanceBetweenPolygonAndPolygon(a, rectToPolygon(b));
|
|
2222
|
+
}
|
|
2223
|
+
if (a.kind === "polygon" && b.kind === "polygon") {
|
|
2224
|
+
return distanceBetweenPolygonAndPolygon(a, b);
|
|
2225
|
+
}
|
|
2226
|
+
return Number.POSITIVE_INFINITY;
|
|
2227
|
+
};
|
|
2228
|
+
|
|
2229
|
+
// lib/compute-gap-between-copper.ts
|
|
2230
|
+
var computeGapBetweenCopper = (elm1, elm2) => {
|
|
2231
|
+
const shapes1 = decomposeCopperIntoShapes(elm1);
|
|
2232
|
+
const shapes2 = decomposeCopperIntoShapes(elm2);
|
|
2233
|
+
if (shapes1.length === 0 || shapes2.length === 0) {
|
|
2234
|
+
return Number.POSITIVE_INFINITY;
|
|
2235
|
+
}
|
|
2236
|
+
let minimumGap = Number.POSITIVE_INFINITY;
|
|
2237
|
+
for (const shape1 of shapes1) {
|
|
2238
|
+
for (const shape2 of shapes2) {
|
|
2239
|
+
minimumGap = Math.min(minimumGap, distanceBetweenShapes(shape1, shape2));
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
return minimumGap;
|
|
2243
|
+
};
|
|
1895
2244
|
export {
|
|
1896
2245
|
applySelector,
|
|
1897
2246
|
applySelectorAST,
|
|
1898
2247
|
buildSubtree,
|
|
1899
2248
|
cju_default as cju,
|
|
1900
2249
|
cju_indexed_default as cjuIndexed,
|
|
2250
|
+
computeGapBetweenCopper,
|
|
1901
2251
|
directionToVec,
|
|
1902
2252
|
findBoundsAndCenter,
|
|
1903
2253
|
getBoardBounds,
|