2015-02-18 00:29:54 +00:00
|
|
|
/*
|
|
|
|
* KiRouter - a push-and-(sometimes-)shove PCB router
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2015 CERN
|
2022-12-04 04:52:21 +00:00
|
|
|
* Copyright (C) 2016-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-02-18 00:29:54 +00:00
|
|
|
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pns_meander_placer_base.h"
|
2019-10-31 13:00:01 +00:00
|
|
|
#include "pns_meander.h"
|
|
|
|
#include "pns_router.h"
|
|
|
|
#include "pns_solid.h"
|
2021-01-04 13:54:29 +00:00
|
|
|
#include "pns_arc.h"
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2016-08-29 14:38:11 +00:00
|
|
|
namespace PNS {
|
|
|
|
|
2022-12-04 04:52:21 +00:00
|
|
|
const int LENGTH_TARGET_TOLERANCE = 20;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
MEANDER_PLACER_BASE::MEANDER_PLACER_BASE( ROUTER* aRouter ) :
|
|
|
|
PLACEMENT_ALGO( aRouter )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2021-07-19 23:56:05 +00:00
|
|
|
m_world = nullptr;
|
2015-06-19 08:55:37 +00:00
|
|
|
m_currentWidth = 0;
|
2022-08-02 03:23:07 +00:00
|
|
|
m_startPad_n = nullptr;
|
|
|
|
m_startPad_p = nullptr;
|
|
|
|
m_endPad_n = nullptr;
|
|
|
|
m_endPad_p = nullptr;
|
2015-02-18 16:53:46 +00:00
|
|
|
}
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
MEANDER_PLACER_BASE::~MEANDER_PLACER_BASE()
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
}
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void MEANDER_PLACER_BASE::AmplitudeStep( int aSign )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
int a = m_settings.m_maxAmplitude + aSign * m_settings.m_step;
|
|
|
|
a = std::max( a, m_settings.m_minAmplitude );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
m_settings.m_maxAmplitude = a;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void MEANDER_PLACER_BASE::SpacingStep( int aSign )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
int s = m_settings.m_spacing + aSign * m_settings.m_step;
|
2021-06-19 20:04:58 +00:00
|
|
|
s = std::max( s, m_currentWidth + Clearance() );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
m_settings.m_spacing = s;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2021-06-19 20:04:58 +00:00
|
|
|
int MEANDER_PLACER_BASE::Clearance()
|
|
|
|
{
|
Fix source comment/doc typos (follow-up)
Found via `codespell -q 3 -S *.po,./thirdparty -L aactual,acount,aline,alocation,alog,anormal,anumber,aother,apoints,aparent,aray,dout,einstance,modul,ot,overide,serie,te,,tesselate,tesselator,tht`
2021-07-03 22:37:31 +00:00
|
|
|
// Assumption: All tracks are part of the same net class.
|
2021-06-19 20:04:58 +00:00
|
|
|
// It shouldn't matter which track we pick. They should all have the same clearance if
|
|
|
|
// they are part of the same net class. Therefore, pick the first one on the list.
|
2022-08-29 20:21:46 +00:00
|
|
|
ITEM* itemToCheck = Traces().CItems().front();
|
2021-06-19 20:04:58 +00:00
|
|
|
PNS::CONSTRAINT constraint;
|
|
|
|
|
|
|
|
Router()->GetRuleResolver()->QueryConstraint( PNS::CONSTRAINT_TYPE::CT_CLEARANCE, itemToCheck,
|
|
|
|
nullptr, CurrentLayer(), &constraint );
|
|
|
|
|
2022-02-05 02:06:25 +00:00
|
|
|
wxCHECK_MSG( constraint.m_Value.HasMin(), m_currentWidth, wxT( "No minimum clearance?" ) );
|
2021-06-19 20:04:58 +00:00
|
|
|
|
|
|
|
return constraint.m_Value.Min();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void MEANDER_PLACER_BASE::UpdateSettings( const MEANDER_SETTINGS& aSettings )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
m_settings = aSettings;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2022-12-04 04:52:21 +00:00
|
|
|
int findAmplitudeBinarySearch( MEANDER_SHAPE& aCopy, int targetLength, int minAmp, int maxAmp )
|
|
|
|
{
|
|
|
|
if( minAmp == maxAmp )
|
|
|
|
return maxAmp;
|
|
|
|
|
|
|
|
aCopy.Resize( minAmp );
|
|
|
|
int minLen = aCopy.CurrentLength();
|
|
|
|
|
|
|
|
aCopy.Resize( maxAmp );
|
|
|
|
int maxLen = aCopy.CurrentLength();
|
|
|
|
|
|
|
|
if( minLen > targetLength )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( maxLen < targetLength )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int minError = minLen - targetLength;
|
|
|
|
int maxError = maxLen - targetLength;
|
|
|
|
|
|
|
|
if( std::abs( minError ) < LENGTH_TARGET_TOLERANCE
|
|
|
|
|| std::abs( maxError ) < LENGTH_TARGET_TOLERANCE )
|
|
|
|
{
|
|
|
|
return std::abs( minError ) < std::abs( maxError ) ? minAmp : maxAmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int left =
|
|
|
|
findAmplitudeBinarySearch( aCopy, targetLength, minAmp, ( minAmp + maxAmp ) / 2 );
|
|
|
|
|
|
|
|
if( left )
|
|
|
|
return left;
|
|
|
|
|
|
|
|
int right =
|
|
|
|
findAmplitudeBinarySearch( aCopy, targetLength, ( minAmp + maxAmp ) / 2, maxAmp );
|
|
|
|
|
|
|
|
if( right )
|
|
|
|
return right;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int findAmplitudeForLength( MEANDER_SHAPE* m, int targetLength, int minAmp, int maxAmp )
|
|
|
|
{
|
|
|
|
MEANDER_SHAPE copy = *m;
|
|
|
|
|
|
|
|
// Try to keep the same baseline length
|
|
|
|
copy.SetTargetBaselineLength( m->BaselineLength() );
|
|
|
|
|
|
|
|
long long initialGuess = m->Amplitude() - ( m->CurrentLength() - targetLength ) / 2;
|
|
|
|
|
|
|
|
if( initialGuess >= minAmp && initialGuess <= maxAmp )
|
|
|
|
{
|
|
|
|
copy.Resize( minAmp );
|
|
|
|
|
|
|
|
if( std::abs( copy.CurrentLength() - targetLength ) < LENGTH_TARGET_TOLERANCE )
|
|
|
|
return initialGuess;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The length is non-trivial, use binary search
|
|
|
|
return findAmplitudeBinarySearch( copy, targetLength, minAmp, maxAmp );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-03 23:38:58 +00:00
|
|
|
void MEANDER_PLACER_BASE::tuneLineLength( MEANDERED_LINE& aTuned, long long int aElongation )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2022-12-04 04:52:21 +00:00
|
|
|
long long int maxElongation = 0;
|
|
|
|
long long int minElongation = 0;
|
|
|
|
bool finished = false;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
for( MEANDER_SHAPE* m : aTuned.Meanders() )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2021-08-09 20:00:35 +00:00
|
|
|
if( m->Type() != MT_CORNER && m->Type() != MT_ARC )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2022-12-04 04:52:21 +00:00
|
|
|
MEANDER_SHAPE end = *m;
|
|
|
|
MEANDER_TYPE endType;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2022-12-04 04:52:21 +00:00
|
|
|
if( m->Type() == MT_START || m->Type() == MT_SINGLE )
|
|
|
|
endType = MT_SINGLE;
|
|
|
|
else
|
|
|
|
endType = MT_FINISH;
|
|
|
|
|
|
|
|
end.SetType( endType );
|
|
|
|
end.Recalculate();
|
|
|
|
|
|
|
|
long long int maxEndElongation = end.CurrentLength() - end.BaselineLength();
|
|
|
|
|
|
|
|
if( maxElongation + maxEndElongation > aElongation )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
if( !finished )
|
|
|
|
{
|
2022-12-04 04:52:21 +00:00
|
|
|
m->SetType( endType );
|
|
|
|
m->Recalculate();
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2022-12-04 04:52:21 +00:00
|
|
|
if( endType == MT_SINGLE )
|
|
|
|
{
|
|
|
|
// Check if we need to fit this meander
|
|
|
|
long long int endMinElongation =
|
|
|
|
( m->MinTunableLength() - m->BaselineLength() );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2022-12-04 04:52:21 +00:00
|
|
|
if( minElongation + endMinElongation >= aElongation )
|
|
|
|
m->MakeEmpty();
|
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
finished = true;
|
2020-11-24 22:16:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
m->MakeEmpty();
|
|
|
|
}
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
2022-12-04 04:52:21 +00:00
|
|
|
|
|
|
|
maxElongation += m->CurrentLength() - m->BaselineLength();
|
|
|
|
minElongation += m->MinTunableLength() - m->BaselineLength();
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 04:52:21 +00:00
|
|
|
long long int remainingElongation = aElongation;
|
|
|
|
int meanderCount = 0;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2020-11-24 22:16:41 +00:00
|
|
|
for( MEANDER_SHAPE* m : aTuned.Meanders() )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2021-08-09 20:00:35 +00:00
|
|
|
if( m->Type() != MT_CORNER && m->Type() != MT_ARC && m->Type() != MT_EMPTY )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2022-12-04 04:52:21 +00:00
|
|
|
remainingElongation -= m->CurrentLength() - m->BaselineLength();
|
|
|
|
meanderCount++;
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 04:52:21 +00:00
|
|
|
long long int lenReductionLeft = -remainingElongation;
|
|
|
|
int meandersLeft = meanderCount;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2022-12-04 04:52:21 +00:00
|
|
|
if( lenReductionLeft < 0 || !meandersLeft )
|
|
|
|
return;
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2022-12-04 04:52:21 +00:00
|
|
|
for( MEANDER_SHAPE* m : aTuned.Meanders() )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2022-12-04 04:52:21 +00:00
|
|
|
if( m->Type() != MT_CORNER && m->Type() != MT_ARC && m->Type() != MT_EMPTY )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2022-12-04 04:52:21 +00:00
|
|
|
long long int lenReductionHere = lenReductionLeft / meandersLeft;
|
|
|
|
long long int initialLen = m->CurrentLength();
|
|
|
|
int minAmpl = m->MinAmplitude();
|
|
|
|
|
|
|
|
int amp = findAmplitudeForLength( m, initialLen - lenReductionHere, minAmpl,
|
|
|
|
m->Amplitude() );
|
|
|
|
|
|
|
|
if( amp < minAmpl )
|
|
|
|
amp = minAmpl;
|
|
|
|
|
|
|
|
m->SetTargetBaselineLength( m->BaselineLength() );
|
|
|
|
m->Resize( amp );
|
|
|
|
|
|
|
|
lenReductionLeft -= initialLen - m->CurrentLength();
|
|
|
|
meandersLeft--;
|
|
|
|
|
|
|
|
if( !meandersLeft )
|
|
|
|
break;
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2019-10-31 13:00:01 +00:00
|
|
|
int MEANDER_PLACER_BASE::GetTotalPadToDieLength( const LINE& aLine ) const
|
|
|
|
{
|
|
|
|
int length = 0;
|
|
|
|
JOINT start;
|
|
|
|
JOINT end;
|
|
|
|
|
|
|
|
m_world->FindLineEnds( aLine, start, end );
|
|
|
|
|
|
|
|
// Extract the length of the pad to die for start and end pads
|
|
|
|
for( auto& link : start.LinkList() )
|
|
|
|
{
|
2022-08-29 20:21:46 +00:00
|
|
|
if( const SOLID* solid = dyn_cast<const SOLID*>( link ) )
|
2019-10-31 13:00:01 +00:00
|
|
|
{
|
|
|
|
// If there are overlapping pads, choose the first with a non-zero length
|
|
|
|
if( solid->GetPadToDie() > 0 )
|
|
|
|
{
|
|
|
|
length += solid->GetPadToDie();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( auto& link : end.LinkList() )
|
|
|
|
{
|
2022-08-29 20:21:46 +00:00
|
|
|
if( const SOLID* solid = dyn_cast<const SOLID*>( link ) )
|
2019-10-31 13:00:01 +00:00
|
|
|
{
|
|
|
|
if( solid->GetPadToDie() > 0 )
|
|
|
|
{
|
|
|
|
length += solid->GetPadToDie();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
const MEANDER_SETTINGS& MEANDER_PLACER_BASE::MeanderSettings() const
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
return m_settings;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2021-01-04 13:54:29 +00:00
|
|
|
VECTOR2I MEANDER_PLACER_BASE::getSnappedStartPoint( LINKED_ITEM* aStartItem, VECTOR2I aStartPoint )
|
|
|
|
{
|
|
|
|
if( aStartItem->Kind() == ITEM::SEGMENT_T )
|
|
|
|
{
|
|
|
|
return static_cast<SEGMENT*>( aStartItem )->Seg().NearestPoint( aStartPoint );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxASSERT( aStartItem->Kind() == ITEM::ARC_T );
|
|
|
|
ARC* arc = static_cast<ARC*>( aStartItem );
|
|
|
|
|
|
|
|
if( ( VECTOR2I( arc->Anchor( 0 ) - aStartPoint ) ).SquaredEuclideanNorm() <=
|
|
|
|
( VECTOR2I( arc->Anchor( 1 ) - aStartPoint ) ).SquaredEuclideanNorm() )
|
|
|
|
{
|
|
|
|
return arc->Anchor( 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return arc->Anchor( 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-20 23:31:10 +00:00
|
|
|
|
2022-08-02 03:23:07 +00:00
|
|
|
long long int MEANDER_PLACER_BASE::lineLength( const ITEM_SET& aLine, const SOLID* aStartPad, const SOLID* aEndPad ) const
|
2021-04-20 23:31:10 +00:00
|
|
|
{
|
|
|
|
long long int total = 0;
|
|
|
|
|
2022-08-02 03:23:07 +00:00
|
|
|
if( aLine.Empty() )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const ITEM* start_item = aLine[0];
|
|
|
|
const ITEM* end_item = aLine[aLine.Size() - 1];
|
|
|
|
bool start_via = false;
|
|
|
|
bool end_via = false;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If there is a start pad but the pad's layers do not overlap the first track layer, then there must be a
|
|
|
|
* fanout via on the line. If there isn't, we still need to have the via back to the pad, so count the distance
|
|
|
|
* in the line tuning
|
|
|
|
*/
|
|
|
|
start_via = aStartPad && ( !aStartPad->LayersOverlap( start_item ) );
|
|
|
|
end_via = aEndPad && ( !aEndPad->LayersOverlap( end_item ) );
|
|
|
|
|
2021-04-20 23:31:10 +00:00
|
|
|
for( int idx = 0; idx < aLine.Size(); idx++ )
|
|
|
|
{
|
|
|
|
const ITEM* item = aLine[idx];
|
|
|
|
|
|
|
|
if( const LINE* l = dyn_cast<const LINE*>( item ) )
|
|
|
|
{
|
|
|
|
total += l->CLine().Length();
|
|
|
|
}
|
|
|
|
else if( item->OfKind( ITEM::VIA_T ) && idx > 0 && idx < aLine.Size() - 1 )
|
|
|
|
{
|
|
|
|
int layerPrev = aLine[idx - 1]->Layer();
|
|
|
|
int layerNext = aLine[idx + 1]->Layer();
|
|
|
|
|
|
|
|
if( layerPrev != layerNext )
|
|
|
|
total += m_router->GetInterface()->StackupHeight( layerPrev, layerNext );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 03:23:07 +00:00
|
|
|
if( start_via )
|
|
|
|
{
|
|
|
|
int layerPrev = aStartPad->Layer();
|
|
|
|
int layerNext = start_item->Layer();
|
|
|
|
|
|
|
|
total += m_router->GetInterface()->StackupHeight( layerPrev, layerNext );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( end_via )
|
|
|
|
{
|
|
|
|
int layerPrev = end_item->Layer();
|
|
|
|
int layerNext = aEndPad->Layer();
|
|
|
|
|
|
|
|
total += m_router->GetInterface()->StackupHeight( layerPrev, layerNext );
|
|
|
|
}
|
|
|
|
|
2021-04-20 23:31:10 +00:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2016-08-29 14:38:11 +00:00
|
|
|
}
|