KiCad PCB EDA Suite
PNS::OPTIMIZER Class Reference

Perform various optimizations of the lines being routed, attempting to make the lines shorter and less cornery. More...

#include <pns_optimizer.h>

Classes

struct  CACHE_VISITOR
 
struct  CACHED_ITEM
 

Public Types

enum  OptimizationEffort {
  MERGE_SEGMENTS = 0x01 , SMART_PADS = 0x02 , MERGE_OBTUSE = 0x04 , FANOUT_CLEANUP = 0x08 ,
  KEEP_TOPOLOGY = 0x10 , PRESERVE_VERTEX = 0x20 , RESTRICT_VERTEX_RANGE = 0x40 , MERGE_COLINEAR = 0x80 ,
  RESTRICT_AREA = 0x100 , LIMIT_CORNER_COUNT = 0x200
}
 

Public Member Functions

 OPTIMIZER (NODE *aWorld)
 
 ~OPTIMIZER ()
 A quick shortcut to optimize a line without creating and setting up an optimizer. More...
 
bool Optimize (LINE *aLine, LINE *aResult=nullptr, LINE *aRoot=nullptr)
 
bool Optimize (DIFF_PAIR *aPair)
 
void SetWorld (NODE *aNode)
 
void CacheRemove (ITEM *aItem)
 
void ClearCache (bool aStaticOnly=false)
 
void SetCollisionMask (int aMask)
 
void SetEffortLevel (int aEffort)
 
void SetPreserveVertex (const VECTOR2I &aV)
 
void SetRestrictVertexRange (int aStart, int aEnd)
 
void SetRestrictArea (const BOX2I &aArea, bool aStrict=true)
 
void ClearConstraints ()
 
void AddConstraint (OPT_CONSTRAINT *aConstraint)
 

Static Public Member Functions

static bool Optimize (LINE *aLine, int aEffortLevel, NODE *aWorld, const VECTOR2I &aV=VECTOR2I(0, 0))
 

Private Types

typedef std::vector< SHAPE_LINE_CHAINBREAKOUT_LIST
 

Private Member Functions

bool mergeObtuse (LINE *aLine)
 
bool mergeFull (LINE *aLine)
 
bool mergeColinear (LINE *aLine)
 
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)
 
bool checkColliding (ITEM *aItem, bool aUpdateCache=true)
 
bool checkColliding (LINE *aLine, const SHAPE_LINE_CHAIN &aOptPath)
 
void cacheAdd (ITEM *aItem, bool aIsStatic)
 
void removeCachedSegments (LINE *aLine, int aStartVertex=0, int aEndVertex=-1)
 
bool checkConstraints (int aVertex1, int aVertex2, LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement)
 
BREAKOUT_LIST circleBreakouts (int aWidth, const SHAPE *aShape, bool aPermitDiagonal) const
 
BREAKOUT_LIST rectBreakouts (int aWidth, const SHAPE *aShape, bool aPermitDiagonal) const
 
BREAKOUT_LIST customBreakouts (int aWidth, const ITEM *aItem, bool aPermitDiagonal) const
 
BREAKOUT_LIST computeBreakouts (int aWidth, const ITEM *aItem, bool aPermitDiagonal) const
 
int smartPadsSingle (LINE *aLine, ITEM *aPad, bool aEnd, int aEndVertex)
 
ITEMfindPadOrVia (int aLayer, int aNet, const VECTOR2I &aP) const
 

Private Attributes

SHAPE_INDEX_LIST< ITEM * > m_cache
 
std::vector< OPT_CONSTRAINT * > m_constraints
 
std::unordered_map< ITEM *, CACHED_ITEMm_cacheTags
 
NODEm_world
 
int m_collisionKindMask
 
int m_effortLevel
 
VECTOR2I m_preservedVertex
 
std::pair< int, int > m_restrictedVertexRange
 
BOX2I m_restrictArea
 
bool m_restrictAreaIsStrict
 

Static Private Attributes

static const int MaxCachedItems = 256
 

Detailed Description

Perform various optimizations of the lines being routed, attempting to make the lines shorter and less cornery.

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.
  • "Smart Pads" - that is, rerouting pad/via exits to make them look nice (SMART_PADS).

Definition at line 94 of file pns_optimizer.h.

Member Typedef Documentation

◆ BREAKOUT_LIST

typedef std::vector<SHAPE_LINE_CHAIN> PNS::OPTIMIZER::BREAKOUT_LIST
private

Definition at line 162 of file pns_optimizer.h.

Member Enumeration Documentation

◆ OptimizationEffort

Enumerator
MERGE_SEGMENTS 

Reduce corner cost iteratively.

SMART_PADS 

Reroute pad exits.

MERGE_OBTUSE 

Reduce corner cost by merging obtuse segments.

FANOUT_CLEANUP 

Simplify pad-pad and pad-via connections if possible.

KEEP_TOPOLOGY 
PRESERVE_VERTEX 
RESTRICT_VERTEX_RANGE 
MERGE_COLINEAR 

Merge co-linear segments.

RESTRICT_AREA 
LIMIT_CORNER_COUNT 

Do not attempt to optimize if the resulting line's corner count is outside the predefined range.

Definition at line 97 of file pns_optimizer.h.

98 {
99 MERGE_SEGMENTS = 0x01,
100 SMART_PADS = 0x02,
101 MERGE_OBTUSE = 0x04,
102 FANOUT_CLEANUP = 0x08,
103 KEEP_TOPOLOGY = 0x10,
104 PRESERVE_VERTEX = 0x20,
106 MERGE_COLINEAR = 0x80,
107 RESTRICT_AREA = 0x100,
108 LIMIT_CORNER_COUNT = 0x200
110 };
@ LIMIT_CORNER_COUNT
Do not attempt to optimize if the resulting line's corner count is outside the predefined range.
@ SMART_PADS
Reroute pad exits.
@ FANOUT_CLEANUP
Simplify pad-pad and pad-via connections if possible.
@ MERGE_SEGMENTS
Reduce corner cost iteratively.
Definition: pns_optimizer.h:99
@ MERGE_COLINEAR
Merge co-linear segments.
@ MERGE_OBTUSE
Reduce corner cost by merging obtuse segments.

Constructor & Destructor Documentation

◆ OPTIMIZER()

PNS::OPTIMIZER::OPTIMIZER ( NODE aWorld)

Definition at line 112 of file pns_optimizer.cpp.

112 :
113 m_world( aWorld ),
117{
118}
bool m_restrictAreaIsStrict

◆ ~OPTIMIZER()

PNS::OPTIMIZER::~OPTIMIZER ( )

A quick shortcut to optimize a line without creating and setting up an optimizer.

Definition at line 121 of file pns_optimizer.cpp.

122{
123}

Member Function Documentation

◆ AddConstraint()

void PNS::OPTIMIZER::AddConstraint ( OPT_CONSTRAINT aConstraint)

Definition at line 426 of file pns_optimizer.cpp.

427{
428 m_constraints.push_back( aConstraint );
429}
std::vector< OPT_CONSTRAINT * > m_constraints

References m_constraints.

Referenced by Optimize().

◆ cacheAdd()

void PNS::OPTIMIZER::cacheAdd ( ITEM aItem,
bool  aIsStatic = false 
)
private

Definition at line 154 of file pns_optimizer.cpp.

