| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include <assert.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <limits.h> |
| 17 |
#include <pathplan/vis.h> |
| 18 |
#include <util/alloc.h> |
| 19 |
|
| 20 |
typedef Ppoint_t ilcoord_t; |
| 21 |
|
| 22 |
#ifdef DEBUG |
| 23 |
static void printVconfig(vconfig_t * cp); |
| 24 |
static void printVis(char *lbl, COORD * vis, int n); |
| 25 |
static void printDad(int *vis, int n); |
| 26 |
#endif |
| 27 |
|
| 28 |
vconfig_t *Pobsopen(Ppoly_t ** obs, int n_obs) |
| 29 |
{ |
| 30 |
vconfig_t *rv; |
| 31 |
int poly_i, pt_i, i; |
| 32 |
int start, end; |
| 33 |
|
| 34 |
rv = malloc(sizeof(vconfig_t)); |
| 35 |
if (!rv) { |
| 36 |
return NULL; |
| 37 |
} |
| 38 |
|
| 39 |
/* get storage */ |
| 40 |
size_t n = 0; |
| 41 |
for (poly_i = 0; poly_i < n_obs; poly_i++) { |
| 42 |
n += obs[poly_i]->pn; |
| 43 |
} |
| 44 |
if (n > INT_MAX) { // will this overflow rv->N? |
| 45 |
free(rv); |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
rv->P = calloc(n, sizeof(Ppoint_t)); |
| 49 |
assert(n_obs >= 0); |
| 50 |
rv->start = calloc((size_t)n_obs + 1, sizeof(int)); |
| 51 |
rv->next = calloc(n, sizeof(int)); |
| 52 |
rv->prev = calloc(n, sizeof(int)); |
| 53 |
rv->N = (int)n; |
| 54 |
rv->Npoly = n_obs; |
| 55 |
|
| 56 |
// bail out if any above allocations failed |
| 57 |
if (rv->start == NULL || (n > 0 && (rv->P == NULL || |
| 58 |
rv->next == NULL || |
| 59 |
rv->prev == NULL))) { |
| 60 |
free(rv->prev); |
| 61 |
free(rv->next); |
| 62 |
free(rv->start); |
| 63 |
free(rv->P); |
| 64 |
free(rv); |
| 65 |
return NULL; |
| 66 |
} |
| 67 |
|
| 68 |
/* build arrays */ |
| 69 |
i = 0; |
| 70 |
for (poly_i = 0; poly_i < n_obs; poly_i++) { |
| 71 |
start = i; |
| 72 |
rv->start[poly_i] = start; |
| 73 |
assert(obs[poly_i]->pn <= INT_MAX); |
| 74 |
end = start + (int)obs[poly_i]->pn - 1; |
| 75 |
for (pt_i = 0; pt_i < (int)obs[poly_i]->pn; pt_i++) { |
| 76 |
rv->P[i] = obs[poly_i]->ps[pt_i]; |
| 77 |
rv->next[i] = i + 1; |
| 78 |
rv->prev[i] = i - 1; |
| 79 |
i++; |
| 80 |
} |
| 81 |
rv->next[end] = start; |
| 82 |
rv->prev[start] = end; |
| 83 |
} |
| 84 |
rv->start[poly_i] = i; |
| 85 |
visibility(rv); |
| 86 |
return rv; |
| 87 |
} |
| 88 |
|
| 89 |
void Pobsclose(vconfig_t * config) |
| 90 |
{ |
| 91 |
free(config->P); |
| 92 |
free(config->start); |
| 93 |
free(config->next); |
| 94 |
free(config->prev); |
| 95 |
if (config->vis) { |
| 96 |
free(config->vis[0]); |
| 97 |
free(config->vis); |
| 98 |
} |
| 99 |
free(config); |
| 100 |
} |
| 101 |
|
| 102 |
void Pobspath(vconfig_t *config, Ppoint_t p0, int poly0, Ppoint_t p1, int poly1, |
| 103 |
Ppolyline_t *output_route) { |
| 104 |
int i, *dad; |
| 105 |
size_t opn; |
| 106 |
Ppoint_t *ops; |
| 107 |
COORD *ptvis0, *ptvis1; |
| 108 |
|
| 109 |
ptvis0 = ptVis(config, poly0, p0); |
| 110 |
ptvis1 = ptVis(config, poly1, p1); |
| 111 |
|
| 112 |
dad = makePath(p0, poly0, ptvis0, p1, poly1, ptvis1, config); |
| 113 |
|
| 114 |
opn = 1; |
| 115 |
for (i = dad[config->N]; i != config->N + 1; i = dad[i]) |
| 116 |
opn++; |
| 117 |
opn++; |
| 118 |
ops = gv_calloc(opn, sizeof(Ppoint_t)); |
| 119 |
|
| 120 |
size_t j = opn - 1; |
| 121 |
ops[j--] = p1; |
| 122 |
for (i = dad[config->N]; i != config->N + 1; i = dad[i]) |
| 123 |
ops[j--] = config->P[i]; |
| 124 |
ops[j] = p0; |
| 125 |
assert(j == 0); |
| 126 |
|
| 127 |
#ifdef DEBUG |
| 128 |
printVconfig(config); |
| 129 |
printVis("p", ptvis0, config->N + 1); |
| 130 |
printVis("q", ptvis1, config->N + 1); |
| 131 |
printDad(dad, config->N + 1); |
| 132 |
#endif |
| 133 |
|
| 134 |
free(ptvis0); |
| 135 |
free(ptvis1); |
| 136 |
|
| 137 |
output_route->pn = opn; |
| 138 |
output_route->ps = ops; |
| 139 |
free(dad); |
| 140 |
} |
| 141 |
|
| 142 |
#ifdef DEBUG |
| 143 |
static void printVconfig(vconfig_t * cp) |
| 144 |
{ |
| 145 |
int i, j; |
| 146 |
int *next, *prev; |
| 147 |
Ppoint_t *pts; |
| 148 |
array2 arr; |
| 149 |
|
| 150 |
next = cp->next; |
| 151 |
prev = cp->prev; |
| 152 |
pts = cp->P; |
| 153 |
arr = cp->vis; |
| 154 |
|
| 155 |
printf("this next prev point\n"); |
| 156 |
for (i = 0; i < cp->N; i++) |
| 157 |
printf("%3d %3d %3d (%3g,%3g)\n", i, next[i], prev[i], |
| 158 |
pts[i].x, pts[i].y); |
| 159 |
|
| 160 |
printf("\n\n"); |
| 161 |
|
| 162 |
for (i = 0; i < cp->N; i++) { |
| 163 |
for (j = 0; j < cp->N; j++) |
| 164 |
printf("%4.1f ", arr[i][j]); |
| 165 |
printf("\n"); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
static void printVis(char *lbl, COORD * vis, int n) |
| 170 |
{ |
| 171 |
int i; |
| 172 |
|
| 173 |
printf("%s: ", lbl); |
| 174 |
for (i = 0; i < n; i++) |
| 175 |
printf("%4.1f ", vis[i]); |
| 176 |
printf("\n"); |
| 177 |
} |
| 178 |
|
| 179 |
static void printDad(int *vis, int n) |
| 180 |
{ |
| 181 |
int i; |
| 182 |
|
| 183 |
printf(" "); |
| 184 |
for (i = 0; i < n; i++) { |
| 185 |
printf("%3d ", i); |
| 186 |
} |
| 187 |
printf("\n"); |
| 188 |
printf("dad: "); |
| 189 |
for (i = 0; i < n; i++) { |
| 190 |
printf("%3d ", vis[i]); |
| 191 |
} |
| 192 |
printf("\n"); |
| 193 |
} |
| 194 |
#endif |