KiCad PCB EDA Suite
PNS::OPTIMIZER Class Reference

OPTIMIZER. 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
}
 

Public Member Functions

 OPTIMIZER (NODE *aWorld)
 Optimizer. More...
 
 ~OPTIMIZER ()
 
bool Optimize (LINE *aLine, LINE *aResult=NULL)
 
bool Optimize (DIFF_PAIR *aPair)
 
void SetWorld (NODE *aNode)
 
void CacheStaticItem (ITEM *aItem)
 
void CacheRemove (ITEM *aItem)
 
void ClearCache (bool aStaticOnly=false)
 
void SetCollisionMask (int aMask)
 
void SetEffortLevel (int aEffort)
 
void SetRestrictArea (const BOX2I &aArea)
 
void ClearConstraints ()
 
void AddConstraint (OPT_CONSTRAINT *aConstraint)
 
bool extractPadGrids (std::vector< JOINT * > &aPadJoints)
 
void BuildPadGrids ()
 

Static Public Member Functions

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

a quick shortcut to optmize a line without creating and setting up an optimizer

More...
 

Private Types

typedef std::vector< SHAPE_LINE_CHAINBREAKOUT_LIST
 
typedef std::unordered_map< ITEM *, CACHED_ITEMCachedItemTags
 

Private Member Functions

bool mergeObtuse (LINE *aLine)
 
bool mergeFull (LINE *aLine)
 
