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
|
2021-01-27 22:15:38 +00:00
|
|
|
* Copyright (C) 2016-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
|
2017-11-01 11:14:16 +00:00
|
|
|
#include <unordered_map>
|
2016-06-29 15:09:55 +00:00
|
|
|
#include <memory>
|
2013-09-18 17:55:16 +00:00
|
|
|
|
|
|
|
#include <geometry/shape_index_list.h>
|
|
|
|
#include <geometry/shape_line_chain.h>
|
|
|
|
|
2014-05-14 13:53:54 +00:00
|
|
|
#include "range.h"
|
|
|
|
|
2020-02-05 15:33:14 +00:00
|
|
|
|
2016-08-29 14:38:11 +00:00
|
|
|
namespace PNS {
|
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
class NODE;
|
|
|
|
class ROUTER;
|
|
|
|
class LINE;
|
|
|
|
class DIFF_PAIR;
|
2020-02-19 17:40:51 +00:00
|
|
|
class ITEM;
|
|
|
|
class JOINT;
|
2021-01-27 22:15:38 +00:00
|
|
|
class OPT_CONSTRAINT;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
|
|
|
/**
|
2021-01-27 22:15:38 +00:00
|
|
|
* Calculate the cost of a given line, taking corner angles and total length into account.
|
|
|
|
*/
|
2016-08-29 17:31:13 +00:00
|
|
|
class COST_ESTIMATOR
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
public:
|
2016-08-29 17:31:13 +00:00
|
|
|
COST_ESTIMATOR() :
|
2013-09-26 21:53:54 +00:00
|
|
|
m_lengthCost( 0 ),
|
|
|
|
m_cornerCost( 0 )
|
2014-05-16 11:37:31 +00:00
|
|
|
{}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
COST_ESTIMATOR( const COST_ESTIMATOR& aB ) :
|
2014-05-16 11:37:31 +00:00
|
|
|
m_lengthCost( aB.m_lengthCost ),
|
|
|
|
m_cornerCost( aB.m_cornerCost )
|
|
|
|
{}
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
~COST_ESTIMATOR() {};
|
2013-09-26 21:53:54 +00:00
|
|
|
|
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 );
|
2016-08-29 17:31:13 +00:00
|
|
|
static int CornerCost( const LINE& aLine );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2020-11-18 19:50:36 +00:00
|
|
|
void Add( const LINE& aLine );
|
|
|
|
void Remove( const LINE& aLine );
|
|
|
|
void Replace( const LINE& aOldLine, const LINE& aNewLine );
|
2013-09-26 21:53:54 +00:00
|
|
|
|
2020-11-18 19:50:36 +00:00
|
|
|
bool IsBetter( const COST_ESTIMATOR& aOther, double aLengthTolerance,
|
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
|
|
|
};
|
|
|
|
|
2020-02-19 17:40:51 +00:00
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
/**
|
2021-01-27 22:15:38 +00:00
|
|
|
* Perform various optimizations of the lines being routed, attempting to make the lines shorter
|
|
|
|
* and less cornery.
|
2013-09-26 21:53:54 +00:00
|
|
|
*
|
2021-01-27 22:15:38 +00:00
|
|
|
* There are 3 kinds of optimizations so far:
|
|
|
|
* - Merging obtuse segments (MERGE_OBTUSE): tries to join together as many 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).
|
2021-01-27 22:15:38 +00:00
|
|
|
*/
|
2016-08-29 17:31:13 +00:00
|
|
|
class OPTIMIZER
|
2013-09-18 17:55:16 +00:00
|
|
|
{
|
2013-09-26 21:53:54 +00:00
|
|
|
public:
|
|
|
|
enum OptimizationEffort
|
|
|
|
{
|
2020-12-30 02:14:14 +00:00
|
|
|
MERGE_SEGMENTS = 0x01, ///< Reduce corner cost iteratively
|
|
|
|
SMART_PADS = 0x02, ///< Reroute pad exits
|
|
|
|
MERGE_OBTUSE = 0x04, ///< Reduce corner cost by merging obtuse segments
|
|
|
|
FANOUT_CLEANUP = 0x08, ///< Simplify pad-pad and pad-via connections if possible
|
2021-02-25 16:18:13 +00:00
|
|
|
KEEP_TOPOLOGY = 0x10,
|
|
|
|
PRESERVE_VERTEX = 0x20,
|
2020-12-30 02:14:14 +00:00
|
|
|
RESTRICT_VERTEX_RANGE = 0x40,
|
2020-10-18 20:58:48 +00:00
|
|
|
MERGE_COLINEAR = 0x80, ///< Merge co-linear segments
|
2021-02-25 16:18:13 +00:00
|
|
|
RESTRICT_AREA = 0x100
|
2013-09-26 21:53:54 +00:00
|
|
|
};
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
OPTIMIZER( NODE* aWorld );
|
|
|
|
~OPTIMIZER();
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
///< A quick shortcut to optimize a line without creating and setting up an optimizer.
|
2021-01-04 22:31:57 +00:00
|
|
|
static bool Optimize( LINE* aLine, int aEffortLevel, NODE* aWorld,
|
|
|
|
const VECTOR2I aV = VECTOR2I(0, 0) );
|
2021-01-27 22:15:38 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool Optimize( LINE* aLine, LINE* aResult = NULL );
|
|
|
|
bool Optimize( DIFF_PAIR* aPair );
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void SetWorld( NODE* aNode ) { m_world = aNode; }
|
|
|
|
void CacheRemove( ITEM* aItem );
|
2013-09-26 21:53:54 +00:00
|
|
|
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
|
|
|
|
2020-10-08 22:00:12 +00:00
|
|
|
void SetPreserveVertex( const VECTOR2I& aV )
|
|
|
|
{
|
|
|
|
m_preservedVertex = aV;
|
|
|
|
m_effortLevel |= OPTIMIZER::PRESERVE_VERTEX;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetRestrictVertexRange( int aStart, int aEnd )
|
|
|
|
{
|
|
|
|
m_restrictedVertexRange.first = aStart;
|
|
|
|
m_restrictedVertexRange.second = aEnd;
|
|
|
|
m_effortLevel |= OPTIMIZER::RESTRICT_VERTEX_RANGE;
|
|
|
|
}
|
|
|
|
|
2021-02-23 21:51:11 +00:00
|
|
|
void SetRestrictArea( const BOX2I& aArea, bool aStrict = true )
|
2015-02-18 00:29:54 +00:00
|
|
|
{
|
|
|
|
m_restrictArea = aArea;
|
2021-02-23 21:51:11 +00:00
|
|
|
m_restrictAreaIsStrict = aStrict;
|
2015-02-18 00:29:54 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 17:40:51 +00:00
|
|
|
void ClearConstraints();
|
|
|
|
void AddConstraint ( OPT_CONSTRAINT *aConstraint );
|
|
|
|
|
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
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool mergeObtuse( LINE* aLine );
|
|
|
|
bool mergeFull( LINE* aLine );
|
2020-12-30 02:14:14 +00:00
|
|
|
bool mergeColinear( LINE* aLine );
|
2016-08-29 17:31:13 +00:00
|
|
|
bool runSmartPads( LINE* aLine );
|
|
|
|
bool mergeStep( LINE* aLine, SHAPE_LINE_CHAIN& aCurrentLine, int step );
|
|
|
|
bool fanoutCleanup( LINE * aLine );
|
|
|
|
bool mergeDpSegments( DIFF_PAIR *aPair );
|
|
|
|
bool mergeDpStep( DIFF_PAIR *aPair, bool aTryP, int step );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
bool checkColliding( ITEM* aItem, bool aUpdateCache = true );
|
|
|
|
bool checkColliding( LINE* aLine, const SHAPE_LINE_CHAIN& aOptPath );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
void cacheAdd( ITEM* aItem, bool aIsStatic );
|
|
|
|
void removeCachedSegments( LINE* aLine, int aStartVertex = 0, int aEndVertex = -1 );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2021-01-04 22:31:57 +00:00
|
|
|
bool checkConstraints( int aVertex1, int aVertex2, LINE* aOriginLine,
|
|
|
|
const SHAPE_LINE_CHAIN& aCurrentPath,
|
|
|
|
const SHAPE_LINE_CHAIN& aReplacement );
|
2020-02-19 17:40:51 +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;
|
2018-05-03 22:46:42 +00:00
|
|
|
BREAKOUT_LIST customBreakouts( int aWidth, const ITEM* aItem, bool aPermitDiagonal ) const;
|
2016-08-29 17:31:13 +00:00
|
|
|
BREAKOUT_LIST computeBreakouts( int aWidth, const ITEM* aItem, bool aPermitDiagonal ) const;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
int smartPadsSingle( LINE* aLine, ITEM* aPad, bool aEnd, int aEndVertex );
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2016-08-29 17:31:13 +00:00
|
|
|
ITEM* findPadOrVia( int aLayer, int aNet, const VECTOR2I& aP ) const;
|
2013-09-18 17:55:16 +00:00
|
|
|
|
2021-01-04 22:31:57 +00:00
|
|
|
private:
|
2021-02-25 16:18:13 +00:00
|
|
|
SHAPE_INDEX_LIST<ITEM*> m_cache;
|
2021-01-04 22:31:57 +00:00
|
|
|
std::vector<OPT_CONSTRAINT*> m_constraints;
|
|
|
|
std::unordered_map<ITEM*, CACHED_ITEM> m_cacheTags;
|
2015-02-18 00:29:54 +00:00
|
|
|
|
2021-02-25 16:18:13 +00:00
|
|
|
NODE* m_world;
|
|
|
|
int m_collisionKindMask;
|
|
|
|
int m_effortLevel;
|
2020-10-08 22:00:12 +00:00
|
|
|
|
2021-02-25 16:18:13 +00:00
|
|
|
VECTOR2I m_preservedVertex;
|
2020-10-08 22:00:12 +00:00
|
|
|
std::pair<int, int> m_restrictedVertexRange;
|
2021-02-25 16:18:13 +00:00
|
|
|
BOX2I m_restrictArea;
|
|
|
|
bool m_restrictAreaIsStrict;
|
2013-09-18 17:55:16 +00:00
|
|
|
};
|
|
|
|
|
2021-01-27 22:15:38 +00:00
|
|
|
|
2020-02-19 17:40:51 +00:00
|
|
|
class OPT_CONSTRAINT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OPT_CONSTRAINT( NODE* aWorld ) :
|
|
|
|
m_world( aWorld )
|
2021-02-25 16:18:13 +00:00
|
|
|
{
|
|
|
|
m_priority = 0;
|
|
|
|
};
|
2020-02-19 17:40:51 +00:00
|
|
|
|
2021-01-04 22:31:57 +00:00
|
|
|
virtual ~OPT_CONSTRAINT()
|
2020-02-19 17:40:51 +00:00
|
|
|
{
|
2021-01-04 22:31:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual bool Check( int aVertex1, int aVertex2, const LINE* aOriginLine,
|
|
|
|
const SHAPE_LINE_CHAIN& aCurrentPath,
|
|
|
|
const SHAPE_LINE_CHAIN& aReplacement ) = 0;
|
|
|
|
|
|
|
|
int GetPriority() const { return m_priority; }
|
|
|
|
void SetPriority( int aPriority ) { m_priority = aPriority; }
|
2020-02-19 17:40:51 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
NODE* m_world;
|
2021-02-25 16:18:13 +00:00
|
|
|
int m_priority;
|
2020-02-19 17:40:51 +00:00
|
|
|
};
|
|
|
|
|
2020-10-18 20:58:48 +00:00
|
|
|
class ANGLE_CONSTRAINT_45: public OPT_CONSTRAINT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ANGLE_CONSTRAINT_45( NODE* aWorld, int aEntryDirectionMask = -1, int aExitDirectionMask = -1 ) :
|
|
|
|
OPT_CONSTRAINT( aWorld ),
|
|
|
|
m_entryDirectionMask( aEntryDirectionMask ),
|
|
|
|
m_exitDirectionMask( aExitDirectionMask )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~ANGLE_CONSTRAINT_45() {};
|
|
|
|
|
2020-10-18 20:59:16 +00:00
|
|
|
virtual bool Check ( int aVertex1, int aVertex2, const LINE* aOriginLine, const SHAPE_LINE_CHAIN& aCurrentPath, const SHAPE_LINE_CHAIN& aReplacement ) override;
|
2020-10-18 20:58:48 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int m_entryDirectionMask;
|
|
|
|
int m_exitDirectionMask;
|
|
|
|
};
|
2020-02-19 17:40:51 +00:00
|
|
|
|
|
|
|
class AREA_CONSTRAINT : public OPT_CONSTRAINT
|
|
|
|
{
|
|
|
|
public:
|
2021-02-23 21:51:11 +00:00
|
|
|
AREA_CONSTRAINT( NODE* aWorld, const BOX2I& aAllowedArea, bool aAllowedAreaStrict ) :
|
2020-02-19 17:40:51 +00:00
|
|
|
OPT_CONSTRAINT( aWorld ),
|
2021-02-23 21:51:11 +00:00
|
|
|
m_allowedArea ( aAllowedArea ),
|
|
|
|
m_allowedAreaStrict ( aAllowedAreaStrict )
|
2021-01-04 22:31:57 +00:00
|
|
|
{
|
|
|
|
};
|
2020-02-19 17:40:51 +00:00
|
|
|
|
2021-01-04 22:31:57 +00:00
|
|
|
bool Check( int aVertex1, int aVertex2, const LINE* aOriginLine,
|
|
|
|
const SHAPE_LINE_CHAIN& aCurrentPath,
|
|
|
|
const SHAPE_LINE_CHAIN& aReplacement ) override;
|
2020-02-19 17:40:51 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
BOX2I m_allowedArea;
|
2021-02-23 21:51:11 +00:00
|
|
|
bool m_allowedAreaStrict;
|
2020-02-19 17:40:51 +00:00
|
|
|
};
|
|
|
|
|
2021-01-04 22:31:57 +00:00
|
|
|
|
2020-02-19 17:40:51 +00:00
|
|
|
class KEEP_TOPOLOGY_CONSTRAINT: public OPT_CONSTRAINT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
KEEP_TOPOLOGY_CONSTRAINT( NODE* aWorld ) :
|
|
|
|
OPT_CONSTRAINT( aWorld )
|
2021-01-04 22:31:57 +00:00
|
|
|
{
|
|
|
|
};
|
2020-02-19 17:40:51 +00:00
|
|
|
|
2021-01-04 22:31:57 +00:00
|
|
|
bool Check( int aVertex1, int aVertex2, const LINE* aOriginLine,
|
|
|
|
const SHAPE_LINE_CHAIN& aCurrentPath,
|
|
|
|
const SHAPE_LINE_CHAIN& aReplacement ) override;
|
2020-02-19 17:40:51 +00:00
|
|
|
};
|
|
|
|
|
2021-01-04 22:31:57 +00:00
|
|
|
|
2020-02-19 17:40:51 +00:00
|
|
|
class PRESERVE_VERTEX_CONSTRAINT: public OPT_CONSTRAINT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PRESERVE_VERTEX_CONSTRAINT( NODE* aWorld, const VECTOR2I& aV ) :
|
|
|
|
OPT_CONSTRAINT( aWorld ),
|
|
|
|
m_v( aV )
|
2021-01-04 22:31:57 +00:00
|
|
|
{
|
|
|
|
};
|
2020-02-19 17:40:51 +00:00
|
|
|
|
2021-01-04 22:31:57 +00:00
|
|
|
bool Check( int aVertex1, int aVertex2, const LINE* aOriginLine,
|
|
|
|
const SHAPE_LINE_CHAIN& aCurrentPath,
|
|
|
|
const SHAPE_LINE_CHAIN& aReplacement ) override;
|
2020-02-19 17:40:51 +00:00
|
|
|
private:
|
|
|
|
VECTOR2I m_v;
|
|
|
|
};
|
|
|
|
|
2021-01-04 22:31:57 +00:00
|
|
|
|
2020-10-08 22:00:12 +00:00
|
|
|
class RESTRICT_VERTEX_RANGE_CONSTRAINT: public OPT_CONSTRAINT
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RESTRICT_VERTEX_RANGE_CONSTRAINT( NODE* aWorld, int aStart, int aEnd ) :
|
|
|
|
OPT_CONSTRAINT( aWorld ),
|
|
|
|
m_start( aStart ),
|
|
|
|
m_end( aEnd )
|
2021-01-04 22:31:57 +00:00
|
|
|
{
|
|
|
|
};
|
2020-10-08 22:00:12 +00:00
|
|
|
|
2021-01-04 22:31:57 +00:00
|
|
|
virtual bool Check( int aVertex1, int aVertex2, const LINE* aOriginLine,
|
|
|
|
const SHAPE_LINE_CHAIN& aCurrentPath,
|
|
|
|
const SHAPE_LINE_CHAIN& aReplacement ) override;
|
2020-10-08 22:00:12 +00:00
|
|
|
private:
|
|
|
|
int m_start;
|
|
|
|
int m_end;
|
|
|
|
};
|
|
|
|
|
2020-02-19 17:40:51 +00:00
|
|
|
|
|
|
|
};
|
2016-08-29 14:38:11 +00:00
|
|
|
|
2020-11-18 19:50:36 +00:00
|
|
|
#endif // __PNS_OPTIMIZER_H
|