00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <vis.h>
00019
00020 #ifdef DMALLOC
00021 #include "dmalloc.h"
00022 #endif
00023
00024
00025 #ifdef TRANSPARENT
00026 #define INTERSECT(a,b,c,d,e) intersect1((a),(b),(c),(d),(e))
00027 #else
00028 #define INTERSECT(a,b,c,d,e) intersect((a),(b),(c),(d))
00029 #endif
00030
00031
00032
00033
00034
00035
00036
00037
00038 static array2 allocArray(int V, int extra)
00039 {
00040 int i, k;
00041 array2 arr;
00042 COORD *p;
00043
00044 arr = (COORD **) malloc((V + extra) * sizeof(COORD *));
00045 for (i = 0; i < V; i++) {
00046 p = (COORD *) malloc(V * sizeof(COORD));
00047 arr[i] = p;
00048 for (k = 0; k < V; k++) {
00049 *p++ = 0;
00050 }
00051 }
00052 for (i = V; i < V + extra; i++)
00053 arr[i] = (COORD *) 0;
00054
00055 return arr;
00056 }
00057
00058
00059
00060
00061 COORD area2(Ppoint_t a, Ppoint_t b, Ppoint_t c)
00062 {
00063 return ((a.y - b.y) * (c.x - b.x) - (c.y - b.y) * (a.x - b.x));
00064 }
00065
00066
00067
00068
00069
00070 int wind(Ppoint_t a, Ppoint_t b, Ppoint_t c)
00071 {
00072 COORD w;
00073
00074 w = ((a.y - b.y) * (c.x - b.x) - (c.y - b.y) * (a.x - b.x));
00075
00076 return (w > .0001) ? 1 : ((w < -.0001) ? -1 : 0);
00077 }
00078
00079 #if 0
00080
00081
00082
00083
00084 static int open_intersect(Ppoint_t a, Ppoint_t b, Ppoint_t c, Ppoint_t d)
00085 {
00086 return (((area2(a, b, c) > 0 && area2(a, b, d) < 0) ||
00087 (area2(a, b, c) < 0 && area2(a, b, d) > 0))
00088 &&
00089 ((area2(c, d, a) > 0 && area2(c, d, b) < 0) ||
00090 (area2(c, d, a) < 0 && area2(c, d, b) > 0)));
00091 }
00092 #endif
00093
00094
00095
00096
00097 int inBetween(Ppoint_t a, Ppoint_t b, Ppoint_t c)
00098 {
00099 if (a.x != b.x)
00100 return (((a.x < c.x) && (c.x < b.x))
00101 || ((b.x < c.x) && (c.x < a.x)));
00102 else
00103 return (((a.y < c.y) && (c.y < b.y))
00104 || ((b.y < c.y) && (c.y < a.y)));
00105 }
00106
00107
00108 #ifdef TRANSPARENT
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 static int intersect1(Ppoint_t a, Ppoint_t b, Ppoint_t q, Ppoint_t n,
00129 Ppoint_t p)
00130 {
00131 int w_abq;
00132 int w_abn;
00133 int w_qna;
00134 int w_qnb;
00135
00136 w_abq = wind(a, b, q);
00137 w_abn = wind(a, b, n);
00138
00139
00140 if ((w_abq == 0) && inBetween(a, b, q)) {
00141 return ((w_abn * wind(a, b, p) < 0) || (wind(p, q, n) > 0));
00142 } else {
00143 w_qna = wind(q, n, a);
00144 w_qnb = wind(q, n, b);
00145
00146
00147
00148 return (((w_abq * w_abn) < 0) && ((w_qna * w_qnb) < 0));
00149 }
00150 }
00151 #else
00152
00153
00154
00155
00156
00157
00158 int intersect(Ppoint_t a, Ppoint_t b, Ppoint_t c, Ppoint_t d)
00159 {
00160 int a_abc;
00161 int a_abd;
00162 int a_cda;
00163 int a_cdb;
00164
00165 a_abc = wind(a, b, c);
00166 if ((a_abc == 0) && inBetween(a, b, c)) {
00167 return 1;
00168 }
00169 a_abd = wind(a, b, d);
00170 if ((a_abd == 0) && inBetween(a, b, d)) {
00171 return 1;
00172 }
00173 a_cda = wind(c, d, a);
00174 a_cdb = wind(c, d, b);
00175
00176
00177
00178
00179 return (((a_abc * a_abd) < 0) && ((a_cda * a_cdb) < 0));
00180 }
00181 #endif
00182
00183
00184
00185
00186
00187 static int in_cone(Ppoint_t a0, Ppoint_t a1, Ppoint_t a2, Ppoint_t b)
00188 {
00189 int m = wind(b, a0, a1);
00190 int p = wind(b, a1, a2);
00191
00192 if (wind(a0, a1, a2) > 0)
00193 return (m >= 0 && p >= 0);
00194 else
00195 return (m >= 0 || p >= 0);
00196 }
00197
00198 #if 0
00199
00200
00201
00202
00203 static int in_open_cone(Ppoint_t a0, Ppoint_t a1, Ppoint_t a2, Ppoint_t b)
00204 {
00205 int m = wind(b, a0, a1);
00206 int p = wind(b, a1, a2);
00207
00208 if (wind(a0, a1, a2) >= 0)
00209 return (m > 0 && p > 0);
00210 else
00211 return (m > 0 || p > 0);
00212 }
00213 #endif
00214
00215
00216
00217
00218 COORD dist2(Ppoint_t a, Ppoint_t b)
00219 {
00220 COORD delx = a.x - b.x;
00221 COORD dely = a.y - b.y;
00222
00223 return (delx * delx + dely * dely);
00224 }
00225
00226
00227
00228
00229 static COORD dist(Ppoint_t a, Ppoint_t b)
00230 {
00231 return sqrt(dist2(a, b));
00232 }
00233
00234 static int inCone(int i, int j, Ppoint_t pts[], int nextPt[], int prevPt[])
00235 {
00236 return in_cone(pts[prevPt[i]], pts[i], pts[nextPt[i]], pts[j]);
00237 }
00238
00239
00240
00241
00242
00243 static int clear(Ppoint_t pti, Ppoint_t ptj,
00244 int start, int end,
00245 int V, Ppoint_t pts[], int nextPt[], int prevPt[])
00246 {
00247 int k;
00248
00249 for (k = 0; k < start; k++) {
00250 if (INTERSECT(pti, ptj, pts[k], pts[nextPt[k]], pts[prevPt[k]]))
00251 return 0;
00252 }
00253 for (k = end; k < V; k++) {
00254 if (INTERSECT(pti, ptj, pts[k], pts[nextPt[k]], pts[prevPt[k]]))
00255 return 0;
00256 }
00257 return 1;
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267 static void compVis(vconfig_t * conf, int start)
00268 {
00269 int V = conf->N;
00270 Ppoint_t *pts = conf->P;
00271 int *nextPt = conf->next;
00272 int *prevPt = conf->prev;
00273 array2 wadj = conf->vis;
00274 int j, i, previ;
00275 COORD d;
00276
00277 for (i = start; i < V; i++) {
00278
00279
00280
00281
00282 previ = prevPt[i];
00283 d = dist(pts[i], pts[previ]);
00284 wadj[i][previ] = d;
00285 wadj[previ][i] = d;
00286
00287
00288 if (previ == i - 1)
00289 j = i - 2;
00290 else
00291 j = i - 1;
00292 for (; j >= 0; j--) {
00293 if (inCone(i, j, pts, nextPt, prevPt) &&
00294 inCone(j, i, pts, nextPt, prevPt) &&
00295 clear(pts[i], pts[j], V, V, V, pts, nextPt, prevPt)) {
00296
00297 d = dist(pts[i], pts[j]);
00298 wadj[i][j] = d;
00299 wadj[j][i] = d;
00300 }
00301 }
00302 }
00303 }
00304
00305
00306
00307
00308
00309
00310 void visibility(vconfig_t * conf)
00311 {
00312 conf->vis = allocArray(conf->N, 2);
00313 compVis(conf, 0);
00314 }
00315
00316
00317
00318
00319
00320
00321 static int polyhit(vconfig_t * conf, Ppoint_t p)
00322 {
00323 int i;
00324 Ppoly_t poly;
00325
00326 for (i = 0; i < conf->Npoly; i++) {
00327 poly.ps = &(conf->P[conf->start[i]]);
00328 poly.pn = conf->start[i + 1] - conf->start[i];
00329 if (in_poly(poly, p))
00330 return i;
00331 }
00332 return POLYID_NONE;
00333 }
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 COORD *ptVis(vconfig_t * conf, int pp, Ppoint_t p)
00345 {
00346 int V = conf->N;
00347 Ppoint_t *pts = conf->P;
00348 int *nextPt = conf->next;
00349 int *prevPt = conf->prev;
00350 int k;
00351 int start, end;
00352 COORD *vadj;
00353 Ppoint_t pk;
00354 COORD d;
00355
00356 vadj = (COORD *) malloc((V + 2) * sizeof(COORD));
00357
00358
00359 if (pp == POLYID_UNKNOWN)
00360 pp = polyhit(conf, p);
00361 if (pp >= 0) {
00362 start = conf->start[pp];
00363 end = conf->start[pp + 1];
00364 } else {
00365 start = V;
00366 end = V;
00367 }
00368
00369 for (k = 0; k < start; k++) {
00370 pk = pts[k];
00371 if (in_cone(pts[prevPt[k]], pk, pts[nextPt[k]], p) &&
00372 clear(p, pk, start, end, V, pts, nextPt, prevPt)) {
00373
00374 d = dist(p, pk);
00375 vadj[k] = d;
00376 } else
00377 vadj[k] = 0;
00378 }
00379
00380 for (k = start; k < end; k++)
00381 vadj[k] = 0;
00382
00383 for (k = end; k < V; k++) {
00384 pk = pts[k];
00385 if (in_cone(pts[prevPt[k]], pk, pts[nextPt[k]], p) &&
00386 clear(p, pk, start, end, V, pts, nextPt, prevPt)) {
00387
00388 d = dist(p, pk);
00389 vadj[k] = d;
00390 } else
00391 vadj[k] = 0;
00392 }
00393 vadj[V] = 0;
00394 vadj[V + 1] = 0;
00395
00396 return vadj;
00397
00398 }
00399
00400
00401
00402
00403
00404
00405 int directVis(Ppoint_t p, int pp, Ppoint_t q, int qp, vconfig_t * conf)
00406 {
00407 int V = conf->N;
00408 Ppoint_t *pts = conf->P;
00409 int *nextPt = conf->next;
00410
00411 int k;
00412 int s1, e1;
00413 int s2, e2;
00414
00415 if (pp < 0) {
00416 s1 = 0;
00417 e1 = 0;
00418 if (qp < 0) {
00419 s2 = 0;
00420 e2 = 0;
00421 } else {
00422 s2 = conf->start[qp];
00423 e2 = conf->start[qp + 1];
00424 }
00425 } else if (qp < 0) {
00426 s1 = 0;
00427 e1 = 0;
00428 s2 = conf->start[pp];
00429 e2 = conf->start[pp + 1];
00430 } else if (pp <= qp) {
00431 s1 = conf->start[pp];
00432 e1 = conf->start[pp + 1];
00433 s2 = conf->start[qp];
00434 e2 = conf->start[qp + 1];
00435 } else {
00436 s1 = conf->start[qp];
00437 e1 = conf->start[qp + 1];
00438 s2 = conf->start[pp];
00439 e2 = conf->start[pp + 1];
00440 }
00441
00442 for (k = 0; k < s1; k++) {
00443 if (INTERSECT(p, q, pts[k], pts[nextPt[k]], pts[prevPt[k]]))
00444 return 0;
00445 }
00446 for (k = e1; k < s2; k++) {
00447 if (INTERSECT(p, q, pts[k], pts[nextPt[k]], pts[prevPt[k]]))
00448 return 0;
00449 }
00450 for (k = e2; k < V; k++) {
00451 if (INTERSECT(p, q, pts[k], pts[nextPt[k]], pts[prevPt[k]]))
00452 return 0;
00453 }
00454 return 1;
00455 }