-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
180 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { PCBTrace } from "@tscircuit/soup" | ||
|
||
function pdist(a: any, b: any) { | ||
return Math.hypot(a.x - b.x, a.y - b.y) | ||
} | ||
|
||
/** | ||
* Merge multiple routes into a single route. | ||
* | ||
* If the end of the next route is closer to the end of the previous route, | ||
* reverse the next route and append it to the previous route. | ||
*/ | ||
export const mergeRoutes = (routes: PCBTrace["route"][]) => { | ||
// routes = routes.filter((route) => route.length > 0) | ||
if (routes.some((r) => r.length === 0)) { | ||
throw new Error("Cannot merge routes with zero length") | ||
} | ||
// for (const route of routes) { | ||
// console.table(route) | ||
// } | ||
const merged: PCBTrace["route"] = [] | ||
// const reverse_log: boolean[] = [] | ||
|
||
// Determine if the first route should be reversed | ||
const first_route_fp = routes[0][0] | ||
const first_route_lp = routes[0][routes[0].length - 1] | ||
|
||
const second_route_fp = routes[1][0] | ||
const second_route_lp = routes[1][routes[1].length - 1] | ||
|
||
const best_reverse_dist = Math.min( | ||
pdist(first_route_fp, second_route_fp), | ||
pdist(first_route_fp, second_route_lp), | ||
) | ||
|
||
const best_normal_dist = Math.min( | ||
pdist(first_route_lp, second_route_fp), | ||
pdist(first_route_lp, second_route_lp), | ||
) | ||
|
||
if (best_reverse_dist < best_normal_dist) { | ||
merged.push(...routes[0].reverse()) | ||
// reverse_log.push(true) | ||
} else { | ||
merged.push(...routes[0]) | ||
// reverse_log.push(false) | ||
} | ||
|
||
for (let i = 1; i < routes.length; i++) { | ||
const last_merged_point = merged[merged.length - 1] | ||
const next_route = routes[i] | ||
|
||
const next_first_point = next_route[0] | ||
const next_last_point = next_route[next_route.length - 1] | ||
|
||
const distance_to_first = pdist(last_merged_point, next_first_point) | ||
const distance_to_last = pdist(last_merged_point, next_last_point) | ||
|
||
if (distance_to_first < distance_to_last) { | ||
// reverse_log.push(false) | ||
merged.push(...next_route) | ||
} else { | ||
// reverse_log.push(true) | ||
merged.push(...next_route.reverse()) | ||
} | ||
} | ||
// console.log(reverse_log) | ||
// console.table(merged) | ||
return merged | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Return pairs of adjacent elements in an array. | ||
*/ | ||
export function pairs<T>(arr: Array<T>): Array<[T, T]> { | ||
const result: Array<[T, T]> = [] | ||
for (let i = 0; i < arr.length - 1; i++) { | ||
result.push([arr[i], arr[i + 1]]) | ||
} | ||
return result | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.