KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pns_shove.h
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 The 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#ifndef __PNS_SHOVE_H
23#define __PNS_SHOVE_H
24
25#include <vector>
26#include <stack>
27
28#include <math/box2.h>
29
30#include "pns_optimizer.h"
32#include "pns_algo_base.h"
33#include "pns_logger.h"
34#include "range.h"
35
36namespace PNS {
37
38class LINE;
39class NODE;
40class ROUTER;
41
45class SHOVE : public ALGO_BASE
46{
47public:
48
50 {
51 SH_OK = 0,
56 };
57
59 {
61 SHP_SHOVE = 0x1,
67 };
68
69
70 void SetDefaultShovePolicy( int aPolicy );
71
72 void SetShovePolicy( const LINKED_ITEM* aItem, int aPolicy );
73 void SetShovePolicy( const LINE& aLine, int aPolicy );
74
75 SHOVE( NODE* aWorld, ROUTER* aRouter );
76 ~SHOVE();
77
78 void ClearHeads();
79 void AddHeads( const LINE& aHead, int aPolicy = SHP_DEFAULT );
80 void AddHeads( VIA_HANDLE aHead, VECTOR2I aNewPos, int aPolicy = SHP_DEFAULT );
81
83
84 SHOVE_STATUS ShoveDraggingVia( const VIA_HANDLE aOldVia, const VECTOR2I& aWhere,
85 VIA_HANDLE& aNewVia );
86 bool ShoveObstacleLine( const LINE& aCurLine, const LINE& aObstacleLine,
87 LINE& aResultLine );
88
89 void ForceClearance ( bool aEnabled, int aClearance )
90 {
91 if( aEnabled )
92 m_forceClearance = aClearance;
93 else
95 }
96
98
99 bool HeadsModified( int aIndex = -1 ) const;
100 const PNS::LINE GetModifiedHead( int aIndex ) const;
101 const VIA_HANDLE GetModifiedHeadVia( int aIndex ) const;
102
103 bool AddLockedSpringbackNode( NODE* aNode );
104 void UnlockSpringbackNode( NODE* aNode );
105 bool RewindSpringbackTo( NODE* aNode );
107 void DisablePostShoveOptimizations( int aMask );
108 void SetSpringbackDoNotTouchNode( const NODE *aNode );
109
110private:
111 typedef std::vector<SHAPE_LINE_CHAIN> HULL_SET;
112 typedef std::optional<LINE> OPT_LINE;
113 typedef std::pair<LINE, LINE> LINE_PAIR;
114 typedef std::vector<LINE_PAIR> LINE_PAIR_VEC;
115
117 {
118 ROOT_LINE_ENTRY( LINE* aLine = nullptr, int aPolicy = SHP_DEFAULT ) :
119 rootLine( aLine ),
120 policy( aPolicy ) {}
121
122 LINE *rootLine = nullptr;
123 VIA* oldVia = nullptr;
124 VIA* newVia = nullptr;
125 std::optional<LINE> newLine;
127 bool isHead = false;
128 };
129
131 {
132 HEAD_LINE_ENTRY( const LINE& aOrig, int aPolicy = SHP_DEFAULT ) :
133 origHead( aOrig ),
134 policy( aPolicy )
135 {
136 origHead->ClearLinks();
137 };
138
139 HEAD_LINE_ENTRY( VIA_HANDLE aVia, int aPolicy = SHP_DEFAULT ) :
140 theVia( aVia ),
141 policy( aPolicy )
142 {};
143
144 bool geometryModified = false;
145 std::optional<VIA_HANDLE> prevVia;
146 std::optional<VIA_HANDLE> theVia;
147 VIA* draggedVia = nullptr;
149 std::optional<LINE> origHead;
150 std::optional<LINE> newHead;
152 };
153
155 {
157 m_length( 0 ),
158 m_node( nullptr ),
159 m_seq( 0 ),
160 m_locked( false )
161 {}
162
163 int64_t m_length;
164 std::vector<VIA_HANDLE> m_draggedVias;
168 int m_seq;
170 };
171
172 bool pruneLineFromOptimizerQueue( const LINE& aLine );
173
174 bool shoveLineToHullSet( const LINE& aCurLine, const LINE& aObstacleLine, LINE& aResultLine,
175 const HULL_SET& aHulls, bool aPermitAdjustingStart = false,
176 bool aPermitAdjustingEnd = false );
177
178 NODE* reduceSpringback( const ITEM_SET& aHeadSet );
179
180 bool patchTadpoleVia( ITEM* nearest, LINE& current );
181
182 bool pushSpringback( NODE* aNode, const OPT_BOX2I& aAffectedArea );
183
184 bool shoveLineFromLoneVia( const LINE& aCurLine, const LINE& aObstacleLine,
185 LINE& aResultLine );
186 bool checkShoveDirection( const LINE& aCurLine, const LINE& aObstacleLine,
187 const LINE& aShovedLine ) const;
188
189 SHOVE_STATUS onCollidingArc( LINE& aCurrent, ARC* aObstacleArc );
190 SHOVE_STATUS onCollidingLine( LINE& aCurrent, LINE& aObstacle, int aNextRank );
191 SHOVE_STATUS onCollidingSegment( LINE& aCurrent, SEGMENT* aObstacleSeg );
192 SHOVE_STATUS onCollidingSolid( LINE& aCurrent, ITEM* aObstacle, OBSTACLE& aObstacleInfo );
193 SHOVE_STATUS onCollidingVia( ITEM* aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo, int aNextRank );
194 SHOVE_STATUS onReverseCollidingVia( LINE& aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo );
195 SHOVE_STATUS pushOrShoveVia( VIA* aVia, const VECTOR2I& aForce, int aNextRank, bool aDontUnwindStack = false );
196
198
199 void unwindLineStack( const LINKED_ITEM* aSeg );
200 void unwindLineStack( const ITEM* aItem );
201
202 void runOptimizer( NODE* aNode );
203
204 bool pushLineStack( const LINE& aL, bool aKeepCurrentOnTop = false );
205 void popLineStack();
206
207 LINE assembleLine( const LINKED_ITEM* aSeg, int* aIndex = nullptr, bool aPreCleanup = false );
208
209 void replaceItems( ITEM* aOld, std::unique_ptr< ITEM > aNew );
210 ROOT_LINE_ENTRY* replaceLine( LINE& aOld, LINE& aNew,
211 bool aIncludeInChangedArea = true,
212 bool aAllowRedundantSegments = true,
213 NODE *aNode = nullptr );
214
215 ROOT_LINE_ENTRY* findRootLine( const LINE& aLine );
217 ROOT_LINE_ENTRY* touchRootLine( const LINE& aLine );
219 void pruneRootLines( NODE *aRemovedNode );
220
221
222 SHOVE_STATUS shoveIteration( int aIter );
224
225 int getClearance( const ITEM* aA, const ITEM* aB ) const;
226 bool fixupViaCollisions( const LINE* aCurrent, OBSTACLE& obs );
227 void sanityCheck( LINE* aOld, LINE* aNew );
228 void reconstructHeads( bool aShoveFailed );
229 void removeHeads();
230 bool preShoveCleanup( LINE* aOld, LINE* aNew );
231 const wxString formatPolicy( int aPolicy );
232
233 std::vector<SPRINGBACK_TAG> m_nodeStack;
234 std::vector<LINE> m_lineStack;
235 std::vector<LINE> m_optimizerQueue;
236 std::deque<HEAD_LINE_ENTRY> m_headLines;
237
238 std::unordered_map<LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY*> m_rootLineHistory;
239
249
251
254
255};
256
257}
258
259#endif // __PNS_SHOVE_H
std::optional< BOX2I > OPT_BOX2I
Definition: box2.h:926
Definition: line.h:36
Base class for all P&S algorithms (shoving, walkaround, line placement, dragging, etc....
Definition: pns_algo_base.h:43
Base class for PNS router board items.
Definition: pns_item.h:98
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:62
Keep the router "world" - i.e.
Definition: pns_node.h:232
The actual Push and Shove algorithm.
Definition: pns_shove.h:46
void SetSpringbackDoNotTouchNode(const NODE *aNode)
Definition: pns_shove.cpp:2215
SHOVE_STATUS shoveIteration(int aIter)
Definition: pns_shove.cpp:1632
SHOVE_STATUS shoveMainLoop()
Definition: pns_shove.cpp:1879
std::vector< SHAPE_LINE_CHAIN > HULL_SET
Definition: pns_shove.h:111
void reconstructHeads(bool aShoveFailed)
Definition: pns_shove.cpp:2289
int m_iter
Definition: pns_shove.h:245
std::pair< LINE, LINE > LINE_PAIR
Definition: pns_shove.h:113
SHOVE_STATUS pushOrShoveVia(VIA *aVia, const VECTOR2I &aForce, int aNextRank, bool aDontUnwindStack=false)
Definition: pns_shove.cpp:1035
SHOVE_STATUS onReverseCollidingVia(LINE &aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo)
Definition: pns_shove.cpp:1262
OPT_BOX2I m_affectedArea
Definition: pns_shove.h:253
std::vector< LINE > m_lineStack
Definition: pns_shove.h:234
void popLineStack()
Definition: pns_shove.cpp:1508
ROOT_LINE_ENTRY * findRootLine(const LINE &aLine)
Definition: pns_shove.cpp:1941
std::vector< SPRINGBACK_TAG > m_nodeStack
Definition: pns_shove.h:233
@ SHP_DONT_OPTIMIZE
Definition: pns_shove.h:65
@ SHP_WALK_BACK
Definition: pns_shove.h:63
@ SHP_IGNORE
Definition: pns_shove.h:64
@ SHP_DEFAULT
Definition: pns_shove.h:60
@ SHP_DONT_LOCK_ENDPOINTS
Definition: pns_shove.h:66
@ SHP_WALK_FORWARD
Definition: pns_shove.h:62
bool patchTadpoleVia(ITEM *nearest, LINE &current)
Definition: pns_shove.cpp:1598
SHOVE_STATUS onCollidingArc(LINE &aCurrent, ARC *aObstacleArc)
Definition: pns_shove.cpp:681
bool shoveLineToHullSet(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine, const HULL_SET &aHulls, bool aPermitAdjustingStart=false, bool aPermitAdjustingEnd=false)
Definition: pns_shove.cpp:309
std::optional< LINE > OPT_LINE
Definition: pns_shove.h:112
NODE * m_currentNode
Definition: pns_shove.h:241
SHOVE_STATUS Run()
Definition: pns_shove.cpp:2390
LINE assembleLine(const LINKED_ITEM *aSeg, int *aIndex=nullptr, bool aPreCleanup=false)
Definition: pns_shove.cpp:218
void ForceClearance(bool aEnabled, int aClearance)
Definition: pns_shove.h:89
bool checkShoveDirection(const LINE &aCurLine, const LINE &aObstacleLine, const LINE &aShovedLine) const
Definition: pns_shove.cpp:242
int m_forceClearance
Definition: pns_shove.h:247
SHOVE_STATUS onCollidingSegment(LINE &aCurrent, SEGMENT *aObstacleSeg)
Definition: pns_shove.cpp:619
void DisablePostShoveOptimizations(int aMask)
Definition: pns_shove.cpp:2209
bool RewindSpringbackTo(NODE *aNode)
Definition: pns_shove.cpp:2144
bool pushLineStack(const LINE &aL, bool aKeepCurrentOnTop=false)
Definition: pns_shove.cpp:1454
@ SH_INCOMPLETE
Definition: pns_shove.h:53
@ SH_HEAD_MODIFIED
Definition: pns_shove.h:54
@ SH_TRY_WALK
Definition: pns_shove.h:55
OPT_BOX2I totalAffectedArea() const
Definition: pns_shove.cpp:1925
bool RewindToLastLockedNode()
Definition: pns_shove.cpp:2178
SHOVE_STATUS ShoveDraggingVia(const VIA_HANDLE aOldVia, const VECTOR2I &aWhere, VIA_HANDLE &aNewVia)
std::deque< HEAD_LINE_ENTRY > m_headLines
Definition: pns_shove.h:236
int m_defaultPolicy
Definition: pns_shove.h:252
const wxString formatPolicy(int aPolicy)
Definition: pns_shove.cpp:2621
bool ShoveObstacleLine(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine)
Definition: pns_shove.cpp:501
SHOVE_STATUS onCollidingVia(ITEM *aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo, int aNextRank)
Definition: pns_shove.cpp:1169
void sanityCheck(LINE *aOld, LINE *aNew)
Definition: pns_shove.cpp:186
int m_restrictSpringbackTagId
Definition: pns_shove.h:243
void runOptimizer(NODE *aNode)
Definition: pns_shove.cpp:2012
ROOT_LINE_ENTRY * touchRootLine(const LINE &aLine)
Definition: pns_shove.cpp:1965
const PNS::LINE GetModifiedHead(int aIndex) const
Definition: pns_shove.cpp:2650
bool HeadsModified(int aIndex=-1) const
Definition: pns_shove.cpp:2642
void UnlockSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:2192
NODE * m_root
Definition: pns_shove.h:240
NODE * reduceSpringback(const ITEM_SET &aHeadSet)
Definition: pns_shove.cpp:916
bool AddLockedSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:2130
void SetDefaultShovePolicy(int aPolicy)
Definition: pns_shove.cpp:2221
void AddHeads(const LINE &aHead, int aPolicy=SHP_DEFAULT)
Definition: pns_shove.cpp:2246
void SetShovePolicy(const LINKED_ITEM *aItem, int aPolicy)
Definition: pns_shove.cpp:2227
void unwindLineStack(const LINKED_ITEM *aSeg)
Definition: pns_shove.cpp:1384
bool preShoveCleanup(LINE *aOld, LINE *aNew)
Definition: pns_shove.cpp:2361
NODE * CurrentNode()
Definition: pns_shove.cpp:2124
int getClearance(const ITEM *aA, const ITEM *aB) const
Definition: pns_shove.cpp:169
bool m_multiLineMode
Definition: pns_shove.h:248
std::vector< LINE > m_optimizerQueue
Definition: pns_shove.h:235
SHOVE_STATUS onCollidingSolid(LINE &aCurrent, ITEM *aObstacle, OBSTACLE &aObstacleInfo)
Definition: pns_shove.cpp:768
bool pushSpringback(NODE *aNode, const OPT_BOX2I &aAffectedArea)
Definition: pns_shove.cpp:977
int m_optFlagDisableMask
Definition: pns_shove.h:250
SHOVE_STATUS onCollidingLine(LINE &aCurrent, LINE &aObstacle, int aNextRank)
Definition: pns_shove.cpp:730
bool fixupViaCollisions(const LINE *aCurrent, OBSTACLE &obs)
Definition: pns_shove.cpp:1516
bool pruneLineFromOptimizerQueue(const LINE &aLine)
Definition: pns_shove.cpp:1480
void replaceItems(ITEM *aOld, std::unique_ptr< ITEM > aNew)
Definition: pns_shove.cpp:49
bool shoveLineFromLoneVia(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine)
Definition: pns_shove.cpp:262
VIA * m_draggedVia
Definition: pns_shove.h:244
bool m_headsModified
Definition: pns_shove.h:246
const NODE * m_springbackDoNotTouchNode
Definition: pns_shove.h:242
void ClearHeads()
Definition: pns_shove.cpp:2240
std::unordered_map< LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY * > m_rootLineHistory
Definition: pns_shove.h:238
void pruneRootLines(NODE *aRemovedNode)
Definition: pns_shove.cpp:893
const VIA_HANDLE GetModifiedHeadVia(int aIndex) const
Definition: pns_shove.cpp:2655
ROOT_LINE_ENTRY * replaceLine(LINE &aOld, LINE &aNew, bool aIncludeInChangedArea=true, bool aAllowRedundantSegments=true, NODE *aNode=nullptr)
Definition: pns_shove.cpp:80
std::vector< LINE_PAIR > LINE_PAIR_VEC
Definition: pns_shove.h:114
void removeHeads()
Definition: pns_shove.cpp:2270
Push and Shove diff pair dimensions (gap) settings dialog.
Hold an object colliding with another object, along with some useful data about the collision.
Definition: pns_node.h:88
Definition: pns_shove.h:131
std::optional< VIA_HANDLE > theVia
Definition: pns_shove.h:146
HEAD_LINE_ENTRY(const LINE &aOrig, int aPolicy=SHP_DEFAULT)
Definition: pns_shove.h:132
int policy
Definition: pns_shove.h:151
VIA * draggedVia
Definition: pns_shove.h:147
std::optional< VIA_HANDLE > prevVia
Definition: pns_shove.h:145
bool geometryModified
Definition: pns_shove.h:144
std::optional< LINE > newHead
Definition: pns_shove.h:150
HEAD_LINE_ENTRY(VIA_HANDLE aVia, int aPolicy=SHP_DEFAULT)
Definition: pns_shove.h:139
std::optional< LINE > origHead
Definition: pns_shove.h:149
VECTOR2I viaNewPos
Definition: pns_shove.h:148
Definition: pns_shove.h:117
int policy
Definition: pns_shove.h:126
bool isHead
Definition: pns_shove.h:127
std::optional< LINE > newLine
Definition: pns_shove.h:125
VIA * oldVia
Definition: pns_shove.h:123
VIA * newVia
Definition: pns_shove.h:124
ROOT_LINE_ENTRY(LINE *aLine=nullptr, int aPolicy=SHP_DEFAULT)
Definition: pns_shove.h:118
LINE * rootLine
Definition: pns_shove.h:122
std::vector< VIA_HANDLE > m_draggedVias
Definition: pns_shove.h:164