diff options
Diffstat (limited to 'tools/router/astar.c')
| -rw-r--r-- | tools/router/astar.c | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/tools/router/astar.c b/tools/router/astar.c new file mode 100644 index 0000000..3b3ccb5 --- /dev/null +++ b/tools/router/astar.c @@ -0,0 +1,136 @@ +// Grid A* maze router core, 2 layers (0 = F.Cu, 1 = B.Cu). +// Clearance uses exact distance fields: d1/n1 = distance to and net of nearest copper, +// d2 = distance to nearest copper of a net other than n1. Foreign distance for net X: +// n1 == X ? d2 : d1. +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> + +static int NX, NY, N; +static float *G = 0; static int32_t *PAR = 0; static uint32_t *STAMP = 0; static uint8_t *DIR = 0; static uint8_t *CLOSED = 0; +static uint32_t CALL = 0; +static int32_t *HEAPI = 0; static float *HEAPF = 0; static int HN = 0, HCAP = 0; + +int init(int nx, int ny) { + NX = nx; NY = ny; N = nx * ny; + free(G); free(PAR); free(STAMP); free(DIR); free(CLOSED); free(HEAPI); free(HEAPF); + G = malloc(sizeof(float) * 2 * N); PAR = malloc(sizeof(int32_t) * 2 * N); + STAMP = calloc(2 * N, sizeof(uint32_t)); DIR = malloc(2 * N); CLOSED = malloc(2 * N); + HCAP = 1 << 22; HEAPI = malloc(sizeof(int32_t) * HCAP); HEAPF = malloc(sizeof(float) * HCAP); + CALL = 0; + return (G && PAR && STAMP && DIR && CLOSED && HEAPI && HEAPF) ? 0 : -1; +} + +static void hpush(float f, int32_t i) { + if (HN >= HCAP) { HCAP *= 2; HEAPI = realloc(HEAPI, sizeof(int32_t) * HCAP); HEAPF = realloc(HEAPF, sizeof(float) * HCAP); } + int k = HN++; + while (k > 0) { int p = (k - 1) >> 1; if (HEAPF[p] <= f) break; HEAPF[k] = HEAPF[p]; HEAPI[k] = HEAPI[p]; k = p; } + HEAPF[k] = f; HEAPI[k] = i; +} +static int32_t hpop(void) { + int32_t top = HEAPI[0]; float lf = HEAPF[--HN]; int32_t li = HEAPI[HN]; + int k = 0; + while (1) { int c = 2 * k + 1; if (c >= HN) break; if (c + 1 < HN && HEAPF[c + 1] < HEAPF[c]) c++; if (HEAPF[c] >= lf) break; HEAPF[k] = HEAPF[c]; HEAPI[k] = HEAPI[c]; k = c; } + HEAPF[k] = lf; HEAPI[k] = li; + return top; +} + +static const int DX[8] = {1, 1, 0, -1, -1, -1, 0, 1}; +static const int DY[8] = {0, 1, 1, 1, 0, -1, -1, -1}; + +static inline float fdist(const float *d1, const int32_t *n1, const float *d2, int32_t s, int net) { + return n1[s] == net ? d2[s] : d1[s]; +} +static inline float heur(int x, int y, int hx, int hy) { + int ax = abs(x - hx), ay = abs(y - hy); + return (ax > ay ? ax : ay) + 0.41421356f * (ax < ay ? ax : ay); +} + +// Returns path length (cells, target back to source) or -1. +// neck flag per output cell: 1 if only the neck radius passed. +int route(const float *d1, const int32_t *n1, const float *d2, const float *padd, const float *costmap, + int net, const int32_t *src, int nsrc, const uint8_t *tgt, int goal_layer1, + int hx, int hy, + float r_nom, float r_neck, float r_via, float r_vpad, float r_diag, + const int32_t *neckbox, int nbox, + float via_cost, float via_cmult, float bmult, float turn_cost, int allow_via, int max_expand, + int32_t *out, uint8_t *outneck, int maxout, int layer_mask) +{ + CALL++; + if (CALL == 0) { memset(STAMP, 0, sizeof(uint32_t) * 2 * N); CALL = 1; } + HN = 0; + for (int i = 0; i < nsrc; i++) { + int32_t s = src[i]; + int L = s / N; if (!((layer_mask >> L) & 1)) continue; + float sfd = fdist(d1, n1, d2, s, net); + if (sfd < (r_neck > 0 ? r_neck : r_nom)) continue; // a track must fit at its start cell + STAMP[s] = CALL; G[s] = 0; PAR[s] = -1; DIR[s] = 255; CLOSED[s] = 0; + int c = s % N; + hpush(goal_layer1 ? 0 : heur(c % NX, c / NX, hx, hy), s); + } + int expanded = 0, found = -1; + while (HN > 0) { + int32_t s = hpop(); + if (CLOSED[s]) continue; + CLOSED[s] = 1; + int L = s / N; int c = s % N; int x = c % NX, y = c / NX; + if ((tgt && tgt[s]) || (goal_layer1 && L == 1)) { found = s; break; } + if (++expanded > max_expand) break; + float gcur = G[s]; + int pd = DIR[s]; + for (int d = 0; d < 8; d++) { + int qx = x + DX[d], qy = y + DY[d]; + if (qx < 0 || qy < 0 || qx >= NX || qy >= NY) continue; + int nc = qy * NX + qx; int32_t ns = (int32_t)(L * N + nc); + float fd = fdist(d1, n1, d2, ns, net); + float need = r_nom + ((d & 1) ? r_diag : 0); + int neck = 0; + if (fd < need) { + if (r_neck <= 0) continue; + float needn = r_neck + ((d & 1) ? r_diag : 0); + if (fd < needn) continue; + int nb = 0; + for (int b = 0; b < nbox; b++) { if (qx >= neckbox[4*b] && qx <= neckbox[4*b+2] && qy >= neckbox[4*b+1] && qy <= neckbox[4*b+3]) { nb = 1; break; } } + if (!nb) continue; + neck = 1; + } + float step = (d & 1) ? 1.41421356f : 1.0f; + float cst = step * (L ? bmult : 1.0f) + (costmap ? costmap[(size_t)L * N + nc] * step : 0); + if (neck) cst += 0.3f * step; + if (pd != 255 && pd != d) { + int t = abs(pd - d); if (t > 4) t = 8 - t; + if (t >= 3) continue; + cst += turn_cost * t; + } + float ng = gcur + cst; + if (STAMP[ns] == CALL && (CLOSED[ns] || G[ns] <= ng)) continue; + STAMP[ns] = CALL; G[ns] = ng; PAR[ns] = s; DIR[ns] = (uint8_t)d; CLOSED[ns] = 0; + hpush(ng + (goal_layer1 ? 0 : heur(qx, qy, hx, hy)), ns); + } + if (allow_via && ((layer_mask & 3) == 3)) { + int32_t s0 = c, s1 = N + c; + if (fdist(d1, n1, d2, s0, net) >= r_via && fdist(d1, n1, d2, s1, net) >= r_via && + padd[s0] >= r_vpad && padd[s1] >= r_vpad) { + int32_t ns = (int32_t)((1 - L) * N + c); + float ng = gcur + via_cost + (costmap ? via_cmult * (costmap[c] + costmap[N + c]) : 0); + if (!(STAMP[ns] == CALL && (CLOSED[ns] || G[ns] <= ng))) { + STAMP[ns] = CALL; G[ns] = ng; PAR[ns] = s; DIR[ns] = 255; CLOSED[ns] = 0; + hpush(ng + (goal_layer1 ? 0 : heur(x, y, hx, hy)), ns); + } + } + } + } + if (found < 0) return -1; + int n = 0; int32_t s = found; + while (s >= 0 && n < maxout) { + int32_t p = PAR[s]; + float fd = fdist(d1, n1, d2, s, net); + // the same test the search used to enter the cell: a full-width track that barely + // fits (e.g. 0.5 mm beside a bus line) must not be drawn necked all along its run + int dd = DIR[s]; + outneck[n] = fd < r_nom + ((dd != 255 && (dd & 1)) ? r_diag : 0) ? 1 : 0; + out[n++] = s; s = p; + } + return n; +} |
