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 SHAPE_LINE_CHAIN hull = current_obs->m_item->Hull( current_obs->m_clearance, aPath.Width() );
104
106
107 if( cornerMode == DIRECTION_45::MITERED_90 || cornerMode == DIRECTION_45::ROUNDED_90 )
108 {
109 BOX2I bbox = hull.BBox();
110 hull.Clear();
111 hull.Append( bbox.GetLeft(), bbox.GetTop() );
112 hull.Append( bbox.GetRight(), bbox.GetTop() );
113 hull.Append( bbox.GetRight(), bbox.GetBottom() );
114 hull.Append( bbox.GetLeft(), bbox.GetBottom() );
115 }
116
117 bool s_cw = aPath.Walkaround( hull, path_walk, aWindingDirection );
118
119 PNS_DBG( Dbg(), BeginGroup, "hull/walk", 1 );
120 PNS_DBG( Dbg(), AddShape, &hull, RED, 0,
121 wxString::Format( "hull-%s-%d-cl %d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ),
122 m_iteration, current_obs->m_clearance ) );
123 PNS_DBG( Dbg(), AddShape, &aPath.CLine(), GREEN, 0,
124 wxString::Format( "path-%s-%d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ),
125 m_iteration ) );
126 PNS_DBG( Dbg(), AddShape, &path_walk, BLUE, 0,
127 wxString::Format( "result-%s-%d", aWindingDirection ? wxT( "cw" ) : wxT( "ccw" ),
128 m_iteration ) );
129 PNS_DBG( Dbg(), Message, wxString::Format( wxT( "Stat cw %d" ), !!s_cw ) );
130 PNS_DBGN( Dbg(), EndGroup );
131
132 path_walk.Simplify();
133 aPath.SetShape( path_walk );
134
135 // If the end of the line is inside an obstacle, additional walkaround iterations are not
136 // going to help. Exit now to prevent pegging the iteration limiter and causing lag.
137 if( current_obs && hull.PointInside( initialLast ) && !hull.PointOnEdge( initialLast ) )
138 {
139 return ALMOST_DONE;
140 }
141
142 current_obs = nearestObstacle( LINE( aPath, path_walk ) );
143
144 return IN_PROGRESS;
145}
146
147
148const WALKAROUND::RESULT WALKAROUND::Route( const LINE& aInitialPath )
149{
150 LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
152 SHAPE_LINE_CHAIN best_path;
153 RESULT result;
154
155 // special case for via-in-the-middle-of-track placement
156 if( aInitialPath.PointCount() <= 1 )
157 {
158 if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(),
159 m_itemMask ) )
160 return RESULT( STUCK, STUCK );
161
162 return RESULT( DONE, DONE, aInitialPath, aInitialPath );
163 }
164
165 start( aInitialPath );
166
167 m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
168
169 result.lineCw = aInitialPath;
170 result.lineCcw = aInitialPath;
171
172 if( m_forceWinding )
173 {
174 s_cw = m_forceCw ? IN_PROGRESS : STUCK;
175 s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
176 }
177
178 // In some situations, there isn't a trivial path (or even a path at all). Hitting the
179 // iteration limit causes lag, so we can exit out early if the walkaround path gets very long
180 // compared with the initial path. If the length exceeds the initial length times this factor,
181 // fail out.
182 const int maxWalkDistFactor = 10;
183 long long lengthLimit = aInitialPath.CLine().Length() * maxWalkDistFactor;
184
186 {
187 if( s_cw != STUCK && s_cw != ALMOST_DONE )
188 s_cw = singleStep( path_cw, true );
189
190 if( s_ccw != STUCK && s_ccw != ALMOST_DONE )
191 s_ccw = singleStep( path_ccw, false );
192
193 if( s_cw != IN_PROGRESS )
194 {
195 result.lineCw = path_cw;
196 result.statusCw = s_cw;
197 }
198
199 if( s_ccw != IN_PROGRESS )
200 {
201 result.lineCcw = path_ccw;
202 result.statusCcw = s_ccw;
203 }
204
205 if( s_cw != IN_PROGRESS && s_ccw != IN_PROGRESS )
206 break;
207
208 double length = (double)aInitialPath.CLine().Length();
209 wxCHECK2( length != 0, length = 1.0 );
210 double lcw = path_cw.Line().Length() / length;
211
212 length = (double)aInitialPath.CLine().Length();
213 wxCHECK2( length != 0, length = 1.0 );
214 double lccw = path_ccw.Line().Length() / length;
215
216 PNS_DBG( Dbg(), Message, wxString::Format( wxT( "lcw %.1f lccw %.1f" ), lcw, lccw ) );
217
218
219 // Safety valve
220 if( m_lengthLimitOn && path_cw.Line().Length() > lengthLimit && path_ccw.Line().Length() >
221 lengthLimit )
222 break;
223
224 m_iteration++;
225 }
226
227 if( s_cw == IN_PROGRESS )
228 {
229 result.lineCw = path_cw;
230 result.statusCw = ALMOST_DONE;
231 }
232
233 if( s_ccw == IN_PROGRESS )
234 {
235 result.lineCcw = path_ccw;
236 result.statusCcw = ALMOST_DONE;
237 }
238
239 if( result.lineCw.SegmentCount() < 1 || result.lineCw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
240 {
241 result.statusCw = STUCK;
242 }
243
244 if( result.lineCw.PointCount() > 0 && result.lineCw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
245 {
246 result.statusCw = ALMOST_DONE;
247 }
248
249 if( result.lineCcw.SegmentCount() < 1 ||
250 result.lineCcw.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
251 {
252 result.statusCcw = STUCK;
253 }
254
255 if( result.lineCcw.PointCount() > 0 &&
256 result.lineCcw.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
257 {
258 result.statusCcw = ALMOST_DONE;
259 }
260
261 result.lineCw.ClearLinks();
262 result.lineCcw.ClearLinks();
263
264 return result;
265}
266
267
269 bool aOptimize )
270{
271 LINE path_cw( aInitialPath ), path_ccw( aInitialPath );
273 SHAPE_LINE_CHAIN best_path;
274
275 // special case for via-in-the-middle-of-track placement
276 if( aInitialPath.PointCount() <= 1 )
277 {
278 if( aInitialPath.EndsWithVia() && m_world->CheckColliding( &aInitialPath.Via(),
279 m_itemMask ) )
280 return STUCK;
281
282 aWalkPath = aInitialPath;
283 return DONE;
284 }
285
286 start( aInitialPath );
287
288 m_currentObstacle[0] = m_currentObstacle[1] = nearestObstacle( aInitialPath );
289
290 aWalkPath = aInitialPath;
291
292 if( m_forceWinding )
293 {
294 s_cw = m_forceCw ? IN_PROGRESS : STUCK;
295 s_ccw = m_forceCw ? STUCK : IN_PROGRESS;
296 }
297
299 {
300 if( path_cw.PointCount() == 0 )
301 s_cw = STUCK; // cw path is empty, can't continue
302
303 if( path_ccw.PointCount() == 0 )
304 s_ccw = STUCK; // ccw path is empty, can't continue
305
306 if( s_cw != STUCK )
307 s_cw = singleStep( path_cw, true );
308
309 if( s_ccw != STUCK )
310 s_ccw = singleStep( path_ccw, false );
311
312 if( ( s_cw == DONE && s_ccw == DONE ) || ( s_cw == STUCK && s_ccw == STUCK ) )
313 {
314 int len_cw = path_cw.CLine().Length();
315 int len_ccw = path_ccw.CLine().Length();
316
318 aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
319 else
320 aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
321
322 break;
323 }
324 else if( s_cw == DONE && !m_forceLongerPath )
325 {
326 aWalkPath = path_cw;
327 break;
328 }
329 else if( s_ccw == DONE && !m_forceLongerPath )
330 {
331 aWalkPath = path_ccw;
332 break;
333 }
334
335 m_iteration++;
336 }
337
339 {
340 int len_cw = path_cw.CLine().Length();
341 int len_ccw = path_ccw.CLine().Length();
342
344 aWalkPath = ( len_cw > len_ccw ? path_cw : path_ccw );
345 else
346 aWalkPath = ( len_cw < len_ccw ? path_cw : path_ccw );
347 }
348
349 aWalkPath.Line().Simplify();
350
351 if( aWalkPath.SegmentCount() < 1 )
352 return STUCK;
353
354 if( aWalkPath.CPoint( -1 ) != aInitialPath.CPoint( -1 ) )
355 return ALMOST_DONE;
356
357 if( aWalkPath.CPoint( 0 ) != aInitialPath.CPoint( 0 ) )
358 return STUCK;
359
360 WALKAROUND_STATUS st = s_ccw == DONE || s_cw == DONE ? DONE : STUCK;
361
362 if( st == DONE )
363 {
364 if( aOptimize )
366 }
367
368 return st;
369}
370}
coord_type GetTop() const
Definition: box2.h:219
coord_type GetRight() const
Definition: box2.h:207
coord_type GetLeft() const
Definition: box2.h:218
coord_type GetBottom() const
Definition: box2.h:212
CORNER_MODE
Corner modes.
Definition: direction45.h:67
@ ROUNDED_90
H/V with filleted corners.
Definition: direction45.h:71
@ MITERED_90
H/V only (90-degree corners)
Definition: direction45.h:70
ROUTING_SETTINGS & Settings() const
Return the logger object, allowing to dump geometry to a file.
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:410
std::optional< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:217
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.
DIRECTION_45::CORNER_MODE GetCornerMode() const
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...
void Simplify(int aMaxError=0)
Simplify the line chain by removing colinear adjacent segments and duplicate vertices.
void Clear()
Remove all points from the line chain.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
long long int Length() const
Return length of the line chain in Euclidean metric.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
@ 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:119
WALKAROUND_STATUS statusCcw
WALKAROUND_STATUS statusCw