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  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_pre[0], path_walk[0],
85  path_post[0], aWindingDirection );
86  aPath.Walkaround( current_obs->m_hull, path_pre[1], path_walk[1],
87  path_post[1], !aWindingDirection );
88 
89  if( !aPath.Walkaround( current_obs->m_hull, path_pre[1], path_walk[1],
90  path_post[1], !aWindingDirection ) )
91  return STUCK;
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  char name[128];
109  snprintf(name, sizeof(name), "hull-%s-%d", aWindingDirection ? "cw" : "ccw", m_iteration );
110  Dbg()->AddLine( current_obs->m_hull, 0, 1, name);
111  snprintf(name, sizeof(name), "path-%s-%d", aWindingDirection ? "cw" : "ccw", m_iteration );
112  Dbg()->AddLine( aPath.CLine(), 1, 1, name );
113  }
114 
115  int len_pre = path_walk[0].Length();
116  int len_alt = path_walk[1].Length();
117 
118  LINE walk_path( aPath, path_walk[1] );
119 
120  bool alt_collides = static_cast<bool>( m_world->CheckColliding( &walk_path, m_itemMask ) );
121 
122  SHAPE_LINE_CHAIN pnew;
123 
124  /*if( !m_forceLongerPath && len_alt < len_pre && !alt_collides && !prev_recursive )
125  {
126  pnew = path_pre[1];
127  pnew.Append( path_walk[1] );
128  pnew.Append( path_post[1] );
129 
130  if( !path_post[1].PointCount() || !path_walk[1].PointCount() )
131  current_obs = nearestObstacle( LINE( aPath, path_pre[1] ) );
132  else
133  current_obs = nearestObstacle( LINE( aPath, path_post[1] ) );
134  }
135  else*/
136  {
137  pnew = path_pre[0];
138  pnew.Append( path_walk[0] );
139  pnew.Append( path_post[0] );
140 
141  if( path_post[0].PointCount() == 0 || path_walk[0].PointCount() == 0 )
142  current_obs = nearestObstacle( LINE( aPath, path_pre[0] ) );
143  else
144  current_obs = nearestObstacle( LINE( aPath, path_walk[0] ) );
145 
146  if( !current_obs )
147  {
148  current_obs = nearestObstacle( LINE( aPath, path_post[0] ) );
149  }
150  }
151 
152  pnew.Simplify();
153  aPath.SetShape( pnew );
154 
155  return IN_PROGRESS;
156 }
157 
158 
159 
161 {
162  auto ip = l.SelfIntersecting();
163 
164  if(!ip)
165  return false;
166  else {
167  int pidx = l.Split( ip->p );
168  auto lead = l.Slice(0, pidx);
169  auto tail = l.Slice(pidx + 1, -1);
170 
171  int pidx2 = tail.Split( ip->p );
172 
174  dbg->AddPoint( ip->p, 5 );
175 
176  l = lead;
177  l.Append( tail.Slice( 0, pidx2 ) );
178  //l = l.Slice(0, pidx);
179  return true;
180  }
181 
182 
183 }
184 
185 
186 
187 const WALKAROUND::RESULT WALKAROUND::Route( const LINE& aInitialPath )
188 {
189  LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
190  WALKAROUND_STATUS s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS;
191  SHAPE_LINE_CHAIN best_path;
192  RESULT result;
193 
194  // special case for via-in-the-middle-of-track placement
195  if( aInitialPath.PointCount() <= 1 )
196  {
197  if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(), m_itemMask ) )
198  return RESULT( STUCK, STUCK );
199 
200  return RESULT( DONE, DONE, aInitialPath, aInitialPath );
201  }
202 
203  start( aInitialPath );
204 
205  m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
207 
208  result.lineCw = aInitialPath;
209  result.lineCcw = aInitialPath;
210 
211  if( m_forceWinding )
212  {
213  s_cw = m_forceCw ? IN_PROGRESS : STUCK;
214  s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
215  m_forceSingleDirection = true;
216  } else {
217  m_forceSingleDirection = false;
218  }
219 
220  while( m_iteration < m_iterationLimit )
221  {
222  if( s_cw != STUCK )
223  s_cw = singleStep( path_cw, true );
224 
225  if( s_ccw != STUCK )
226  s_ccw = singleStep( path_ccw, false );
227 
228  //Dbg()->AddLine( path_cw.CLine(), 2, 10000 );
229 
230 
231  //printf("iter %d s_cw %d s_ccw %d\n", m_iteration, s_cw, s_ccw );
232 
233  auto old = path_cw.CLine();
234 
235  if( clipToLoopStart( path_cw.Line() ))
236  {
237  //printf("ClipCW\n");
238  //Dbg()->AddLine( old, 1, 40000 );
239  s_cw = ALMOST_DONE;
240  }
241 
242  if( clipToLoopStart( path_ccw.Line() ))
243  {
244  //printf("ClipCCW\n");
245  s_ccw = ALMOST_DONE;
246  }
247 
248 
249  if( s_cw != IN_PROGRESS )
250  {
251  result.lineCw = path_cw;
252  result.statusCw = s_cw;
253  }
254 
255  if( s_ccw != IN_PROGRESS )
256  {
257  result.lineCcw = path_ccw;
258  result.statusCcw = s_ccw;
259  }
260 
261  if( s_cw != IN_PROGRESS && s_ccw != IN_PROGRESS )
262  break;
263 
264  m_iteration++;
265  }
266 
267  if( s_cw == IN_PROGRESS )
268  {
269  result.lineCw = path_cw;
270  result.statusCw = ALMOST_DONE;
271  }
272 
273  if( s_ccw == IN_PROGRESS )
274  {
275  result.lineCcw = path_ccw;
276  result.statusCcw = ALMOST_DONE;
277  }
278 
279  result.lineCw.Line().Simplify();
280  result.lineCcw.Line().Simplify();
281 
282  if( result.lineCw.SegmentCount() < 1 || result.lineCw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
283  {
284  result.statusCw = STUCK;
285  }
286 
287  if( result.lineCw.PointCount() > 0 && result.lineCw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
288  {
289  result.statusCw = ALMOST_DONE;
290  }
291 
292  if( result.lineCcw.SegmentCount() < 1 || result.lineCcw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
293  {
294  result.statusCcw = STUCK;
295  }
296 
297  if( result.lineCcw.PointCount() > 0 && result.lineCcw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
298  {
299  result.statusCcw = ALMOST_DONE;
300  }
301 
302  return result;
303 }
304 
305 
306 
308  LINE& aWalkPath, bool aOptimize )
309 {
310  LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
311  WALKAROUND_STATUS s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS;
312  SHAPE_LINE_CHAIN best_path;
313 
314  // special case for via-in-the-middle-of-track placement
315  if( aInitialPath.PointCount() <= 1 )
316  {
317  if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(), m_itemMask ) )
318  return STUCK;
319 
320  aWalkPath = aInitialPath;
321  return DONE;
322  }
323 
324  start( aInitialPath );
325 
326  m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
328 
329  aWalkPath = aInitialPath;
330 
331  if( m_forceWinding )
332  {
333  s_cw = m_forceCw ? IN_PROGRESS : STUCK;
334  s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
335  m_forceSingleDirection = true;
336  } else {
337  m_forceSingleDirection = false;
338  }
339 
340  while( m_iteration < m_iterationLimit )
341  {
342  if( s_cw != STUCK )
343  s_cw = singleStep( path_cw, true );
344 
345  if( s_ccw != STUCK )
346  s_ccw = singleStep( path_ccw, false );
347 
348  if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) )
349  {
350  int len_cw = path_cw.CLine().Length();
351  int len_ccw = path_ccw.CLine().Length();
352 
353  if( m_forceLongerPath )
354  aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
355  else
356  aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
357 
358  break;
359  }
360  else if( s_cw == DONE && !m_forceLongerPath )
361  {
362  aWalkPath = path_cw;
363  break;
364  }
365  else if( s_ccw == DONE && !m_forceLongerPath )
366  {
367  aWalkPath = path_ccw;
368  break;
369  }
370 
371  m_iteration++;
372  }
373 
375  {
376  int len_cw = path_cw.CLine().Length();
377  int len_ccw = path_ccw.CLine().Length();
378 
379  if( m_forceLongerPath )
380  aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
381  else
382  aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
383  }
384 
386  {
387  // int len_cw = path_cw.GetCLine().Length();
388  // int len_ccw = path_ccw.GetCLine().Length();
389  bool found = false;
390 
391  SHAPE_LINE_CHAIN l = aWalkPath.CLine();
392 
393  for( int i = 0; i < l.SegmentCount(); i++ )
394  {
395  const SEG s = l.Segment( i );
396 
397  VECTOR2I nearest = s.NearestPoint( m_cursorPos );
398  VECTOR2I::extended_type dist_a = ( s.A - m_cursorPos ).SquaredEuclideanNorm();
399  VECTOR2I::extended_type dist_b = ( s.B - m_cursorPos ).SquaredEuclideanNorm();
400  VECTOR2I::extended_type dist_n = ( nearest - m_cursorPos ).SquaredEuclideanNorm();
401 
402  if( dist_n <= dist_a && dist_n < dist_b )
403  {
404  l.Remove( i + 1, -1 );
405  l.Append( nearest );
406  l.Simplify();
407  found = true;
408  break;
409  }
410  }
411 
412  if( found )
413  {
414  aWalkPath = aInitialPath;
415  aWalkPath.SetShape( l );
416  }
417  }
418 
419  aWalkPath.Line().Simplify();
420 
421  if( aWalkPath.SegmentCount() < 1 )
422  return STUCK;
423  if( aWalkPath.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
424  return ALMOST_DONE;
425  if( aWalkPath.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
426  return STUCK;
427 
428  WALKAROUND_STATUS st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK;
429 
430  if( st == DONE )
431  {
432  if( aOptimize )
434  }
435 
436  return st;
437 }
438 
439 }
const SHAPE_LINE_CHAIN & CLine() const
Const accessor to the underlying shape
Definition: pns_line.h:149
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:156
int SegmentCount() const
Returns the number of segments in the line
Definition: pns_line.h:155
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:161
bool EndsWithVia() const
Definition: pns_line.h:234
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:173
void SetShape(const SHAPE_LINE_CHAIN &aLine)
Assigns a shape to the line (a polyline/line chain)
Definition: pns_line.h:130
SHAPE_LINE_CHAIN & Simplify()
Function Simplify()
NODE::OPT_OBSTACLE m_currentObstacle[2]
#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:431
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:395
void start(const LINE &aInitialPath)
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:143
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:239
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:233
static ROUTER * GetInstance()
Definition: pns_router.cpp:85
VECTOR2I B
Definition: seg.h:48