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 "pns_node.h"
36#include "range.h"
37
38namespace PNS {
39
40class LINE;
41class NODE;
42class ROUTER;
43
47class SHOVE : public ALGO_BASE
48{
49public:
50
59
71
72
73 void SetDefaultShovePolicy( int aPolicy );
74
75 void SetShovePolicy( const LINKED_ITEM* aItem, int aPolicy );
76 void SetShovePolicy( const LINE& aLine, int aPolicy );
77
78 SHOVE( NODE* aWorld, ROUTER* aRouter );
79 ~SHOVE();
80
81 void ClearHeads();
82 void AddHeads( const LINE& aHead, int aPolicy = SHP_DEFAULT );
83 void AddHeads( VIA_HANDLE aHead, VECTOR2I aNewPos, int aPolicy = SHP_DEFAULT );
84
86
87 SHOVE_STATUS ShoveDraggingVia( const VIA_HANDLE aOldVia, const VECTOR2I& aWhere,
88 VIA_HANDLE& aNewVia );
89 bool ShoveObstacleLine( const LINE& aCurLine, const LINE& aObstacleLine,
90 LINE& aResultLine );
91
94
96
97 bool HeadsModified( int aIndex = -1 ) const;
98 const PNS::LINE GetModifiedHead( int aIndex ) const;
99 const VIA_HANDLE GetModifiedHeadVia( int aIndex ) const;
100
101 bool AddLockedSpringbackNode( NODE* aNode );
102 void UnlockSpringbackNode( NODE* aNode );
103 bool RewindSpringbackTo( NODE* aNode );
105 void DisablePostShoveOptimizations( int aMask );
106 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( std::unique_ptr<LINE> aLine = {}, int aPolicy = SHP_DEFAULT ) :
118 rootLine( std::move( aLine ) ),
119 policy( aPolicy )
120 {}
121
122 std::unique_ptr<LINE> rootLine;
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
145 {
146 *this = aOther;
147 }
148
149 // Copy operator
150 HEAD_LINE_ENTRY& operator=( const HEAD_LINE_ENTRY& aOther ) noexcept
151 {
152 geometryModified = aOther.geometryModified;
153 prevVia = aOther.prevVia;
154 theVia = aOther.theVia;
155 draggedVia = aOther.draggedVia;
156 viaNewPos = aOther.viaNewPos;
157 origHead = aOther.origHead;
158 newHead = aOther.newHead;
159 policy = aOther.policy;
160 return *this;
161 }
162
163 // Move assignment operator
165 {
166 if (this != &aOther)
167 {
168 geometryModified = aOther.geometryModified;
169 prevVia = aOther.prevVia;
170 theVia = aOther.theVia;
171 draggedVia = aOther.draggedVia;
172 viaNewPos = aOther.viaNewPos;
173 origHead = std::move( aOther.origHead );
174 newHead = std::move( aOther.newHead );
175 policy = aOther.policy;
176 }
177
178 return *this;
179 }
180
181 bool geometryModified = false;
182 std::optional<VIA_HANDLE> prevVia;
183 std::optional<VIA_HANDLE> theVia;
184 VIA* draggedVia = nullptr;
186 std::optional<LINE> origHead;
187 std::optional<LINE> newHead;
189 };
190
192 {
194 m_length( 0 ),
195 m_node( nullptr ),
196 m_seq( 0 ),
197 m_locked( false )
198 {}
199
200 int64_t m_length;
201 std::vector<VIA_HANDLE> m_draggedVias;
205 int m_seq;
207 };
208
209 bool pruneLineFromOptimizerQueue( const LINE& aLine );
210
211 bool shoveLineToHullSet( const LINE& aCurLine, const LINE& aObstacleLine, LINE& aResultLine,
212 const HULL_SET& aHulls, bool aPermitAdjustingStart = false,
213 bool aPermitAdjustingEnd = false );
214
215 NODE* reduceSpringback( const ITEM_SET& aHeadSet );
216
217 bool patchTadpoleVia( ITEM* nearest, LINE& current );
218
219 bool pushSpringback( NODE* aNode, const OPT_BOX2I& aAffectedArea );
220
221 bool shoveLineFromLoneVia( const LINE& aCurLine, const LINE& aObstacleLine,
222 LINE& aResultLine );
223 bool checkShoveDirection( const LINE& aCurLine, const LINE& aObstacleLine,
224 const LINE& aShovedLine ) const;
225
226 SHOVE_STATUS onCollidingArc( LINE& aCurrent, ARC* aObstacleArc );
227 SHOVE_STATUS onCollidingLine( LINE& aCurrent, LINE& aObstacle, int aNextRank );
228 SHOVE_STATUS onCollidingSegment( LINE& aCurrent, SEGMENT* aObstacleSeg );
229 SHOVE_STATUS onCollidingSolid( LINE& aCurrent, ITEM* aObstacle, OBSTACLE& aObstacleInfo );
230 SHOVE_STATUS onCollidingVia( ITEM* aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo, int aNextRank );
231 SHOVE_STATUS onReverseCollidingVia( LINE& aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo );
232 SHOVE_STATUS pushOrShoveVia( VIA* aVia, const VECTOR2I& aForce, int aNextRank, bool aDontUnwindStack = false );
233
235
236 void unwindLineStack( const LINKED_ITEM* aSeg );
237 void unwindLineStack( const ITEM* aItem );
238
239 void runOptimizer( NODE* aNode );
240
241 bool pushLineStack( const LINE& aL, bool aKeepCurrentOnTop = false );
242 void popLineStack();
243
244 LINE assembleLine( const LINKED_ITEM* aSeg, int* aIndex = nullptr, bool aPreCleanup = false );
245
246 void replaceItems( ITEM* aOld, std::unique_ptr< ITEM > aNew );
247 ROOT_LINE_ENTRY* replaceLine( LINE& aOld, LINE& aNew,
248 bool aIncludeInChangedArea = true,
249 bool aAllowRedundantSegments = true,
250 NODE *aNode = nullptr );
251
252 ROOT_LINE_ENTRY* allocRootLine( std::unique_ptr<LINE> aLine,
253 int aPolicy = SHP_DEFAULT );
254 ROOT_LINE_ENTRY* findRootLine( const LINE& aLine ) const;
255 ROOT_LINE_ENTRY* findRootLine( const LINKED_ITEM *aItem ) const;
256 ROOT_LINE_ENTRY* touchRootLine( const LINE& aLine );
258 void pruneRootLines( NODE *aRemovedNode );
259
260
261 SHOVE_STATUS shoveIteration( int aIter );
263
264 int getClearance( const ITEM* aA, const ITEM* aB, CLEARANCE_ASPECT aAspect = CLEARANCE_ASPECT::ASP_SHOVE_FORWARD ) const;
265 bool fixupViaCollisions( const LINE* aCurrent, OBSTACLE& obs );
266 void sanityCheck( LINE* aOld, LINE* aNew );
267 void reconstructHeads( bool aShoveFailed );
268 void removeHeads();
269 bool preShoveCleanup( LINE* aOld, LINE* aNew );
270 const wxString formatPolicy( int aPolicy );
271
272 std::vector<SPRINGBACK_TAG> m_nodeStack;
273 std::vector<LINE> m_lineStack;
274 std::vector<LINE> m_optimizerQueue;
275 std::deque<HEAD_LINE_ENTRY> m_headLines;
277
278 // UID entries may alias the same history entry, so ownership lives outside the index.
279 std::vector<std::unique_ptr<ROOT_LINE_ENTRY>> m_rootLineHistoryEntries;
280 std::unordered_map<LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY*> m_rootLineHistory;
281
291
293
296};
297
298}
299
300#endif // __PNS_SHOVE_H
std::optional< BOX2I > OPT_BOX2I
Definition box2.h:921
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:274
void SetSpringbackDoNotTouchNode(const NODE *aNode)
SHOVE_STATUS shoveIteration(int aIter)
SHOVE_STATUS shoveMainLoop()
std::vector< SHAPE_LINE_CHAIN > HULL_SET
Definition pns_shove.h:110
void reconstructHeads(bool aShoveFailed)
std::pair< LINE, LINE > LINE_PAIR
Definition pns_shove.h:112
SHOVE_STATUS pushOrShoveVia(VIA *aVia, const VECTOR2I &aForce, int aNextRank, bool aDontUnwindStack=false)
SHOVE_STATUS onReverseCollidingVia(LINE &aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo)
COLLISION_FILTER_FUNC m_collisionFilter
Definition pns_shove.h:276
void SetCollisionFilter(COLLISION_FILTER_FUNC aFunc)
OPT_BOX2I m_affectedArea
Definition pns_shove.h:295
std::vector< LINE > m_lineStack
Definition pns_shove.h:273
void popLineStack()
std::vector< SPRINGBACK_TAG > m_nodeStack
Definition pns_shove.h:272
@ SHP_DONT_OPTIMIZE
Definition pns_shove.h:67
@ SHP_WALK_BACK
Definition pns_shove.h:65
@ SHP_DONT_LOCK_ENDPOINTS
Definition pns_shove.h:68
@ SHP_WALK_FORWARD
Definition pns_shove.h:64
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:279
std::optional< LINE > OPT_LINE
Definition pns_shove.h:111
NODE * m_currentNode
Definition pns_shove.h:283
SHOVE_STATUS Run()
LINE assembleLine(const LINKED_ITEM *aSeg, int *aIndex=nullptr, bool aPreCleanup=false)
bool checkShoveDirection(const LINE &aCurLine, const LINE &aObstacleLine, const LINE &aShovedLine) const
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:55
@ SH_HEAD_MODIFIED
Definition pns_shove.h:56
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:275
void SetClearanceOverride(CLEARANCE_OVERRIDE_FUNC aFunc)
int m_defaultPolicy
Definition pns_shove.h:294
const wxString formatPolicy(int aPolicy)
bool ShoveObstacleLine(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine)
void SetClearanceOvevride(CLEARANCE_OVERRIDE_FUNC aFunc)
SHOVE_STATUS onCollidingVia(ITEM *aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo, int aNextRank)
void sanityCheck(LINE *aOld, LINE *aNew)
int m_restrictSpringbackTagId
Definition pns_shove.h:285
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:282
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
int getClearance(const ITEM *aA, const ITEM *aB, CLEARANCE_ASPECT aAspect=CLEARANCE_ASPECT::ASP_SHOVE_FORWARD) 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()
bool m_multiLineMode
Definition pns_shove.h:290
CLEARANCE_OVERRIDE_FUNC m_overrideClearance
Definition pns_shove.h:289
std::vector< LINE > m_optimizerQueue
Definition pns_shove.h:274
SHOVE_STATUS onCollidingSolid(LINE &aCurrent, ITEM *aObstacle, OBSTACLE &aObstacleInfo)
bool pushSpringback(NODE *aNode, const OPT_BOX2I &aAffectedArea)
int m_optFlagDisableMask
Definition pns_shove.h:292
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:286
bool m_headsModified
Definition pns_shove.h:288
const NODE * m_springbackDoNotTouchNode
Definition pns_shove.h:284
void ClearHeads()
std::unordered_map< LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY * > m_rootLineHistory
Definition pns_shove.h:280
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:113
void removeHeads()
Push and Shove diff pair dimensions (gap) settings dialog.
std::function< int(const ITEM *, const ITEM *, CLEARANCE_ASPECT)> CLEARANCE_OVERRIDE_FUNC
Definition pns_node.h:123
CLEARANCE_ASPECT
Definition pns_node.h:69
@ SHP_DEFAULT
std::function< bool(const ITEM *, const ITEM *)> COLLISION_FILTER_FUNC
Definition pns_node.h:122
STL namespace.
Hold an object colliding with another object, along with some useful data about the collision.
Definition pns_node.h:98
HEAD_LINE_ENTRY & operator=(const HEAD_LINE_ENTRY &aOther) noexcept
Definition pns_shove.h:150
std::optional< VIA_HANDLE > theVia
Definition pns_shove.h:183
HEAD_LINE_ENTRY(const HEAD_LINE_ENTRY &aOther)
Definition pns_shove.h:144
HEAD_LINE_ENTRY(const LINE &aOrig, int aPolicy=SHP_DEFAULT)
Definition pns_shove.h:132
int policy
Definition pns_shove.h:188
HEAD_LINE_ENTRY & operator=(HEAD_LINE_ENTRY &&aOther) noexcept
Definition pns_shove.h:164
VIA * draggedVia
Definition pns_shove.h:184
std::optional< VIA_HANDLE > prevVia
Definition pns_shove.h:182
bool geometryModified
Definition pns_shove.h:181
std::optional< LINE > newHead
Definition pns_shove.h:187
HEAD_LINE_ENTRY(VIA_HANDLE aVia, int aPolicy=SHP_DEFAULT)
Definition pns_shove.h:139
std::optional< LINE > origHead
Definition pns_shove.h:186
VECTOR2I viaNewPos
Definition pns_shove.h:185
Definition pns_shove.h:116
std::unique_ptr< LINE > rootLine
Definition pns_shove.h:122
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
ROOT_LINE_ENTRY(std::unique_ptr< LINE > aLine={}, int aPolicy=SHP_DEFAULT)
Definition pns_shove.h:117
VIA * newVia
Definition pns_shove.h:124
std::vector< VIA_HANDLE > m_draggedVias
Definition pns_shove.h:201
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708