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 
31 namespace PNS {
32 
33 void WALKAROUND::start( const LINE& aInitialPath )
34 {
35  m_iteration = 0;
36  m_iterationLimit = 50;
37 }
38 
39 
41 {
43 
44  if( m_restrictedSet.empty() )
45  return obs;
46 
47  else if( obs && m_restrictedSet.find ( obs->m_item ) != m_restrictedSet.end() )
48  return obs;
49 
50  return NODE::OPT_OBSTACLE();
51 }
52 
53 
55  bool aWindingDirection )
56 {
57  OPT<OBSTACLE>& current_obs =
58  aWindingDirection ? m_currentObstacle[0] : m_currentObstacle[1];
59 
60  bool& prev_recursive = aWindingDirection ? m_recursiveCollision[0] : m_recursiveCollision[1];
61 
62  if( !current_obs )
63  return DONE;
64 
65  SHAPE_LINE_CHAIN path_pre[2], path_walk[2], path_post[2];
66 
67  VECTOR2I last = aPath.CPoint( -1 );
68 
69  if( ( current_obs->m_hull ).PointInside( last ) || ( current_obs->m_hull ).PointOnEdge( last ) )
70  {
72 
73  if( m_recursiveBlockageCount < 3 )
74  aPath.Line().Append( current_obs->m_hull.NearestPoint( last ) );
75  else
76  {
77  aPath = aPath.ClipToNearestObstacle( m_world );
78  return DONE;
79  }
80  }
81 
82  aPath.Walkaround( current_obs->m_hull, path_pre[0], path_walk[0],
83  path_post[0], aWindingDirection );
84  aPath.Walkaround( current_obs->m_hull, path_pre[1], path_walk[1],
85  path_post[1], !aWindingDirection );
86 
87 #ifdef DEBUG
88  m_logger.NewGroup( aWindingDirection ? "walk-cw" : "walk-ccw", m_iteration );
89  m_logger.Log( &path_walk[0], 0, "path-walk" );
90  m_logger.Log( &path_pre[0], 1, "path-pre" );
91  m_logger.Log( &path_post[0], 4, "path-post" );
92  m_logger.Log( &current_obs->m_hull, 2, "hull" );
93  m_logger.Log( current_obs->m_item, 3, "item" );
94 #endif
95 
96  int len_pre = path_walk[0].Length();
97  int len_alt = path_walk[1].Length();
98 
99  LINE walk_path( aPath, path_walk[1] );
100 
101  bool alt_collides = static_cast<bool>( m_world->CheckColliding( &walk_path, m_itemMask ) );
102 
103  SHAPE_LINE_CHAIN pnew;
104 
105  if( !m_forceLongerPath && len_alt < len_pre && !alt_collides && !prev_recursive )
106  {
107  pnew = path_pre[1];
108  pnew.Append( path_walk[1] );
109  pnew.Append( path_post[1] );
110 
111  if( !path_post[1].PointCount() || !path_walk[1].PointCount() )
112  current_obs = nearestObstacle( LINE( aPath, path_pre[1] ) );
113  else
114  current_obs = nearestObstacle( LINE( aPath, path_post[1] ) );
115  prev_recursive = false;
116  }
117  else
118  {
119  pnew = path_pre[0];
120  pnew.Append( path_walk[0] );
121  pnew.Append( path_post[0] );
122 
123  if( !path_post[0].PointCount() || !path_walk[0].PointCount() )
124  current_obs = nearestObstacle( LINE( aPath, path_pre[0] ) );
125  else
126  current_obs = nearestObstacle( LINE( aPath, path_walk[0] ) );
127 
128  if( !current_obs )
129  {
130  prev_recursive = false;
131  current_obs = nearestObstacle( LINE( aPath, path_post[0] ) );
132  }
133  else
134  prev_recursive = true;
135  }
136 
137  pnew.Simplify();
138  aPath.SetShape( pnew );
139 
140  return IN_PROGRESS;
141 }
142 
143 
145  LINE& aWalkPath, bool aOptimize )
146 {
147  LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
148  WALKAROUND_STATUS s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS;
149  SHAPE_LINE_CHAIN best_path;
150 
151  // special case for via-in-the-middle-of-track placement
152  if( aInitialPath.PointCount() <= 1 )
153  {
154  if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(), m_itemMask ) )
155  return STUCK;
156 
157  aWalkPath = aInitialPath;
158  return DONE;
159  }
160 
161  start( aInitialPath );
162 
163  m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
165 
166  aWalkPath = aInitialPath;
167 
168  if( m_forceWinding )
169  {
170  s_cw = m_forceCw ? IN_PROGRESS : STUCK;
171  s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
172  m_forceSingleDirection = true;
173  } else {
174  m_forceSingleDirection = false;
175  }
176 
177  while( m_iteration < m_iterationLimit )
178  {
179  if( s_cw != STUCK )
180  s_cw = singleStep( path_cw, true );
181 
182  if( s_ccw != STUCK )
183  s_ccw = singleStep( path_ccw, false );
184 
185  if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) )
186  {
187  int len_cw = path_cw.CLine().Length();
188  int len_ccw = path_ccw.CLine().Length();
189 
190  if( m_forceLongerPath )
191  aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
192  else
193  aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
194 
195  break;
196  }
197  else if( s_cw == DONE && !m_forceLongerPath )
198  {
199  aWalkPath = path_cw;
200  break;
201  }
202  else if( s_ccw == DONE && !m_forceLongerPath )
203  {
204  aWalkPath = path_ccw;
205  break;
206  }
207 
208  m_iteration++;
209  }
210 
212  {
213  int len_cw = path_cw.CLine().Length();
214  int len_ccw = path_ccw.CLine().Length();
215 
216  if( m_forceLongerPath )
217  aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
218  else
219  aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
220  }
221 
223  {
224  // int len_cw = path_cw.GetCLine().Length();
225  // int len_ccw = path_ccw.GetCLine().Length();
226  bool found = false;
227 
228  SHAPE_LINE_CHAIN l = aWalkPath.CLine();
229 
230  for( int i = 0; i < l.SegmentCount(); i++ )
231  {
232  const SEG s = l.Segment( i );
233 
234  VECTOR2I nearest = s.NearestPoint( m_cursorPos );
235  VECTOR2I::extended_type dist_a = ( s.A - m_cursorPos ).SquaredEuclideanNorm();
236  VECTOR2I::extended_type dist_b = ( s.B - m_cursorPos ).SquaredEuclideanNorm();
237  VECTOR2I::extended_type dist_n = ( nearest - m_cursorPos ).SquaredEuclideanNorm();
238 
239  if( dist_n <= dist_a && dist_n < dist_b )
240  {
241  l.Remove( i + 1, -1 );
242  l.Append( nearest );
243  l.Simplify();
244  found = true;
245  break;
246  }
247  }
248 
249  if( found )
250  {
251  aWalkPath = aInitialPath;
252  aWalkPath.SetShape( l );
253  }
254  }
255 
256  aWalkPath.Line().Simplify();
257 
258  if( aWalkPath.SegmentCount() < 1 )
259  return STUCK;
260  if( aWalkPath.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
261  return STUCK;
262  if( aWalkPath.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
263  return STUCK;
264 
265  WALKAROUND_STATUS st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK;
266 
267  if( st == DONE )
268  {
269  if( aOptimize )
271  }
272 
273  return st;
274 }
275 
276 }
const SHAPE_LINE_CHAIN & CLine() const
Const accessor to the underlying shape
Definition: pns_line.h:123
VECTOR2_TRAITS< int >::extended_type extended_type
Definition: vector2d.h:77
const VIA & Via() const
Definition: pns_line.h:253
OPT_OBSTACLE NearestObstacle(const LINE *aItem, int aKindMask=ITEM::ANY_T, const std::set< ITEM * > *aRestrictedSet=NULL)
Function NearestObstacle()
Definition: pns_node.cpp:302
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:158
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
void Append(int aX, int aY, bool aAllowDuplication=false)
Function Append()
int PointCount() const
Returns the number of points in the line
Definition: pns_line.h:135
void SetShape(const SHAPE_LINE_CHAIN &aLine)
Assigns a shape to the line (a polyline/line chain)
Definition: pns_line.h:105
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
int SegmentCount() const
Returns the number of segments in the line
Definition: pns_line.h:129
bool EndsWithVia() const
Definition: pns_line.h:248
void start(const LINE &aInitialPath)
void Remove(int aStartIndex, int aEndIndex)
Function Remove()
SHAPE_LINE_CHAIN & Line()
Modifiable accessor to the underlying shape
Definition: pns_line.h:117
const VECTOR2I NearestPoint(const VECTOR2I &aP) const
Function NearestPoint()
Definition: seg.h:364
bool m_recursiveCollision[2]
Definition: seg.h:36
SEG Segment(int aIndex)
Function Segment()
NODE::OPT_OBSTACLE nearestObstacle(const LINE &aPath)
Class SHAPE_LINE_CHAIN.
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Function CheckColliding()
Definition: pns_node.cpp:425
VECTOR2I A
Definition: seg.h:46
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:297
boost::optional< T > OPT
Definition: optional.h:7
OPT< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:140
void Log(const ITEM *aItem, int aKind=0, const std::string aName=std::string())
Definition: pns_logger.cpp:75
static bool Optimize(LINE *aLine, int aEffortLevel, NODE *aWorld)
a quick shortcut to optmize a line without creating and setting up an optimizer
Push and Shove diff pair dimensions (gap) settings dialog.
int SegmentCount() const
Function SegmentCount()
int Length() const
Function Length()
const VECTOR2I & CPoint(int aIdx) const
Returns the aIdx-th point of the line
Definition: pns_line.h:141
VECTOR2I B
Definition: seg.h:47