KiCad PCB EDA Suite
pns_walkaround.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-2020 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 <core/optional.h>
23 
25 
26 #include "pns_walkaround.h"
27 #include "pns_optimizer.h"
28 #include "pns_utils.h"
29 #include "pns_router.h"
30 #include "pns_debug_decorator.h"
31 
32 namespace PNS {
33 
34 void WALKAROUND::start( const LINE& aInitialPath )
35 {
36  m_iteration = 0;
37  m_iterationLimit = 50;
38 }
39 
40 
42 {
44 
45  if( m_restrictedSet.empty() )
46  return obs;
47 
48  else if( obs && m_restrictedSet.find ( obs->m_item ) != m_restrictedSet.end() )
49  return obs;
50 
51  return NODE::OPT_OBSTACLE();
52 }
53 
54 
56  bool aWindingDirection )
57 {
58  OPT<OBSTACLE>& current_obs =
59  aWindingDirection ? m_currentObstacle[0] : m_currentObstacle[1];
60 
61  if( !current_obs )
62  return DONE;
63 
64  SHAPE_LINE_CHAIN path_walk[2];
65 
66  if( aPath.PointCount() > 1 )
67  {
68  VECTOR2I last = aPath.CPoint( -1 );
69 
70  if( ( current_obs->m_hull ).PointInside( last ) || ( current_obs->m_hull ).PointOnEdge( last ) )
71  {
73 
74  if( m_recursiveBlockageCount < 3 )
75  aPath.Line().Append( current_obs->m_hull.NearestPoint( last ) );
76  else
77  {
78  aPath = aPath.ClipToNearestObstacle( m_world );
79  return DONE;
80  }
81  }
82  }
83 
84  aPath.Walkaround( current_obs->m_hull, path_walk[0],
85  aWindingDirection );
86  aPath.Walkaround( current_obs->m_hull, path_walk[1],
87  !aWindingDirection );
88 
89  if( !aPath.Walkaround( current_obs->m_hull, path_walk[1], !aWindingDirection ) )
90  return STUCK;
91 
92  auto l =aPath.CLine();
93 
94 #if 0
95  if( m_logger )
96  {
97  m_logger->NewGroup( aWindingDirection ? "walk-cw" : "walk-ccw", m_iteration );
98  m_logger->Log( &path_walk[0], 0, "path_walk" );
99  m_logger->Log( &path_pre[0], 1, "path_pre" );
100  m_logger->Log( &path_post[0], 4, "path_post" );
101  m_logger->Log( &current_obs->m_hull, 2, "hull" );
102  m_logger->Log( current_obs->m_item, 3, "item" );
103  }
104 #endif
105 
106  if ( Dbg() )
107  {
108  Dbg()->BeginGroup("hull/walk");
109  char name[128];
110  snprintf(name, sizeof(name), "hull-%s-%d", aWindingDirection ? "cw" : "ccw", m_iteration );
111  Dbg()->AddLine( current_obs->m_hull, 1, 1, name);
112  snprintf(name, sizeof(name), "path-%s-%d", aWindingDirection ? "cw" : "ccw", m_iteration );
113  Dbg()->AddLine( aPath.CLine(), 2, 1, name );
114  Dbg()->EndGroup();
115  }
116 
117  int len_pre = path_walk[0].Length();
118  int len_alt = path_walk[1].Length();
119 
120  LINE walk_path( aPath, path_walk[1] );
121 
122  bool alt_collides = static_cast<bool>( m_world->CheckColliding( &walk_path, m_itemMask ) );
123 
124  SHAPE_LINE_CHAIN pnew;
125 
126  /*if( !m_forceLongerPath && len_alt < len_pre && !alt_collides && !prev_recursive )
127  {
128  pnew = path_pre[1];
129  pnew.Append( path_walk[1] );
130  pnew.Append( path_post[1] );
131 
132  if( !path_post[1].PointCount() || !path_walk[1].PointCount() )
133  current_obs = nearestObstacle( LINE( aPath, path_pre[1] ) );
134  else
135  current_obs = nearestObstacle( LINE( aPath, path_post[1] ) );
136  }
137  else*/
138  {
139  pnew = path_walk[0];
140  current_obs = nearestObstacle( LINE( aPath, path_walk[0] ) );
141  }
142 
143  pnew.Simplify();
144  aPath.SetShape( pnew );
145 
146  return IN_PROGRESS;
147 }
148 
149 
150 
152 {
153  auto ip = l.SelfIntersecting();
154 
155  if(!ip)
156  return false;
157  else {
158  int pidx = l.Split( ip->p );
159  auto lead = l.Slice(0, pidx);
160  auto tail = l.Slice(pidx + 1, -1);
161 
162  int pidx2 = tail.Split( ip->p );
163 
165  dbg->AddPoint( ip->p, 5 );
166 
167  l = lead;
168  l.Append( tail.Slice( 0, pidx2 ) );
169  //l = l.Slice(0, pidx);
170  return true;
171  }
172 
173 
174 }
175 
176 
177 
178 const WALKAROUND::RESULT WALKAROUND::Route( const LINE& aInitialPath )
179 {
180  LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
181  WALKAROUND_STATUS s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS;
182  SHAPE_LINE_CHAIN best_path;
183  RESULT result;
184 
185  // special case for via-in-the-middle-of-track placement
186  if( aInitialPath.PointCount() <= 1 )
187  {
188  if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(), m_itemMask ) )
189  return RESULT( STUCK, STUCK );
190 
191  return RESULT( DONE, DONE, aInitialPath, aInitialPath );
192  }
193 
194  start( aInitialPath );
195 
196  m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
198 
199  result.lineCw = aInitialPath;
200  result.lineCcw = aInitialPath;
201 
202  if( m_forceWinding )
203  {
204  s_cw = m_forceCw ? IN_PROGRESS : STUCK;
205  s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
206  m_forceSingleDirection = true;
207  } else {
208  m_forceSingleDirection = false;
209  }
210 
211  while( m_iteration < m_iterationLimit )
212  {
213  if( s_cw != STUCK )
214  s_cw = singleStep( path_cw, true );
215 
216  if( s_ccw != STUCK )
217  s_ccw = singleStep( path_ccw, false );
218 
219  auto old = path_cw.CLine();
220 
221  if( clipToLoopStart( path_cw.Line() ) )
222  s_cw = ALMOST_DONE;
223 
224  if( clipToLoopStart( path_ccw.Line() ) )
225  s_ccw = ALMOST_DONE;
226 
227 
228  if( s_cw != IN_PROGRESS )
229  {
230  result.lineCw = path_cw;
231  result.statusCw = s_cw;
232  }
233 
234  if( s_ccw != IN_PROGRESS )
235  {
236  result.lineCcw = path_ccw;
237  result.statusCcw = s_ccw;
238  }
239 
240  if( s_cw != IN_PROGRESS && s_ccw != IN_PROGRESS )
241  break;
242 
243  m_iteration++;
244  }
245 
246  if( s_cw == IN_PROGRESS )
247  {
248  result.lineCw = path_cw;
249  result.statusCw = ALMOST_DONE;
250  }
251 
252  if( s_ccw == IN_PROGRESS )
253  {
254  result.lineCcw = path_ccw;
255  result.statusCcw = ALMOST_DONE;
256  }
257 
258  result.lineCw.Line().Simplify();
259  result.lineCcw.Line().Simplify();
260 
261  if( result.lineCw.SegmentCount() < 1 || result.lineCw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
262  {
263  result.statusCw = STUCK;
264  }
265 
266  if( result.lineCw.PointCount() > 0 && result.lineCw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
267  {
268  result.statusCw = ALMOST_DONE;
269  }
270 
271  if( result.lineCcw.SegmentCount() < 1 || result.lineCcw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
272  {
273  result.statusCcw = STUCK;
274  }
275 
276  if( result.lineCcw.PointCount() > 0 && result.lineCcw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
277  {
278  result.statusCcw = ALMOST_DONE;
279  }
280 
281  return result;
282 }
283 
284 
285 
287  LINE& aWalkPath, bool aOptimize )
288 {
289  LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
290  WALKAROUND_STATUS s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS;
291  SHAPE_LINE_CHAIN best_path;
292 
293  // special case for via-in-the-middle-of-track placement
294  if( aInitialPath.PointCount() <= 1 )
295  {
296  if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(), m_itemMask ) )
297  return STUCK;
298 
299  aWalkPath = aInitialPath;
300  return DONE;
301  }
302 
303  start( aInitialPath );
304 
305  m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
307 
308  aWalkPath = aInitialPath;
309 
310  if( m_forceWinding )
311  {
312  s_cw = m_forceCw ? IN_PROGRESS : STUCK;
313  s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
314  m_forceSingleDirection = true;
315  } else {
316  m_forceSingleDirection = false;
317  }
318 
319  while( m_iteration < m_iterationLimit )
320  {
321  if( s_cw != STUCK )
322  s_cw = singleStep( path_cw, true );
323 
324  if( s_ccw != STUCK )
325  s_ccw = singleStep( path_ccw, false );
326 
327  if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) )
328  {
329  int len_cw = path_cw.CLine().Length();
330  int len_ccw = path_ccw.CLine().Length();
331 
332  if( m_forceLongerPath )
333  aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
334  else
335  aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
336 
337  break;
338  }
339  else if( s_cw == DONE && !m_forceLongerPath )
340  {
341  aWalkPath = path_cw;
342  break;
343  }
344  else if( s_ccw == DONE && !m_forceLongerPath )
345  {
346  aWalkPath = path_ccw;
347  break;
348  }
349 
350  m_iteration++;
351  }
352 
354  {
355  int len_cw = path_cw.CLine().Length();
356  int len_ccw = path_ccw.CLine().Length();
357 
358  if( m_forceLongerPath )
359  aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
360  else
361  aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
362  }
363 
365  {
366  // int len_cw = path_cw.GetCLine().Length();
367  // int len_ccw = path_ccw.GetCLine().Length();
368  bool found = false;
369 
370  SHAPE_LINE_CHAIN l = aWalkPath.CLine();
371 
372  for( int i = 0; i < l.SegmentCount(); i++ )
373  {
374  const SEG s = l.Segment( i );
375 
376  VECTOR2I nearest = s.NearestPoint( m_cursorPos );
377  VECTOR2I::extended_type dist_a = ( s.A - m_cursorPos ).SquaredEuclideanNorm();
378  VECTOR2I::extended_type dist_b = ( s.B - m_cursorPos ).SquaredEuclideanNorm();
379  VECTOR2I::extended_type dist_n = ( nearest - m_cursorPos ).SquaredEuclideanNorm();
380 
381  if( dist_n <= dist_a && dist_n < dist_b )
382  {
383  l.Remove( i + 1, -1 );
384  l.Append( nearest );
385  l.Simplify();
386  found = true;
387  break;
388  }
389  }
390 
391  if( found )
392  {
393  aWalkPath = aInitialPath;
394  aWalkPath.SetShape( l );
395  }
396  }
397 
398  aWalkPath.Line().Simplify();
399 
400  if( aWalkPath.SegmentCount() < 1 )
401  return STUCK;
402  if( aWalkPath.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
403  return ALMOST_DONE;
404  if( aWalkPath.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
405  return STUCK;
406 
407  WALKAROUND_STATUS st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK;
408 
409  if( st == DONE )
410  {
411  if( aOptimize )
413  }
414 
415  return st;
416 }
417 
418 }
const SHAPE_LINE_CHAIN & CLine() const
Definition: pns_line.h:137
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:76
bool clipToLoopStart(SHAPE_LINE_CHAIN &l)
OPT_OBSTACLE NearestObstacle(const LINE *aLine, int aKindMask=ITEM::ANY_T, const std::set< ITEM * > *aRestrictedSet=NULL)
Follow the line in search of an obstacle that is nearest to the starting to the line's starting point...
Definition: pns_node.cpp:285
long long int Length() const
Function Length()
virtual void AddPoint(VECTOR2I aP, int aColor, int aSize=100000, const std::string aName="")
int Split(const VECTOR2I &aP)
Function Split()
virtual void AddLine(const SHAPE_LINE_CHAIN &aLine, int aType=0, int aWidth=0, const std::string aName="")
bool Walkaround(SHAPE_LINE_CHAIN aObstacle, SHAPE_LINE_CHAIN &aPre, SHAPE_LINE_CHAIN &aWalk, SHAPE_LINE_CHAIN &aPost, bool aCw) const
Calculate a line tightly wrapping a convex hull of an obstacle object (aObstacle).
int SegmentCount() const
Definition: pns_line.h:139
SHAPE_LINE_CHAIN & Simplify(bool aRemoveColinear=true)
Function Simplify()
const SHAPE_LINE_CHAIN Slice(int aStartIndex, int aEndIndex=-1) const
Function Slice()
WALKAROUND_STATUS statusCw
LOGGER * m_logger
Definition: pns_algo_base.h:88
VECTOR2I m_cursorPos
WALKAROUND_STATUS Route(const LINE &aInitialPath, LINE &aWalkPath, bool aOptimize=true)
WALKAROUND_STATUS singleStep(LINE &aPath, bool aWindingDirection)
std::set< ITEM * > m_restrictedSet
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:60
const OPT< INTERSECTION > SelfIntersecting() const
Function SelfIntersecting()
int PointCount() const
Definition: pns_line.h:140
bool EndsWithVia() const
Definition: pns_line.h:191
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
const VECTOR2I & CPoint(int aIdx) const
Definition: pns_line.h:145
void SetShape(const SHAPE_LINE_CHAIN &aLine)
Return the shape of the line.
Definition: pns_line.h:126
NODE::OPT_OBSTACLE m_currentObstacle[2]
#define NULL
static bool Optimize(LINE *aLine, int aEffortLevel, NODE *aWorld, const VECTOR2I aV=VECTOR2I(0, 0))
DEBUG_DECORATOR * Dbg() const
Definition: pns_algo_base.h:78
const LINE ClipToNearestObstacle(NODE *aNode) const
Clip the line to a given range of vertices.
Definition: pns_line.cpp:456
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Compute a point on the segment (this) that is closest to point aP.
Definition: seg.h:422
void start(const LINE &aInitialPath)
void Remove(int aStartIndex, int aEndIndex)
Function Remove()
int SegmentCount() const
Function SegmentCount()
SHAPE_LINE_CHAIN & Line()
Definition: pns_line.h:136
Definition: seg.h:41
virtual void BeginGroup(const std::string name)
virtual DEBUG_DECORATOR * GetDebugDecorator()=0
SEG Segment(int aIndex)
Function Segment()
NODE::OPT_OBSTACLE nearestObstacle(const LINE &aPath)
const char * name
Definition: DXF_plotter.cpp:59
Reduce corner cost by merging obtuse segments.
SHAPE_LINE_CHAIN.
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Check if the item collides with anything else in the world, and if found, returns the obstacle.
Definition: pns_node.cpp:427
WALKAROUND_STATUS statusCcw
VECTOR2I A
Definition: seg.h:49
boost::optional< T > OPT
Definition: optional.h:7
OPT< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:152
const VIA & Via() const
Definition: pns_line.h:196
Push and Shove diff pair dimensions (gap) settings dialog.
void Log(EVENT_TYPE evt, VECTOR2I pos, const ITEM *item=nullptr)
Definition: pns_logger.cpp:73
ROUTER_IFACE * GetInterface() const
Definition: pns_router.h:215
static ROUTER * GetInstance()
Definition: pns_router.cpp:79
VECTOR2I B
Definition: seg.h:50