KiCad PCB EDA Suite
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"
31 #include "pns_routing_settings.h"
32 #include "pns_algo_base.h"
33 #include "pns_logger.h"
34 #include "range.h"
35 
36 namespace PNS {
37 
38 class LINE;
39 class NODE;
40 class ROUTER;
41 
45 class SHOVE : public ALGO_BASE
46 {
47 public:
48 
50  {
51  SH_OK = 0,
56  };
57 
58  SHOVE( NODE* aWorld, ROUTER* aRouter );
59  ~SHOVE();
60 
61  virtual LOGGER* Logger() override
62  {
63  return &m_logger;
64  }
65 
66  SHOVE_STATUS ShoveLines( const LINE& aCurrentHead );
67  SHOVE_STATUS ShoveMultiLines( const ITEM_SET& aHeadSet );
68 
69  SHOVE_STATUS ShoveDraggingVia( const VIA_HANDLE aOldVia, const VECTOR2I& aWhere,
70  VIA_HANDLE& aNewVia );
71  SHOVE_STATUS ShoveObstacleLine( const LINE& aCurLine, const LINE& aObstacleLine,
72  LINE& aResultLine );
73 
74  void ForceClearance ( bool aEnabled, int aClearance )
75  {
76  if( aEnabled )
77  m_forceClearance = aClearance;
78  else
79  m_forceClearance = -1;
80  }
81 
82  NODE* CurrentNode();
83 
84  const LINE NewHead() const;
85 
86  void SetInitialLine( LINE& aInitial );
87 
88  bool AddLockedSpringbackNode( NODE* aNode );
89  void UnlockSpringbackNode( NODE* aNode );
90  bool RewindSpringbackTo( NODE* aNode );
91 
92 private:
93  typedef std::vector<SHAPE_LINE_CHAIN> HULL_SET;
95  typedef std::pair<LINE, LINE> LINE_PAIR;
96  typedef std::vector<LINE_PAIR> LINE_PAIR_VEC;
97 
99  {
101  m_length( 0 ),
102  m_draggedVia(),
103  m_node( nullptr ),
104  m_seq( 0 ),
105  m_locked( false )
106  {}
107 
108  int64_t m_length;
113  int m_seq;
114  bool m_locked;
115  };
116 
117  SHOVE_STATUS shoveLineToHullSet( const LINE& aCurLine, const LINE& aObstacleLine,
118  LINE& aResultLine, const HULL_SET& aHulls );
119 
120  NODE* reduceSpringback( const ITEM_SET& aHeadSet, VIA_HANDLE& aDraggedVia );
121 
122  bool pushSpringback( NODE* aNode, const OPT_BOX2I& aAffectedArea, VIA* aDraggedVia );
123 
124  SHOVE_STATUS shoveLineFromLoneVia( const LINE& aCurLine, const LINE& aObstacleLine,
125  LINE& aResultLine );
126  bool checkShoveDirection( const LINE& aCurLine, const LINE& aObstacleLine,
127  const LINE& aShovedLine ) const;
128 
129  SHOVE_STATUS onCollidingArc( LINE& aCurrent, ARC* aObstacleArc );
130  SHOVE_STATUS onCollidingLine( LINE& aCurrent, LINE& aObstacle );
131  SHOVE_STATUS onCollidingSegment( LINE& aCurrent, SEGMENT* aObstacleSeg );
132  SHOVE_STATUS onCollidingSolid( LINE& aCurrent, ITEM* aObstacle );
133  SHOVE_STATUS onCollidingVia( ITEM* aCurrent, VIA* aObstacleVia );
134  SHOVE_STATUS onReverseCollidingVia( LINE& aCurrent, VIA* aObstacleVia );
135  SHOVE_STATUS pushOrShoveVia( VIA* aVia, const VECTOR2I& aForce, int aCurrentRank );
136 
138 
139  void unwindLineStack( LINKED_ITEM* aSeg );
140  void unwindLineStack( ITEM* aItem );
141 
142  void runOptimizer( NODE* aNode );
143 
144  bool pushLineStack( const LINE& aL, bool aKeepCurrentOnTop = false );
145  void popLineStack();
146 
147  LINE assembleLine( const LINKED_ITEM* aSeg, int* aIndex = nullptr );
148 
149  void replaceItems( ITEM* aOld, std::unique_ptr< ITEM > aNew );
150  void replaceLine( LINE& aOld, LINE& aNew, bool aIncludeInChangedArea = true,
151  NODE *aNode = nullptr );
152 
153  LINE* findRootLine( LINE *aLine );
154 
156 
157  SHOVE_STATUS shoveIteration( int aIter );
159 
160  int getClearance( const ITEM* aA, const ITEM* aB ) const;
161  int getHoleClearance( const ITEM* aA, const ITEM* aB ) const;
162 
163  void sanityCheck( LINE* aOld, LINE* aNew );
164 
165  std::vector<SPRINGBACK_TAG> m_nodeStack;
166  std::vector<LINE> m_lineStack;
167  std::vector<LINE> m_optimizerQueue;
168  std::unordered_map<const LINKED_ITEM*, LINE*> m_rootLineHistory;
169 
173 
175 
178 
179  int m_iter;
182 };
183 
184 }
185 
186 #endif // __PNS_SHOVE_H
LINE * findRootLine(LINE *aLine)
Definition: pns_shove.cpp:1679
bool pushLineStack(const LINE &aL, bool aKeepCurrentOnTop=false)
Definition: pns_shove.cpp:1147
Base class for PNS router board items.
Definition: pns_item.h:55
void popLineStack()
Definition: pns_shove.cpp:1167
int m_restrictSpringbackTagId
Definition: pns_shove.h:172
Keep the router "world" - i.e.
Definition: pns_node.h:144
SHOVE_STATUS onCollidingSolid(LINE &aCurrent, ITEM *aObstacle)
Definition: pns_shove.cpp:649
void unwindLineStack(LINKED_ITEM *aSeg)
Definition: pns_shove.cpp:1113
OPT< LINE > OPT_LINE
Definition: pns_shove.h:94
The actual Push and Shove algorithm.
Definition: pns_shove.h:45
const LINE NewHead() const
Definition: pns_shove.cpp:1786
SHOVE_STATUS shoveLineToHullSet(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine, const HULL_SET &aHulls)
Definition: pns_shove.cpp:271
SHOVE_STATUS onCollidingArc(LINE &aCurrent, ARC *aObstacleArc)
Definition: pns_shove.cpp:545
Base class for all P&S algorithms (shoving, walkaround, line placement, dragging, etc....
Definition: pns_algo_base.h:42
std::vector< LINE > m_optimizerQueue
Definition: pns_shove.h:167
bool RewindSpringbackTo(NODE *aNode)
Definition: pns_shove.cpp:1812
NODE * m_root
Definition: pns_shove.h:170
std::pair< LINE, LINE > LINE_PAIR
Definition: pns_shove.h:95
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:60
SHOVE_STATUS ShoveObstacleLine(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine)
Definition: pns_shove.cpp:393
SHOVE_STATUS pushOrShoveVia(VIA *aVia, const VECTOR2I &aForce, int aCurrentRank)
Definition: pns_shove.cpp:847
std::vector< LINE_PAIR > LINE_PAIR_VEC
Definition: pns_shove.h:96
SHOVE_STATUS onCollidingLine(LINE &aCurrent, LINE &aObstacle)
Definition: pns_shove.cpp:608
SHOVE_STATUS onReverseCollidingVia(LINE &aCurrent, VIA *aObstacleVia)
Definition: pns_shove.cpp:1030
std::vector< SHAPE_LINE_CHAIN > HULL_SET
Definition: pns_shove.h:93
OPT_BOX2I totalAffectedArea() const
Definition: pns_shove.cpp:1386
NODE * reduceSpringback(const ITEM_SET &aHeadSet, VIA_HANDLE &aDraggedVia)
Definition: pns_shove.cpp:777
SHOVE_STATUS ShoveLines(const LINE &aCurrentHead)
Definition: pns_shove.cpp:1402
bool AddLockedSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:1801
SHOVE_STATUS onCollidingVia(ITEM *aCurrent, VIA *aObstacleVia)
Definition: pns_shove.cpp:970
void runOptimizer(NODE *aNode)
Definition: pns_shove.cpp:1696
void sanityCheck(LINE *aOld, LINE *aNew)
Definition: pns_shove.cpp:152
SHOVE_STATUS shoveIteration(int aIter)
Definition: pns_shove.cpp:1196
SHOVE(NODE *aWorld, ROUTER *aRouter)
Definition: pns_shove.cpp:159
std::unordered_map< const LINKED_ITEM *, LINE * > m_rootLineHistory
Definition: pns_shove.h:168
bool pushSpringback(NODE *aNode, const OPT_BOX2I &aAffectedArea, VIA *aDraggedVia)
Definition: pns_shove.cpp:807
SHOVE_STATUS onCollidingSegment(LINE &aCurrent, SEGMENT *aObstacleSeg)
Definition: pns_shove.cpp:475
void UnlockSpringbackNode(NODE *aNode)
Definition: pns_shove.cpp:1841
void replaceLine(LINE &aOld, LINE &aNew, bool aIncludeInChangedArea=true, NODE *aNode=nullptr)
Definition: pns_shove.cpp:62
SHOVE_STATUS ShoveDraggingVia(const VIA_HANDLE aOldVia, const VECTOR2I &aWhere, VIA_HANDLE &aNewVia)
Definition: pns_shove.cpp:1611
std::vector< SPRINGBACK_TAG > m_nodeStack
Definition: pns_shove.h:165
VIA * m_draggedVia
Definition: pns_shove.h:177
OPT_BOX2I m_affectedArea
Definition: pns_shove.h:155
void ForceClearance(bool aEnabled, int aClearance)
Definition: pns_shove.h:74
virtual LOGGER * Logger() override
Definition: pns_shove.h:61
bool checkShoveDirection(const LINE &aCurLine, const LINE &aObstacleLine, const LINE &aShovedLine) const
Definition: pns_shove.cpp:206
SHOVE_STATUS shoveMainLoop()
Definition: pns_shove.cpp:1342
OPT< BOX2I > OPT_BOX2I
Definition: box2.h:509
void SetInitialLine(LINE &aInitial)
Definition: pns_shove.cpp:1794
boost::optional< T > OPT
Definition: optional.h:7
int getClearance(const ITEM *aA, const ITEM *aB) const
Definition: pns_shove.cpp:134
int getHoleClearance(const ITEM *aA, const ITEM *aB) const
Definition: pns_shove.cpp:143
void replaceItems(ITEM *aOld, std::unique_ptr< ITEM > aNew)
Definition: pns_shove.cpp:51
bool m_multiLineMode
Definition: pns_shove.h:181
OPT_LINE m_newHead
Definition: pns_shove.h:174
std::vector< LINE > m_lineStack
Definition: pns_shove.h:166
SHOVE_STATUS ShoveMultiLines(const ITEM_SET &aHeadSet)
Definition: pns_shove.cpp:1509
NODE * m_currentNode
Definition: pns_shove.h:171
Push and Shove diff pair dimensions (gap) settings dialog.
LOGGER m_logger
Definition: pns_shove.h:176
int m_iter
Definition: pns_shove.h:179
SHOVE_STATUS shoveLineFromLoneVia(const LINE &aCurLine, const LINE &aObstacleLine, LINE &aResultLine)
Definition: pns_shove.cpp:226
NODE * CurrentNode()
Definition: pns_shove.cpp:1780
int m_forceClearance
Definition: pns_shove.h:180
LINE assembleLine(const LINKED_ITEM *aSeg, int *aIndex=nullptr)
Definition: pns_shove.cpp:192