155{
156 if( m_cacheTags.find( aItem ) != m_cacheTags.end() )
157 return;
158
159 m_cache.Add( aItem );
160 m_cacheTags[aItem].m_hits = 1;
161 m_cacheTags[aItem].m_isStatic = aIsStatic;
162}
std::unordered_map< ITEM *, CACHED_ITEM > m_cacheTags
SHAPE_INDEX_LIST< ITEM * > m_cache

References m_cache, and m_cacheTags.

◆ CacheRemove()

void PNS::OPTIMIZER::CacheRemove ( ITEM aItem)

Definition at line 184 of file pns_optimizer.cpp.

185{
186 if( aItem->Kind() == ITEM::LINE_T )
187 removeCachedSegments( static_cast<LINE*>( aItem ) );
188}
@ LINE_T
Definition: pns_item.h:64
void removeCachedSegments(LINE *aLine, int aStartVertex=0, int aEndVertex=-1)

References PNS::ITEM::Kind(), PNS::ITEM::LINE_T, and removeCachedSegments().

◆ checkColliding() [1/2]

bool PNS::OPTIMIZER::checkColliding ( ITEM aItem,
bool  aUpdateCache = true 
)
private

Definition at line 409 of file pns_optimizer.cpp.

410{
411 CACHE_VISITOR v( aItem, m_world, m_collisionKindMask );
412
413 return static_cast<bool>( m_world->CheckColliding( aItem ) );
414}
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Check if the item collides with anything else in the world, and if found, returns the obstacle.
Definition: pns_node.cpp:468

References PNS::NODE::CheckColliding(), m_collisionKindMask, and m_world.

Referenced by checkColliding(), mergeObtuse(), mergeStep(), and smartPadsSingle().

◆ checkColliding() [2/2]

bool PNS::OPTIMIZER::checkColliding ( LINE aLine,
const SHAPE_LINE_CHAIN aOptPath 
)
private

Definition at line 446 of file pns_optimizer.cpp.

447{
448 LINE tmp( *aLine, aOptPath );
449
450 return checkColliding( &tmp );
451}
bool checkColliding(ITEM *aItem, bool aUpdateCache=true)

References checkColliding().

◆ checkConstraints()

bool PNS::OPTIMIZER::checkConstraints ( int  aVertex1,
int  aVertex2,
LINE aOriginLine,
const SHAPE_LINE_CHAIN aCurrentPath,
const SHAPE_LINE_CHAIN aReplacement 
)
private

Definition at line 432 of file pns_optimizer.cpp.

435{
436 for( OPT_CONSTRAINT* c : m_constraints )
437 {
438 if( !c->Check( aVertex1, aVertex2, aOriginLine, aCurrentPath, aReplacement ) )
439 return false;
440 }
441
442 return true;
443}

References m_constraints.

Referenced by mergeStep().

◆ circleBreakouts()

OPTIMIZER::BREAKOUT_LIST PNS::OPTIMIZER::circleBreakouts ( int  aWidth,
const SHAPE aShape,
bool  aPermitDiagonal 
) const
private

Definition at line 748 of file pns_optimizer.cpp.

750{
751 BREAKOUT_LIST breakouts;
752
754 {
755 const SHAPE_CIRCLE* cir = static_cast<const SHAPE_CIRCLE*>( aShape );
757 VECTOR2I p0 = cir->GetCenter();
758 VECTOR2I v0( cir->GetRadius() * M_SQRT2, 0 );
759
760 RotatePoint( v0, -angle );
761
762 l.Append( p0 );
763 l.Append( p0 + v0 );
764 breakouts.push_back( l );
765 }
766
767 return breakouts;
768}
std::vector< SHAPE_LINE_CHAIN > BREAKOUT_LIST
int GetRadius() const
Definition: shape_circle.h:108
const VECTOR2I GetCenter() const
Definition: shape_circle.h:113
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
static constexpr EDA_ANGLE & ANGLE_360
Definition: eda_angle.h:429
static constexpr EDA_ANGLE & ANGLE_45
Definition: eda_angle.h:424
static constexpr EDA_ANGLE & ANGLE_0
Definition: eda_angle.h:423
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Definition: trigo.cpp:183

References PNS::angle(), ANGLE_0, ANGLE_360, ANGLE_45, SHAPE_LINE_CHAIN::Append(), SHAPE_CIRCLE::GetCenter(), SHAPE_CIRCLE::GetRadius(), and RotatePoint().

Referenced by computeBreakouts().

◆ ClearCache()

void PNS::OPTIMIZER::ClearCache ( bool  aStaticOnly = false)

Definition at line 191 of file pns_optimizer.cpp.

192{
193 if( !aStaticOnly )
194 {
195 m_cacheTags.clear();
196 m_cache.Clear();
197 return;
198 }
199
200 for( auto i = m_cacheTags.begin(); i!= m_cacheTags.end(); ++i )
201 {
202 if( i->second.m_isStatic )
203 {
204 m_cache.Remove( i->first );
205 m_cacheTags.erase( i->first );
206 }
207 }
208}

References m_cache, and m_cacheTags.

◆ ClearConstraints()

void PNS::OPTIMIZER::ClearConstraints ( )

Definition at line 417 of file pns_optimizer.cpp.

418{
419 for( OPT_CONSTRAINT* c : m_constraints )
420 delete c;
421
422 m_constraints.clear();
423}

References m_constraints.

◆ computeBreakouts()

OPTIMIZER::BREAKOUT_LIST PNS::OPTIMIZER::computeBreakouts ( int  aWidth,
const ITEM aItem,
bool  aPermitDiagonal 
) const
private

Definition at line 874 of file pns_optimizer.cpp.

876{
877 switch( aItem->Kind() )
878 {
879 case ITEM::VIA_T:
880 {
881 const VIA* via = static_cast<const VIA*>( aItem );
882 return circleBreakouts( aWidth, via->Shape(), aPermitDiagonal );
883 }
884
885 case ITEM::SOLID_T:
886 {
887 const SHAPE* shape = aItem->Shape();
888
889 switch( shape->Type() )
890 {
891 case SH_RECT:
892 return rectBreakouts( aWidth, shape, aPermitDiagonal );
893
894 case SH_SEGMENT:
895 {
896 const SHAPE_SEGMENT* seg = static_cast<const SHAPE_SEGMENT*> (shape);
897 const SHAPE_RECT rect = ApproximateSegmentAsRect ( *seg );
898 return rectBreakouts( aWidth, &rect, aPermitDiagonal );
899 }
900
901 case SH_CIRCLE:
902 return circleBreakouts( aWidth, shape, aPermitDiagonal );
903
904 case SH_SIMPLE:
905 return customBreakouts( aWidth, aItem, aPermitDiagonal );
906
907 default:
908 break;
909 }
910
911 break;
912 }
913
914 default:
915 break;
916 }
917
918 return BREAKOUT_LIST();
919}
@ SOLID_T
Definition: pns_item.h:63
BREAKOUT_LIST rectBreakouts(int aWidth, const SHAPE *aShape, bool aPermitDiagonal) const
BREAKOUT_LIST customBreakouts(int aWidth, const ITEM *aItem, bool aPermitDiagonal) const
BREAKOUT_LIST circleBreakouts(int aWidth, const SHAPE *aShape, bool aPermitDiagonal) const
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:95
An abstract shape on 2D plane.
Definition: shape.h:123
SHAPE_RECT ApproximateSegmentAsRect(const SHAPE_SEGMENT &aSeg)
Definition: pns_utils.cpp:340
@ VIA
Normal via.
Definition: router_tool.cpp:76
@ SH_RECT
axis-aligned rectangle
Definition: shape.h:44
@ SH_CIRCLE
circle
Definition: shape.h:47
@ SH_SIMPLE
simple polygon
Definition: shape.h:48
@ SH_SEGMENT
line segment
Definition: shape.h:45

