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 (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#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,
66 };
67
68
69 void SetDefaultShovePolicy( int aPolicy );
70
71 void SetShovePolicy( const LINKED_ITEM* aItem, int aPolicy );
72 void SetShovePolicy( const LINE& aLine, int aPolicy );
73
74 SHOVE( NODE* aWorld, ROUTER* aRouter );
75 ~SHOVE();
76
77 void ClearHeads();
78 void AddHeads( const LINE& aHead, int aPolicy = SHP_DEFAULT );
79 void AddHeads( VIA_HANDLE aHead, VECTOR2I aNewPos, int aPolicy = SHP_DEFAULT );
80
82
83 SHOVE_STATUS ShoveDraggingVia( const VIA_HANDLE aOldVia, const VECTOR2I& aWhere,
84 VIA_HANDLE& aNewVia );
85 bool ShoveObstacleLine( const LINE& aCurLine, const LINE& aObstacleLine,
86 LINE& aResultLine );
87
88 void ForceClearance ( bool aEnabled, int aClearance )
89 {
90 if( aEnabled )
91 m_forceClearance = aClearance;
92 else
94 }
95
97
98 bool HeadsModified( int aIndex = -1 ) const;
99 const PNS::LINE GetModifiedHead( int aIndex ) const;
100 const VIA_HANDLE GetModifiedHeadVia( int aIndex ) const;
101
102 bool AddLockedSpringbackNode( NODE* aNode );
103 void UnlockSpringbackNode( NODE* aNode );
104 bool RewindSpringbackTo( NODE* aNode );
106 void DisablePostShoveOptimizations( int aMask );
107 void SetSpringbackDoNotTouchNode( const NODE *aNode );
108
109private:
110 typedef std::vector<SHAPE_LINE_CHAIN> HULL_SET;
111 typedef std::optional<LINE> OPT_LINE;
112 typedef std::pair<LINE, LINE> LINE_PAIR;
113 typedef std::vector<LINE_PAIR> LINE_PAIR_VEC;
114
116 {
117 ROOT_LINE_ENTRY( LINE* aLine = nullptr, int aPolicy = SHP_DEFAULT ) :
118 rootLine( aLine ),
119 policy( aPolicy ) {}
120
121 LINE *rootLine = nullptr;
122 VIA* oldVia = nullptr;
123 VIA* newVia = nullptr;
124 std::optional<LINE> newLine;
126 bool isHead = false;
127 };
128
130 {
131 HEAD_LINE_ENTRY( const LINE& aOrig, int aPolicy = SHP_DEFAULT ) :
132 origHead( aOrig ),
133 policy( aPolicy )
134 {
135 origHead->ClearLinks();
136 };
137
138 HEAD_LINE_ENTRY( VIA_HANDLE aVia, int aPolicy = SHP_DEFAULT ) :
139 theVia( aVia ),
140 policy( aPolicy )
141 {};
142
143 bool geometryModified = false;
144 std::optional<VIA_HANDLE> prevVia;
145 std::optional<VIA_HANDLE> theVia;
146 VIA* draggedVia = nullptr;
148 std::optional<LINE> origHead;
149 std::optional<LINE> newHead;
151 };
152
154 {
156 m_length( 0 ),
157 m_node( nullptr ),
158 m_seq( 0 ),
159 m_locked( false )
160 {}
161
162 int64_t m_length;
163 std::vector<VIA_HANDLE> m_draggedVias;
167 int m_seq;
169 };
170
171 bool shoveLineToHullSet( const LINE& aCurLine, const LINE& aObstacleLine,
172 LINE& aResultLine, const HULL_SET& aHulls );
173
174 NODE* reduceSpringback( const ITEM_SET& aHeadSet );
175
176 bool pushSpringback( NODE* aNode, const OPT_BOX2I& aAffectedArea );
177
178 bool shoveLineFromLoneVia( const LINE& aCurLine, const LINE& aObstacleLine,
179 LINE& aResultLine );
180 bool checkShoveDirection( const LINE& aCurLine, const LINE& aObstacleLine,
181 const LINE& aShovedLine ) const;
182
183 SHOVE_STATUS onCollidingArc( LINE& aCurrent, ARC* aObstacleArc );
184 SHOVE_STATUS onCollidingLine( LINE& aCurrent, LINE& aObstacle, int aNextRank );
185 SHOVE_STATUS onCollidingSegment( LINE& aCurrent, SEGMENT* aObstacleSeg );
186 SHOVE_STATUS onCollidingSolid( LINE& aCurrent, ITEM* aObstacle, OBSTACLE& aObstacleInfo );
187 SHOVE_STATUS onCollidingVia( ITEM* aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo, int aNextRank );
188 SHOVE_STATUS onReverseCollidingVia( LINE& aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo );
189 SHOVE_STATUS pushOrShoveVia( VIA* aVia, const VECTOR2I& aForce, int aNextRank );
190
192
193 void unwindLineStack( const LINKED_ITEM* aSeg );
194 void unwindLineStack( const ITEM* aItem );
195
196 void runOptimizer( NODE* aNode );
197
198 bool pushLineStack( const LINE& aL, bool aKeepCurrentOnTop = false );
199 void popLineStack();
200
201 LINE assembleLine( const LINKED_ITEM* aSeg, int* aIndex = nullptr, bool aPreCleanup = false );
202
203 void replaceItems( ITEM* aOld, std::unique_ptr< ITEM > aNew );
204 ROOT_LINE_ENTRY* replaceLine( LINE& aOld, LINE& aNew,
205 bool aIncludeInChangedArea = true,
206 NODE *aNode = nullptr );
207
208 ROOT_LINE_ENTRY* findRootLine( const LINE& aLine );
210 ROOT_LINE_ENTRY* touchRootLine( const LINE& aLine );
212 void pruneRootLines( NODE *aRemovedNode );
213
214
215 SHOVE_STATUS shoveIteration( int aIter );
217
218 int getClearance( const ITEM* aA, const ITEM* aB ) const;
219 bool fixupViaCollisions( const LINE* aCurrent, OBSTACLE& obs );
220 void sanityCheck( LINE* aOld, LINE* aNew );
221 void reconstructHeads( bool aShoveFailed );
222 void removeHeads();
223 bool preShoveCleanup( LINE* aOld, LINE* aNew );
224 const wxString formatPolicy( int aPolicy );
225
226 std::vector<SPRINGBACK_TAG> m_nodeStack;
227 std::vector<LINE> m_lineStack;
228 std::vector<LINE> m_optimizerQueue;
229 std::deque<HEAD_LINE_ENTRY> m_headLines;
230
231 std::unordered_map<LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY*> m_rootLineHistory;
232
242
244
247
248};
249
250}
251
252#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:97
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:231
The actual Push and Shove algorithm.
Definition: pns_shove.h:46
void SetSpringbackDoNotTouchNode(const NODE *aNode)
Definition: pns_shove.cpp:2057
SHOVE_STATUS shoveIteration(int aIter)
Definition: pns_shove.cpp:1466
SHOVE_STATUS shoveMainLoop()
Definition: pns_shove.cpp:1710
std::vector< SHAPE_LINE_CHAIN > HULL_SET
Definition: pns_shove.h:110
void reconstructHeads(bool aShoveFailed)
Definition: pns_shove.cpp:2150
int m_iter
Definition: pns_shove.h:238
std::pair< LINE, LINE > LINE_PAIR
Definition: pns_shove.h:112
SHOVE_STATUS onReverseCollidingVia(LINE &aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo)
Definition: pns_shove.cpp:1165
OPT_BOX2I m_affectedArea
Definition: pns_shove.h:246
std::vector< LINE > m_lineStack
Definition: pns_shove.h:227
void popLineStack()
Definition: pns_shove.cpp:1355
ROOT_LINE_ENTRY * findRootLine(const LINE &aLine)
Definition: pns_shove.cpp:1772
std::vector< SPRINGBACK_TAG > m_nodeStack
Definition: pns_shove.h:226
@ 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_WALK_FORWARD
Definition: pns_shove.h:62
SHOVE_STATUS onCollidingArc(LINE &aCurrent, ARC *aObstacleArc)
Definition: pns_shove.cpp:588
std::optional< LINE > OPT_LINE
Definition: pns_shove.h:111
NODE * m_currentNode
Definition: pns_shove.h:234
SHOVE_STATUS Run()
Definition: pns_shove.cpp:2251
LINE assembleLine(const LINKED_ITEM *aSeg, int *aIndex=nullptr, bool aPreCleanup=false)
Definition: pns_shove.cpp:209
void ForceClearance(bool aEnabled, int aClearance)
Definition: pns_shove.h:88
bool checkShoveDirection(const LINE &aCurLine, const LINE &aObstacleLine, const LINE &aShovedLine) const
Definition: pns_shove.cpp:232
int m_forceClearance
Definition: pns_shove.h:240
SHOVE_STATUS onCollidingSegment(LINE &aCurrent, SEGMENT *aObstacleSeg)
Definition: pns_shove.cpp:529
void DisablePostShoveOptimizations(int aMask)
Definition: pns_shove.cpp:2051
bool RewindSpringbackTo(NODE *aNode)
Definition: pns_shove.cpp:1986
bool pushLineStack(const LINE &aL, bool aKeepCurrentOnTop=false)
Definition: pns_shove.cpp:1331
@ 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:1756
bool RewindToLastLockedNode()
Definition: pns_shove.cpp:2020
SHOVE_STATUS ShoveDraggingVia(const VIA_HANDLE aOldVia, const VECTOR2I &aWhere, VIA_HANDLE &aNewVia)
std::deque< HEAD_LINE_ENTRY > m_headLines
Definition: pns_shove.h:229
int m_defaultPolicy
Definition: pns_shove.h:245
const wxString formatPolicy(int aPolicy)
Definition: pns_shove.cpp:2474
bool ShoveObstacleLine(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine)
Definition: pns_shove.cpp:428
SHOVE_STATUS onCollidingVia(ITEM *aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo, int aNextRank)
Definition: pns_shove.cpp:1072
void sanityCheck(LINE *aOld, LINE *aNew)
Definition: pns_shove.cpp:178
int m_restrictSpringbackTagId
Definition: pns_shove.h:236
void runOptimizer(NODE *aNode)
Definition: pns_shove.cpp:1843
ROOT_LINE_ENTRY * touchRootLine(const LINE &aLine)
Definition: pns_shove.cpp:1796
const PNS::LINE GetModifiedHead(int aIndex) const
Definition: pns_shove.cpp:2503
bool HeadsModified(int aIndex=-1) const
Definition: pns_shove.cpp:2495
bool shoveLineToHullSet(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine, const HULL_SET &aHulls)
Definition: pns_shove.cpp:299
void UnlockSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:2034
NODE * m_root
Definition: pns_shove.h:233
NODE * reduceSpringback(const ITEM_SET &aHeadSet)
Definition: pns_shove.cpp:824
bool AddLockedSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:1972
void SetDefaultShovePolicy(int aPolicy)
Definition: pns_shove.cpp:2063
void AddHeads(const LINE &aHead, int aPolicy=SHP_DEFAULT)
Definition: pns_shove.cpp:2088
void SetShovePolicy(const LINKED_ITEM *aItem, int aPolicy)
Definition: pns_shove.cpp:2069
void unwindLineStack(const LINKED_ITEM *aSeg)
Definition: pns_shove.cpp:1286
bool preShoveCleanup(LINE *aOld, LINE *aNew)
Definition: pns_shove.cpp:2222
NODE * CurrentNode()
Definition: pns_shove.cpp:1966
int getClearance(const ITEM *aA, const ITEM *aB) const
Definition: pns_shove.cpp:161
bool m_multiLineMode
Definition: pns_shove.h:241
std::vector< LINE > m_optimizerQueue
Definition: pns_shove.h:228
ROOT_LINE_ENTRY * replaceLine(LINE &aOld, LINE &aNew, bool aIncludeInChangedArea=true, NODE *aNode=nullptr)
Definition: pns_shove.cpp:80
SHOVE_STATUS onCollidingSolid(LINE &aCurrent, ITEM *aObstacle, OBSTACLE &aObstacleInfo)
Definition: pns_shove.cpp:675
bool pushSpringback(NODE *aNode, const OPT_BOX2I &aAffectedArea)
Definition: pns_shove.cpp:885
int m_optFlagDisableMask
Definition: pns_shove.h:243
SHOVE_STATUS onCollidingLine(LINE &aCurrent, LINE &aObstacle, int aNextRank)
Definition: pns_shove.cpp:638
bool fixupViaCollisions(const LINE *aCurrent, OBSTACLE &obs)
Definition: pns_shove.cpp:1381
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:252
VIA * m_draggedVia
Definition: pns_shove.h:237
bool m_headsModified
Definition: pns_shove.h:239
const NODE * m_springbackDoNotTouchNode
Definition: pns_shove.h:235
SHOVE_STATUS pushOrShoveVia(VIA *aVia, const VECTOR2I &aForce, int aNextRank)
Definition: pns_shove.cpp:943
void ClearHeads()
Definition: pns_shove.cpp:2082
std::unordered_map< LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY * > m_rootLineHistory
Definition: pns_shove.h:231
void pruneRootLines(NODE *aRemovedNode)
Definition: pns_shove.cpp:801
const VIA_HANDLE GetModifiedHeadVia(int aIndex) const
Definition: pns_shove.cpp:2508
std::vector< LINE_PAIR > LINE_PAIR_VEC
Definition: pns_shove.h:113
void removeHeads()
Definition: pns_shove.cpp:2114
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:87
Definition: pns_shove.h:130
std::optional< VIA_HANDLE > theVia
Definition: pns_shove.h:145
HEAD_LINE_ENTRY(const LINE &aOrig, int aPolicy=SHP_DEFAULT)
Definition: pns_shove.h:131
int policy
Definition: pns_shove.h:150
VIA * draggedVia
Definition: pns_shove.h:146
std::optional< VIA_HANDLE > prevVia
Definition: pns_shove.h:144
bool geometryModified
Definition: pns_shove.h:143
std::optional< LINE > newHead
Definition: pns_shove.h:149
HEAD_LINE_ENTRY(VIA_HANDLE aVia, int aPolicy=SHP_DEFAULT)
Definition: pns_shove.h:138
std::optional< LINE > origHead
Definition: pns_shove.h:148
VECTOR2I viaNewPos
Definition: pns_shove.h:147
Definition: pns_shove.h:116
int policy
Definition: pns_shove.h:125
bool isHead
Definition: pns_shove.h:126
std::optional< LINE > newLine
Definition: pns_shove.h:124
VIA * oldVia
Definition: pns_shove.h:122
VIA * newVia
Definition: pns_shove.h:123
ROOT_LINE_ENTRY(LINE *aLine=nullptr, int aPolicy=SHP_DEFAULT)
Definition: pns_shove.h:117
LINE * rootLine
Definition: pns_shove.h:121
std::vector< VIA_HANDLE > m_draggedVias
Definition: pns_shove.h:163