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 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_NODE_H
23 #define __PNS_NODE_H
24 
25 #include <vector>
26 #include <list>
27 #include <unordered_set>
28 #include <unordered_map>
29 
30 #include <core/optional.h>
31 
32 #include <geometry/shape.h>
34 #include <geometry/shape_index.h>
35 
36 #include "pns_item.h"
37 #include "pns_joint.h"
38 #include "pns_itemset.h"
39 
40 namespace PNS {
41 
42 class SEGMENT;
43 class LINE;
44 class SOLID;
45 class VIA;
46 class INDEX;
47 class ROUTER;
48 class NODE;
49 
57 {
58 public:
59  virtual ~RULE_RESOLVER() {}
60 
61  virtual int Clearance( const ITEM* aA, const ITEM* aB ) const = 0;
62  virtual int Clearance( int aNetCode ) const = 0;
63  virtual int DpCoupledNet( int aNet ) = 0;
64  virtual int DpNetPolarity( int aNet ) = 0;
65  virtual bool DpNetPair( ITEM* aItem, int& aNetP, int& aNetN ) = 0;
66  virtual wxString NetName( int aNet ) = 0;
67 };
68 
75 struct OBSTACLE
76 {
78  const ITEM* m_head;
79 
82 
85 
88  VECTOR2I m_ipFirst, m_ipLast;
89 
91  int m_distFirst, m_distLast;
92 };
93 
98 
99 public:
100 
101  OBSTACLE_VISITOR( const ITEM* aItem );
102 
103  void SetWorld( const NODE* aNode, const NODE* aOverride = NULL );
104 
105  virtual bool operator()( ITEM* aCandidate ) = 0;
106 
107 protected:
108 
109  bool visit( ITEM* aCandidate );
110 
112  const ITEM* m_item;
113 
115  const NODE* m_node;
116 
118  const NODE* m_override;
119 
122 };
123 
136 class NODE
137 {
138 public:
140  typedef std::vector<ITEM*> ITEM_VECTOR;
141  typedef std::vector<OBSTACLE> OBSTACLES;
142 
143  NODE();
144  ~NODE();
145 
147  int GetClearance( const ITEM* aA, const ITEM* aB ) const;
148 
150  int GetMaxClearance() const
151  {
152  return m_maxClearance;
153  }
154 
156  void SetMaxClearance( int aClearance )
157  {
158  m_maxClearance = aClearance;
159  }
160 
163  {
164  m_ruleResolver = aFunc;
165  }
166 
168  {
169  return m_ruleResolver;
170  }
171 
173  int JointCount() const
174  {
175  return m_joints.size();
176  }
177 
179  int Depth() const
180  {
181  return m_depth;
182  }
183 
194  int QueryColliding( const ITEM* aItem,
195  OBSTACLES& aObstacles,
196  int aKindMask = ITEM::ANY_T,
197  int aLimitCount = -1,
198  bool aDifferentNetsOnly = true,
199  int aForceClearance = -1 );
200 
201  int QueryColliding( const ITEM* aItem,
202  OBSTACLE_VISITOR& aVisitor
203  );
204 
214  OPT_OBSTACLE NearestObstacle( const LINE* aItem,
215  int aKindMask = ITEM::ANY_T,
216  const std::set<ITEM*>* aRestrictedSet = NULL );
217 
227  OPT_OBSTACLE CheckColliding( const ITEM* aItem,
228  int aKindMask = ITEM::ANY_T );
229 
230 
240  OPT_OBSTACLE CheckColliding( const ITEM_SET& aSet,
241  int aKindMask = ITEM::ANY_T );
242 
243 
254  bool CheckColliding( const ITEM* aItemA,
255  const ITEM* aItemB,
256  int aKindMask = ITEM::ANY_T,
257  int aForceClearance = -1 );
258 
266  const ITEM_SET HitTest( const VECTOR2I& aPoint ) const;
267 
277  bool Add( std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant = false );
278  void Add( std::unique_ptr< SOLID > aSolid );
279  void Add( std::unique_ptr< VIA > aVia );
280 
281  void Add( LINE& aLine, bool aAllowRedundant = false );
282 
283 private:
284  void Add( std::unique_ptr< ITEM > aItem, bool aAllowRedundant = false );
285 
286 public:
292  void Remove( SOLID* aSolid );
293  void Remove( VIA* aVia );
294  void Remove( SEGMENT* aSegment );
295  void Remove( ITEM* aItem );
296 
297 public:
304  void Remove( LINE& aLine );
305 
313  void Replace( ITEM* aOldItem, std::unique_ptr< ITEM > aNewItem );
314  void Replace( LINE& aOldLine, LINE& aNewLine );
315 
324  NODE* Branch();
325 
335  const LINE AssembleLine( SEGMENT* aSeg, int* aOriginSegmentIndex = NULL,
336  bool aStopAtLockedJoints = false );
337 
339  void Dump( bool aLong = false );
340 
349  void GetUpdatedItems( ITEM_VECTOR& aRemoved, ITEM_VECTOR& aAdded );
350 
358  void Commit( NODE* aNode );
359 
366  JOINT* FindJoint( const VECTOR2I& aPos, int aLayer, int aNet );
367 
368  void LockJoint( const VECTOR2I& aPos, const ITEM* aItem, bool aLock );
369 
376  JOINT* FindJoint( const VECTOR2I& aPos, const ITEM* aItem )
377  {
378  return FindJoint( aPos, aItem->Layers().Start(), aItem->Net() );
379  }
380 
381 #if 0
382  void MapConnectivity( JOINT* aStart, std::vector<JOINT*> & aFoundJoints );
383 
384  ITEM* NearestUnconnectedItem( JOINT* aStart, int* aAnchor = NULL,
385  int aKindMask = ITEM::ANY_T);
386 
387 #endif
388 
390  int FindLinesBetweenJoints( JOINT& aA,
391  JOINT& aB,
392  std::vector<LINE>& aLines );
393 
395  void FindLineEnds( const LINE& aLine, JOINT& aA, JOINT& aB );
396 
398  void KillChildren();
399 
400  void AllItemsInNet( int aNet, std::set<ITEM*>& aItems );
401 
402  void ClearRanks( int aMarkerMask = MK_HEAD | MK_VIOLATION );
403 
404  int FindByMarker( int aMarker, ITEM_SET& aItems );
405  int RemoveByMarker( int aMarker );
406 
407  ITEM* FindItemByParent( const BOARD_CONNECTED_ITEM* aParent );
408 
409  bool HasChildren() const
410  {
411  return !m_children.empty();
412  }
413 
416  bool Overrides( ITEM* aItem ) const
417  {
418  return m_override.find( aItem ) != m_override.end();
419  }
420 
421 private:
423  typedef std::unordered_multimap<JOINT::HASH_TAG, JOINT, JOINT::JOINT_TAG_HASH> JOINT_MAP;
424  typedef JOINT_MAP::value_type TagJointPair;
425 
427  NODE( const NODE& aB );
428  NODE& operator=( const NODE& aB );
429 
431  JOINT& touchJoint( const VECTOR2I& aPos,
432  const LAYER_RANGE& aLayers,
433  int aNet );
434 
436  void linkJoint( const VECTOR2I& aPos, const LAYER_RANGE& aLayers, int aNet, ITEM* aWhere );
437 
439  void unlinkJoint( const VECTOR2I& aPos, const LAYER_RANGE& aLayers, int aNet, ITEM* aWhere );
440 
442  void addSolid( SOLID* aSeg );
443  void addSegment( SEGMENT* aSeg );
444  void addVia( VIA* aVia );
445 
446  void removeLine( LINE& aLine );
447  void removeSolidIndex( SOLID* aSeg );
448  void removeSegmentIndex( SEGMENT* aSeg );
449  void removeViaIndex( VIA* aVia );
450 
451  void doRemove( ITEM* aItem );
452  void unlinkParent();
453  void releaseChildren();
454  void releaseGarbage();
455 
456  bool isRoot() const
457  {
458  return m_parent == NULL;
459  }
460 
461  SEGMENT* findRedundantSegment( const VECTOR2I& A, const VECTOR2I& B,
462  const LAYER_RANGE & lr, int aNet );
463  SEGMENT* findRedundantSegment( SEGMENT* aSeg );
464 
466  void followLine( SEGMENT* aCurrent,
467  bool aScanDirection,
468  int& aPos,
469  int aLimit,
470  VECTOR2I* aCorners,
471  SEGMENT** aSegments,
472  bool& aGuardHit,
473  bool aStopAtLockedJoints );
474 
477  JOINT_MAP m_joints;
478 
481 
484 
486  std::set<NODE*> m_children;
487 
489  std::unordered_set<ITEM*> m_override;
490 
493 
496 
499 
501  int m_depth;
502 
503  std::unordered_set<ITEM*> m_garbageItems;
504 };
505 
506 }
507 
508 #endif
Class ITEM.
Definition: pns_item.h:53
std::set< NODE * > m_children
list of nodes branched from this one
Definition: pns_node.h:486
ITEM * m_item
Item found to be colliding with m_head
Definition: pns_node.h:81
int Depth() const
Returns the number of nodes in the inheritance chain (wrs to the root node)
Definition: pns_node.h:179
const NODE * m_node
node we are searching in (either root or a branch)
Definition: pns_node.h:115
VECTOR2I m_ipLast
Definition: pns_node.h:88
int GetMaxClearance() const
Returns the pre-set worst case clearance between any pair of items
Definition: pns_node.h:150
Class NODE.
Definition: pns_node.h:136
virtual ~RULE_RESOLVER()
Definition: pns_node.h:59
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:209
virtual int DpCoupledNet(int aNet)=0
bool Overrides(ITEM *aItem) const
checks if this branch contains an updated version of the m_item from the root branch.
Definition: pns_node.h:416
int m_extraClearance
additional clearance
Definition: pns_node.h:121
Class RULE_RESOLVER.
Definition: pns_node.h:56
void SetRuleResolver(RULE_RESOLVER *aFunc)
Assigns a clerance resolution function object
Definition: pns_node.h:162
Class BOARD_CONNECTED_ITEM is a base class derived from BOARD_ITEM for items that can be connected an...
INDEX * m_index
Geometric/Net index of the items
Definition: pns_node.h:498
void SetMaxClearance(int aClearance)
Sets the worst-case clerance between any pair of items
Definition: pns_node.h:156
NODE * m_root
root node of the whole hierarchy
Definition: pns_node.h:483
int m_depth
depth of the node (number of parent nodes in the inheritance chain)
Definition: pns_node.h:501
Class JOINT.
Definition: pns_joint.h:43
RULE_RESOLVER * m_ruleResolver
Design rules resolver
Definition: pns_node.h:495
Struct OBSTACLE_VISITOR.
Definition: pns_node.h:97
std::unordered_multimap< JOINT::HASH_TAG, JOINT, JOINT::JOINT_TAG_HASH > JOINT_MAP
Definition: pns_node.h:422
JOINT * FindJoint(const VECTOR2I &aPos, const ITEM *aItem)
Function FindJoint()
Definition: pns_node.h:376
virtual int Clearance(const ITEM *aA, const ITEM *aB) const =0
std::unordered_set< ITEM * > m_override
hash of root&#39;s items that have been changed in this node
Definition: pns_node.h:489
const ITEM * m_item
the item we are looking for collisions with
Definition: pns_node.h:112
int Start() const
Definition: pns_layerset.h:83
bool isRoot() const
Definition: pns_node.h:456
virtual bool DpNetPair(ITEM *aItem, int &aNetP, int &aNetN)=0
int m_distLast
Definition: pns_node.h:91
SHAPE_LINE_CHAIN m_hull
Hull of the colliding m_item
Definition: pns_node.h:84
JOINT_MAP::value_type TagJointPair
Definition: pns_node.h:424
RULE_RESOLVER * GetRuleResolver()
Definition: pns_node.h:167
int JointCount() const
Returns the number of joints
Definition: pns_node.h:173
Class SHAPE_LINE_CHAIN.
std::unordered_set< ITEM * > m_garbageItems
Definition: pns_node.h:503
int Net() const
Function Net()
Definition: pns_item.h:179
const ITEM * m_head
Item we search collisions with
Definition: pns_node.h:78
boost::optional< T > OPT
Definition: optional.h:7
OPT< OBSTACLE > OPT_OBSTACLE
Definition: pns_node.h:139
virtual int DpNetPolarity(int aNet)=0
virtual wxString NetName(int aNet)=0
int m_maxClearance
worst case item-item clearance
Definition: pns_node.h:492
JOINT_MAP m_joints
hash table with the joints, linking the items.
Definition: pns_node.h:477
bool HasChildren() const
Definition: pns_node.h:409
std::vector< OBSTACLE > OBSTACLES
Definition: pns_node.h:141
const NODE * m_override
node that overrides root entries
Definition: pns_node.h:118
Push and Shove diff pair dimensions (gap) settings dialog.
NODE * m_parent
node this node was branched from
Definition: pns_node.h:480
Class INDEX.
Definition: pns_index.h:46
Class LAYER_RANGE.
Definition: pns_layerset.h:32
std::vector< ITEM * > ITEM_VECTOR
Definition: pns_node.h:140
Struct OBSTACLE.
Definition: pns_node.h:75