aboutsummaryrefslogtreecommitdiff
path: root/tools/router/astar.c
blob: 3b3ccb532a8e2d10378e7a7894259af6b604e771 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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;
}