bool removeUglyCorners (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 ovalBreakouts (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
 
CachedItemTags m_cacheTags
 
NODEm_world
 
int m_collisionKindMask
 
int m_effortLevel
 
bool m_keepPostures
 
BOX2I m_restrictArea
 
bool m_restrictAreaActive
 

Static Private Attributes

static const int MaxCachedItems = 256
 

Detailed Description

OPTIMIZER.

Performs various optimizations of the lines being routed, attempting to make the lines shorter and less cornery. There are 3 kinds of optimizations so far:

  • 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 95 of file pns_optimizer.h.

Member Typedef Documentation

◆ BREAKOUT_LIST

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

Definition at line 149 of file pns_optimizer.h.

◆ CachedItemTags

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

Definition at line 191 of file pns_optimizer.h.

Member Enumeration Documentation

◆ OptimizationEffort

Enumerator
MERGE_SEGMENTS 
SMART_PADS 
MERGE_OBTUSE 
FANOUT_CLEANUP 
KEEP_TOPOLOGY 
PRESERVE_VERTEX 

Definition at line 98 of file pns_optimizer.h.

Constructor & Destructor Documentation

◆ OPTIMIZER()

PNS::OPTIMIZER::OPTIMIZER ( NODE aWorld)

Optimizer.

Definition at line 133 of file pns_optimizer.cpp.

133  :
134  m_world( aWorld ),
137  m_keepPostures( false ),
138  m_restrictAreaActive( false )
139 {
140 }
bool m_restrictAreaActive

◆ ~OPTIMIZER()

PNS::OPTIMIZER::~OPTIMIZER ( )

Definition at line 143 of file pns_optimizer.cpp.

144 {
145 }

Member Function Documentation

◆ AddConstraint()

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

Definition at line 445 of file pns_optimizer.cpp.

446  {
447  m_constraints.push_back(aConstraint);
448 }
std::vector< OPT_CONSTRAINT * > m_constraints

References m_constraints.

Referenced by Optimize().

◆ BuildPadGrids()

void PNS::OPTIMIZER::BuildPadGrids ( )

◆ cacheAdd()

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

Definition at line 178 of file pns_optimizer.cpp.

179 {
180  if( m_cacheTags.find( aItem ) != m_cacheTags.end() )
181  return;
182 
183  m_cache.Add( aItem );
184  m_cacheTags[aItem].m_hits = 1;
185  m_cacheTags[aItem].m_isStatic = aIsStatic;
186 }
CachedItemTags m_cacheTags
SHAPE_INDEX_LIST< ITEM * > m_cache

References m_cache, and m_cacheTags.

Referenced by CacheStaticItem().

◆ CacheRemove()

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

Definition at line 207 of file pns_optimizer.cpp.

208 {
209  if( aItem->Kind() == ITEM::LINE_T )
210  removeCachedSegments( static_cast<LINE*>( aItem ) );
211 }
void removeCachedSegments(LINE *aLine, int aStartVertex=0, int aEndVertex=-1)

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

◆ CacheStaticItem()

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

Definition at line 214 of file pns_optimizer.cpp.

215 {
216  cacheAdd( aItem, true );
217 }
void cacheAdd(ITEM *aItem, bool aIsStatic)

References cacheAdd().

◆ checkColliding() [1/2]

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

Definition at line 431 of file pns_optimizer.cpp.

432 {
433  CACHE_VISITOR v( aItem, m_world, m_collisionKindMask );
434 
435  return static_cast<bool>( m_world->CheckColliding( aItem ) );
436 }
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Function CheckColliding()
Definition: pns_node.cpp:427

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

465 {
466  LINE tmp( *aLine, aOptPath );
467 
468  return checkColliding( &tmp );
469 }
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 450 of file pns_optimizer.cpp.

451 {
452  for( auto c: m_constraints)
453  {
454  if ( !c->Check( aVertex1, aVertex2, aOriginLine, aCurrentPath, aReplacement ) )
455  {
456  return false;
457  }
458  }
459 
460  return true;
461 }
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 686 of file pns_optimizer.cpp.

688 {
689  BREAKOUT_LIST breakouts;
690 
691  for( int angle = 0; angle < 360; angle += 45 )
692  {
693  const SHAPE_CIRCLE* cir = static_cast<const SHAPE_CIRCLE*>( aShape );
695  VECTOR2I p0 = cir->GetCenter();
696  VECTOR2I v0( cir->GetRadius() * M_SQRT2, 0 );
697  l.Append( p0 );
698  l.Append( p0 + v0.Rotate( angle * M_PI / 180.0 ) );
699  breakouts.push_back( l );
700  }
701 
702  return breakouts;
703 }
int GetRadius() const
Definition: shape_circle.h:94
const VECTOR2I GetCenter() const
Definition: shape_circle.h:99
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
SHAPE_LINE_CHAIN.
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 220 of file pns_optimizer.cpp.

221 {
222  if( !aStaticOnly )
223  {
224  m_cacheTags.clear();
225  m_cache.Clear();
226  return;
227  }
228 
229  for( CachedItemTags::iterator i = m_cacheTags.begin(); i!= m_cacheTags.end(); ++i )
230  {
231  if( i->second.m_isStatic )
232  {
233  m_cache.Remove( i->first );
234  m_cacheTags.erase( i->first );
235  }
236  }
237 }
CachedItemTags m_cacheTags
SHAPE_INDEX_LIST< ITEM * > m_cache

References m_cache, and m_cacheTags.

◆ ClearConstraints()

void PNS::OPTIMIZER::ClearConstraints ( )

Definition at line 438 of file pns_optimizer.cpp.

439 {
440  for (auto c : m_constraints)
441  delete c;
442  m_constraints.clear();
443 }
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 803 of file pns_optimizer.cpp.

805 {
806  switch( aItem->Kind() )
807  {
808  case ITEM::VIA_T:
809  {
810  const VIA* via = static_cast<const VIA*>( aItem );
811  return circleBreakouts( aWidth, via->Shape(), aPermitDiagonal );
812  }
813 
814  case ITEM::SOLID_T:
815  {
816  const SHAPE* shape = aItem->Shape();
817 
818  switch( shape->Type() )
819  {
820  case SH_RECT:
821  return rectBreakouts( aWidth, shape, aPermitDiagonal );
822 
823  case SH_SEGMENT:
824  {
825  const SHAPE_SEGMENT* seg = static_cast<const SHAPE_SEGMENT*> (shape);
826  const SHAPE_RECT rect = ApproximateSegmentAsRect ( *seg );
827  return rectBreakouts( aWidth, &rect, aPermitDiagonal );
828  }
829 
830  case SH_CIRCLE:
831  return circleBreakouts( aWidth, shape, aPermitDiagonal );
832 
833  case SH_SIMPLE:
834  return customBreakouts( aWidth, aItem, aPermitDiagonal );
835 
836  default:
837  break;
838  }
839 
840  break;
841  }
842 
843  default:
844  break;
845  }
846 
847  return BREAKOUT_LIST();
848 }
BREAKOUT_LIST circleBreakouts(int aWidth, const SHAPE *aShape, bool aPermitDiagonal) const
SHAPE_TYPE Type() const
Function Type()
Definition: shape.h:99
BREAKOUT_LIST rectBreakouts(int aWidth, const SHAPE *aShape, bool aPermitDiagonal) const
BREAKOUT_LIST customBreakouts(int aWidth, const ITEM *aItem, bool aPermitDiagonal) const
SHAPE.
Definition: shape.h:74
line chain (polyline)
Definition: shape.h:44
SHAPE_RECT ApproximateSegmentAsRect(const SHAPE_SEGMENT &aSeg)
Definition: pns_utils.cpp:216
Definition: shape.h:41
std::vector< SHAPE_LINE_CHAIN > BREAKOUT_LIST
circle
Definition: shape.h:45
axis-aligned rectangle
Definition: shape.h:42

References PNS::ApproximateSegmentAsRect(), circleBreakouts(), customBreakouts(), PNS::ITEM::Kind(), rectBreakouts(), SH_CIRCLE, SH_RECT, SH_SEGMENT, SH_SIMPLE, PNS::VIA::Shape(), PNS::ITEM::Shape(), PNS::ITEM::SOLID_T, SHAPE::Type(), 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 706 of file pns_optimizer.cpp.

708 {
709  BREAKOUT_LIST breakouts;
710  const SHAPE_SIMPLE* convex = static_cast<const SHAPE_SIMPLE*>( aItem->Shape() );
711 
712  BOX2I bbox = convex->BBox( 0 );
713  VECTOR2I p0 = static_cast<const SOLID*>( aItem )->Pos();
714  // must be large enough to guarantee intersecting the convex polygon
715  int length = std::max( bbox.GetWidth(), bbox.GetHeight() ) / 2 + 5;
716 
717  for( int angle = 0; angle < 360; angle += ( aPermitDiagonal ? 45 : 90 ) )
718  {
720  VECTOR2I v0( p0 + VECTOR2I( length, 0 ).Rotate( angle * M_PI / 180.0 ) );
721  SHAPE_LINE_CHAIN::INTERSECTIONS intersections;
722  int n = convex->Vertices().Intersect( SEG( p0, v0 ), intersections );
723  // if n == 1 intersected a segment
724  // if n == 2 intersected the common point of 2 segments
725  // n == 0 can not happen I think, but...
726  if( n > 0 )
727  {
728  l.Append( p0 );
729 
730  // for a breakout distance relative to the distance between
731  // center and polygon edge
732  //l.Append( intersections[0].p + (v0 - p0).Resize( (intersections[0].p - p0).EuclideanNorm() * 0.4 ) );
733 
734  // for an absolute breakout distance, e.g. 0.1 mm
735  //l.Append( intersections[0].p + (v0 - p0).Resize( 100000 ) );
736 
737  // for the breakout right on the polygon edge
738  l.Append( intersections[0].p );
739 
740  breakouts.push_back( l );
741  }
742  }
743 
744  return breakouts;
745 }
SHAPE_SIMPLE.
Definition: shape_simple.h:42
int Intersect(const SEG &aSeg, INTERSECTIONS &aIp) const
Function Intersect()
std::vector< INTERSECTION > INTERSECTIONS
const BOX2I BBox(int aClearance=0) const override
Function BBox()
Definition: shape_simple.h:81
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
const SHAPE_LINE_CHAIN & Vertices() const
Function Vertices()
Definition: shape_simple.h:132
Definition: seg.h:39
SHAPE_LINE_CHAIN.
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().

◆ extractPadGrids()

bool PNS::OPTIMIZER::extractPadGrids ( std::vector< JOINT * > &  aPadJoints)

◆ fanoutCleanup()

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

Definition at line 1035 of file pns_optimizer.cpp.

1036 {
1037  if( aLine->PointCount() < 3 )
1038  return false;
1039 
1040  VECTOR2I p_start = aLine->CPoint( 0 ), p_end = aLine->CPoint( -1 );
1041 
1042  ITEM* startPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_start );
1043  ITEM* endPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_end );
1044 
1045  int thr = aLine->Width() * 10;
1046  int len = aLine->CLine().Length();
1047 
1048  if( !startPad )
1049  return false;
1050 
1051  bool startMatch = startPad->OfKind( ITEM::VIA_T | ITEM::SOLID_T );
1052  bool endMatch = false;
1053 
1054  if(endPad)
1055  {
1056  endMatch = endPad->OfKind( ITEM::VIA_T | ITEM::SOLID_T );
1057  }
1058  else
1059  {
1060  endMatch = aLine->EndsWithVia();
1061  }
1062 
1063  if( startMatch && endMatch && len < thr )
1064  {
1065  for( int i = 0; i < 2; i++ )
1066  {
1067  SHAPE_LINE_CHAIN l2 = DIRECTION_45().BuildInitialTrace( p_start, p_end, i );
1068  LINE repl;
1069  repl = LINE( *aLine, l2 );
1070 
1071  if( !m_world->CheckColliding( &repl ) )
1072  {
1073  aLine->SetShape( repl.CLine() );
1074  return true;
1075  }
1076  }
1077  }
1078 
1079  return false;
1080 }
ITEM * findPadOrVia(int aLayer, int aNet, const VECTOR2I &aP) const
DIRECTION_45.
Definition: direction45.h:37
const SHAPE_LINE_CHAIN BuildInitialTrace(const VECTOR2I &aP0, const VECTOR2I &aP1, bool aStartDiagonal=false, int aMaxRadius=0) const
Function BuildInitialTrace()
SHAPE_LINE_CHAIN.
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Function CheckColliding()
Definition: pns_node.cpp:427
bool OfKind(int aKindMask) const
Function OfKind()
Definition: pns_item.h:133

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

