KiCad PCB EDA Suite
pns_optimizer.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  *
7  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
8  *
9  * This program is free software: you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License as published by the
11  * Free Software Foundation, either version 3 of the License, or (at your
12  * option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #ifndef __PNS_OPTIMIZER_H
24 #define __PNS_OPTIMIZER_H
25 
26 #include <unordered_map>
27 #include <memory>
28 
31 
32 #include "range.h"
33 
34 
35 namespace PNS {
36 
37 class NODE;
38 class ROUTER;
39 class LINE;
40 class DIFF_PAIR;
41 class ITEM;
42 class JOINT;
43 class OPT_CONSTRAINT;
44 
49 {
50 public:
52  m_lengthCost( 0 ),
53  m_cornerCost( 0 )
54  {}
55 
59  {}
60 
62 
63  static int CornerCost( const SEG& aA, const SEG& aB );
64  static int CornerCost( const SHAPE_LINE_CHAIN& aLine );
65  static int CornerCost( const LINE& aLine );
66 
67  void Add( const LINE& aLine );
68  void Remove( const LINE& aLine );
69  void Replace( const LINE& aOldLine, const LINE& aNewLine );
70 
71  bool IsBetter( const COST_ESTIMATOR& aOther, double aLengthTolerance,
72  double aCornerTollerace ) const;
73 
74  double GetLengthCost() const { return m_lengthCost; }
75  double GetCornerCost() const { return m_cornerCost; }
76 
77 private:
78  double m_lengthCost;
80 };
81 
82 
94 class OPTIMIZER
95 {
96 public:
98  {
99  MERGE_SEGMENTS = 0x01,
100  SMART_PADS = 0x02,
101  MERGE_OBTUSE = 0x04,
102  FANOUT_CLEANUP = 0x08,
106  MERGE_COLINEAR = 0x80,
107  RESTRICT_AREA = 0x100,
109  };
111 
112  OPTIMIZER( NODE* aWorld );
113  ~OPTIMIZER();
114 
116  static bool Optimize( LINE* aLine, int aEffortLevel, NODE* aWorld,
117  const VECTOR2I& aV = VECTOR2I(0, 0) );
118 
119  bool Optimize( LINE* aLine, LINE* aResult = nullptr, LINE* aRoot = nullptr );
120  bool Optimize( DIFF_PAIR* aPair );
121 
122 
123  void SetWorld( NODE* aNode ) { m_world = aNode; }
124  void CacheRemove( ITEM* aItem );
125  void ClearCache( bool aStaticOnly = false );
126 
127  void SetCollisionMask( int aMask )
128  {
129  m_collisionKindMask = aMask;
130  }
131 
132  void SetEffortLevel( int aEffort )
133  {
134  m_effortLevel = aEffort;
135  }
136 
137  void SetPreserveVertex( const VECTOR2I& aV )
138  {
139  m_preservedVertex = aV;
141  }
142 
143  void SetRestrictVertexRange( int aStart, int aEnd )
144  {
145  m_restrictedVertexRange.first = aStart;
146  m_restrictedVertexRange.second = aEnd;
148  }
149 
150  void SetRestrictArea( const BOX2I& aArea, bool aStrict = true )
151  {
152  m_restrictArea = aArea;
153  m_restrictAreaIsStrict = aStrict;
154  }
155 
156  void ClearConstraints();
157  void AddConstraint ( OPT_CONSTRAINT *aConstraint );
158 
159 private:
160  static const int MaxCachedItems = 256;
161 
162  typedef std::vector<SHAPE_LINE_CHAIN> BREAKOUT_LIST;
163 
164  struct CACHE_VISITOR;
165 
166  struct CACHED_ITEM
167  {
168  int m_hits;
170  };
171 
172  bool mergeObtuse( LINE* aLine );
173  bool mergeFull( LINE* aLine );
174  bool mergeColinear( LINE* aLine );
175  bool runSmartPads( LINE* aLine );
176  bool mergeStep( LINE* aLine, SHAPE_LINE_CHAIN& aCurrentLine, int step );
177  bool fanoutCleanup( LINE * aLine );
178  bool mergeDpSegments( DIFF_PAIR *aPair );
179  bool mergeDpStep( DIFF_PAIR *aPair, bool aTryP, int step );
180 
181  bool checkColliding( ITEM* aItem, bool aUpdateCache = true );
182  bool checkColliding( LINE* aLine, const SHAPE_LINE_CHAIN& aOptPath );
183 
184  void cacheAdd( ITEM* aItem, bool aIsStatic );
185  void removeCachedSegments( LINE* aLine, int aStartVertex = 0, int aEndVertex = -1 );
186 
187  bool checkConstraints( int aVertex1, int aVertex2, LINE* aOriginLine,
188  const SHAPE_LINE_CHAIN& aCurrentPath,
189  const SHAPE_LINE_CHAIN& aReplacement );
190 
191  BREAKOUT_LIST circleBreakouts( int aWidth, const SHAPE* aShape, bool aPermitDiagonal ) const;
192  BREAKOUT_LIST rectBreakouts( int aWidth, const SHAPE* aShape, bool aPermitDiagonal ) const;
193  BREAKOUT_LIST customBreakouts( int aWidth, const ITEM* aItem, bool aPermitDiagonal ) const;
194  BREAKOUT_LIST computeBreakouts( int aWidth, const ITEM* aItem, bool aPermitDiagonal ) const;
195 
196  int smartPadsSingle( LINE* aLine, ITEM* aPad, bool aEnd, int aEndVertex );
197 
198  ITEM* findPadOrVia( int aLayer, int aNet, const VECTOR2I& aP ) const;
199 
200 private:
202  std::vector<OPT_CONSTRAINT*> m_constraints;
203  std::unordered_map<ITEM*, CACHED_ITEM> m_cacheTags;
204 
208 
210  std::pair<int, int> m_restrictedVertexRange;
213 };
214 
215 
217 {
218 public:
219  OPT_CONSTRAINT( NODE* aWorld ) :
220  m_world( aWorld )
221  {
222  m_priority = 0;
223  };
224 
225  virtual ~OPT_CONSTRAINT()
226  {
227  };
228 
229  virtual bool Check( int aVertex1, int aVertex2, const LINE* aOriginLine,
230  const SHAPE_LINE_CHAIN& aCurrentPath,
231  const SHAPE_LINE_CHAIN& aReplacement ) = 0;
232 
233  int GetPriority() const { return m_priority; }
234  void SetPriority( int aPriority ) { m_priority = aPriority; }
235 
236 protected:
239 };
240 
242 {
243 public:
244  ANGLE_CONSTRAINT_45( NODE* aWorld, int aEntryDirectionMask = -1, int aExitDirectionMask = -1 ) :
245  OPT_CONSTRAINT( aWorld ),
246  m_entryDirectionMask( aEntryDirectionMask ),
247  m_exitDirectionMask( aExitDirectionMask )
248  {
249 
250  }
251 
252  virtual ~ANGLE_CONSTRAINT_45() {};
253 
254  virtual bool Check ( int aVertex1, int aVertex2, const LINE* aOriginLine,
255  const SHAPE_LINE_CHAIN& aCurrentPath,
256  const SHAPE_LINE_CHAIN& aReplacement ) override;
257 
258 private:
261 };
262 
264 {
265 public:
266  AREA_CONSTRAINT( NODE* aWorld, const BOX2I& aAllowedArea, bool aAllowedAreaStrict ) :
267  OPT_CONSTRAINT( aWorld ),
268  m_allowedArea ( aAllowedArea ),
269  m_allowedAreaStrict ( aAllowedAreaStrict )
270  {
271  };
272 
273  bool Check( int aVertex1, int aVertex2, const LINE* aOriginLine,
274  const SHAPE_LINE_CHAIN& aCurrentPath,
275  const SHAPE_LINE_CHAIN& aReplacement ) override;
276 
277 private:
280 };
281 
282 
284 {
285 public:
287  OPT_CONSTRAINT( aWorld )
288  {
289  };
290 
291  bool Check( int aVertex1, int aVertex2, const LINE* aOriginLine,
292  const SHAPE_LINE_CHAIN& aCurrentPath,
293  const SHAPE_LINE_CHAIN& aReplacement ) override;
294 };
295 
296 
298 {
299 public:
300  PRESERVE_VERTEX_CONSTRAINT( NODE* aWorld, const VECTOR2I& aV ) :
301  OPT_CONSTRAINT( aWorld ),
302  m_v( aV )
303  {
304  };
305 
306  bool Check( int aVertex1, int aVertex2, const LINE* aOriginLine,
307  const SHAPE_LINE_CHAIN& aCurrentPath,
308  const SHAPE_LINE_CHAIN& aReplacement ) override;
309 private:
311 };
312 
313 
315 {
316 public:
317  RESTRICT_VERTEX_RANGE_CONSTRAINT( NODE* aWorld, int aStart, int aEnd ) :
318  OPT_CONSTRAINT( aWorld ),
319  m_start( aStart ),
320  m_end( aEnd )
321  {
322  };
323 
324  virtual bool Check( int aVertex1, int aVertex2, const LINE* aOriginLine,
325  const SHAPE_LINE_CHAIN& aCurrentPath,
326  const SHAPE_LINE_CHAIN& aReplacement ) override;
327 private:
328  int m_start;
329  int m_end;
330 };
331 
332 
334 {
335 public:
336  CORNER_COUNT_LIMIT_CONSTRAINT( NODE* aWorld, int aMinCorners, int aMaxCorners,
337  int aAngleMask ) :
338  OPT_CONSTRAINT( aWorld ),
339  m_minCorners( aMinCorners ),
340  m_maxCorners( aMaxCorners ),
341  m_angleMask( aAngleMask )
342  {
343  };
344 
345  virtual bool Check( int aVertex1, int aVertex2, const LINE* aOriginLine,
346  const SHAPE_LINE_CHAIN& aCurrentPath,
347  const SHAPE_LINE_CHAIN& aReplacement ) override;
348 
349 private:
353 };
354 
355 
356 
357 };
358 
359 #endif // __PNS_OPTIMIZER_H
BREAKOUT_LIST computeBreakouts(int aWidth, const ITEM *aItem, bool aPermitDiagonal) const
Base class for PNS router board items.
Definition: pns_item.h:55
virtual ~OPT_CONSTRAINT()
int GetPriority() const
BREAKOUT_LIST circleBreakouts(int aWidth, const SHAPE *aShape, bool aPermitDiagonal) const
void SetRestrictVertexRange(int aStart, int aEnd)
std::unordered_map< ITEM *, CACHED_ITEM > m_cacheTags
Keep the router "world" - i.e.
Definition: pns_node.h:144
void CacheRemove(ITEM *aItem)
ANGLE_CONSTRAINT_45(NODE *aWorld, int aEntryDirectionMask=-1, int aExitDirectionMask=-1)
void removeCachedSegments(LINE *aLine, int aStartVertex=0, int aEndVertex=-1)
COST_ESTIMATOR(const COST_ESTIMATOR &aB)
Definition: pns_optimizer.h:56
~OPTIMIZER()
A quick shortcut to optimize a line without creating and setting up an optimizer.
Simplify pad-pad and pad-via connections if possible.
AREA_CONSTRAINT(NODE *aWorld, const BOX2I &aAllowedArea, bool aAllowedAreaStrict)
void SetRestrictArea(const BOX2I &aArea, bool aStrict=true)
bool mergeStep(LINE *aLine, SHAPE_LINE_CHAIN &aCurrentLine, int step)
ITEM * findPadOrVia(int aLayer, int aNet, const VECTOR2I &aP) const
void Add(const LINE &aLine)
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:60
BREAKOUT_LIST rectBreakouts(int aWidth, const SHAPE *aShape, bool aPermitDiagonal) const
bool runSmartPads(LINE *aLine)
KEEP_TOPOLOGY_CONSTRAINT(NODE *aWorld)
VECTOR2< int > VECTOR2I
Definition: vector2d.h:623
bool mergeFull(LINE *aLine)
bool checkConstraints(int aVertex1, int aVertex2, LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement)
double GetCornerCost() const
Definition: pns_optimizer.h:75
static bool Optimize(LINE *aLine, int aEffortLevel, NODE *aWorld, const VECTOR2I &aV=VECTOR2I(0, 0))
bool m_restrictAreaIsStrict
void SetCollisionMask(int aMask)
bool mergeDpStep(DIFF_PAIR *aPair, bool aTryP, int step)
void AddConstraint(OPT_CONSTRAINT *aConstraint)
BREAKOUT_LIST customBreakouts(int aWidth, const ITEM *aItem, bool aPermitDiagonal) const
virtual bool Check(int aVertex1, int aVertex2, const LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement) override
void SetWorld(NODE *aNode)
virtual bool Check(int aVertex1, int aVertex2, const LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement)=0
bool Check(int aVertex1, int aVertex2, const LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement) override
bool mergeDpSegments(DIFF_PAIR *aPair)
void SetPreserveVertex(const VECTOR2I &aV)
virtual bool Check(int aVertex1, int aVertex2, const LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement) override
bool mergeObtuse(LINE *aLine)
An abstract shape on 2D plane.
Definition: shape.h:116
void Remove(const LINE &aLine)
Reduce corner cost iteratively.
Definition: pns_optimizer.h:99
bool Check(int aVertex1, int aVertex2, const LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement) override
std::vector< OPT_CONSTRAINT * > m_constraints
static const int MaxCachedItems
Definition: seg.h:40
OPTIMIZER(NODE *aWorld)
VECTOR2I m_preservedVertex
Basic class for a differential pair.
void cacheAdd(ITEM *aItem, bool aIsStatic)
double GetLengthCost() const
Definition: pns_optimizer.h:74
RESTRICT_VERTEX_RANGE_CONSTRAINT(NODE *aWorld, int aStart, int aEnd)
bool Check(int aVertex1, int aVertex2, const LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement) override
void SetPriority(int aPriority)
Reduce corner cost by merging obtuse segments.
bool IsBetter(const COST_ESTIMATOR &aOther, double aLengthTolerance, double aCornerTollerace) const
bool fanoutCleanup(LINE *aLine)
Do not attempt to optimize if the resulting line's corner count is outside the predefined range.
Represent a polyline (an zero-thickness chain of connected line segments).
bool mergeColinear(LINE *aLine)
PRESERVE_VERTEX_CONSTRAINT(NODE *aWorld, const VECTOR2I &aV)
bool checkColliding(ITEM *aItem, bool aUpdateCache=true)
virtual bool Check(int aVertex1, int aVertex2, const LINE *aOriginLine, const SHAPE_LINE_CHAIN &aCurrentPath, const SHAPE_LINE_CHAIN &aReplacement) override
static int CornerCost(const SEG &aA, const SEG &aB)
void Replace(const LINE &aOldLine, const LINE &aNewLine)
Perform various optimizations of the lines being routed, attempting to make the lines shorter and les...
Definition: pns_optimizer.h:94
Merge co-linear segments.
SHAPE_INDEX_LIST< ITEM * > m_cache
int smartPadsSingle(LINE *aLine, ITEM *aPad, bool aEnd, int aEndVertex)
void SetEffortLevel(int aEffort)
std::vector< SHAPE_LINE_CHAIN > BREAKOUT_LIST
Calculate the cost of a given line, taking corner angles and total length into account.
Definition: pns_optimizer.h:48
Push and Shove diff pair dimensions (gap) settings dialog.
std::pair< int, int > m_restrictedVertexRange
void ClearCache(bool aStaticOnly=false)
Reroute pad exits.
OPT_CONSTRAINT(NODE *aWorld)
CORNER_COUNT_LIMIT_CONSTRAINT(NODE *aWorld, int aMinCorners, int aMaxCorners, int aAngleMask)