References PNS::ApproximateSegmentAsRect(), circleBreakouts(), customBreakouts(), PNS::ITEM::Kind(), rectBreakouts(), SH_CIRCLE, SH_RECT, SH_SEGMENT, SH_SIMPLE, PNS::ITEM::Shape(), PNS::ITEM::SOLID_T, SHAPE_BASE::Type(), via, and PNS::ITEM::VIA_T.

Referenced by smartPadsSingle().

◆ customBreakouts()

OPTIMIZER::BREAKOUT_LIST PNS::OPTIMIZER::customBreakouts ( int  aWidth,
const ITEM aItem,
bool  aPermitDiagonal 
) const
private

Definition at line 771 of file pns_optimizer.cpp.

773{
774 BREAKOUT_LIST breakouts;
775 const SHAPE_SIMPLE* convex = static_cast<const SHAPE_SIMPLE*>( aItem->Shape() );
776
777 BOX2I bbox = convex->BBox( 0 );
778 VECTOR2I p0 = static_cast<const SOLID*>( aItem )->Pos();
779 // must be large enough to guarantee intersecting the convex polygon
780 int length = std::max( bbox.GetWidth(), bbox.GetHeight() ) / 2 + 5;
781 EDA_ANGLE increment = ( aPermitDiagonal ? ANGLE_45 : ANGLE_90 );
782
783 for( EDA_ANGLE angle = ANGLE_0; angle < ANGLE_360; angle += increment )
784 {
786 VECTOR2I v0( p0 + VECTOR2I( length, 0 ) );
787 RotatePoint( v0, p0, -angle );
788
790 int n = convex->Vertices().Intersect( SEG( p0, v0 ), intersections );
791
792 // if n == 1 intersected a segment
793 // if n == 2 intersected the common point of 2 segments
794 // n == 0 can not happen I think, but...
795 if( n > 0 )
796 {
797 l.Append( p0 );
798
799 // for a breakout distance relative to the distance between
800 // center and polygon edge
801 //l.Append( intersections[0].p + (v0 - p0).Resize( (intersections[0].p - p0).EuclideanNorm() * 0.4 ) );
802
803 // for an absolute breakout distance, e.g. 0.1 mm
804 //l.Append( intersections[0].p + (v0 - p0).Resize( 100000 ) );
805
806 // for the breakout right on the polygon edge
807 l.Append( intersections[0].p );
808
809 breakouts.push_back( l );
810 }
811 }
812
813 return breakouts;
814}
coord_type GetHeight() const
Definition: box2.h:181
coord_type GetWidth() const
Definition: box2.h:180
Definition: seg.h:42
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Find all intersection points between our line chain and the segment aSeg.
std::vector< INTERSECTION > INTERSECTIONS
Represent a simple polygon consisting of a zero-thickness closed chain of connected line segments.
Definition: shape_simple.h:42
const SHAPE_LINE_CHAIN & Vertices() const
Return the list of vertices defining this simple polygon.
Definition: shape_simple.h:124
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Definition: shape_simple.h:78
static constexpr EDA_ANGLE & ANGLE_90
Definition: eda_angle.h:425
VECTOR2< int > VECTOR2I
Definition: vector2d.h:607

References PNS::angle(), ANGLE_0, ANGLE_360, ANGLE_45, ANGLE_90, SHAPE_LINE_CHAIN::Append(), SHAPE_SIMPLE::BBox(), BOX2< Vec >::GetHeight(), BOX2< Vec >::GetWidth(), SHAPE_LINE_CHAIN::Intersect(), RotatePoint(), PNS::ITEM::Shape(), and SHAPE_SIMPLE::Vertices().

Referenced by computeBreakouts().

◆ fanoutCleanup()

bool PNS::OPTIMIZER::fanoutCleanup ( LINE aLine)
private

Definition at line 1101 of file pns_optimizer.cpp.

1102{
1103 if( aLine->PointCount() < 3 )
1104 return false;
1105
1106 VECTOR2I p_start = aLine->CPoint( 0 ), p_end = aLine->CPoint( -1 );
1107
1108 ITEM* startPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_start );
1109 ITEM* endPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_end );
1110
1111 int thr = aLine->Width() * 10;
1112 int len = aLine->CLine().Length();
1113
1114 if( !startPad )
1115 return false;
1116
1117 bool startMatch = startPad->OfKind( ITEM::VIA_T | ITEM::SOLID_T );
1118 bool endMatch = false;
1119
1120 if(endPad)
1121 {
1122 endMatch = endPad->OfKind( ITEM::VIA_T | ITEM::SOLID_T );
1123 }
1124 else
1125 {
1126 endMatch = aLine->EndsWithVia();
1127 }
1128
1129 if( startMatch && endMatch && len < thr )
1130 {
1131 for( int i = 0; i < 2; i++ )
1132 {
1133 SHAPE_LINE_CHAIN l2 = DIRECTION_45().BuildInitialTrace( p_start, p_end, i );
1134 LINE repl;
1135 repl = LINE( *aLine, l2 );
1136
1137 if( !m_world->CheckColliding( &repl ) )
1138 {
1139 aLine->SetShape( repl.CLine() );
1140 return true;
1141 }
1142 }
1143 }
1144
1145 return false;
1146}
Represent route directions & corner angles in a 45-degree metric.
Definition: direction45.h:37
const SHAPE_LINE_CHAIN BuildInitialTrace(const VECTOR2I &aP0, const VECTOR2I &aP1, bool aStartDiagonal=false, CORNER_MODE aMode=CORNER_MODE::MITERED_45) const
Build a 2-segment line chain between points aP0 and aP1 and following 45-degree routing regime.
ITEM * findPadOrVia(int aLayer, int aNet, const VECTOR2I &aP) const

References DIRECTION_45::BuildInitialTrace(), PNS::NODE::CheckColliding(), PNS::LINE::CLine(), PNS::LINE::CPoint(), PNS::LINE::EndsWithVia(), findPadOrVia(), PNS::ITEM::Layer(), SHAPE_LINE_CHAIN::Length(), m_world, PNS::ITEM::Net(), PNS::ITEM::OfKind(), PNS::LINE::PointCount(), PNS::LINE::SetShape(), PNS::ITEM::SOLID_T, PNS::ITEM::VIA_T, and PNS::LINE::Width().

Referenced by Optimize().

◆ findPadOrVia()

ITEM * PNS::OPTIMIZER::findPadOrVia ( int  aLayer,
int  aNet,
const VECTOR2I aP 
) const
private

Definition at line 922 of file pns_optimizer.cpp.

