KiCad PCB EDA Suite
pns_shove.cpp
Go to the documentation of this file.
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2014 CERN
5  * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <deque>
23 #include <cassert>
24 #include <math/box2.h>
25 
26 #include "pns_arc.h"
27 #include "pns_line.h"
28 #include "pns_node.h"
29 #include "pns_debug_decorator.h"
30 #include "pns_walkaround.h"
31 #include "pns_shove.h"
32 #include "pns_solid.h"
33 #include "pns_optimizer.h"
34 #include "pns_via.h"
35 #include "pns_utils.h"
36 #include "pns_router.h"
37 #include "pns_topology.h"
38 
39 #include "time_limit.h"
40 
41 // fixme - move all logger calls to debug decorator
42 
44 
45 namespace PNS {
46 
47 void SHOVE::replaceItems( ITEM* aOld, std::unique_ptr< ITEM > aNew )
48 {
49  OPT_BOX2I changed_area = ChangedArea( aOld, aNew.get() );
50 
51  if( changed_area )
52  m_affectedArea = m_affectedArea ? m_affectedArea->Merge( *changed_area ) : *changed_area;
53 
54  m_currentNode->Replace( aOld, std::move( aNew ) );
55 }
56 
57 void SHOVE::replaceLine( LINE& aOld, LINE& aNew )
58 {
59  OPT_BOX2I changed_area = ChangedArea( aOld, aNew );
60 
61  if( changed_area )
62  m_affectedArea = m_affectedArea ? m_affectedArea->Merge( *changed_area ) : *changed_area;
63 
64  m_currentNode->Replace( aOld, aNew );
65 }
66 
67 int SHOVE::getClearance( const ITEM* aA, const ITEM* aB ) const
68 {
69  if( m_forceClearance >= 0 )
70  return m_forceClearance;
71 
72  return m_currentNode->GetClearance( aA, aB );
73 }
74 
75 
76 void SHOVE::sanityCheck( LINE* aOld, LINE* aNew )
77 {
78  assert( aOld->CPoint( 0 ) == aNew->CPoint( 0 ) );
79  assert( aOld->CPoint( -1 ) == aNew->CPoint( -1 ) );
80 }
81 
82 
83 SHOVE::SHOVE( NODE* aWorld, ROUTER* aRouter ) :
84  ALGO_BASE( aRouter )
85 {
86  m_forceClearance = -1;
87  m_root = aWorld;
88  m_currentNode = aWorld;
90 
91  // Initialize other temporary variables:
93  m_iter = 0;
94  m_multiLineMode = false;
96 }
97 
98 
100 {
101 }
102 
103 
104 LINE SHOVE::assembleLine( const LINKED_ITEM* aSeg, int* aIndex )
105 {
106  return m_currentNode->AssembleLine( const_cast<LINKED_ITEM*>( aSeg ), aIndex, true );
107 }
108 
109 // A dumb function that checks if the shoved line is shoved the right way, e.g.
110 // visually "outwards" of the line/via applying pressure on it. Unfortunately there's no
111 // mathematical concept of orientation of an open curve, so we use some primitive heuristics:
112 // if the shoved line wraps around the start of the "pusher", it's likely shoved in wrong direction.
113 
114 // Update: there's no concept of an orientation of an open curve, but nonetheless Tom's dumb as.... (censored)
115 // Two open curves put together make a closed polygon... Tom should learn high school geometry!
116 
117 bool SHOVE::checkBumpDirection( const LINE& aCurrent, const LINE& aObstacle, const LINE& aShoved ) const
118 {
119  SHAPE_LINE_CHAIN::POINT_INSIDE_TRACKER checker( aCurrent.CPoint(0) );
120  checker.AddPolyline( aObstacle.CLine() );
121  checker.AddPolyline( aShoved.CLine().Reverse() );
122 
123  bool inside = checker.IsInside();
124 
125  return !inside;
126 }
127 
128 
129 SHOVE::SHOVE_STATUS SHOVE::walkaroundLoneVia( LINE& aCurrent, LINE& aObstacle, LINE& aShoved )
130 {
131  int clearance = getClearance( &aCurrent, &aObstacle );
132  const SHAPE_LINE_CHAIN hull = aCurrent.Via().Hull( clearance, aObstacle.Width() );
133  SHAPE_LINE_CHAIN path_cw;
134  SHAPE_LINE_CHAIN path_ccw;
135 
136  if( ! aObstacle.Walkaround( hull, path_cw, true ) )
137  return SH_INCOMPLETE;
138 
139  if( ! aObstacle.Walkaround( hull, path_ccw, false ) )
140  return SH_INCOMPLETE;
141 
142  const SHAPE_LINE_CHAIN& shortest = path_ccw.Length() < path_cw.Length() ? path_ccw : path_cw;
143 
144  if( shortest.PointCount() < 2 )
145  return SH_INCOMPLETE;
146 
147  if( aObstacle.CPoint( -1 ) != shortest.CPoint( -1 ) )
148  return SH_INCOMPLETE;
149 
150  if( aObstacle.CPoint( 0 ) != shortest.CPoint( 0 ) )
151  return SH_INCOMPLETE;
152 
153  aShoved.SetShape( shortest );
154 
155  if( m_currentNode->CheckColliding( &aShoved, &aCurrent ) )
156  return SH_INCOMPLETE;
157 
158  return SH_OK;
159 }
160 
161 
162 /*
163  * TODO describe....
164  */
166  LINE& aShoved, const HULL_SET& aHulls )
167 {
168  const SHAPE_LINE_CHAIN& obs = aObstacle.CLine();
169 
170  int attempt;
171 
172  for( attempt = 0; attempt < 4; attempt++ )
173  {
174  bool invertTraversal = ( attempt >= 2 );
175  bool clockwise = attempt % 2;
176  int vFirst = -1, vLast = -1;
177 
178  SHAPE_LINE_CHAIN path;
179  LINE l( aObstacle );
180 
181  for( int i = 0; i < (int) aHulls.size(); i++ )
182  {
183  const SHAPE_LINE_CHAIN& hull = aHulls[invertTraversal ? aHulls.size() - 1 - i : i];
184 
185  if( ! l.Walkaround( hull, path, clockwise ) )
186  return SH_INCOMPLETE;
187 
188  path.Simplify();
189  l.SetShape( path );
190  }
191 
192  for( int i = 0; i < std::min( path.PointCount(), obs.PointCount() ); i++ )
193  {
194  if( path.CPoint( i ) != obs.CPoint( i ) )
195  {
196  vFirst = i;
197  break;
198  }
199  }
200 
201  int k = obs.PointCount() - 1;
202  for( int i = path.PointCount() - 1; i >= 0 && k >= 0; i--, k-- )
203  {
204  if( path.CPoint( i ) != obs.CPoint( k ) )
205  {
206  vLast = i;
207  break;
208  }
209  }
210 
211  if( ( vFirst < 0 || vLast < 0 ) && !path.CompareGeometry( aObstacle.CLine() ) )
212  {
213  wxLogTrace( "PNS", "attempt %d fail vfirst-last", attempt );
214  continue;
215  }
216 
217  if( path.CPoint( -1 ) != obs.CPoint( -1 ) || path.CPoint( 0 ) != obs.CPoint( 0 ) )
218  {
219  wxLogTrace( "PNS", "attempt %d fail vend-start\n", attempt );
220  continue;
221  }
222 
223  if( !checkBumpDirection( aCurrent, aObstacle, l ) )
224  {
225  wxLogTrace( "PNS", "attempt %d fail direction-check", attempt );
226  aShoved.SetShape( l.CLine() );
227 
228  continue;
229  }
230 
231  if( path.SelfIntersecting() )
232  {
233  wxLogTrace( "PNS", "attempt %d fail self-intersect", attempt );
234  continue;
235  }
236 
237  bool colliding = m_currentNode->CheckColliding( &l, &aCurrent, ITEM::ANY_T, m_forceClearance );
238 
239 #ifdef DEBUG
240  char str[128];
241  sprintf(str,"att-%d-shoved", attempt);
242  Dbg()->AddLine( l.CLine(), 3, 20000, str );
243 #endif
244 
245  if( ( aCurrent.Marker() & MK_HEAD ) && !colliding )
246  {
247  JOINT* jtStart = m_currentNode->FindJoint( aCurrent.CPoint( 0 ), &aCurrent );
248 
249  for( ITEM* item : jtStart->LinkList() )
250  {
251  if( m_currentNode->CheckColliding( item, &l ) )
252  colliding = true;
253  }
254  }
255 
256  if( colliding )
257  {
258  wxLogTrace( "PNS", "attempt %d fail coll-check", attempt );
259  continue;
260  }
261 
262  aShoved.SetShape( l.CLine() );
263 
264  return SH_OK;
265  }
266 
267  return SH_INCOMPLETE;
268 }
269 
270 
271 /*
272  * TODO describe....
273  */
274 SHOVE::SHOVE_STATUS SHOVE::ProcessSingleLine( LINE& aCurrent, LINE& aObstacle, LINE& aShoved )
275 {
276  aShoved.ClearLinks();
277 
278  bool obstacleIsHead = false;
279 
280  for( auto s : aObstacle.Links() )
281  {
282  if( s->Marker() & MK_HEAD )
283  {
284  obstacleIsHead = true;
285  break;
286  }
287  }
288 
289  SHOVE_STATUS rv;
290 
291  bool viaOnEnd = aCurrent.EndsWithVia();
292 
293  if( viaOnEnd && ( !aCurrent.LayersOverlap( &aObstacle ) || aCurrent.SegmentCount() == 0 ) )
294  {
295  rv = walkaroundLoneVia( aCurrent, aObstacle, aShoved );
296  }
297  else
298  {
299  int w = aObstacle.Width();
300  int n_segs = aCurrent.SegmentCount();
301 
302  int clearance = getClearance( &aCurrent, &aObstacle ) + 1;
303 
304 #ifdef DEBUG
305  Dbg()->Message( wxString::Format( "shove process-single: cur net %d obs %d cl %d", aCurrent.Net(), aObstacle.Net(), clearance ) );
306 #endif
307 
308  HULL_SET hulls;
309 
310  hulls.reserve( n_segs + 1 );
311 
312  for( int i = 0; i < n_segs; i++ )
313  {
314  SEGMENT seg( aCurrent, aCurrent.CSegment( i ) );
315  SHAPE_LINE_CHAIN hull = seg.Hull( clearance, w );
316 
317  hulls.push_back( hull );
318  }
319 
320  if( viaOnEnd )
321  hulls.push_back( aCurrent.Via().Hull( clearance, w ) );
322 
323 #ifdef DEBUG
324  char str[128];
325  sprintf(str,"current-cl-%d", clearance );
326  Dbg()->AddLine( aCurrent.CLine(), 5, 20000, str );
327 #endif
328 
329  rv = processHullSet( aCurrent, aObstacle, aShoved, hulls );
330  }
331 
332  if( obstacleIsHead )
333  aShoved.Mark( aShoved.Marker() | MK_HEAD );
334 
335  return rv;
336 }
337 
338 
339 /*
340  * TODO describe....
341  */
343 {
344  int segIndex;
345  LINE obstacleLine = assembleLine( aObstacleSeg, &segIndex );
346  LINE shovedLine( obstacleLine );
347  SEGMENT tmp( *aObstacleSeg );
348 
349  if( obstacleLine.HasLockedSegments() )
350  return SH_TRY_WALK;
351 
352  SHOVE_STATUS rv = ProcessSingleLine( aCurrent, obstacleLine, shovedLine );
353 
354  const double extensionWalkThreshold = 1.0;
355 
356  double obsLen = obstacleLine.CLine().Length();
357  double shovedLen = shovedLine.CLine().Length();
358  double extensionFactor = 0.0;
359 
360  if( obsLen != 0.0f )
361  extensionFactor = shovedLen / obsLen - 1.0;
362 
363  if( extensionFactor > extensionWalkThreshold )
364  return SH_TRY_WALK;
365 
366  assert( obstacleLine.LayersOverlap( &shovedLine ) );
367 
368 #if 0
369  m_logger.NewGroup( "on-colliding-segment", m_iter );
370  m_logger.Log( &tmp, 0, "obstacle-segment" );
371  m_logger.Log( &aCurrent, 1, "current-line" );
372  m_logger.Log( &obstacleLine, 2, "obstacle-line" );
373  m_logger.Log( &shovedLine, 3, "shoved-line" );
374 #endif
375 
376  if( rv == SH_OK )
377  {
378  if( shovedLine.Marker() & MK_HEAD )
379  {
380  if( m_multiLineMode )
381  return SH_INCOMPLETE;
382 
383  m_newHead = shovedLine;
384  }
385 
386  int rank = aCurrent.Rank();
387  shovedLine.SetRank( rank - 1 );
388 
389  sanityCheck( &obstacleLine, &shovedLine );
390  replaceLine( obstacleLine, shovedLine );
391 
392  if( !pushLineStack( shovedLine ) )
393  rv = SH_INCOMPLETE;
394  }
395 
396  return rv;
397 }
398 
399 
400 /*
401  * TODO describe....
402  */
404 {
405  int segIndex;
406  LINE obstacleLine = assembleLine( aObstacleArc, &segIndex );
407  LINE shovedLine( obstacleLine );
408  ARC tmp( *aObstacleArc );
409 
410  if( obstacleLine.HasLockedSegments() )
411  return SH_TRY_WALK;
412 
413  SHOVE_STATUS rv = ProcessSingleLine( aCurrent, obstacleLine, shovedLine );
414 
415  const double extensionWalkThreshold = 1.0;
416 
417  double obsLen = obstacleLine.CLine().Length();
418  double shovedLen = shovedLine.CLine().Length();
419  double extensionFactor = 0.0;
420 
421  if( obsLen != 0.0f )
422  extensionFactor = shovedLen / obsLen - 1.0;
423 
424  if( extensionFactor > extensionWalkThreshold )
425  return SH_TRY_WALK;
426 
427  assert( obstacleLine.LayersOverlap( &shovedLine ) );
428 
429 #if 0
430  m_logger.NewGroup( "on-colliding-segment", m_iter );
431  m_logger.Log( &tmp, 0, "obstacle-segment" );
432  m_logger.Log( &aCurrent, 1, "current-line" );
433  m_logger.Log( &obstacleLine, 2, "obstacle-line" );
434  m_logger.Log( &shovedLine, 3, "shoved-line" );
435 #endif
436 
437  if( rv == SH_OK )
438  {
439  if( shovedLine.Marker() & MK_HEAD )
440  {
441  if( m_multiLineMode )
442  return SH_INCOMPLETE;
443 
444  m_newHead = shovedLine;
445  }
446 
447  int rank = aCurrent.Rank();
448  shovedLine.SetRank( rank - 1 );
449 
450  sanityCheck( &obstacleLine, &shovedLine );
451  replaceLine( obstacleLine, shovedLine );
452 
453  if( !pushLineStack( shovedLine ) )
454  rv = SH_INCOMPLETE;
455  }
456 
457  return rv;
458 }
459 
460 
461 /*
462  * TODO describe....
463  */
465 {
466  LINE shovedLine( aObstacle );
467 
468  SHOVE_STATUS rv = ProcessSingleLine( aCurrent, aObstacle, shovedLine );
469 
470  #if 0
471  m_logger.NewGroup( "on-colliding-line", m_iter );
472  m_logger.Log( &aObstacle, 0, "obstacle-line" );
473  m_logger.Log( &aCurrent, 1, "current-line" );
474  m_logger.Log( &shovedLine, 3, "shoved-line" );
475  #endif
476 
477  if( rv == SH_OK )
478  {
479  if( shovedLine.Marker() & MK_HEAD )
480  {
481  if( m_multiLineMode )
482  return SH_INCOMPLETE;
483 
484  m_newHead = shovedLine;
485  }
486 
487  sanityCheck( &aObstacle, &shovedLine );
488  replaceLine( aObstacle, shovedLine );
489 
490  int rank = aObstacle.Rank();
491  shovedLine.SetRank( rank - 1 );
492 
493 
494  if( !pushLineStack( shovedLine ) )
495  {
496  rv = SH_INCOMPLETE;
497  }
498  }
499 
500  return rv;
501 }
502 
503 
504 /*
505  * TODO describe....
506  */
508 {
509  WALKAROUND walkaround( m_currentNode, Router() );
510  LINE walkaroundLine( aCurrent );
511 
512  if( aCurrent.EndsWithVia() )
513  {
514  VIA vh = aCurrent.Via();
515  VIA* via = NULL;
516  JOINT* jtStart = m_currentNode->FindJoint( vh.Pos(), &aCurrent );
517 
518  if( !jtStart )
519  return SH_INCOMPLETE;
520 
521  for( ITEM* item : jtStart->LinkList() )
522  {
523  if( item->OfKind( ITEM::VIA_T ) )
524  {
525  via = (VIA*) item;
526  break;
527  }
528  }
529 
530  if( via && m_currentNode->CheckColliding( via, aObstacle ) )
531  return onCollidingVia( aObstacle, via );
532  }
533 
534  TOPOLOGY topo( m_currentNode );
535 
536  std::set<ITEM*> cluster = topo.AssembleCluster( aObstacle, aCurrent.Layers().Start() );
537 
538 #if 0
539  m_logger.NewGroup( "on-colliding-solid-cluster", m_iter );
540  for( ITEM* item : cluster )
541  {
542  m_logger.Log( item, 0, "cluster-entry" );
543  }
544 #endif
545 
546  walkaround.SetSolidsOnly( false );
547  walkaround.RestrictToSet( true, cluster );
548  walkaround.SetIterationLimit( 16 ); // fixme: make configurable
549 
550  int currentRank = aCurrent.Rank();
551  int nextRank;
552 
553  bool success = false;
554 
555  for( int attempt = 0; attempt < 2; attempt++ )
556  {
557  if( attempt == 1 || Settings().JumpOverObstacles() )
558  {
559 
560  nextRank = currentRank - 1;
561  walkaround.SetSingleDirection( true );
562  }
563  else
564  {
565  nextRank = currentRank + 10000;
566  walkaround.SetSingleDirection( false );
567  }
568 
569 
570  WALKAROUND::WALKAROUND_STATUS status = walkaround.Route( aCurrent, walkaroundLine, false );
571 
572  if( status != WALKAROUND::DONE )
573  continue;
574 
575  walkaroundLine.ClearLinks();
576  walkaroundLine.Unmark();
577  walkaroundLine.Line().Simplify();
578 
579  if( walkaroundLine.HasLoops() )
580  continue;
581 
582  if( aCurrent.Marker() & MK_HEAD )
583  {
584  walkaroundLine.Mark( MK_HEAD );
585 
586  if( m_multiLineMode )
587  continue;
588 
589  m_newHead = walkaroundLine;
590  }
591 
592  sanityCheck( &aCurrent, &walkaroundLine );
593 
594  if( !m_lineStack.empty() )
595  {
596  LINE lastLine = m_lineStack.front();
597 
598  if( m_currentNode->CheckColliding( &lastLine, &walkaroundLine ) )
599  {
600  LINE dummy( lastLine );
601 
602  if( ProcessSingleLine( walkaroundLine, lastLine, dummy ) == SH_OK )
603  {
604  success = true;
605  break;
606  }
607  } else {
608  success = true;
609  break;
610  }
611  }
612  }
613 
614  if(!success)
615  return SH_INCOMPLETE;
616 
617  replaceLine( aCurrent, walkaroundLine );
618  walkaroundLine.SetRank( nextRank );
619 
620 #if 0
621  m_logger.NewGroup( "on-colliding-solid", m_iter );
622  m_logger.Log( aObstacle, 0, "obstacle-solid" );
623  m_logger.Log( &aCurrent, 1, "current-line" );
624  m_logger.Log( &walkaroundLine, 3, "walk-line" );
625 #endif
626 
627  popLineStack();
628 
629  if( !pushLineStack( walkaroundLine ) )
630  return SH_INCOMPLETE;
631 
632  return SH_OK;
633 }
634 
635 
636 /*
637  * Pops NODE stackframes which no longer collide with aHeadSet. Optionally sets aDraggedVia
638  * to the dragged via of the last unpopped state.
639  */
640 NODE* SHOVE::reduceSpringback( const ITEM_SET& aHeadSet, VIA_HANDLE& aDraggedVia )
641 {
642  while( !m_nodeStack.empty() )
643  {
644  SPRINGBACK_TAG& spTag = m_nodeStack.back();
645 
646  auto obs = spTag.m_node->CheckColliding( aHeadSet );
647 
648  if( !obs && !spTag.m_locked )
649  {
650  aDraggedVia = spTag.m_draggedVia;
651  aDraggedVia.valid = true;
652 
653  delete spTag.m_node;
654  m_nodeStack.pop_back();
655  }
656  else
657  break;
658  }
659 
660  return m_nodeStack.empty() ? m_root : m_nodeStack.back().m_node;
661 }
662 
663 
664 /*
665  * Push the current NODE on to the stack. aDraggedVia is the dragged via *before* the push
666  * (which will be restored in the event the stackframe is popped).
667  */
668 bool SHOVE::pushSpringback( NODE* aNode, const OPT_BOX2I& aAffectedArea, VIA* aDraggedVia )
669 {
670  SPRINGBACK_TAG st;
671  OPT_BOX2I prev_area;
672 
673  if( !m_nodeStack.empty() )
674  prev_area = m_nodeStack.back().m_affectedArea;
675 
676  if( aDraggedVia )
677  {
678  st.m_draggedVia = aDraggedVia->MakeHandle();
679  }
680 
681  st.m_node = aNode;
682 
683  if( aAffectedArea )
684  {
685  if( prev_area )
686  st.m_affectedArea = prev_area->Merge( *aAffectedArea );
687  else
688  st.m_affectedArea = aAffectedArea;
689  } else
690  st.m_affectedArea = prev_area;
691 
692  st.m_seq = (m_nodeStack.empty() ? 1 : m_nodeStack.back().m_seq + 1);
693  st.m_locked = false;
694 
695  m_nodeStack.push_back( st );
696 
697  return true;
698 }
699 
700 
701 /*
702  * Push or shove a via by at least aForce. (The via might be pushed or shoved slightly further
703  * to keep it from landing on an existing joint.)
704  */
705 SHOVE::SHOVE_STATUS SHOVE::pushOrShoveVia( VIA* aVia, const VECTOR2I& aForce, int aCurrentRank )
706 {
707  LINE_PAIR_VEC draggedLines;
708  VECTOR2I p0( aVia->Pos() );
709  JOINT* jt = m_currentNode->FindJoint( p0, aVia );
710  VECTOR2I p0_pushed( p0 + aForce );
711 
712  // nothing to do...
713  if ( aForce.x == 0 && aForce.y == 0 )
714  return SH_OK;
715 
716  if( !jt )
717  {
718  wxLogTrace( "PNS", "weird, can't find the center-of-via joint\n" );
719  return SH_INCOMPLETE;
720  }
721 
722  if( aVia->IsLocked() )
723  return SH_TRY_WALK;
724 
725  if( jt->IsLocked() )
726  return SH_INCOMPLETE;
727 
728  // make sure pushed via does not overlap with any existing joint
729  while( true )
730  {
731  JOINT* jt_next = m_currentNode->FindJoint( p0_pushed, aVia );
732 
733  if( !jt_next )
734  break;
735 
736  p0_pushed += aForce.Resize( 2 );
737  }
738 
739  std::unique_ptr<VIA> pushedVia = Clone( *aVia );
740  pushedVia->SetPos( p0_pushed );
741  pushedVia->Mark( aVia->Marker() );
742 
743  for( ITEM* item : jt->LinkList() )
744  {
745  if( item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
746  {
747  LINKED_ITEM* li = static_cast<LINKED_ITEM*>( item );
748  LINE_PAIR lp;
749  int segIndex;
750 
751  lp.first = assembleLine( li, &segIndex );
752 
753  if( lp.first.HasLockedSegments() )
754  return SH_TRY_WALK;
755 
756  assert( segIndex == 0 || ( segIndex == ( lp.first.SegmentCount() - 1 ) ) );
757 
758  if( segIndex == 0 )
759  lp.first.Reverse();
760 
761  lp.second = lp.first;
762  lp.second.ClearLinks();
763  lp.second.DragCorner( p0_pushed, lp.second.CLine().Find( p0 ) );
764  lp.second.AppendVia( *pushedVia );
765  draggedLines.push_back( lp );
766  }
767  }
768 
769 #if 0
770  m_logger.Log( aVia, 0, "obstacle-via" );
771 #endif
772 
773  pushedVia->SetRank( aCurrentRank - 1 );
774 
775 #if 0
776  m_logger.Log( pushedVia.get(), 1, "pushed-via" );
777 #endif
778 
779  if( aVia->Marker() & MK_HEAD ) // push
780  {
781  m_draggedVia = pushedVia.get();
782  }
783  else
784  { // shove
785  if( jt->IsStitchingVia() )
786  pushLineStack( LINE( *pushedVia ) );
787  }
788 
789  replaceItems( aVia, std::move( pushedVia ) );
790 
791  for( LINE_PAIR lp : draggedLines )
792  {
793  if( lp.first.Marker() & MK_HEAD )
794  {
795  lp.second.Mark( MK_HEAD );
796 
797  if( m_multiLineMode )
798  return SH_INCOMPLETE;
799 
800  m_newHead = lp.second;
801  }
802 
803  unwindLineStack( &lp.first );
804 
805  if( lp.second.SegmentCount() )
806  {
807  replaceLine( lp.first, lp.second );
808  lp.second.SetRank( aCurrentRank - 1 );
809 
810  if( !pushLineStack( lp.second, true ) )
811  return SH_INCOMPLETE;
812  }
813  else
814  {
815  m_currentNode->Remove( lp.first );
816  }
817 
818 #if 0
819  m_logger.Log( &lp.first, 2, "fan-pre" );
820  m_logger.Log( &lp.second, 3, "fan-post" );
821 #endif
822  }
823 
824  return SH_OK;
825 }
826 
827 
828 /*
829  * Calculate the minimum translation vector required to resolve a collision with a via and
830  * shove the via by that distance.
831  */
833 {
834  int clearance = getClearance( aCurrent, aObstacleVia ) + PNS_HULL_MARGIN;
835  LINE_PAIR_VEC draggedLines;
836  bool lineCollision = false;
837  bool viaCollision = false;
838  bool holeCollision = false;
839  LINE* currentLine = NULL;
840  VECTOR2I mtvLine; // Minimum translation vector to correct line collisions
841  VECTOR2I mtvVia; // MTV to correct via collisions
842  VECTOR2I mtvHoles; // MTV to correct hole collisions
843  VECTOR2I mtvSolid; // MTV to correct solid collisions
844  VECTOR2I mtv; // Union of relevant MTVs (will correct all collisions)
845  int rank = -1;
846 
847  if( aCurrent->OfKind( ITEM::LINE_T ) )
848  {
849 #if 0
850  m_logger.NewGroup( "push-via-by-line", m_iter );
851  m_logger.Log( aCurrent, 4, "current" );
852 #endif
853 
854  currentLine = (LINE*) aCurrent;
855  lineCollision = aObstacleVia->Shape()->Collide( currentLine->Shape(),
856  clearance + currentLine->Width() / 2,
857  &mtvLine );
858 
859  if( currentLine->EndsWithVia() )
860  {
861  int currentNet = currentLine->Net();
862  int obstacleNet = aObstacleVia->Net();
863 
864  if( currentNet != obstacleNet && currentNet >= 0 && obstacleNet >= 0 )
865  {
866  viaCollision = currentLine->Via().Shape()->Collide( aObstacleVia->Shape(),
867  clearance, &mtvVia );
868  }
869 
870  // hole-to-hole is a mechanical constraint (broken drill bits), not an electrical
871  // one, so it has to be checked irrespective of matching nets.
872 
873  // temporarily removed hole-to-hole collision check due to conflicts with the springback algorithm...
874  // we need to figure out a better solution here - TW
875  holeCollision = false; //rr->CollideHoles( &currentLine->Via(), aObstacleVia, true, &mtvHoles );
876  }
877 
878  // These aren't /actually/ lengths as we don't bother to do the square-root part,
879  // but we're just comparing them to each other so it's faster this way.
880  ecoord lineMTVLength = lineCollision ? mtvLine.SquaredEuclideanNorm() : 0;
881  ecoord viaMTVLength = viaCollision ? mtvVia.SquaredEuclideanNorm() : 0;
882  ecoord holeMTVLength = holeCollision ? mtvHoles.SquaredEuclideanNorm() : 0;
883 
884  if( lineMTVLength >= viaMTVLength && lineMTVLength >= holeMTVLength )
885  mtv = mtvLine;
886  else if( viaMTVLength >= lineMTVLength && viaMTVLength >= holeMTVLength )
887  mtv = mtvVia;
888  else
889  mtv = mtvHoles;
890 
891  rank = currentLine->Rank();
892  }
893  else if( aCurrent->OfKind( ITEM::SOLID_T ) )
894  {
895  aObstacleVia->Shape()->Collide( aCurrent->Shape(), clearance, &mtvSolid );
896  mtv = -mtvSolid;
897  rank = aCurrent->Rank() + 10000;
898  }
899 
900  return pushOrShoveVia( aObstacleVia, mtv, rank );
901 }
902 
903 
904 /*
905  * TODO describe....
906  */
908 {
909  int n = 0;
910  LINE cur( aCurrent );
911  cur.ClearLinks();
912 
913  JOINT* jt = m_currentNode->FindJoint( aObstacleVia->Pos(), aObstacleVia );
914  LINE shoved( aCurrent );
915  shoved.ClearLinks();
916 
917  cur.RemoveVia();
918  unwindLineStack( &aCurrent );
919 
920  for( ITEM* item : jt->LinkList() )
921  {
922  if( item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) && item->LayersOverlap( &aCurrent ) )
923  {
924  LINKED_ITEM* li = static_cast<LINKED_ITEM*>( item );
925  LINE head = assembleLine( li );
926 
927  head.AppendVia( *aObstacleVia );
928 
929  SHOVE_STATUS st = ProcessSingleLine( head, cur, shoved );
930 
931  if( st != SH_OK )
932  {
933 #if 0
934  m_logger.NewGroup( "on-reverse-via-fail-shove", m_iter );
935  m_logger.Log( aObstacleVia, 0, "the-via" );
936  m_logger.Log( &aCurrent, 1, "current-line" );
937  m_logger.Log( &shoved, 3, "shoved-line" );
938 #endif
939 
940  return st;
941  }
942 
943  cur.SetShape( shoved.CLine() );
944  n++;
945  }
946  }
947 
948  if( !n )
949  {
950 #if 0
951  m_logger.NewGroup( "on-reverse-via-fail-lonevia", m_iter );
952  m_logger.Log( aObstacleVia, 0, "the-via" );
953  m_logger.Log( &aCurrent, 1, "current-line" );
954 #endif
955 
956  LINE head( aCurrent );
957  head.Line().Clear();
958  head.AppendVia( *aObstacleVia );
959  head.ClearLinks();
960 
961  SHOVE_STATUS st = ProcessSingleLine( head, aCurrent, shoved );
962 
963  if( st != SH_OK )
964  return st;
965 
966  cur.SetShape( shoved.CLine() );
967  }
968 
969  if( aCurrent.EndsWithVia() )
970  shoved.AppendVia( aCurrent.Via() );
971 
972 #if 0
973  m_logger.NewGroup( "on-reverse-via", m_iter );
974  m_logger.Log( aObstacleVia, 0, "the-via" );
975  m_logger.Log( &aCurrent, 1, "current-line" );
976  m_logger.Log( &shoved, 3, "shoved-line" );
977 #endif
978  int currentRank = aCurrent.Rank();
979  replaceLine( aCurrent, shoved );
980 
981  if( !pushLineStack( shoved ) )
982  return SH_INCOMPLETE;
983 
984  shoved.SetRank( currentRank );
985 
986  return SH_OK;
987 }
988 
989 
991 {
992  for( std::vector<LINE>::iterator i = m_lineStack.begin(); i != m_lineStack.end() ; )
993  {
994  if( i->ContainsLink( aSeg ) )
995  i = m_lineStack.erase( i );
996  else
997  i++;
998  }
999 
1000  for( std::vector<LINE>::iterator i = m_optimizerQueue.begin(); i != m_optimizerQueue.end() ; )
1001  {
1002  if( i->ContainsLink( aSeg ) )
1003  i = m_optimizerQueue.erase( i );
1004  else
1005  i++;
1006  }
1007 }
1008 
1009 
1011 {
1012  if( aItem->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
1013  unwindLineStack( static_cast<LINKED_ITEM*>( aItem ) );
1014  else if( aItem->OfKind( ITEM::LINE_T ) )
1015  {
1016  LINE* l = static_cast<LINE*>( aItem );
1017 
1018  for( auto seg : l->Links() )
1019  unwindLineStack( seg );
1020  }
1021 }
1022 
1023 
1024 bool SHOVE::pushLineStack( const LINE& aL, bool aKeepCurrentOnTop )
1025 {
1026  if( !aL.IsLinkedChecked() && aL.SegmentCount() != 0 )
1027  return false;
1028 
1029  if( aKeepCurrentOnTop && m_lineStack.size() > 0)
1030  {
1031  m_lineStack.insert( m_lineStack.begin() + m_lineStack.size() - 1, aL );
1032  }
1033  else
1034  {
1035  m_lineStack.push_back( aL );
1036  }
1037 
1038  m_optimizerQueue.push_back( aL );
1039 
1040  return true;
1041 }
1042 
1044 {
1045  LINE& l = m_lineStack.back();
1046 
1047  for( std::vector<LINE>::iterator i = m_optimizerQueue.begin(); i != m_optimizerQueue.end(); )
1048  {
1049  bool found = false;
1050 
1051  for( auto s : l.Links() )
1052  {
1053  if( i->ContainsLink( s ) )
1054  {
1055  i = m_optimizerQueue.erase( i );
1056  found = true;
1057  break;
1058  }
1059  }
1060 
1061  if( !found )
1062  i++;
1063  }
1064 
1065  m_lineStack.pop_back();
1066 }
1067 
1068 
1069 /*
1070  * Resolve the next collision.
1071  */
1073 {
1074  LINE currentLine = m_lineStack.back();
1075  NODE::OPT_OBSTACLE nearest;
1076  SHOVE_STATUS st = SH_NULL;
1077 
1078 #ifdef DEBUG
1079  Dbg()->SetIteration( aIter );
1080 #endif
1081 
1082  for( ITEM::PnsKind search_order : { ITEM::SOLID_T, ITEM::VIA_T, ITEM::SEGMENT_T } )
1083  {
1084  nearest = m_currentNode->NearestObstacle( &currentLine, search_order );
1085 
1086  if( nearest )
1087  break;
1088  }
1089 
1090  if( !nearest )
1091  {
1092  m_lineStack.pop_back();
1093  return SH_OK;
1094  }
1095 
1096  ITEM* ni = nearest->m_item;
1097 
1098  unwindLineStack( ni );
1099 
1100  if( !ni->OfKind( ITEM::SOLID_T ) && ni->Rank() >= 0 && ni->Rank() > currentLine.Rank() )
1101  {
1102  // Collision with a higher-ranking object (ie: one that we've already shoved)
1103  //
1104  switch( ni->Kind() )
1105  {
1106  case ITEM::VIA_T:
1107  {
1108  wxLogTrace( "PNS", "iter %d: reverse-collide-via", aIter );
1109 
1110  if( currentLine.EndsWithVia()
1111  && m_currentNode->CheckColliding( &currentLine.Via(), (VIA*) ni ) )
1112  {
1113  st = SH_INCOMPLETE;
1114  }
1115  else
1116  {
1117  st = onReverseCollidingVia( currentLine, (VIA*) ni );
1118  }
1119 
1120  break;
1121  }
1122 
1123  case ITEM::SEGMENT_T:
1124  {
1125  wxLogTrace( "PNS", "iter %d: reverse-collide-segment ", aIter );
1126  LINE revLine = assembleLine( static_cast<SEGMENT*>( ni ) );
1127 
1128  popLineStack();
1129  st = onCollidingLine( revLine, currentLine );
1130  if( !pushLineStack( revLine ) )
1131  return SH_INCOMPLETE;
1132 
1133  break;
1134  }
1135 
1136  case ITEM::ARC_T:
1137  {
1138  //TODO(snh): Handle Arc shove separate from track
1139  wxLogTrace( "PNS", "iter %d: reverse-collide-arc ", aIter );
1140  LINE revLine = assembleLine( static_cast<ARC*>( ni ) );
1141 
1142  popLineStack();
1143  st = onCollidingLine( revLine, currentLine );
1144  if( !pushLineStack( revLine ) )
1145  return SH_INCOMPLETE;
1146 
1147  break;
1148  }
1149 
1150  default:
1151  assert( false );
1152  }
1153  }
1154  else
1155  {
1156  // Collision with a lower-ranking object or a solid
1157  //
1158  switch( ni->Kind() )
1159  {
1160  case ITEM::SEGMENT_T:
1161  wxLogTrace( "PNS", "iter %d: collide-segment ", aIter );
1162 
1163  st = onCollidingSegment( currentLine, (SEGMENT*) ni );
1164 
1165  if( st == SH_TRY_WALK )
1166  st = onCollidingSolid( currentLine, ni );
1167 
1168  break;
1169 
1170  //TODO(snh): Customize Arc collide
1171  case ITEM::ARC_T:
1172  wxLogTrace( "PNS", "iter %d: collide-arc ", aIter );
1173 
1174  st = onCollidingArc( currentLine, static_cast<ARC*>( ni ) );
1175 
1176  if( st == SH_TRY_WALK )
1177  st = onCollidingSolid( currentLine, ni );
1178 
1179  break;
1180 
1181  case ITEM::VIA_T:
1182  wxLogTrace( "PNS", "iter %d: shove-via ", aIter );
1183  st = onCollidingVia( &currentLine, (VIA*) ni );
1184 
1185  if( st == SH_TRY_WALK )
1186  st = onCollidingSolid( currentLine, ni );
1187 
1188  break;
1189 
1190  case ITEM::SOLID_T:
1191  wxLogTrace( "PNS", "iter %d: walk-solid ", aIter );
1192  st = onCollidingSolid( currentLine, (SOLID*) ni );
1193  break;
1194 
1195  default:
1196  break;
1197  }
1198  }
1199 
1200  return st;
1201 }
1202 
1203 
1204 /*
1205  * Resolve collisions.
1206  * Each iteration pushes the next colliding object out of the way. Iterations are continued as
1207  * long as they propagate further collisions, or until the iteration timeout or max iteration
1208  * count is reached.
1209  */
1211 {
1212  SHOVE_STATUS st = SH_OK;
1213 
1215 
1216  wxLogTrace( "PNS", "ShoveStart [root: %d jts, current: %d jts]", m_root->JointCount(),
1218 
1219  int iterLimit = Settings().ShoveIterationLimit();
1220  TIME_LIMIT timeLimit = Settings().ShoveTimeLimit();
1221 
1222  m_iter = 0;
1223 
1224  timeLimit.Restart();
1225 
1226  if( m_lineStack.empty() && m_draggedVia )
1227  {
1228  // If we're shoving a free via then push a proxy LINE (with the via on the end) onto
1229  // the stack.
1231  }
1232 
1233  while( !m_lineStack.empty() )
1234  {
1235  st = shoveIteration( m_iter );
1236 
1237  m_iter++;
1238 
1239  if( st == SH_INCOMPLETE || timeLimit.Expired() || m_iter >= iterLimit )
1240  {
1241  st = SH_INCOMPLETE;
1242  break;
1243  }
1244  }
1245 
1246  return st;
1247 }
1248 
1249 
1251 {
1252  OPT_BOX2I area;
1253 
1254  if( !m_nodeStack.empty() )
1255  area = m_nodeStack.back().m_affectedArea;
1256 
1257  if( area && m_affectedArea)
1258  area->Merge( *m_affectedArea );
1259  else if( !area )
1260  area = m_affectedArea;
1261 
1262  return area;
1263 }
1264 
1265 
1267 {
1268  SHOVE_STATUS st = SH_OK;
1269 
1270  m_multiLineMode = false;
1271 
1272  // empty head? nothing to shove...
1273 
1274  if( !aCurrentHead.SegmentCount() && !aCurrentHead.EndsWithVia() )
1275  return SH_INCOMPLETE;
1276 
1277  LINE head( aCurrentHead );
1278  head.ClearLinks();
1279 
1280  m_lineStack.clear();
1281  m_optimizerQueue.clear();
1282  m_newHead = OPT_LINE();
1283 #if 0
1284  m_logger.Clear();
1285 #endif
1286 
1287  // Pop NODEs containing previous shoves which are no longer necessary
1288  //
1289  ITEM_SET headSet;
1290  headSet.Add( aCurrentHead );
1291 
1292  VIA_HANDLE dummyVia;
1293 
1294  NODE* parent = reduceSpringback( headSet, dummyVia );
1295 
1296  // Create a new NODE to store this version of the world
1297  //
1298  m_currentNode = parent->Branch();
1300  m_currentNode->Add( head );
1301 
1302  m_currentNode->LockJoint( head.CPoint(0), &head, true );
1303 
1304  if( !head.EndsWithVia() )
1305  m_currentNode->LockJoint( head.CPoint( -1 ), &head, true );
1306 
1307  head.Mark( MK_HEAD );
1308  head.SetRank( 100000 );
1309 
1310 #if 0
1311  m_logger.NewGroup( "initial", 0 );
1312  m_logger.Log( &head, 0, "head" );
1313 #endif
1314 
1315  if( head.EndsWithVia() )
1316  {
1317  std::unique_ptr< VIA >headVia = Clone( head.Via() );
1318  headVia->Mark( MK_HEAD );
1319  headVia->SetRank( 100000 );
1320  m_currentNode->Add( std::move( headVia ) );
1321  }
1322 
1323  if( !pushLineStack( head ) )
1324  {
1325  delete m_currentNode;
1326  m_currentNode = parent;
1327 
1328  return SH_INCOMPLETE;
1329  }
1330 
1331  st = shoveMainLoop();
1332 
1333  if( st == SH_OK )
1334  {
1336 
1337  if( m_newHead )
1339  else
1340  st = m_currentNode->CheckColliding( &head ) ? SH_INCOMPLETE : SH_OK;
1341  }
1342 
1344 
1345  wxLogTrace( "PNS", "Shove status : %s after %d iterations",
1346  ( ( st == SH_OK || st == SH_HEAD_MODIFIED ) ? "OK" : "FAILURE"), m_iter );
1347 
1348  if( st == SH_OK || st == SH_HEAD_MODIFIED )
1349  {
1351  }
1352  else
1353  {
1354  delete m_currentNode;
1355 
1356  m_currentNode = parent;
1357  m_newHead = OPT_LINE();
1358  }
1359 
1360  if(m_newHead)
1361  m_newHead->Unmark();
1362 
1363  if( m_newHead && head.EndsWithVia() )
1364  {
1365  VIA v = head.Via();
1366  v.SetPos( m_newHead->CPoint( -1 ) );
1367  m_newHead->AppendVia(v);
1368  }
1369 
1370  return st;
1371 }
1372 
1373 
1375 {
1376  SHOVE_STATUS st = SH_OK;
1377 
1378  m_multiLineMode = true;
1379 
1380  ITEM_SET headSet;
1381 
1382  for( const ITEM* item : aHeadSet.CItems() )
1383  {
1384  const LINE* headOrig = static_cast<const LINE*>( item );
1385 
1386  // empty head? nothing to shove...
1387  if( !headOrig->SegmentCount() )
1388  return SH_INCOMPLETE;
1389 
1390  headSet.Add( *headOrig );
1391  }
1392 
1393  m_lineStack.clear();
1394  m_optimizerQueue.clear();
1395 #if 0
1396  m_logger.Clear();
1397 #endif
1398 
1399  VIA_HANDLE dummyVia;
1400 
1401  NODE* parent = reduceSpringback( headSet, dummyVia );
1402 
1403  m_currentNode = parent->Branch();
1405  int n = 0;
1406 
1407  for( const ITEM* item : aHeadSet.CItems() )
1408  {
1409  const LINE* headOrig = static_cast<const LINE*>( item );
1410  LINE head( *headOrig );
1411  head.ClearLinks();
1412 
1413  m_currentNode->Add( head );
1414 
1415  head.Mark( MK_HEAD );
1416  head.SetRank( 100000 );
1417  n++;
1418 
1419  if( !pushLineStack( head ) )
1420  return SH_INCOMPLETE;
1421 
1422  if( head.EndsWithVia() )
1423  {
1424  std::unique_ptr< VIA > headVia = Clone( head.Via() );
1425  headVia->Mark( MK_HEAD );
1426  headVia->SetRank( 100000 );
1427  m_currentNode->Add( std::move( headVia ) );
1428  }
1429  }
1430 
1431  st = shoveMainLoop();
1432 
1433  if( st == SH_OK )
1435 
1437 
1438  wxLogTrace( "PNS", "Shove status : %s after %d iterations",
1439  ( st == SH_OK ? "OK" : "FAILURE"), m_iter );
1440 
1441  if( st == SH_OK )
1442  {
1444  }
1445  else
1446  {
1447  delete m_currentNode;
1448  m_currentNode = parent;
1449  }
1450 
1451  return st;
1452 }
1453 
1454 static VIA* findViaByHandle ( NODE *aNode, const VIA_HANDLE& handle )
1455 {
1456  JOINT* jt = aNode->FindJoint( handle.pos, handle.layers.Start(), handle.net );
1457 
1458  if( !jt )
1459  return nullptr;
1460 
1461  for( ITEM* item : jt->LinkList() )
1462  {
1463  if ( item->OfKind( ITEM::VIA_T ))
1464  {
1465  if( item->Net() == handle.net && item->Layers().Overlaps(handle.layers) )
1466  return static_cast<VIA*>( item );
1467  }
1468  }
1469 
1470  return nullptr;
1471 }
1472 
1474 {
1475  SHOVE_STATUS st = SH_OK;
1476 
1477  m_lineStack.clear();
1478  m_optimizerQueue.clear();
1479  m_newHead = OPT_LINE();
1480  m_draggedVia = NULL;
1481 
1482  auto viaToDrag = findViaByHandle( m_currentNode, aOldVia );
1483 
1484  if( !viaToDrag )
1485  {
1486  return SH_INCOMPLETE;
1487  }
1488 
1489  // Pop NODEs containing previous shoves which are no longer necessary
1490  ITEM_SET headSet;
1491 
1492  VIA headVia ( *viaToDrag );
1493  headVia.SetPos( aWhere );
1494  headSet.Add( headVia );
1495  VIA_HANDLE prevViaHandle;
1496  NODE* parent = reduceSpringback( headSet, prevViaHandle );
1497 
1498  if( prevViaHandle.valid )
1499  {
1500  aNewVia = prevViaHandle;
1501  viaToDrag = findViaByHandle( parent, prevViaHandle );
1502  }
1503 
1504  // Create a new NODE to store this version of the world
1505  //
1506  m_currentNode = parent->Branch();
1508 
1509  viaToDrag->Mark( MK_HEAD );
1510  viaToDrag->SetRank( 100000 );
1511 
1512  // Push the via to its new location
1513  //
1514  st = pushOrShoveVia( viaToDrag, ( aWhere - viaToDrag->Pos()), 0 );
1515 
1516  // Shove any colliding objects out of the way
1517  //
1518  if( st == SH_OK )
1519  st = shoveMainLoop();
1520 
1521  if( st == SH_OK )
1523 
1524  if( st == SH_OK || st == SH_HEAD_MODIFIED )
1525  {
1526  wxLogTrace( "PNS","setNewV %p", m_draggedVia );
1527 
1528  if (!m_draggedVia)
1529  m_draggedVia = viaToDrag;
1530 
1531  aNewVia = m_draggedVia->MakeHandle();
1532 
1534  }
1535  else
1536  {
1537  delete m_currentNode;
1538  m_currentNode = parent;
1539  }
1540 
1541  return st;
1542 }
1543 
1544 
1546 {
1547  OPTIMIZER optimizer( aNode );
1548  int optFlags = 0;
1549  int n_passes = 0;
1550 
1552 
1553  OPT_BOX2I area = totalAffectedArea();
1554 
1555  int maxWidth = 0;
1556 
1557  for( LINE& line : m_optimizerQueue )
1558  maxWidth = std::max( line.Width(), maxWidth );
1559 
1560  if( area )
1561  area->Inflate( 10 * maxWidth );
1562 
1563  switch( effort )
1564  {
1565  case OE_LOW:
1566  optFlags = OPTIMIZER::MERGE_OBTUSE;
1567  n_passes = 1;
1568  break;
1569 
1570  case OE_MEDIUM:
1571  optFlags = OPTIMIZER::MERGE_SEGMENTS;
1572 
1573  if( area )
1574  optimizer.SetRestrictArea( *area );
1575 
1576  n_passes = 2;
1577  break;
1578 
1579  case OE_FULL:
1580  optFlags = OPTIMIZER::MERGE_SEGMENTS;
1581  n_passes = 2;
1582  break;
1583 
1584  default:
1585  break;
1586  }
1587 
1588  if( Settings().SmartPads() )
1589  optFlags |= OPTIMIZER::SMART_PADS;
1590 
1591  optimizer.SetEffortLevel( optFlags );
1592  optimizer.SetCollisionMask( ITEM::ANY_T );
1593 
1594  for( int pass = 0; pass < n_passes; pass++ )
1595  {
1596  std::reverse( m_optimizerQueue.begin(), m_optimizerQueue.end() );
1597 
1598  for( LINE& line : m_optimizerQueue)
1599  {
1600  if( !( line.Marker() & MK_HEAD ) )
1601  {
1602  LINE optimized;
1603 
1604  if( optimizer.Optimize( &line, &optimized ) )
1605  {
1606  aNode->Remove( line );
1607  line.SetShape( optimized.CLine() );
1608  aNode->Add( line );
1609  }
1610  }
1611  }
1612  }
1613 }
1614 
1615 
1617 {
1618  return m_nodeStack.empty() ? m_root : m_nodeStack.back().m_node;
1619 }
1620 
1621 
1622 const LINE SHOVE::NewHead() const
1623 {
1624  assert( m_newHead );
1625 
1626  return *m_newHead;
1627 }
1628 
1629 
1630 void SHOVE::SetInitialLine( LINE& aInitial )
1631 {
1632  m_root = m_root->Branch();
1633  m_root->Remove( aInitial );
1634 }
1635 
1636 
1638 {
1639  SPRINGBACK_TAG sp;
1640  sp.m_node = aNode;
1641  sp.m_locked = true;
1642 
1643  m_nodeStack.push_back(sp);
1644  return true;
1645 }
1646 
1647 
1649 {
1650  bool found = false;
1651 
1652  auto iter = m_nodeStack.begin();
1653 
1654  while( iter != m_nodeStack.end() )
1655  {
1656  if ( iter->m_node == aNode )
1657  {
1658  found = true;
1659  break;
1660  }
1661  iter++;
1662  }
1663 
1664  if( !found )
1665  return false;
1666 
1667  auto start = iter;
1668 
1669  aNode->KillChildren();
1670  m_nodeStack.erase( start, m_nodeStack.end() );
1671 
1672  return true;
1673 }
1674 
1675 
1677 {
1678  auto iter = m_nodeStack.begin();
1679 
1680  while( iter != m_nodeStack.end() )
1681  {
1682  if ( iter->m_node == aNode )
1683  {
1684  iter->m_locked = false;
1685  break;
1686  }
1687  iter++;
1688  }
1689 }
1690 
1691 }
1692 
const SHAPE_LINE_CHAIN & CLine() const
Const accessor to the underlying shape
Definition: pns_line.h:149
bool pushLineStack(const LINE &aL, bool aKeepCurrentOnTop=false)
Definition: pns_shove.cpp:1024
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:77
virtual void SetIteration(int iter)
ITEM.
Definition: pns_item.h:53
void ClearRanks(int aMarkerMask=MK_HEAD|MK_VIOLATION)
Definition: pns_node.cpp:1316
LINE assembleLine(const LINKED_ITEM *aSeg, int *aIndex=NULL)
Definition: pns_shove.cpp:104
ROUTER * Router() const
Returns the instance of our router
Definition: pns_algo_base.h:51
long long int Length() const
Function Length()
void popLineStack()
Definition: pns_shove.cpp:1043
int m_restrictSpringbackTagId
Definition: pns_shove.h:163
NODE.
Definition: pns_node.h:145
virtual void AddLine(const SHAPE_LINE_CHAIN &aLine, int aType=0, int aWidth=0, const std::string aName="")
VECTOR2I::extended_type ecoord
Definition: pns_shove.cpp:43
SHOVE_STATUS onCollidingSolid(LINE &aCurrent, ITEM *aObstacle)
Definition: pns_shove.cpp:507
void unwindLineStack(LINKED_ITEM *aSeg)
Definition: pns_shove.cpp:990
OPT_OBSTACLE NearestObstacle(const LINE *aItem, int aKindMask=ITEM::ANY_T, const std::set< ITEM * > *aRestrictedSet=NULL)
Function NearestObstacle()
Definition: pns_node.cpp:304
OPT< LINE > OPT_LINE
Definition: pns_shove.h:95
bool Walkaround(SHAPE_LINE_CHAIN aObstacle, SHAPE_LINE_CHAIN &aPre, SHAPE_LINE_CHAIN &aWalk, SHAPE_LINE_CHAIN &aPost, bool aCw) const
Calculates a line thightly wrapping a convex hull of an obstacle object (aObstacle).
Definition: pns_line.cpp:156
int SegmentCount() const
Returns the number of segments in the line
Definition: pns_line.h:155
const VIA_HANDLE MakeHandle() const
Definition: pns_via.cpp:115
const LINE NewHead() const
Definition: pns_shove.cpp:1622
const SEG CSegment(int aIdx) const
Returns the aIdx-th segment of the line
Definition: pns_line.h:179
SHOVE_STATUS onCollidingArc(LINE &aCurrent, ARC *aObstacleArc)
Definition: pns_shove.cpp:403
int Rank() const override
Definition: pns_line.cpp:862
ALGO_BASE.
Definition: pns_algo_base.h:39
SHOVE_STATUS ProcessSingleLine(LINE &aCurrent, LINE &aObstacle, LINE &aShoved)
Definition: pns_shove.cpp:274
std::vector< LINE > m_optimizerQueue
Definition: pns_shove.h:159
bool RewindSpringbackTo(NODE *aNode)
Definition: pns_shove.cpp:1648
#define PNS_HULL_MARGIN
LINE.
Definition: pns_line.h:60
void SetSingleDirection(bool aForceSingleDirection)
SHOVE_STATUS processHullSet(LINE &aCurrent, LINE &aObstacle, LINE &aShoved, const HULL_SET &hulls)
Definition: pns_shove.cpp:165
int JointCount() const
Returns the number of joints
Definition: pns_node.h:182
void RemoveVia()
Definition: pns_line.h:237
bool checkBumpDirection(const LINE &aCurrent, const LINE &aObstacle, const LINE &aShoved) const
Definition: pns_shove.cpp:117
NODE * m_root
Definition: pns_shove.h:161
std::pair< LINE, LINE > LINE_PAIR
Definition: pns_shove.h:96
extended_type SquaredEuclideanNorm() const
Function Squared Euclidean Norm computes the squared euclidean norm of the vector,...
Definition: vector2d.h:306
WALKAROUND_STATUS Route(const LINE &aInitialPath, LINE &aWalkPath, bool aOptimize=true)
void AppendVia(const VIA &aVia)
Definition: pns_line.cpp:839
void AddPolyline(const SHAPE_LINE_CHAIN &aPolyline)
OPT_BOX2I ChangedArea(const ITEM *aItemA, const ITEM *aItemB)
Definition: pns_utils.cpp:290
const SHAPE_LINE_CHAIN Reverse() const
Function Reverse()
int PointCount() const
Function PointCount()
SHOVE_STATUS pushOrShoveVia(VIA *aVia, const VECTOR2I &aForce, int aCurrentRank)
Definition: pns_shove.cpp:705
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:32
const SHAPE * Shape() const override
Function Shape()
Definition: pns_via.h:148
const OPT< INTERSECTION > SelfIntersecting() const
Function SelfIntersecting()
NODE * Branch()
Function Branch()
Definition: pns_node.cpp:106
const VECTOR2I & Pos() const
Definition: pns_via.h:100
bool EndsWithVia() const
Definition: pns_line.h:234
void Remove(ARC *aArc)
Function Remove()
Definition: pns_node.cpp:796
std::vector< LINE_PAIR > LINE_PAIR_VEC
Definition: pns_shove.h:97
SHOVE_STATUS onCollidingLine(LINE &aCurrent, LINE &aObstacle)
Definition: pns_shove.cpp:464
int Start() const
Definition: pns_layerset.h:83
bool LayersOverlap(const ITEM *aOther) const
Function LayersOverlap()
Definition: pns_item.h:163
const VECTOR2I & CPoint(int aIdx) const
Returns the aIdx-th point of the line
Definition: pns_line.h:173
SHOVE_STATUS onReverseCollidingVia(LINE &aCurrent, VIA *aObstacleVia)
Definition: pns_shove.cpp:907
std::vector< SHAPE_LINE_CHAIN > HULL_SET
Definition: pns_shove.h:94
void SetCollisionMask(int aMask)
const std::set< ITEM * > AssembleCluster(ITEM *aStart, int aLayer)
void SetShape(const SHAPE_LINE_CHAIN &aLine)
Assigns a shape to the line (a polyline/line chain)
Definition: pns_line.h:130
ROUTING_SETTINGS & Settings() const
Returns current router settings
bool IsLinkedChecked() const
Definition: pns_line.h:124
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
OPT_BOX2I totalAffectedArea() const
Definition: pns_shove.cpp:1250
NODE * reduceSpringback(const ITEM_SET &aHeadSet, VIA_HANDLE &aDraggedVia)
Definition: pns_shove.cpp:640
const SHAPE * Shape() const override
Returns the shape of the line
Definition: pns_line.h:137
SHOVE_STATUS ShoveLines(const LINE &aCurrentHead)
Definition: pns_shove.cpp:1266
const VECTOR2I & CPoint(int aIndex) const
Function Point()
JOINT.
Definition: pns_joint.h:43
int GetClearance(const ITEM *aA, const ITEM *aB) const
Returns the expected clearance between items a and b.
Definition: pns_node.cpp:97
void KillChildren()
Destroys all child nodes. Applicable only to the root node.
Definition: pns_node.cpp:1288
bool AddLockedSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:1637
const LINE AssembleLine(LINKED_ITEM *aSeg, int *aOriginSegmentIndex=NULL, bool aStopAtLockedJoints=false)
Function AssembleLine()
Definition: pns_node.cpp:893
#define NULL
SHOVE_STATUS onCollidingVia(ITEM *aCurrent, VIA *aObstacleVia)
Definition: pns_shove.cpp:832
void runOptimizer(NODE *aNode)
Definition: pns_shove.cpp:1545
void sanityCheck(LINE *aOld, LINE *aNew)
Definition: pns_shove.cpp:76
SHOVE_STATUS shoveIteration(int aIter)
Definition: pns_shove.cpp:1072
const SHAPE_LINE_CHAIN Hull(int aClearance=0, int aWalkaroundThickness=0) const override
Definition: pns_via.cpp:74
virtual const SHAPE * Shape() const
Function Shape()
Definition: pns_item.h:214
int Net() const
Definition: pns_item.h:149
static VIA * findViaByHandle(NODE *aNode, const VIA_HANDLE &handle)
Definition: pns_shove.cpp:1454
PNS_OPTIMIZATION_EFFORT OptimizerEffort() const
Returns the optimizer effort. Bigger means cleaner traces, but slower routing.
VECTOR2I::extended_type ecoord
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
void RestrictToSet(bool aEnabled, const std::set< ITEM * > &aSet)
SHOVE(NODE *aWorld, ROUTER *aRouter)
Definition: pns_shove.cpp:83
DEBUG_DECORATOR * Dbg() const
Definition: pns_algo_base.h:77
void SetSolidsOnly(bool aSolidsOnly)
bool pushSpringback(NODE *aNode, const OPT_BOX2I &aAffectedArea, VIA *aDraggedVia)
Definition: pns_shove.cpp:668
SHOVE_STATUS onCollidingSegment(LINE &aCurrent, SEGMENT *aObstacleSeg)
Definition: pns_shove.cpp:342
const LINKED_ITEMS & LinkList() const
Definition: pns_joint.h:196
void SetRank(int aRank) override
Definition: pns_line.cpp:852
virtual void Mark(int aMarker) override
Definition: pns_line.cpp:88
void UnlockSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:1676
void Clear()
Definition: pns_logger.cpp:47
JOINT * FindJoint(const VECTOR2I &aPos, int aLayer, int aNet)
Function FindJoint()
Definition: pns_node.cpp:1027
SHAPE_LINE_CHAIN & Line()
Modifiable accessor to the underlying shape
Definition: pns_line.h:143
SHOVE_STATUS ShoveDraggingVia(const VIA_HANDLE aOldVia, const VECTOR2I &aWhere, VIA_HANDLE &aNewVia)
Definition: pns_shove.cpp:1473
void SetPos(const VECTOR2I &aPos)
Definition: pns_via.h:105
virtual void Message(const wxString msg)
std::unique_ptr< typename std::remove_const< T >::type > Clone(const T &aItem)
Definition: pns_item.h:271
void Replace(ITEM *aOldItem, std::unique_ptr< ITEM > aNewItem)
Function Replace()
Definition: pns_node.cpp:766
void RemoveByMarker(int aMarker)
Definition: pns_node.cpp:1326
PnsKind
Supported item types
Definition: pns_item.h:59
std::vector< SPRINGBACK_TAG > m_nodeStack
Definition: pns_shove.h:157
virtual DEBUG_DECORATOR * GetDebugDecorator()=0
VECTOR2< T > Resize(T aNewLength) const
Function Resize returns a vector of the same direction, but length specified in aNewLength.
Definition: vector2d.h:392
VIA * m_draggedVia
Definition: pns_shove.h:168
virtual int Rank() const
Definition: pns_item.h:224
virtual int Marker() const override
Definition: pns_line.cpp:107
OPT_BOX2I m_affectedArea
Definition: pns_shove.h:150
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205
static LIB_PART * dummy()
Used to draw a dummy shape when a LIB_PART is not found in library.
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr) const
Function Collide()
Definition: shape.h:125
SHAPE_LINE_CHAIN.
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Function CheckColliding()
Definition: pns_node.cpp:427
bool HasLoops() const
Definition: pns_line.cpp:901
LAYER_RANGE layers
Definition: pns_via.h:44
bool OfKind(int aKindMask) const
Function OfKind()
Definition: pns_item.h:133
void replaceLine(LINE &aOld, LINE &aNew)
Definition: pns_shove.cpp:57
const ENTRIES & CItems() const
Definition: pns_itemset.h:141
PNS_OPTIMIZATION_EFFORT
Optimization effort
int Width() const
Returns line width
Definition: pns_line.h:192
SHOVE_STATUS shoveMainLoop()
Definition: pns_shove.cpp:1210
bool IsLocked() const
Definition: pns_item.h:236
OPT< BOX2I > OPT_BOX2I
Definition: box2.h:525
void SetInitialLine(LINE &aInitial)
Definition: pns_shove.cpp:1630
void SetDebugDecorator(DEBUG_DECORATOR *aDecorator)
Function SetDebugDecorator.
Definition: pns_algo_base.h:72
VECTOR2I pos
Definition: pns_via.h:43
PnsKind Kind() const
Function Kind()
Definition: pns_item.h:123
OPTIMIZER.
Definition: pns_optimizer.h:95
void Clear()
Function Clear() Removes all points from the line chain.
OPT< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:148
int getClearance(const ITEM *aA, const ITEM *aB) const
Definition: pns_shove.cpp:67
void replaceItems(ITEM *aOld, std::unique_ptr< ITEM > aNew)
Definition: pns_shove.cpp:47
void SetEffortLevel(int aEffort)
bool m_multiLineMode
Definition: pns_shove.h:172
OPT_LINE m_newHead
Definition: pns_shove.h:165
std::vector< LINE > m_lineStack
Definition: pns_shove.h:158
SHOVE_STATUS ShoveMultiLines(const ITEM_SET &aHeadSet)
Definition: pns_shove.cpp:1374
NODE * m_currentNode
Definition: pns_shove.h:162
void SetRestrictArea(const BOX2I &aArea)
const VIA & Via() const
Definition: pns_line.h:239
Push and Shove diff pair dimensions (gap) settings dialog.
SHOVE_STATUS walkaroundLoneVia(LINE &aCurrent, LINE &aObstacle, LINE &aShoved)
Definition: pns_shove.cpp:129
void Log(EVENT_TYPE evt, VECTOR2I pos, const ITEM *item=nullptr)
Definition: pns_logger.cpp:73
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Function Add()
Definition: pns_node.cpp:620
ROUTER_IFACE * GetInterface() const
Definition: pns_router.h:233
bool Expired() const
Definition: time_limit.cpp:39
LOGGER m_logger
Definition: pns_shove.h:167
int m_iter
Definition: pns_shove.h:170
virtual void Unmark(int aMarker=-1) override
Definition: pns_line.cpp:98
NODE * CurrentNode()
Definition: pns_shove.cpp:1616
void SetIterationLimit(const int aIterLimit)
const LAYER_RANGE & Layers() const
Definition: pns_item.h:151
void LockJoint(const VECTOR2I &aPos, const ITEM *aItem, bool aLock)
Definition: pns_node.cpp:1057
virtual int Marker() const
Definition: pns_item.h:221
int m_forceClearance
Definition: pns_shove.h:171
bool CompareGeometry(const SHAPE_LINE_CHAIN &aOther) const
bool HasLockedSegments() const
Definition: pns_line.cpp:1011
TIME_LIMIT ShoveTimeLimit() const