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,
105  RESTRICT_VERTEX_RANGE = 0x40,
106  MERGE_COLINEAR = 0x80,
107  RESTRICT_AREA = 0x100,
108  LIMIT_CORNER_COUNT = 0x200
109  };
Simplify pad-pad and pad-via connections if possible.
Reduce corner cost iteratively.
Definition: pns_optimizer.h:99
Reduce corner cost by merging obtuse segments.
Do not attempt to optimize if the resulting line's corner count is outside the predefined range.
Merge co-linear segments.
Reroute pad exits.

Constructor & Destructor Documentation

◆ OPTIMIZER()

PNS::OPTIMIZER::OPTIMIZER ( NODE aWorld)

Definition at line 112 of file pns_optimizer.cpp.

112  :
113  m_world( aWorld ),
116  m_restrictAreaIsStrict( false )
117 {
118 }
bool m_restrictAreaIsStrict
Reduce corner cost iteratively.
Definition: pns_optimizer.h:99

◆ ~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 425 of file pns_optimizer.cpp.

426 {
427  m_constraints.push_back( aConstraint );
428 }
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 }
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 408 of file pns_optimizer.cpp.

409 {
410  CACHE_VISITOR v( aItem, m_world, m_collisionKindMask );
411 
412  return static_cast<bool>( m_world->CheckColliding( aItem ) );
413 }
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:450

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 445 of file pns_optimizer.cpp.

446 {
447  LINE tmp( *aLine, aOptPath );
448 
449  return checkColliding( &tmp );
450 }
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 431 of file pns_optimizer.cpp.

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

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 749 of file pns_optimizer.cpp.

751 {
752  BREAKOUT_LIST breakouts;
753 
754  for( int angle = 0; angle < 360; angle += 45 )
755  {
756  const SHAPE_CIRCLE* cir = static_cast<const SHAPE_CIRCLE*>( aShape );
758  VECTOR2I p0 = cir->GetCenter();
759  VECTOR2I v0( cir->GetRadius() * M_SQRT2, 0 );
760 
761  l.Append( p0 );
762  l.Append( p0 + v0.Rotate( angle * M_PI / 180.0 ) );
763  breakouts.push_back( l );
764  }
765 
766  return breakouts;
767 }
int GetRadius() const
Definition: shape_circle.h:107
const VECTOR2I GetCenter() const
Definition: shape_circle.h:112
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
Represent a polyline (an zero-thickness chain of connected line segments).
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
std::vector< SHAPE_LINE_CHAIN > BREAKOUT_LIST

References PNS::angle(), SHAPE_LINE_CHAIN::Append(), SHAPE_CIRCLE::GetCenter(), SHAPE_CIRCLE::GetRadius(), and VECTOR2< T >::Rotate().

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 }
std::unordered_map< ITEM *, CACHED_ITEM > m_cacheTags
SHAPE_INDEX_LIST< ITEM * > m_cache

References m_cache, and m_cacheTags.

◆ ClearConstraints()

void PNS::OPTIMIZER::ClearConstraints ( )

Definition at line 416 of file pns_optimizer.cpp.

417 {
418  for( OPT_CONSTRAINT* c : m_constraints )
419  delete c;
420 
421  m_constraints.clear();
422 }
std::vector< OPT_CONSTRAINT * > m_constraints

References m_constraints.

◆ computeBreakouts()

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

Definition at line 868 of file pns_optimizer.cpp.

