cde387c5b5be6443193aa6c2516b8acc8461df13
1 /*
2 * vim: ts=4 sw=4 et tw=0 wm=0
3 *
4 * libavoid - Fast, Incremental, Object-avoiding Line Router
5 * Copyright (C) 2004-2005 Michael Wybrow <mjwybrow@users.sourceforge.net>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
23 #include "libavoid/graph.h"
24 #include "libavoid/makepath.h"
25 #include "libavoid/visibility.h"
26 #include "libavoid/debug.h"
29 namespace Avoid {
32 ConnRefList connRefs;
35 ConnRef::ConnRef(const uint id)
36 : _id(id)
37 , _needs_reroute_flag(true)
38 , _false_path(false)
39 , _active(false)
40 , _route_dist(0)
41 , _srcVert(NULL)
42 , _dstVert(NULL)
43 , _initialised(false)
44 , _callback(NULL)
45 , _connector(NULL)
46 {
47 // TODO: Store endpoints and details.
48 _route.pn = 0;
49 _route.ps = NULL;
50 }
53 ConnRef::ConnRef(const uint id, const Point& src, const Point& dst)
54 : _id(id)
55 , _needs_reroute_flag(true)
56 , _false_path(false)
57 , _active(false)
58 , _route_dist(0)
59 , _srcVert(NULL)
60 , _dstVert(NULL)
61 , _initialised(false)
62 , _callback(NULL)
63 , _connector(NULL)
64 {
65 _route.pn = 0;
66 _route.ps = NULL;
68 if (IncludeEndpoints)
69 {
70 bool isShape = false;
71 _srcVert = new VertInf(VertID(id, isShape, 1), src);
72 _dstVert = new VertInf(VertID(id, isShape, 2), dst);
73 vertices.addVertex(_srcVert);
74 vertices.addVertex(_dstVert);
75 makeActive();
76 _initialised = true;
77 }
78 }
81 ConnRef::~ConnRef()
82 {
83 freeRoute();
85 if (_srcVert)
86 {
87 vertices.removeVertex(_srcVert);
88 delete _srcVert;
89 _srcVert = NULL;
90 }
92 if (_dstVert)
93 {
94 vertices.removeVertex(_dstVert);
95 delete _dstVert;
96 _dstVert = NULL;
97 }
99 if (_active)
100 {
101 makeInactive();
102 }
103 }
105 void ConnRef::updateEndPoint(const uint type, const Point& point)
106 {
107 assert((type == (uint) VertID::src) || (type == (uint) VertID::tar));
108 //assert(IncludeEndpoints);
110 VertInf *altered = NULL;
111 VertInf *partner = NULL;
112 bool isShape = false;
114 if (type == (uint) VertID::src)
115 {
116 if (_srcVert)
117 {
118 _srcVert->Reset(point);
119 }
120 else
121 {
122 _srcVert = new VertInf(VertID(_id, isShape, type), point);
123 vertices.addVertex(_srcVert);
124 }
126 altered = _srcVert;
127 partner = _dstVert;
128 }
129 else // if (type == (uint) VertID::dst)
130 {
131 if (_dstVert)
132 {
133 _dstVert->Reset(point);
134 }
135 else
136 {
137 _dstVert = new VertInf(VertID(_id, isShape, type), point);
138 vertices.addVertex(_dstVert);
139 }
141 altered = _dstVert;
142 partner = _srcVert;
143 }
145 bool knownNew = false;
146 vertexVisibility(altered, partner, knownNew, true);
147 }
150 void ConnRef::makeActive(void)
151 {
152 assert(!_active);
154 // Add to connRefs list.
155 _pos = connRefs.insert(connRefs.begin(), this);
156 _active = true;
157 }
160 void ConnRef::makeInactive(void)
161 {
162 assert(_active);
164 // Remove from connRefs list.
165 connRefs.erase(_pos);
166 _active = false;
167 }
170 void ConnRef::freeRoute(void)
171 {
172 if (_route.ps)
173 {
174 _route.pn = 0;
175 std::free(_route.ps);
176 _route.ps = NULL;
177 }
178 }
181 PolyLine& ConnRef::route(void)
182 {
183 return _route;
184 }
187 void ConnRef::calcRouteDist(void)
188 {
189 _route_dist = 0;
190 for (int i = 1; i < _route.pn; i++)
191 {
192 _route_dist += dist(_route.ps[i], _route.ps[i - 1]);
193 }
194 }
197 bool ConnRef::needsReroute(void)
198 {
199 return (_false_path || _needs_reroute_flag);
200 }
203 void ConnRef::moveRoute(const int& diff_x, const int& diff_y)
204 {
205 for (int i = 0; i < _route.pn; i++)
206 {
207 _route.ps[i].x += diff_x;
208 _route.ps[i].y += diff_y;
209 }
210 }
213 void ConnRef::lateSetup(const Point& src, const Point& dst)
214 {
215 assert(!_initialised);
217 bool isShape = false;
218 _srcVert = new VertInf(VertID(_id, isShape, 1), src);
219 _dstVert = new VertInf(VertID(_id, isShape, 2), dst);
220 vertices.addVertex(_srcVert);
221 vertices.addVertex(_dstVert);
222 makeActive();
223 _initialised = true;
224 }
227 VertInf *ConnRef::src(void)
228 {
229 return _srcVert;
230 }
233 VertInf *ConnRef::dst(void)
234 {
235 return _dstVert;
236 }
239 bool ConnRef::isInitialised(void)
240 {
241 return _initialised;
242 }
245 void ConnRef::unInitialise(void)
246 {
247 vertices.removeVertex(_srcVert);
248 vertices.removeVertex(_dstVert);
249 makeInactive();
250 _initialised = false;
251 }
254 void ConnRef::removeFromGraph(void)
255 {
256 for (VertInf *iter = _srcVert; iter != NULL; )
257 {
258 VertInf *tmp = iter;
259 iter = (iter == _srcVert) ? _dstVert : NULL;
261 // For each vertex.
262 EdgeInfList& visList = tmp->visList;
263 EdgeInfList::iterator finish = visList.end();
264 EdgeInfList::iterator edge;
265 while ((edge = visList.begin()) != finish)
266 {
267 // Remove each visibility edge
268 delete (*edge);
269 }
271 EdgeInfList& invisList = tmp->invisList;
272 finish = invisList.end();
273 while ((edge = invisList.begin()) != finish)
274 {
275 // Remove each invisibility edge
276 delete (*edge);
277 }
278 }
279 }
282 void ConnRef::setCallback(void (*cb)(void *), void *ptr)
283 {
284 _callback = cb;
285 _connector = ptr;
286 }
289 void ConnRef::handleInvalid(void)
290 {
291 if (_false_path || _needs_reroute_flag) {
292 if (_callback) {
293 _callback(_connector);
294 }
295 }
296 }
299 void ConnRef::makePathInvalid(void)
300 {
301 _needs_reroute_flag = true;
302 }
305 int ConnRef::generatePath(Point p0, Point p1)
306 {
307 if (!_false_path && !_needs_reroute_flag) {
308 // This connector is up to date.
309 return (int) false;
310 }
312 _false_path = false;
313 _needs_reroute_flag = false;
315 VertInf *src = _srcVert;
316 VertInf *tar = _dstVert;
318 if (!IncludeEndpoints)
319 {
320 lateSetup(p0, p1);
322 // Update as they have just been set by lateSetup.
323 src = _srcVert;
324 tar = _dstVert;
326 bool knownNew = true;
327 vertexVisibility(src, tar, knownNew);
328 vertexVisibility(tar, src, knownNew);
329 }
331 bool *flag = &(_needs_reroute_flag);
333 makePath(this, flag);
335 bool result = true;
337 int pathlen = 1;
338 for (VertInf *i = tar; i != src; i = i->pathNext)
339 {
340 pathlen++;
341 if (i == NULL)
342 {
343 db_printf("Warning: Path not found...\n");
344 pathlen = 2;
345 tar->pathNext = src;
346 if (InvisibilityGrph)
347 {
348 // TODO: Could we know this edge already?
349 EdgeInf *edge = EdgeInf::existingEdge(src, tar);
350 assert(edge != NULL);
351 edge->addCycleBlocker();
352 }
353 result = false;
354 break;
355 }
356 if (pathlen > 100)
357 {
358 fprintf(stderr, "ERROR: Should never be here...\n");
359 exit(1);
360 }
361 }
362 Point *path = (Point *) malloc(pathlen * sizeof(Point));
364 int j = pathlen - 1;
365 for (VertInf *i = tar; i != src; i = i->pathNext)
366 {
367 if (InvisibilityGrph)
368 {
369 // TODO: Again, we could know this edge without searching.
370 EdgeInf *edge = EdgeInf::existingEdge(i, i->pathNext);
371 edge->addConn(flag);
372 }
373 else
374 {
375 _false_path = true;
376 }
377 path[j--] = i->point;
378 }
379 path[0] = src->point;
382 // Would clear visibility for endpoints here if required.
384 PolyLine& output_route = route();
385 output_route.pn = pathlen;
386 output_route.ps = path;
388 return (int) result;
389 }
392 //============================================================================
395 // It's intended this function is called after shape movement has
396 // happened to alert connectors that they need to be rerouted.
397 void callbackAllInvalidConnectors(void)
398 {
399 ConnRefList::iterator fin = connRefs.end();
400 for (ConnRefList::iterator i = connRefs.begin(); i != fin; ++i) {
401 (*i)->handleInvalid();
402 }
403 }
406 }