852 {
853  JOINT* jt = m_world->FindJoint( aP, aLayer, aNet );
854 
855  if( !jt )
856  return NULL;
857 
858  for( ITEM* item : jt->LinkList() )
859  {
860  if( item->OfKind( ITEM::VIA_T | ITEM::SOLID_T ) )
861  return item;
862  }
863 
864  return NULL;
865 }
#define NULL
JOINT * FindJoint(const VECTOR2I &aPos, int aLayer, int aNet)
Function FindJoint()
Definition: pns_node.cpp:1027

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

Referenced by fanoutCleanup(), and runSmartPads().

◆ mergeDpSegments()

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

Definition at line 1248 of file pns_optimizer.cpp.

1249 {
1250  int step_p = aPair->CP().SegmentCount() - 2;
1251  int step_n = aPair->CN().SegmentCount() - 2;
1252 
1253  while( 1 )
1254  {
1255  int n_segs_p = aPair->CP().SegmentCount();
1256  int n_segs_n = aPair->CN().SegmentCount();
1257 
1258  int max_step_p = n_segs_p - 2;
1259  int max_step_n = n_segs_n - 2;
1260 
1261  if( step_p > max_step_p )
1262  step_p = max_step_p;
1263 
1264  if( step_n > max_step_n )
1265  step_n = max_step_n;
1266 
1267  if( step_p < 1 && step_n < 1)
1268  break;
1269 
1270  bool found_anything_p = false;
1271  bool found_anything_n = false;
1272 
1273  if( step_p > 1 )
1274  found_anything_p = mergeDpStep( aPair, true, step_p );
1275 
1276  if( step_n > 1 )
1277  found_anything_n = mergeDpStep( aPair, false, step_n );
1278 
1279  if( !found_anything_n && !found_anything_p )
1280  {
1281  step_n--;
1282  step_p--;
1283  }
1284  }
1285  return true;
1286 }
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 1186 of file pns_optimizer.cpp.

