kicad/pcbnew/router/pns_meander_placer.cpp

269 lines
6.3 KiB
C++
Raw Normal View History

/*
* KiRouter - a push-and-(sometimes-)shove PCB router
*
* Copyright (C) 2013-2015 CERN
* 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 <boost/foreach.hpp>
#include <base_units.h> // God forgive me doing this...
#include <colors.h>
#include "trace.h"
#include "pns_node.h"
#include "pns_itemset.h"
#include "pns_topology.h"
#include "pns_meander_placer.h"
#include "pns_router.h"
PNS_MEANDER_PLACER::PNS_MEANDER_PLACER( PNS_ROUTER* aRouter ) :
2015-02-18 16:53:46 +00:00
PNS_MEANDER_PLACER_BASE( aRouter )
{
m_world = NULL;
m_currentNode = NULL;
// Init temporary variables (do not leave uninitialized members)
m_initialSegment = NULL;
m_lastLength = 0;
m_lastStatus = TOO_SHORT;
}
2015-02-18 16:53:46 +00:00
PNS_MEANDER_PLACER::~PNS_MEANDER_PLACER()
{
}
2015-02-18 16:53:46 +00:00
PNS_NODE* PNS_MEANDER_PLACER::CurrentNode( bool aLoopsRemoved ) const
{
if( !m_currentNode )
return m_world;
return m_currentNode;
}
2015-02-18 16:53:46 +00:00
bool PNS_MEANDER_PLACER::Start( const VECTOR2I& aP, PNS_ITEM* aStartItem )
{
VECTOR2I p;
if( !aStartItem || !aStartItem->OfKind( PNS_ITEM::SEGMENT ) )
{
2015-02-18 16:53:46 +00:00
Router()->SetFailureReason( _( "Please select a track whose length you want to tune." ) );
return false;
}
2015-02-18 16:53:46 +00:00
m_initialSegment = static_cast<PNS_SEGMENT*>( aStartItem );
2015-02-18 16:53:46 +00:00
p = m_initialSegment->Seg().NearestPoint( aP );
m_currentNode = NULL;
m_currentStart = p;
2015-02-18 16:53:46 +00:00
m_world = Router()->GetWorld()->Branch();
m_originLine = m_world->AssembleLine( m_initialSegment );
PNS_TOPOLOGY topo( m_world );
m_tunedPath = topo.AssembleTrivialPath( m_initialSegment );
m_world->Remove( &m_originLine );
2015-02-18 16:53:46 +00:00
m_currentWidth = m_originLine.Width();
m_currentEnd = VECTOR2I( 0, 0 );
2015-02-18 16:53:46 +00:00
return true;
}
2015-02-18 16:53:46 +00:00
int PNS_MEANDER_PLACER::origPathLength() const
{
int total = 0;
2015-02-18 16:53:46 +00:00
BOOST_FOREACH( const PNS_ITEM* item, m_tunedPath.CItems() )
{
2015-02-18 16:53:46 +00:00
if( const PNS_LINE* l = dyn_cast<const PNS_LINE*>( item ) )
{
2015-02-18 16:53:46 +00:00
total += l->CLine().Length();
}
}
return total;
}
2015-02-18 16:53:46 +00:00
bool PNS_MEANDER_PLACER::Move( const VECTOR2I& aP, PNS_ITEM* aEndItem )
{
2015-02-18 16:53:46 +00:00
return doMove( aP, aEndItem, m_settings.m_targetLength );
}
2015-02-18 16:53:46 +00:00
bool PNS_MEANDER_PLACER::doMove( const VECTOR2I& aP, PNS_ITEM* aEndItem, int aTargetLength )
{
SHAPE_LINE_CHAIN pre, tuned, post;
2015-02-18 16:53:46 +00:00
if( m_currentNode )
delete m_currentNode;
2015-02-18 16:53:46 +00:00
m_currentNode = m_world->Branch();
cutTunedLine( m_originLine.CLine(), m_currentStart, aP, pre, tuned, post );
m_result = PNS_MEANDERED_LINE( this, false );
m_result.SetWidth( m_originLine.Width() );
m_result.SetBaselineOffset( 0 );
2015-02-18 16:53:46 +00:00
for( int i = 0; i < tuned.SegmentCount(); i++ )
{
const SEG s = tuned.CSegment( i );
m_result.AddCorner( s.A );
m_result.MeanderSegment( s );
m_result.AddCorner( s.B );
}
2015-02-18 16:53:46 +00:00
int lineLen = origPathLength();
m_lastLength = lineLen;
m_lastStatus = TUNED;
2015-02-18 16:53:46 +00:00
if( compareWithTolerance( lineLen, aTargetLength, m_settings.m_lengthTolerance ) > 0 )
{
m_lastStatus = TOO_LONG;
} else {
2015-02-18 16:53:46 +00:00
m_lastLength = lineLen - tuned.Length();
tuneLineLength( m_result, aTargetLength - lineLen );
}
2015-02-18 16:53:46 +00:00
BOOST_FOREACH ( const PNS_ITEM* item, m_tunedPath.CItems() )
{
2015-02-18 16:53:46 +00:00
if( const PNS_LINE* l = dyn_cast<const PNS_LINE*>( item ) )
{
Router()->DisplayDebugLine( l->CLine(), 5, 30000 );
}
}
2015-02-18 16:53:46 +00:00
if( m_lastStatus != TOO_LONG )
{
2015-02-18 16:53:46 +00:00
tuned.Clear();
2015-02-18 16:53:46 +00:00
BOOST_FOREACH( PNS_MEANDER_SHAPE* m, m_result.Meanders() )
{
if( m->Type() != MT_EMPTY )
{
2015-02-18 16:53:46 +00:00
tuned.Append ( m->CLine( 0 ) );
}
}
2015-02-18 16:53:46 +00:00
m_lastLength += tuned.Length();
2015-02-18 16:53:46 +00:00
int comp = compareWithTolerance( m_lastLength - aTargetLength, 0, m_settings.m_lengthTolerance );
if( comp > 0 )
m_lastStatus = TOO_LONG;
else if( comp < 0 )
m_lastStatus = TOO_SHORT;
else
m_lastStatus = TUNED;
}
2015-02-18 16:53:46 +00:00
m_finalShape.Clear();
m_finalShape.Append( pre );
m_finalShape.Append( tuned );
m_finalShape.Append( post );
2015-02-18 16:53:46 +00:00
m_finalShape.Simplify();
return true;
}
bool PNS_MEANDER_PLACER::FixRoute( const VECTOR2I& aP, PNS_ITEM* aEndItem )
{
2015-03-03 20:58:36 +00:00
if( !m_currentNode )
return false;
m_currentTrace = PNS_LINE( m_originLine, m_finalShape );
2015-02-18 16:53:46 +00:00
m_currentNode->Add( &m_currentTrace );
2015-02-18 16:53:46 +00:00
Router()->CommitRouting( m_currentNode );
return true;
}
2015-02-18 16:53:46 +00:00
bool PNS_MEANDER_PLACER::CheckFit( PNS_MEANDER_SHAPE* aShape )
{
PNS_LINE l( m_originLine, aShape->CLine( 0 ) );
2015-02-18 16:53:46 +00:00
if( m_currentNode->CheckColliding( &l ) )
return false;
2015-02-18 16:53:46 +00:00
int w = aShape->Width();
int clearance = w + m_settings.m_spacing;
return m_result.CheckSelfIntersections( aShape, clearance );
}
2015-02-18 16:53:46 +00:00
const PNS_ITEMSET PNS_MEANDER_PLACER::Traces()
{
m_currentTrace = PNS_LINE( m_originLine, m_finalShape );
return PNS_ITEMSET( &m_currentTrace );
}
2015-02-18 16:53:46 +00:00
const VECTOR2I& PNS_MEANDER_PLACER::CurrentEnd() const
{
return m_currentEnd;
}
2015-02-18 16:53:46 +00:00
int PNS_MEANDER_PLACER::CurrentLayer() const
{
2015-02-18 16:53:46 +00:00
return m_initialSegment->Layers().Start();
}
2015-02-18 16:53:46 +00:00
const wxString PNS_MEANDER_PLACER::TuningInfo() const
{
wxString status;
switch ( m_lastStatus )
{
2015-10-05 16:28:41 +00:00
case TOO_LONG:
status = _( "Too long: " );
break;
case TOO_SHORT:
status = _( "Too short: " );
break;
case TUNED:
status = _( "Tuned: " );
break;
default:
return _( "?" );
}
status += LengthDoubleToString( (double) m_lastLength, false );
status += "/";
status += LengthDoubleToString( (double) m_settings.m_targetLength, false );
2015-02-18 16:53:46 +00:00
return status;
}
2015-02-18 16:53:46 +00:00
PNS_MEANDER_PLACER::TUNING_STATUS PNS_MEANDER_PLACER::TuningStatus() const
{
return m_lastStatus;
}