2013-09-18 17:55:16 +00:00
|
|
|
/*
|
|
|
|
* KiRouter - a push-and-(sometimes-)shove PCB router
|
|
|
|
*
|
2014-05-14 13:53:54 +00:00
|
|
|
* Copyright (C) 2013-2014 CERN
|
2013-09-18 17:55:16 +00:00
|
|
|
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-09-18 17:55:16 +00:00
|
|
|
* 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 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-09-18 17:55:16 +00:00
|
|
|
* 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.
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-09-18 17:55:16 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
2014-05-14 13:53:54 +00:00
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2013-09-18 17:55:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __PNS_JOINT_H
|
|
|
|
#define __PNS_JOINT_H
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <boost/functional/hash.hpp>
|
|
|
|
|
|
|
|
#include <math/vector2d.h>
|
|
|
|
|
|
|
|
#include "pns_item.h"
|
|
|
|
#include "pns_segment.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PNS_JOINT
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-10-14 11:43:57 +00:00
|
|
|
* Represents a 2D point on a given set of layers and belonging to a certain
|
2013-09-26 21:53:54 +00:00
|
|
|
* net, that links together a number of board items.
|
2013-10-14 11:43:57 +00:00
|
|
|
* A hash table of joints is used by the router to follow connectivity between
|
2013-09-26 21:53:54 +00:00
|
|
|
* the items.
|
2013-09-18 17:55:16 +00:00
|
|
|
**/
|
|
|
|
class PNS_JOINT : public PNS_ITEM
|
|
|
|
{
|
|
|
|
public:
|
2014-05-16 11:37:31 +00:00
|
|
|
typedef std::vector<PNS_ITEM*> LINKED_ITEMS;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
///> Joints are hashed by their position, layers and net.
|
|
|
|
/// Linked items are, obviously, not hashed
|
2014-05-16 11:37:31 +00:00
|
|
|
struct HASH_TAG
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
VECTOR2I pos;
|
|
|
|
int net;
|
|
|
|
};
|
|
|
|
|
|
|
|
PNS_JOINT() :
|
|
|
|
PNS_ITEM( JOINT ) {}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
PNS_JOINT( const VECTOR2I& aPos, const PNS_LAYERSET& aLayers, int aNet = -1 ) :
|
2013-09-26 21:53:54 +00:00
|
|
|
PNS_ITEM( JOINT )
|
|
|
|
{
|
|
|
|
m_tag.pos = aPos;
|
|
|
|
m_tag.net = aNet;
|
|
|
|
m_layers = aLayers;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
PNS_JOINT( const PNS_JOINT& aB ) :
|
2013-09-26 21:53:54 +00:00
|
|
|
PNS_ITEM( JOINT )
|
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
m_layers = aB.m_layers;
|
|
|
|
m_tag.pos = aB.m_tag.pos;
|
|
|
|
m_tag.net = aB.m_tag.net;
|
|
|
|
m_linkedItems = aB.m_linkedItems;
|
|
|
|
m_layers = aB.m_layers;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
PNS_ITEM* Clone ( ) const
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
assert( false );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-14 11:43:57 +00:00
|
|
|
///> Returns true if the joint is a trivial line corner, connecting two
|
2013-09-26 21:53:54 +00:00
|
|
|
/// segments of the same net, on the same layer.
|
|
|
|
bool IsLineCorner() const
|
|
|
|
{
|
|
|
|
if( m_linkedItems.size() != 2 )
|
|
|
|
return false;
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( m_linkedItems[0]->Kind() != SEGMENT || m_linkedItems[1]->Kind() != SEGMENT )
|
2013-09-26 21:53:54 +00:00
|
|
|
return false;
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
PNS_SEGMENT* seg1 = static_cast<PNS_SEGMENT*>( m_linkedItems[0] );
|
|
|
|
PNS_SEGMENT* seg2 = static_cast<PNS_SEGMENT*>( m_linkedItems[1] );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
// joints between segments of different widths are not considered trivial.
|
|
|
|
return seg1->Width() == seg2->Width();
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///> Links the joint to a given board item (when it's added to the PNS_NODE)
|
|
|
|
void Link( PNS_ITEM* aItem )
|
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
LINKED_ITEMS::iterator f = std::find( m_linkedItems.begin(), m_linkedItems.end(), aItem );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( f != m_linkedItems.end() )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_linkedItems.push_back( aItem );
|
|
|
|
}
|
|
|
|
|
|
|
|
///> Unlinks a given board item from the joint (upon its removal from a PNS_NODE)
|
|
|
|
///> Returns true if the joint became dangling after unlinking.
|
|
|
|
bool Unlink( PNS_ITEM* aItem )
|
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
LINKED_ITEMS::iterator f = std::find( m_linkedItems.begin(), m_linkedItems.end(), aItem );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( f != m_linkedItems.end() )
|
2014-06-23 06:50:53 +00:00
|
|
|
f = m_linkedItems.erase( f );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
return m_linkedItems.size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
///> For trivial joints, returns the segment adjacent to (aCurrent). For non-trival ones, returns
|
|
|
|
///> NULL, indicating the end of line.
|
|
|
|
PNS_SEGMENT* NextSegment( PNS_SEGMENT* aCurrent ) const
|
|
|
|
{
|
|
|
|
if( !IsLineCorner() )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return static_cast<PNS_SEGMENT*>( m_linkedItems[m_linkedItems[0] == aCurrent ? 1 : 0] );
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
PNS_VIA* Via()
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
for( LINKED_ITEMS::iterator i = m_linkedItems.begin(); i != m_linkedItems.end(); ++i )
|
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
if( (*i)->Kind() == PNS_ITEM::VIA )
|
2014-05-16 11:37:31 +00:00
|
|
|
return (PNS_VIA*)( *i );
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
/// trivial accessors
|
2014-05-16 11:37:31 +00:00
|
|
|
const HASH_TAG& Tag() const
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
return m_tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
const VECTOR2I& Pos() const
|
|
|
|
{
|
|
|
|
return m_tag.pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Net() const
|
|
|
|
{
|
|
|
|
return m_tag.net;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
LINKED_ITEMS& LinkList()
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
return m_linkedItems;
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
///> Returns the number of linked items of types listed in aMask.
|
|
|
|
int LinkCount( int aMask = -1 ) const
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
for( LINKED_ITEMS::const_iterator i = m_linkedItems.begin();
|
|
|
|
i != m_linkedItems.end(); ++i )
|
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
if( (*i)->Kind() & aMask )
|
2013-09-26 21:53:54 +00:00
|
|
|
n++;
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Dump() const;
|
|
|
|
|
|
|
|
bool operator==( const PNS_JOINT& rhs ) const
|
|
|
|
{
|
|
|
|
return m_tag.pos == rhs.m_tag.pos && m_tag.net == rhs.m_tag.net;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Merge( const PNS_JOINT& aJoint )
|
|
|
|
{
|
|
|
|
if( !Overlaps( aJoint ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_layers.Merge( aJoint.m_layers );
|
|
|
|
|
|
|
|
// fixme: duplicate links (?)
|
2014-05-16 11:37:31 +00:00
|
|
|
for( LINKED_ITEMS::const_iterator i = aJoint.m_linkedItems.begin();
|
|
|
|
i != aJoint.m_linkedItems.end(); ++i )
|
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
m_linkedItems.push_back( *i );
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Overlaps( const PNS_JOINT& rhs ) const
|
|
|
|
{
|
2013-10-14 11:43:57 +00:00
|
|
|
return m_tag.pos == rhs.m_tag.pos &&
|
2013-09-26 21:53:54 +00:00
|
|
|
m_tag.net == rhs.m_tag.net && m_layers.Overlaps( rhs.m_layers );
|
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
|
|
|
|
private:
|
2013-09-26 21:53:54 +00:00
|
|
|
///> hash tag for unordered_multimap
|
2014-05-16 11:37:31 +00:00
|
|
|
HASH_TAG m_tag;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
///> list of items linked to this joint
|
2014-05-16 11:37:31 +00:00
|
|
|
LINKED_ITEMS m_linkedItems;
|
2013-09-18 17:55:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// hash function & comparison operator for boost::unordered_map<>
|
2014-05-16 11:37:31 +00:00
|
|
|
inline bool operator==( PNS_JOINT::HASH_TAG const& aP1,
|
|
|
|
PNS_JOINT::HASH_TAG const& aP2 )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
return aP1.pos == aP2.pos && aP1.net == aP2.net;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
inline std::size_t hash_value( PNS_JOINT::HASH_TAG const& aP )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
|
|
|
std::size_t seed = 0;
|
2014-05-16 11:37:31 +00:00
|
|
|
boost::hash_combine( seed, aP.pos.x );
|
|
|
|
boost::hash_combine( seed, aP.pos.y );
|
|
|
|
boost::hash_combine( seed, aP.net );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
return seed;
|
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
#endif // __PNS_JOINT_H
|