Pcbnew: starting work on better ratsnest and connections calculations algorithms.

This first draft should fix bug 851670 and is faster than existing alogorithm.
This commit is contained in:
jean-pierre charras 2011-10-31 14:44:13 +01:00
parent 0ee7234cf9
commit d5ea4750e7
6 changed files with 349 additions and 128 deletions

View File

@ -4,6 +4,7 @@
*/
#include <limits.h>
#include <algorithm>
#include "fctsys.h"
#include "common.h"
@ -30,18 +31,6 @@ wxPoint BOARD_ITEM::ZeroOffset( 0, 0 );
BOARD_DESIGN_SETTINGS boardDesignSettings;
/**
* Function SortPadsByXCoord
* is used to Sort a pad list by x coordinate value.
*/
static int sortPadsByXCoord( const void* pt_ref, const void* pt_comp )
{
D_PAD* ref = *(D_PAD**) pt_ref;
D_PAD* comp = *(D_PAD**) pt_comp;
return ref->m_Pos.x - comp->m_Pos.x;
}
BOARD::BOARD( EDA_ITEM* parent, PCB_BASE_FRAME* frame ) :
BOARD_ITEM( (BOARD_ITEM*)parent, PCB_T ),
@ -1688,80 +1677,83 @@ D_PAD* BOARD::GetPadFast( const wxPoint& aPosition, int aLayerMask )
}
D_PAD* BOARD::GetPad( D_PAD** aPad, const wxPoint& aPosition, int aLayerMask )
D_PAD* BOARD::GetPad( std::vector<D_PAD*>& aPadList, const wxPoint& aPosition, int aLayerMask )
{
D_PAD* pad;
int ii;
// Search the aPoint coordinates in aPadList
// aPadList is sorted by X then Y values, and a fast binary search is used
int idxmax = aPadList.size()-1;
int nb_pad = GetPadsCount();
D_PAD** ptr_pad = aPad;
D_PAD** lim = aPad + nb_pad - 1;
ptr_pad = aPad;
while( nb_pad )
int delta = aPadList.size();
if( delta & 1 && delta > 1 )
delta += 1;
delta /= 2;
int idx = delta; // Starting index is the middle of list
while( delta )
{
pad = *ptr_pad;
ii = nb_pad;
nb_pad >>= 1;
if( (delta & 1) && ( delta > 1 ) )
delta++;
delta /= 2;
if( (ii & 1) && ( ii > 1 ) )
nb_pad++;
D_PAD* pad = aPadList[idx];
if( pad->m_Pos.x < aPosition.x ) /* Must search after this item */
if( pad->m_Pos == aPosition ) // candidate found
{
ptr_pad += nb_pad;
// The pad must match the layer mask:
if( (aLayerMask & pad->m_layerMask) != 0 )
return pad;
if( ptr_pad > lim )
ptr_pad = lim;
// More than one pad can be at aPosition
// search for a pad at aPosition that matched this mask
continue;
}
if( pad->m_Pos.x > aPosition.x ) /* Must search before this item */
// search next
for( int ii = idx+1; ii <= idxmax; ii++ )
{
ptr_pad -= nb_pad;
if( ptr_pad < aPad )
ptr_pad = aPad;
continue;
}
/* A suitable block is found (X coordinate matches the px reference: but we
* must matches the Y coordinate */
if( pad->m_Pos.x == aPosition.x )
{
/* Search the beginning of the block */
while( ptr_pad >= aPad )
{
pad = *ptr_pad;
if( pad->m_Pos.x == aPosition.x )
ptr_pad--;
else
pad = aPadList[ii];
if( pad->m_Pos != aPosition )
break;
}
ptr_pad++; /* ptr_pad = first pad which have pad->m_Pos.x = px */
for( ; ; ptr_pad++ )
{
if( ptr_pad > lim )
return NULL; /* outside suitable block */
pad = *ptr_pad;
if( pad->m_Pos.x != aPosition.x )
return NULL; /* outside suitable block */
if( pad->m_Pos.y != aPosition.y )
continue;
/* A Pad if found here: but it must mach the layer */
if( pad->m_layerMask & aLayerMask ) // Matches layer => a connected pad is found!
if( (aLayerMask & pad->m_layerMask) != 0 )
return pad;
}
// search previous
for( int ii = idx-1 ;ii >=0; ii-- )
{
pad = aPadList[ii];
if( pad->m_Pos != aPosition )
break;
if( (aLayerMask & pad->m_layerMask) != 0 )
return pad;
}
// Not found:
return 0;
}
if( pad->m_Pos.x == aPosition.x ) // Must search considering Y coordinate
{
if(pad->m_Pos.y < aPosition.y) // Must search after this item
{
idx += delta;
if( idx > idxmax )
idx = idxmax;
}
else // Must search before this item
{
idx -= delta;
if( idx < 0 )
idx = 0;
}
}
else if( pad->m_Pos.x < aPosition.x ) // Must search after this item
{
idx += delta;
if( idx > idxmax )
idx = idxmax;
}
else // Must search before this item
{
idx -= delta;
if( idx < 0 )
idx = 0;
}
}
@ -1769,12 +1761,24 @@ D_PAD* BOARD::GetPad( D_PAD** aPad, const wxPoint& aPosition, int aLayerMask )
}
void BOARD::GetSortedPadListByXCoord( std::vector<D_PAD*>& aVector )
/**
* Function SortPadsByXCoord
* is used by GetSortedPadListByXCoord to Sort a pad list by x coordinate value.
*/
static bool sortPadsByXthenYCoord( D_PAD* const & ref, D_PAD* const & comp )
{
if( ref->m_Pos.x == comp->m_Pos.x )
return ref->m_Pos.y < comp->m_Pos.y;
return ref->m_Pos.x < comp->m_Pos.x;
}
void BOARD::GetSortedPadListByXthenYCoord( std::vector<D_PAD*>& aVector )
{
aVector.insert( aVector.end(), m_NetInfo->m_PadsFullList.begin(),
m_NetInfo->m_PadsFullList.end() );
qsort( &aVector[0], GetPadsCount(), sizeof( D_PAD*), sortPadsByXCoord );
sort( aVector.begin(), aVector.end(), sortPadsByXthenYCoord );
}