870 {
871  switch( aItem->Kind() )
872  {
873  case ITEM::VIA_T:
874  {
875  const VIA* via = static_cast<const VIA*>( aItem );
876  return circleBreakouts( aWidth, via->Shape(), aPermitDiagonal );
877  }
878 
879  case ITEM::SOLID_T:
880  {
881  const SHAPE* shape = aItem->Shape();
882 
883  switch( shape->Type() )
884  {
885  case SH_RECT:
886  return rectBreakouts( aWidth, shape, aPermitDiagonal );
887 
888  case SH_SEGMENT:
889  {
890  const SHAPE_SEGMENT* seg = static_cast<const SHAPE_SEGMENT*> (shape);
891  const SHAPE_RECT rect = ApproximateSegmentAsRect ( *seg );
892  return rectBreakouts( aWidth, &rect, aPermitDiagonal );
893  }
894 
895  case SH_CIRCLE:
896  return circleBreakouts( aWidth, shape, aPermitDiagonal );
897 
898  case SH_SIMPLE:
899  return customBreakouts( aWidth, aItem, aPermitDiagonal );
900 
901  default:
902  break;
903  }
904 
905  break;
906  }
907 
908  default:
909  break;
910  }
911 
912  return BREAKOUT_LIST();
913 }
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
Normal via.
Definition: router_tool.cpp:70
An abstract shape on 2D plane.
Definition: shape.h:116
circle
Definition: shape.h:46
SHAPE_RECT ApproximateSegmentAsRect(const SHAPE_SEGMENT &aSeg)
Definition: pns_utils.cpp:236
axis-aligned rectangle
Definition: shape.h:43
std::vector< SHAPE_LINE_CHAIN > BREAKOUT_LIST
simple polygon
Definition: shape.h:47
SHAPE_TYPE Type() const
Return the type of the shape.
Definition: shape.h:94
line segment
Definition: shape.h:44

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 770 of file pns_optimizer.cpp.

772 {
773  BREAKOUT_LIST breakouts;
774  const SHAPE_SIMPLE* convex = static_cast<const SHAPE_SIMPLE*>( aItem->Shape() );
775 
776  BOX2I bbox = convex->BBox( 0 );
777  VECTOR2I p0 = static_cast<const SOLID*>( aItem )->Pos();
778  // must be large enough to guarantee intersecting the convex polygon
779  int length = std::max( bbox.GetWidth(), bbox.GetHeight() ) / 2 + 5;
780 
781  for( int angle = 0; angle < 360; angle += ( aPermitDiagonal ? 45 : 90 ) )
782  {
784  VECTOR2I v0( p0 + VECTOR2I( length, 0 ).Rotate( angle * M_PI / 180.0 ) );
785  SHAPE_LINE_CHAIN::INTERSECTIONS intersections;
786  int n = convex->Vertices().Intersect( SEG( p0, v0 ), intersections );
787 
788  // if n == 1 intersected a segment
789  // if n == 2 intersected the common point of 2 segments
790  // n == 0 can not happen I think, but...
791  if( n > 0 )
792  {
793  l.Append( p0 );
794 
795  // for a breakout distance relative to the distance between
796  // center and polygon edge
797  //l.Append( intersections[0].p + (v0 - p0).Resize( (intersections[0].p - p0).EuclideanNorm() * 0.4 ) );
798 
799  // for an absolute breakout distance, e.g. 0.1 mm
800  //l.Append( intersections[0].p + (v0 - p0).Resize( 100000 ) );
801 
802  // for the breakout right on the polygon edge
803  l.Append( intersections[0].p );
804 
805  breakouts.push_back( l );
806  }
807  }
808 
809  return breakouts;
810 }
Represent a simple polygon consisting of a zero-thickness closed chain of connected line segments.
Definition: shape_simple.h:41
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Find all intersection points between our line chain and the segment aSeg.
std::vector< INTERSECTION > INTERSECTIONS
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
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
const SHAPE_LINE_CHAIN & Vertices() const
Return the list of vertices defining this simple polygon.
Definition: shape_simple.h:124
Definition: seg.h:40
Represent a polyline (an zero-thickness chain of connected line segments).
static DIRECTION_45::AngleType angle(const VECTOR2I &a, const VECTOR2I &b)
std::vector< SHAPE_LINE_CHAIN > BREAKOUT_LIST

References PNS::angle(), SHAPE_LINE_CHAIN::Append(), SHAPE_SIMPLE::BBox(), SHAPE_LINE_CHAIN::Intersect(), PNS::ITEM::Shape(), and SHAPE_SIMPLE::Vertices().

Referenced by computeBreakouts().

