Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions lib/solvers/TraceCleanupSolver/simplifyPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,36 @@ import {
isVertical,
} from "lib/solvers/SchematicTraceLinesSolver/SchematicTraceSingleLineSolver2/collisions"

const EPS = 1e-9

const isDuplicate = (a: Point, b: Point): boolean =>
Math.abs(a.x - b.x) < EPS && Math.abs(a.y - b.y) < EPS

/**
* Remove duplicate consecutive points from a path.
* These can appear after path concatenation in _applyBestRoute.
*/
const removeDuplicateConsecutivePoints = (path: Point[]): Point[] => {
if (path.length < 2) return path
const result: Point[] = [path[0]]
for (let i = 1; i < path.length; i++) {
if (!isDuplicate(result[result.length - 1], path[i])) {
result.push(path[i])
}
}
return result
}

export const simplifyPath = (path: Point[]): Point[] => {
if (path.length < 3) return path
const newPath: Point[] = [path[0]]
for (let i = 1; i < path.length - 1; i++) {
// First remove any duplicate consecutive points
const deduped = removeDuplicateConsecutivePoints(path)

if (deduped.length < 3) return deduped
const newPath: Point[] = [deduped[0]]
for (let i = 1; i < deduped.length - 1; i++) {
const p1 = newPath[newPath.length - 1]
const p2 = path[i]
const p3 = path[i + 1]
const p2 = deduped[i]
const p3 = deduped[i + 1]
if (
(isVertical(p1, p2) && isVertical(p2, p3)) ||
(isHorizontal(p1, p2) && isHorizontal(p2, p3))
Expand All @@ -19,7 +42,7 @@ export const simplifyPath = (path: Point[]): Point[] => {
}
newPath.push(p2)
}
newPath.push(path[path.length - 1])
newPath.push(deduped[deduped.length - 1])

if (newPath.length < 3) return newPath
const finalPath: Point[] = [newPath[0]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ import { visualizeTightRectangle } from "../visualizeTightRectangle"
import { visualizeCandidates } from "./visualizeCandidates"
import { mergeGraphicsObjects } from "../mergeGraphicsObjects"
import { visualizeCollision } from "./visualizeCollision"
const EPS = 1e-9

/**
* Remove duplicate consecutive points from a path.
* Path concatenation in _applyBestRoute can produce duplicate points
* at the junctions, which render as zero-length extra trace segments.
*/
const removeDuplicateConsecutivePoints = (
path: Array<{ x: number; y: number }>,
) => {
if (path.length < 2) return path
const result = [path[0]]
for (let i = 1; i < path.length; i++) {
const prev = result[result.length - 1]
const curr = path[i]
if (Math.abs(prev.x - curr.x) >= EPS || Math.abs(prev.y - curr.y) >= EPS) {
result.push(curr)
}
}
return result
}

/**
* Defines the input structure for the UntangleTraceSubsolver.
Expand Down Expand Up @@ -258,11 +279,11 @@ export class UntangleTraceSubsolver extends BaseSolver {
p.x === this.currentLShape!.p2.x && p.y === this.currentLShape!.p2.y,
)
if (p2Index !== -1) {
const newTracePath = [
const newTracePath = removeDuplicateConsecutivePoints([
...originalTrace.tracePath.slice(0, p2Index),
...bestRoute,
...originalTrace.tracePath.slice(p2Index + 1),
]
])
this.input.allTraces[traceIndex] = {
...originalTrace,
tracePath: newTracePath,
Expand Down
98 changes: 98 additions & 0 deletions tests/assets/example_issue78.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"chips": [
{
"chipId": "schematic_component_0",
"center": { "x": 0, "y": 0 },
"width": 2.4000000000000004,
"height": 1,
"pins": [
{ "pinId": "U1.1", "x": 1.2000000000000002, "y": -0.30000000000000004 },
{
"pinId": "U1.2",
"x": -1.2000000000000002,
"y": -0.30000000000000004
},
{ "pinId": "U1.3", "x": 1.2000000000000002, "y": 0.09999999999999998 },
{ "pinId": "U1.4", "x": -1.2000000000000002, "y": 0.30000000000000004 },
{ "pinId": "U1.5", "x": -1.2000000000000002, "y": 0.10000000000000003 },
{
"pinId": "U1.6",
"x": -1.2000000000000002,
"y": -0.09999999999999998
},
{ "pinId": "U1.7", "x": 1.2000000000000002, "y": -0.10000000000000003 },
{ "pinId": "U1.8", "x": 1.2000000000000002, "y": 0.30000000000000004 }
]
},
{
"chipId": "schematic_component_1",
"center": { "x": 2.45, "y": 1.0 },
"width": 1.0999999999999996,
"height": 0.84,
"pins": [
{ "pinId": "R1.1", "x": 2.45, "y": 1.42 },
{ "pinId": "R1.2", "x": 2.45, "y": 0.58 }
]
},
{
"chipId": "schematic_component_2",
"center": { "x": 3.0, "y": -0.10000000000000009 },
"width": 1.1,
"height": 0.388910699999999,
"pins": [
{ "pinId": "R2.1", "x": 3.55, "y": -0.10000000000000002 },
{ "pinId": "R2.2", "x": 2.45, "y": -0.10000000000000016 }
]
},
{
"chipId": "schematic_component_3",
"center": { "x": -1.5, "y": -3.0 },
"width": 1.06,
"height": 1.1,
"pins": [
{ "pinId": "C1.1", "x": -1.5, "y": -2.45 },
{ "pinId": "C1.2", "x": -1.5, "y": -3.55 }
]
},
{
"chipId": "schematic_component_4",
"center": { "x": -2.75, "y": 0 },
"width": 1.06,
"height": 1.1,
"pins": [
{ "pinId": "C2.1", "x": -2.75, "y": 0.55 },
{ "pinId": "C2.2", "x": -2.75, "y": -0.55 }
]
},
{
"chipId": "schematic_component_5",
"center": { "x": 1.5, "y": -3.2 },
"width": 1.2,
"height": 0.8,
"pins": [
{ "pinId": "J1.1", "x": 0.9, "y": -2.9 },
{ "pinId": "J1.2", "x": 0.9, "y": -3.2 },
{ "pinId": "J1.3", "x": 0.9, "y": -3.5 }
]
}
],
"directConnections": [
{ "pinIds": ["U1.5", "C2.1"], "netId": "U1.CTRL to C2.pin1" },
{ "pinIds": ["U1.6", "U1.2"], "netId": "U1.THRES to U1.TRIG" },
{ "pinIds": ["R1.2", "U1.7"], "netId": "R1.pin2 to U1.DISCH" },
{ "pinIds": ["U1.7", "R2.2"], "netId": "U1.DISCH to R2.pin2" },
{ "pinIds": ["R2.1", "U1.6"], "netId": "R2.pin1 to U1.THRES" },
{ "pinIds": ["U1.6", "C1.1"], "netId": "U1.THRES to C1.pin1" },
{ "pinIds": ["U1.3", "J1.2"], "netId": "U1.OUT to J1.OUT" },
{ "pinIds": ["R1.2", "U1.8"], "netId": "R1.pin2 to U1.VCC" }
],
"netConnections": [
{ "netId": "GND", "pinIds": ["U1.1", "C1.2", "C2.2", "J1.3"] },
{ "netId": "VCC", "pinIds": ["U1.4", "U1.8", "R1.1", "J1.1"] }
],
"availableNetLabelOrientations": {
"VCC": ["y+"],
"GND": ["y-"]
},
"maxMspPairDistance": 6
}
Loading