1187 {
1188  int n = 1;
1189 
1190  SHAPE_LINE_CHAIN currentPath = aTryP ? aPair->CP() : aPair->CN();
1191  SHAPE_LINE_CHAIN coupledPath = aTryP ? aPair->CN() : aPair->CP();
1192 
1193  int n_segs = currentPath.SegmentCount() - 1;
1194 
1195  int64_t clenPre = aPair->CoupledLength( currentPath, coupledPath );
1196  int64_t budget = clenPre / 10; // fixme: come up with somethig more intelligent here...
1197 
1198  while( n < n_segs - step )
1199  {
1200  const SEG s1 = currentPath.CSegment( n );
1201  const SEG s2 = currentPath.CSegment( n + step );
1202 
1203  DIRECTION_45 dir1( s1 );
1204  DIRECTION_45 dir2( s2 );
1205 
1206  if( dir1.IsObtuse( dir2 ) )
1207  {
1208  SHAPE_LINE_CHAIN bypass = DIRECTION_45().BuildInitialTrace( s1.A, s2.B, dir1.IsDiagonal() );
1209  SHAPE_LINE_CHAIN newRef;
1210  SHAPE_LINE_CHAIN newCoup;
1211  int64_t deltaCoupled = -1, deltaUni = -1;
1212 
1213  newRef = currentPath;
1214  newRef.Replace( s1.Index(), s2.Index(), bypass );
1215 
1216  deltaUni = aPair->CoupledLength ( newRef, coupledPath ) - clenPre + budget;
1217 
1218  if ( coupledBypass( m_world, aPair, aTryP, newRef, bypass, coupledPath, newCoup ) )
1219  {
1220  deltaCoupled = aPair->CoupledLength( newRef, newCoup ) - clenPre + budget;
1221 
1222  if( deltaCoupled >= 0 )
1223  {
1224  newRef.Simplify();
1225  newCoup.Simplify();
1226 
1227  aPair->SetShape( newRef, newCoup, !aTryP );
1228  return true;
1229  }
1230  }
1231  else if( deltaUni >= 0 && verifyDpBypass ( m_world, aPair, aTryP, newRef, coupledPath ) )
1232  {
1233  newRef.Simplify();
1234  coupledPath.Simplify();
1235 
1236  aPair->SetShape( newRef, coupledPath, !aTryP );
1237  return true;
1238  }
1239  }
1240 
1241  n++;
1242  }
1243 
1244  return false;
1245 }
int Index() const
Function Index()
Definition: seg.h:337
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
DIRECTION_45.
Definition: direction45.h:37
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)
int SegmentCount() const
Function SegmentCount()
const SHAPE_LINE_CHAIN BuildInitialTrace(const VECTOR2I &aP0, const VECTOR2I &aP1, bool aStartDiagonal=false, int aMaxRadius=0) const
Function BuildInitialTrace()
Definition: seg.h:39
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
Function CSegment()
SHAPE_LINE_CHAIN.
VECTOR2I A
Definition: seg.h:47
void Replace(int aStartIndex, int aEndIndex, const VECTOR2I &aP)
Function Replace()
VECTOR2I B
Definition: seg.h:48

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

553 {
554  SHAPE_LINE_CHAIN& line = aLine->Line();
555  int step = line.SegmentCount() - 1;
556 
557  int segs_pre = line.SegmentCount();
558 
559  line.Simplify();
560 
561  if( step < 0 )
562  return false;
563 
564  SHAPE_LINE_CHAIN current_path( line );
565 
566  while( 1 )
567  {
568  int n_segs = current_path.SegmentCount();
569  int max_step = n_segs - 2;
570 
571  if( step > max_step )
572  step = max_step;
573 
574  if( step < 1 )
575  break;
576 
577  bool found_anything = mergeStep( aLine, current_path, step );
578 
579  if( !found_anything )
580  step--;
581 
582  if( !step )
583  break;
584  }
585 
586  aLine->SetShape( current_path );
587 
588  return current_path.SegmentCount() < segs_pre;
589 }
bool mergeStep(LINE *aLine, SHAPE_LINE_CHAIN &aCurrentLine, int step)
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
int SegmentCount() const
Function SegmentCount()
SHAPE_LINE_CHAIN.

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

