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
92
93 void ForceClearance ( bool aEnabled, int aClearance )
94 {
95 if( aEnabled )
96 m_forceClearance = aClearance;
97 else
99 }
100
101 NODE* CurrentNode();
102
103 bool HeadsModified( int aIndex = -1 ) const;
104 const PNS::LINE GetModifiedHead( int aIndex ) const;
105 const VIA_HANDLE GetModifiedHeadVia( int aIndex ) const;
106
107 bool AddLockedSpringbackNode( NODE* aNode );
108 void UnlockSpringbackNode( NODE* aNode );
109 bool RewindSpringbackTo( NODE* aNode );
111 void DisablePostShoveOptimizations( int aMask );
112 void SetSpringbackDoNotTouchNode( const NODE *aNode );
113
114private:
115 typedef std::vector<SHAPE_LINE_CHAIN> HULL_SET;
116 typedef std::optional<LINE> OPT_LINE;
117 typedef std::pair<LINE, LINE> LINE_PAIR;
118 typedef std::vector<LINE_PAIR> LINE_PAIR_VEC;
119
121 {
122 ROOT_LINE_ENTRY( std::unique_ptr<LINE> aLine = {}, int aPolicy = SHP_DEFAULT ) :
123 rootLine( std::move( aLine ) ),
124 policy( aPolicy )
125 {}
126
127 std::unique_ptr<LINE> rootLine;
128 VIA* oldVia = nullptr;
129 VIA* newVia = nullptr;
130 std::optional<LINE> newLine;
132 bool isHead = false;
133 };
134
136 {
137 HEAD_LINE_ENTRY( const LINE& aOrig, int aPolicy = SHP_DEFAULT ) :
138 origHead( aOrig ),
139 policy( aPolicy )
140 {
141 origHead->ClearLinks();
142 };
143
144 HEAD_LINE_ENTRY( VIA_HANDLE aVia, int aPolicy = SHP_DEFAULT ) :
145 theVia( aVia ),
146 policy( aPolicy )
147 {};
148
150 {
151 *this = aOther;
152 }
153
154 // Copy operator
155 HEAD_LINE_ENTRY& operator=( const HEAD_LINE_ENTRY& aOther ) noexcept
156 {
157 geometryModified = aOther.geometryModified;
158 prevVia = aOther.prevVia;
159 theVia = aOther.theVia;
160 draggedVia = aOther.draggedVia;
161 viaNewPos = aOther.viaNewPos;
162 origHead = aOther.origHead;
163 newHead = aOther.newHead;
164 policy = aOther.policy;
165 return *this;
166 }
167
168 // Move assignment operator
170 {
171 if (this != &aOther)
172 {
173 geometryModified = aOther.geometryModified;
174 prevVia = aOther.prevVia;
175 theVia = aOther.theVia;
176 draggedVia = aOther.draggedVia;
177 viaNewPos = aOther.viaNewPos;
178 origHead = std::move( aOther.origHead );
179 newHead = std::move( aOther.newHead );
180 policy = aOther.policy;
181 }
182
183 return *this;
184 }
185
186 bool geometryModified = false;
187 std::optional<VIA_HANDLE> prevVia;
188 std::optional<VIA_HANDLE> theVia;
189 VIA* draggedVia = nullptr;
191 std::optional<LINE> origHead;
192 std::optional<LINE> newHead;
194 };
195
197 {
199 m_length( 0 ),
200 m_node( nullptr ),
201 m_seq( 0 ),
202 m_locked( false )
203 {}
204
205 int64_t m_length;
206 std::vector<VIA_HANDLE> m_draggedVias;
210 int m_seq;
212 };
213
214 bool pruneLineFromOptimizerQueue( const LINE& aLine );
215
216 bool shoveLineToHullSet( const LINE& aCurLine, const LINE& aObstacleLine, LINE& aResultLine,
217 const HULL_SET& aHulls, bool aPermitAdjustingStart = false,
218 bool aPermitAdjustingEnd = false );
219
220 NODE* reduceSpringback( const ITEM_SET& aHeadSet );
221
222 bool patchTadpoleVia( ITEM* nearest, LINE& current );
223
224 bool pushSpringback( NODE* aNode, const OPT_BOX2I& aAffectedArea );
225
226 bool shoveLineFromLoneVia( const LINE& aCurLine, const LINE& aObstacleLine,
227 LINE& aResultLine );
228 bool checkShoveDirection( const LINE& aCurLine, const LINE& aObstacleLine,
229 const LINE& aShovedLine ) const;
230
231 SHOVE_STATUS onCollidingArc( LINE& aCurrent, ARC* aObstacleArc );
232 SHOVE_STATUS onCollidingLine( LINE& aCurrent, LINE& aObstacle, int aNextRank );
233 SHOVE_STATUS onCollidingSegment( LINE& aCurrent, SEGMENT* aObstacleSeg );
234 SHOVE_STATUS onCollidingSolid( LINE& aCurrent, ITEM* aObstacle, OBSTACLE& aObstacleInfo );
235 SHOVE_STATUS onCollidingVia( ITEM* aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo, int aNextRank );
236 SHOVE_STATUS onReverseCollidingVia( LINE& aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo );
237 SHOVE_STATUS pushOrShoveVia( VIA* aVia, const VECTOR2I& aForce, int aNextRank, bool aDontUnwindStack = false );
238
240
241 void unwindLineStack( const LINKED_ITEM* aSeg );
242 void unwindLineStack( const ITEM* aItem );
243
244 void runOptimizer( NODE* aNode );
245
246 bool pushLineStack( const LINE& aL, bool aKeepCurrentOnTop = false );
247 void popLineStack();
248
249 LINE assembleLine( const LINKED_ITEM* aSeg, int* aIndex = nullptr, bool aPreCleanup = false );
250
251 void replaceItems( ITEM* aOld, std::unique_ptr< ITEM > aNew );
252 ROOT_LINE_ENTRY* replaceLine( LINE& aOld, LINE& aNew,
253 bool aIncludeInChangedArea = true,
254 bool aAllowRedundantSegments = true,
255 NODE *aNode = nullptr );
256
257 ROOT_LINE_ENTRY* allocRootLine( std::unique_ptr<LINE> aLine,
258 int aPolicy = SHP_DEFAULT );
259 ROOT_LINE_ENTRY* findRootLine( const LINE& aLine ) const;
260 ROOT_LINE_ENTRY* findRootLine( const LINKED_ITEM *aItem ) const;
261 ROOT_LINE_ENTRY* touchRootLine( const LINE& aLine );
263 void pruneRootLines( NODE *aRemovedNode );
264
265
266 SHOVE_STATUS shoveIteration( int aIter );
268
269 int getClearance( const ITEM* aA, const ITEM* aB ) const;
270 bool fixupViaCollisions( const LINE* aCurrent, OBSTACLE& obs );
271 void sanityCheck( LINE* aOld, LINE* aNew );
272 void reconstructHeads( bool aShoveFailed );
273 void removeHeads();
274 bool preShoveCleanup( LINE* aOld, LINE* aNew );
275 const wxString formatPolicy( int aPolicy );
276
277 std::vector<SPRINGBACK_TAG> m_nodeStack;
278 std::vector<LINE> m_lineStack;
279 std::vector<LINE> m_optimizerQueue;
280 std::deque<HEAD_LINE_ENTRY> m_headLines;
282
283 // UID entries may alias the same history entry, so ownership lives outside the index.
284 std::vector<std::unique_ptr<ROOT_LINE_ENTRY>> m_rootLineHistoryEntries;
285 std::unordered_map<LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY*> m_rootLineHistory;
286
296
298
301
302};
303
304}
305
306#endif // __PNS_SHOVE_H
std::optional< BOX2I > OPT_BOX2I
Definition box2.h:931
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:243
void SetSpringbackDoNotTouchNode(const NODE *aNode)
SHOVE_STATUS shoveIteration(int aIter)
SHOVE_STATUS shoveMainLoop()
std::vector< SHAPE_LINE_CHAIN > HULL_SET
Definition pns_shove.h:115
void reconstructHeads(bool aShoveFailed)
std::pair< LINE, LINE > LINE_PAIR
Definition pns_shove.h:117
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:281
void SetCollisionFilter(COLLISION_FILTER_FUNC aFunc)
OPT_BOX2I m_affectedArea
Definition pns_shove.h:300
std::vector< LINE > m_lineStack
Definition pns_shove.h:278
void popLineStack()
std::vector< SPRINGBACK_TAG > m_nodeStack
Definition pns_shove.h:277
@ 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:284
std::optional< LINE > OPT_LINE
Definition pns_shove.h:116
NODE * m_currentNode
Definition pns_shove.h:288
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:93
bool checkShoveDirection(const LINE &aCurLine, const LINE &aObstacleLine, const LINE &aShovedLine) const
int m_forceClearance
Definition pns_shove.h:294
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:280
int m_defaultPolicy
Definition pns_shove.h:299
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:290
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:287
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:295
std::vector< LINE > m_optimizerQueue
Definition pns_shove.h:279
SHOVE_STATUS onCollidingSolid(LINE &aCurrent, ITEM *aObstacle, OBSTACLE &aObstacleInfo)
bool pushSpringback(NODE *aNode, const OPT_BOX2I &aAffectedArea)
int m_optFlagDisableMask
Definition pns_shove.h:297
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:291
bool m_headsModified
Definition pns_shove.h:293
const NODE * m_springbackDoNotTouchNode
Definition pns_shove.h:289
void ClearHeads()
std::unordered_map< LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY * > m_rootLineHistory
Definition pns_shove.h:285
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:118
void removeHeads()
Push and Shove diff pair dimensions (gap) settings dialog.
std::function< bool(const ITEM *aTestItem, const ITEM *aRefItem)> COLLISION_FILTER_FUNC
Definition pns_node.h:113
@ 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:155
std::optional< VIA_HANDLE > theVia
Definition pns_shove.h:188
HEAD_LINE_ENTRY(const HEAD_LINE_ENTRY &aOther)
Definition pns_shove.h:149
HEAD_LINE_ENTRY(const LINE &aOrig, int aPolicy=SHP_DEFAULT)
Definition pns_shove.h:137
int policy
Definition pns_shove.h:193
HEAD_LINE_ENTRY & operator=(HEAD_LINE_ENTRY &&aOther) noexcept
Definition pns_shove.h:169
VIA * draggedVia
Definition pns_shove.h:189
std::optional< VIA_HANDLE > prevVia
Definition pns_shove.h:187
bool geometryModified
Definition pns_shove.h:186
std::optional< LINE > newHead
Definition pns_shove.h:192
HEAD_LINE_ENTRY(VIA_HANDLE aVia, int aPolicy=SHP_DEFAULT)
Definition pns_shove.h:144
std::optional< LINE > origHead
Definition pns_shove.h:191
VECTOR2I viaNewPos
Definition pns_shove.h:190
Definition pns_shove.h:121
std::unique_ptr< LINE > rootLine
Definition pns_shove.h:127
int policy
Definition pns_shove.h:131
bool isHead
Definition pns_shove.h:132
std::optional< LINE > newLine
Definition pns_shove.h:130
VIA * oldVia
Definition pns_shove.h:128
ROOT_LINE_ENTRY(std::unique_ptr< LINE > aLine={}, int aPolicy=SHP_DEFAULT)
Definition pns_shove.h:122
VIA * newVia
Definition pns_shove.h:129
std::vector< VIA_HANDLE > m_draggedVias
Definition pns_shove.h:206
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683