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
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef __PNS_SHOVE_H
23#define __PNS_SHOVE_H
24
25#include <memory>
26#include <vector>
27#include <stack>
28
29#include <math/box2.h>
30
31#include "pns_optimizer.h"
33#include "pns_algo_base.h"
34#include "pns_logger.h"
35#include "range.h"
36
37namespace PNS {
38
39class LINE;
40class NODE;
41class ROUTER;
42
46class SHOVE : public ALGO_BASE
47{
48public:
49
58
70
71
72 void SetDefaultShovePolicy( int aPolicy );
73
74 void SetShovePolicy( const LINKED_ITEM* aItem, int aPolicy );
75 void SetShovePolicy( const LINE& aLine, int aPolicy );
76
77 SHOVE( NODE* aWorld, ROUTER* aRouter );
78 ~SHOVE();
79
80 void ClearHeads();
81 void AddHeads( const LINE& aHead, int aPolicy = SHP_DEFAULT );
82 void AddHeads( VIA_HANDLE aHead, VECTOR2I aNewPos, int aPolicy = SHP_DEFAULT );
83
85
86 SHOVE_STATUS ShoveDraggingVia( const VIA_HANDLE aOldVia, const VECTOR2I& aWhere,
87 VIA_HANDLE& aNewVia );
88 bool ShoveObstacleLine( const LINE& aCurLine, const LINE& aObstacleLine,
89 LINE& aResultLine );
90
91 void ForceClearance ( bool aEnabled, int aClearance )
92 {
93 if( aEnabled )
94 m_forceClearance = aClearance;
95 else
97 }
98
100
101 bool HeadsModified( int aIndex = -1 ) const;
102 const PNS::LINE GetModifiedHead( int aIndex ) const;
103 const VIA_HANDLE GetModifiedHeadVia( int aIndex ) const;
104
105 bool AddLockedSpringbackNode( NODE* aNode );
106 void UnlockSpringbackNode( NODE* aNode );
107 bool RewindSpringbackTo( NODE* aNode );
109 void DisablePostShoveOptimizations( int aMask );
110 void SetSpringbackDoNotTouchNode( const NODE *aNode );
111
112private:
113 typedef std::vector<SHAPE_LINE_CHAIN> HULL_SET;
114 typedef std::optional<LINE> OPT_LINE;
115 typedef std::pair<LINE, LINE> LINE_PAIR;
116 typedef std::vector<LINE_PAIR> LINE_PAIR_VEC;
117
119 {
120 ROOT_LINE_ENTRY( std::unique_ptr<LINE> aLine = {}, int aPolicy = SHP_DEFAULT ) :
121 rootLine( std::move( aLine ) ),
122 policy( aPolicy )
123 {}
124
125 std::unique_ptr<LINE> rootLine;
126 VIA* oldVia = nullptr;
127 VIA* newVia = nullptr;
128 std::optional<LINE> newLine;
130 bool isHead = false;
131 };
132
134 {
135 HEAD_LINE_ENTRY( const LINE& aOrig, int aPolicy = SHP_DEFAULT ) :
136 origHead( aOrig ),
137 policy( aPolicy )
138 {
139 origHead->ClearLinks();
140 };
141
142 HEAD_LINE_ENTRY( VIA_HANDLE aVia, int aPolicy = SHP_DEFAULT ) :
143 theVia( aVia ),
144 policy( aPolicy )
145 {};
146
148 {
149 *this = aOther;
150 }
151
152 // Copy operator
153 HEAD_LINE_ENTRY& operator=( const HEAD_LINE_ENTRY& aOther ) noexcept
154 {
155 geometryModified = aOther.geometryModified;
156 prevVia = aOther.prevVia;
157 theVia = aOther.theVia;
158 draggedVia = aOther.draggedVia;
159 viaNewPos = aOther.viaNewPos;
160 origHead = aOther.origHead;
161 newHead = aOther.newHead;
162 policy = aOther.policy;
163 return *this;
164 }
165
166 // Move assignment operator
168 {
169 if (this != &aOther)
170 {
171 geometryModified = aOther.geometryModified;
172 prevVia = aOther.prevVia;
173 theVia = aOther.theVia;
174 draggedVia = aOther.draggedVia;
175 viaNewPos = aOther.viaNewPos;
176 origHead = std::move( aOther.origHead );
177 newHead = std::move( aOther.newHead );
178 policy = aOther.policy;
179 }
180
181 return *this;
182 }
183
184 bool geometryModified = false;
185 std::optional<VIA_HANDLE> prevVia;
186 std::optional<VIA_HANDLE> theVia;
187 VIA* draggedVia = nullptr;
189 std::optional<LINE> origHead;
190 std::optional<LINE> newHead;
192 };
193
195 {
197 m_length( 0 ),
198 m_node( nullptr ),
199 m_seq( 0 ),
200 m_locked( false )
201 {}
202
203 int64_t m_length;
204 std::vector<VIA_HANDLE> m_draggedVias;
208 int m_seq;
210 };
211
212 bool pruneLineFromOptimizerQueue( const LINE& aLine );
213
214 bool shoveLineToHullSet( const LINE& aCurLine, const LINE& aObstacleLine, LINE& aResultLine,
215 const HULL_SET& aHulls, bool aPermitAdjustingStart = false,
216 bool aPermitAdjustingEnd = false );
217
218 NODE* reduceSpringback( const ITEM_SET& aHeadSet );
219
220 bool patchTadpoleVia( ITEM* nearest, LINE& current );
221
222 bool pushSpringback( NODE* aNode, const OPT_BOX2I& aAffectedArea );
223
224 bool shoveLineFromLoneVia( const LINE& aCurLine, const LINE& aObstacleLine,
225 LINE& aResultLine );
226 bool checkShoveDirection( const LINE& aCurLine, const LINE& aObstacleLine,
227 const LINE& aShovedLine ) const;
228
229 SHOVE_STATUS onCollidingArc( LINE& aCurrent, ARC* aObstacleArc );
230 SHOVE_STATUS onCollidingLine( LINE& aCurrent, LINE& aObstacle, int aNextRank );
231 SHOVE_STATUS onCollidingSegment( LINE& aCurrent, SEGMENT* aObstacleSeg );
232 SHOVE_STATUS onCollidingSolid( LINE& aCurrent, ITEM* aObstacle, OBSTACLE& aObstacleInfo );
233 SHOVE_STATUS onCollidingVia( ITEM* aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo, int aNextRank );
234 SHOVE_STATUS onReverseCollidingVia( LINE& aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo );
235 SHOVE_STATUS pushOrShoveVia( VIA* aVia, const VECTOR2I& aForce, int aNextRank, bool aDontUnwindStack = false );
236
238
239 void unwindLineStack( const LINKED_ITEM* aSeg );
240 void unwindLineStack( const ITEM* aItem );
241
242 void runOptimizer( NODE* aNode );
243
244 bool pushLineStack( const LINE& aL, bool aKeepCurrentOnTop = false );
245 void popLineStack();
246
247 LINE assembleLine( const LINKED_ITEM* aSeg, int* aIndex = nullptr, bool aPreCleanup = false );
248
249 void replaceItems( ITEM* aOld, std::unique_ptr< ITEM > aNew );
250 ROOT_LINE_ENTRY* replaceLine( LINE& aOld, LINE& aNew,
251 bool aIncludeInChangedArea = true,
252 bool aAllowRedundantSegments = true,
253 NODE *aNode = nullptr );
254
255 ROOT_LINE_ENTRY* allocRootLine( std::unique_ptr<LINE> aLine,
256 int aPolicy = SHP_DEFAULT );
257 ROOT_LINE_ENTRY* findRootLine( const LINE& aLine ) const;
258 ROOT_LINE_ENTRY* findRootLine( const LINKED_ITEM *aItem ) const;
259 ROOT_LINE_ENTRY* touchRootLine( const LINE& aLine );
261 void pruneRootLines( NODE *aRemovedNode );
262
263
264 SHOVE_STATUS shoveIteration( int aIter );
266
267 int getClearance( const ITEM* aA, const ITEM* aB ) const;
268 bool fixupViaCollisions( const LINE* aCurrent, OBSTACLE& obs );
269 void sanityCheck( LINE* aOld, LINE* aNew );
270 void reconstructHeads( bool aShoveFailed );
271 void removeHeads();
272 bool preShoveCleanup( LINE* aOld, LINE* aNew );
273 const wxString formatPolicy( int aPolicy );
274
275 std::vector<SPRINGBACK_TAG> m_nodeStack;
276 std::vector<LINE> m_lineStack;
277 std::vector<LINE> m_optimizerQueue;
278 std::deque<HEAD_LINE_ENTRY> m_headLines;
279
280 // UID entries may alias the same history entry, so ownership lives outside the index.
281 std::vector<std::unique_ptr<ROOT_LINE_ENTRY>> m_rootLineHistoryEntries;
282 std::unordered_map<LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY*> m_rootLineHistory;
283
293
295
298
299};
300
301}
302
303#endif // __PNS_SHOVE_H
std::optional< BOX2I > OPT_BOX2I
Definition box2.h:922
ALGO_BASE(ROUTER *aRouter)
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:242
void SetSpringbackDoNotTouchNode(const NODE *aNode)
SHOVE_STATUS shoveIteration(int aIter)
SHOVE_STATUS shoveMainLoop()
std::vector< SHAPE_LINE_CHAIN > HULL_SET
Definition pns_shove.h:113
void reconstructHeads(bool aShoveFailed)
std::pair< LINE, LINE > LINE_PAIR
Definition pns_shove.h:115
SHOVE_STATUS pushOrShoveVia(VIA *aVia, const VECTOR2I &aForce, int aNextRank, bool aDontUnwindStack=false)
SHOVE_STATUS onReverseCollidingVia(LINE &aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo)
OPT_BOX2I m_affectedArea
Definition pns_shove.h:297
std::vector< LINE > m_lineStack
Definition pns_shove.h:276
void popLineStack()
std::vector< SPRINGBACK_TAG > m_nodeStack
Definition pns_shove.h:275
@ SHP_DONT_OPTIMIZE
Definition pns_shove.h:66
@ SHP_WALK_BACK
Definition pns_shove.h:64
@ SHP_DONT_LOCK_ENDPOINTS
Definition pns_shove.h:67
@ SHP_WALK_FORWARD
Definition pns_shove.h:63
bool patchTadpoleVia(ITEM *nearest, LINE &current)
SHOVE_STATUS onCollidingArc(LINE &aCurrent, ARC *aObstacleArc)
bool shoveLineToHullSet(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine, const HULL_SET &aHulls, bool aPermitAdjustingStart=false, bool aPermitAdjustingEnd=false)
std::vector< std::unique_ptr< ROOT_LINE_ENTRY > > m_rootLineHistoryEntries
Definition pns_shove.h:281
std::optional< LINE > OPT_LINE
Definition pns_shove.h:114
NODE * m_currentNode
Definition pns_shove.h:285
SHOVE_STATUS Run()
LINE assembleLine(const LINKED_ITEM *aSeg, int *aIndex=nullptr, bool aPreCleanup=false)
void ForceClearance(bool aEnabled, int aClearance)
Definition pns_shove.h:91
bool checkShoveDirection(const LINE &aCurLine, const LINE &aObstacleLine, const LINE &aShovedLine) const
int m_forceClearance
Definition pns_shove.h:291
SHOVE_STATUS onCollidingSegment(LINE &aCurrent, SEGMENT *aObstacleSeg)
SHOVE(NODE *aWorld, ROUTER *aRouter)
void DisablePostShoveOptimizations(int aMask)
bool RewindSpringbackTo(NODE *aNode)
bool pushLineStack(const LINE &aL, bool aKeepCurrentOnTop=false)
@ SH_INCOMPLETE
Definition pns_shove.h:54
@ SH_HEAD_MODIFIED
Definition pns_shove.h:55
OPT_BOX2I totalAffectedArea() const
bool RewindToLastLockedNode()
SHOVE_STATUS ShoveDraggingVia(const VIA_HANDLE aOldVia, const VECTOR2I &aWhere, VIA_HANDLE &aNewVia)
std::deque< HEAD_LINE_ENTRY > m_headLines
Definition pns_shove.h:278
int m_defaultPolicy
Definition pns_shove.h:296
const wxString formatPolicy(int aPolicy)
bool ShoveObstacleLine(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine)
SHOVE_STATUS onCollidingVia(ITEM *aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo, int aNextRank)
void sanityCheck(LINE *aOld, LINE *aNew)
int m_restrictSpringbackTagId
Definition pns_shove.h:287
void runOptimizer(NODE *aNode)
ROOT_LINE_ENTRY * touchRootLine(const LINE &aLine)
const PNS::LINE GetModifiedHead(int aIndex) const
bool HeadsModified(int aIndex=-1) const
void UnlockSpringbackNode(NODE *aNode)
NODE * m_root
Definition pns_shove.h:284
ROOT_LINE_ENTRY * allocRootLine(std::unique_ptr< LINE > aLine, int aPolicy=SHP_DEFAULT)
NODE * reduceSpringback(const ITEM_SET &aHeadSet)
bool AddLockedSpringbackNode(NODE *aNode)
ROOT_LINE_ENTRY * findRootLine(const LINE &aLine) const
void SetDefaultShovePolicy(int aPolicy)
void AddHeads(const LINE &aHead, int aPolicy=SHP_DEFAULT)
void SetShovePolicy(const LINKED_ITEM *aItem, int aPolicy)
void unwindLineStack(const LINKED_ITEM *aSeg)
bool preShoveCleanup(LINE *aOld, LINE *aNew)
NODE * CurrentNode()
int getClearance(const ITEM *aA, const ITEM *aB) const
bool m_multiLineMode
Definition pns_shove.h:292
std::vector< LINE > m_optimizerQueue
Definition pns_shove.h:277
SHOVE_STATUS onCollidingSolid(LINE &aCurrent, ITEM *aObstacle, OBSTACLE &aObstacleInfo)
bool pushSpringback(NODE *aNode, const OPT_BOX2I &aAffectedArea)
int m_optFlagDisableMask
Definition pns_shove.h:294
SHOVE_STATUS onCollidingLine(LINE &aCurrent, LINE &aObstacle, int aNextRank)
bool fixupViaCollisions(const LINE *aCurrent, OBSTACLE &obs)
bool pruneLineFromOptimizerQueue(const LINE &aLine)
void replaceItems(ITEM *aOld, std::unique_ptr< ITEM > aNew)
Definition pns_shove.cpp:52
bool shoveLineFromLoneVia(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine)
VIA * m_draggedVia
Definition pns_shove.h:288
bool m_headsModified
Definition pns_shove.h:290
const NODE * m_springbackDoNotTouchNode
Definition pns_shove.h:286
void ClearHeads()
std::unordered_map< LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY * > m_rootLineHistory
Definition pns_shove.h:282
void pruneRootLines(NODE *aRemovedNode)
const VIA_HANDLE GetModifiedHeadVia(int aIndex) const
ROOT_LINE_ENTRY * replaceLine(LINE &aOld, LINE &aNew, bool aIncludeInChangedArea=true, bool aAllowRedundantSegments=true, NODE *aNode=nullptr)
Definition pns_shove.cpp:83
std::vector< LINE_PAIR > LINE_PAIR_VEC
Definition pns_shove.h:116
void removeHeads()
Push and Shove diff pair dimensions (gap) settings dialog.
@ SHP_DEFAULT
STL namespace.
Hold an object colliding with another object, along with some useful data about the collision.
Definition pns_node.h:89
HEAD_LINE_ENTRY & operator=(const HEAD_LINE_ENTRY &aOther) noexcept
Definition pns_shove.h:153
std::optional< VIA_HANDLE > theVia
Definition pns_shove.h:186
HEAD_LINE_ENTRY(const HEAD_LINE_ENTRY &aOther)
Definition pns_shove.h:147
HEAD_LINE_ENTRY(const LINE &aOrig, int aPolicy=SHP_DEFAULT)
Definition pns_shove.h:135
int policy
Definition pns_shove.h:191
HEAD_LINE_ENTRY & operator=(HEAD_LINE_ENTRY &&aOther) noexcept
Definition pns_shove.h:167
VIA * draggedVia
Definition pns_shove.h:187
std::optional< VIA_HANDLE > prevVia
Definition pns_shove.h:185
bool geometryModified
Definition pns_shove.h:184
std::optional< LINE > newHead
Definition pns_shove.h:190
HEAD_LINE_ENTRY(VIA_HANDLE aVia, int aPolicy=SHP_DEFAULT)
Definition pns_shove.h:142
std::optional< LINE > origHead
Definition pns_shove.h:189
VECTOR2I viaNewPos
Definition pns_shove.h:188
Definition pns_shove.h:119
std::unique_ptr< LINE > rootLine
Definition pns_shove.h:125
int policy
Definition pns_shove.h:129
bool isHead
Definition pns_shove.h:130
std::optional< LINE > newLine
Definition pns_shove.h:128
VIA * oldVia
Definition pns_shove.h:126
ROOT_LINE_ENTRY(std::unique_ptr< LINE > aLine={}, int aPolicy=SHP_DEFAULT)
Definition pns_shove.h:120
VIA * newVia
Definition pns_shove.h:127
std::vector< VIA_HANDLE > m_draggedVias
Definition pns_shove.h:204
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683