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
|
2023-08-22 12:06:33 +00:00
|
|
|
* Copyright (C) 2016-2023 KiCad Developers, see AUTHORS.txt for contributors.
|
2021-01-27 22:15:38 +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 <math/vector2d.h>
|
|
|
|
|
|
|
|
#include "pns_item.h"
|
|
|
|
#include "pns_segment.h"
|
2014-11-14 18:15:58 +00:00
|
|
|
#include "pns_itemset.h"
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 14:38:11 +00:00
|
|
|
namespace PNS {
|
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
/**
|
2021-07-19 23:56:05 +00:00
|
|
|
* A 2D point on a given set of layers and belonging to a certain net, that links
|
2021-01-27 22:15:38 +00:00
|
|
|
* together a number of board items.
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2021-01-27 22:15:38 +00:00
|
|
|
* A hash table of joints is used by the router to follow connectivity between the items.
|
2021-07-19 23:56:05 +00:00
|
|
|
*/
|
2016-08-29 17:31:13 +00:00
|
|
|
class JOINT : public ITEM
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-01-27 22:15:38 +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;
|
2023-08-22 12:06:33 +00:00
|
|
|
NET_HANDLE net;
|
2013-09-26 21:53:54 +00:00
|
|
|
};
|
|
|
|
|
2017-11-01 11:14:16 +00:00
|
|
|
struct JOINT_TAG_HASH
|
|
|
|
{
|
|
|
|
std::size_t operator()( const JOINT::HASH_TAG& aP ) const
|
|
|
|
{
|
|
|
|
using std::size_t;
|
|
|
|
using std::hash;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
return ( (hash<int>()( aP.pos.x )
|
|
|
|
^ (hash<int>()( aP.pos.y ) << 1) ) >> 1 )
|
2023-08-22 12:06:33 +00:00
|
|
|
^ (hash<void*>()( aP.net ) << 1);
|
2017-11-01 11:14:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
JOINT() :
|
2021-05-30 23:47:57 +00:00
|
|
|
ITEM( JOINT_T ), m_tag(), m_locked( false ) {}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2023-08-22 12:06:33 +00:00
|
|
|
JOINT( const VECTOR2I& aPos, const LAYER_RANGE& aLayers, NET_HANDLE aNet = nullptr ) :
|
2016-08-29 17:31:13 +00:00
|
|
|
ITEM( JOINT_T )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
m_tag.pos = aPos;
|
|
|
|
m_tag.net = aNet;
|
|
|
|
m_layers = aLayers;
|
2015-08-03 19:11:51 +00:00
|
|
|
m_locked = false;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
JOINT( const JOINT& aB ) :
|
|
|
|
ITEM( JOINT_T )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2018-10-09 21:50:20 +00:00
|
|
|
m_layers = aB.m_layers;
|
2014-05-16 11:37:31 +00:00
|
|
|
m_tag.pos = aB.m_tag.pos;
|
|
|
|
m_tag.net = aB.m_tag.net;
|
|
|
|
m_linkedItems = aB.m_linkedItems;
|
2018-10-09 21:50:20 +00:00
|
|
|
m_layers = aB.m_layers;
|
2015-09-14 16:40:29 +00:00
|
|
|
m_locked = aB.m_locked;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 17:06:49 +00:00
|
|
|
ITEM* Clone( ) const override
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
assert( false );
|
2021-07-19 23:56:05 +00:00
|
|
|
return nullptr;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2021-12-05 18:35:28 +00:00
|
|
|
/**
|
|
|
|
* Checks if a joint connects two segments of the same net, layer, and width.
|
|
|
|
* @param aAllowLockedSegs will consider joints between locked and unlocked segments as trivial
|
|
|
|
* @return true if the joint is a trivial line corner
|
|
|
|
*/
|
|
|
|
bool IsLineCorner( bool aAllowLockedSegs = false ) const
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2022-07-12 02:46:20 +00:00
|
|
|
if( m_linkedItems.Size() == 2 && m_linkedItems.Count( SEGMENT_T | ARC_T ) == 2 )
|
|
|
|
{
|
|
|
|
LINKED_ITEM* seg1 = static_cast<LINKED_ITEM*>( m_linkedItems[0] );
|
|
|
|
LINKED_ITEM* seg2 = static_cast<LINKED_ITEM*>( m_linkedItems[1] );
|
|
|
|
|
2023-10-22 16:06:15 +00:00
|
|
|
if( !aAllowLockedSegs && ( seg1->IsLocked() || seg2->IsLocked() ) )
|
|
|
|
return false;
|
|
|
|
|
2022-07-12 02:46:20 +00:00
|
|
|
// joints between segments of different widths are not considered trivial.
|
|
|
|
return seg1->Width() == seg2->Width();
|
|
|
|
}
|
|
|
|
else if( m_linkedItems.Size() > 2 && m_linkedItems.Count( SEGMENT_T | ARC_T ) == 2 )
|
2021-12-05 18:35:28 +00:00
|
|
|
{
|
|
|
|
if( !aAllowLockedSegs )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-03 21:20:47 +00:00
|
|
|
// There will be multiple VVIAs on joints between two locked segments, because we
|
|
|
|
// naively add a VVIA to each end of a locked segment.
|
|
|
|
else if( ( m_linkedItems.Size() - m_linkedItems.Count( SEGMENT_T | ARC_T ) )
|
|
|
|
== m_linkedItems.Count( VIA_T ) )
|
2021-12-05 18:35:28 +00:00
|
|
|
{
|
2022-07-12 02:46:20 +00:00
|
|
|
const LINKED_ITEM* seg1 = nullptr;
|
|
|
|
const LINKED_ITEM* seg2 = nullptr;
|
|
|
|
const VIA* via = nullptr;
|
|
|
|
bool hasNonVirtualVia = false;
|
2021-12-05 18:35:28 +00:00
|
|
|
|
2022-06-01 00:18:16 +00:00
|
|
|
for( const ITEM* item : m_linkedItems.CItems() )
|
|
|
|
{
|
|
|
|
if( item->Kind() == VIA_T )
|
|
|
|
{
|
|
|
|
via = static_cast<const VIA*>( item );
|
2022-06-03 21:20:47 +00:00
|
|
|
|
2023-09-23 17:33:32 +00:00
|
|
|
hasNonVirtualVia |= !via->IsVirtual();
|
2022-07-12 02:46:20 +00:00
|
|
|
}
|
|
|
|
else if( item->Kind() == SEGMENT_T || item->Kind() == ARC_T )
|
|
|
|
{
|
|
|
|
if( !seg1 )
|
|
|
|
seg1 = static_cast<const LINKED_ITEM*>( item );
|
|
|
|
else
|
|
|
|
seg2 = static_cast<const LINKED_ITEM*>( item );
|
2022-06-01 00:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-05 18:35:28 +00:00
|
|
|
|
2022-06-03 21:20:47 +00:00
|
|
|
if( !via || hasNonVirtualVia )
|
2021-12-05 18:35:28 +00:00
|
|
|
return false;
|
2022-07-12 02:46:20 +00:00
|
|
|
|
2023-10-16 21:04:01 +00:00
|
|
|
// This compiles away in release builds so we still need to check it below
|
|
|
|
// to prevent segfaults in release builds.
|
|
|
|
assert( seg1 && seg2 );
|
2022-07-12 02:46:20 +00:00
|
|
|
|
2023-10-16 21:04:01 +00:00
|
|
|
return ( seg1 && seg2 ) ? seg1->Width() == seg2->Width() : false;
|
2021-12-05 18:35:28 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2022-07-12 02:46:20 +00:00
|
|
|
return false;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
bool IsNonFanoutVia() const
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2023-09-23 17:33:32 +00:00
|
|
|
int vias = 0;
|
|
|
|
int segs = 0;
|
|
|
|
int realItems = 0;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2023-09-23 17:33:32 +00:00
|
|
|
for( const ITEM* item : m_linkedItems.CItems() )
|
|
|
|
{
|
|
|
|
if( item->Kind() == VIA_T )
|
|
|
|
{
|
|
|
|
if( static_cast<const VIA*>( item )->IsVirtual() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
vias++;
|
|
|
|
}
|
|
|
|
else if( item->Kind() == SEGMENT_T || item->Kind() == ARC_T )
|
|
|
|
{
|
|
|
|
segs++;
|
|
|
|
}
|
|
|
|
|
|
|
|
realItems++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ( realItems == 3 && vias == 1 && segs == 2 );
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
2019-07-21 22:36:08 +00:00
|
|
|
bool IsStitchingVia() const
|
|
|
|
{
|
|
|
|
return ( m_linkedItems.Size() == 1 && m_linkedItems.Count( VIA_T ) == 1 );
|
|
|
|
}
|
|
|
|
|
2015-11-05 09:20:08 +00:00
|
|
|
bool IsTraceWidthChange() const
|
|
|
|
{
|
|
|
|
if( m_linkedItems.Size() != 2 )
|
|
|
|
return false;
|
|
|
|
|
2016-08-29 16:11:42 +00:00
|
|
|
if( m_linkedItems.Count( SEGMENT_T ) != 2)
|
2015-11-05 09:20:08 +00:00
|
|
|
return false;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
SEGMENT* seg1 = static_cast<SEGMENT*>( m_linkedItems[0] );
|
|
|
|
SEGMENT* seg2 = static_cast<SEGMENT*>( m_linkedItems[1] );
|
2015-11-05 09:20:08 +00:00
|
|
|
|
|
|
|
return seg1->Width() != seg2->Width();
|
|
|
|
}
|
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< Link the joint to a given board item (when it's added to the NODE).
|
2016-08-29 17:31:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< Unlink a given board item from the joint (upon its removal from a NODE)
|
|
|
|
///< @return true if the joint became dangling after unlinking.
|
2016-08-29 17:31:13 +00:00
|
|
|
bool Unlink( ITEM* aItem )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-11-14 19:19:00 +00:00
|
|
|
m_linkedItems.Erase( aItem );
|
2014-11-14 18:15:58 +00:00
|
|
|
return m_linkedItems.Size() == 0;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< For trivial joints, return the segment adjacent to (aCurrent). For non-trival ones,
|
|
|
|
///< return NULL, indicating the end of line.
|
2021-12-05 18:35:28 +00:00
|
|
|
LINKED_ITEM* NextSegment( ITEM* aCurrent, bool aAllowLockedSegs = false ) const
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2021-12-05 18:35:28 +00:00
|
|
|
if( !IsLineCorner( aAllowLockedSegs ) )
|
2021-07-19 23:56:05 +00:00
|
|
|
return nullptr;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2023-11-28 17:55:37 +00:00
|
|
|
const std::vector<ITEM*>& citems = m_linkedItems.CItems();
|
|
|
|
const size_t size = citems.size();
|
|
|
|
|
|
|
|
for( size_t i = 0; i < size; i++ )
|
|
|
|
{
|
|
|
|
ITEM* item = m_linkedItems[i];
|
|
|
|
|
|
|
|
if( item != aCurrent && item->Kind() != VIA_T )
|
|
|
|
return static_cast<LINKED_ITEM*>( item );
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2022-08-29 20:21:46 +00:00
|
|
|
VIA* Via() const
|
2015-08-03 19:11:51 +00:00
|
|
|
{
|
2022-08-29 20:21:46 +00:00
|
|
|
for( ITEM* item : m_linkedItems.CItems() )
|
2015-08-03 19:11:51 +00:00
|
|
|
{
|
2016-08-29 16:11:42 +00:00
|
|
|
if( item->OfKind( VIA_T ) )
|
2022-08-29 20:21:46 +00:00
|
|
|
return static_cast<VIA*>( item ); // fixme: const correctness
|
2015-08-03 19:11:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:56:05 +00:00
|
|
|
return nullptr;
|
2015-08-03 19:11:51 +00:00
|
|
|
}
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
|
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-05-14 13:53:54 +00:00
|
|
|
}
|
2014-11-14 19:19:00 +00:00
|
|
|
|
|
|
|
const VECTOR2I& Pos() const
|
|
|
|
{
|
|
|
|
return m_tag.pos;
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2023-08-22 12:06:33 +00:00
|
|
|
NET_HANDLE Net() const override
|
2014-11-14 19:19:00 +00:00
|
|
|
{
|
|
|
|
return m_tag.net;
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2014-11-14 19:19:00 +00:00
|
|
|
|
2022-08-29 20:21:46 +00:00
|
|
|
const std::vector<ITEM*>& LinkList() const
|
2014-11-14 19:19:00 +00:00
|
|
|
{
|
|
|
|
return m_linkedItems.CItems();
|
2014-05-14 13:53:54 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
const ITEM_SET& CLinks() const
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-11-14 18:15:58 +00:00
|
|
|
return m_linkedItems;
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
ITEM_SET& Links()
|
2014-11-14 18:15:58 +00:00
|
|
|
{
|
|
|
|
return m_linkedItems;
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-11-14 18:15:58 +00:00
|
|
|
int LinkCount( int aMask = -1 ) const
|
|
|
|
{
|
|
|
|
return m_linkedItems.Count( aMask );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Dump() const;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void Merge( const JOINT& aJoint )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
if( !Overlaps( aJoint ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_layers.Merge( aJoint.m_layers );
|
|
|
|
|
2015-09-14 16:40:29 +00:00
|
|
|
if( aJoint.IsLocked() )
|
|
|
|
m_locked = true;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
for( ITEM* item : aJoint.LinkList() )
|
2014-05-16 11:37:31 +00:00
|
|
|
{
|
2014-11-14 19:19:00 +00:00
|
|
|
m_linkedItems.Add( item );
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool Overlaps( const JOINT& rhs ) const
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
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
|
|
|
|
2015-08-03 19:11:51 +00:00
|
|
|
void Lock( bool aLock = true )
|
|
|
|
{
|
|
|
|
m_locked = aLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsLocked() const
|
|
|
|
{
|
|
|
|
return m_locked;
|
|
|
|
}
|
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
private:
|
2021-01-27 22:15:38 +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
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< list of items linked to this joint
|
2016-08-29 17:31:13 +00:00
|
|
|
ITEM_SET m_linkedItems;
|
2015-08-03 19:11:51 +00:00
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< locked (non-movable) flag
|
2015-08-03 19:11:51 +00:00
|
|
|
bool m_locked;
|
2013-09-18 17:55:16 +00:00
|
|
|
};
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
inline bool operator==( JOINT::HASH_TAG const& aP1, 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
|
|
|
}
|
|
|
|
|
2016-08-29 14:38:11 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
#endif // __PNS_JOINT_H
|