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
|
2016-08-29 14:38:11 +00:00
|
|
|
* Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
|
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
|
|
|
*/
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
#include <geometry/shape_line_chain.h>
|
|
|
|
#include <geometry/shape_rect.h>
|
2016-05-28 16:46:29 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
#include "pns_line.h"
|
2015-02-18 00:29:54 +00:00
|
|
|
#include "pns_diff_pair.h"
|
2013-09-18 17:55:16 +00:00
|
|
|
#include "pns_node.h"
|
2015-08-04 10:15:47 +00:00
|
|
|
#include "pns_solid.h"
|
2013-09-18 17:55:16 +00:00
|
|
|
#include "pns_optimizer.h"
|
2018-05-03 17:52:42 +00:00
|
|
|
|
|
|
|
#include "../../include/geometry/shape_simple.h"
|
2013-09-18 17:55:16 +00:00
|
|
|
#include "pns_utils.h"
|
2014-05-14 13:53:54 +00:00
|
|
|
#include "pns_router.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
|
|
|
/**
|
2013-09-26 21:53:54 +00:00
|
|
|
* Cost Estimator Methods
|
2014-05-14 13:53:54 +00:00
|
|
|
*/
|
2016-08-29 17:31:13 +00:00
|
|
|
int COST_ESTIMATOR::CornerCost( const SEG& aA, const SEG& aB )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
DIRECTION_45 dir_a( aA ), dir_b( aB );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
switch( dir_a.Angle( dir_b ) )
|
|
|
|
{
|
|
|
|
case DIRECTION_45::ANG_OBTUSE:
|
2019-08-21 02:33:26 +00:00
|
|
|
return 10;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
case DIRECTION_45::ANG_STRAIGHT:
|
2019-08-21 02:33:26 +00:00
|
|
|
return 5;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
case DIRECTION_45::ANG_ACUTE:
|
|
|
|
return 50;
|
|
|
|
|
|
|
|
case DIRECTION_45::ANG_RIGHT:
|
|
|
|
return 30;
|
|
|
|
|
|
|
|
case DIRECTION_45::ANG_HALF_FULL:
|
|
|
|
return 60;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return 100;
|
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
int COST_ESTIMATOR::CornerCost( const SHAPE_LINE_CHAIN& aLine )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
int total = 0;
|
|
|
|
|
|
|
|
for( int i = 0; i < aLine.SegmentCount() - 1; ++i )
|
|
|
|
total += CornerCost( aLine.CSegment( i ), aLine.CSegment( i + 1 ) );
|
|
|
|
|
|
|
|
return total;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
int COST_ESTIMATOR::CornerCost( const LINE& aLine )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
return CornerCost( aLine.CLine() );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void COST_ESTIMATOR::Add( LINE& aLine )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
m_lengthCost += aLine.CLine().Length();
|
2013-09-26 21:53:54 +00:00
|
|
|
m_cornerCost += CornerCost( aLine );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void COST_ESTIMATOR::Remove( LINE& aLine )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
m_lengthCost -= aLine.CLine().Length();
|
2013-09-26 21:53:54 +00:00
|
|
|
m_cornerCost -= CornerCost( aLine );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void COST_ESTIMATOR::Replace( LINE& aOldLine, LINE& aNewLine )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
m_lengthCost -= aOldLine.CLine().Length();
|
2013-09-26 21:53:54 +00:00
|
|
|
m_cornerCost -= CornerCost( aOldLine );
|
2014-05-14 13:53:54 +00:00
|
|
|
m_lengthCost += aNewLine.CLine().Length();
|
2013-09-26 21:53:54 +00:00
|
|
|
m_cornerCost += CornerCost( aNewLine );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool COST_ESTIMATOR::IsBetter( COST_ESTIMATOR& aOther,
|
2015-02-18 16:53:46 +00:00
|
|
|
double aLengthTolerance,
|
|
|
|
double aCornerTolerance ) const
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
if( aOther.m_cornerCost < m_cornerCost && aOther.m_lengthCost < m_lengthCost )
|
|
|
|
return true;
|
2013-10-14 11:43:57 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
else if( aOther.m_cornerCost < m_cornerCost * aCornerTolerance &&
|
|
|
|
aOther.m_lengthCost < m_lengthCost * aLengthTolerance )
|
2013-09-26 21:53:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-09-26 21:53:54 +00:00
|
|
|
* Optimizer
|
|
|
|
**/
|
2016-08-29 17:31:13 +00:00
|
|
|
OPTIMIZER::OPTIMIZER( NODE* aWorld ) :
|
2015-03-10 14:38:27 +00:00
|
|
|
m_world( aWorld ),
|
2016-08-29 17:31:13 +00:00
|
|
|
m_collisionKindMask( ITEM::ANY_T ),
|
2015-03-10 14:38:27 +00:00
|
|
|
m_effortLevel( MERGE_SEGMENTS ),
|
|
|
|
m_keepPostures( false ),
|
|
|
|
m_restrictAreaActive( false )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
OPTIMIZER::~OPTIMIZER()
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
struct OPTIMIZER::CACHE_VISITOR
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
CACHE_VISITOR( const ITEM* aOurItem, NODE* aNode, int aMask ) :
|
2013-09-26 21:53:54 +00:00
|
|
|
m_ourItem( aOurItem ),
|
|
|
|
m_collidingItem( NULL ),
|
|
|
|
m_node( aNode ),
|
|
|
|
m_mask( aMask )
|
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 operator()( ITEM* aOtherItem )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2015-07-07 16:38:49 +00:00
|
|
|
if( !( m_mask & aOtherItem->Kind() ) )
|
2013-09-26 21:53:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
int clearance = m_node->GetClearance( aOtherItem, m_ourItem );
|
|
|
|
|
2019-07-24 15:19:03 +00:00
|
|
|
if( !aOtherItem->Collide( m_ourItem, clearance, false, nullptr, m_node ) )
|
2013-09-26 21:53:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
m_collidingItem = aOtherItem;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
const ITEM* m_ourItem;
|
|
|
|
ITEM* m_collidingItem;
|
|
|
|
NODE* m_node;
|
2013-09-26 21:53:54 +00:00
|
|
|
int m_mask;
|
2013-09-18 17:55:16 +00:00
|
|
|
};
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void OPTIMIZER::cacheAdd( ITEM* aItem, bool aIsStatic = false )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
if( m_cacheTags.find( aItem ) != m_cacheTags.end() )
|
|
|
|
return;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
m_cache.Add( aItem );
|
2014-05-16 11:37:31 +00:00
|
|
|
m_cacheTags[aItem].m_hits = 1;
|
|
|
|
m_cacheTags[aItem].m_isStatic = aIsStatic;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void OPTIMIZER::removeCachedSegments( LINE* aLine, int aStartVertex, int aEndVertex )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2016-08-29 18:36:05 +00:00
|
|
|
if( !aLine->IsLinked() ) return;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 18:36:05 +00:00
|
|
|
LINE::SEGMENT_REFS& segs = aLine->LinkedSegments();
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
if( aEndVertex < 0 )
|
2014-05-14 13:53:54 +00:00
|
|
|
aEndVertex += aLine->PointCount();
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
for( int i = aStartVertex; i < aEndVertex - 1; i++ )
|
|
|
|
{
|
2016-08-29 18:36:05 +00:00
|
|
|
SEGMENT* s = segs[i];
|
2013-09-26 21:53:54 +00:00
|
|
|
m_cacheTags.erase( s );
|
|
|
|
m_cache.Remove( s );
|
2015-03-10 14:38:27 +00:00
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void OPTIMIZER::CacheRemove( ITEM* aItem )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
if( aItem->Kind() == ITEM::LINE_T )
|
|
|
|
removeCachedSegments( static_cast<LINE*>( aItem ) );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void OPTIMIZER::CacheStaticItem( ITEM* aItem )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
cacheAdd( aItem, true );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void OPTIMIZER::ClearCache( bool aStaticOnly )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
if( !aStaticOnly )
|
|
|
|
{
|
|
|
|
m_cacheTags.clear();
|
|
|
|
m_cache.Clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( CachedItemTags::iterator i = m_cacheTags.begin(); i!= m_cacheTags.end(); ++i )
|
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
if( i->second.m_isStatic )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
m_cache.Remove( i->first );
|
|
|
|
m_cacheTags.erase( i->first );
|
|
|
|
}
|
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
class LINE_RESTRICTIONS
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-02-18 16:53:46 +00:00
|
|
|
LINE_RESTRICTIONS() {};
|
|
|
|
~LINE_RESTRICTIONS() {};
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void Build( NODE* aWorld, LINE* aOriginLine, const SHAPE_LINE_CHAIN& aLine, const BOX2I& aRestrictedArea, bool aRestrictedAreaEnable );
|
2015-02-18 00:29:54 +00:00
|
|
|
bool Check ( int aVertex1, int aVertex2, const SHAPE_LINE_CHAIN& aReplacement );
|
|
|
|
void Dump();
|
|
|
|
|
|
|
|
private:
|
2016-08-29 17:31:13 +00:00
|
|
|
int allowedAngles( NODE* aWorld, const LINE* aLine, const VECTOR2I& aP, bool aFirst );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
struct RVERTEX
|
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
RVERTEX ( bool aRestricted, int aAllowedAngles ) :
|
|
|
|
restricted( aRestricted ),
|
|
|
|
allowedAngles( aAllowedAngles )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool restricted;
|
|
|
|
int allowedAngles;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<RVERTEX> m_rs;
|
|
|
|
};
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
// fixme: use later
|
2016-08-29 17:31:13 +00:00
|
|
|
int LINE_RESTRICTIONS::allowedAngles( NODE* aWorld, const LINE* aLine, const VECTOR2I& aP, bool aFirst )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
JOINT* jt = aWorld->FindJoint( aP , aLine );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
if( !jt )
|
|
|
|
return 0xff;
|
|
|
|
|
|
|
|
DIRECTION_45 dirs [8];
|
|
|
|
|
|
|
|
int n_dirs = 0;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
for( const ITEM* item : jt->Links().CItems() )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
if( item->OfKind( ITEM::VIA_T ) || item->OfKind( ITEM::SOLID_T ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
return 0xff;
|
2016-08-29 17:31:13 +00:00
|
|
|
else if( const SEGMENT* seg = dyn_cast<const SEGMENT*>( item ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
SEG s = seg->Seg();
|
2015-02-18 16:53:46 +00:00
|
|
|
if( s.A != aP )
|
2015-02-18 00:29:54 +00:00
|
|
|
s.Reverse();
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( n_dirs < 8 )
|
|
|
|
dirs[n_dirs++] = aFirst ? DIRECTION_45( s ) : DIRECTION_45( s ).Opposite();
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const int angleMask = DIRECTION_45::ANG_OBTUSE | DIRECTION_45::ANG_HALF_FULL | DIRECTION_45::ANG_STRAIGHT;
|
|
|
|
int outputMask = 0xff;
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
for( int d = 0; d < 8; d++ )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
DIRECTION_45 refDir( ( DIRECTION_45::Directions ) d );
|
|
|
|
|
|
|
|
for( int i = 0; i < n_dirs; i++ )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-07-07 16:36:38 +00:00
|
|
|
if( !( refDir.Angle( dirs[i] ) & angleMask ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
outputMask &= ~refDir.Mask();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 15:16:48 +00:00
|
|
|
//DrawDebugDirs( aP, outputMask, 3 );
|
2015-02-18 00:29:54 +00:00
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void LINE_RESTRICTIONS::Build( NODE* aWorld, LINE* aOriginLine, const SHAPE_LINE_CHAIN& aLine, const BOX2I& aRestrictedArea, bool aRestrictedAreaEnable )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
const SHAPE_LINE_CHAIN& l = aLine;
|
|
|
|
VECTOR2I v_prev;
|
|
|
|
int n = l.PointCount( );
|
|
|
|
|
|
|
|
m_rs.reserve( n );
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
for( int i = 0; i < n; i++ )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-06-15 08:27:24 +00:00
|
|
|
const VECTOR2I &v = l.CPoint( i );
|
2015-02-18 16:53:46 +00:00
|
|
|
RVERTEX r( false, 0xff );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( aRestrictedAreaEnable )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
bool exiting = ( i > 0 && aRestrictedArea.Contains( v_prev ) && !aRestrictedArea.Contains( v ) );
|
|
|
|
bool entering = false;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( i != l.PointCount() - 1 )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
const VECTOR2I& v_next = l.CPoint( i + 1 );
|
2015-02-18 00:29:54 +00:00
|
|
|
entering = ( !aRestrictedArea.Contains( v ) && aRestrictedArea.Contains( v_next ) );
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( entering )
|
|
|
|
{
|
|
|
|
const SEG& sp = l.CSegment( i );
|
|
|
|
r.allowedAngles = DIRECTION_45( sp ).Mask();
|
|
|
|
}
|
|
|
|
else if( exiting )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
const SEG& sp = l.CSegment( i - 1 );
|
|
|
|
r.allowedAngles = DIRECTION_45( sp ).Mask();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
r.allowedAngles = ( !aRestrictedArea.Contains( v ) ) ? 0 : 0xff;
|
2015-02-18 00:29:54 +00:00
|
|
|
r.restricted = r.allowedAngles ? false : true;
|
|
|
|
}
|
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
v_prev = v;
|
2015-02-18 16:53:46 +00:00
|
|
|
m_rs.push_back( r );
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
void LINE_RESTRICTIONS::Dump()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
bool LINE_RESTRICTIONS::Check( int aVertex1, int aVertex2, const SHAPE_LINE_CHAIN& aReplacement )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
if( m_rs.empty( ) )
|
|
|
|
return true;
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
for( int i = aVertex1; i <= aVertex2; i++ )
|
2015-02-18 00:29:54 +00:00
|
|
|
if ( m_rs[i].restricted )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const RVERTEX& v1 = m_rs[ aVertex1 ];
|
|
|
|
const RVERTEX& v2 = m_rs[ aVertex2 ];
|
|
|
|
|
|
|
|
int m1 = DIRECTION_45( aReplacement.CSegment( 0 ) ).Mask();
|
|
|
|
int m2;
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
if( aReplacement.SegmentCount() == 1 )
|
2015-02-18 00:29:54 +00:00
|
|
|
m2 = m1;
|
|
|
|
else
|
2015-02-18 16:53:46 +00:00
|
|
|
m2 = DIRECTION_45( aReplacement.CSegment( 1 ) ).Mask();
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
return ( ( v1.allowedAngles & m1 ) != 0 ) &&
|
|
|
|
( ( v2.allowedAngles & m2 ) != 0 );
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::checkColliding( ITEM* aItem, bool aUpdateCache )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
CACHE_VISITOR v( aItem, m_world, m_collisionKindMask );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2015-07-22 08:46:45 +00:00
|
|
|
return static_cast<bool>( m_world->CheckColliding( aItem ) );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-05-11 09:18:27 +00:00
|
|
|
#if 0
|
2013-09-26 21:53:54 +00:00
|
|
|
// something is wrong with the cache, need to investigate.
|
2014-05-14 13:53:54 +00:00
|
|
|
m_cache.Query( aItem->Shape(), m_world->GetMaxClearance(), v, false );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( !v.m_collidingItem )
|
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
NODE::OPT_OBSTACLE obs = m_world->CheckColliding( aItem );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( obs )
|
|
|
|
{
|
|
|
|
if( aUpdateCache )
|
2014-05-16 11:37:31 +00:00
|
|
|
cacheAdd( obs->m_item );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
m_cacheTags[v.m_collidingItem].m_hits++;
|
2013-09-26 21:53:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-05-11 09:18:27 +00:00
|
|
|
#endif
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::checkColliding( LINE* aLine, const SHAPE_LINE_CHAIN& aOptPath )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE tmp( *aLine, aOptPath );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
return checkColliding( &tmp );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::mergeObtuse( LINE* aLine )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
SHAPE_LINE_CHAIN& line = aLine->Line();
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
int step = line.PointCount() - 3;
|
|
|
|
int iter = 0;
|
|
|
|
int segs_pre = line.SegmentCount();
|
|
|
|
|
|
|
|
if( step < 0 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SHAPE_LINE_CHAIN current_path( line );
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
iter++;
|
|
|
|
int n_segs = current_path.SegmentCount();
|
|
|
|
int max_step = n_segs - 2;
|
|
|
|
|
|
|
|
if( step > max_step )
|
|
|
|
step = max_step;
|
|
|
|
|
|
|
|
if( step < 2 )
|
|
|
|
{
|
|
|
|
line = current_path;
|
|
|
|
return current_path.SegmentCount() < segs_pre;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool found_anything = false;
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
while( n < n_segs - step )
|
|
|
|
{
|
2013-10-14 14:13:35 +00:00
|
|
|
const SEG s1 = current_path.CSegment( n );
|
|
|
|
const SEG s2 = current_path.CSegment( n + step );
|
2013-09-26 21:53:54 +00:00
|
|
|
SEG s1opt, s2opt;
|
|
|
|
|
|
|
|
if( DIRECTION_45( s1 ).IsObtuse( DIRECTION_45( s2 ) ) )
|
|
|
|
{
|
|
|
|
VECTOR2I ip = *s1.IntersectLines( s2 );
|
|
|
|
|
|
|
|
if( s1.Distance( ip ) <= 1 || s2.Distance( ip ) <= 1 )
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
s1opt = SEG( s1.A, ip );
|
|
|
|
s2opt = SEG( ip, s2.B );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-14 18:40:36 +00:00
|
|
|
s1opt = SEG( s1.A, ip );
|
|
|
|
s2opt = SEG( ip, s2.B );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( DIRECTION_45( s1opt ).IsObtuse( DIRECTION_45( s2opt ) ) )
|
|
|
|
{
|
|
|
|
SHAPE_LINE_CHAIN opt_path;
|
2013-10-14 18:40:36 +00:00
|
|
|
opt_path.Append( s1opt.A );
|
|
|
|
opt_path.Append( s1opt.B );
|
|
|
|
opt_path.Append( s2opt.B );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE opt_track( *aLine, opt_path );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( !checkColliding( &opt_track ) )
|
|
|
|
{
|
|
|
|
current_path.Replace( s1.Index() + 1, s2.Index(), ip );
|
|
|
|
// removeCachedSegments(aLine, s1.Index(), s2.Index());
|
|
|
|
n_segs = current_path.SegmentCount();
|
|
|
|
found_anything = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !found_anything )
|
|
|
|
{
|
|
|
|
if( step <= 2 )
|
|
|
|
{
|
|
|
|
line = current_path;
|
|
|
|
return line.SegmentCount() < segs_pre;
|
|
|
|
}
|
|
|
|
|
|
|
|
step--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return line.SegmentCount() < segs_pre;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::mergeFull( LINE* aLine )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
SHAPE_LINE_CHAIN& line = aLine->Line();
|
2013-09-26 21:53:54 +00:00
|
|
|
int step = line.SegmentCount() - 1;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
int segs_pre = line.SegmentCount();
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
line.Simplify();
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
if( step < 0 )
|
|
|
|
return false;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
SHAPE_LINE_CHAIN current_path( line );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
int n_segs = current_path.SegmentCount();
|
|
|
|
int max_step = n_segs - 2;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
if( step > max_step )
|
|
|
|
step = max_step;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
if( step < 1 )
|
|
|
|
break;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
bool found_anything = mergeStep( aLine, current_path, step );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
if( !found_anything )
|
|
|
|
step--;
|
|
|
|
}
|
|
|
|
|
|
|
|
aLine->SetShape( current_path );
|
|
|
|
|
|
|
|
return current_path.SegmentCount() < segs_pre;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::Optimize( LINE* aLine, LINE* aResult )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
if( !aResult )
|
|
|
|
aResult = aLine;
|
|
|
|
else
|
|
|
|
*aResult = *aLine;
|
|
|
|
|
|
|
|
m_keepPostures = false;
|
|
|
|
|
|
|
|
bool rv = false;
|
|
|
|
|
|
|
|
if( m_effortLevel & MERGE_SEGMENTS )
|
|
|
|
rv |= mergeFull( aResult );
|
|
|
|
|
|
|
|
if( m_effortLevel & MERGE_OBTUSE )
|
|
|
|
rv |= mergeObtuse( aResult );
|
|
|
|
|
|
|
|
if( m_effortLevel & SMART_PADS )
|
|
|
|
rv |= runSmartPads( aResult );
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
if( m_effortLevel & FANOUT_CLEANUP )
|
|
|
|
rv |= fanoutCleanup( aResult );
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
return rv;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::mergeStep( LINE* aLine, SHAPE_LINE_CHAIN& aCurrentPath, int step )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
int n = 0;
|
|
|
|
int n_segs = aCurrentPath.SegmentCount();
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
int cost_orig = COST_ESTIMATOR::CornerCost( aCurrentPath );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
LINE_RESTRICTIONS restr;
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
if( aLine->SegmentCount() < 4 )
|
2013-09-26 21:53:54 +00:00
|
|
|
return false;
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
DIRECTION_45 orig_start( aLine->CSegment( 0 ) );
|
|
|
|
DIRECTION_45 orig_end( aLine->CSegment( -1 ) );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
restr.Build( m_world, aLine, aCurrentPath, m_restrictArea, m_restrictAreaActive );
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
while( n < n_segs - step )
|
|
|
|
{
|
|
|
|
const SEG s1 = aCurrentPath.CSegment( n );
|
|
|
|
const SEG s2 = aCurrentPath.CSegment( n + step );
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
SHAPE_LINE_CHAIN path[2];
|
|
|
|
SHAPE_LINE_CHAIN* picked = NULL;
|
2013-09-26 21:53:54 +00:00
|
|
|
int cost[2];
|
|
|
|
|
|
|
|
for( int i = 0; i < 2; i++ )
|
|
|
|
{
|
|
|
|
bool postureMatch = true;
|
2013-10-14 18:40:36 +00:00
|
|
|
SHAPE_LINE_CHAIN bypass = DIRECTION_45().BuildInitialTrace( s1.A, s2.B, i );
|
2013-09-26 21:53:54 +00:00
|
|
|
cost[i] = INT_MAX;
|
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
bool restrictionsOK = restr.Check ( n, n + step + 1, bypass );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( n == 0 && orig_start != DIRECTION_45( bypass.CSegment( 0 ) ) )
|
|
|
|
postureMatch = false;
|
|
|
|
else if( n == n_segs - step && orig_end != DIRECTION_45( bypass.CSegment( -1 ) ) )
|
|
|
|
postureMatch = false;
|
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
if( restrictionsOK && (postureMatch || !m_keepPostures) && !checkColliding( aLine, bypass ) )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
|
|
|
path[i] = aCurrentPath;
|
|
|
|
path[i].Replace( s1.Index(), s2.Index(), bypass );
|
|
|
|
path[i].Simplify();
|
2016-08-29 17:31:13 +00:00
|
|
|
cost[i] = COST_ESTIMATOR::CornerCost( path[i] );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( cost[0] < cost_orig && cost[0] < cost[1] )
|
|
|
|
picked = &path[0];
|
|
|
|
else if( cost[1] < cost_orig )
|
|
|
|
picked = &path[1];
|
|
|
|
|
|
|
|
if( picked )
|
|
|
|
{
|
|
|
|
n_segs = aCurrentPath.SegmentCount();
|
|
|
|
aCurrentPath = *picked;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
OPTIMIZER::BREAKOUT_LIST OPTIMIZER::circleBreakouts( int aWidth,
|
2013-09-26 21:53:54 +00:00
|
|
|
const SHAPE* aShape, bool aPermitDiagonal ) const
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
BREAKOUT_LIST breakouts;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
for( int angle = 0; angle < 360; angle += 45 )
|
|
|
|
{
|
|
|
|
const SHAPE_CIRCLE* cir = static_cast<const SHAPE_CIRCLE*>( aShape );
|
|
|
|
SHAPE_LINE_CHAIN l;
|
|
|
|
VECTOR2I p0 = cir->GetCenter();
|
|
|
|
VECTOR2I v0( cir->GetRadius() * M_SQRT2, 0 );
|
|
|
|
l.Append( p0 );
|
|
|
|
l.Append( p0 + v0.Rotate( angle * M_PI / 180.0 ) );
|
|
|
|
breakouts.push_back( l );
|
|
|
|
}
|
|
|
|
|
|
|
|
return breakouts;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-03 22:46:42 +00:00
|
|
|
OPTIMIZER::BREAKOUT_LIST OPTIMIZER::customBreakouts( int aWidth,
|
|
|
|
const ITEM* aItem, bool aPermitDiagonal ) const
|
2015-07-02 14:09:43 +00:00
|
|
|
{
|
|
|
|
BREAKOUT_LIST breakouts;
|
2018-05-03 22:46:42 +00:00
|
|
|
const SHAPE_SIMPLE* convex = static_cast<const SHAPE_SIMPLE*>( aItem->Shape() );
|
2015-07-02 14:09:43 +00:00
|
|
|
|
|
|
|
BOX2I bbox = convex->BBox( 0 );
|
2018-05-03 22:46:42 +00:00
|
|
|
VECTOR2I p0 = static_cast<const SOLID*>( aItem )->Pos();
|
2015-07-02 14:09:43 +00:00
|
|
|
// must be large enough to guarantee intersecting the convex polygon
|
2019-08-21 02:33:26 +00:00
|
|
|
int length = std::max( bbox.GetWidth(), bbox.GetHeight() ) / 2 + 5;
|
2015-07-02 14:09:43 +00:00
|
|
|
|
|
|
|
for( int angle = 0; angle < 360; angle += ( aPermitDiagonal ? 45 : 90 ) )
|
|
|
|
{
|
|
|
|
SHAPE_LINE_CHAIN l;
|
|
|
|
VECTOR2I v0( p0 + VECTOR2I( length, 0 ).Rotate( angle * M_PI / 180.0 ) );
|
|
|
|
SHAPE_LINE_CHAIN::INTERSECTIONS intersections;
|
|
|
|
int n = convex->Vertices().Intersect( SEG( p0, v0 ), intersections );
|
|
|
|
// if n == 1 intersected a segment
|
|
|
|
// if n == 2 intersected the common point of 2 segments
|
|
|
|
// n == 0 can not happen I think, but...
|
|
|
|
if( n > 0 )
|
|
|
|
{
|
|
|
|
l.Append( p0 );
|
|
|
|
|
|
|
|
// for a breakout distance relative to the distance between
|
|
|
|
// center and polygon edge
|
|
|
|
//l.Append( intersections[0].p + (v0 - p0).Resize( (intersections[0].p - p0).EuclideanNorm() * 0.4 ) );
|
|
|
|
|
|
|
|
// for an absolute breakout distance, e.g. 0.1 mm
|
2019-08-21 02:33:26 +00:00
|
|
|
//l.Append( intersections[0].p + (v0 - p0).Resize( 100000 ) );
|
2015-07-02 14:09:43 +00:00
|
|
|
|
|
|
|
// for the breakout right on the polygon edge
|
2019-08-21 02:33:26 +00:00
|
|
|
l.Append( intersections[0].p );
|
2015-07-02 14:09:43 +00:00
|
|
|
|
|
|
|
breakouts.push_back( l );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return breakouts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
OPTIMIZER::BREAKOUT_LIST OPTIMIZER::rectBreakouts( int aWidth,
|
2013-09-26 21:53:54 +00:00
|
|
|
const SHAPE* aShape, bool aPermitDiagonal ) const
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
const SHAPE_RECT* rect = static_cast<const SHAPE_RECT*>(aShape);
|
2019-10-25 06:18:16 +00:00
|
|
|
VECTOR2I s = rect->GetSize();
|
|
|
|
VECTOR2I c = rect->GetPosition() + VECTOR2I( s.x / 2, s.y / 2 );
|
2014-05-16 11:37:31 +00:00
|
|
|
BREAKOUT_LIST breakouts;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
VECTOR2I d_offset;
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
d_offset.x = ( s.x > s.y ) ? ( s.x - s.y ) / 2 : 0;
|
|
|
|
d_offset.y = ( s.x < s.y ) ? ( s.y - s.x ) / 2 : 0;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
VECTOR2I d_vert = VECTOR2I( 0, s.y / 2 + aWidth );
|
|
|
|
VECTOR2I d_horiz = VECTOR2I( s.x / 2 + aWidth, 0 );
|
|
|
|
|
2019-03-24 13:54:56 +00:00
|
|
|
breakouts.push_back( SHAPE_LINE_CHAIN( { c, c + d_horiz } ) );
|
|
|
|
breakouts.push_back( SHAPE_LINE_CHAIN( { c, c - d_horiz } ) );
|
|
|
|
breakouts.push_back( SHAPE_LINE_CHAIN( { c, c + d_vert } ) );
|
|
|
|
breakouts.push_back( SHAPE_LINE_CHAIN( { c, c - d_vert } ) );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( aPermitDiagonal )
|
|
|
|
{
|
|
|
|
int l = aWidth + std::min( s.x, s.y ) / 2;
|
|
|
|
VECTOR2I d_diag;
|
|
|
|
|
|
|
|
if( s.x >= s.y )
|
|
|
|
{
|
2019-03-24 13:54:56 +00:00
|
|
|
breakouts.push_back(
|
|
|
|
SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( l, l ) } ) );
|
|
|
|
breakouts.push_back(
|
|
|
|
SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset - VECTOR2I( -l, l ) } ) );
|
|
|
|
breakouts.push_back(
|
|
|
|
SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset + VECTOR2I( -l, l ) } ) );
|
|
|
|
breakouts.push_back(
|
|
|
|
SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( l, l ) } ) );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// fixme: this could be done more efficiently
|
2019-03-24 13:54:56 +00:00
|
|
|
breakouts.push_back(
|
|
|
|
SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( l, l ) } ) );
|
|
|
|
breakouts.push_back(
|
|
|
|
SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( -l, l ) } ) );
|
|
|
|
breakouts.push_back(
|
|
|
|
SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( -l, l ) } ) );
|
|
|
|
breakouts.push_back(
|
|
|
|
SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( l, l ) } ) );
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return breakouts;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
OPTIMIZER::BREAKOUT_LIST OPTIMIZER::computeBreakouts( int aWidth,
|
|
|
|
const ITEM* aItem, bool aPermitDiagonal ) const
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
switch( aItem->Kind() )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
case ITEM::VIA_T:
|
2014-05-16 11:37:31 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
const VIA* via = static_cast<const VIA*>( aItem );
|
2014-05-16 11:37:31 +00:00
|
|
|
return circleBreakouts( aWidth, via->Shape(), aPermitDiagonal );
|
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
case ITEM::SOLID_T:
|
2014-05-16 11:37:31 +00:00
|
|
|
{
|
|
|
|
const SHAPE* shape = aItem->Shape();
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
switch( shape->Type() )
|
|
|
|
{
|
|
|
|
case SH_RECT:
|
|
|
|
return rectBreakouts( aWidth, shape, aPermitDiagonal );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
case SH_SEGMENT:
|
|
|
|
{
|
|
|
|
const SHAPE_SEGMENT* seg = static_cast<const SHAPE_SEGMENT*> (shape);
|
|
|
|
const SHAPE_RECT rect = ApproximateSegmentAsRect ( *seg );
|
|
|
|
return rectBreakouts( aWidth, &rect, aPermitDiagonal );
|
|
|
|
}
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
case SH_CIRCLE:
|
|
|
|
return circleBreakouts( aWidth, shape, aPermitDiagonal );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2018-05-03 17:52:42 +00:00
|
|
|
case SH_SIMPLE:
|
2018-05-03 22:46:42 +00:00
|
|
|
return customBreakouts( aWidth, aItem, aPermitDiagonal );
|
2015-07-02 14:09:43 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
2013-09-26 21:53:54 +00:00
|
|
|
}
|
2018-05-03 22:46:42 +00:00
|
|
|
|
|
|
|
break;
|
2014-05-16 11:37:31 +00:00
|
|
|
}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
return BREAKOUT_LIST();
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
ITEM* OPTIMIZER::findPadOrVia( int aLayer, int aNet, const VECTOR2I& aP ) const
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
JOINT* jt = m_world->FindJoint( aP, aLayer, aNet );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( !jt )
|
|
|
|
return NULL;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
for( ITEM* item : jt->LinkList() )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
if( item->OfKind( ITEM::VIA_T | ITEM::SOLID_T ) )
|
2013-09-26 21:53:54 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
int OPTIMIZER::smartPadsSingle( LINE* aLine, ITEM* aPad, bool aEnd, int aEndVertex )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
DIRECTION_45 dir;
|
|
|
|
|
|
|
|
const int ForbiddenAngles = DIRECTION_45::ANG_ACUTE | DIRECTION_45::ANG_RIGHT |
|
|
|
|
DIRECTION_45::ANG_HALF_FULL | DIRECTION_45::ANG_UNDEFINED;
|
|
|
|
|
2019-10-25 06:18:16 +00:00
|
|
|
typedef std::tuple<int, long long int, SHAPE_LINE_CHAIN> RtVariant;
|
2013-12-13 16:27:30 +00:00
|
|
|
std::vector<RtVariant> variants;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
SOLID* solid = dyn_cast<SOLID*>( aPad );
|
2015-08-04 10:15:47 +00:00
|
|
|
|
2018-02-15 15:09:39 +00:00
|
|
|
// don't do optimized connections for offset pads
|
2015-08-04 10:15:47 +00:00
|
|
|
if( solid && solid->Offset() != VECTOR2I( 0, 0 ) )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
2019-08-21 02:33:26 +00:00
|
|
|
BREAKOUT_LIST breakouts = computeBreakouts( aLine->Width(), aPad, true );
|
2014-05-14 13:53:54 +00:00
|
|
|
SHAPE_LINE_CHAIN line = ( aEnd ? aLine->CLine().Reverse() : aLine->CLine() );
|
2019-08-21 02:33:26 +00:00
|
|
|
int p_end = std::min( aEndVertex, std::min( 3, line.PointCount() - 1 ) );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-08-21 02:33:26 +00:00
|
|
|
// Start at 1 to find a potentially better breakout (0 is the pad connection)
|
2013-09-26 21:53:54 +00:00
|
|
|
for( int p = 1; p <= p_end; p++ )
|
|
|
|
{
|
2019-08-21 02:33:26 +00:00
|
|
|
// If the line is contained inside the pad, don't optimize
|
2019-08-22 14:55:27 +00:00
|
|
|
if( solid && solid->Shape() && !solid->Shape()->Collide(
|
2019-08-21 02:33:26 +00:00
|
|
|
SEG( line.CPoint( 0 ), line.CPoint( p ) ), aLine->Width() / 2 ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for( SHAPE_LINE_CHAIN & breakout : breakouts ) {
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
for( int diag = 0; diag < 2; diag++ )
|
|
|
|
{
|
|
|
|
SHAPE_LINE_CHAIN v;
|
2019-08-21 02:33:26 +00:00
|
|
|
SHAPE_LINE_CHAIN connect = dir.BuildInitialTrace(
|
|
|
|
breakout.CPoint( -1 ), line.CPoint( p ), diag == 0 );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2019-08-21 02:33:26 +00:00
|
|
|
DIRECTION_45 dir_bkout( breakout.CSegment( -1 ) );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
if(!connect.SegmentCount())
|
|
|
|
continue;
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
int ang1 = dir_bkout.Angle( DIRECTION_45( connect.CSegment( 0 ) ) );
|
|
|
|
|
2019-08-21 02:33:26 +00:00
|
|
|
if( ang1 & ForbiddenAngles )
|
2013-09-26 21:53:54 +00:00
|
|
|
continue;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2019-08-21 02:33:26 +00:00
|
|
|
if( breakout.Length() > line.Length() )
|
2013-09-26 21:53:54 +00:00
|
|
|
continue;
|
|
|
|
|
2019-08-21 02:33:26 +00:00
|
|
|
v = breakout;
|
2013-09-26 21:53:54 +00:00
|
|
|
v.Append( connect );
|
|
|
|
|
|
|
|
for( int i = p + 1; i < line.PointCount(); i++ )
|
|
|
|
v.Append( line.CPoint( i ) );
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE tmp( *aLine, v );
|
2013-09-26 21:53:54 +00:00
|
|
|
int cc = tmp.CountCorners( ForbiddenAngles );
|
|
|
|
|
|
|
|
if( cc == 0 )
|
|
|
|
{
|
|
|
|
RtVariant vp;
|
2019-10-25 06:18:16 +00:00
|
|
|
std::get<0>( vp ) = p;
|
|
|
|
std::get<1>( vp ) = breakout.Length();
|
|
|
|
std::get<2>( vp ) = aEnd ? v.Reverse() : v;
|
|
|
|
std::get<2>( vp ).Simplify();
|
2013-09-26 21:53:54 +00:00
|
|
|
variants.push_back( vp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 02:33:26 +00:00
|
|
|
// We attempt to minimize the corner cost (minimizes the segments and types of corners)
|
|
|
|
// but given two, equally valid costs, we want to pick the longer pad exit. The logic
|
|
|
|
// here is that if the pad is oblong, the track should not exit the shorter side and parallel
|
|
|
|
// the pad but should follow the pad's preferential direction before exiting.
|
|
|
|
int min_cost = INT_MAX;
|
2019-10-25 06:18:16 +00:00
|
|
|
long long int max_length = 0;
|
2013-09-26 21:53:54 +00:00
|
|
|
SHAPE_LINE_CHAIN l_best;
|
2013-10-14 14:13:35 +00:00
|
|
|
bool found = false;
|
|
|
|
int p_best = -1;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-06-29 20:07:55 +00:00
|
|
|
for( RtVariant& vp : variants )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2019-10-25 06:18:16 +00:00
|
|
|
LINE tmp( *aLine, std::get<2>( vp ) );
|
|
|
|
int cost = COST_ESTIMATOR::CornerCost( std::get<2>( vp ) );
|
|
|
|
long long int len = std::get<1>( vp );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
if( !checkColliding( &tmp ) )
|
|
|
|
{
|
2019-08-21 02:33:26 +00:00
|
|
|
if( cost < min_cost || ( cost == min_cost && len > max_length ) )
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2019-10-25 06:18:16 +00:00
|
|
|
l_best = std::get<2>( vp );
|
|
|
|
p_best = std::get<0>( vp );
|
2013-09-26 21:53:54 +00:00
|
|
|
found = true;
|
|
|
|
|
2019-08-21 02:33:26 +00:00
|
|
|
if( cost <= min_cost )
|
|
|
|
max_length = std::max<int>( len, max_length );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
min_cost = std::min( cost, min_cost );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( found )
|
|
|
|
{
|
|
|
|
aLine->SetShape( l_best );
|
|
|
|
return p_best;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::runSmartPads( LINE* aLine )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
SHAPE_LINE_CHAIN& line = aLine->Line();
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
if( line.PointCount() < 3 )
|
|
|
|
return false;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
VECTOR2I p_start = line.CPoint( 0 ), p_end = line.CPoint( -1 );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
ITEM* startPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_start );
|
|
|
|
ITEM* endPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_end );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
int vtx = -1;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
if( startPad )
|
|
|
|
vtx = smartPadsSingle( aLine, startPad, false, 3 );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
if( endPad )
|
|
|
|
smartPadsSingle( aLine, endPad, true,
|
2014-05-16 11:37:31 +00:00
|
|
|
vtx < 0 ? line.PointCount() - 1 : line.PointCount() - 1 - vtx );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
aLine->Line().Simplify();
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
return true;
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::Optimize( LINE* aLine, int aEffortLevel, NODE* aWorld )
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
OPTIMIZER opt( aWorld );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
opt.SetEffortLevel( aEffortLevel );
|
|
|
|
opt.SetCollisionMask( -1 );
|
|
|
|
return opt.Optimize( aLine );
|
2013-09-18 17:55:16 +00:00
|
|
|
}
|
2014-05-14 13:53:54 +00:00
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::fanoutCleanup( LINE* aLine )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
|
|
|
if( aLine->PointCount() < 3 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
VECTOR2I p_start = aLine->CPoint( 0 ), p_end = aLine->CPoint( -1 );
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
ITEM* startPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_start );
|
|
|
|
ITEM* endPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_end );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
int thr = aLine->Width() * 10;
|
2014-05-14 13:53:54 +00:00
|
|
|
int len = aLine->CLine().Length();
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( !startPad )
|
2014-05-14 13:53:54 +00:00
|
|
|
return false;
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool startMatch = startPad->OfKind( ITEM::VIA_T | ITEM::SOLID_T );
|
2015-02-18 16:53:46 +00:00
|
|
|
bool endMatch = false;
|
2014-05-14 13:53:54 +00:00
|
|
|
|
|
|
|
if(endPad)
|
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
endMatch = endPad->OfKind( ITEM::VIA_T | ITEM::SOLID_T );
|
2015-02-18 16:53:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-14 13:53:54 +00:00
|
|
|
endMatch = aLine->EndsWithVia();
|
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( startMatch && endMatch && len < thr )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
for( int i = 0; i < 2; i++ )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
SHAPE_LINE_CHAIN l2 = DIRECTION_45().BuildInitialTrace( p_start, p_end, i );
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE repl;
|
|
|
|
repl = LINE( *aLine, l2 );
|
2014-05-14 13:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
if( !m_world->CheckColliding( &repl ) )
|
2014-05-14 13:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
aLine->SetShape( repl.CLine() );
|
2014-05-14 13:53:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-16 11:37:31 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
int findCoupledVertices( const VECTOR2I& aVertex, const SEG& aOrigSeg, const SHAPE_LINE_CHAIN& aCoupled, DIFF_PAIR* aPair, int* aIndices )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
int count = 0;
|
2015-02-18 00:29:54 +00:00
|
|
|
for ( int i = 0; i < aCoupled.SegmentCount(); i++ )
|
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
SEG s = aCoupled.CSegment( i );
|
2015-02-18 00:29:54 +00:00
|
|
|
VECTOR2I projOverCoupled = s.LineProject ( aVertex );
|
|
|
|
|
|
|
|
if( s.ApproxParallel ( aOrigSeg ) )
|
|
|
|
{
|
|
|
|
int64_t dist = ( projOverCoupled - aVertex ).EuclideanNorm() - aPair->Width();
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
if( aPair->GapConstraint().Matches( dist ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
*aIndices++ = i;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool verifyDpBypass( NODE* aNode, DIFF_PAIR* aPair, bool aRefIsP, const SHAPE_LINE_CHAIN& aNewRef, const SHAPE_LINE_CHAIN& aNewCoupled )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE refLine ( aRefIsP ? aPair->PLine() : aPair->NLine(), aNewRef );
|
|
|
|
LINE coupledLine ( aRefIsP ? aPair->NLine() : aPair->PLine(), aNewCoupled );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
if( aNode->CheckColliding( &refLine, &coupledLine, ITEM::ANY_T, aPair->Gap() - 10 ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
return false;
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( aNode->CheckColliding ( &refLine ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
return false;
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( aNode->CheckColliding ( &coupledLine ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool coupledBypass( NODE* aNode, DIFF_PAIR* aPair, bool aRefIsP, const SHAPE_LINE_CHAIN& aRef, const SHAPE_LINE_CHAIN& aRefBypass, const SHAPE_LINE_CHAIN& aCoupled, SHAPE_LINE_CHAIN& aNewCoupled )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
int vStartIdx[1024]; // fixme: possible overflow
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
int nStarts = findCoupledVertices( aRefBypass.CPoint( 0 ), aRefBypass.CSegment( 0 ), aCoupled, aPair, vStartIdx );
|
|
|
|
DIRECTION_45 dir( aRefBypass.CSegment( 0 ) );
|
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
int64_t bestLength = -1;
|
|
|
|
bool found = false;
|
|
|
|
SHAPE_LINE_CHAIN bestBypass;
|
|
|
|
int si, ei;
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
for( int i=0; i< nStarts; i++ )
|
|
|
|
{
|
|
|
|
for( int j = 1; j < aCoupled.PointCount() - 1; j++ )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
int delta = std::abs ( vStartIdx[i] - j );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
if( delta > 1 )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
const VECTOR2I& vs = aCoupled.CPoint( vStartIdx[i] );
|
|
|
|
SHAPE_LINE_CHAIN bypass = dir.BuildInitialTrace( vs, aCoupled.CPoint(j), dir.IsDiagonal() );
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
int64_t coupledLength = aPair->CoupledLength( aRef, bypass );
|
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
SHAPE_LINE_CHAIN newCoupled = aCoupled;
|
|
|
|
|
|
|
|
si = vStartIdx[i];
|
|
|
|
ei = j;
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
if(si < ei)
|
|
|
|
newCoupled.Replace( si, ei, bypass );
|
|
|
|
else
|
|
|
|
newCoupled.Replace( ei, si, bypass.Reverse() );
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if(coupledLength > bestLength && verifyDpBypass( aNode, aPair, aRefIsP, aRef, newCoupled) )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
bestBypass = newCoupled;
|
2015-02-18 16:53:46 +00:00
|
|
|
bestLength = coupledLength;
|
2015-02-18 00:29:54 +00:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
}
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( found )
|
2015-02-18 00:29:54 +00:00
|
|
|
aNewCoupled = bestBypass;
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool checkDpColliding( NODE* aNode, DIFF_PAIR* aPair, bool aIsP, const SHAPE_LINE_CHAIN& aPath )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2016-08-29 17:31:13 +00:00
|
|
|
LINE tmp ( aIsP ? aPair->PLine() : aPair->NLine(), aPath );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-07-22 08:46:45 +00:00
|
|
|
return static_cast<bool>( aNode->CheckColliding( &tmp ) );
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::mergeDpStep( DIFF_PAIR* aPair, bool aTryP, int step )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
int n = 1;
|
2015-02-18 16:53:46 +00:00
|
|
|
|
|
|
|
SHAPE_LINE_CHAIN currentPath = aTryP ? aPair->CP() : aPair->CN();
|
|
|
|
SHAPE_LINE_CHAIN coupledPath = aTryP ? aPair->CN() : aPair->CP();
|
2015-02-18 00:29:54 +00:00
|
|
|
|
|
|
|
int n_segs = currentPath.SegmentCount() - 1;
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
int64_t clenPre = aPair->CoupledLength( currentPath, coupledPath );
|
2015-02-18 00:29:54 +00:00
|
|
|
int64_t budget = clenPre / 10; // fixme: come up with somethig more intelligent here...
|
|
|
|
|
|
|
|
while( n < n_segs - step )
|
|
|
|
{
|
|
|
|
const SEG s1 = currentPath.CSegment( n );
|
|
|
|
const SEG s2 = currentPath.CSegment( n + step );
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
DIRECTION_45 dir1( s1 );
|
|
|
|
DIRECTION_45 dir2( s2 );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( dir1.IsObtuse( dir2 ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
SHAPE_LINE_CHAIN bypass = DIRECTION_45().BuildInitialTrace( s1.A, s2.B, dir1.IsDiagonal() );
|
|
|
|
SHAPE_LINE_CHAIN newRef;
|
|
|
|
SHAPE_LINE_CHAIN newCoup;
|
|
|
|
int64_t deltaCoupled = -1, deltaUni = -1;
|
|
|
|
|
|
|
|
newRef = currentPath;
|
|
|
|
newRef.Replace( s1.Index(), s2.Index(), bypass );
|
|
|
|
|
|
|
|
deltaUni = aPair->CoupledLength ( newRef, coupledPath ) - clenPre + budget;
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if ( coupledBypass( m_world, aPair, aTryP, newRef, bypass, coupledPath, newCoup ) )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
deltaCoupled = aPair->CoupledLength( newRef, newCoup ) - clenPre + budget;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( deltaCoupled >= 0 )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
newRef.Simplify();
|
|
|
|
newCoup.Simplify();
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
aPair->SetShape( newRef, newCoup, !aTryP );
|
2015-02-18 00:29:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( deltaUni >= 0 && verifyDpBypass ( m_world, aPair, aTryP, newRef, coupledPath ) )
|
|
|
|
{
|
|
|
|
newRef.Simplify();
|
|
|
|
coupledPath.Simplify();
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
aPair->SetShape( newRef, coupledPath, !aTryP );
|
2015-02-18 00:29:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
return false;
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::mergeDpSegments( DIFF_PAIR* aPair )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
int step_p = aPair->CP().SegmentCount() - 2;
|
|
|
|
int step_n = aPair->CN().SegmentCount() - 2;
|
|
|
|
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
int n_segs_p = aPair->CP().SegmentCount();
|
|
|
|
int n_segs_n = aPair->CN().SegmentCount();
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
int max_step_p = n_segs_p - 2;
|
|
|
|
int max_step_n = n_segs_n - 2;
|
|
|
|
|
|
|
|
if( step_p > max_step_p )
|
|
|
|
step_p = max_step_p;
|
|
|
|
|
|
|
|
if( step_n > max_step_n )
|
|
|
|
step_n = max_step_n;
|
|
|
|
|
|
|
|
if( step_p < 1 && step_n < 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bool found_anything_p = false;
|
|
|
|
bool found_anything_n = false;
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( step_p > 1 )
|
2015-02-18 00:29:54 +00:00
|
|
|
found_anything_p = mergeDpStep( aPair, true, step_p );
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
if( step_n > 1 )
|
2015-02-18 00:29:54 +00:00
|
|
|
found_anything_n = mergeDpStep( aPair, false, step_n );
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2015-02-18 00:29:54 +00:00
|
|
|
if( !found_anything_n && !found_anything_p )
|
|
|
|
{
|
|
|
|
step_n--;
|
|
|
|
step_p--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-02-18 16:53:46 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool OPTIMIZER::Optimize( DIFF_PAIR* aPair )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
2015-02-18 16:53:46 +00:00
|
|
|
return mergeDpSegments( aPair );
|
|
|
|
}
|
2016-08-29 14:38:11 +00:00
|
|
|
|
|
|
|
}
|