473 {
474  SHAPE_LINE_CHAIN& line = aLine->Line();
475 
476  int step = line.PointCount() - 3;
477  int iter = 0;
478  int segs_pre = line.SegmentCount();
479 
480  if( step < 0 )
481  return false;
482 
483  SHAPE_LINE_CHAIN current_path( line );
484 
485  while( 1 )
486  {
487  iter++;
488  int n_segs = current_path.SegmentCount();
489  int max_step = n_segs - 2;
490 
491  if( step > max_step )
492  step = max_step;
493 
494  if( step < 2 )
495  {
496  line = current_path;
497  return current_path.SegmentCount() < segs_pre;
498  }
499 
500  bool found_anything = false;
501 
502  for( int n = 0; n < n_segs - step; n++ )
503  {
504  const SEG s1 = current_path.CSegment( n );
505  const SEG s2 = current_path.CSegment( n + step );
506  SEG s1opt, s2opt;
507 
508  if( DIRECTION_45( s1 ).IsObtuse( DIRECTION_45( s2 ) ) )
509  {
510  VECTOR2I ip = *s1.IntersectLines( s2 );
511 
512  s1opt = SEG( s1.A, ip );
513  s2opt = SEG( ip, s2.B );
514 
515  if( DIRECTION_45( s1opt ).IsObtuse( DIRECTION_45( s2opt ) ) )
516  {
517  SHAPE_LINE_CHAIN opt_path;
518  opt_path.Append( s1opt.A );
519  opt_path.Append( s1opt.B );
520  opt_path.Append( s2opt.B );
521 
522  LINE opt_track( *aLine, opt_path );
523 
524  if( !checkColliding( &opt_track ) )
525  {
526  current_path.Replace( s1.Index() + 1, s2.Index(), ip );
527  // removeCachedSegments(aLine, s1.Index(), s2.Index());
528  n_segs = current_path.SegmentCount();
529  found_anything = true;
530  break;
531  }
532  }
533  }
534  }
535 
536  if( !found_anything )
537  {
538  if( step <= 2 )
539  {
540  line = current_path;
541  return line.SegmentCount() < segs_pre;
542  }
543 
544  step--;
545  }
546  }
547 
548  return line.SegmentCount() < segs_pre;
549 }
int Index() const
Function Index()
Definition: seg.h:337
OPT_VECTOR2I IntersectLines(const SEG &aSeg) const
Function IntersectLines()
Definition: seg.h:191
int PointCount() const
Function PointCount()
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
DIRECTION_45.
Definition: direction45.h:37
int SegmentCount() const
Function SegmentCount()
Definition: seg.h:39
SHAPE_LINE_CHAIN.
VECTOR2I A
Definition: seg.h:47
bool checkColliding(ITEM *aItem, bool aUpdateCache=true)
VECTOR2I B
Definition: seg.h:48

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

623 {
624  int n_segs = aCurrentPath.SegmentCount();
625 
626  int cost_orig = COST_ESTIMATOR::CornerCost( aCurrentPath );
627 
628  if( aLine->SegmentCount() < 2 )
629  return false;
630 
631  DIRECTION_45 orig_start( aLine->CSegment( 0 ) );
632  DIRECTION_45 orig_end( aLine->CSegment( -1 ) );
633 
634 
635  for( int n = 0; n < n_segs - step; n++ )
636  {
637  // Do not attempt to merge false segments that are part of an arc
638  if( aCurrentPath.isArc( n ) || aCurrentPath.isArc( n + step ) )
639  continue;
640 
641  const SEG s1 = aCurrentPath.CSegment( n );
642  const SEG s2 = aCurrentPath.CSegment( n + step );
643 
644  SHAPE_LINE_CHAIN path[2];
645  SHAPE_LINE_CHAIN* picked = NULL;
646  int cost[2];
647 
648  for( int i = 0; i < 2; i++ )
649  {
650  SHAPE_LINE_CHAIN bypass = DIRECTION_45().BuildInitialTrace( s1.A, s2.B, i );
651  cost[i] = INT_MAX;
652 
653 
654  bool ok = false;
655  if ( !checkColliding( aLine, bypass ) )
656  {
657  ok = checkConstraints ( n, n + step + 1, aLine, aCurrentPath, bypass );
658  }
659 
660  if( ok )
661  {
662  path[i] = aCurrentPath;
663  path[i].Replace( s1.Index(), s2.Index(), bypass );
664  path[i].Simplify();
665  cost[i] = COST_ESTIMATOR::CornerCost( path[i] );
666  }
667  }
668 
669  if( cost[0] < cost_orig && cost[0] < cost[1] )
670  picked = &path[0];
671  else if( cost[1] < cost_orig )
672  picked = &path[1];
673 
674  if( picked )
675  {
676  n_segs = aCurrentPath.SegmentCount();
677  aCurrentPath = *picked;
678  return true;
679  }
680  }
681 
682  return false;
683 }
int Index() const
Function Index()
Definition: seg.h:337
bool checkConstraints(int aVertex1, int aVertex2, LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement)
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
#define NULL
DIRECTION_45.
Definition: direction45.h:37
int SegmentCount() const
Function SegmentCount()
const SHAPE_LINE_CHAIN BuildInitialTrace(const VECTOR2I &aP0, const VECTOR2I &aP1, bool aStartDiagonal=false, int aMaxRadius=0) const
Function BuildInitialTrace()
Definition: seg.h:39
SHAPE_LINE_CHAIN.
VECTOR2I A
Definition: seg.h:47
bool checkColliding(ITEM *aItem, bool aUpdateCache=true)
static int CornerCost(const SEG &aA, const SEG &aB)
Cost Estimator Methods.
void Replace(int aStartIndex, int aEndIndex, const VECTOR2I &aP)
Function Replace()
VECTOR2I B
Definition: seg.h:48

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

