2009-11-20 14:55:20 +00:00
|
|
|
/************************/
|
|
|
|
/* Autorouting routines */
|
|
|
|
/************************/
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
#include "fctsys.h"
|
|
|
|
#include "gr_basic.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "pcbnew.h"
|
|
|
|
#include "autorout.h"
|
|
|
|
#include "cell.h"
|
|
|
|
|
|
|
|
#include "protos.h"
|
2009-11-20 14:55:20 +00:00
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
struct CWORK /* a unit of work is a hole-pair to connect */
|
2009-11-20 14:55:20 +00:00
|
|
|
{
|
|
|
|
struct CWORK* Next;
|
|
|
|
int FromRow; /* source row */
|
|
|
|
int FromCol; /* source column */
|
|
|
|
int net_code; /* net_code */
|
|
|
|
int ToRow; /* target row */
|
|
|
|
int ToCol; /* target column */
|
|
|
|
RATSNEST_ITEM* pt_rats; /* Corresponding ratsnest */
|
|
|
|
int ApxDist; /* approximate distance */
|
|
|
|
int Cost; /* cost for sort by length */
|
|
|
|
int Priority; /* route priority */
|
|
|
|
};
|
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
/* pointers to the first and last item of work to do */
|
2009-11-20 14:55:20 +00:00
|
|
|
static CWORK* Head = NULL;
|
|
|
|
static CWORK* Tail = NULL;
|
|
|
|
static CWORK* Current = NULL;
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
|
2007-09-13 11:55:46 +00:00
|
|
|
void InitWork();
|
|
|
|
void ReInitWork();
|
2009-11-20 14:55:20 +00:00
|
|
|
int SetWork( int, int, int, int, int, RATSNEST_ITEM*, int );
|
|
|
|
void GetWork( int*, int*, int*, int*, int*, RATSNEST_ITEM** );
|
2007-09-01 12:00:30 +00:00
|
|
|
void SortWork();
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
/* initialize the work list */
|
2007-09-13 11:55:46 +00:00
|
|
|
void InitWork()
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2009-11-20 14:55:20 +00:00
|
|
|
CWORK* ptr;
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
while( ( ptr = Head ) != NULL )
|
|
|
|
{
|
|
|
|
Head = ptr->Next;
|
|
|
|
MyFree( ptr );
|
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
Tail = Current = NULL;
|
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
/* initialize the work list */
|
2007-09-13 11:55:46 +00:00
|
|
|
void ReInitWork()
|
2009-11-20 14:55:20 +00:00
|
|
|
{
|
|
|
|
Current = Head;
|
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* add a unit of work to the work list
|
2009-11-20 14:55:20 +00:00
|
|
|
* Return:
|
|
|
|
* 1 if OK
|
|
|
|
* 0 if memory allocation failed
|
|
|
|
*/
|
|
|
|
static int GetCost( int r1, int c1, int r2, int c2 );
|
|
|
|
|
|
|
|
int SetWork( int r1,
|
|
|
|
int c1,
|
|
|
|
int n_c,
|
|
|
|
int r2,
|
|
|
|
int c2,
|
|
|
|
RATSNEST_ITEM* pt_ch,
|
|
|
|
int pri )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2009-11-20 14:55:20 +00:00
|
|
|
CWORK* p;
|
|
|
|
|
|
|
|
if( ( p = (CWORK*) MyMalloc( sizeof(CWORK) ) ) != NULL )
|
|
|
|
{
|
|
|
|
p->FromRow = r1;
|
|
|
|
p->FromCol = c1;
|
|
|
|
p->net_code = n_c;
|
|
|
|
p->ToRow = r2;
|
|
|
|
p->ToCol = c2;
|
|
|
|
p->pt_rats = pt_ch;
|
|
|
|
p->ApxDist = GetApxDist( r1, c1, r2, c2 );
|
|
|
|
p->Cost = GetCost( r1, c1, r2, c2 );
|
|
|
|
p->Priority = pri;
|
|
|
|
p->Next = NULL;
|
|
|
|
if( Head ) /* attach at end */
|
|
|
|
Tail->Next = p;
|
|
|
|
else /* first in list */
|
|
|
|
Head = Current = p;
|
|
|
|
Tail = p;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else /* can't get any more memory */
|
|
|
|
return 0;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
/* fetch a unit of work from the work list */
|
|
|
|
void GetWork( int* r1,
|
|
|
|
int* c1,
|
|
|
|
int* n_c,
|
|
|
|
int* r2,
|
|
|
|
int* c2,
|
|
|
|
RATSNEST_ITEM** pt_ch )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2009-11-20 14:55:20 +00:00
|
|
|
if( Current )
|
|
|
|
{
|
|
|
|
*r1 = Current->FromRow;
|
|
|
|
*c1 = Current->FromCol;
|
|
|
|
*n_c = Current->net_code;
|
|
|
|
*r2 = Current->ToRow;
|
|
|
|
*c2 = Current->ToCol;
|
|
|
|
*pt_ch = Current->pt_rats;
|
|
|
|
Current = Current->Next;
|
|
|
|
}
|
|
|
|
else /* none left */
|
|
|
|
{
|
|
|
|
*r1 = *c1 = *r2 = *c2 = ILLEGAL;
|
|
|
|
*n_c = 0;
|
|
|
|
*pt_ch = NULL;
|
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* order the work items; shortest (low cost) first */
|
2007-09-13 11:55:46 +00:00
|
|
|
void SortWork()
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2009-11-20 14:55:20 +00:00
|
|
|
CWORK* p;
|
|
|
|
CWORK* q0; /* put PRIORITY PAD_CONNECTs in q0 */
|
|
|
|
CWORK* q1; /* sort other PAD_CONNECTs in q1 */
|
|
|
|
CWORK* r;
|
|
|
|
|
|
|
|
q0 = q1 = NULL;
|
|
|
|
while( (p = Head) != NULL ) /* prioritize each work item */
|
|
|
|
{
|
|
|
|
Head = Head->Next;
|
|
|
|
if( p->Priority ) /* put at end of priority list */
|
|
|
|
{
|
|
|
|
p->Next = NULL;
|
|
|
|
if( (r = q0) == NULL ) /* empty list? */
|
|
|
|
q0 = p;
|
|
|
|
else /* attach at end */
|
|
|
|
{
|
|
|
|
while( r->Next ) /* search for end */
|
|
|
|
r = r->Next;
|
|
|
|
|
|
|
|
r->Next = p; /* attach */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( ( ( r = q1 ) == NULL ) || ( p->Cost < q1->Cost ) )
|
|
|
|
{
|
|
|
|
p->Next = q1;
|
|
|
|
q1 = p;
|
|
|
|
}
|
|
|
|
else /* find proper position in list */
|
|
|
|
{
|
|
|
|
while( r->Next && p->Cost >= r->Next->Cost )
|
|
|
|
r = r->Next;
|
|
|
|
|
|
|
|
p->Next = r->Next;
|
|
|
|
r->Next = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( (p = q0) != NULL ) /* any priority PAD_CONNECTs? */
|
|
|
|
{
|
|
|
|
while( q0->Next )
|
|
|
|
q0 = q0->Next;
|
|
|
|
|
|
|
|
q0->Next = q1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
p = q1;
|
|
|
|
|
|
|
|
/* reposition Head and Tail */
|
|
|
|
for( Head = Current = Tail = p; Tail && Tail->Next; Tail = Tail->Next )
|
|
|
|
;
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
/* Calculate the cost of a net:
|
|
|
|
* cost = (| dx | + | dy |) * disability
|
|
|
|
* disability = 1 if dx or dy = 0, max if | dx | # | dy |
|
|
|
|
*/
|
|
|
|
static int GetCost( int r1, int c1, int r2, int c2 )
|
2007-05-06 16:03:28 +00:00
|
|
|
{
|
2009-11-20 14:55:20 +00:00
|
|
|
int dx, dy, mx, my;
|
|
|
|
float incl;
|
|
|
|
|
|
|
|
dx = abs( c2 - c1 );
|
|
|
|
dy = abs( r2 - r1 );
|
|
|
|
incl = 1.0;
|
|
|
|
mx = dx;
|
|
|
|
my = dy;
|
|
|
|
|
|
|
|
if( mx < my )
|
|
|
|
{
|
|
|
|
mx = dy; my = dx;
|
|
|
|
}
|
|
|
|
if( mx )
|
|
|
|
incl += (2 * (float) my / mx);
|
|
|
|
|
|
|
|
return (int) ( ( dx + dy ) * incl );
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|