◆ fanoutCleanup()

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

Definition at line 1095 of file pns_optimizer.cpp.

1096 {
1097  if( aLine->PointCount() < 3 )
1098  return false;
1099 
1100  VECTOR2I p_start = aLine->CPoint( 0 ), p_end = aLine->CPoint( -1 );
1101 
1102  ITEM* startPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_start );
1103  ITEM* endPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_end );
1104 
1105  int thr = aLine->Width() * 10;
1106  int len = aLine->CLine().Length();
1107 
1108  if( !startPad )
1109  return false;
1110 
1111  bool startMatch = startPad->OfKind( ITEM::VIA_T | ITEM::SOLID_T );
1112  bool endMatch = false;
1113 
1114  if(endPad)
1115  {
1116  endMatch = endPad->OfKind( ITEM::VIA_T | ITEM::SOLID_T );
1117  }
1118  else
1119  {
1120  endMatch = aLine->EndsWithVia();
1121  }
1122 
1123  if( startMatch && endMatch && len < thr )
1124  {
1125  for( int i = 0; i < 2; i++ )
1126  {
1127  SHAPE_LINE_CHAIN l2 = DIRECTION_45().BuildInitialTrace( p_start, p_end, i );
1128  LINE repl;
1129  repl = LINE( *aLine, l2 );
1130 
1131  if( !m_world->CheckColliding( &repl ) )
1132  {
1133  aLine->SetShape( repl.CLine() );
1134  return true;
1135  }
1136  }
1137  }
1138 
1139  return false;
1140 }
ITEM * findPadOrVia(int aLayer, int aNet, const VECTOR2I &aP) const
Represent route directions & corner angles in a 45-degree metric.
Definition: direction45.h:36
const SHAPE_LINE_CHAIN BuildInitialTrace(const VECTOR2I &aP0, const VECTOR2I &aP1, bool aStartDiagonal=false, bool aFillet=false) const
Build a 2-segment line chain between points aP0 and aP1 and following 45-degree routing regime.
Represent a polyline (an zero-thickness chain of connected line segments).
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:450
bool OfKind(int aKindMask) const
Return true if the item's type matches the mask aKindMask.
Definition: pns_item.h:136

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 916 of file pns_optimizer.cpp.

917 {
918  JOINT* jt = m_world->FindJoint( aP, aLayer, aNet );
919 
920  if( !jt )
921  return nullptr;
922 
923  for( ITEM* item : jt->LinkList() )
924  {
925  if( item->OfKind( ITEM::VIA_T | ITEM::SOLID_T ) )
926  return item;
927  }
928 
929  return nullptr;
930 }
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:1140

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 574 of file pns_optimizer.cpp.

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

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 1317 of file pns_optimizer.cpp.

1318 {
1319  int step_p = aPair->CP().SegmentCount() - 2;
1320  int step_n = aPair->CN().SegmentCount() - 2;
1321 
1322  while( 1 )
1323  {
1324  int n_segs_p = aPair->CP().SegmentCount();
1325  int n_segs_n = aPair->CN().SegmentCount();
1326 
1327  int max_step_p = n_segs_p - 2;
1328  int max_step_n = n_segs_n - 2;
1329 
1330  if( step_p > max_step_p )
1331  step_p = max_step_p;
1332 
1333  if( step_n > max_step_n )
1334  step_n = max_step_n;
1335 
1336  if( step_p < 1 && step_n < 1 )
1337  break;
1338 
1339  bool found_anything_p = false;
1340  bool found_anything_n = false;
1341 
1342  if( step_p > 1 )
1343  found_anything_p = mergeDpStep( aPair, true, step_p );
1344 
1345  if( step_n > 1 )
1346  found_anything_n = mergeDpStep( aPair, false, step_n );
1347 
1348  if( !found_anything_n && !found_anything_p )
1349  {
1350  step_n--;
1351  step_p--;
1352  }
1353  }
1354  return true;
1355 }
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 1254 of file pns_optimizer.cpp.

