summaryrefslogtreecommitdiffstats
path: root/src/libavoid/shape.cpp
blob: 2b241b7283e255db6dd30e15e8b9f1cacbd18f16 (plain)
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
/*
 * 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 <cassert>

#include "libavoid/shape.h"
#include "libavoid/graph.h"  // For alertConns
#include "libavoid/vertices.h"
#include "libavoid/polyutil.h"
#include "libavoid/router.h"


namespace Avoid {


ShapeRef::ShapeRef(Router *router, unsigned int id, Polygn& ply)
    : _router(router)
    , _id(id)
    , _poly(copyPoly(ply))
    , _active(false)
    , _inMoveList(false)
    , _firstVert(NULL)
    , _lastVert(NULL)
{
    bool isShape = true;
    VertID i = VertID(id, isShape, 0);
    
    VertInf *last = NULL;
    VertInf *node = NULL;
    for (int pt_i = 0; pt_i < _poly.pn; pt_i++)
    {
        node = new VertInf(_router, i, _poly.ps[pt_i]);

        if (!_firstVert)
        {
            _firstVert = node;
        }
        else
        {
            node->shPrev = last;
            last->shNext = node;
            //node->lstPrev = last;
            //last->lstNext = node;
        }
        
        last = node;
        i++;
    }
    _lastVert = node;
    
    _lastVert->shNext = _firstVert;
    _firstVert->shPrev = _lastVert;
    
    makeActive();
}


ShapeRef::~ShapeRef()
{
    assert(_firstVert != NULL);
    
    makeInactive();

    VertInf *it = _firstVert;
    do
    {
        VertInf *tmp = it;
        it = it->shNext;

        delete tmp;
    }
    while (it != _firstVert);
    _firstVert = _lastVert = NULL;

    freePoly(_poly);
}


void ShapeRef::setNewPoly(Polygn& poly)
{
    assert(_firstVert != NULL);
    assert(_poly.pn == poly.pn);
    
    VertInf *curr = _firstVert;
    for (int pt_i = 0; pt_i < _poly.pn; pt_i++)
    {
        assert(curr->visListSize == 0);
        assert(curr->invisListSize == 0);

        // Reset with the new polygon point.
        curr->Reset(poly.ps[pt_i]);
        curr->pathNext = NULL;
        curr->pathDist = 0;
        
        curr = curr->shNext;
    }
    assert(curr == _firstVert);
        
    freePoly(_poly);
    _poly = copyPoly(poly);
}


void ShapeRef::makeActive(void)
{
    assert(!_active);
    
    // Add to connRefs list.
    _pos = _router->shapeRefs.insert(_router->shapeRefs.begin(), this);

    // Add points to vertex list.
    VertInf *it = _firstVert;
    do
    {
        VertInf *tmp = it;
        it = it->shNext;

        _router->vertices.addVertex(tmp);
    }
    while (it != _firstVert);
    
    _active = true;
}


void ShapeRef::makeInactive(void)
{
    assert(_active);
    
    // Remove from connRefs list.
    _router->shapeRefs.erase(_pos);

    // Remove points from vertex list.
    VertInf *it = _firstVert;
    do
    {
        VertInf *tmp = it;
        it = it->shNext;

        _router->vertices.removeVertex(tmp);
    }
    while (it != _firstVert);
    
    _active = false;
}
    

VertInf *ShapeRef::firstVert(void)
{
    return _firstVert;
}


VertInf *ShapeRef::lastVert(void)
{
    return _lastVert;
}


unsigned int ShapeRef::id(void)
{
    return _id;
}


Polygn ShapeRef::poly(void)
{
    return _poly;
}


Router *ShapeRef::router(void)
{
    return _router;
}


void ShapeRef::boundingBox(BBox& bbox)
{
    assert(_poly.pn > 0);

    bbox.a = bbox.b = _poly.ps[0];
    Point& a = bbox.a;
    Point& b = bbox.b;

    for (int i = 1; i < _poly.pn; ++i)
    {
        const Point& p = _poly.ps[i];

        a.x = (p.x < a.x) ? p.x : a.x;
        a.y = (p.y < a.y) ? p.y : a.y;
        b.x = (p.x > b.x) ? p.x : b.x;
        b.y = (p.y > b.y) ? p.y : b.y;
    }
}


void ShapeRef::removeFromGraph(void)
{
    for (VertInf *iter = firstVert(); iter != lastVert()->lstNext; )
    {
        VertInf *tmp = iter;
        iter = iter->lstNext;
        
        // For each vertex.
        EdgeInfList& visList = tmp->visList;
        EdgeInfList::iterator finish = visList.end();
        EdgeInfList::iterator edge;
        while ((edge = visList.begin()) != finish)
        {
            // Remove each visibility edge
            (*edge)->alertConns();
            delete (*edge);
        }

        EdgeInfList& invisList = tmp->invisList;
        finish = invisList.end();
        while ((edge = invisList.begin()) != finish)
        {
            // Remove each invisibility edge
            delete (*edge);
        }
    }
}


void ShapeRef::markForMove(void)
{
    if (!_inMoveList)
    {
        _inMoveList = true;
    }
    else
    {
        fprintf(stderr, "WARNING: two moves queued for same shape prior to "
                "rerouting.\n         This is not safe.\n");
    }
}


void ShapeRef::clearMoveMark(void)
{
    _inMoveList = false;
}


VertInf *ShapeRef::getPointVertex(const Point& point)
{
    VertInf *curr = _firstVert;
    do
    {
        if (curr->point == point)
        {
            return curr;
        }
        curr = curr->shNext;
    }
    while (curr != _firstVert);

    return NULL;
}


}