923{
924 JOINT* jt = m_world->FindJoint( aP, aLayer, aNet );
925
926 if( !jt )
927 return nullptr;
928
929 for( ITEM* item : jt->LinkList() )
930 {
931 if( item->OfKind( ITEM::VIA_T | ITEM::SOLID_T ) )
932 return item;
933 }
934
935 return nullptr;
936}
JOINT * FindJoint(const VECTOR2I &aPos, int aLayer, int aNet)
Search for a joint at a given position, layer and belonging to given net.
Definition: pns_node.cpp:1181

References PNS::NODE::FindJoint(), PNS::JOINT::LinkList(), m_world, PNS::ITEM::SOLID_T, and PNS::ITEM::VIA_T.

Referenced by fanoutCleanup(), and runSmartPads().

◆ mergeColinear()

bool PNS::OPTIMIZER::mergeColinear ( LINE aLine)
private

Definition at line 575 of file pns_optimizer.cpp.

576{
577 SHAPE_LINE_CHAIN& line = aLine->Line();
578
579 int nSegs = line.SegmentCount();
580
581 for( int segIdx = 0; segIdx < line.SegmentCount() - 1; ++segIdx )
582 {
583 SEG s1 = line.CSegment( segIdx );
584 SEG s2 = line.CSegment( segIdx + 1 );
585
586 // Skip zero-length segs caused by abutting arcs
587 if( s1.SquaredLength() == 0 || s2.SquaredLength() == 0 )
588 continue;
589
590 if( s1.Collinear( s2 ) && !line.IsPtOnArc( segIdx + 1 ) )
591 {
592 line.Remove( segIdx + 1 );
593 }
594 }
595
596 return line.SegmentCount() < nSegs;
597}
bool Collinear(const SEG &aSeg) const
Check if segment aSeg lies on the same line as (this).
Definition: seg.h:269
ecoord SquaredLength() const
Definition: seg.h:356
bool IsPtOnArc(size_t aPtIndex) const
int SegmentCount() const
Return the number of segments in this line chain.
void Remove(int aStartIndex, int aEndIndex)
Remove the range of points [start_index, end_index] from the line chain.
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.

References SEG::Collinear(), SHAPE_LINE_CHAIN::CSegment(), SHAPE_LINE_CHAIN::IsPtOnArc(), PNS::LINE::Line(), SHAPE_LINE_CHAIN::Remove(), SHAPE_LINE_CHAIN::SegmentCount(), and SEG::SquaredLength().

Referenced by Optimize().

◆ mergeDpSegments()

bool PNS::OPTIMIZER::mergeDpSegments ( DIFF_PAIR aPair)
private

Definition at line 1323 of file pns_optimizer.cpp.

1324{
1325 int step_p = aPair->CP().SegmentCount() - 2;
1326 int step_n = aPair->CN().SegmentCount() - 2;
1327
1328 while( 1 )
1329 {
1330 int n_segs_p = aPair->CP().SegmentCount();
1331 int n_segs_n = aPair->CN().SegmentCount();
1332
1333 int max_step_p = n_segs_p - 2;
1334 int max_step_n = n_segs_n - 2;
1335
1336 if( step_p > max_step_p )
1337 step_p = max_step_p;
1338
1339 if( step_n > max_step_n )
1340 step_n = max_step_n;
1341
1342 if( step_p < 1 && step_n < 1 )
1343 break;
1344
1345 bool found_anything_p = false;
1346 bool found_anything_n = false;
1347
1348 if( step_p > 1 )
1349 found_anything_p = mergeDpStep( aPair, true, step_p );
1350
1351 if( step_n > 1 )
1352 found_anything_n = mergeDpStep( aPair, false, step_n );
1353
1354 if( !found_anything_n && !found_anything_p )
1355 {
1356 step_n--;
1357 step_p--;
1358 }
1359 }
1360 return true;
1361}
bool mergeDpStep(DIFF_PAIR *aPair, bool aTryP, int step)

References PNS::DIFF_PAIR::CN(), PNS::DIFF_PAIR::CP(), mergeDpStep(), and SHAPE_LINE_CHAIN::SegmentCount().

Referenced by Optimize().

◆ mergeDpStep()

bool PNS::OPTIMIZER::mergeDpStep ( DIFF_PAIR aPair,
bool  aTryP,
int  step 
)
private

Definition at line 1260 of file pns_optimizer.cpp.

1261{
1262 int n = 1;
1263
1264 SHAPE_LINE_CHAIN currentPath = aTryP ? aPair->CP() : aPair->CN();
1265 SHAPE_LINE_CHAIN coupledPath = aTryP ? aPair->CN() : aPair->CP();
1266
1267 int n_segs = currentPath.SegmentCount() - 1;
1268
1269 int64_t clenPre = aPair->CoupledLength( currentPath, coupledPath );
1270 int64_t budget = clenPre / 10; // fixme: come up with something more intelligent here...
1271
1272 while( n < n_segs - step )
1273 {
1274 const SEG s1 = currentPath.CSegment( n );
1275 const SEG s2 = currentPath.CSegment( n + step );
1276
1277 DIRECTION_45 dir1( s1 );
1278 DIRECTION_45 dir2( s2 );
1279
1280 if( dir1.IsObtuse( dir2 ) )
1281 {
1283 dir1.IsDiagonal() );
1284 SHAPE_LINE_CHAIN newRef;
1285 SHAPE_LINE_CHAIN newCoup;
1286 int64_t deltaCoupled = -1, deltaUni = -1;
1287
1288 newRef = currentPath;
1289 newRef.Replace( s1.Index(), s2.Index(), bypass );
1290
1291 deltaUni = aPair->CoupledLength ( newRef, coupledPath ) - clenPre + budget;
1292
1293 if( coupledBypass( m_world, aPair, aTryP, newRef, bypass, coupledPath, newCoup ) )
1294 {
1295 deltaCoupled = aPair->CoupledLength( newRef, newCoup ) - clenPre + budget;
1296
1297 if( deltaCoupled >= 0 )
1298 {
1299 newRef.Simplify();
1300 newCoup.Simplify();
1301
1302 aPair->SetShape( newRef, newCoup, !aTryP );
1303 return true;
1304 }
1305 }
1306 else if( deltaUni >= 0 && verifyDpBypass( m_world, aPair, aTryP, newRef, coupledPath ) )
1307 {
1308 newRef.Simplify();
1309 coupledPath.Simplify();
1310
1311 aPair->SetShape( newRef, coupledPath, !aTryP );
1312 return true;
1313 }
1314 }
1315
1316 n++;
1317 }
1318
1319 return false;
1320}
VECTOR2I A
Definition: seg.h:49
VECTOR2I B
Definition: seg.h:50
int Index() const
Return the index of this segment in its parent shape (applicable only to non-local segments).
Definition: seg.h:369
SHAPE_LINE_CHAIN & Simplify(bool aRemoveColinear=true)
Simplify the line chain by removing colinear adjacent segments and duplicate vertices.
void Replace(int aStartIndex, int aEndIndex, const VECTOR2I &aP)
Replace points with indices in range [start_index, end_index] with a single point aP.
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)
bool verifyDpBypass(NODE *aNode, DIFF_PAIR *aPair, bool aRefIsP, const SHAPE_LINE_CHAIN &aNewRef, const SHAPE_LINE_CHAIN &aNewCoupled)

