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  if( ! aPath.Walkaround( current_obs->m_hull, path_pre[0], path_walk[0],
83  path_post[0], aWindingDirection ) )
84  return STUCK;
85 
86  if( ! aPath.Walkaround( current_obs->m_hull, path_pre[1], path_walk[1],
87  path_post[1], !aWindingDirection ) )
88  return STUCK;
89 
90 #ifdef DEBUG
91  m_logger.NewGroup( aWindingDirection ? "walk-cw" : "walk-ccw", m_iteration );
92  m_logger.Log( &path_walk[0], 0, "path-walk" );
93  m_logger.Log( &path_pre[0], 1, "path-pre" );
94  m_logger.Log( &path_post[0], 4, "path-post" );
95  m_logger.Log( &current_obs->m_hull, 2, "hull" );
96  m_logger.Log( current_obs->m_item, 3, "item" );
97 #endif
98 
99  int len_pre = path_walk[0].Length();
100  int len_alt = path_walk[1].Length();
101 
102  LINE walk_path( aPath, path_walk[1] );
103 
104  bool alt_collides = static_cast<bool>( m_world->CheckColliding( &walk_path, m_itemMask ) );
105 
106  SHAPE_LINE_CHAIN pnew;
107 
108  if( !m_forceLongerPath && len_alt < len_pre && !alt_collides && !prev_recursive )
109  {
110  pnew = path_pre[1];
111  pnew.Append( path_walk[1] );
112  pnew.Append( path_post[1] );
113 
114  if( !path_post[1].PointCount() || !path_walk[1].PointCount() )
115  current_obs = nearestObstacle( LINE( aPath, path_pre[1] ) );
116  else
117  current_obs = nearestObstacle( LINE( aPath, path_post[1] ) );
118  prev_recursive = false;
119  }
120  else
121  {
122  pnew = path_pre[0];
123  pnew.Append( path_walk[0] );
124  pnew.Append( path_post[0] );
125 
126  if( !path_post[0].PointCount() || !path_walk[0].PointCount() )
127  current_obs = nearestObstacle( LINE( aPath, path_pre[0] ) );
128  else
129  current_obs = nearestObstacle( LINE( aPath, path_walk[0] ) );
130 
131  if( !current_obs )
132  {
133  prev_recursive = false;
134  current_obs = nearestObstacle( LINE( aPath, path_post[0] ) );
135  }
136  else
137  prev_recursive = true;
138  }
139 
140  pnew.Simplify();
141  aPath.SetShape( pnew );
142 
143  return IN_PROGRESS;
144 }
145 
146 
148  LINE& aWalkPath, bool aOptimize )
149 {
150  LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
151  WALKAROUND_STATUS s_cw = IN_PROGRESS, s_ccw = IN_PROGRESS;
152  SHAPE_LINE_CHAIN best_path;
153 
154  // special case for via-in-the-middle-of-track placement
155  if( aInitialPath.PointCount() <= 1 )
156  {
157  if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(), m_itemMask ) )
158  return STUCK;
159 
160  aWalkPath = aInitialPath;
161  return DONE;
162  }
163 
164  start( aInitialPath );
165 
166  m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
168 
169  aWalkPath = aInitialPath;
170 
171  if( m_forceWinding )
172  {
173  s_cw = m_forceCw ? IN_PROGRESS : STUCK;
174  s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
175  m_forceSingleDirection = true;
176  } else {
177  m_forceSingleDirection = false;
178  }
179 
180  while( m_iteration < m_iterationLimit )
181  {
182  if( s_cw != STUCK )
183  s_cw = singleStep( path_cw, true );
184 
185  if( s_ccw != STUCK )
186  s_ccw = singleStep( path_ccw, false );
187 
188  if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) )
189  {
190  int len_cw = path_cw.CLine().Length();
191  int len_ccw = path_ccw.CLine().Length();
192 
193  if( m_forceLongerPath )
194  aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
195  else
196  aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
197 
198  break;
199  }
200  else if( s_cw == DONE && !m_forceLongerPath )
201  {
202  aWalkPath = path_cw;
203  break;
204  }
205  else if( s_ccw == DONE && !m_forceLongerPath )
206  {
207  aWalkPath = path_ccw;
208  break;
209  }
210 
211  m_iteration++;
212  }
213 
215  {
216  int len_cw = path_cw.CLine().Length();
217  int len_ccw = path_ccw.CLine().Length();
218 
219  if( m_forceLongerPath )
220  aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
221  else
222  aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
223  }
224 
226  {
227  // int len_cw = path_cw.GetCLine().Length();
228  // int len_ccw = path_ccw.GetCLine().Length();
229  bool found = false;
230 
231  SHAPE_LINE_CHAIN l = aWalkPath.CLine();
232 
233  for( int i = 0; i < l.SegmentCount(); i++ )
234  {
235  const SEG s = l.Segment( i );
236 
237  VECTOR2I nearest = s.NearestPoint( m_cursorPos );
238  VECTOR2I::extended_type dist_a = ( s.A - m_cursorPos ).SquaredEuclideanNorm();
239  VECTOR2I::extended_type dist_b = ( s.B - m_cursorPos ).SquaredEuclideanNorm();
240  VECTOR2I::extended_type dist_n = ( nearest - m_cursorPos ).SquaredEuclideanNorm();
241 
242  if( dist_n <= dist_a && dist_n < dist_b )
243  {
244  l.Remove( i + 1, -1 );
245  l.Append( nearest );
246  l.Simplify();
247  found = true;
248  break;
249  }
250  }
251 
252  if( found )
253  {
254  aWalkPath = aInitialPath;
255  aWalkPath.SetShape( l );
256  }
257  }
258 
259  aWalkPath.Line().Simplify();
260 
261  if( aWalkPath.SegmentCount() < 1 )
262  return STUCK;
263  if( aWalkPath.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
264  return STUCK;
265  if( aWalkPath.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
266  return STUCK;
267 
268  WALKAROUND_STATUS st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK;
269 
270  if( st == DONE )
271  {
272  if( aOptimize )
274  }
275 
276  return st;
277 }
278 
279 }
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 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()
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:370
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
size_t i
Definition: json11.cpp:597
VECTOR2I A
Definition: seg.h:46
const LINE ClipToNearestObstacle(NODE *aNode) const
Clips the line to the nearest obstacle, traversing from the line&#39;s start vertex (0).
Definition: pns_line.cpp:302
boost::optional< T > OPT
Definition: optional.h:7
OPT< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:139
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