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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
/*
* vim: ts=4 sw=4 et tw=0 wm=0
*
* libavoid - Fast, Incremental, Object-avoiding Line Router
* Copyright (C) 2004-2006 Michael Wybrow <mjwybrow@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <algorithm>
#include <cfloat>
#include "libavoid/shape.h"
#include "libavoid/debug.h"
#include "libavoid/visibility.h"
#include "libavoid/vertices.h"
#include "libavoid/graph.h"
#include "libavoid/geometry.h"
#include "libavoid/router.h"
#include <math.h>
#ifdef LINEDEBUG
#include "SDL_gfxPrimitives.h"
#endif
namespace Avoid {
void shapeVis(ShapeRef *shape)
{
Router *router = shape->router();
if ( !(router->InvisibilityGrph) )
{
// Clear shape from graph.
shape->removeFromGraph();
}
VertInf *shapeBegin = shape->firstVert();
VertInf *shapeEnd = shape->lastVert()->lstNext;
VertInf *pointsBegin = NULL;
if (router->IncludeEndpoints)
{
pointsBegin = router->vertices.connsBegin();
}
else
{
pointsBegin = router->vertices.shapesBegin();
}
for (VertInf *curr = shapeBegin; curr != shapeEnd; curr = curr->lstNext)
{
bool knownNew = true;
db_printf("-- CONSIDERING --\n");
curr->id.db_print();
db_printf("\tFirst Half:\n");
for (VertInf *j = pointsBegin ; j != curr; j = j->lstNext)
{
EdgeInf::checkEdgeVisibility(curr, j, knownNew);
}
db_printf("\tSecond Half:\n");
VertInf *pointsEnd = router->vertices.end();
for (VertInf *k = shapeEnd; k != pointsEnd; k = k->lstNext)
{
EdgeInf::checkEdgeVisibility(curr, k, knownNew);
}
}
}
void shapeVisSweep(ShapeRef *shape)
{
Router *router = shape->router();
if ( !(router->InvisibilityGrph) )
{
// Clear shape from graph.
shape->removeFromGraph();
}
VertInf *startIter = shape->firstVert();
VertInf *endIter = shape->lastVert()->lstNext;
for (VertInf *i = startIter; i != endIter; i = i->lstNext)
{
vertexSweep(i);
}
}
void vertexVisibility(VertInf *point, VertInf *partner, bool knownNew,
const bool gen_contains)
{
Router *router = point->_router;
const VertID& pID = point->id;
// Make sure we're only doing ptVis for endpoints.
assert(!(pID.isShape));
if ( !(router->InvisibilityGrph) )
{
point->removeFromGraph();
}
if (gen_contains && !(pID.isShape))
{
router->generateContains(point);
}
if (router->UseLeesAlgorithm)
{
vertexSweep(point);
}
else
{
VertInf *shapesEnd = router->vertices.end();
for (VertInf *k = router->vertices.shapesBegin(); k != shapesEnd;
k = k->lstNext)
{
EdgeInf::checkEdgeVisibility(point, k, knownNew);
}
if (router->IncludeEndpoints && partner)
{
EdgeInf::checkEdgeVisibility(point, partner, knownNew);
}
}
}
//============================================================================
// SWEEP CODE
//
static VertInf *centerInf;
static Point centerPoint;
static VertID centerID;
static double centerAngle;
class PointPair
{
public:
PointPair(VertInf *inf)
: vInf(inf)
{
double x = vInf->point.x - centerPoint.x;
double y = vInf->point.y - centerPoint.y;
angle = pos_to_angle(x, y);
}
bool operator==(const PointPair& rhs) const
{
if (vInf->id == rhs.vInf->id)
{
return true;
}
return false;
}
static double pos_to_angle(double x, double y)
{
double ang = atan(y / x);
ang = (ang * 180) / M_PI;
if (x < 0)
{
ang += 180;
}
else if (y < 0)
{
ang += 360;
}
return ang;
}
VertInf *vInf;
double angle;
};
typedef std::list<PointPair > VertList;
class EdgePair
{
public:
EdgePair(VertInf *v1, VertInf *v2, double d, double a)
: vInf1(v1), vInf2(v2), initdist(d), initangle(a)
{
currdist = initdist;
currangle = initangle;
}
bool operator<(const EdgePair& rhs) const
{
if (initdist == rhs.initdist)
{
// TODO: This is a bit of a hack, should be
// set by the call to the constructor.
return dist(centerPoint, vInf2->point) <
dist(centerPoint, rhs.vInf2->point);
}
return (initdist < rhs.initdist);
}
bool operator==(const EdgePair& rhs) const
{
if (((vInf1->id == rhs.vInf1->id) &&
(vInf2->id == rhs.vInf2->id)) ||
((vInf1->id == rhs.vInf2->id) &&
(vInf2->id == rhs.vInf1->id)))
{
return true;
}
return false;
}
bool operator!=(const EdgePair& rhs) const
{
if (((vInf1->id == rhs.vInf1->id) &&
(vInf2->id == rhs.vInf2->id)) ||
((vInf1->id == rhs.vInf2->id) &&
(vInf2->id == rhs.vInf1->id)))
{
return false;
}
return true;
}
void SetObsAng(double a)
{
obsAngle = fmod(initangle - (a - 180), 360);
//db_printf("SetObsAng: %.2f (from init %.2f, a %.2f)\n",
// obsAngle, initangle, a);
}
VertInf *vInf1;
VertInf *vInf2;
double initdist;
double initangle;
double currdist;
double currangle;
double obsAngle;
};
typedef std::set<EdgePair> EdgeSet;
static bool ppCompare(PointPair& pp1, PointPair& pp2)
{
if (pp1.angle == pp2.angle)
{
// If the points are colinear, then order them in increasing
// distance from the point we are sweeping around.
return dist(centerPoint, pp1.vInf->point) <
dist(centerPoint, pp2.vInf->point);
}
return pp1.angle < pp2.angle;
}
#define AHEAD 1
#define BEHIND -1
class isBoundingShape
{
public:
// constructor remembers the value provided
isBoundingShape(ShapeSet& set)
: ss(set)
{ }
// the following is an overloading of the function call operator
bool operator () (const PointPair& pp)
{
if (pp.vInf->id.isShape &&
(ss.find(pp.vInf->id.objID) != ss.end()))
{
return true;
}
return false;
}
private:
ShapeSet& ss;
};
static bool sweepVisible(EdgeSet& T, VertInf *currInf, VertInf *lastInf,
bool lastVisible, double lastAngle, int *blocker)
{
if (!lastInf || (lastAngle != centerAngle))
{
// Nothing before it on the current ray
EdgeSet::iterator closestIt = T.begin();
if (closestIt != T.end())
{
Point &e1 = (*closestIt).vInf1->point;
Point &e2 = (*closestIt).vInf2->point;
if (segmentIntersect(centerInf->point, currInf->point, e1, e2))
{
*blocker = (*closestIt).vInf1->id.objID;
return false;
}
}
}
else
{
// There was another point before this on the ray (lastInf)
if (!lastVisible)
{
*blocker = -1;
return false;
}
else
{
// Check if there is an edge in T that blocks the ray
// between lastInf and currInf.
EdgeSet::iterator tfin = T.end();
for (EdgeSet::iterator l = T.begin(); l != tfin; ++l)
{
Point &e1 = (*l).vInf1->point;
Point &e2 = (*l).vInf2->point;
if (segmentIntersect(lastInf->point, currInf->point, e1, e2))
{
*blocker = (*l).vInf1->id.objID;
return false;
}
}
}
}
return true;
}
void vertexSweep(VertInf *vert)
{
Router *router = vert->_router;
VertID& pID = vert->id;
Point& pPoint = vert->point;
centerInf = vert;
centerID = pID;
centerPoint = pPoint;
Point centerPt = pPoint;
centerAngle = -1;
// List of shape (and maybe endpt) vertices, except p
// Sort list, around
VertList v;
// Initialise the vertex list
VertInf *beginVert = router->vertices.connsBegin();
VertInf *endVert = router->vertices.end();
for (VertInf *inf = beginVert; inf != endVert; inf = inf->lstNext)
{
if (inf->id == centerID)
{
// Don't include the center point
continue;
}
if (inf->id.isShape)
{
// Add shape vertex
v.push_back(inf);
}
else
{
if (router->IncludeEndpoints)
{
if (centerID.isShape)
{
// Add endpoint vertex
v.push_back(inf);
}
else
{
// Center is an endpoint, so only include the other
// endpoint from the matching connector.
VertID partnerID = VertID(centerID.objID, false,
(centerID.vn == 1) ? 2 : 1);
if (inf->id == partnerID)
{
v.push_back(inf);
}
}
}
}
}
// TODO: This should be done with a sorted data type and insertion sort.
v.sort(ppCompare);
EdgeSet e;
ShapeSet& ss = router->contains[centerID];
// And edge to T that intersect the initial ray.
VertInf *last = router->vertices.end();
for (VertInf *k = router->vertices.shapesBegin(); k != last; )
{
VertID kID = k->id;
if (!(centerID.isShape) && (ss.find(kID.objID) != ss.end()))
{
unsigned int shapeID = kID.objID;
db_printf("Center is inside shape %u so ignore shape edges.\n",
shapeID);
// One of the endpoints is inside this shape so ignore it.
while ((k != last) && (k->id.objID == shapeID))
{
// And skip the other vertices from this shape.
k = k->lstNext;
}
continue;
}
VertInf *kPrev = k->shPrev;
if ((centerInf == k) || (centerInf == kPrev))
{
k = k->lstNext;
continue;
}
Point xaxis(DBL_MAX, centerInf->point.y);
if (segmentIntersect(centerInf->point, xaxis, kPrev->point, k->point))
{
double distance;
if (vecDir(centerInf->point, xaxis, kPrev->point) == BEHIND)
{
distance = dist(centerInf->point, kPrev->point);
}
else
{
distance = dist(centerInf->point, k->point);
}
EdgePair intPair = EdgePair(k, kPrev, distance, 0.0);
e.insert(intPair).first;
}
k = k->lstNext;
}
// Start the actual sweep.
db_printf("SWEEP: "); centerID.db_print(); db_printf("\n");
VertInf *lastInf = NULL;
double lastAngle = 0;
bool lastVisible = false;
int lastBlocker = 0;
isBoundingShape isBounding(router->contains[centerID]);
VertList::iterator vfst = v.begin();
VertList::iterator vfin = v.end();
for (VertList::iterator t = vfst; t != vfin; ++t)
{
VertInf *currInf = (*t).vInf;
VertID& currID = currInf->id;
Point& currPt = currInf->point;
centerAngle = (*t).angle;
#ifdef LINEDEBUG
Sint16 ppx = (int) centerPt.x;
Sint16 ppy = (int) centerPt.y;
Sint16 cx = (int) currPt.x;
Sint16 cy = (int) currPt.y;
#endif
double currDist = dist(centerPt, currPt);
db_printf("Dist: %.1f.\n", currDist);
EdgeInf *edge = EdgeInf::existingEdge(centerInf, currInf);
if (edge == NULL)
{
edge = new EdgeInf(centerInf, currInf);
}
// Ignore vertices from bounding shapes, if sweeping round an endpoint.
if (!(centerID.isShape) && isBounding(*t))
{
if (router->InvisibilityGrph)
{
// if p and t can't see each other, add blank edge
db_printf("\tSkipping visibility edge... \n\t\t");
edge->addBlocker(currInf->id.objID);
edge->db_print();
}
continue;
}
bool cone1 = true, cone2 = true;
if (centerID.isShape)
{
cone1 = inValidRegion(router->IgnoreRegions,
centerInf->shPrev->point, centerPoint,
centerInf->shNext->point, currInf->point);
}
if (currInf->id.isShape)
{
cone2 = inValidRegion(router->IgnoreRegions,
currInf->shPrev->point, currInf->point,
currInf->shNext->point, centerPoint);
}
if (!cone1 || !cone2)
{
lastInf = NULL;
if (router->InvisibilityGrph)
{
db_printf("\tSetting invisibility edge... \n\t\t");
edge->addBlocker(0);
edge->db_print();
}
}
else
{
int blocker = 0;
// Check visibility.
bool currVisible = sweepVisible(e, currInf,
lastInf, lastVisible, lastAngle, &blocker);
if (blocker == -1)
{
blocker = lastBlocker;
}
if (currVisible)
{
#ifdef LINEDEBUG
lineRGBA(avoid_screen, ppx, ppy, cx, cy, 255, 0, 0, 32);
#endif
db_printf("\tSetting visibility edge... \n\t\t");
edge->setDist(currDist);
edge->db_print();
}
else if (router->InvisibilityGrph)
{
db_printf("\tSetting invisibility edge... \n\t\t");
edge->addBlocker(blocker);
edge->db_print();
}
lastVisible = currVisible;
lastInf = currInf;
lastAngle = centerAngle;
lastBlocker = blocker;
}
if (currID.isShape)
{
// This is a shape edge
Point& prevPt = currInf->shPrev->point;
Point& nextPt = currInf->shNext->point;
int prevDir = vecDir(centerPt, currPt, prevPt);
EdgePair prevPair = EdgePair(currInf, currInf->shPrev,
currDist, centerAngle);
EdgeSet::iterator ePtr;
if (prevDir == BEHIND)
{
// XXX: Strangely e.find does not return the correct results.
// ePtr = e.find(prevPair);
ePtr = std::find(e.begin(), e.end(), prevPair);
if (ePtr != e.end())
{
e.erase(ePtr);
}
}
else if ((prevDir == AHEAD) && (currInf->shPrev != centerInf))
{
double x = prevPt.x - currPt.x;
double y = prevPt.y - currPt.y;
double angle = PointPair::pos_to_angle(x, y);
prevPair.SetObsAng(angle);
ePtr = e.insert(prevPair).first;
}
int nextDir = vecDir(centerPt, currPt, nextPt);
EdgePair nextPair = EdgePair(currInf, currInf->shNext,
currDist, centerAngle);
if (nextDir == BEHIND)
{
// XXX: Strangely e.find does not return the correct results.
// ePtr = e.find(nextPair);
ePtr = std::find(e.begin(), e.end(), nextPair);
if (ePtr != e.end())
{
e.erase(ePtr);
}
}
else if ((nextDir == AHEAD) && (currInf->shNext != centerInf))
{
double x = nextPt.x - currPt.x;
double y = nextPt.y - currPt.y;
double angle = PointPair::pos_to_angle(x, y);
nextPair.SetObsAng(angle);
ePtr = e.insert(nextPair).first;
}
}
#ifdef LINEDEBUG
SDL_Flip(avoid_screen);
#endif
}
}
}
|