References SEG::A, SEG::B, DIRECTION_45::BuildInitialTrace(), PNS::DIFF_PAIR::CN(), PNS::coupledBypass(), PNS::DIFF_PAIR::CoupledLength(), PNS::DIFF_PAIR::CP(), SHAPE_LINE_CHAIN::CSegment(), SEG::Index(), DIRECTION_45::IsDiagonal(), DIRECTION_45::IsObtuse(), m_world, SHAPE_LINE_CHAIN::Replace(), SHAPE_LINE_CHAIN::SegmentCount(), PNS::DIFF_PAIR::SetShape(), SHAPE_LINE_CHAIN::Simplify(), and PNS::verifyDpBypass().

Referenced by mergeDpSegments().

◆ mergeFull()

bool PNS::OPTIMIZER::mergeFull ( LINE aLine)
private

Definition at line 535 of file pns_optimizer.cpp.

536{
537 SHAPE_LINE_CHAIN& line = aLine->Line();
538 int step = line.SegmentCount() - 1;
539
540 int segs_pre = line.SegmentCount();
541
542 line.Simplify();
543
544 if( step < 0 )
545 return false;
546
547 SHAPE_LINE_CHAIN current_path( line );
548
549 while( 1 )
550 {
551 int n_segs = current_path.SegmentCount();
552 int max_step = n_segs - 2;
553
554 if( step > max_step )
555 step = max_step;
556
557 if( step < 1 )
558 break;
559
560 bool found_anything = mergeStep( aLine, current_path, step );
561
562 if( !found_anything )
563 step--;
564
565 if( !step )
566 break;
567 }
568
569 aLine->SetShape( current_path );
570
571 return current_path.SegmentCount() < segs_pre;
572}
bool mergeStep(LINE *aLine, SHAPE_LINE_CHAIN &aCurrentLine, int step)

References PNS::LINE::Line(), mergeStep(), SHAPE_LINE_CHAIN::SegmentCount(), PNS::LINE::SetShape(), and SHAPE_LINE_CHAIN::Simplify().

Referenced by Optimize().

◆ mergeObtuse()

bool PNS::OPTIMIZER::mergeObtuse ( LINE aLine)
private

Definition at line 454 of file pns_optimizer.cpp.

455{
456 SHAPE_LINE_CHAIN& line = aLine->Line();
457
458 int step = line.PointCount() - 3;
459 int iter = 0;
460 int segs_pre = line.SegmentCount();
461
462 if( step < 0 )
463 return false;
464
465 SHAPE_LINE_CHAIN current_path( line );
466
467 while( 1 )
468 {
469 iter++;
470 int n_segs = current_path.SegmentCount();
471 int max_step = n_segs - 2;
472
473 if( step > max_step )
474 step = max_step;
475
476 if( step < 2 )
477 {
478 line = current_path;
479 return current_path.SegmentCount() < segs_pre;
480 }
481
482 bool found_anything = false;
483
484 for( int n = 0; n < n_segs - step; n++ )
485 {
486 const SEG s1 = current_path.CSegment( n );
487 const SEG s2 = current_path.CSegment( n + step );
488 SEG s1opt, s2opt;
489
490 if( DIRECTION_45( s1 ).IsObtuse( DIRECTION_45( s2 ) ) )
491 {
492 VECTOR2I ip = *s1.IntersectLines( s2 );
493
494 s1opt = SEG( s1.A, ip );
495 s2opt = SEG( ip, s2.B );
496
497 if( DIRECTION_45( s1opt ).IsObtuse( DIRECTION_45( s2opt ) ) )
498 {
499 SHAPE_LINE_CHAIN opt_path;
500 opt_path.Append( s1opt.A );
501 opt_path.Append( s1opt.B );
502 opt_path.Append( s2opt.B );
503
504 LINE opt_track( *aLine, opt_path );
505
506 if( !checkColliding( &opt_track ) )
507 {
508 current_path.Replace( s1.Index() + 1, s2.Index(), ip );
509
510 // removeCachedSegments(aLine, s1.Index(), s2.Index());
511 n_segs = current_path.SegmentCount();
512 found_anything = true;
513 break;
514 }
515 }
516 }
517 }
518
519 if( !found_anything )
520 {
521 if( step <= 2 )
522 {
523 line = current_path;
524 return line.SegmentCount() < segs_pre;
525 }
526
527 step--;
528 }
529 }
530
531 return line.SegmentCount() < segs_pre;
532}
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Compute the intersection point of lines passing through ends of (this) and aSeg.
Definition: seg.h:210
int PointCount() const
Return the number of points (vertices) in this line chain.

References SEG::A, SHAPE_LINE_CHAIN::Append(), SEG::B, checkColliding(), SHAPE_LINE_CHAIN::CSegment(), SEG::Index(), SEG::IntersectLines(), PNS::LINE::Line(), SHAPE_LINE_CHAIN::PointCount(), SHAPE_LINE_CHAIN::Replace(), and SHAPE_LINE_CHAIN::SegmentCount().

Referenced by Optimize().

◆ mergeStep()

bool PNS::OPTIMIZER::mergeStep ( LINE aLine,
SHAPE_LINE_CHAIN aCurrentLine,
int  step 
)
private

Definition at line 680 of file pns_optimizer.cpp.

681{
682 int n_segs = aCurrentPath.SegmentCount();
683
684 int cost_orig = COST_ESTIMATOR::CornerCost( aCurrentPath );
685
686 if( aLine->SegmentCount() < 2 )
687 return false;
688
689 DIRECTION_45 orig_start( aLine->CSegment( 0 ) );
690 DIRECTION_45 orig_end( aLine->CSegment( -1 ) );
691
692
693 for( int n = 0; n < n_segs - step; n++ )
694 {
695 // Do not attempt to merge false segments that are part of an arc
696 if( aCurrentPath.IsArcSegment( n )
697 || aCurrentPath.IsArcSegment( static_cast<std::size_t>( n ) + step ) )
698 {
699 continue;
700 }
701
702 const SEG s1 = aCurrentPath.CSegment( n );
703 const SEG s2 = aCurrentPath.CSegment( n + step );
704
706 SHAPE_LINE_CHAIN* picked = nullptr;
707 int cost[2];
708
709 for( int i = 0; i < 2; i++ )
710 {
711 SHAPE_LINE_CHAIN bypass = DIRECTION_45().BuildInitialTrace( s1.A, s2.B, i );
712 cost[i] = INT_MAX;
713
714 bool ok = false;
715
716 if( !checkColliding( aLine, bypass ) )
717 {
718 //printf("Chk-constraints: %d %d\n", n, n+step+1 );
719 ok = checkConstraints ( n, n + step + 1, aLine, aCurrentPath, bypass );
720 }
721
722 if( ok )
723 {
724 path[i] = aCurrentPath;
725 path[i].Replace( s1.Index(), s2.Index(), bypass );
726 path[i].Simplify();
727 cost[i] = COST_ESTIMATOR::CornerCost( path[i] );
728 }
729 }
730
731 if( cost[0] < cost_orig && cost[0] < cost[1] )
732 picked = &path[0];
733 else if( cost[1] < cost_orig )
734 picked = &path[1];
735
736 if( picked )
737 {
738 n_segs = aCurrentPath.SegmentCount();
739 aCurrentPath = *picked;
740 return true;
741 }
742 }
743
744 return false;
745}
static int CornerCost(const SEG &aA, const SEG &aB)
bool checkConstraints(int aVertex1, int aVertex2, LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement)