1255 {
1256  int n = 1;
1257 
1258  SHAPE_LINE_CHAIN currentPath = aTryP ? aPair->CP() : aPair->CN();
1259  SHAPE_LINE_CHAIN coupledPath = aTryP ? aPair->CN() : aPair->CP();
1260 
1261  int n_segs = currentPath.SegmentCount() - 1;
1262 
1263  int64_t clenPre = aPair->CoupledLength( currentPath, coupledPath );
1264  int64_t budget = clenPre / 10; // fixme: come up with something more intelligent here...
1265 
1266  while( n < n_segs - step )
1267  {
1268  const SEG s1 = currentPath.CSegment( n );
1269  const SEG s2 = currentPath.CSegment( n + step );
1270 
1271  DIRECTION_45 dir1( s1 );
1272  DIRECTION_45 dir2( s2 );
1273 
1274  if( dir1.IsObtuse( dir2 ) )
1275  {
1276  SHAPE_LINE_CHAIN bypass = DIRECTION_45().BuildInitialTrace( s1.A, s2.B,
1277  dir1.IsDiagonal() );
1278  SHAPE_LINE_CHAIN newRef;
1279  SHAPE_LINE_CHAIN newCoup;
1280  int64_t deltaCoupled = -1, deltaUni = -1;
1281 
1282  newRef = currentPath;
1283  newRef.Replace( s1.Index(), s2.Index(), bypass );
1284 
1285  deltaUni = aPair->CoupledLength ( newRef, coupledPath ) - clenPre + budget;
1286 
1287  if( coupledBypass( m_world, aPair, aTryP, newRef, bypass, coupledPath, newCoup ) )
1288  {
1289  deltaCoupled = aPair->CoupledLength( newRef, newCoup ) - clenPre + budget;
1290 
1291  if( deltaCoupled >= 0 )
1292  {
1293  newRef.Simplify();
1294  newCoup.Simplify();
1295 
1296  aPair->SetShape( newRef, newCoup, !aTryP );
1297  return true;
1298  }
1299  }
1300  else if( deltaUni >= 0 && verifyDpBypass( m_world, aPair, aTryP, newRef, coupledPath ) )
1301  {
1302  newRef.Simplify();
1303  coupledPath.Simplify();
1304 
1305  aPair->SetShape( newRef, coupledPath, !aTryP );
1306  return true;
1307  }
1308  }
1309 
1310  n++;
1311  }
1312 
1313  return false;
1314 }
int Index() const
Return the index of this segment in its parent shape (applicable only to non-local segments).
Definition: seg.h:368
SHAPE_LINE_CHAIN & Simplify(bool aRemoveColinear=true)
Simplify the line chain by removing colinear adjacent segments and duplicate vertices.
Represent route directions & corner angles in a 45-degree metric.
Definition: direction45.h:36
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)
const SHAPE_LINE_CHAIN BuildInitialTrace(const VECTOR2I &aP0, const VECTOR2I &aP1, bool aStartDiagonal=false, bool aFillet=false) const
Build a 2-segment line chain between points aP0 and aP1 and following 45-degree routing regime.
int SegmentCount() const
Return the number of segments in this line chain.
Definition: seg.h:40
bool verifyDpBypass(NODE *aNode, DIFF_PAIR *aPair, bool aRefIsP, const SHAPE_LINE_CHAIN &aNewRef, const SHAPE_LINE_CHAIN &aNewCoupled)
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
Represent a polyline (an zero-thickness chain of connected line segments).
VECTOR2I A
Definition: seg.h:48
void Replace(int aStartIndex, int aEndIndex, const VECTOR2I &aP)
Replace points with indices in range [start_index, end_index] with a single point aP.
VECTOR2I B
Definition: seg.h:49

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 534 of file pns_optimizer.cpp.

