4 * Topological Autorouter for
5 * PCB, interactive printed circuit board design
6 * Copyright (C) 2009 Anthony Blake
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * Contact addresses for email:
23 * Anthony Blake, tonyb33@gmail.com
27 #ifndef __TOPOROUTER_INCLUDED__
28 #define __TOPOROUTER_INCLUDED__
33 #include "autoroute.h"
57 #define TOPOROUTER_FLAG_VERBOSE (1<<0)
58 #define TOPOROUTER_FLAG_HARDDEST (1<<1)
59 #define TOPOROUTER_FLAG_HARDSRC (1<<2)
60 #define TOPOROUTER_FLAG_MATCH (1<<3)
61 #define TOPOROUTER_FLAG_LAYERHINT (1<<4)
62 #define TOPOROUTER_FLAG_LEASTINVALID (1<<5)
63 #define TOPOROUTER_FLAG_AFTERORDER (1<<6)
64 #define TOPOROUTER_FLAG_AFTERRUBIX (1<<7)
65 #define TOPOROUTER_FLAG_GOFAR (1<<8)
67 #if TOPO_OUTPUT_ENABLED
71 #define EPSILON 0.0001f
75 #define coord_distance(a,b,c,d) sqrt(pow(a-c,2)+pow(b-d,2))
76 #define coord_distance2(a,b,c,d) (pow(a-c,2)+pow(b-d,2))
78 #define tvdistance(a,b) sqrt(pow(vx(a)-vx(b),2)+pow(vy(a)-vy(b),2))
79 #define tvdistance2(a,b) (pow(vx(a)-vx(b),2)+pow(vy(a)-vy(b),2))
81 #define edge_v1(e) (GTS_SEGMENT(e)->v1)
82 #define edge_v2(e) (GTS_SEGMENT(e)->v2)
83 #define tedge_v1(e) (TOPOROUTER_VERTEX(GTS_SEGMENT(e)->v1))
84 #define tedge_v2(e) (TOPOROUTER_VERTEX(GTS_SEGMENT(e)->v2))
86 #define tedge(v1,v2) TOPOROUTER_EDGE(gts_vertices_are_connected(GTS_VERTEX(v1), GTS_VERTEX(v2)))
88 #define edge_routing(e) (TOPOROUTER_IS_CONSTRAINT(e) ? TOPOROUTER_CONSTRAINT(e)->routing : e->routing)
89 #define vrouting(v) (edge_routing(v->routingedge))
91 #define edge_routing_next(e,list) ((list->next) ? TOPOROUTER_VERTEX(list->next->data) : TOPOROUTER_VERTEX(edge_v2(e)))
92 #define edge_routing_prev(e,list) ((list->prev) ? TOPOROUTER_VERTEX(list->prev->data) : TOPOROUTER_VERTEX(edge_v1(e)))
94 #define vx(v) (GTS_POINT(v)->x)
95 #define vy(v) (GTS_POINT(v)->y)
96 #define vz(v) (GTS_POINT(v)->z)
98 #define close_enough_xy(a,b) (vx(a) > vx(b) - EPSILON && vx(a) < vx(b) + EPSILON && vy(a) > vy(b) - EPSILON && vy(a) < vy(b) + EPSILON)
100 #define tev1x(e) (vx(tedge_v1(e))
101 #define tev1y(e) (vy(tedge_v1(e))
102 #define tev2x(e) (vx(tedge_v2(e))
103 #define tev2y(e) (vy(tedge_v2(e))
105 #define TOPOROUTER_IS_BBOX(obj) (gts_object_is_from_class (obj, toporouter_bbox_class ()))
106 #define TOPOROUTER_BBOX(obj) GTS_OBJECT_CAST (obj, toporouter_bbox_t, toporouter_bbox_class ())
107 #define TOPOROUTER_BBOX_CLASS(klass) GTS_OBJECT_CLASS_CAST (klass, toporouter_bbox_class_t, toporouter_bbox_class ())
123 struct _toporouter_bbox_t
{
126 toporouter_term_t type
;
131 GtsTriangle
*enclosing
;
137 // char *netlist, *style;
139 struct _toporouter_cluster_t
*cluster
;
143 struct _toporouter_bbox_class_t
{
144 GtsBBoxClass parent_class
;
147 typedef struct _toporouter_bbox_t toporouter_bbox_t
;
148 typedef struct _toporouter_bbox_class_t toporouter_bbox_class_t
;
150 #define TOPOROUTER_IS_EDGE(obj) (gts_object_is_from_class (obj, toporouter_edge_class ()))
151 #define TOPOROUTER_EDGE(obj) GTS_OBJECT_CAST (obj, toporouter_edge_t, toporouter_edge_class ())
152 #define TOPOROUTER_EDGE_CLASS(klass) GTS_OBJECT_CLASS_CAST (klass, toporouter_edge_class_t, toporouter_edge_class ())
154 #define EDGE_FLAG_DIRECTCONNECTION (1<<0)
156 struct _toporouter_edge_t
{
158 //NetListType *netlist;
165 struct _toporouter_edge_class_t
{
166 GtsEdgeClass parent_class
;
169 typedef struct _toporouter_edge_t toporouter_edge_t
;
170 typedef struct _toporouter_edge_class_t toporouter_edge_class_t
;
172 #define TOPOROUTER_IS_VERTEX(obj) (gts_object_is_from_class (obj, toporouter_vertex_class ()))
173 #define TOPOROUTER_VERTEX(obj) GTS_OBJECT_CAST (obj, toporouter_vertex_t, toporouter_vertex_class ())
174 #define TOPOROUTER_VERTEX_CLASS(klass) GTS_OBJECT_CLASS_CAST (klass, toporouter_vertex_class_t, toporouter_vertex_class ())
176 #define VERTEX_FLAG_VIZ (1<<1)
177 #define VERTEX_FLAG_CCW (1<<2)
178 #define VERTEX_FLAG_CW (1<<3)
179 #define VERTEX_FLAG_RED (1<<4)
180 #define VERTEX_FLAG_GREEN (1<<5)
181 #define VERTEX_FLAG_BLUE (1<<6)
182 #define VERTEX_FLAG_TEMP (1<<7)
183 #define VERTEX_FLAG_ROUTE (1<<8)
184 #define VERTEX_FLAG_FAKE (1<<10)
185 #define VERTEX_FLAG_SPECCUT (1<<11)
187 struct _toporouter_vertex_t
{
190 struct _toporouter_bbox_t
*bbox
;
192 struct _toporouter_vertex_t
*parent
;
193 struct _toporouter_vertex_t
*child
;
195 toporouter_edge_t
*routingedge
;
199 gdouble gcost
, hcost
;
202 struct _toporouter_arc_t
*arc
;
204 struct _toporouter_oproute_t
*oproute
;
205 struct _toporouter_route_t
*route
;
211 struct _toporouter_vertex_class_t
{
212 GtsVertexClass parent_class
;
215 typedef struct _toporouter_vertex_t toporouter_vertex_t
;
216 typedef struct _toporouter_vertex_class_t toporouter_vertex_class_t
;
218 #define TOPOROUTER_IS_CONSTRAINT(obj) (gts_object_is_from_class (obj, toporouter_constraint_class ()))
219 #define TOPOROUTER_CONSTRAINT(obj) GTS_OBJECT_CAST (obj, toporouter_constraint_t, toporouter_constraint_class ())
220 #define TOPOROUTER_CONSTRAINT_CLASS(klass) GTS_OBJECT_CLASS_CAST (klass, toporouter_constraint_class_t, toporouter_constraint_class ())
222 struct _toporouter_constraint_t
{
224 toporouter_bbox_t
*box
;
228 struct _toporouter_constraint_class_t
{
229 GtsConstraintClass parent_class
;
234 } toporouter_spoint_t
;
236 typedef struct _toporouter_constraint_t toporouter_constraint_t
;
237 typedef struct _toporouter_constraint_class_t toporouter_constraint_class_t
;
248 } toporouter_direction_t
;
253 toporouter_vertex_t
*a
, *b
, *p
;
255 char *netlist
, *style
;
256 } toporouter_attachment_t
;
259 toporouter_vertex_t
*v
;
261 } toporouter_visibility_t
;
266 // GtsVertex *v1, *v2, *v3;
272 } toporouter_layer_t
;
274 #define TOPOROUTER_VERTEX_REGION(x) ((toporouter_vertex_region_t *)x)
278 toporouter_vertex_t
*v1
, *v2
;
279 toporouter_vertex_t
*origin
;
281 } toporouter_vertex_region_t
;
283 struct _toporouter_rubberband_arc_t
{
284 toporouter_vertex_t
*pathv
, *arcv
;
290 typedef struct _toporouter_rubberband_arc_t toporouter_rubberband_arc_t
;
291 #define TOPOROUTER_RUBBERBAND_ARC(x) ((toporouter_rubberband_arc_t *)x)
293 struct _toporouter_route_t
{
295 struct _toporouter_netlist_t
*netlist
;
297 struct _toporouter_cluster_t
*src
, *dest
;
298 struct _toporouter_cluster_t
*psrc
, *pdest
;
300 gdouble score
, detourscore
;
302 toporouter_vertex_t
*curpoint
;
303 GHashTable
*alltemppoints
;
309 GList
*destvertices
, *srcvertices
;
319 typedef struct _toporouter_route_t toporouter_route_t
;
321 #define TOPOROUTER_ROUTE(x) ((toporouter_route_t *)x)
323 struct _toporouter_netlist_t
{
324 GPtrArray
*clusters
, *routes
;
325 char *netlist
, *style
;
329 typedef struct _toporouter_netlist_t toporouter_netlist_t
;
331 #define TOPOROUTER_NETLIST(x) ((toporouter_netlist_t *)x)
333 struct _toporouter_cluster_t
{
336 toporouter_netlist_t
*netlist
;
339 typedef struct _toporouter_cluster_t toporouter_cluster_t
;
341 #define TOPOROUTER_CLUSTER(x) ((toporouter_cluster_t *)x)
343 #define TOPOROUTER_OPROUTE(x) ((toporouter_oproute_t *)x)
345 #define oproute_next(a,b) (b->next ? TOPOROUTER_ARC(b->next->data) : a->term2)
346 #define oproute_prev(a,b) (b->prev ? TOPOROUTER_ARC(b->prev->data) : a->term1)
348 #define TOPOROUTER_SERPINTINE(x) ((toporouter_serpintine_t *)x)
350 struct _toporouter_serpintine_t
{
353 gdouble x0
, y0
, x1
, y1
;
356 gdouble halfa
, radius
;
360 typedef struct _toporouter_serpintine_t toporouter_serpintine_t
;
362 struct _toporouter_oproute_t
{
364 toporouter_vertex_t
*term1
, *term2
;
365 char *style
; char *netlist
;
370 toporouter_serpintine_t
*serp
;
373 typedef struct _toporouter_oproute_t toporouter_oproute_t
;
376 #define TOPOROUTER_IS_ARC(obj) (gts_object_is_from_class (obj, toporouter_arc_class()))
377 #define TOPOROUTER_ARC(obj) GTS_OBJECT_CAST (obj, toporouter_arc_t, toporouter_arc_class())
378 #define TOPOROUTER_ARC_CLASS(klass) GTS_OBJECT_CLASS_CAST (klass, toporouter_arc_class_t, toporouter_arc_class())
380 struct _toporouter_arc_t
{
383 gdouble x0
, y0
, x1
, y1
;
384 toporouter_vertex_t
*centre
, *v
;
390 toporouter_oproute_t
*oproute
;
392 toporouter_vertex_t
*v1
, *v2
;
395 struct _toporouter_arc_class_t
{
396 GtsObjectClass parent_class
;
400 typedef struct _toporouter_arc_t toporouter_arc_t
;
401 typedef struct _toporouter_arc_class_t toporouter_arc_class_t
;
403 typedef struct _toporouter_t toporouter_t
;
410 guint
*pairwise_nodetour
;
411 gdouble pairwise_detour_sum
;
413 guint pairwise_fails
;
415 toporouter_route_t
*routedata
;
419 } toporouter_netscore_t
;
421 #define TOPOROUTER_NETSCORE(x) ((toporouter_netscore_t *)x)
423 struct _toporouter_t
{
427 toporouter_layer_t
*layers
;
431 GList
*keepoutlayers
;
435 GList
*destboxes
, *consumeddestboxes
;
441 gdouble serpintine_half_amplitude
;
443 gdouble wiring_score
;
448 GList
*routednets
, *failednets
;
450 gint (*netsort
)(toporouter_netscore_t
**, toporouter_netscore_t
**);
452 struct timeval starttime
;
457 typedef gint (*oproute_adjseg_func
)
462 gdouble
, gdouble
, gdouble
, gdouble
,
463 toporouter_oproute_t
*,
464 toporouter_oproute_t
*);
469 cairo_surface_t
*surface
;
472 double s
; /* scale factor */
478 double iw
, ih
; /* image dimensions */
481 #define FOREACH_CLUSTER(clusters) do { \
482 for(toporouter_cluster_t **i = ((toporouter_cluster_t **)clusters->pdata) + clusters->len - 1; i >= (toporouter_cluster_t **)clusters->pdata && clusters->len > 0; --i) { \
483 toporouter_cluster_t *cluster = *i;
485 #define FOREACH_BBOX(boxes) do { \
486 for(toporouter_bbox_t **i = ((toporouter_bbox_t **)boxes->pdata) + boxes->len - 1; i >= (toporouter_bbox_t **)boxes->pdata && boxes->len > 0; --i) { \
487 toporouter_bbox_t *box = *i;
489 #define FOREACH_ROUTE(routes) do { \
490 for(toporouter_route_t **i = ((toporouter_route_t **)routes->pdata) + routes->len - 1; i >= (toporouter_route_t **)routes->pdata && routes->len > 0; --i) { \
491 toporouter_route_t *routedata = *i;
493 #define FOREACH_NETSCORE(netscores) do { \
494 for(toporouter_netscore_t **i = ((toporouter_netscore_t **)netscores->pdata) + netscores->len - 1; i >= (toporouter_netscore_t **)netscores->pdata && netscores->len > 0; --i) { \
495 toporouter_netscore_t *netscore = *i;
497 #define FOREACH_NETLIST(netlists) do { \
498 for(toporouter_netlist_t **i = ((toporouter_netlist_t **)netlists->pdata) + netlists->len - 1; i >= (toporouter_netlist_t **)netlists->pdata && netlists->len > 0; --i) { \
499 toporouter_netlist_t *netlist = *i;
501 #define FOREACH_END }} while(0)
503 #endif /* __TOPOROUTER_INCLUDED__ */