References SEG::A, SEG::B, DIRECTION_45::BuildInitialTrace(), checkColliding(), checkConstraints(), PNS::COST_ESTIMATOR::CornerCost(), PNS::LINE::CSegment(), SHAPE_LINE_CHAIN::CSegment(), SEG::Index(), SHAPE_LINE_CHAIN::IsArcSegment(), path, SHAPE_LINE_CHAIN::SegmentCount(), and PNS::LINE::SegmentCount().

Referenced by mergeFull().

◆ Optimize() [1/3]

bool PNS::OPTIMIZER::Optimize ( DIFF_PAIR aPair)

Definition at line 1364 of file pns_optimizer.cpp.

1365{
1366 return mergeDpSegments( aPair );
1367}
bool mergeDpSegments(DIFF_PAIR *aPair)

References mergeDpSegments().

◆ Optimize() [2/3]

bool PNS::OPTIMIZER::Optimize ( LINE aLine,
int  aEffortLevel,
NODE aWorld,
const VECTOR2I aV = VECTOR2I(0, 0) 
)
static

Definition at line 1087 of file pns_optimizer.cpp.

1088{
1089 OPTIMIZER opt( aWorld );
1090
1091 opt.SetEffortLevel( aEffortLevel );
1092 opt.SetCollisionMask( -1 );
1093
1094 if( aEffortLevel & OPTIMIZER::PRESERVE_VERTEX )
1095 opt.SetPreserveVertex( aV );
1096
1097 return opt.Optimize( aLine );
1098}
OPTIMIZER(NODE *aWorld)

References Optimize(), PRESERVE_VERTEX, SetCollisionMask(), SetEffortLevel(), and SetPreserveVertex().

Referenced by Optimize(), PNS::DRAGGER::optimizeAndUpdateDraggedLine(), PNS::LINE_PLACER::optimizeTailHeadTransition(), PNS::LINE_PLACER::rhShoveOnly(), PNS::LINE_PLACER::rhWalkOnly(), PNS::WALKAROUND::Route(), PNS::SHOVE::runOptimizer(), PNS::LINE_PLACER::simplifyNewLine(), and PNS::DIFF_PAIR_PLACER::tryWalkDp().

◆ Optimize() [3/3]

bool PNS::OPTIMIZER::Optimize ( LINE aLine,
LINE aResult = nullptr,
LINE aRoot = nullptr 
)

Definition at line 600 of file pns_optimizer.cpp.

601{
602 DEBUG_DECORATOR* dbg = ROUTER::GetInstance()->GetInterface()->GetDebugDecorator();
603
604 if( aRoot )
605 {
606 PNS_DBG( dbg, AddItem, aRoot, BLUE, 100000, wxT( "root-line" ) );
607 }
608
609
610 if( !aResult )
611 {
612 aResult = aLine;
613 }
614 else
615 {
616 *aResult = *aLine;
617 aResult->ClearLinks();
618 }
619
620 bool hasArcs = aLine->ArcCount();
621 bool rv = false;
622
623 if( (m_effortLevel & LIMIT_CORNER_COUNT) && aRoot )
624 {
625 const int angleMask = DIRECTION_45::ANG_OBTUSE;
626 int rootObtuseCorners = aRoot->CountCorners( angleMask );
627 auto c = new CORNER_COUNT_LIMIT_CONSTRAINT( m_world, rootObtuseCorners,
628 aLine->SegmentCount(), angleMask );
629 AddConstraint( c );
630 }
631
633 {
634 auto c = new PRESERVE_VERTEX_CONSTRAINT( m_world, m_preservedVertex );
635 AddConstraint( c );
636 }
637
639 {
640 auto c = new RESTRICT_VERTEX_RANGE_CONSTRAINT( m_world, m_restrictedVertexRange.first,
642 AddConstraint( c );
643 }
644
646 {
647 auto c = new AREA_CONSTRAINT( m_world, m_restrictArea, m_restrictAreaIsStrict );
648 AddConstraint( c );
649 }
650
652 {
653 auto c = new KEEP_TOPOLOGY_CONSTRAINT( m_world );
654 AddConstraint( c );
655 }
656
657 // TODO: Fix for arcs
658 if( !hasArcs && m_effortLevel & MERGE_SEGMENTS )
659 rv |= mergeFull( aResult );
660
661 // TODO: Fix for arcs
662 if( !hasArcs && m_effortLevel & MERGE_OBTUSE )
663 rv |= mergeObtuse( aResult );
664
666 rv |= mergeColinear( aResult );
667
668 // TODO: Fix for arcs
669 if( !hasArcs && m_effortLevel & SMART_PADS )
670 rv |= runSmartPads( aResult );
671
672 // TODO: Fix for arcs
673 if( !hasArcs && m_effortLevel & FANOUT_CLEANUP )
674 rv |= fanoutCleanup( aResult );
675
676 return rv;
677}
std::pair< int, int > m_restrictedVertexRange
bool mergeColinear(LINE *aLine)
bool fanoutCleanup(LINE *aLine)
bool mergeFull(LINE *aLine)
void AddConstraint(OPT_CONSTRAINT *aConstraint)
bool mergeObtuse(LINE *aLine)
bool runSmartPads(LINE *aLine)
VECTOR2I m_preservedVertex
virtual DEBUG_DECORATOR * GetDebugDecorator()=0
ROUTER_IFACE * GetInterface() const
Definition: pns_router.h:208
static ROUTER * GetInstance()
Definition: pns_router.cpp:78
@ BLUE
Definition: color4d.h:56
#define PNS_DBG(dbg, method,...)

References AddConstraint(), DIRECTION_45::ANG_OBTUSE, PNS::LINE::ArcCount(), BLUE, PNS::LINK_HOLDER::ClearLinks(), PNS::LINE::CountCorners(), FANOUT_CLEANUP, fanoutCleanup(), PNS::ROUTER_IFACE::GetDebugDecorator(), PNS::ROUTER::GetInstance(), PNS::ROUTER::GetInterface(), KEEP_TOPOLOGY, LIMIT_CORNER_COUNT, m_effortLevel, m_preservedVertex, m_restrictArea, m_restrictAreaIsStrict, m_restrictedVertexRange, m_world, MERGE_COLINEAR, MERGE_OBTUSE, MERGE_SEGMENTS, mergeColinear(), mergeFull(), mergeObtuse(), PNS_DBG, PRESERVE_VERTEX, RESTRICT_AREA, RESTRICT_VERTEX_RANGE, runSmartPads(), PNS::LINE::SegmentCount(), and SMART_PADS.

◆ rectBreakouts()

OPTIMIZER::BREAKOUT_LIST PNS::OPTIMIZER::rectBreakouts ( int  aWidth,
const SHAPE aShape,
bool  aPermitDiagonal 
) const
private

Definition at line 817 of file pns_optimizer.cpp.

819{
820 const SHAPE_RECT* rect = static_cast<const SHAPE_RECT*>(aShape);
821 VECTOR2I s = rect->GetSize();
822 VECTOR2I c = rect->GetPosition() + VECTOR2I( s.x / 2, s.y / 2 );
823
824 BREAKOUT_LIST breakouts;
825 breakouts.reserve( 12 );
826
827 VECTOR2I d_offset;
828
829 d_offset.x = ( s.x > s.y ) ? ( s.x - s.y ) / 2 : 0;
830 d_offset.y = ( s.x < s.y ) ? ( s.y - s.x ) / 2 : 0;
831
832 VECTOR2I d_vert = VECTOR2I( 0, s.y / 2 + aWidth );
833 VECTOR2I d_horiz = VECTOR2I( s.x / 2 + aWidth, 0 );
834
835 breakouts.emplace_back( SHAPE_LINE_CHAIN( { c, c + d_horiz } ) );
836 breakouts.emplace_back( SHAPE_LINE_CHAIN( { c, c - d_horiz } ) );
837 breakouts.emplace_back( SHAPE_LINE_CHAIN( { c, c + d_vert } ) );
838 breakouts.emplace_back( SHAPE_LINE_CHAIN( { c, c - d_vert } ) );
839
840 if( aPermitDiagonal )
841 {
842 int l = aWidth + std::min( s.x, s.y ) / 2;
843 VECTOR2I d_diag;
844
845 if( s.x >= s.y )
846 {
847 breakouts.emplace_back(
848 SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( l, l ) } ) );
849 breakouts.emplace_back(
850 SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset - VECTOR2I( -l, l ) } ) );
851 breakouts.emplace_back(
852 SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset + VECTOR2I( -l, l ) } ) );
853 breakouts.emplace_back(
854 SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( l, l ) } ) );
855 }
856 else
857 {
858 // fixme: this could be done more efficiently
859 breakouts.emplace_back(
860 SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( l, l ) } ) );
861 breakouts.emplace_back(
862 SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( -l, l ) } ) );
863 breakouts.emplace_back(
864 SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( -l, l ) } ) );
865 breakouts.emplace_back(
866 SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( l, l ) } ) );
867 }
868 }
869
870 return breakouts;
871}
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:127
const VECTOR2I GetSize() const
Definition: shape_rect.h:135

