Horizon
pns_node.h
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 <set>
29 #include <unordered_map>
30 
31 #include <core/optional.h>
32 
33 #include <geometry/shape.h>
34 #include <geometry/shape_line_chain.h>
35 #include <geometry/shape_index.h>
36 
37 #include "pns_item.h"
38 #include "pns_joint.h"
39 #include "pns_itemset.h"
40 
41 namespace PNS {
42 
43 class SEGMENT;
44 class LINE;
45 class SOLID;
46 class VIA;
47 class INDEX;
48 class ROUTER;
49 class NODE;
50 
58 {
59 public:
60  virtual ~RULE_RESOLVER() {}
61 
62  virtual int Clearance( const ITEM* aA, const ITEM* aB ) const = 0;
63  virtual int Clearance( int aNetCode ) const = 0;
64  virtual int DpCoupledNet( int aNet ) = 0;
65  virtual int DpNetPolarity( int aNet ) = 0;
66  virtual bool DpNetPair( ITEM* aItem, int& aNetP, int& aNetN ) = 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:
139  typedef OPT<OBSTACLE> OPT_OBSTACLE;
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 
167  RULE_RESOLVER* GetRuleResolver()
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 class PNS_HORIZON_PARENT_ITEM* aParent, int net);
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:
422  struct DEFAULT_OBSTACLE_VISITOR;
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 
480  NODE* m_parent;
481 
483  NODE* m_root;
484 
486  std::set<NODE*> m_children;
487 
489  std::unordered_set<ITEM*> m_override;
490 
492  int m_maxClearance;
493 
495  RULE_RESOLVER* m_ruleResolver;
496 
498  INDEX* m_index;
499 
501  int m_depth;
502 
503  std::unordered_set<ITEM*> m_garbageItems;
504 };
505 
506 }
507 
508 #endif
PNS::LINE
Definition: pns_line.h:60
PNS::NODE::NearestObstacle
OPT_OBSTACLE NearestObstacle(const LINE *aItem, int aKindMask=ITEM::ANY_T, const std::set< ITEM * > *aRestrictedSet=NULL)
Function NearestObstacle()
Definition: pns_node.cpp:303
PNS::OBSTACLE
Struct OBSTACLE.
Definition: pns_node.h:75
PNS::NODE::JointCount
int JointCount() const
Returns the number of joints
Definition: pns_node.h:173
PNS::ITEM::Layers
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:210
LAYER_RANGE
Class LAYER_RANGE.
Definition: pns_layerset.h:32
PNS::NODE::FindLinesBetweenJoints
int FindLinesBetweenJoints(JOINT &aA, JOINT &aB, std::vector< LINE > &aLines)
finds all lines between a pair of joints. Used by the loop removal procedure.
Definition: pns_node.cpp:935
PNS::NODE::FindLineEnds
void FindLineEnds(const LINE &aLine, JOINT &aA, JOINT &aB)
finds the joints corresponding to the ends of line aLine
Definition: pns_node.cpp:890
PNS::SEGMENT
Definition: pns_segment.h:38
PNS::NODE::Depth
int Depth() const
Returns the number of nodes in the inheritance chain (wrs to the root node)
Definition: pns_node.h:179
PNS::RULE_RESOLVER
Class RULE_RESOLVER.
Definition: pns_node.h:57
PNS::NODE::Overrides
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
PNS::NODE::Commit
void Commit(NODE *aNode)
Function Commit()
Definition: pns_node.cpp:1207
PNS::NODE::AssembleLine
const LINE AssembleLine(SEGMENT *aSeg, int *aOriginSegmentIndex=NULL, bool aStopAtLockedJoints=false)
Function AssembleLine()
Definition: pns_node.cpp:835
PNS::ITEM::Net
int Net() const
Function Net()
Definition: pns_item.h:180
VECTOR2< int >
PNS::VIA
Definition: pns_via.h:37
PNS::NODE::SetRuleResolver
void SetRuleResolver(RULE_RESOLVER *aFunc)
Assigns a clerance resolution function object
Definition: pns_node.h:162
PNS::OBSTACLE::m_distFirst
int m_distFirst
... and the distance thereof
Definition: pns_node.h:91
PNS::NODE::Branch
NODE * Branch()
Function Branch()
Definition: pns_node.cpp:107
PNS::OBSTACLE_VISITOR::m_override
const NODE * m_override
node that overrides root entries
Definition: pns_node.h:118
PNS::PNS_HORIZON_PARENT_ITEM
Definition: pns_horizon_iface.hpp:28
PNS::NODE::GetClearance
int GetClearance(const ITEM *aA, const ITEM *aB) const
Returns the expected clearance between items a and b.
Definition: pns_node.cpp:98
PNS::NODE::KillChildren
void KillChildren()
Destroys all child nodes. Applicable only to the root node.
Definition: pns_node.cpp:1228
PNS::NODE::FindJoint
JOINT * FindJoint(const VECTOR2I &aPos, int aLayer, int aNet)
Function FindJoint()
Definition: pns_node.cpp:969
PNS::NODE::SetMaxClearance
void SetMaxClearance(int aClearance)
Sets the worst-case clerance between any pair of items
Definition: pns_node.h:156
PNS::JOINT
Class JOINT.
Definition: pns_joint.h:43
PNS::NODE::Dump
void Dump(bool aLong=false)
Prints the contents and joints structure
Definition: pns_node.cpp:1083
PNS::NODE::CheckColliding
OPT_OBSTACLE CheckColliding(const ITEM *aItem, int aKindMask=ITEM::ANY_T)
Function CheckColliding()
Definition: pns_node.cpp:426
PNS::OBSTACLE_VISITOR::m_extraClearance
int m_extraClearance
additional clearance
Definition: pns_node.h:121
PNS::NODE::FindJoint
JOINT * FindJoint(const VECTOR2I &aPos, const ITEM *aItem)
Function FindJoint()
Definition: pns_node.h:376
PNS::OBSTACLE_VISITOR
Struct OBSTACLE_VISITOR.
Definition: pns_node.h:97
PNS::SOLID
Definition: pns_solid.h:35
PNS::OBSTACLE_VISITOR::m_item
const ITEM * m_item
the item we are looking for collisions with
Definition: pns_node.h:112
PNS::OBSTACLE::m_hull
SHAPE_LINE_CHAIN m_hull
Hull of the colliding m_item
Definition: pns_node.h:84
PNS::ITEM_SET
Definition: pns_itemset.h:39
PNS::ITEM
Class ITEM.
Definition: pns_item.h:54
PNS::NODE::Add
bool Add(std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant=false)
Function Add()
Definition: pns_node.cpp:595
PNS::OBSTACLE_VISITOR::m_node
const NODE * m_node
node we are searching in (either root or a branch)
Definition: pns_node.h:115
PNS::NODE::HitTest
const ITEM_SET HitTest(const VECTOR2I &aPoint) const
Function HitTest()
Definition: pns_node.cpp:505
PNS::NODE::Remove
void Remove(SOLID *aSolid)
Function Remove()
Definition: pns_node.cpp:730
PNS::NODE::Replace
void Replace(ITEM *aOldItem, std::unique_ptr< ITEM > aNewItem)
Function Replace()
Definition: pns_node.cpp:718
PNS::NODE::GetMaxClearance
int GetMaxClearance() const
Returns the pre-set worst case clearance between any pair of items
Definition: pns_node.h:150
PNS::NODE
Class NODE.
Definition: pns_node.h:136
SHAPE_LINE_CHAIN
Class SHAPE_LINE_CHAIN.
Definition: shape_line_chain.h:47
PNS::NODE::GetUpdatedItems
void GetUpdatedItems(ITEM_VECTOR &aRemoved, ITEM_VECTOR &aAdded)
Function GetUpdatedItems()
Definition: pns_node.cpp:1164
PNS::OBSTACLE::m_item
ITEM * m_item
Item found to be colliding with m_head
Definition: pns_node.h:81
PNS::OBSTACLE::m_ipFirst
VECTOR2I m_ipFirst
First and last intersection point between the head item and the hull of the colliding m_item
Definition: pns_node.h:88
PNS::NODE::QueryColliding
int QueryColliding(const ITEM *aItem, OBSTACLES &aObstacles, int aKindMask=ITEM::ANY_T, int aLimitCount=-1, bool aDifferentNetsOnly=true, int aForceClearance=-1)
Function QueryColliding()
Definition: pns_node.cpp:277
PNS::OBSTACLE::m_head
const ITEM * m_head
Item we search collisions with
Definition: pns_node.h:78