Skip to content

Commit e6895bf

Browse files
committed
wip
1 parent b0f5a1d commit e6895bf

File tree

2 files changed

+270
-83
lines changed

2 files changed

+270
-83
lines changed

lib/solvers/HighDensityRepairSolver/HighDensityRepairSolver.ts

Lines changed: 106 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,107 @@ export interface HighDensityRepairSolverParams {
2020
connMap: ConnectivityMap
2121
}
2222

23+
export const SINGLE_NODE_REPAIR_ADJACENT_OBSTACLE_MARGIN_MM = 0.1
24+
25+
export const createNodeHdRoutesByNodeId = (
26+
hdRoutes: HighDensityIntraNodeRoute[],
27+
): Map<string, HighDensityIntraNodeRoute[]> => {
28+
const nodeHdRoutesByNodeId = new Map<string, HighDensityIntraNodeRoute[]>()
29+
30+
for (const hdRoute of hdRoutes) {
31+
const nodeRoutes =
32+
nodeHdRoutesByNodeId.get(hdRoute.capacityMeshNodeId) ?? []
33+
nodeRoutes.push(hdRoute)
34+
nodeHdRoutesByNodeId.set(hdRoute.capacityMeshNodeId, nodeRoutes)
35+
}
36+
37+
return nodeHdRoutesByNodeId
38+
}
39+
40+
const getObstacleBounds = (obstacle: Obstacle) => ({
41+
minX: obstacle.center.x - obstacle.width / 2,
42+
minY: obstacle.center.y - obstacle.height / 2,
43+
maxX: obstacle.center.x + obstacle.width / 2,
44+
maxY: obstacle.center.y + obstacle.height / 2,
45+
})
46+
47+
const getRectDistance = (
48+
a: { minX: number; minY: number; maxX: number; maxY: number },
49+
b: { minX: number; minY: number; maxX: number; maxY: number },
50+
) => {
51+
const dx = Math.max(a.minX - b.maxX, b.minX - a.maxX, 0)
52+
const dy = Math.max(a.minY - b.maxY, b.minY - a.maxY, 0)
53+
return Math.hypot(dx, dy)
54+
}
55+
56+
export const getAdjacentObstaclesForNode = (
57+
nodeWithPortPoints: NodeWithPortPoints,
58+
obstacleIndex: ObstacleSpatialHashIndex,
59+
adjacentObstacleMarginMm: number = SINGLE_NODE_REPAIR_ADJACENT_OBSTACLE_MARGIN_MM,
60+
): Obstacle[] => {
61+
const bounds = getBoundsFromNodeWithPortPoints(nodeWithPortPoints)
62+
const candidates = obstacleIndex.search({
63+
minX: bounds.minX - adjacentObstacleMarginMm,
64+
minY: bounds.minY - adjacentObstacleMarginMm,
65+
maxX: bounds.maxX + adjacentObstacleMarginMm,
66+
maxY: bounds.maxY + adjacentObstacleMarginMm,
67+
})
68+
69+
return candidates.filter((obstacle) => {
70+
const obstacleBounds = getObstacleBounds(obstacle)
71+
return getRectDistance(bounds, obstacleBounds) <= adjacentObstacleMarginMm
72+
})
73+
}
74+
75+
export const createSingleNodeHighDensityRepairParams = ({
76+
nodeWithPortPoints,
77+
obstacleIndex,
78+
nodeHdRoutesByNodeId,
79+
connMap,
80+
adjacentObstacleMarginMm = SINGLE_NODE_REPAIR_ADJACENT_OBSTACLE_MARGIN_MM,
81+
}: {
82+
nodeWithPortPoints: NodeWithPortPoints
83+
obstacleIndex: ObstacleSpatialHashIndex
84+
nodeHdRoutesByNodeId: Map<string, HighDensityIntraNodeRoute[]>
85+
connMap: ConnectivityMap
86+
adjacentObstacleMarginMm?: number
87+
}): SingleNodeHighDensityRepairParams => {
88+
return {
89+
nodeWithPortPoints,
90+
adjacentObstacles: getAdjacentObstaclesForNode(
91+
nodeWithPortPoints,
92+
obstacleIndex,
93+
adjacentObstacleMarginMm,
94+
),
95+
nodeHdRoutes:
96+
nodeHdRoutesByNodeId.get(nodeWithPortPoints.capacityMeshNodeId) ?? [],
97+
connMap,
98+
}
99+
}
100+
101+
export const createSingleNodeHighDensityRepairParamsList = ({
102+
nodePortPoints,
103+
obstacles,
104+
hdRoutes,
105+
connMap,
106+
adjacentObstacleMarginMm = SINGLE_NODE_REPAIR_ADJACENT_OBSTACLE_MARGIN_MM,
107+
}: HighDensityRepairSolverParams & {
108+
adjacentObstacleMarginMm?: number
109+
}): SingleNodeHighDensityRepairParams[] => {
110+
const obstacleIndex = new ObstacleSpatialHashIndex("flatbush", obstacles)
111+
const nodeHdRoutesByNodeId = createNodeHdRoutesByNodeId(hdRoutes)
112+
113+
return nodePortPoints.map((nodeWithPortPoints) =>
114+
createSingleNodeHighDensityRepairParams({
115+
nodeWithPortPoints,
116+
obstacleIndex,
117+
nodeHdRoutesByNodeId,
118+
connMap,
119+
adjacentObstacleMarginMm,
120+
}),
121+
)
122+
}
123+
23124
export class HighDensityRepairSolver extends BaseSolver {
24125
override getSolverName(): string {
25126
return "HighDensityRepairSolver"
@@ -39,15 +140,8 @@ export class HighDensityRepairSolver extends BaseSolver {
39140
"flatbush",
40141
params.obstacles,
41142
)
42-
this.nodeHdRoutesByNodeId = new Map()
143+
this.nodeHdRoutesByNodeId = createNodeHdRoutesByNodeId(params.hdRoutes)
43144
this.MAX_ITERATIONS = Math.max(1000, this.unprocessedNodes.length * 4)
44-
45-
for (const hdRoute of params.hdRoutes) {
46-
const nodeRoutes =
47-
this.nodeHdRoutesByNodeId.get(hdRoute.capacityMeshNodeId) ?? []
48-
nodeRoutes.push(hdRoute)
49-
this.nodeHdRoutesByNodeId.set(hdRoute.capacityMeshNodeId, nodeRoutes)
50-
}
51145
}
52146

53147
getConstructorParams(): [HighDensityRepairSolverParams] {
@@ -57,15 +151,12 @@ export class HighDensityRepairSolver extends BaseSolver {
57151
private getSingleNodeParams(
58152
nodeWithPortPoints: NodeWithPortPoints,
59153
): SingleNodeHighDensityRepairParams {
60-
const bounds = getBoundsFromNodeWithPortPoints(nodeWithPortPoints)
61-
return {
154+
return createSingleNodeHighDensityRepairParams({
62155
nodeWithPortPoints,
63-
adjacentObstacles: this.obstacleIndex.search(bounds),
64-
nodeHdRoutes:
65-
this.nodeHdRoutesByNodeId.get(nodeWithPortPoints.capacityMeshNodeId) ??
66-
[],
156+
obstacleIndex: this.obstacleIndex,
157+
nodeHdRoutesByNodeId: this.nodeHdRoutesByNodeId,
67158
connMap: this.params.connMap,
68-
}
159+
})
69160
}
70161

71162
_step() {

0 commit comments

Comments
 (0)