References SHAPE_RECT::GetPosition(), SHAPE_RECT::GetSize(), VECTOR2< T >::x, and VECTOR2< T >::y.

Referenced by computeBreakouts().

◆ removeCachedSegments()

void PNS::OPTIMIZER::removeCachedSegments ( LINE aLine,
int  aStartVertex = 0,
int  aEndVertex = -1 
)
private

Definition at line 165 of file pns_optimizer.cpp.

166{
167 if( !aLine->IsLinked() )
168 return;
169
170 auto links = aLine->Links();
171
172 if( aEndVertex < 0 )
173 aEndVertex += aLine->PointCount();
174
175 for( int i = aStartVertex; i < aEndVertex - 1; i++ )
176 {
177 LINKED_ITEM* s = links[i];
178 m_cacheTags.erase( s );
179 m_cache.Remove( s );
180 }
181}

References PNS::LINK_HOLDER::IsLinked(), PNS::LINK_HOLDER::Links(), m_cache, m_cacheTags, and PNS::LINE::PointCount().

Referenced by CacheRemove().

◆ runSmartPads()

bool PNS::OPTIMIZER::runSmartPads ( LINE aLine)
private

Definition at line 1060 of file pns_optimizer.cpp.

1061{
1062 SHAPE_LINE_CHAIN& line = aLine->Line();
1063
1064 if( line.PointCount() < 3 )
1065 return false;
1066
1067 VECTOR2I p_start = line.CPoint( 0 ), p_end = line.CPoint( -1 );
1068
1069 ITEM* startPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_start );
1070 ITEM* endPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_end );
1071
1072 int vtx = -1;
1073
1074 if( startPad )
1075 vtx = smartPadsSingle( aLine, startPad, false, 3 );
1076
1077 if( endPad )
1078 smartPadsSingle( aLine, endPad, true,
1079 vtx < 0 ? line.PointCount() - 1 : line.PointCount() - 1 - vtx );
1080
1081 aLine->Line().Simplify();
1082
1083 return true;
1084}
int smartPadsSingle(LINE *aLine, ITEM *aPad, bool aEnd, int aEndVertex)
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.

References SHAPE_LINE_CHAIN::CPoint(), findPadOrVia(), PNS::ITEM::Layer(), PNS::LINE::Line(), PNS::ITEM::Net(), SHAPE_LINE_CHAIN::PointCount(), SHAPE_LINE_CHAIN::Simplify(), and smartPadsSingle().

Referenced by Optimize().

◆ SetCollisionMask()

void PNS::OPTIMIZER::SetCollisionMask ( int  aMask)
inline

Definition at line 127 of file pns_optimizer.h.

128 {
129 m_collisionKindMask = aMask;
130 }

References m_collisionKindMask.

Referenced by Optimize(), PNS::LINE_PLACER::rhShoveOnly(), and PNS::SHOVE::runOptimizer().

◆ SetEffortLevel()

void PNS::OPTIMIZER::SetEffortLevel ( int  aEffort)
inline

◆ SetPreserveVertex()

void PNS::OPTIMIZER::SetPreserveVertex ( const VECTOR2I aV)
inline

◆ SetRestrictArea()

void PNS::OPTIMIZER::SetRestrictArea ( const BOX2I aArea,
bool  aStrict = true 
)
inline

Definition at line 150 of file pns_optimizer.h.

151 {
152 m_restrictArea = aArea;
153 m_restrictAreaIsStrict = aStrict;
154 }

References m_restrictArea, and m_restrictAreaIsStrict.

Referenced by PNS::DRAGGER::optimizeAndUpdateDraggedLine(), and PNS::SHOVE::runOptimizer().

◆ SetRestrictVertexRange()

void PNS::OPTIMIZER::SetRestrictVertexRange ( int  aStart,
int  aEnd 
)
inline

Definition at line 143 of file pns_optimizer.h.

144 {
145 m_restrictedVertexRange.first = aStart;
146 m_restrictedVertexRange.second = aEnd;
148 }

References m_effortLevel, m_restrictedVertexRange, and RESTRICT_VERTEX_RANGE.

◆ SetWorld()

void PNS::OPTIMIZER::SetWorld ( NODE aNode)
inline

Definition at line 123 of file pns_optimizer.h.

123{ m_world = aNode; }

References m_world.

Referenced by PNS::LINE_PLACER::rhShoveOnly().

◆ smartPadsSingle()

int PNS::OPTIMIZER::smartPadsSingle ( LINE aLine,
ITEM aPad,
bool  aEnd,
int  aEndVertex 
)
private

Definition at line 939 of file pns_optimizer.cpp.

