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
|
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
|
|
|
#ifndef __PNS_OPTIMIZER_H
|
|
|
|
#define __PNS_OPTIMIZER_H
|
|
|
|
|
|
|
|
#include <boost/unordered_map.hpp>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
|
|
|
#include <geometry/shape_index_list.h>
|
|
|
|
#include <geometry/shape_line_chain.h>
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
#include "range.h"
|
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
class PNS_NODE;
|
|
|
|
class PNS_LINE;
|
|
|
|
class PNS_ROUTER;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PNS_COST_ESTIMATOR
|
|
|
|
*
|
|
|
|
* Calculates the cost of a given line, taking corner angles and total length into account.
|
2013-09-26 21:53:54 +00:00
|
|
|
**/
|
|
|
|
class PNS_COST_ESTIMATOR
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
public:
|
|
|
|
PNS_COST_ESTIMATOR() :
|
|
|
|
m_lengthCost( 0 ),
|
|
|
|
m_cornerCost( 0 )
|
2014-05-16 11:37:31 +00:00
|
|
|
{}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
PNS_COST_ESTIMATOR( const PNS_COST_ESTIMATOR& aB ) :
|
|
|
|
m_lengthCost( aB.m_lengthCost ),
|
|
|
|
m_cornerCost( aB.m_cornerCost )
|
|
|
|
{}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
~PNS_COST_ESTIMATOR() {};
|
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
static int CornerCost( const SEG& aA, const SEG& aB );
|
2013-09-26 21:53:54 +00:00
|
|
|
static int CornerCost( const SHAPE_LINE_CHAIN& aLine );
|
|
|
|
static int CornerCost( const PNS_LINE& aLine );
|
|
|
|
|
|
|
|
void Add( PNS_LINE& aLine );
|
|
|
|
void Remove( PNS_LINE& aLine );
|
|
|
|
void Replace( PNS_LINE& aOldLine, PNS_LINE& aNewLine );
|
|
|
|
|
|
|
|
bool IsBetter( PNS_COST_ESTIMATOR& aOther, double aLengthTollerance,
|
2014-05-16 11:37:31 +00:00
|
|
|
double aCornerTollerace ) const;
|
2013-09-26 21:53:54 +00:00
|
|
|
|
|
|
|
double GetLengthCost() const { return m_lengthCost; }
|
|
|
|
double GetCornerCost() const { return m_cornerCost; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
double m_lengthCost;
|
|
|
|
int m_cornerCost;
|
2013-09-18 17:55:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class PNS_OPTIMIZER
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2013-09-18 17:55:16 +00:00
|
|
|
* Performs various optimizations of the lines being routed, attempting to make the lines shorter
|
|
|
|
* and less cornery. There are 3 kinds of optimizations so far:
|
2013-09-26 21:53:54 +00:00
|
|
|
* - Merging obtuse segments (MERGE_OBTUSE): tries to join together as many
|
2013-09-18 17:55:16 +00:00
|
|
|
* obtuse segments as possible without causing collisions
|
|
|
|
* - Rerouting path between pair of line corners with a 2-segment "\__" line and iteratively repeating
|
|
|
|
* the procedure as long as the total cost of the line keeps decreasing
|
2013-09-26 21:53:54 +00:00
|
|
|
* - "Smart Pads" - that is, rerouting pad/via exits to make them look nice (SMART_PADS).
|
2013-09-18 17:55:16 +00:00
|
|
|
**/
|
2013-09-26 21:53:54 +00:00
|
|
|
class PNS_OPTIMIZER
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
public:
|
|
|
|
enum OptimizationEffort
|
|
|
|
{
|
|
|
|
MERGE_SEGMENTS = 0x01,
|
|
|
|
SMART_PADS = 0x02,
|
2014-05-14 13:53:54 +00:00
|
|
|
MERGE_OBTUSE = 0x04,
|
|
|
|
FANOUT_CLEANUP = 0x08
|
2013-09-26 21:53:54 +00:00
|
|
|
};
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
PNS_OPTIMIZER( PNS_NODE* aWorld );
|
|
|
|
~PNS_OPTIMIZER();
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
///> a quick shortcut to optmize a line without creating and setting up an optimizer
|
2014-05-14 13:53:54 +00:00
|
|
|
static bool Optimize( PNS_LINE* aLine, int aEffortLevel, PNS_NODE* aWorld);
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
bool Optimize( PNS_LINE* aLine, PNS_LINE* aResult = NULL );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
void SetWorld( PNS_NODE* aNode ) { m_world = aNode; }
|
|
|
|
void CacheStaticItem( PNS_ITEM* aItem );
|
|
|
|
void CacheRemove( PNS_ITEM* aItem );
|
|
|
|
void ClearCache( bool aStaticOnly = false );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
void SetCollisionMask( int aMask )
|
|
|
|
{
|
|
|
|
m_collisionKindMask = aMask;
|
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
void SetEffortLevel( int aEffort )
|
|
|
|
{
|
|
|
|
m_effortLevel = aEffort;
|
|
|
|
}
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
private:
|
|
|
|
static const int MaxCachedItems = 256;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
typedef std::vector<SHAPE_LINE_CHAIN> BREAKOUT_LIST;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
struct CACHE_VISITOR;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
struct CACHED_ITEM
|
2013-09-26 21:53:54 +00:00
|
|
|
{
|
2014-05-16 11:37:31 +00:00
|
|
|
int m_hits;
|
|
|
|
bool m_isStatic;
|
2013-09-26 21:53:54 +00:00
|
|
|
};
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
bool mergeObtuse( PNS_LINE* aLine );
|
|
|
|
bool mergeFull( PNS_LINE* aLine );
|
|
|
|
bool removeUglyCorners( PNS_LINE* aLine );
|
|
|
|
bool runSmartPads( PNS_LINE* aLine );
|
|
|
|
bool mergeStep( PNS_LINE* aLine, SHAPE_LINE_CHAIN& aCurrentLine, int step );
|
2014-05-14 13:53:54 +00:00
|
|
|
bool fanoutCleanup( PNS_LINE * aLine );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
bool checkColliding( PNS_ITEM* aItem, bool aUpdateCache = true );
|
|
|
|
bool checkColliding( PNS_LINE* aLine, const SHAPE_LINE_CHAIN& aOptPath );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
void cacheAdd( PNS_ITEM* aItem, bool aIsStatic );
|
|
|
|
void removeCachedSegments( PNS_LINE* aLine, int aStartVertex = 0, int aEndVertex = -1 );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
BREAKOUT_LIST circleBreakouts( int aWidth, const SHAPE* aShape, bool aPermitDiagonal ) const;
|
|
|
|
BREAKOUT_LIST rectBreakouts( int aWidth, const SHAPE* aShape, bool aPermitDiagonal ) const;
|
|
|
|
BREAKOUT_LIST ovalBreakouts( int aWidth, const SHAPE* aShape, bool aPermitDiagonal ) const;
|
|
|
|
BREAKOUT_LIST computeBreakouts( int aWidth, const PNS_ITEM* aItem, bool aPermitDiagonal ) const;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
int smartPadsSingle( PNS_LINE* aLine, PNS_ITEM* aPad, bool aEnd, int aEndVertex );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
PNS_ITEM* findPadOrVia( int aLayer, int aNet, const VECTOR2I& aP ) const;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2013-09-26 21:53:54 +00:00
|
|
|
SHAPE_INDEX_LIST<PNS_ITEM*> m_cache;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2014-05-16 11:37:31 +00:00
|
|
|
typedef boost::unordered_map<PNS_ITEM*, CACHED_ITEM> CachedItemTags;
|
2013-09-26 21:53:54 +00:00
|
|
|
CachedItemTags m_cacheTags;
|
|
|
|
PNS_NODE* m_world;
|
|
|
|
int m_collisionKindMask;
|
|
|
|
int m_effortLevel;
|
|
|
|
bool m_keepPostures;
|
2013-09-18 17:55:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|