Referenced by mergeFull().

◆ Optimize() [1/3]

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

a quick shortcut to optmize a line without creating and setting up an optimizer

Definition at line 1009 of file pns_optimizer.cpp.

1010 {
1011  OPTIMIZER opt( aWorld );
1012 
1014 
1015  opt.SetEffortLevel( aEffortLevel );
1016  opt.SetCollisionMask( -1 );
1017 
1018  if ( aEffortLevel & PRESERVE_VERTEX )
1019  {
1020  auto c = new PRESERVE_VERTEX_CONSTRAINT( aWorld, aV );
1021  opt.AddConstraint( c );
1022  //printf("pres-v %d %d\n", aV.x, aV.y );
1023  }
1024 
1025  if ( aEffortLevel & KEEP_TOPOLOGY )
1026  {
1027  auto c = new KEEP_TOPOLOGY_CONSTRAINT( aWorld );
1028  opt.AddConstraint( c );
1029  }
1030 
1031  return opt.Optimize( aLine );
1032 }
static DEBUG_DECORATOR * g_dbg
OPTIMIZER(NODE *aWorld)
Optimizer.
virtual DEBUG_DECORATOR * GetDebugDecorator()=0
ROUTER_IFACE * GetInterface() const
Definition: pns_router.h:233
static ROUTER * GetInstance()
Definition: pns_router.cpp:85

References AddConstraint(), PNS::g_dbg, PNS::ROUTER_IFACE::GetDebugDecorator(), PNS::ROUTER::GetInstance(), PNS::ROUTER::GetInterface(), KEEP_TOPOLOGY, Optimize(), PRESERVE_VERTEX, SetCollisionMask(), and SetEffortLevel().

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

◆ Optimize() [2/3]

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

Definition at line 592 of file pns_optimizer.cpp.

593 {
594  if( !aResult )
595  aResult = aLine;
596  else
597  {
598  *aResult = *aLine;
599  aResult->ClearLinks();
600  }
601 
602  m_keepPostures = false;
603 
604  bool rv = false;
605 
607  rv |= mergeFull( aResult );
608 
610  rv |= mergeObtuse( aResult );
611 
612  if( m_effortLevel & SMART_PADS )
613  rv |= runSmartPads( aResult );
614 
616  rv |= fanoutCleanup( aResult );
617 
618  return rv;
619 }
bool runSmartPads(LINE *aLine)
bool mergeFull(LINE *aLine)
bool mergeObtuse(LINE *aLine)
bool fanoutCleanup(LINE *aLine)

References PNS::LINK_HOLDER::ClearLinks(), FANOUT_CLEANUP, fanoutCleanup(), m_effortLevel, m_keepPostures, MERGE_OBTUSE, MERGE_SEGMENTS, mergeFull(), mergeObtuse(), runSmartPads(), and SMART_PADS.

◆ Optimize() [3/3]

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

Definition at line 1289 of file pns_optimizer.cpp.

1290 {
1291  return mergeDpSegments( aPair );
1292 }
bool mergeDpSegments(DIFF_PAIR *aPair)

References mergeDpSegments().

◆ ovalBreakouts()

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

◆ rectBreakouts()

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

Definition at line 748 of file pns_optimizer.cpp.

750 {
751  const SHAPE_RECT* rect = static_cast<const SHAPE_RECT*>(aShape);
752  VECTOR2I s = rect->GetSize();
753  VECTOR2I c = rect->GetPosition() + VECTOR2I( s.x / 2, s.y / 2 );
754  BREAKOUT_LIST breakouts;
755 
756  VECTOR2I d_offset;
757 
758  d_offset.x = ( s.x > s.y ) ? ( s.x - s.y ) / 2 : 0;
759  d_offset.y = ( s.x < s.y ) ? ( s.y - s.x ) / 2 : 0;
760 
761  VECTOR2I d_vert = VECTOR2I( 0, s.y / 2 + aWidth );
762  VECTOR2I d_horiz = VECTOR2I( s.x / 2 + aWidth, 0 );
763 
764  breakouts.push_back( SHAPE_LINE_CHAIN( { c, c + d_horiz } ) );
765  breakouts.push_back( SHAPE_LINE_CHAIN( { c, c - d_horiz } ) );
766  breakouts.push_back( SHAPE_LINE_CHAIN( { c, c + d_vert } ) );
767  breakouts.push_back( SHAPE_LINE_CHAIN( { c, c - d_vert } ) );
768 
769  if( aPermitDiagonal )
770  {
771  int l = aWidth + std::min( s.x, s.y ) / 2;
772  VECTOR2I d_diag;
773 
774  if( s.x >= s.y )
775  {
776  breakouts.push_back(
777  SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( l, l ) } ) );
778  breakouts.push_back(
779  SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset - VECTOR2I( -l, l ) } ) );
780  breakouts.push_back(
781  SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset + VECTOR2I( -l, l ) } ) );
782  breakouts.push_back(
783  SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( l, l ) } ) );
784  }
785  else
786  {
787  // fixme: this could be done more efficiently
788  breakouts.push_back(
789  SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( l, l ) } ) );
790  breakouts.push_back(
791  SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( -l, l ) } ) );
792  breakouts.push_back(
793  SHAPE_LINE_CHAIN( { c, c + d_offset, c + d_offset + VECTOR2I( -l, l ) } ) );
794  breakouts.push_back(
795  SHAPE_LINE_CHAIN( { c, c - d_offset, c - d_offset - VECTOR2I( l, l ) } ) );
796  }
797  }
798 
799  return breakouts;
800 }
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
const VECTOR2I GetSize() const
Function GetSize()
Definition: shape_rect.h:121
const VECTOR2I & GetPosition() const
Function GetPosition()
Definition: shape_rect.h:111
SHAPE_LINE_CHAIN.
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 189 of file pns_optimizer.cpp.