535 {
536  SHAPE_LINE_CHAIN& line = aLine->Line();
537  int step = line.SegmentCount() - 1;
538 
539  int segs_pre = line.SegmentCount();
540 
541  line.Simplify();
542 
543  if( step < 0 )
544  return false;
545 
546  SHAPE_LINE_CHAIN current_path( line );
547 
548  while( 1 )
549  {
550  int n_segs = current_path.SegmentCount();
551  int max_step = n_segs - 2;
552 
553  if( step > max_step )
554  step = max_step;
555 
556  if( step < 1 )
557  break;
558 
559  bool found_anything = mergeStep( aLine, current_path, step );
560 
561  if( !found_anything )
562  step--;
563 
564  if( !step )
565  break;
566  }
567 
568  aLine->SetShape( current_path );
569 
570  return current_path.SegmentCount() < segs_pre;
571 }
SHAPE_LINE_CHAIN & Simplify(bool aRemoveColinear=true)
Simplify the line chain by removing colinear adjacent segments and duplicate vertices.
bool mergeStep(LINE *aLine, SHAPE_LINE_CHAIN &aCurrentLine, int step)
int SegmentCount() const
Return the number of segments in this line chain.
Represent a polyline (an zero-thickness chain of connected line segments).

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 453 of file pns_optimizer.cpp.

454 {
455  SHAPE_LINE_CHAIN& line = aLine->Line();
456 
457  int step = line.PointCount() - 3;
458  int iter = 0;
459  int segs_pre = line.SegmentCount();
460 
461  if( step < 0 )
462  return false;
463 
464  SHAPE_LINE_CHAIN current_path( line );
465 
466  while( 1 )
467  {
468  iter++;
469  int n_segs = current_path.SegmentCount();
470  int max_step = n_segs - 2;
471 
472  if( step > max_step )
473  step = max_step;
474 
475  if( step < 2 )
476  {
477  line = current_path;
478  return current_path.SegmentCount() < segs_pre;
479  }
480 
481  bool found_anything = false;
482 
483  for( int n = 0; n < n_segs - step; n++ )
484  {
485  const SEG s1 = current_path.CSegment( n );
486  const SEG s2 = current_path.CSegment( n + step );
487  SEG s1opt, s2opt;
488 
489  if( DIRECTION_45( s1 ).IsObtuse( DIRECTION_45( s2 ) ) )
490  {
491  VECTOR2I ip = *s1.IntersectLines( s2 );
492 
493  s1opt = SEG( s1.A, ip );
494  s2opt = SEG( ip, s2.B );
495 
496  if( DIRECTION_45( s1opt ).IsObtuse( DIRECTION_45( s2opt ) ) )
497  {
498  SHAPE_LINE_CHAIN opt_path;
499  opt_path.Append( s1opt.A );
500  opt_path.Append( s1opt.B );
501  opt_path.Append( s2opt.B );
502 
503  LINE opt_track( *aLine, opt_path );
504 
505  if( !checkColliding( &opt_track ) )
506  {
507  current_path.Replace( s1.Index() + 1, s2.Index(), ip );
508 
509  // removeCachedSegments(aLine, s1.Index(), s2.Index());
510  n_segs = current_path.SegmentCount();
511  found_anything = true;
512  break;
513  }
514  }
515  }
516  }
517 
518  if( !found_anything )
519  {
520  if( step <= 2 )
521  {
522  line = current_path;
523  return line.SegmentCount() < segs_pre;
524  }
525 
526  step--;
527  }
528  }
529 
530  return line.SegmentCount() < segs_pre;
531 }
int Index() const
Return the index of this segment in its parent shape (applicable only to non-local segments).
Definition: seg.h:368
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Compute the intersection point of lines passing through ends of (this) and aSeg.
Definition: seg.h:209
int PointCount() const
Return the number of points (vertices) in this line chain.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
Represent route directions & corner angles in a 45-degree metric.
Definition: direction45.h:36
int SegmentCount() const
Return the number of segments in this line chain.
Definition: seg.h:40
Represent a polyline (an zero-thickness chain of connected line segments).
VECTOR2I A
Definition: seg.h:48
bool checkColliding(ITEM *aItem, bool aUpdateCache=true)
VECTOR2I B
Definition: seg.h:49

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 681 of file pns_optimizer.cpp.

