kicad/pcbnew/router/pns_joint.h

265 lines
6.3 KiB
C
Raw Normal View History

/*
* KiRouter - a push-and-(sometimes-)shove PCB router
*
* Copyright (C) 2013-2014 CERN
* Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
2013-09-26 21:53:54 +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
*
* 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
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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"
#include "pns_itemset.h"
namespace PNS {
/**
* Class JOINT
2013-09-26 21:53:54 +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.
* A hash table of joints is used by the router to follow connectivity between
2013-09-26 21:53:54 +00:00
* the items.
**/
class JOINT : public ITEM
{
public:
typedef ITEM_SET::ENTRIES 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
struct HASH_TAG
2013-09-26 21:53:54 +00:00
{
VECTOR2I pos;
int net;
};
JOINT() :
ITEM( JOINT_T ), m_locked( false ) {}
2013-09-26 21:53:54 +00:00
JOINT( const VECTOR2I& aPos, const LAYER_RANGE& aLayers, int aNet = -1 ) :
ITEM( JOINT_T )
2013-09-26 21:53:54 +00:00
{
m_tag.pos = aPos;
m_tag.net = aNet;
m_layers = aLayers;
m_locked = false;
2013-09-26 21:53:54 +00:00
}
JOINT( const JOINT& aB ) :
ITEM( JOINT_T )
2013-09-26 21:53:54 +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;
m_locked = aB.m_locked;
2013-09-26 21:53:54 +00:00
}
ITEM* Clone( ) const
2013-09-26 21:53:54 +00:00
{
assert( false );
return NULL;
}
///> 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 || m_linkedItems.Count( SEGMENT_T ) != 2 )
2013-09-26 21:53:54 +00:00
return false;
SEGMENT* seg1 = static_cast<SEGMENT*>( m_linkedItems[0] );
SEGMENT* seg2 = static_cast<SEGMENT*>( m_linkedItems[1] );
2013-09-26 21: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
}
2015-02-18 16:53:46 +00:00
bool IsNonFanoutVia() const
{
int vias = m_linkedItems.Count( VIA_T );
int segs = m_linkedItems.Count( SEGMENT_T );
return ( m_linkedItems.Size() == 3 && vias == 1 && segs == 2 );
}
bool IsTraceWidthChange() const
{
if( m_linkedItems.Size() != 2 )
return false;
if( m_linkedItems.Count( SEGMENT_T ) != 2)
return false;
SEGMENT* seg1 = static_cast<SEGMENT*>( m_linkedItems[0] );
SEGMENT* seg2 = static_cast<SEGMENT*>( m_linkedItems[1] );
return seg1->Width() != seg2->Width();
}
///> Links the joint to a given board item (when it's added to the NODE)
void Link( ITEM* aItem )
2013-09-26 21:53:54 +00:00
{
2014-11-14 19:19:00 +00:00
if( m_linkedItems.Contains( aItem ) )
2013-09-26 21:53:54 +00:00
return;
2014-11-14 19:19:00 +00:00
m_linkedItems.Add( aItem );
2013-09-26 21:53:54 +00:00
}
///> Unlinks a given board item from the joint (upon its removal from a NODE)
2013-09-26 21:53:54 +00:00
///> Returns true if the joint became dangling after unlinking.
bool Unlink( ITEM* aItem )
2013-09-26 21:53:54 +00:00
{
2014-11-14 19:19:00 +00:00
m_linkedItems.Erase( aItem );
return m_linkedItems.Size() == 0;
2013-09-26 21:53:54 +00:00
}
///> For trivial joints, returns the segment adjacent to (aCurrent). For non-trival ones, returns
///> NULL, indicating the end of line.
SEGMENT* NextSegment( SEGMENT* aCurrent ) const
2013-09-26 21:53:54 +00:00
{
if( !IsLineCorner() )
return NULL;
return static_cast<SEGMENT*>( m_linkedItems[m_linkedItems[0] == aCurrent ? 1 : 0] );
2013-09-26 21:53:54 +00:00
}
VIA* Via()
{
for( ITEM* item : m_linkedItems.Items() )
{
if( item->OfKind( VIA_T ) )
return static_cast<VIA*>( item );
}
return NULL;
}
2013-09-26 21:53:54 +00:00
/// trivial accessors
2014-11-14 19:19:00 +00:00
const HASH_TAG& Tag() const
{
return m_tag;
}
2014-11-14 19:19:00 +00:00
const VECTOR2I& Pos() const
{
return m_tag.pos;
}
2014-11-14 19:19:00 +00:00
int Net() const
{
return m_tag.net;
}
2014-11-14 19:19:00 +00:00
const LINKED_ITEMS& LinkList() const
2014-11-14 19:19:00 +00:00
{
return m_linkedItems.CItems();
}
2013-09-26 21:53:54 +00:00
const ITEM_SET& CLinks() const
2013-09-26 21:53:54 +00:00
{
return m_linkedItems;
}
2013-09-26 21:53:54 +00:00
ITEM_SET& Links()
{
return m_linkedItems;
}
2013-09-26 21:53:54 +00:00
int LinkCount( int aMask = -1 ) const
{
return m_linkedItems.Count( aMask );
2013-09-26 21:53:54 +00:00
}
void Dump() const;
bool operator==( const JOINT& rhs ) const
2013-09-26 21:53:54 +00:00
{
return m_tag.pos == rhs.m_tag.pos && m_tag.net == rhs.m_tag.net;
}
void Merge( const JOINT& aJoint )
2013-09-26 21:53:54 +00:00
{
if( !Overlaps( aJoint ) )
return;
m_layers.Merge( aJoint.m_layers );
if( aJoint.IsLocked() )
m_locked = true;
for( ITEM* item : aJoint.LinkList() )
{
2014-11-14 19:19:00 +00:00
m_linkedItems.Add( item );
}
2013-09-26 21:53:54 +00:00
}
bool Overlaps( const JOINT& rhs ) const
2013-09-26 21:53:54 +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 );
}
void Lock( bool aLock = true )
{
m_locked = aLock;
}
bool IsLocked() const
{
return m_locked;
}
private:
2013-09-26 21:53:54 +00:00
///> hash tag for unordered_multimap
HASH_TAG m_tag;
2013-09-26 21:53:54 +00:00
///> list of items linked to this joint
ITEM_SET m_linkedItems;
///> locked (non-movable) flag
bool m_locked;
};
inline bool operator==( JOINT::HASH_TAG const& aP1, JOINT::HASH_TAG const& aP2 )
{
return aP1.pos == aP2.pos && aP1.net == aP2.net;
}
inline std::size_t hash_value( JOINT::HASH_TAG const& aP )
{
std::size_t seed = 0;
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
return seed;
}
}
2013-09-26 21:53:54 +00:00
#endif // __PNS_JOINT_H