190 {
191  if( !aLine->IsLinked() ) return;
192 
193  auto links = aLine->Links();
194 
195  if( aEndVertex < 0 )
196  aEndVertex += aLine->PointCount();
197 
198  for( int i = aStartVertex; i < aEndVertex - 1; i++ )
199  {
200  LINKED_ITEM* s = links[i];
201  m_cacheTags.erase( s );
202  m_cache.Remove( s );
203  }
204 }
CachedItemTags 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().

◆ removeUglyCorners()

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

◆ runSmartPads()

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

Definition at line 982 of file pns_optimizer.cpp.

983 {
984  SHAPE_LINE_CHAIN& line = aLine->Line();
985 
986  if( line.PointCount() < 3 )
987  return false;
988 
989  VECTOR2I p_start = line.CPoint( 0 ), p_end = line.CPoint( -1 );
990 
991  ITEM* startPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_start );
992  ITEM* endPad = findPadOrVia( aLine->Layer(), aLine->Net(), p_end );
993 
994  int vtx = -1;
995 
996  if( startPad )
997  vtx = smartPadsSingle( aLine, startPad, false, 3 );
998 
999  if( endPad )
1000  smartPadsSingle( aLine, endPad, true,
1001  vtx < 0 ? line.PointCount() - 1 : line.PointCount() - 1 - vtx );
1002 
1003  aLine->Line().Simplify();
1004 
1005  return true;
1006 }
ITEM * findPadOrVia(int aLayer, int aNet, const VECTOR2I &aP) const
int PointCount() const
Function PointCount()
const VECTOR2I & CPoint(int aIndex) const
Function Point()
SHAPE_LINE_CHAIN.
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 123 of file pns_optimizer.h.

124  {
125  m_collisionKindMask = aMask;
126  }

References m_collisionKindMask.

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

◆ SetEffortLevel()

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

Definition at line 128 of file pns_optimizer.h.

129  {
130  m_effortLevel = aEffort;
131  }

References m_effortLevel.

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

◆ SetRestrictArea()

void PNS::OPTIMIZER::SetRestrictArea ( const BOX2I aArea)
inline

Definition at line 134 of file pns_optimizer.h.

135  {
136  m_restrictArea = aArea;
137  m_restrictAreaActive = true;
138  }
bool m_restrictAreaActive

References m_restrictArea, and m_restrictAreaActive.

Referenced by PNS::SHOVE::runOptimizer().

◆ SetWorld()

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

Definition at line 118 of file pns_optimizer.h.