682 {
683  int n_segs = aCurrentPath.SegmentCount();
684 
685  int cost_orig = COST_ESTIMATOR::CornerCost( aCurrentPath );
686 
687  if( aLine->SegmentCount() < 2 )
688  return false;
689 
690  DIRECTION_45 orig_start( aLine->CSegment( 0 ) );
691  DIRECTION_45 orig_end( aLine->CSegment( -1 ) );
692 
693 
694  for( int n = 0; n < n_segs - step; n++ )
695  {
696  // Do not attempt to merge false segments that are part of an arc
697  if( aCurrentPath.IsArcSegment( n )
698  || aCurrentPath.IsArcSegment( static_cast<std::size_t>( n ) + step ) )
699  {
700  continue;
701  }
702 
703  const SEG s1 = aCurrentPath.CSegment( n );
704  const SEG s2 = aCurrentPath.CSegment( n + step );
705 
707  SHAPE_LINE_CHAIN* picked = nullptr;
708  int cost[2];
709 
710  for( int i = 0; i < 2; i++ )
711  {
712  SHAPE_LINE_CHAIN bypass = DIRECTION_45().BuildInitialTrace( s1.A, s2.B, i );
713  cost[i] = INT_MAX;
714 
715  bool ok = false;
716 
717  if( !checkColliding( aLine, bypass ) )
718  {
719  //printf("Chk-constraints: %d %d\n", n, n+step+1 );
720  ok = checkConstraints ( n, n + step + 1, aLine, aCurrentPath, bypass );
721  }
722 
723  if( ok )
724  {
725  path[i] = aCurrentPath;
726  path[i].Replace( s1.Index(), s2.Index(), bypass );
727  path[i].Simplify();
728  cost[i] = COST_ESTIMATOR::CornerCost( path[i] );
729  }
730  }
731 
732  if( cost[0] < cost_orig && cost[0] < cost[1] )
733  picked = &path[0];
734  else if( cost[1] < cost_orig )
735  picked = &path[1];
736 
737  if( picked )
738  {
739  n_segs = aCurrentPath.SegmentCount();
740  aCurrentPath = *picked;
741  return true;
742  }
743  }
744 
745  return false;
746 }
int Index() const
Return the index of this segment in its parent shape (applicable only to non-local segments).
Definition: seg.h:368
bool checkConstraints(int aVertex1, int aVertex2, LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement)
Represent route directions & corner angles in a 45-degree metric.
Definition: direction45.h:36
const SHAPE_LINE_CHAIN BuildInitialTrace(const VECTOR2I &aP0, const VECTOR2I &aP1, bool aStartDiagonal=false, bool aFillet=false) const
Build a 2-segment line chain between points aP0 and aP1 and following 45-degree routing regime.
Definition: seg.h:40
Represent a polyline (an zero-thickness chain of connected line segments).
VECTOR2I A
Definition: seg.h:48
bool checkColliding(ITEM *aItem, bool aUpdateCache=true)
static int CornerCost(const SEG &aA, const SEG &aB)
VECTOR2I B
Definition: seg.h:49

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, PNS::LINE::SegmentCount(), and SHAPE_LINE_CHAIN::SegmentCount().

Referenced by mergeFull().

◆ Optimize() [1/3]

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

Definition at line 1081 of file pns_optimizer.cpp.

1082 {
1083  OPTIMIZER opt( aWorld );
1084 
1085  opt.SetEffortLevel( aEffortLevel );
1086  opt.SetCollisionMask( -1 );
1087 
1088  if( aEffortLevel & OPTIMIZER::PRESERVE_VERTEX )
1089  opt.SetPreserveVertex( aV );
1090 
1091  return opt.Optimize( aLine );
1092 }
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() [2/3]

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

Definition at line 601 of file pns_optimizer.cpp.

