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