connector.h
Go to the documentation of this file.
1 /*
2  * vim: ts=4 sw=4 et tw=0 wm=0
3  *
4  * libavoid - Fast, Incremental, Object-avoiding Line Router
5  *
6  * Copyright (C) 2004-2010 Monash University
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  * See the file LICENSE.LGPL distributed with the library.
13  *
14  * Licensees holding a valid commercial license may use this file in
15  * accordance with the commercial license agreement provided with the
16  * library.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21  *
22  * Author(s): Michael Wybrow <mjwybrow@users.sourceforge.net>
23 */
24 
27 
28 
29 #ifndef AVOID_CONNECTOR_H
30 #define AVOID_CONNECTOR_H
31 
32 #include <list>
33 #include <vector>
34 
35 #include "libavoid/vertices.h"
36 #include "libavoid/geometry.h"
37 #include "libavoid/connend.h"
38 
39 
40 namespace Avoid {
41 
42 class Router;
43 class ConnRef;
44 class JunctionRef;
45 class ShapeRef;
46 typedef std::list<ConnRef *> ConnRefList;
47 
48 
51 enum ConnType {
52  ConnType_None = 0,
60 };
61 
62 
79 class ConnRef
80 {
81  public:
93  ConnRef(Router *router, const unsigned int id = 0);
107  ConnRef(Router *router, const ConnEnd& src, const ConnEnd& dst,
108  const unsigned int id = 0);
110  ~ConnRef();
111 
121  void setEndpoints(const ConnEnd& srcPoint, const ConnEnd& dstPoint);
122 
130  void setSourceEndpoint(const ConnEnd& srcPoint);
131 
139  void setDestEndpoint(const ConnEnd& dstPoint);
140 
143  unsigned int id(void) const;
144 
147  Router *router(void) const;
148 
158  bool needsRepaint(void) const;
159 
172  const PolyLine& route(void) const;
173 
183  PolyLine& displayRoute(void);
184 
195  void setCallback(void (*cb)(void *), void *ptr);
196 
200  ConnType routingType(void) const;
201 
212  void setRoutingType(ConnType type);
213 
225  std::pair<JunctionRef *, ConnRef *> splitAtSegment(
226  const size_t segmentN);
227 
228  // @brief Returns the source endpoint vertex in the visibility graph.
229  // @returns The source endpoint vertex.
230  VertInf *src(void);
231  // @brief Returns the destination endpoint vertex in the
232  // visibility graph.
233  // @returns The destination endpoint vertex.
234  VertInf *dst(void);
235 
247  void setRoutingCheckpoints(const std::vector<Point>& checkpoints);
248 
253  std::vector<Point> routingCheckpoints(void) const;
254 
255  void set_route(const PolyLine& route);
256  void calcRouteDist(void);
257  void makeActive(void);
258  void makeInactive(void);
259  VertInf *start(void);
260  void removeFromGraph(void);
261  bool isInitialised(void);
262  void makePathInvalid(void);
263  void setHateCrossings(bool value);
264  bool doesHateCrossings(void);
265  void setEndpoint(const unsigned int type, const ConnEnd& connEnd);
266  bool setEndpoint(const unsigned int type, const VertID& pointID,
267  Point *pointSuggestion = NULL);
268  std::vector<Point> possibleDstPinPoints(void) const;
269 
270  private:
271  friend class Router;
272  friend class ConnEnd;
273  friend class JunctionRef;
274 
275  PolyLine& routeRef(void);
276  void freeRoutes(void);
277  void performCallback(void);
278  bool generatePath(void);
279  void generateCheckpointsPath(std::vector<Point>& path,
280  std::vector<VertInf *>& vertices);
281  void generateStandardPath(std::vector<Point>& path,
282  std::vector<VertInf *>& vertices);
283  void unInitialise(void);
284  void updateEndPoint(const unsigned int type, const ConnEnd& connEnd);
285  void common_updateEndPoint(const unsigned int type, ConnEnd connEnd);
286  void freeActivePins(void);
287 
288  Router *m_router;
289  unsigned int m_id;
290  ConnType m_type;
291  bool m_orthogonal;
292  bool m_needs_reroute_flag;
293  bool m_false_path;
294  bool m_needs_repaint;
295  bool m_active;
296  PolyLine m_route;
297  Polygon m_display_route;
298  double m_route_dist;
299  ConnRefList::iterator m_connrefs_pos;
300  VertInf *m_src_vert;
301  VertInf *m_dst_vert;
302  VertInf *m_start_vert;
303  bool m_initialised;
304  void (*m_callback_func)(void *);
305  void *m_connector;
306  bool m_hate_crossings;
307  ConnEnd *m_src_connend;
308  ConnEnd *m_dst_connend;
309  std::vector<Point> m_checkpoints;
310  std::vector<VertInf *> m_checkpoint_vertices;
311 };
312 
313 
314 typedef std::pair<Point *, ConnRef *> PtConnPtrPair;
315 
316 typedef std::vector< PtConnPtrPair > PointRepVector;
317 typedef std::list<std::pair<size_t, size_t> > NodeIndexPairLinkList;
318 
319 class PtOrder
320 {
321  public:
322  PtOrder();
323  ~PtOrder();
324  void addPoints(const size_t dim, const PtConnPtrPair& arg1,
325  const PtConnPtrPair& arg2);
326  void addOrderedPoints(const size_t dim, const PtConnPtrPair& innerArg,
327  const PtConnPtrPair& outerArg, bool swapped);
328  int positionFor(const size_t dim, const ConnRef *conn);
329 
330  private:
331  size_t insertPoint(const size_t dim, const PtConnPtrPair& point);
332  void sort(const size_t dim);
333 
334  // One for each dimension.
335  bool sorted[2];
336  PointRepVector nodes[2];
337  NodeIndexPairLinkList links[2];
338  PointRepVector sortedConnVector[2];
339 };
340 
341 typedef std::map<Avoid::Point,PtOrder> PtOrderMap;
342 typedef std::set<Avoid::Point> PointSet;
343 
344 
345 const unsigned int CROSSING_NONE = 0;
346 const unsigned int CROSSING_TOUCHES = 1;
347 const unsigned int CROSSING_SHARES_PATH = 2;
348 const unsigned int CROSSING_SHARES_PATH_AT_END = 4;
349 const unsigned int CROSSING_SHARES_FIXED_SEGMENT = 8;
350 
351 
352 typedef std::pair<int, unsigned int> CrossingsInfoPair;
353 typedef std::vector<Avoid::Point> PointList;
354 typedef std::vector<PointList> SharedPathList;
355 
356 class ConnectorCrossings
357 {
358  public:
359  ConnectorCrossings(Avoid::Polygon& poly, bool polyIsConn,
360  Avoid::Polygon& conn, ConnRef *polyConnRef = NULL,
361  ConnRef *connConnRef = NULL);
362  void clear(void);
363  void countForSegment(size_t cIndex, const bool finalSegment);
364 
365  Avoid::Polygon& poly;
366  bool polyIsConn;
367  Avoid::Polygon& conn;
368  bool checkForBranchingSegments;
369  ConnRef *polyConnRef;
370  ConnRef *connConnRef;
371 
372  unsigned int crossingCount;
373  unsigned int crossingFlags;
374  PointSet *crossingPoints;
375  PtOrderMap *pointOrders;
376  SharedPathList *sharedPaths;
377 };
378 
379 extern void splitBranchingSegments(Avoid::Polygon& poly, bool polyIsConn,
380  Avoid::Polygon& conn, const double tolerance = 0);
381 extern bool validateBendPoint(VertInf *aInf, VertInf *bInf, VertInf *cInf);
382 
383 }
384 
385 
386 #endif
387 
388