602 {
603  DEBUG_DECORATOR* dbg = ROUTER::GetInstance()->GetInterface()->GetDebugDecorator();
604 
605  if( aRoot )
606  {
607  PNS_DBG( dbg, AddLine, aRoot->CLine(), BLUE, 100000, "root-line" );
608  }
609 
610 
611  if( !aResult )
612  {
613  aResult = aLine;
614  }
615  else
616  {
617  *aResult = *aLine;
618  aResult->ClearLinks();
619  }
620 
621  bool hasArcs = aLine->ArcCount();
622  bool rv = false;
623 
624  if( (m_effortLevel & LIMIT_CORNER_COUNT) && aRoot )
625  {
626  const int angleMask = DIRECTION_45::ANG_OBTUSE;
627  int rootObtuseCorners = aRoot->CountCorners( angleMask );
628  auto c = new CORNER_COUNT_LIMIT_CONSTRAINT( m_world, rootObtuseCorners,
629  aLine->SegmentCount(), angleMask );
630  AddConstraint( c );
631  }
632 
634  {
635  auto c = new PRESERVE_VERTEX_CONSTRAINT( m_world, m_preservedVertex );
636  AddConstraint( c );
637  }
638 
640  {
641  auto c = new RESTRICT_VERTEX_RANGE_CONSTRAINT( m_world, m_restrictedVertexRange.first,
642  m_restrictedVertexRange.second );
643  AddConstraint( c );
644  }
645 
647  {
648  auto c = new AREA_CONSTRAINT( m_world, m_restrictArea, m_restrictAreaIsStrict );
649  AddConstraint( c );
650  }
651 
653  {
654  auto c = new KEEP_TOPOLOGY_CONSTRAINT( m_world );
655  AddConstraint( c );
656  }
657 
658  // TODO: Fix for arcs
659  if( !hasArcs && m_effortLevel & MERGE_SEGMENTS )
660  rv |= mergeFull( aResult );
661 
662  // TODO: Fix for arcs
663  if( !hasArcs && m_effortLevel & MERGE_OBTUSE )
664  rv |= mergeObtuse( aResult );
665 
667  rv |= mergeColinear( aResult );
668 
669  // TODO: Fix for arcs
670  if( !hasArcs && m_effortLevel & SMART_PADS )
671  rv |= runSmartPads( aResult );
672 
673  // TODO: Fix for arcs
674  if( !hasArcs && m_effortLevel & FANOUT_CLEANUP )
675  rv |= fanoutCleanup( aResult );
676 
677  return rv;
678 }
Simplify pad-pad and pad-via connections if possible.
bool runSmartPads(LINE *aLine)
bool mergeFull(LINE *aLine)
bool m_restrictAreaIsStrict
void AddConstraint(OPT_CONSTRAINT *aConstraint)
#define PNS_DBG(dbg, method,...)
bool mergeObtuse(LINE *aLine)
Reduce corner cost iteratively.
Definition: pns_optimizer.h:99
VECTOR2I m_preservedVertex
virtual DEBUG_DECORATOR * GetDebugDecorator()=0
Definition: color4d.h:56
Reduce corner cost by merging obtuse segments.
bool fanoutCleanup(LINE *aLine)
Do not attempt to optimize if the resulting line's corner count is outside the predefined range.
bool mergeColinear(LINE *aLine)
Merge co-linear segments.
std::pair< int, int > m_restrictedVertexRange
ROUTER_IFACE * GetInterface() const
Definition: pns_router.h:207
static ROUTER * GetInstance()
Definition: pns_router.cpp:78
Reroute pad exits.

References AddConstraint(), DIRECTION_45::ANG_OBTUSE, PNS::LINE::ArcCount(), BLUE, PNS::LINK_HOLDER::ClearLinks(), PNS::LINE::CLine(), 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.

◆ Optimize() [3/3]

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

Definition at line 1358 of file pns_optimizer.cpp.

1359 {
1360  return mergeDpSegments( aPair );
1361 }
bool mergeDpSegments(DIFF_PAIR *aPair)

References mergeDpSegments().

◆ rectBreakouts()

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

Definition at line 813 of file pns_optimizer.cpp.

