KiCad PCB EDA Suite
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <tomasz.wlostowski@cern.ch>
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 pruneLineFromOptimizerQueue( const LINE& aLine );
172
173 bool shoveLineToHullSet( const LINE& aCurLine, const LINE& aObstacleLine, LINE& aResultLine,
174 const HULL_SET& aHulls, bool aPermitAdjustingStart = false,
175 bool aPermitAdjustingEnd = false );
176
177 NODE* reduceSpringback( const ITEM_SET& aHeadSet );
178
179 bool patchTadpoleVia( ITEM* nearest, LINE& current );
180
181 bool pushSpringback( NODE* aNode, const OPT_BOX2I& aAffectedArea );
182
183 bool shoveLineFromLoneVia( const LINE& aCurLine, const LINE& aObstacleLine,
184 LINE& aResultLine );
185 bool checkShoveDirection( const LINE& aCurLine, const LINE& aObstacleLine,
186 const LINE& aShovedLine ) const;
187
188 SHOVE_STATUS onCollidingArc( LINE& aCurrent, ARC* aObstacleArc );
189 SHOVE_STATUS onCollidingLine( LINE& aCurrent, LINE& aObstacle, int aNextRank );
190 SHOVE_STATUS onCollidingSegment( LINE& aCurrent, SEGMENT* aObstacleSeg );
191 SHOVE_STATUS onCollidingSolid( LINE& aCurrent, ITEM* aObstacle, OBSTACLE& aObstacleInfo );
192 SHOVE_STATUS onCollidingVia( ITEM* aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo, int aNextRank );
193 SHOVE_STATUS onReverseCollidingVia( LINE& aCurrent, VIA* aObstacleVia, OBSTACLE& aObstacleInfo );
194 SHOVE_STATUS pushOrShoveVia( VIA* aVia, const VECTOR2I& aForce, int aNextRank, bool aDontUnwindStack = false );
195
197
198 void unwindLineStack( const LINKED_ITEM* aSeg );
199 void unwindLineStack( const ITEM* aItem );
200
201 void runOptimizer( NODE* aNode );
202
203 bool pushLineStack( const LINE& aL, bool aKeepCurrentOnTop = false );
204 void popLineStack();
205
206 LINE assembleLine( const LINKED_ITEM* aSeg, int* aIndex = nullptr, bool aPreCleanup = false );
207
208 void replaceItems( ITEM* aOld, std::unique_ptr< ITEM > aNew );
209 ROOT_LINE_ENTRY* replaceLine( LINE& aOld, LINE& aNew,
210 bool aIncludeInChangedArea = true,
211 bool aAllowRedundantSegments = true,
212 NODE *aNode = nullptr );
213
214 ROOT_LINE_ENTRY* findRootLine( const LINE& aLine );
216 ROOT_LINE_ENTRY* touchRootLine( const LINE& aLine );
218 void pruneRootLines( NODE *aRemovedNode );
219
220
221 SHOVE_STATUS shoveIteration( int aIter );
223
224 int getClearance( const ITEM* aA, const ITEM* aB ) const;
225 bool fixupViaCollisions( const LINE* aCurrent, OBSTACLE& obs );
226 void sanityCheck( LINE* aOld, LINE* aNew );
227 void reconstructHeads( bool aShoveFailed );
228 void removeHeads();
229 bool preShoveCleanup( LINE* aOld, LINE* aNew );
230 const wxString formatPolicy( int aPolicy );
231
232 std::vector<SPRINGBACK_TAG> m_nodeStack;
233 std::vector<LINE> m_lineStack;
234 std::vector<LINE> m_optimizerQueue;
235 std::deque<HEAD_LINE_ENTRY> m_headLines;
236
237 std::unordered_map<LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY*> m_rootLineHistory;
238
248
250
253
254};
255
256}
257
258#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: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:231
The actual Push and Shove algorithm.
Definition: pns_shove.h:46
void SetSpringbackDoNotTouchNode(const NODE *aNode)
Definition: pns_shove.cpp:2215
SHOVE_STATUS shoveIteration(int aIter)
Definition: pns_shove.cpp:1632
SHOVE_STATUS shoveMainLoop()
Definition: pns_shove.cpp:1879
std::vector< SHAPE_LINE_CHAIN > HULL_SET
Definition: pns_shove.h:110
void reconstructHeads(bool aShoveFailed)
Definition: pns_shove.cpp:2289
int m_iter
Definition: pns_shove.h:244
std::pair< LINE, LINE > LINE_PAIR
Definition: pns_shove.h:112
SHOVE_STATUS pushOrShoveVia(VIA *aVia, const VECTOR2I &aForce, int aNextRank, bool aDontUnwindStack=false)
Definition: pns_shove.cpp:1035
SHOVE_STATUS onReverseCollidingVia(LINE &aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo)
Definition: pns_shove.cpp:1262
OPT_BOX2I m_affectedArea
Definition: pns_shove.h:252
std::vector< LINE > m_lineStack
Definition: pns_shove.h:233
void popLineStack()
Definition: pns_shove.cpp:1508
ROOT_LINE_ENTRY * findRootLine(const LINE &aLine)
Definition: pns_shove.cpp:1941
std::vector< SPRINGBACK_TAG > m_nodeStack
Definition: pns_shove.h:232
@ 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
bool patchTadpoleVia(ITEM *nearest, LINE &current)
Definition: pns_shove.cpp:1598
SHOVE_STATUS onCollidingArc(LINE &aCurrent, ARC *aObstacleArc)
Definition: pns_shove.cpp:681
bool shoveLineToHullSet(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine, const HULL_SET &aHulls, bool aPermitAdjustingStart=false, bool aPermitAdjustingEnd=false)
Definition: pns_shove.cpp:309
std::optional< LINE > OPT_LINE
Definition: pns_shove.h:111
NODE * m_currentNode
Definition: pns_shove.h:240
SHOVE_STATUS Run()
Definition: pns_shove.cpp:2390
LINE assembleLine(const LINKED_ITEM *aSeg, int *aIndex=nullptr, bool aPreCleanup=false)
Definition: pns_shove.cpp:218
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:242
int m_forceClearance
Definition: pns_shove.h:246
SHOVE_STATUS onCollidingSegment(LINE &aCurrent, SEGMENT *aObstacleSeg)
Definition: pns_shove.cpp:619
void DisablePostShoveOptimizations(int aMask)
Definition: pns_shove.cpp:2209
bool RewindSpringbackTo(NODE *aNode)
Definition: pns_shove.cpp:2144
bool pushLineStack(const LINE &aL, bool aKeepCurrentOnTop=false)
Definition: pns_shove.cpp:1454
@ 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:1925
bool RewindToLastLockedNode()
Definition: pns_shove.cpp:2178
SHOVE_STATUS ShoveDraggingVia(const VIA_HANDLE aOldVia, const VECTOR2I &aWhere, VIA_HANDLE &aNewVia)
std::deque< HEAD_LINE_ENTRY > m_headLines
Definition: pns_shove.h:235
int m_defaultPolicy
Definition: pns_shove.h:251
const wxString formatPolicy(int aPolicy)
Definition: pns_shove.cpp:2620
bool ShoveObstacleLine(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine)
Definition: pns_shove.cpp:501
SHOVE_STATUS onCollidingVia(ITEM *aCurrent, VIA *aObstacleVia, OBSTACLE &aObstacleInfo, int aNextRank)
Definition: pns_shove.cpp:1169
void sanityCheck(LINE *aOld, LINE *aNew)
Definition: pns_shove.cpp:186
int m_restrictSpringbackTagId
Definition: pns_shove.h:242
void runOptimizer(NODE *aNode)
Definition: pns_shove.cpp:2012
ROOT_LINE_ENTRY * touchRootLine(const LINE &aLine)
Definition: pns_shove.cpp:1965
const PNS::LINE GetModifiedHead(int aIndex) const
Definition: pns_shove.cpp:2649
bool HeadsModified(int aIndex=-1) const
Definition: pns_shove.cpp:2641
void UnlockSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:2192
NODE * m_root
Definition: pns_shove.h:239
NODE * reduceSpringback(const ITEM_SET &aHeadSet)
Definition: pns_shove.cpp:916
bool AddLockedSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:2130
void SetDefaultShovePolicy(int aPolicy)
Definition: pns_shove.cpp:2221
void AddHeads(const LINE &aHead, int aPolicy=SHP_DEFAULT)
Definition: pns_shove.cpp:2246
void SetShovePolicy(const LINKED_ITEM *aItem, int aPolicy)
Definition: pns_shove.cpp:2227
void unwindLineStack(const LINKED_ITEM *aSeg)
Definition: pns_shove.cpp:1384
bool preShoveCleanup(LINE *aOld, LINE *aNew)
Definition: pns_shove.cpp:2361
NODE * CurrentNode()
Definition: pns_shove.cpp:2124
int getClearance(const ITEM *aA, const ITEM *aB) const
Definition: pns_shove.cpp:169
bool m_multiLineMode
Definition: pns_shove.h:247
std::vector< LINE > m_optimizerQueue
Definition: pns_shove.h:234
SHOVE_STATUS onCollidingSolid(LINE &aCurrent, ITEM *aObstacle, OBSTACLE &aObstacleInfo)
Definition: pns_shove.cpp:768
bool pushSpringback(NODE *aNode, const OPT_BOX2I &aAffectedArea)
Definition: pns_shove.cpp:977
int m_optFlagDisableMask
Definition: pns_shove.h:249
SHOVE_STATUS onCollidingLine(LINE &aCurrent, LINE &aObstacle, int aNextRank)
Definition: pns_shove.cpp:730
bool fixupViaCollisions(const LINE *aCurrent, OBSTACLE &obs)
Definition: pns_shove.cpp:1516
bool pruneLineFromOptimizerQueue(const LINE &aLine)
Definition: pns_shove.cpp:1480
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:262
VIA * m_draggedVia
Definition: pns_shove.h:243
bool m_headsModified
Definition: pns_shove.h:245
const NODE * m_springbackDoNotTouchNode
Definition: pns_shove.h:241
void ClearHeads()
Definition: pns_shove.cpp:2240
std::unordered_map< LINKED_ITEM::UNIQ_ID, ROOT_LINE_ENTRY * > m_rootLineHistory
Definition: pns_shove.h:237
void pruneRootLines(NODE *aRemovedNode)
Definition: pns_shove.cpp:893
const VIA_HANDLE GetModifiedHeadVia(int aIndex) const
Definition: pns_shove.cpp:2654
ROOT_LINE_ENTRY * replaceLine(LINE &aOld, LINE &aNew, bool aIncludeInChangedArea=true, bool aAllowRedundantSegments=true, NODE *aNode=nullptr)
Definition: pns_shove.cpp:80
std::vector< LINE_PAIR > LINE_PAIR_VEC
Definition: pns_shove.h:113
void removeHeads()
Definition: pns_shove.cpp:2270
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