118 { 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 868 of file pns_optimizer.cpp.

869 {
870  DIRECTION_45 dir;
871 
872  const int ForbiddenAngles = DIRECTION_45::ANG_ACUTE | DIRECTION_45::ANG_RIGHT |
874 
875  typedef std::tuple<int, long long int, SHAPE_LINE_CHAIN> RtVariant;
876  std::vector<RtVariant> variants;
877 
878  SOLID* solid = dyn_cast<SOLID*>( aPad );
879 
880  // don't do optimized connections for offset pads
881  if( solid && solid->Offset() != VECTOR2I( 0, 0 ) )
882  return -1;
883 
884 
885  BREAKOUT_LIST breakouts = computeBreakouts( aLine->Width(), aPad, true );
886  SHAPE_LINE_CHAIN line = ( aEnd ? aLine->CLine().Reverse() : aLine->CLine() );
887  int p_end = std::min( aEndVertex, std::min( 3, line.PointCount() - 1 ) );
888 
889  // Start at 1 to find a potentially better breakout (0 is the pad connection)
890  for( int p = 1; p <= p_end; p++ )
891  {
892  // If the line is contained inside the pad, don't optimize
893  if( solid && solid->Shape() && !solid->Shape()->Collide(
894  SEG( line.CPoint( 0 ), line.CPoint( p ) ), aLine->Width() / 2 ) )
895  continue;
896 
897  for( SHAPE_LINE_CHAIN & breakout : breakouts ) {
898 
899  for( int diag = 0; diag < 2; diag++ )
900  {
902  SHAPE_LINE_CHAIN connect = dir.BuildInitialTrace(
903  breakout.CPoint( -1 ), line.CPoint( p ), diag == 0 );
904 
905  DIRECTION_45 dir_bkout( breakout.CSegment( -1 ) );
906 
907  if(!connect.SegmentCount())
908  continue;
909 
910  int ang1 = dir_bkout.Angle( DIRECTION_45( connect.CSegment( 0 ) ) );
911 
912  if( ang1 & ForbiddenAngles )
913  continue;
914 
915  if( breakout.Length() > line.Length() )
916  continue;
917 
918  v = breakout;
919  v.Append( connect );
920 
921  for( int i = p + 1; i < line.PointCount(); i++ )
922  v.Append( line.CPoint( i ) );
923 
924  LINE tmp( *aLine, v );
925  int cc = tmp.CountCorners( ForbiddenAngles );
926 
927  if( cc == 0 )
928  {
929  RtVariant vp;
930  std::get<0>( vp ) = p;
931  std::get<1>( vp ) = breakout.Length();
932  std::get<2>( vp ) = aEnd ? v.Reverse() : v;
933  std::get<2>( vp ).Simplify();
934  variants.push_back( vp );
935  }
936  }
937  }
938  }
939 
940  // We attempt to minimize the corner cost (minimizes the segments and types of corners)
941  // but given two, equally valid costs, we want to pick the longer pad exit. The logic
942  // here is that if the pad is oblong, the track should not exit the shorter side and parallel
943  // the pad but should follow the pad's preferential direction before exiting.
944  // The baseline guess is to start with the existing line the user has drawn.
945  int min_cost = COST_ESTIMATOR::CornerCost( *aLine );
946  long long int max_length = 0;
947  bool found = false;
948  int p_best = -1;
949  SHAPE_LINE_CHAIN l_best;
950 
951  for( RtVariant& vp : variants )
952  {
953  LINE tmp( *aLine, std::get<2>( vp ) );
954  int cost = COST_ESTIMATOR::CornerCost( std::get<2>( vp ) );
955  long long int len = std::get<1>( vp );
956 
957  if( !checkColliding( &tmp ) )
958  {
959  if( cost < min_cost || ( cost == min_cost && len > max_length ) )
960  {
961  l_best = std::get<2>( vp );
962  p_best = std::get<0>( vp );
963  found = true;
964 
965  if( cost <= min_cost )
966  max_length = std::max<int>( len, max_length );
967 
968  min_cost = std::min( cost, min_cost );
969  }
970  }
971  }
972 
973  if( found )
974  {
975  aLine->SetShape( l_best );
976  return p_best;
977  }
978 
979  return -1;
980 }
BREAKOUT_LIST computeBreakouts(int aWidth, const ITEM *aItem, bool aPermitDiagonal) const
const SHAPE_LINE_CHAIN Reverse() const
Function Reverse()
VECTOR2< int > VECTOR2I
Definition: vector2d.h:594
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
AngleType Angle(const DIRECTION_45 &aOther) const
Function Angle() Returns the type of angle between directions (this) and aOther.
Definition: direction45.h:153
DIRECTION_45.
Definition: direction45.h:37
int SegmentCount() const
Function SegmentCount()
const SHAPE_LINE_CHAIN BuildInitialTrace(const VECTOR2I &aP0, const VECTOR2I &aP1, bool aStartDiagonal=false, int aMaxRadius=0) const
Function BuildInitialTrace()
Definition: seg.h:39
const SEG CSegment(int aIndex) const
Function CSegment()
SHAPE_LINE_CHAIN.
bool checkColliding(ITEM *aItem, bool aUpdateCache=true)
static int CornerCost(const SEG &aA, const SEG &aB)
Cost Estimator Methods.
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::SOLID::Offset(), SHAPE_LINE_CHAIN::Reverse(), SHAPE_LINE_CHAIN::SegmentCount(), PNS::LINE::SetShape(), PNS::SOLID::Shape(), SHAPE_LINE_CHAIN::Simplify(), and PNS::LINE::Width().

Referenced by runSmartPads().

Member Data Documentation

◆ m_cache

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

Definition at line 188 of file pns_optimizer.h.

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

◆ m_cacheTags

CachedItemTags PNS::OPTIMIZER::m_cacheTags
private

Definition at line 192 of file pns_optimizer.h.

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

◆ m_collisionKindMask

int PNS::OPTIMIZER::m_collisionKindMask
private

Definition at line 194 of file pns_optimizer.h.

Referenced by checkColliding(), and SetCollisionMask().

◆ m_constraints

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

Definition at line 190 of file pns_optimizer.h.

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

◆ m_effortLevel

int PNS::OPTIMIZER::m_effortLevel
private

Definition at line 195 of file pns_optimizer.h.

Referenced by Optimize(), and SetEffortLevel().

◆ m_keepPostures

bool PNS::OPTIMIZER::m_keepPostures
private

Definition at line 196 of file pns_optimizer.h.

Referenced by Optimize().

◆ m_restrictArea

BOX2I PNS::OPTIMIZER::m_restrictArea
private

Definition at line 198 of file pns_optimizer.h.

Referenced by SetRestrictArea().

◆ m_restrictAreaActive

bool PNS::OPTIMIZER::m_restrictAreaActive
private

Definition at line 199 of file pns_optimizer.h.

Referenced by SetRestrictArea().

◆ m_world

NODE* PNS::OPTIMIZER::m_world
private

Definition at line 193 of file pns_optimizer.h.

Referenced by checkColliding(), fanoutCleanup(), findPadOrVia(), mergeDpStep(), and SetWorld().

◆ MaxCachedItems

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

Definition at line 147 of file pns_optimizer.h.


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