815 {
816  const SHAPE_RECT* rect = static_cast<const SHAPE_RECT*>(aShape);
817  VECTOR2I s = rect->GetSize();
818  VECTOR2I c = rect->GetPosition() + VECTOR2I( s.x / 2, s.y / 2 );
819  BREAKOUT_LIST breakouts;
820 
821  VECTOR2I d_offset;
822 
823  d_offset.x = ( s.x > s.y ) ? ( s.x - s.y ) / 2 : 0;
824  d_offset.y = ( s.x < s.y ) ? ( s.y - s.x ) / 2 : 0;
825 
826  VECTOR2I d_vert = VECTOR2I( 0, s.y / 2 + aWidth );
827  VECTOR2I d_horiz = VECTOR2I( s.x / 2 + aWidth, 0 );
828 
829  breakouts.emplace_back( SHAPE_LINE_CHAIN( { c, c + d_horiz } ) );
830  breakouts.emplace_back( SHAPE_LINE_CHAIN( { c, c - d_horiz } ) );
831  breakouts.emplace_back( SHAPE_LINE_CHAIN( { c, c + d_vert } ) );
832  breakouts.emplace_back( SHAPE_LINE_CHAIN( { c, c - d_vert } ) );
833 
834  if( aPermitDiagonal )
835  {
836  int l = aWidth + std::min( s.x, s.y ) / 2;
837  VECTOR2I d_diag;
838 
839  if( s.x >= s.y )
840  {
841  breakouts.emplace_back(
842  SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( l, l ) } ) );
843  breakouts.emplace_back(
844  SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset - VECTOR2I( -l, l ) } ) );
845  breakouts.emplace_back(
846  SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset + VECTOR2I( -l, l ) } ) );
847  breakouts.emplace_back(
848  SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( l, l ) } ) );
849  }
850  else
851  {
852  // fixme: this could be done more efficiently
853  breakouts.emplace_back(
854  SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( l, l ) } ) );
855  breakouts.emplace_back(
856  SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( -l, l ) } ) );
857  breakouts.emplace_back(
858  SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( -l, l ) } ) );
859  breakouts.emplace_back(
860  SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( l, l ) } ) );
861  }
862  }
863 
864  return breakouts;
865 }
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
const VECTOR2I GetSize() const
Definition: shape_rect.h:124
const VECTOR2I & GetPosition() const
Definition: shape_rect.h:116
Represent a polyline (an zero-thickness chain of connected line segments).
std::vector< SHAPE_LINE_CHAIN > BREAKOUT_LIST

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 }
std::unordered_map< ITEM *, CACHED_ITEM > m_cacheTags
SHAPE_INDEX_LIST< ITEM * > m_cache

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 1054 of file pns_optimizer.cpp.

1055 {
1056  SHAPE_LINE_CHAIN& line = aLine->Line();
1057 
1058  if( line.PointCount() < 3 )
1059  return false;
1060 
1061  VECTOR2I p_start = line.CPoint( 0 ), p_end = line.CPoint( -1 );
1062 
1063  ITEM* startPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_start );
1064  ITEM* endPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_end );
1065 
1066  int vtx = -1;
1067 
1068  if( startPad )
1069  vtx = smartPadsSingle( aLine, startPad, false, 3 );
1070 
1071  if( endPad )
1072  smartPadsSingle( aLine, endPad, true,
1073  vtx < 0 ? line.PointCount() - 1 : line.PointCount() - 1 - vtx );
1074 
1075  aLine->Line().Simplify();
1076 
1077  return true;
1078 }
ITEM * findPadOrVia(int aLayer, int aNet, const VECTOR2I &aP) const
int PointCount() const
Return the number of points (vertices) in this line chain.
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
Represent a polyline (an zero-thickness chain of connected line segments).
int smartPadsSingle(LINE *aLine, ITEM *aPad, bool aEnd, int aEndVertex)

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  }
bool m_restrictAreaIsStrict

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  }
std::pair< int, int > m_restrictedVertexRange

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 933 of file pns_optimizer.cpp.

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

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::CSegment(), PNS::ITEM::Kind(), PNS::SOLID::Offset(), 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: