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-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 * Author: Tomasz Wlostowski <[email protected]>
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 <optional>
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#include "pns_solid.h"
32
33
34namespace PNS {
35
36void WALKAROUND::start( const LINE& aInitialPath )
37{
38 m_iteration = 0;
40}
41
42
44{
46 &aPath, m_itemMask, m_restrictedSet.empty() ? nullptr : &m_restrictedSet, false );
47
48 if( m_restrictedSet.empty() )
49 return obs;
50
51 else if( obs && m_restrictedSet.find ( obs->m_item ) != m_restrictedSet.end() )
52 return obs;
53
54 return NODE::OPT_OBSTACLE();
55}
56
57
58void WALKAROUND::RestrictToSet( bool aEnabled, const std::set<ITEM*>& aSet )
59{
61
62 if( aEnabled )
63 m_restrictedSet = aSet;
64 else
65 m_restrictedSet.clear();
66
67 for( auto item : aSet )
68 {
69 if( auto solid = dyn_cast<SOLID*>( item ) )
70 {
71 m_restrictedVertices.push_back( solid->Anchor( 0 ) );
72 }
73 }
74}
75
76
78{
79 std::optional<OBSTACLE>& current_obs =
80 aWindingDirection ? m_currentObstacle[0] : m_currentObstacle[1];
81
82 if( !current_obs )
83 return DONE;
84
85 VECTOR2I initialLast = aPath.CPoint( -1 );
86
87 SHAPE_LINE_CHAIN path_walk;
88
89 bool s_cw = aPath.Walkaround( current_obs->m_hull, path_walk, aWindingDirection );
90
91 PNS_DBG( Dbg(), BeginGroup, "hull/walk", 1 );
92 PNS_DBG( Dbg(), AddShape, &current_obs->m_hull, RED, 0, wxString::Format( "hull-%s-%d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ), m_iteration ) );
93 PNS_DBG( Dbg(), AddShape, &aPath.CLine(), GREEN, 0, wxString::Format( "path-%s-%d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ), m_iteration ) );
94 PNS_DBG( Dbg(), AddShape, &path_walk, BLUE, 0, wxString::Format( "result-%s-%d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ), m_iteration ) );
95 PNS_DBG( Dbg(), Message, wxString::Format( wxT( "Stat cw %d" ), !!s_cw ) );
96 PNS_DBGN( Dbg(), EndGroup );
97
98 path_walk.Simplify();
99 aPath.SetShape( path_walk );
100
101 // If the end of the line is inside an obstacle, additional walkaround iterations are not
102 // going to help. Exit now to prevent pegging the iteration limiter and causing lag.
103 if( current_obs && current_obs->m_hull.PointInside( initialLast ) &&
104 !current_obs->m_hull.PointOnEdge( initialLast ) )
105 {
106 return ALMOST_DONE;
107 }
108
109 current_obs = nearestObstacle( LINE( aPath, path_walk ) );
110
111 return IN_PROGRESS;
112}
113
114
115const WALKAROUND::RESULT WALKAROUND::Route( const LINE& aInitialPath )
116{
117 LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
119 SHAPE_LINE_CHAIN best_path;
120 RESULT result;
121
122 // special case for via-in-the-middle-of-track placement
123 if( aInitialPath.PointCount() <= 1 )
124 {
125 if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(),
126 m_itemMask ) )
127 return RESULT( STUCK, STUCK );
128
129 return RESULT( DONE, DONE, aInitialPath, aInitialPath );
130 }
131
132 start( aInitialPath );
133
134 m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
135
136 result.lineCw = aInitialPath;
137 result.lineCcw = aInitialPath;
138
139 if( m_forceWinding )
140 {
141 s_cw = m_forceCw ? IN_PROGRESS : STUCK;
142 s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
143 }
144
145 // In some situations, there isn't a trivial path (or even a path at all). Hitting the
146 // iteration limit causes lag, so we can exit out early if the walkaround path gets very long
147 // compared with the initial path. If the length exceeds the initial length times this factor,
148 // fail out.
149 const int maxWalkDistFactor = 10;
150 long long lengthLimit = aInitialPath.CLine().Length() * maxWalkDistFactor;
151
153 {
154 if( s_cw != STUCK && s_cw != ALMOST_DONE )
155 s_cw = singleStep( path_cw, true );
156
157 if( s_ccw != STUCK && s_ccw != ALMOST_DONE )
158 s_ccw = singleStep( path_ccw, false );
159
160 if( s_cw != IN_PROGRESS )
161 {
162 result.lineCw = path_cw;
163 result.statusCw = s_cw;
164 }
165
166 if( s_ccw != IN_PROGRESS )
167 {
168 result.lineCcw = path_ccw;
169 result.statusCcw = s_ccw;
170 }
171
172 if( s_cw != IN_PROGRESS && s_ccw != IN_PROGRESS )
173 break;
174
175 // Safety valve
176 if( m_lengthLimitOn && path_cw.Line().Length() > lengthLimit && path_ccw.Line().Length() > lengthLimit )
177 break;
178
179 m_iteration++;
180 }
181
182 if( s_cw == IN_PROGRESS )
183 {
184 result.lineCw = path_cw;
185 result.statusCw = ALMOST_DONE;
186 }
187
188 if( s_ccw == IN_PROGRESS )
189 {
190 result.lineCcw = path_ccw;
191 result.statusCcw = ALMOST_DONE;
192 }
193
194 if( result.lineCw.SegmentCount() < 1 || result.lineCw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
195 {
196 result.statusCw = STUCK;
197 }
198
199 if( result.lineCw.PointCount() > 0 && result.lineCw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
200 {
201 result.statusCw = ALMOST_DONE;
202 }
203
204 if( result.lineCcw.SegmentCount() < 1 ||
205 result.lineCcw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
206 {
207 result.statusCcw = STUCK;
208 }
209
210 if( result.lineCcw.PointCount() > 0 &&
211 result.lineCcw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
212 {
213 result.statusCcw = ALMOST_DONE;
214 }
215
216 result.lineCw.ClearLinks();
217 result.lineCcw.ClearLinks();
218
219 return result;
220}
221
222
224 bool aOptimize )
225{
226 LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
228 SHAPE_LINE_CHAIN best_path;
229
230 // special case for via-in-the-middle-of-track placement
231 if( aInitialPath.PointCount() <= 1 )
232 {
233 if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(),
234 m_itemMask ) )
235 return STUCK;
236
237 aWalkPath = aInitialPath;
238 return DONE;
239 }
240
241 start( aInitialPath );
242
243 m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
244
245 aWalkPath = aInitialPath;
246
247 if( m_forceWinding )
248 {
249 s_cw = m_forceCw ? IN_PROGRESS : STUCK;
250 s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
251 }
252
254 {
255 if( path_cw.PointCount() == 0 )
256 s_cw = STUCK; // cw path is empty, can't continue
257
258 if( path_ccw.PointCount() == 0 )
259 s_ccw = STUCK; // ccw path is empty, can't continue
260
261 if( s_cw != STUCK )
262 s_cw = singleStep( path_cw, true );
263
264 if( s_ccw != STUCK )
265 s_ccw = singleStep( path_ccw, false );
266
267 if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) )
268 {
269 int len_cw = path_cw.CLine().Length();
270 int len_ccw = path_ccw.CLine().Length();
271
273 aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
274 else
275 aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
276
277 break;
278 }
279 else if( s_cw == DONE && !m_forceLongerPath )
280 {
281 aWalkPath = path_cw;
282 break;
283 }
284 else if( s_ccw == DONE && !m_forceLongerPath )
285 {
286 aWalkPath = path_ccw;
287 break;
288 }
289
290 m_iteration++;
291 }
292
294 {
295 int len_cw = path_cw.CLine().Length();
296 int len_ccw = path_ccw.CLine().Length();
297
299 aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
300 else
301 aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
302 }
303
304 aWalkPath.Line().Simplify();
305
306 if( aWalkPath.SegmentCount() < 1 )
307 return STUCK;
308
309 if( aWalkPath.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
310 return ALMOST_DONE;
311
312 if( aWalkPath.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
313 return STUCK;
314
315 WALKAROUND_STATUS st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK;
316
317 if( st == DONE )
318 {
319 if( aOptimize )
321 }
322
323 return st;
324}
325}
DEBUG_DECORATOR * Dbg() const
Definition: pns_algo_base.h:78
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:61
const VECTOR2I & CPoint(int aIdx) const
Definition: pns_line.h:150
void SetShape(const SHAPE_LINE_CHAIN &aLine)
Return the shape of the line.
Definition: pns_line.h:126
const VIA & Via() const
Definition: pns_line.h:199
const SHAPE_LINE_CHAIN & CLine() const
Definition: pns_line.h:142
SHAPE_LINE_CHAIN & Line()
Definition: pns_line.h:141
int SegmentCount() const
Definition: pns_line.h:144
int PointCount() const
Definition: pns_line.h:145
bool Walkaround(SHAPE_LINE_CHAIN aObstacle, SHAPE_LINE_CHAIN &aPre, SHAPE_LINE_CHAIN &aWalk, SHAPE_LINE_CHAIN &aPost, bool aCw) const
Calculate a line tightly wrapping a convex hull of an obstacle object (aObstacle).
bool EndsWithVia() const
Definition: pns_line.h:194
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Check if the item collides with anything else in the world, and if found, returns the obstacle.
Definition: pns_node.cpp:475
std::optional< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:166
OPT_OBSTACLE NearestObstacle(const LINE *aLine, int aKindMask=ITEM::ANY_T, const std::set< ITEM * > *aRestrictedSet=nullptr, bool aUseClearanceEpsilon=true)
Follow the line in search of an obstacle that is nearest to the starting to the line's starting point...
Definition: pns_node.cpp:303
static bool Optimize(LINE *aLine, int aEffortLevel, NODE *aWorld, const VECTOR2I &aV=VECTOR2I(0, 0))
@ MERGE_OBTUSE
Reduce corner cost by merging obtuse segments.
std::set< ITEM * > m_restrictedSet
WALKAROUND_STATUS singleStep(LINE &aPath, bool aWindingDirection)
NODE::OPT_OBSTACLE m_currentObstacle[2]
NODE::OPT_OBSTACLE nearestObstacle(const LINE &aPath)
std::vector< VECTOR2I > m_restrictedVertices
WALKAROUND_STATUS Route(const LINE &aInitialPath, LINE &aWalkPath, bool aOptimize=true)
void start(const LINE &aInitialPath)
void RestrictToSet(bool aEnabled, const std::set< ITEM * > &aSet)
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
SHAPE_LINE_CHAIN & Simplify(bool aRemoveColinear=true)
Simplify the line chain by removing colinear adjacent segments and duplicate vertices.
long long int Length() const
Return length of the line chain in Euclidean metric.
@ BLUE
Definition: color4d.h:54
@ GREEN
Definition: color4d.h:55
@ RED
Definition: color4d.h:57
Push and Shove diff pair dimensions (gap) settings dialog.
#define PNS_DBG(dbg, method,...)
#define PNS_DBGN(dbg, method)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
WALKAROUND_STATUS statusCcw
WALKAROUND_STATUS statusCw