KiCad PCB EDA Suite
pns_node.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_NODE_H
24 #define __PNS_NODE_H
25 
26 #include <vector>
27 #include <list>
28 #include <unordered_set>
29 #include <core/minoptmax.h>
30 
32 #include <geometry/shape_index.h>
33 
34 #include "pns_item.h"
35 #include "pns_joint.h"
36 #include "pns_itemset.h"
37 
38 namespace PNS {
39 
40 class ARC;
41 class SEGMENT;
42 class LINE;
43 class SOLID;
44 class VIA;
45 class INDEX;
46 class ROUTER;
47 class NODE;
48 
54 enum class CONSTRAINT_TYPE
55 {
56  CT_CLEARANCE = 1,
57  CT_DIFF_PAIR_GAP = 2,
58  CT_LENGTH = 3,
59  CT_WIDTH = 4,
60  CT_VIA_DIAMETER = 5,
61  CT_VIA_HOLE = 6,
64  CT_HOLE_TO_HOLE = 9
65 };
66 
67 struct CONSTRAINT
68 {
71  bool m_Allowed;
72  wxString m_RuleName;
73  wxString m_FromName;
74  wxString m_ToName;
75 };
76 
78 {
79 public:
80  virtual ~RULE_RESOLVER() {}
81 
82  virtual int Clearance( const ITEM* aA, const ITEM* aB ) = 0;
83  virtual int HoleClearance( const ITEM* aA, const ITEM* aB ) = 0;
84  virtual int HoleToHoleClearance( const ITEM* aA, const ITEM* aB ) = 0;
85 
86  virtual int DpCoupledNet( int aNet ) = 0;
87  virtual int DpNetPolarity( int aNet ) = 0;
88  virtual bool DpNetPair( const ITEM* aItem, int& aNetP, int& aNetN ) = 0;
89 
90  virtual bool IsDiffPair( const ITEM* aA, const ITEM* aB ) = 0;
91 
92  virtual bool QueryConstraint( CONSTRAINT_TYPE aType, const PNS::ITEM* aItemA,
93  const PNS::ITEM* aItemB, int aLayer,
94  PNS::CONSTRAINT* aConstraint ) = 0;
95 
96  virtual wxString NetName( int aNet ) = 0;
97 };
98 
102 struct OBSTACLE
103 {
104  const ITEM* m_head;
105 
110 };
111 
113 {
114 public:
115  OBSTACLE_VISITOR( const ITEM* aItem );
116 
118  {
119  }
120 
121  void SetWorld( const NODE* aNode, const NODE* aOverride = NULL );
122 
123  virtual bool operator()( ITEM* aCandidate ) = 0;
124 
125 protected:
126  bool visit( ITEM* aCandidate );
127 
128 protected:
129  const ITEM* m_item;
130 
131  const NODE* m_node;
132  const NODE* m_override;
134 };
135 
145 class NODE
146 {
147 public:
149  typedef std::vector<ITEM*> ITEM_VECTOR;
150  typedef std::vector<OBSTACLE> OBSTACLES;
151 
152  NODE();
153  ~NODE();
154 
156  int GetClearance( const ITEM* aA, const ITEM* aB ) const;
157  int GetHoleClearance( const ITEM* aA, const ITEM* aB ) const;
158  int GetHoleToHoleClearance( const ITEM* aA, const ITEM* aB ) const;
159 
161  int GetMaxClearance() const
162  {
163  return m_maxClearance;
164  }
165 
167  void SetMaxClearance( int aClearance )
168  {
169  m_maxClearance = aClearance;
170  }
171 
174  {
175  m_ruleResolver = aFunc;
176  }
177 
179  {
180  return m_ruleResolver;
181  }
182 
184  int JointCount() const
185  {
186  return m_joints.size();
187  }
188 
190  int Depth() const
191  {
192  return m_depth;
193  }
194 
204  int QueryColliding( const ITEM* aItem, OBSTACLES& aObstacles, int aKindMask = ITEM::ANY_T,
205  int aLimitCount = -1, bool aDifferentNetsOnly = true );
206 
207  int QueryJoints( const BOX2I& aBox, std::vector<JOINT*>& aJoints,
208  LAYER_RANGE aLayerMask = LAYER_RANGE::All(), int aKindMask = ITEM::ANY_T );
209 
218  OPT_OBSTACLE NearestObstacle( const LINE* aLine, int aKindMask = ITEM::ANY_T,
219  const std::set<ITEM*>* aRestrictedSet = NULL );
220 
229  OPT_OBSTACLE CheckColliding( const ITEM* aItem, int aKindMask = ITEM::ANY_T );
230 
231 
240  OPT_OBSTACLE CheckColliding( const ITEM_SET& aSet, int aKindMask = ITEM::ANY_T );
241 
248  const ITEM_SET HitTest( const VECTOR2I& aPoint ) const;
249 
258  bool Add( std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant = false );
259  void Add( std::unique_ptr< SOLID > aSolid );
260  void Add( std::unique_ptr< VIA > aVia );
261  void Add( std::unique_ptr< ARC > aArc );
262 
263  void Add( LINE& aLine, bool aAllowRedundant = false );
264 
268  void Remove( ARC* aArc );
269  void Remove( SOLID* aSolid );
270  void Remove( VIA* aVia );
271  void Remove( SEGMENT* aSegment );
272  void Remove( ITEM* aItem );
273 
279  void Remove( LINE& aLine );
280 
287  void Replace( ITEM* aOldItem, std::unique_ptr< ITEM > aNewItem );
288  void Replace( LINE& aOldLine, LINE& aNewLine );
289 
298  NODE* Branch();
299 
308  const LINE AssembleLine( LINKED_ITEM* aSeg, int* aOriginSegmentIndex = NULL,
309  bool aStopAtLockedJoints = false );
310 
312  void Dump( bool aLong = false );
313 
320  void GetUpdatedItems( ITEM_VECTOR& aRemoved, ITEM_VECTOR& aAdded );
321 
330  void Commit( NODE* aNode );
331 
337  JOINT* FindJoint( const VECTOR2I& aPos, int aLayer, int aNet );
338 
339  void LockJoint( const VECTOR2I& aPos, const ITEM* aItem, bool aLock );
340 
346  JOINT* FindJoint( const VECTOR2I& aPos, const ITEM* aItem )
347  {
348  return FindJoint( aPos, aItem->Layers().Start(), aItem->Net() );
349  }
350 
352  int FindLinesBetweenJoints( const JOINT& aA, const JOINT& aB, std::vector<LINE>& aLines );
353 
355  void FindLineEnds( const LINE& aLine, JOINT& aA, JOINT& aB );
356 
358  void KillChildren();
359 
360  void AllItemsInNet( int aNet, std::set<ITEM*>& aItems, int aKindMask = -1 );
361 
362  void ClearRanks( int aMarkerMask = MK_HEAD | MK_VIOLATION | MK_HOLE );
363 
364  void RemoveByMarker( int aMarker );
365 
366  ITEM* FindItemByParent( const BOARD_ITEM* aParent );
367 
368  bool HasChildren() const
369  {
370  return !m_children.empty();
371  }
372 
373  NODE* GetParent() const
374  {
375  return m_parent;
376  }
377 
379  bool Overrides( ITEM* aItem ) const
380  {
381  return m_override.find( aItem ) != m_override.end();
382  }
383 
384 private:
385  void Add( std::unique_ptr< ITEM > aItem, bool aAllowRedundant = false );
386 
388  NODE( const NODE& aB );
389  NODE& operator=( const NODE& aB );
390 
392  JOINT& touchJoint( const VECTOR2I& aPos, const LAYER_RANGE& aLayers, int aNet );
393 
395  void linkJoint( const VECTOR2I& aPos, const LAYER_RANGE& aLayers, int aNet, ITEM* aWhere );
396 
398  void unlinkJoint( const VECTOR2I& aPos, const LAYER_RANGE& aLayers, int aNet, ITEM* aWhere );
399 
401  void addSolid( SOLID* aSeg );
402  void addSegment( SEGMENT* aSeg );
403  void addVia( VIA* aVia );
404  void addArc( ARC* aVia );
405 
406  void removeSolidIndex( SOLID* aSeg );
407  void removeSegmentIndex( SEGMENT* aSeg );
408  void removeViaIndex( VIA* aVia );
409  void removeArcIndex( ARC* aVia );
410 
411  void doRemove( ITEM* aItem );
412  void unlinkParent();
413  void releaseChildren();
414  void releaseGarbage();
415  void rebuildJoint( JOINT* aJoint, ITEM* aItem );
416 
417  bool isRoot() const
418  {
419  return m_parent == NULL;
420  }
421 
422  SEGMENT* findRedundantSegment( const VECTOR2I& A, const VECTOR2I& B, const LAYER_RANGE& lr,
423  int aNet );
425 
426  ARC* findRedundantArc( const VECTOR2I& A, const VECTOR2I& B, const LAYER_RANGE& lr, int aNet );
427  ARC* findRedundantArc( ARC* aSeg );
428 
430  void followLine( LINKED_ITEM* aCurrent, bool aScanDirection, int& aPos, int aLimit,
431  VECTOR2I* aCorners, LINKED_ITEM** aSegments, bool* aArcReversed,
432  bool& aGuardHit, bool aStopAtLockedJoints );
433 
434 private:
436  typedef std::unordered_multimap<JOINT::HASH_TAG, JOINT, JOINT::JOINT_TAG_HASH> JOINT_MAP;
437  typedef JOINT_MAP::value_type TagJointPair;
438 
440 
444  std::set<NODE*> m_children;
445 
446  std::unordered_set<ITEM*> m_override;
447 
452  int m_depth;
453 
455  std::unordered_set<ITEM*> m_garbageItems;
456 };
457 
458 }
459 
460 #endif
void ClearRanks(int aMarkerMask=MK_HEAD|MK_VIOLATION|MK_HOLE)
Definition: pns_node.cpp:1335
Base class for PNS router board items.
Definition: pns_item.h:55
OPT_OBSTACLE NearestObstacle(const LINE *aLine, int aKindMask=ITEM::ANY_T, const std::set< ITEM * > *aRestrictedSet=NULL)
Follow the line in search of an obstacle that is nearest to the starting to the line's starting point...
Definition: pns_node.cpp:283
std::set< NODE * > m_children
list of nodes branched from this one
Definition: pns_node.h:444
ITEM * m_item
Item found to be colliding with m_head.
Definition: pns_node.h:106
const NODE * m_node
node we are searching in (either root or a branch)
Definition: pns_node.h:131
Definition: track.h:343
Keep the router "world" - i.e.
Definition: pns_node.h:145
virtual ~RULE_RESOLVER()
Definition: pns_node.h:80
int GetHoleToHoleClearance(const ITEM *aA, const ITEM *aB) const
Return the pre-set worst case clearance between any pair of items.
Definition: pns_node.cpp:115
A base class for any item which can be embedded within the BOARD container class, and therefore insta...
Definition: board_item.h:82
void releaseChildren()
Definition: pns_node.cpp:1252
virtual bool QueryConstraint(CONSTRAINT_TYPE aType, const PNS::ITEM *aItemA, const PNS::ITEM *aItemB, int aLayer, PNS::CONSTRAINT *aConstraint)=0
virtual int DpCoupledNet(int aNet)=0
void unlinkParent()
Definition: pns_node.cpp:160
int m_distFirst
... and the distance thereof
Definition: pns_node.h:109
void FindLineEnds(const LINE &aLine, JOINT &aA, JOINT &aB)
Destroy all child nodes. Applicable only to the root node.
Definition: pns_node.cpp:994
void doRemove(ITEM *aItem)
Definition: pns_node.cpp:667
void removeSegmentIndex(SEGMENT *aSeg)
Definition: pns_node.cpp:688
int m_extraClearance
additional clearance
Definition: pns_node.h:133
bool HasChildren() const
Definition: pns_node.h:368
int JointCount() const
Return the number of nodes in the inheritance chain (wrs to the root node).
Definition: pns_node.h:184
int FindLinesBetweenJoints(const JOINT &aA, const JOINT &aB, std::vector< LINE > &aLines)
Find the joints corresponding to the ends of line aLine.
Definition: pns_node.cpp:1001
wxString m_RuleName
Definition: pns_node.h:72
virtual int Clearance(const ITEM *aA, const ITEM *aB)=0
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:60
int Depth() const
Definition: pns_node.h:190
bool Overrides(ITEM *aItem) const
Definition: pns_node.h:379
virtual bool operator()(ITEM *aCandidate)=0
void SetRuleResolver(RULE_RESOLVER *aFunc)
Definition: pns_node.h:173
void linkJoint(const VECTOR2I &aPos, const LAYER_RANGE &aLayers, int aNet, ITEM *aWhere)
Unlink an item from a joint.
Definition: pns_node.cpp:1134
int GetHoleClearance(const ITEM *aA, const ITEM *aB) const
Definition: pns_node.cpp:106
void Commit(NODE *aNode)
Apply the changes from a given branch (aNode) to the root branch.
Definition: pns_node.cpp:1280
NODE * Branch()
Create a lightweight copy (called branch) of self that tracks the changes (added/removed items) wrs t...
Definition: pns_node.cpp:124
void unlinkJoint(const VECTOR2I &aPos, const LAYER_RANGE &aLayers, int aNet, ITEM *aWhere)
Helpers for adding/removing items.
Definition: pns_node.cpp:1142
bool isRoot() const
Definition: pns_node.h:417
virtual ~OBSTACLE_VISITOR()
Definition: pns_node.h:117
static LAYER_RANGE All()
Definition: pns_layerset.h:109
INDEX * m_index
Geometric/Net index of the items.
Definition: pns_node.h:451
void Remove(ARC *aArc)
Remove an item from this branch.
Definition: pns_node.cpp:802
int Start() const
Definition: pns_layerset.h:82
void SetMaxClearance(int aClearance)
Assign a clearance resolution function object.
Definition: pns_node.h:167
void addSegment(SEGMENT *aSeg)
Definition: pns_node.cpp:604
NODE * m_root
root node of the whole hierarchy
Definition: pns_node.h:443
int m_depth
depth of the node (number of parent nodes in the inheritance chain)
Definition: pns_node.h:452
Represents a 2D point on a given set of layers and belonging to a certain net, that links together a ...
Definition: pns_joint.h:42
RULE_RESOLVER * m_ruleResolver
Design rules resolver.
Definition: pns_node.h:450
void addSolid(SOLID *aSeg)
Definition: pns_node.cpp:521
int GetClearance(const ITEM *aA, const ITEM *aB) const
Definition: pns_node.cpp:97
void KillChildren()
Definition: pns_node.cpp:1300
const LINE AssembleLine(LINKED_ITEM *aSeg, int *aOriginSegmentIndex=NULL, bool aStopAtLockedJoints=false)
Follow the joint map to assemble a line connecting two non-trivial joints starting from segment aSeg.
Definition: pns_node.cpp:912
#define NULL
void rebuildJoint(JOINT *aJoint, ITEM *aItem)
Definition: pns_node.cpp:702
std::unordered_multimap< JOINT::HASH_TAG, JOINT, JOINT::JOINT_TAG_HASH > JOINT_MAP
Definition: pns_node.h:435
ITEM * FindItemByParent(const BOARD_ITEM *aParent)
Definition: pns_node.cpp:1468
int Net() const
Definition: pns_item.h:148
JOINT * FindJoint(const VECTOR2I &aPos, const ITEM *aItem)
Search for a joint at a given position, linked to given item.
Definition: pns_node.h:346
SEGMENT * findRedundantSegment(const VECTOR2I &A, const VECTOR2I &B, const LAYER_RANGE &lr, int aNet)
Definition: pns_node.cpp:1360
virtual int HoleClearance(const ITEM *aA, const ITEM *aB)=0
OBSTACLE_VISITOR(const ITEM *aItem)
Definition: pns_node.cpp:169
bool visit(ITEM *aCandidate)
Definition: pns_node.cpp:185
std::unordered_set< ITEM * > m_override
hash of root's items that have been changed in this node
Definition: pns_node.h:446
int QueryJoints(const BOX2I &aBox, std::vector< JOINT * > &aJoints, LAYER_RANGE aLayerMask=LAYER_RANGE::All(), int aKindMask=ITEM::ANY_T)
Definition: pns_node.cpp:1430
void SetWorld(const NODE *aNode, const NODE *aOverride=NULL)
Definition: pns_node.cpp:178
wxString m_FromName
Definition: pns_node.h:73
virtual int HoleToHoleClearance(const ITEM *aA, const ITEM *aB)=0
const ITEM * m_item
the item we are looking for collisions with
Definition: pns_node.h:129
void releaseGarbage()
Definition: pns_node.cpp:1265
void addVia(VIA *aVia)
Definition: pns_node.cpp:537
~NODE()
Return the expected clearance between items a and b.
Definition: pns_node.cpp:65
ARC * findRedundantArc(const VECTOR2I &A, const VECTOR2I &B, const LAYER_RANGE &lr, int aNet)
Definition: pns_node.cpp:1395
const ITEM_SET HitTest(const VECTOR2I &aPoint) const
Find all items that contain the point aPoint.
Definition: pns_node.cpp:492
void addArc(ARC *aVia)
Definition: pns_node.cpp:631
int QueryColliding(const ITEM *aItem, OBSTACLES &aObstacles, int aKindMask=ITEM::ANY_T, int aLimitCount=-1, bool aDifferentNetsOnly=true)
Find items colliding (closer than clearance) with the item aItem.
Definition: pns_node.cpp:257
SHAPE_LINE_CHAIN m_hull
Hull of the colliding m_item.
Definition: pns_node.h:107
JOINT * FindJoint(const VECTOR2I &aPos, int aLayer, int aNet)
Search for a joint at a given position, layer and belonging to given net.
Definition: pns_node.cpp:1035
RULE_RESOLVER * GetRuleResolver() const
Return the number of joints.
Definition: pns_node.h:178
void Replace(ITEM *aOldItem, std::unique_ptr< ITEM > aNewItem)
Replace an item with another one.
Definition: pns_node.cpp:767
void RemoveByMarker(int aMarker)
Definition: pns_node.cpp:1345
void removeSolidIndex(SOLID *aSeg)
Definition: pns_node.cpp:755
JOINT_MAP::value_type TagJointPair
Definition: pns_node.h:437
void Dump(bool aLong=false)
Definition: pns_node.cpp:1151
NODE * GetParent() const
Check if this branch contains an updated version of the m_item from the root branch.
Definition: pns_node.h:373
Definition: track.h:262
SHAPE_LINE_CHAIN.
void AllItemsInNet(int aNet, std::set< ITEM * > &aItems, int aKindMask=-1)
Definition: pns_node.cpp:1306
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Check if the item collides with anything else in the world, and if found, returns the obstacle.
Definition: pns_node.cpp:425
int GetMaxClearance() const
Set the worst-case clearance between any pair of items.
Definition: pns_node.h:161
JOINT & touchJoint(const VECTOR2I &aPos, const LAYER_RANGE &aLayers, int aNet)
Touch a joint and links it to an m_item.
Definition: pns_node.cpp:1072
std::unordered_set< ITEM * > m_garbageItems
Definition: pns_node.h:455
void removeViaIndex(VIA *aVia)
Definition: pns_node.cpp:747
void followLine(LINKED_ITEM *aCurrent, bool aScanDirection, int &aPos, int aLimit, VECTOR2I *aCorners, LINKED_ITEM **aSegments, bool *aArcReversed, bool &aGuardHit, bool aStopAtLockedJoints)
Definition: pns_node.cpp:863
const ITEM * m_head
Item we search collisions with.
Definition: pns_node.h:104
boost::optional< T > OPT
Definition: optional.h:7
void GetUpdatedItems(ITEM_VECTOR &aRemoved, ITEM_VECTOR &aAdded)
Return the list of items removed and added in this branch with respect to the root branch.
Definition: pns_node.cpp:1233
void removeArcIndex(ARC *aVia)
Definition: pns_node.cpp:695
OPT< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:148
virtual int DpNetPolarity(int aNet)=0
virtual wxString NetName(int aNet)=0
int m_maxClearance
worst case item-item clearance
Definition: pns_node.h:449
MINOPTMAX< int > m_Value
Definition: pns_node.h:70
virtual bool DpNetPair(const ITEM *aItem, int &aNetP, int &aNetN)=0
wxString m_ToName
Definition: pns_node.h:74
JOINT_MAP m_joints
hash table with the joints, linking the items.
Definition: pns_node.h:439
VECTOR2I m_ipFirst
First intersection between m_head and m_hull.
Definition: pns_node.h:108
virtual bool IsDiffPair(const ITEM *aA, const ITEM *aB)=0
std::vector< OBSTACLE > OBSTACLES
Definition: pns_node.h:150
const NODE * m_override
node that overrides root entries
Definition: pns_node.h:132
Push and Shove diff pair dimensions (gap) settings dialog.
NODE * m_parent
node this node was branched from
Definition: pns_node.h:442
NODE & operator=(const NODE &aB)
Try to find matching joint and creates a new one if not found.
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Add an item to the current node.
Definition: pns_node.cpp:613
CONSTRAINT_TYPE m_Type
Definition: pns_node.h:69
INDEX.
Definition: pns_index.h:45
Represent a contiguous set of PCB layers.
Definition: pns_layerset.h:31
const LAYER_RANGE & Layers() const
Definition: pns_item.h:150
void LockJoint(const VECTOR2I &aPos, const ITEM *aItem, bool aLock)
Definition: pns_node.cpp:1065
std::vector< ITEM * > ITEM_VECTOR
Definition: pns_node.h:149
CONSTRAINT_TYPE
An abstract function object, returning a design rule (clearance, diff pair gap, etc) required between...
Definition: pns_node.h:54
bool m_Allowed
Definition: pns_node.h:71
Hold an object colliding with another object, along with some useful data about the collision.
Definition: pns_node.h:102