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 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_pre[2], path_walk[2], path_post[2];
65 
66  VECTOR2I last = aPath.CPoint( -1 );
67 
68  if( ( current_obs->m_hull ).PointInside( last ) || ( current_obs->m_hull ).PointOnEdge( last ) )
69  {
71 
72  if( m_recursiveBlockageCount < 3 )
73  aPath.Line().Append( current_obs->m_hull.NearestPoint( last ) );
74  else
75  {
76  aPath = aPath.ClipToNearestObstacle( m_world );
77  return DONE;
78  }
79  }
80 
81  aPath.Walkaround( current_obs->m_hull, path_pre[0], path_walk[0],
82  path_post[0], aWindingDirection );
83  aPath.Walkaround( current_obs->m_hull, path_pre[1], path_walk[1],
84  path_post[1], !aWindingDirection );
85 
86  if( ! aPath.Walkaround( current_obs->m_hull, path_pre[1], path_walk[1],
87  path_post[1], !aWindingDirection ) )
88  return STUCK;
89  auto l =aPath.CLine();
90 #ifdef DEBUG
91  if( m_logger )
92  {
93  m_logger->NewGroup( aWindingDirection ? "walk-cw" : "walk-ccw", m_iteration );
94  m_logger->Log( &path_walk[0], 0, "path_walk" );
95  m_logger->Log( &path_pre[0], 1, "path_pre" );
96  m_logger->Log( &path_post[0], 4, "path_post" );
97  m_logger->Log( &current_obs->m_hull, 2, "hull" );
98  m_logger->Log( current_obs->m_item, 3, "item" );
99  }
100 #endif
101 
102  if ( Dbg() )
103  {
104  char name[128];
105  snprintf(name, sizeof(name), "hull-%s-%d", aWindingDirection ? "cw" : "ccw", m_iteration );
106  Dbg()->AddLine( current_obs->m_hull, 0, 1, name);
107  snprintf(name, sizeof(name), "path-%s-%d", aWindingDirection ? "cw" : "ccw", m_iteration );
108  Dbg()->AddLine( aPath.CLine(), 1, 1, name );
109  }
110 
111  int len_pre = path_walk[0].Length();
112  int len_alt = path_walk[1].Length();
113 
114  LINE walk_path( aPath, path_walk[1] );
115 
116  bool alt_collides = static_cast<bool>( m_world->CheckColliding( &walk_path, m_itemMask ) );
117 
118  SHAPE_LINE_CHAIN pnew;
119 
120  /*if( !m_forceLongerPath && len_alt < len_pre && !alt_collides && !prev_recursive )
121  {
122  pnew = path_pre[1];
123  pnew.Append( path_walk[1] );
124  pnew.Append( path_post[1] );
125 
126  if( !path_post[1].PointCount() || !path_walk[1].PointCount() )
127  current_obs = nearestObstacle( LINE( aPath, path_pre[1] ) );
128  else
129  current_obs = nearestObstacle( LINE( aPath, path_post[1] ) );
130  }
131  else*/
132  {
133  pnew = path_pre[0];
134  pnew.Append( path_walk[0] );
135  pnew.Append( path_post[0] );
136 
137  if( path_post[0].PointCount() == 0 || path_walk[0].PointCount() == 0 )
138  current_obs = nearestObstacle( LINE( aPath, path_pre[0] ) );
139  else
140  current_obs = nearestObstacle( LINE( aPath, path_walk[0] ) );
141 
142  if( !current_obs )
143  {
144  current_obs = nearestObstacle( LINE( aPath, path_post[0] ) );
145  }
146  }
147 
148  pnew.Simplify();
149  aPath.SetShape( pnew );
150 
151  return IN_PROGRESS;
152 }
153 
154 
155 
157 {
158  auto ip = l.SelfIntersecting();
159 
160  if(!ip)
161  return false;
162  else {
163  int pidx = l.Split( ip->p );
164  auto lead = l.Slice(0, pidx);
165  auto tail = l.Slice(pidx + 1, -1);
166 
167  int pidx2 = tail.Split( ip->p );
168 
170  dbg->AddPoint( ip->p, 5 );
171 
172  l = lead;
173  l.Append( tail.Slice( 0, pidx2 ) );
174  //l = l.Slice(0, pidx);
175  return true;
176  }
177 
178 
179 }
180 
181 
182 
183 const WALKAROUND::RESULT WALKAROUND::Route( const LINE& aInitialPath )
184 {
185  LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
186  WALKAROUND_STATUS s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS;
187  SHAPE_LINE_CHAIN best_path;
188  RESULT result;
189 
190  // special case for via-in-the-middle-of-track placement
191  if( aInitialPath.PointCount() <= 1 )
192  {
193  if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(), m_itemMask ) )
194  return RESULT( STUCK, STUCK );
195 
196  return RESULT( DONE, DONE, aInitialPath, aInitialPath );
197  }
198 
199  start( aInitialPath );
200 
201  m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
203 
204  result.lineCw = aInitialPath;
205  result.lineCcw = aInitialPath;
206 
207  if( m_forceWinding )
208  {
209  s_cw = m_forceCw ? IN_PROGRESS : STUCK;
210  s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
211  m_forceSingleDirection = true;
212  } else {
213  m_forceSingleDirection = false;
214  }
215 
216  while( m_iteration < m_iterationLimit )
217  {
218  if( s_cw != STUCK )
219  s_cw = singleStep( path_cw, true );
220 
221  if( s_ccw != STUCK )
222  s_ccw = singleStep( path_ccw, false );
223 
224  //Dbg()->AddLine( path_cw.CLine(), 2, 10000 );
225 
226 
227  //printf("iter %d s_cw %d s_ccw %d\n", m_iteration, s_cw, s_ccw );
228 
229  auto old = path_cw.CLine();
230 
231  if( clipToLoopStart( path_cw.Line() ))
232  {
233  //printf("ClipCW\n");
234  //Dbg()->AddLine( old, 1, 40000 );
235  s_cw = ALMOST_DONE;
236  }
237 
238  if( clipToLoopStart( path_ccw.Line() ))
239  {
240  //printf("ClipCCW\n");
241  s_ccw = ALMOST_DONE;
242  }
243 
244 
245  if( s_cw != IN_PROGRESS )
246  {
247  result.lineCw = path_cw;
248  result.statusCw = s_cw;
249  }
250 
251  if( s_ccw != IN_PROGRESS )
252  {
253  result.lineCcw = path_ccw;
254  result.statusCcw = s_ccw;
255  }
256 
257  if( s_cw != IN_PROGRESS && s_ccw != IN_PROGRESS )
258  break;
259 
260  m_iteration++;
261  }
262 
263  if( s_cw == IN_PROGRESS )
264  {
265  result.lineCw = path_cw;
266  result.statusCw = ALMOST_DONE;
267  }
268 
269  if( s_ccw == IN_PROGRESS )
270  {
271  result.lineCcw = path_ccw;
272  result.statusCcw = ALMOST_DONE;
273  }
274 
275  result.lineCw.Line().Simplify();
276  result.lineCcw.Line().Simplify();
277 
278  if( result.lineCw.SegmentCount() < 1 || result.lineCw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
279  {
280  result.statusCw = STUCK;
281  }
282 
283  if( result.lineCw.PointCount() > 0 && result.lineCw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
284  {
285  result.statusCw = ALMOST_DONE;
286  }
287 
288  if( result.lineCcw.SegmentCount() < 1 || result.lineCcw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
289  {
290  result.statusCcw = STUCK;
291  }
292 
293  if( result.lineCcw.PointCount() > 0 && result.lineCcw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
294  {
295  result.statusCcw = ALMOST_DONE;
296  }
297 
298  return result;
299 }
300 
301 
302 
304  LINE& aWalkPath, bool aOptimize )
305 {
306  LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
307  WALKAROUND_STATUS s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS;
308  SHAPE_LINE_CHAIN best_path;
309 
310  // special case for via-in-the-middle-of-track placement
311  if( aInitialPath.PointCount() <= 1 )
312  {
313  if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(), m_itemMask ) )
314  return STUCK;
315 
316  aWalkPath = aInitialPath;
317  return DONE;
318  }
319 
320  start( aInitialPath );
321 
322  m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
324 
325  aWalkPath = aInitialPath;
326 
327  if( m_forceWinding )
328  {
329  s_cw = m_forceCw ? IN_PROGRESS : STUCK;
330  s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
331  m_forceSingleDirection = true;
332  } else {
333  m_forceSingleDirection = false;
334  }
335 
336  while( m_iteration < m_iterationLimit )
337  {
338  if( s_cw != STUCK )
339  s_cw = singleStep( path_cw, true );
340 
341  if( s_ccw != STUCK )
342  s_ccw = singleStep( path_ccw, false );
343 
344  if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) )
345  {
346  int len_cw = path_cw.CLine().Length();
347  int len_ccw = path_ccw.CLine().Length();
348 
349  if( m_forceLongerPath )
350  aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
351  else
352  aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
353 
354  break;
355  }
356  else if( s_cw == DONE && !m_forceLongerPath )
357  {
358  aWalkPath = path_cw;
359  break;
360  }
361  else if( s_ccw == DONE && !m_forceLongerPath )
362  {
363  aWalkPath = path_ccw;
364  break;
365  }
366 
367  m_iteration++;
368  }
369 
371  {
372  int len_cw = path_cw.CLine().Length();
373  int len_ccw = path_ccw.CLine().Length();
374 
375  if( m_forceLongerPath )
376  aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
377  else
378  aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
379  }
380 
382  {
383  // int len_cw = path_cw.GetCLine().Length();
384  // int len_ccw = path_ccw.GetCLine().Length();
385  bool found = false;
386 
387  SHAPE_LINE_CHAIN l = aWalkPath.CLine();
388 
389  for( int i = 0; i < l.SegmentCount(); i++ )
390  {
391  const SEG s = l.Segment( i );
392 
393  VECTOR2I nearest = s.NearestPoint( m_cursorPos );
394  VECTOR2I::extended_type dist_a = ( s.A - m_cursorPos ).SquaredEuclideanNorm();
395  VECTOR2I::extended_type dist_b = ( s.B - m_cursorPos ).SquaredEuclideanNorm();
396  VECTOR2I::extended_type dist_n = ( nearest - m_cursorPos ).SquaredEuclideanNorm();
397 
398  if( dist_n <= dist_a && dist_n < dist_b )
399  {
400  l.Remove( i + 1, -1 );
401  l.Append( nearest );
402  l.Simplify();
403  found = true;
404  break;
405  }
406  }
407 
408  if( found )
409  {
410  aWalkPath = aInitialPath;
411  aWalkPath.SetShape( l );
412  }
413  }
414 
415  aWalkPath.Line().Simplify();
416 
417  if( aWalkPath.SegmentCount() < 1 )
418  return STUCK;
419  if( aWalkPath.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
420  return ALMOST_DONE;
421  if( aWalkPath.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
422  return STUCK;
423 
424  WALKAROUND_STATUS st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK;
425 
426  if( st == DONE )
427  {
428  if( aOptimize )
430  }
431 
432  return st;
433 }
434 
435 }
const SHAPE_LINE_CHAIN & CLine() const
Const accessor to the underlying shape
Definition: pns_line.h:144
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:77
bool clipToLoopStart(SHAPE_LINE_CHAIN &l)
long long int Length() const
Function Length()
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="")
OPT_OBSTACLE NearestObstacle(const LINE *aItem, int aKindMask=ITEM::ANY_T, const std::set< ITEM * > *aRestrictedSet=NULL)
Function NearestObstacle()
Definition: pns_node.cpp:304
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:162
int SegmentCount() const
Returns the number of segments in the line
Definition: pns_line.h:150
const SHAPE_LINE_CHAIN Slice(int aStartIndex, int aEndIndex=-1) const
Function Slice()
WALKAROUND_STATUS statusCw
LOGGER * m_logger
Definition: pns_algo_base.h:85
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
const OPT< INTERSECTION > SelfIntersecting() const
Function SelfIntersecting()
int PointCount() const
Returns the number of points in the line
Definition: pns_line.h:156
bool EndsWithVia() const
Definition: pns_line.h:276
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
const VECTOR2I & CPoint(int aIdx) const
Returns the aIdx-th point of the line
Definition: pns_line.h:168
void SetShape(const SHAPE_LINE_CHAIN &aLine)
Assigns a shape to the line (a polyline/line chain)
Definition: pns_line.h:125
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
NODE::OPT_OBSTACLE m_currentObstacle[2]
void NewGroup(const std::string &aName, int aIter=0)
Definition: pns_logger.cpp:55
#define NULL
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
DEBUG_DECORATOR * Dbg() const
Definition: pns_algo_base.h:77
const LINE ClipToNearestObstacle(NODE *aNode) const
Clips the line to the nearest obstacle, traversing from the line's start vertex (0).
Definition: pns_line.cpp:437
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:392
void start(const LINE &aInitialPath)
void Log(const ITEM *aItem, int aKind=0, const std::string &aName=std::string())
Definition: pns_logger.cpp:75
void Remove(int aStartIndex, int aEndIndex)
Function Remove()
int SegmentCount() const
Function SegmentCount()
SHAPE_LINE_CHAIN & Line()
Modifiable accessor to the underlying shape
Definition: pns_line.h:138
Definition: seg.h:39
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:60
SHAPE_LINE_CHAIN.
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Function CheckColliding()
Definition: pns_node.cpp:427
WALKAROUND_STATUS statusCcw
virtual void AddPoint(VECTOR2I aP, int aColor, const std::string aName="")
VECTOR2I A
Definition: seg.h:47
boost::optional< T > OPT
Definition: optional.h:7
OPT< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:148
const VIA & Via() const
Definition: pns_line.h:281
Push and Shove diff pair dimensions (gap) settings dialog.
ROUTER_IFACE * GetInterface() const
Definition: pns_router.h:230
static ROUTER * GetInstance()
Definition: pns_router.cpp:83
VECTOR2I B
Definition: seg.h:48