KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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-2023 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_router.h"
29#include "pns_debug_decorator.h"
30#include "pns_solid.h"
31
32
33namespace PNS {
34
35void WALKAROUND::start( const LINE& aInitialPath )
36{
37 m_iteration = 0;
39}
40
41
43{
45
47
48 if( ! m_restrictedSet.empty() )
50 else
51 opts.m_restrictedSet = nullptr;
52
53 opts.m_useClearanceEpsilon = false;
54
55 NODE::OPT_OBSTACLE obs = m_world->NearestObstacle( &aPath, opts );
56
57 if( m_restrictedSet.empty() )
58 return obs;
59
60 else if( obs && m_restrictedSet.find ( obs->m_item ) != m_restrictedSet.end() )
61 return obs;
62
63 return NODE::OPT_OBSTACLE();
64}
65
66
67void WALKAROUND::RestrictToSet( bool aEnabled, const std::set<ITEM*>& aSet )
68{
70 m_restrictedSet.clear();
71
72 if( aEnabled )
73 {
74 for( ITEM* item : aSet )
75 {
76 m_restrictedSet.insert( item );
77
78 if ( item->HasHole() )
79 m_restrictedSet.insert( item->Hole() );
80 }
81 }
82
83 for( ITEM* item : aSet )
84 {
85 if( SOLID* solid = dyn_cast<SOLID*>( item ) )
86 m_restrictedVertices.push_back( solid->Anchor( 0 ) );
87 }
88}
89
90
92{
93 std::optional<OBSTACLE>& current_obs =
94 aWindingDirection ? m_currentObstacle[0] : m_currentObstacle[1];
95
96 if( !current_obs )
97 return DONE;
98
99 VECTOR2I initialLast = aPath.CPoint( -1 );
100
101 SHAPE_LINE_CHAIN path_walk;
102
103
104 SHAPE_LINE_CHAIN hull = current_obs->m_item->Hull( current_obs->m_clearance, aPath.Width() );
105
106 bool s_cw = aPath.Walkaround( hull, path_walk, aWindingDirection );
107
108 PNS_DBG( Dbg(), BeginGroup, "hull/walk", 1 );
109 PNS_DBG( Dbg(), AddShape, &hull, RED, 0, wxString::Format( "hull-%s-%d-cl %d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ), m_iteration, current_obs->m_clearance ) );
110 PNS_DBG( Dbg(), AddShape, &aPath.CLine(), GREEN, 0, wxString::Format( "path-%s-%d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ), m_iteration ) );
111 PNS_DBG( Dbg(), AddShape, &path_walk, BLUE, 0, wxString::Format( "result-%s-%d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ), m_iteration ) );
112 PNS_DBG( Dbg(), Message, wxString::Format( wxT( "Stat cw %d" ), !!s_cw ) );
113 PNS_DBGN( Dbg(), EndGroup );
114
115 path_walk.Simplify();
116 aPath.SetShape( path_walk );
117
118 // If the end of the line is inside an obstacle, additional walkaround iterations are not
119 // going to help. Exit now to prevent pegging the iteration limiter and causing lag.
120 if( current_obs && hull.PointInside( initialLast ) && !hull.PointOnEdge( initialLast ) )
121 {
122 return ALMOST_DONE;
123 }
124
125 current_obs = nearestObstacle( LINE( aPath, path_walk ) );
126
127 return IN_PROGRESS;
128}
129
130
131const WALKAROUND::RESULT WALKAROUND::Route( const LINE& aInitialPath )
132{
133 LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
135 SHAPE_LINE_CHAIN best_path;
136 RESULT result;
137
138 // special case for via-in-the-middle-of-track placement
139 if( aInitialPath.PointCount() <= 1 )
140 {
141 if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(),
142 m_itemMask ) )
143 return RESULT( STUCK, STUCK );
144
145 return RESULT( DONE, DONE, aInitialPath, aInitialPath );
146 }
147
148 start( aInitialPath );
149
150 m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
151
152 result.lineCw = aInitialPath;
153 result.lineCcw = aInitialPath;
154
155 if( m_forceWinding )
156 {
157 s_cw = m_forceCw ? IN_PROGRESS : STUCK;
158 s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
159 }
160
161 // In some situations, there isn't a trivial path (or even a path at all). Hitting the
162 // iteration limit causes lag, so we can exit out early if the walkaround path gets very long
163 // compared with the initial path. If the length exceeds the initial length times this factor,
164 // fail out.
165 const int maxWalkDistFactor = 10;
166 long long lengthLimit = aInitialPath.CLine().Length() * maxWalkDistFactor;
167
169 {
170 if( s_cw != STUCK && s_cw != ALMOST_DONE )
171 s_cw = singleStep( path_cw, true );
172
173 if( s_ccw != STUCK && s_ccw != ALMOST_DONE )
174 s_ccw = singleStep( path_ccw, false );
175
176 if( s_cw != IN_PROGRESS )
177 {
178 result.lineCw = path_cw;
179 result.statusCw = s_cw;
180 }
181
182 if( s_ccw != IN_PROGRESS )
183 {
184 result.lineCcw = path_ccw;
185 result.statusCcw = s_ccw;
186 }
187
188 if( s_cw != IN_PROGRESS && s_ccw != IN_PROGRESS )
189 break;
190
191 double lcw = path_cw.Line().Length() / (double)aInitialPath.CLine().Length();
192 double lccw = path_ccw.Line().Length() / (double)aInitialPath.CLine().Length();
193
194 PNS_DBG( Dbg(), Message, wxString::Format( wxT( "lcw %.1f lccw %.1f" ), lcw, lccw ) );
195
196
197 // Safety valve
198 if( m_lengthLimitOn && path_cw.Line().Length() > lengthLimit && path_ccw.Line().Length() > lengthLimit )
199 break;
200
201 m_iteration++;
202 }
203
204 if( s_cw == IN_PROGRESS )
205 {
206 result.lineCw = path_cw;
207 result.statusCw = ALMOST_DONE;
208 }
209
210 if( s_ccw == IN_PROGRESS )
211 {
212 result.lineCcw = path_ccw;
213 result.statusCcw = ALMOST_DONE;
214 }
215
216 if( result.lineCw.SegmentCount() < 1 || result.lineCw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
217 {
218 result.statusCw = STUCK;
219 }
220
221 if( result.lineCw.PointCount() > 0 && result.lineCw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
222 {
223 result.statusCw = ALMOST_DONE;
224 }
225
226 if( result.lineCcw.SegmentCount() < 1 ||
227 result.lineCcw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
228 {
229 result.statusCcw = STUCK;
230 }
231
232 if( result.lineCcw.PointCount() > 0 &&
233 result.lineCcw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
234 {
235 result.statusCcw = ALMOST_DONE;
236 }
237
238 result.lineCw.ClearLinks();
239 result.lineCcw.ClearLinks();
240
241 return result;
242}
243
244
246 bool aOptimize )
247{
248 LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
250 SHAPE_LINE_CHAIN best_path;
251
252 // special case for via-in-the-middle-of-track placement
253 if( aInitialPath.PointCount() <= 1 )
254 {
255 if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(), m_itemMask ) )
256 return STUCK;
257
258 aWalkPath = aInitialPath;
259 return DONE;
260 }
261
262 start( aInitialPath );
263
264 m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
265
266 aWalkPath = aInitialPath;
267
268 if( m_forceWinding )
269 {
270 s_cw = m_forceCw ? IN_PROGRESS : STUCK;
271 s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
272 }
273
275 {
276 if( path_cw.PointCount() == 0 )
277 s_cw = STUCK; // cw path is empty, can't continue
278
279 if( path_ccw.PointCount() == 0 )
280 s_ccw = STUCK; // ccw path is empty, can't continue
281
282 if( s_cw != STUCK )
283 s_cw = singleStep( path_cw, true );
284
285 if( s_ccw != STUCK )
286 s_ccw = singleStep( path_ccw, false );
287
288 if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) )
289 {
290 int len_cw = path_cw.CLine().Length();
291 int len_ccw = path_ccw.CLine().Length();
292
294 aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
295 else
296 aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
297
298 break;
299 }
300 else if( s_cw == DONE && !m_forceLongerPath )
301 {
302 aWalkPath = path_cw;
303 break;
304 }
305 else if( s_ccw == DONE && !m_forceLongerPath )
306 {
307 aWalkPath = path_ccw;
308 break;
309 }
310
311 m_iteration++;
312 }
313
315 {
316 int len_cw = path_cw.CLine().Length();
317 int len_ccw = path_ccw.CLine().Length();
318
320 aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
321 else
322 aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
323 }
324
325 aWalkPath.Line().Simplify();
326
327 if( aWalkPath.SegmentCount() < 1 )
328 return STUCK;
329
330 if( aWalkPath.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
331 return ALMOST_DONE;
332
333 if( aWalkPath.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
334 return STUCK;
335
336 WALKAROUND_STATUS st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK;
337
338 if( st == DONE )
339 {
340 if( aOptimize )
342 }
343
344 return st;
345}
346}
DEBUG_DECORATOR * Dbg() const
Definition: pns_algo_base.h:78
Base class for PNS router board items.
Definition: pns_item.h:97
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:144
void SetShape(const SHAPE_LINE_CHAIN &aLine)
Return the shape of the line.
Definition: pns_line.h:125
const SHAPE_LINE_CHAIN & CLine() const
Definition: pns_line.h:136
SHAPE_LINE_CHAIN & Line()
Definition: pns_line.h:135
VIA & Via()
Definition: pns_line.h:193
int SegmentCount() const
Definition: pns_line.h:138
int PointCount() const
Definition: pns_line.h:139
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:188
int Width() const
Return true if the line is geometrically identical as line aOther.
Definition: pns_line.h:155
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:393
std::optional< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:208
OPT_OBSTACLE NearestObstacle(const LINE *aLine, const COLLISION_SEARCH_OPTIONS &aOpts=COLLISION_SEARCH_OPTIONS())
Follow the line in search of an obstacle that is nearest to the starting to the line's starting point...
Definition: pns_node.cpp:283
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)
bool PointOnEdge(const VECTOR2I &aP, int aAccuracy=0) const
Check if point aP lies on an edge or vertex of the line chain.
bool PointInside(const VECTOR2I &aPt, int aAccuracy=0, bool aUseBBoxCache=false) const override
Check if point aP lies inside a closed shape.
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:56
@ GREEN
Definition: color4d.h:57
@ RED
Definition: color4d.h:59
Push and Shove diff pair dimensions (gap) settings dialog.
#define PNS_DBG(dbg, method,...)
#define PNS_DBGN(dbg, method)
std::set< ITEM * > * m_restrictedSet
Definition: pns_node.h:118
WALKAROUND_STATUS statusCcw
WALKAROUND_STATUS statusCw