View File

@ -1162,22 +1162,23 @@ public:
* function.
* </p>
* @note The normal pad list is sorted by increasing netcodes.
* @param aPad A D_PAD object pointer the first pad in the list to begin searching.
* @param aPadList = the list of pads candidates (a std::vector<D_PAD*>)
* @param aPosition A wxPoint object containing the position to test.
* @param aLayerMask A layer or layers to mask the hit test.
* @return A D_PAD object pointer to the connected pad.
* @return a D_PAD object pointer to the connected pad.
*/
D_PAD* GetPad( D_PAD** aPad, const wxPoint& aPosition, int aLayerMask );
D_PAD* GetPad( std::vector<D_PAD*>& aPadList, const wxPoint& aPosition, int aLayerMask );
/**
* Function GetSortedPadListByXCoord
* Function GetSortedPadListByXthenYCoord
* first empties then fills the vector with all pads and sorts them by
* increasing x coordinate. The vector only holds pointers to the pads and
* increasing x coordinate, and for increasing y coordinate for same values of x coordinates.
* The vector only holds pointers to the pads and
* those pointers are only references to pads which are owned by the BOARD
* through other links.
* @param aVector Where to put the pad pointers.
*/
void GetSortedPadListByXCoord( std::vector<D_PAD*>& aVector );
void GetSortedPadListByXthenYCoord( std::vector<D_PAD*>& aVector );
/**

View File

@ -447,7 +447,7 @@ bool SEGVIA::IsOnLayer( int layer_number ) const
}
int TRACK::ReturnMaskLayer()
int TRACK::ReturnMaskLayer() const
{
if( Type() == PCB_VIA_T )
{

View File

@ -225,7 +225,7 @@ public:
* TRACK segment or SEGVIA physically resides.
* @return int - a layer mask, see pcbstruct.h's LAYER_BACK, etc.
*/
int ReturnMaskLayer();
int ReturnMaskLayer() const;
/**
* Function IsPointOnEnds

View File

@ -4,7 +4,6 @@
*/
#include "fctsys.h"
#include "gr_basic.h"
#include "common.h"
#include "pcbcommon.h"
#include "macros.h"
@ -14,7 +13,6 @@
#include "class_board.h"
#include "pcbnew.h"
#include "protos.h"
extern void Merge_SubNets_Connected_By_CopperAreas( BOARD* aPcb );
@ -25,7 +23,225 @@ static void Propagate_SubNet( TRACK* pt_start_conn, TRACK* pt_end_conn );
static void Build_Pads_Info_Connections_By_Tracks( TRACK* pt_start_conn, TRACK* pt_end_conn );
static void RebuildTrackChain( BOARD* pcb );
/*..*/
// A helper class to handle connection points
class CONNECTED_POINT
{
public:
TRACK * m_Track; // a link to the connected item (track or via)
wxPoint m_Point; // the connection point
CONNECTED_POINT( TRACK * aTrack, wxPoint & aPoint)
{
m_Track = aTrack;
m_Point = aPoint;
}
};
// A helper class to handle connection calculations:
class CONNECTIONS
{
public:
std::vector <TRACK*> m_Connected; // List of connected tracks/vias
// to a given track or via
std::vector <CONNECTED_POINT> m_Candidates; // List of points to test
// (end points of tracks or vias location )
private:
BOARD * m_brd; // the master board.
public:
CONNECTIONS( BOARD * aBrd );
~CONNECTIONS() {};
/** Function BuildCandidatesList
* Fills m_Candidates with all connecting points (track ends or via location)
* with tracks from aBegin to aEnd.
* if aBegin == NULL, use first track in brd list
* if aEnd == NULL, uses all tracks from aBegin in brd list
*/
void BuildCandidatesList( TRACK * aBegin = NULL, TRACK * aEnd = NULL);
/**
* function SearchConnectedTracks
* Fills m_Connected with tracks/vias connected to aTrack
* @param aTrack = track or via to use as reference
*/
int SearchConnectedTracks( const TRACK * aTrack );
private:
/**
* function searchEntryPoint
* Search an item in m_Connected connected to aPoint
* note m_Connected containts usually more than one candidate
* and searchEntryPoint returns an index to one of these candidates
* Others are neightbor of the indexed item.
* @param aPoint is the reference coordinates
* @return the index of item found or -1 if no candidate
*/
int searchEntryPoint( const wxPoint & aPoint);
};
/* sort function used to sort .m_Connected by X the Y values
* items are sorted by X coordinate value,
* and for same X value, by Y coordinate value.
*/
static bool sortConnectedPointByXthenYCoordinates( const CONNECTED_POINT & aRef,
const CONNECTED_POINT & aTst )
{
if( aRef.m_Point.x == aTst.m_Point.x )
return aRef.m_Point.y < aTst.m_Point.y;
return aRef.m_Point.x < aTst.m_Point.x;
}
CONNECTIONS::CONNECTIONS( BOARD * aBrd )
{
m_brd = aBrd;
}
void CONNECTIONS::BuildCandidatesList( TRACK * aBegin, TRACK * aEnd)
{
m_Connected.clear();
if( aBegin == NULL )
aBegin = m_brd->m_Track;
unsigned ii = 0;
// Count candidates ( i.e. end points )
for( const TRACK* track = aBegin; track; track = track->Next() )
{
if( track->Type() == PCB_VIA_T )
ii++;
else
ii += 2;
if( track == aEnd )
break;
}
// Build candidate list
m_Connected.reserve( ii );
for( TRACK* track = aBegin; track != aEnd; track = track->Next() )
{
CONNECTED_POINT candidate( track, track->m_Start);
m_Candidates.push_back( candidate );
if( track->Type() != PCB_VIA_T )
{
candidate.m_Track = track;
candidate.m_Point = track->m_End;
m_Candidates.push_back( candidate );
}
if( track == aEnd )
break;
}
// Sort list by increasing X coordinate,
// and for increasing Y coordinate when items have the same X coordinate
// So candidates to the same location are consecutive in list.
sort( m_Candidates.begin(), m_Candidates.end(), sortConnectedPointByXthenYCoordinates );
}
int CONNECTIONS::SearchConnectedTracks( const TRACK * aTrack )
{
int count = 0;
m_Connected.clear();
int layerMask = aTrack->ReturnMaskLayer();
// Search for connections to starting point:
wxPoint position = aTrack->m_Start;
for( int kk = 0; kk < 2; kk++ )
{
int idx = searchEntryPoint( position );
if ( idx >= 0 )
{
// search after:
for ( unsigned ii = idx; ii < m_Candidates.size(); ii ++ )
{
if( m_Candidates[ii].m_Track == aTrack )
continue;
if( m_Candidates[ii].m_Point != position )
break;
if( (m_Candidates[ii].m_Track->ReturnMaskLayer() & layerMask ) != 0 )
m_Connected.push_back( m_Candidates[ii].m_Track );
}
// search before:
for ( unsigned ii = idx-1; ii >= 0; ii -- )
{
if( m_Candidates[ii].m_Track == aTrack )
continue;
if( m_Candidates[ii].m_Point != position )
break;
if( (m_Candidates[ii].m_Track->ReturnMaskLayer() & layerMask ) != 0 )
m_Connected.push_back( m_Candidates[ii].m_Track );
}
}
// Search for connections to ending point:
if( aTrack->Type() == PCB_VIA_T )
break;
position = aTrack->m_End;
}
return count;
}
int CONNECTIONS::searchEntryPoint( const wxPoint & aPoint)
{
// Search the aPoint coordinates in m_Candidates
// m_Candidates is sorted by X then Y values, and a fast binary search is used
int idxmax = m_Candidates.size()-1;
int delta = m_Candidates.size();
if( delta & 1 && delta > 1 )
delta += 1;
delta /= 2;
int idx = delta; // Starting index is the middle of list
while( delta )
{
if( (delta & 1) && ( delta > 1 ) )
delta++;
delta /= 2;
CONNECTED_POINT & candidate = m_Candidates[idx];
if( candidate.m_Point == aPoint ) // candidate found
{
return idx;
}
// Not found: test the middle of the remaining sub list
if( candidate.m_Point.x == aPoint.x ) // Must search considering Y coordinate
{
if(candidate.m_Point.y < aPoint.y) // Must search after this item
{
idx += delta;
if( idx > idxmax )
idx = idxmax;
}
else // Must search before this item
{
idx -= delta;
if( idx < 0 )
idx = 0;
}
}
else if( candidate.m_Point.x < aPoint.x ) // Must search after this item
{
idx += delta;
if( idx > idxmax )
idx = idxmax;
}
else // Must search before this item
{
idx -= delta;
if( idx < 0 )
idx = 0;
}
}
return -1;
}
/**
@ -472,9 +688,6 @@ static void Build_Pads_Info_Connections_By_Tracks( TRACK* pt_start_conn, TRACK*
}
#define POS_AFF_CHREF 62
void PCB_BASE_FRAME::RecalculateAllTracksNetcode()
{
TRACK* pt_trace;
@ -507,7 +720,8 @@ void PCB_BASE_FRAME::RecalculateAllTracksNetcode()
/**************************************************************/
/* Pass 1: search the connections between track ends and pads */
/**************************************************************/
m_Pcb->GetSortedPadListByXCoord( sortedPads );
m_Pcb->GetSortedPadListByXthenYCoord( sortedPads );
/* Reset variables and flags used in computation */
pt_trace = m_Pcb->m_Track;
@ -529,7 +743,7 @@ void PCB_BASE_FRAME::RecalculateAllTracksNetcode()
layerMask = g_TabOneLayerMask[pt_trace->GetLayer()];
/* Search for a pad on the segment starting point */
pt_trace->start = m_Pcb->GetPad( &sortedPads[0], pt_trace->m_Start, layerMask );
pt_trace->start = m_Pcb->GetPad( sortedPads, pt_trace->m_Start, layerMask );
if( pt_trace->start != NULL )
{
@ -538,7 +752,7 @@ void PCB_BASE_FRAME::RecalculateAllTracksNetcode()
}
/* Search for a pad on the segment ending point */
pt_trace->end = m_Pcb->GetPad( &sortedPads[0], pt_trace->m_End, layerMask );
pt_trace->end = m_Pcb->GetPad( sortedPads, pt_trace->m_End, layerMask );
if( pt_trace->end != NULL )
{
@ -547,17 +761,16 @@ void PCB_BASE_FRAME::RecalculateAllTracksNetcode()
}
}
/*****************************************************/
/* Pass 2: search the connections between track ends */
/*****************************************************/
/* the .start and .end member pointers are updated, only if NULLs
* (if not nuls, the end is already connected to a pad).
* (if not null, the end is already connected to a pad).
* the connection (if found) is between segments
* when a track has a net code and the other has a null net code, the null net code is changed
*/
#if 1
#if 0
for( pt_trace = m_Pcb->m_Track; pt_trace != NULL; pt_trace = pt_trace->Next() )
{
if( pt_trace->start == NULL )
@ -571,41 +784,44 @@ void PCB_BASE_FRAME::RecalculateAllTracksNetcode()
}
}
#else
CONNECTIONS connections( m_Pcb );
connections.BuildCandidatesList();
for( pt_trace = m_Pcb->m_Track; pt_trace != NULL; pt_trace = pt_trace->Next() )
{
if( pt_trace->start != NULL )
if( pt_trace->start != NULL && pt_trace->end != NULL )
continue;
TRACK * candidate = pt_trace->GetTrace( m_Pcb->m_Track, NULL, START );
if( candidate == NULL )
continue;
if( candidate->start == pt_trace || candidate->end == pt_trace )
connections.SearchConnectedTracks( pt_trace );
for( unsigned ii = 0; ii < connections.m_Connected.size(); ii ++ )
{
candidate->SetState( BUSY, ON );
pt_trace->start = pt_trace->GetTrace( m_Pcb->m_Track, NULL, START );
candidate->SetState( BUSY, OFF );
}
else
TRACK * candidate = connections.m_Connected[ii];
// Do not create a link to an other track already linked
// to avoid loops when we have 4 and more ends at the same location
// like this case for 4 tracks named A, B, C ,D:
// A links B; B links A and C links D; D links C, but never C or D links A or B
// Try to find a not already linked track:
if( candidate->start == pt_trace || candidate->end == pt_trace )
continue;
// A link is found:
if( pt_trace->start == NULL )
{
if( ( pt_trace->m_Start == candidate->m_Start ) ||
( pt_trace->m_Start == candidate->m_End ) )
pt_trace->start = candidate;
}
for( pt_trace = m_Pcb->m_Track; pt_trace != NULL; pt_trace = pt_trace->Next() )
if( pt_trace->end == NULL )
{
if( pt_trace->end != NULL )
continue;
TRACK * candidate = pt_trace->GetTrace( m_Pcb->m_Track, NULL, END );
if( candidate == NULL )
continue;
if( candidate->start == pt_trace || candidate->end == pt_trace )
{
candidate->SetState( BUSY, ON );
pt_trace->end = pt_trace->GetTrace( m_Pcb->m_Track, NULL, END );
candidate->SetState( BUSY, OFF );
}
else
if( ( pt_trace->m_End == candidate->m_Start ) ||
( pt_trace->m_End == candidate->m_End ) )
pt_trace->end = candidate;
}
}
}
#endif
/**********************************************************/
/* Propagate net codes from a segment to an other segment */
/**********************************************************/

View File

@ -414,12 +414,12 @@ void DRC::testPad2Pad()
{
std::vector<D_PAD*> sortedPads;
m_pcb->GetSortedPadListByXCoord( sortedPads );
m_pcb->GetSortedPadListByXthenYCoord( sortedPads );
// find the max size of the pads (used to stop the test)
int max_size = 0;
for( unsigned i = 0; i<sortedPads.size(); ++i )
for( unsigned i = 0; i < sortedPads.size(); ++i )
{
D_PAD* pad = sortedPads[i];
@ -431,7 +431,7 @@ void DRC::testPad2Pad()
// Test the pads
D_PAD** listEnd = &sortedPads[ sortedPads.size() ];
for( unsigned i = 0; i<sortedPads.size(); ++i )
for( unsigned i = 0; i< sortedPads.size(); ++i )
{
D_PAD* pad = sortedPads[i];