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 void replaceLine( LINE& aOld, LINE& aNew, bool aIncludeInChangedArea = true,
205 NODE *aNode = nullptr );
206
207 ROOT_LINE_ENTRY* findRootLine( const LINE& aLine );
209 ROOT_LINE_ENTRY* touchRootLine( const LINE& aLine );
211 void pruneRootLines( NODE *aRemovedNode );
212
213
214 SHOVE_STATUS shoveIteration( int aIter );
216
217 int getClearance( const ITEM* aA, const ITEM* aB ) const;
218 bool fixupViaCollisions( const LINE* aCurrent, OBSTACLE& obs );
219 void sanityCheck( LINE* aOld, LINE* aNew );
220 void reconstructHeads( bool aShoveFailed );
221 void removeHeads();
222 bool preShoveCleanup( LINE* aOld, LINE* aNew );
223 const wxString formatPolicy( int aPolicy );
224
225 std::vector<SPRINGBACK_TAG> m_nodeStack;
226 std::vector<LINE> m_lineStack;
227 std::vector<LINE> m_optimizerQueue;
228 std::deque<HEAD_LINE_ENTRY> m_headLines;
229
230 std::unordered_map<LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY*> m_rootLineHistory;
231
241
243
246
247};
248
249}
250
251#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:207
The actual Push and Shove algorithm.
Definition: pns_shove.h:46
void SetSpringbackDoNotTouchNode(const NODE *aNode)
Definition: pns_shove.cpp:2016
SHOVE_STATUS shoveIteration(int aIter)
Definition: pns_shove.cpp:1429
SHOVE_STATUS shoveMainLoop()
Definition: pns_shove.cpp:1669
std::vector< SHAPE_LINE_CHAIN > HULL_SET
Definition: pns_shove.h:110
void reconstructHeads(bool aShoveFailed)
Definition: pns_shove.cpp:2109
int m_iter
Definition: pns_shove.h:237
std::pair< LINE, LINE > LINE_PAIR
Definition: pns_shove.h:112
SHOVE_STATUS onReverseCollidingVia(LINE &aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo)
Definition: pns_shove.cpp:1142
OPT_BOX2I m_affectedArea
Definition: pns_shove.h:245
std::vector< LINE > m_lineStack
Definition: pns_shove.h:226
void popLineStack()
Definition: pns_shove.cpp:1321
ROOT_LINE_ENTRY * findRootLine(const LINE &aLine)
Definition: pns_shove.cpp:1731
std::vector< SPRINGBACK_TAG > m_nodeStack
Definition: pns_shove.h:225
@ 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:582
std::optional< LINE > OPT_LINE
Definition: pns_shove.h:111
NODE * m_currentNode
Definition: pns_shove.h:233
SHOVE_STATUS Run()
Definition: pns_shove.cpp:2210
LINE assembleLine(const LINKED_ITEM *aSeg, int *aIndex=nullptr, bool aPreCleanup=false)
Definition: pns_shove.cpp:206
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:229
int m_forceClearance
Definition: pns_shove.h:239
SHOVE_STATUS onCollidingSegment(LINE &aCurrent, SEGMENT *aObstacleSeg)
Definition: pns_shove.cpp:523
void DisablePostShoveOptimizations(int aMask)
Definition: pns_shove.cpp:2010
bool RewindSpringbackTo(NODE *aNode)
Definition: pns_shove.cpp:1945
bool pushLineStack(const LINE &aL, bool aKeepCurrentOnTop=false)
Definition: pns_shove.cpp:1297
@ 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:1715
bool RewindToLastLockedNode()
Definition: pns_shove.cpp:1979
SHOVE_STATUS ShoveDraggingVia(const VIA_HANDLE aOldVia, const VECTOR2I &aWhere, VIA_HANDLE &aNewVia)
std::deque< HEAD_LINE_ENTRY > m_headLines
Definition: pns_shove.h:228
void replaceLine(LINE &aOld, LINE &aNew, bool aIncludeInChangedArea=true, NODE *aNode=nullptr)
Definition: pns_shove.cpp:80
int m_defaultPolicy
Definition: pns_shove.h:244
const wxString formatPolicy(int aPolicy)
Definition: pns_shove.cpp:2434
bool ShoveObstacleLine(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine)
Definition: pns_shove.cpp:425
SHOVE_STATUS onCollidingVia(ITEM *aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo, int aNextRank)
Definition: pns_shove.cpp:1061
void sanityCheck(LINE *aOld, LINE *aNew)
Definition: pns_shove.cpp:175
int m_restrictSpringbackTagId
Definition: pns_shove.h:235
void runOptimizer(NODE *aNode)
Definition: pns_shove.cpp:1802
ROOT_LINE_ENTRY * touchRootLine(const LINE &aLine)
Definition: pns_shove.cpp:1755
const PNS::LINE GetModifiedHead(int aIndex) const
Definition: pns_shove.cpp:2463
bool HeadsModified(int aIndex=-1) const
Definition: pns_shove.cpp:2455
bool shoveLineToHullSet(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine, const HULL_SET &aHulls)
Definition: pns_shove.cpp:296
void UnlockSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:1993
NODE * m_root
Definition: pns_shove.h:232
NODE * reduceSpringback(const ITEM_SET &aHeadSet)
Definition: pns_shove.cpp:817
bool AddLockedSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:1931
void SetDefaultShovePolicy(int aPolicy)
Definition: pns_shove.cpp:2022
void AddHeads(const LINE &aHead, int aPolicy=SHP_DEFAULT)
Definition: pns_shove.cpp:2047
void SetShovePolicy(const LINKED_ITEM *aItem, int aPolicy)
Definition: pns_shove.cpp:2028
void unwindLineStack(const LINKED_ITEM *aSeg)
Definition: pns_shove.cpp:1252
bool preShoveCleanup(LINE *aOld, LINE *aNew)
Definition: pns_shove.cpp:2181
NODE * CurrentNode()
Definition: pns_shove.cpp:1925
int getClearance(const ITEM *aA, const ITEM *aB) const
Definition: pns_shove.cpp:158
bool m_multiLineMode
Definition: pns_shove.h:240
std::vector< LINE > m_optimizerQueue
Definition: pns_shove.h:227
SHOVE_STATUS onCollidingSolid(LINE &aCurrent, ITEM *aObstacle, OBSTACLE &aObstacleInfo)
Definition: pns_shove.cpp:669
bool pushSpringback(NODE *aNode, const OPT_BOX2I &aAffectedArea)
Definition: pns_shove.cpp:878
int m_optFlagDisableMask
Definition: pns_shove.h:242
SHOVE_STATUS onCollidingLine(LINE &aCurrent, LINE &aObstacle, int aNextRank)
Definition: pns_shove.cpp:632
bool fixupViaCollisions(const LINE *aCurrent, OBSTACLE &obs)
Definition: pns_shove.cpp:1347
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:249
VIA * m_draggedVia
Definition: pns_shove.h:236
bool m_headsModified
Definition: pns_shove.h:238
const NODE * m_springbackDoNotTouchNode
Definition: pns_shove.h:234
SHOVE_STATUS pushOrShoveVia(VIA *aVia, const VECTOR2I &aForce, int aNextRank)
Definition: pns_shove.cpp:936
void ClearHeads()
Definition: pns_shove.cpp:2041
std::unordered_map< LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY * > m_rootLineHistory
Definition: pns_shove.h:230
void pruneRootLines(NODE *aRemovedNode)
Definition: pns_shove.cpp:794
const VIA_HANDLE GetModifiedHeadVia(int aIndex) const
Definition: pns_shove.cpp:2468
std::vector< LINE_PAIR > LINE_PAIR_VEC
Definition: pns_shove.h:113
void removeHeads()
Definition: pns_shove.cpp:2073
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