2011-11-10 15:55:05 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
|
|
|
|
* Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
|
|
|
|
* Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2011-09-20 13:57:40 +00:00
|
|
|
/**
|
|
|
|
* @file work.cpp
|
|
|
|
* @brief Automatic routing routines
|
|
|
|
*/
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
#include "fctsys.h"
|
|
|
|
#include "common.h"
|
2011-09-20 13:57:40 +00:00
|
|
|
|
2007-06-05 12:10:51 +00:00
|
|
|
#include "pcbnew.h"
|
|
|
|
#include "cell.h"
|
2011-09-20 13:57:40 +00:00
|
|
|
#include "ar_protos.h"
|
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;
|
2011-09-20 13:57:40 +00:00
|
|
|
int FromRow; /* source row */
|
|
|
|
int FromCol; /* source column */
|
|
|
|
int net_code; /* net_code */
|
|
|
|
int ToRow; /* target row */
|
|
|
|
int ToCol; /* target column */
|
2009-11-20 14:55:20 +00:00
|
|
|
RATSNEST_ITEM* pt_rats; /* Corresponding ratsnest */
|
2011-09-20 13:57:40 +00:00
|
|
|
int ApxDist; /* approximate distance */
|
2009-11-20 14:55:20 +00:00
|
|
|
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;
|
2011-11-10 15:55:05 +00:00
|
|
|
delete ptr;
|
2009-11-20 14:55:20 +00:00
|
|
|
}
|
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;
|
|
|
|
|
2011-11-10 15:55:05 +00:00
|
|
|
if( ( p = (CWORK*) operator new( sizeof(CWORK), std::nothrow ) ) != NULL )
|
2009-11-20 14:55:20 +00:00
|
|
|
{
|
|
|
|
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;
|
2011-09-24 18:33:28 +00:00
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
if( Head ) /* attach at end */
|
2011-09-20 13:57:40 +00:00
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
Tail->Next = p;
|
|
|
|
else /* first in list */
|
|
|
|
Head = Current = p;
|
2011-09-20 13:57:40 +00:00
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
Tail = p;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else /* can't get any more memory */
|
2011-09-20 13:57:40 +00:00
|
|
|
{
|
2009-11-20 14:55:20 +00:00
|
|
|
return 0;
|
2011-09-20 13:57:40 +00:00
|
|
|
}
|
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;
|
2011-09-20 13:57:40 +00:00
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
while( (p = Head) != NULL ) /* prioritize each work item */
|
|
|
|
{
|
|
|
|
Head = Head->Next;
|
2011-09-20 13:57:40 +00:00
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
if( p->Priority ) /* put at end of priority list */
|
|
|
|
{
|
|
|
|
p->Next = NULL;
|
2011-09-20 13:57:40 +00:00
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
if( (r = q0) == NULL ) /* empty list? */
|
2011-11-10 15:55:05 +00:00
|
|
|
{
|
2009-11-20 14:55:20 +00:00
|
|
|
q0 = p;
|
2011-11-10 15:55:05 +00:00
|
|
|
}
|
2009-11-20 14:55:20 +00:00
|
|
|
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;
|
|
|
|
}
|
2011-09-20 13:57:40 +00:00
|
|
|
|
2009-11-20 14:55:20 +00:00
|
|
|
if( mx )
|
|
|
|
incl += (2 * (float) my / mx);
|
|
|
|
|
|
|
|
return (int) ( ( dx + dy ) * incl );
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|