940{
941 DIRECTION_45 dir;
942
943 const int ForbiddenAngles = DIRECTION_45::ANG_ACUTE | DIRECTION_45::ANG_RIGHT |
945
946 typedef std::tuple<int, long long int, SHAPE_LINE_CHAIN> RtVariant;
947 std::vector<RtVariant> variants;
948
949 SOLID* solid = dyn_cast<SOLID*>( aPad );
950
951 // don't do optimized connections for offset pads
952 if( solid && solid->Offset() != VECTOR2I( 0, 0 ) )
953 return -1;
954
955 // don't do optimization on vias, they are always round at the moment and the optimizer
956 // will possibly mess up an intended via exit posture
957 if( aPad->Kind() == ITEM::VIA_T )
958 return -1;
959
960 BREAKOUT_LIST breakouts = computeBreakouts( aLine->Width(), aPad, true );
961 SHAPE_LINE_CHAIN line = ( aEnd ? aLine->CLine().Reverse() : aLine->CLine() );
962 int p_end = std::min( aEndVertex, std::min( 3, line.PointCount() - 1 ) );
963
964 // Start at 1 to find a potentially better breakout (0 is the pad connection)
965 for( int p = 1; p <= p_end; p++ )
966 {
967 // If the line is contained inside the pad, don't optimize
968 if( solid && solid->Shape() && !solid->Shape()->Collide(
969 SEG( line.CPoint( 0 ), line.CPoint( p ) ), aLine->Width() / 2 ) )
970 {
971 continue;
972 }
973
974 for( SHAPE_LINE_CHAIN & breakout : breakouts )
975 {
976 for( int diag = 0; diag < 2; diag++ )
977 {
980 breakout.CPoint( -1 ), line.CPoint( p ), diag == 0 );
981
982 DIRECTION_45 dir_bkout( breakout.CSegment( -1 ) );
983
984 if( !connect.SegmentCount() )
985 continue;
986
987 int ang1 = dir_bkout.Angle( DIRECTION_45( connect.CSegment( 0 ) ) );
988
989 if( ang1 & ForbiddenAngles )
990 continue;
991
992 if( breakout.Length() > line.Length() )
993 continue;
994
995 v = breakout;
996 v.Append( connect );
997
998 for( int i = p + 1; i < line.PointCount(); i++ )
999 v.Append( line.CPoint( i ) );
1000
1001 LINE tmp( *aLine, v );
1002 int cc = tmp.CountCorners( ForbiddenAngles );
1003
1004 if( cc == 0 )
1005 {
1006 RtVariant vp;
1007 std::get<0>( vp ) = p;
1008 std::get<1>( vp ) = breakout.Length();
1009 std::get<2>( vp ) = aEnd ? v.Reverse() : v;
1010 std::get<2>( vp ).Simplify();
1011 variants.push_back( vp );
1012 }
1013 }
1014 }
1015 }
1016
1017 // We attempt to minimize the corner cost (minimizes the segments and types of corners)
1018 // but given two, equally valid costs, we want to pick the longer pad exit. The logic
1019 // here is that if the pad is oblong, the track should not exit the shorter side and parallel
1020 // the pad but should follow the pad's preferential direction before exiting.
1021 // The baseline guess is to start with the existing line the user has drawn.
1022 int min_cost = COST_ESTIMATOR::CornerCost( *aLine );
1023 long long int max_length = 0;
1024 bool found = false;
1025 int p_best = -1;
1026 SHAPE_LINE_CHAIN l_best;
1027
1028 for( RtVariant& vp : variants )
1029 {
1030 LINE tmp( *aLine, std::get<2>( vp ) );
1031 int cost = COST_ESTIMATOR::CornerCost( std::get<2>( vp ) );
1032 long long int len = std::get<1>( vp );
1033
1034 if( !checkColliding( &tmp ) )
1035 {
1036 if( cost < min_cost || ( cost == min_cost && len > max_length ) )
1037 {
1038 l_best = std::get<2>( vp );
1039 p_best = std::get<0>( vp );
1040 found = true;
1041
1042 if( cost <= min_cost )
1043 max_length = std::max<int>( len, max_length );
1044
1045 min_cost = std::min( cost, min_cost );
1046 }
1047 }
1048 }
1049
1050 if( found )
1051 {
1052 aLine->SetShape( l_best );
1053 return p_best;
1054 }
1055
1056 return -1;
1057}
BREAKOUT_LIST computeBreakouts(int aWidth, const ITEM *aItem, bool aPermitDiagonal) const
const SHAPE_LINE_CHAIN Reverse() const
Reverse point order in the line chain.
long long int Length() const
Return length of the line chain in Euclidean metric.

References DIRECTION_45::ANG_ACUTE, DIRECTION_45::ANG_HALF_FULL, DIRECTION_45::ANG_RIGHT, DIRECTION_45::ANG_UNDEFINED, DIRECTION_45::Angle(), SHAPE_LINE_CHAIN::Append(), DIRECTION_45::BuildInitialTrace(), checkColliding(), PNS::LINE::CLine(), SHAPE::Collide(), computeBreakouts(), PNS::COST_ESTIMATOR::CornerCost(), PNS::LINE::CountCorners(), SHAPE_LINE_CHAIN::CPoint(), SHAPE_LINE_CHAIN::CSegment(), PNS::ITEM::Kind(), SHAPE_LINE_CHAIN::Length(), PNS::SOLID::Offset(), SHAPE_LINE_CHAIN::PointCount(), SHAPE_LINE_CHAIN::Reverse(), SHAPE_LINE_CHAIN::SegmentCount(), PNS::LINE::SetShape(), PNS::SOLID::Shape(), SHAPE_LINE_CHAIN::Simplify(), PNS::ITEM::VIA_T, and PNS::LINE::Width().

Referenced by runSmartPads().

Member Data Documentation

◆ m_cache

SHAPE_INDEX_LIST<ITEM*> PNS::OPTIMIZER::m_cache
private

Definition at line 201 of file pns_optimizer.h.

Referenced by cacheAdd(), ClearCache(), and removeCachedSegments().

◆ m_cacheTags

std::unordered_map<ITEM*, CACHED_ITEM> PNS::OPTIMIZER::m_cacheTags
private

Definition at line 203 of file pns_optimizer.h.

Referenced by cacheAdd(), ClearCache(), and removeCachedSegments().

◆ m_collisionKindMask

int PNS::OPTIMIZER::m_collisionKindMask
private

Definition at line 206 of file pns_optimizer.h.

Referenced by checkColliding(), and SetCollisionMask().

◆ m_constraints

std::vector<OPT_CONSTRAINT*> PNS::OPTIMIZER::m_constraints
private

Definition at line 202 of file pns_optimizer.h.

Referenced by AddConstraint(), checkConstraints(), and ClearConstraints().

◆ m_effortLevel

int PNS::OPTIMIZER::m_effortLevel
private

◆ m_preservedVertex

VECTOR2I PNS::OPTIMIZER::m_preservedVertex
private

Definition at line 209 of file pns_optimizer.h.

Referenced by Optimize(), and SetPreserveVertex().

◆ m_restrictArea

BOX2I PNS::OPTIMIZER::m_restrictArea
private

Definition at line 211 of file pns_optimizer.h.

Referenced by Optimize(), and SetRestrictArea().

◆ m_restrictAreaIsStrict

bool PNS::OPTIMIZER::m_restrictAreaIsStrict
private

Definition at line 212 of file pns_optimizer.h.

Referenced by Optimize(), and SetRestrictArea().

◆ m_restrictedVertexRange

std::pair<int, int> PNS::OPTIMIZER::m_restrictedVertexRange
private

Definition at line 210 of file pns_optimizer.h.

Referenced by Optimize(), and SetRestrictVertexRange().

◆ m_world

NODE* PNS::OPTIMIZER::m_world
private

◆ MaxCachedItems

const int PNS::OPTIMIZER::MaxCachedItems = 256
staticprivate

Definition at line 160 of file pns_optimizer.h.


The documentation for this class was generated from the following files: