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 <boost/optional.hpp>
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 void OverrideClearance( bool aEnable, int aNetA = 0, int aNetB = 0, int aClearance = 0 ) = 0;
65  virtual void UseDpGap( bool aUseDpGap ) = 0;
66  virtual int DpCoupledNet( int aNet ) = 0;
67  virtual int DpNetPolarity( int aNet ) = 0;
68  virtual bool DpNetPair( ITEM* aItem, int& aNetP, int& aNetN ) = 0;
69 };
70 
77 struct OBSTACLE
78 {
80  const ITEM* m_head;
81 
84 
87 
90  VECTOR2I m_ipFirst, m_ipLast;
91 
93  int m_distFirst, m_distLast;
94 };
95 
100 
101 public:
102 
103  OBSTACLE_VISITOR( const ITEM* aItem );
104 
105  void SetWorld( const NODE* aNode, const NODE* aOverride = NULL );
106 
107  virtual bool operator()( ITEM* aCandidate ) = 0;
108 
109 protected:
110 
111  bool visit( ITEM* aCandidate );
112 
114  const ITEM* m_item;
115 
117  const NODE* m_node;
118 
120  const NODE* m_override;
121 
124 };
125 
138 class NODE
139 {
140 public:
141  typedef boost::optional<OBSTACLE> OPT_OBSTACLE;
142  typedef std::vector<ITEM*> ITEM_VECTOR;
143  typedef std::vector<OBSTACLE> OBSTACLES;
144 
145  NODE();
146  ~NODE();
147 
149  int GetClearance( const ITEM* aA, const ITEM* aB ) const;
150 
152  int GetMaxClearance() const
153  {
154  return m_maxClearance;
155  }
156 
158  void SetMaxClearance( int aClearance )
159  {
160  m_maxClearance = aClearance;
161  }
162 
165  {
166  m_ruleResolver = aFunc;
167  }
168 
169  RULE_RESOLVER* GetRuleResolver()
170  {
171  return m_ruleResolver;
172  }
173 
175  int JointCount() const
176  {
177  return m_joints.size();
178  }
179 
181  int Depth() const
182  {
183  return m_depth;
184  }
185 
196  int QueryColliding( const ITEM* aItem,
197  OBSTACLES& aObstacles,
198  int aKindMask = ITEM::ANY_T,
199  int aLimitCount = -1,
200  bool aDifferentNetsOnly = true,
201  int aForceClearance = -1 );
202 
203  int QueryColliding( const ITEM* aItem,
204  OBSTACLE_VISITOR& aVisitor
205  );
206 
216  OPT_OBSTACLE NearestObstacle( const LINE* aItem,
217  int aKindMask = ITEM::ANY_T,
218  const std::set<ITEM*>* aRestrictedSet = NULL );
219 
229  OPT_OBSTACLE CheckColliding( const ITEM* aItem,
230  int aKindMask = ITEM::ANY_T );
231 
232 
242  OPT_OBSTACLE CheckColliding( const ITEM_SET& aSet,
243  int aKindMask = ITEM::ANY_T );
244 
245 
256  bool CheckColliding( const ITEM* aItemA,
257  const ITEM* aItemB,
258  int aKindMask = ITEM::ANY_T,
259  int aForceClearance = -1 );
260 
268  const ITEM_SET HitTest( const VECTOR2I& aPoint ) const;
269 
278  void Add( std::unique_ptr< SEGMENT > aSegment, bool aAllowRedundant = false );
279  void Add( std::unique_ptr< SOLID > aSolid );
280  void Add( std::unique_ptr< VIA > aVia );
281 
282  void Add( LINE& aLine, bool aAllowRedundant = false );
283 
284 private:
285  void Add( std::unique_ptr< ITEM > aItem, bool aAllowRedundant = false );
286 
287 public:
293  void Remove( SOLID* aSolid );
294  void Remove( VIA* aVia );
295  void Remove( SEGMENT* aSegment );
296  void Remove( ITEM* aItem );
297 
298 public:
305  void Remove( LINE& aLine );
306 
314  void Replace( ITEM* aOldItem, std::unique_ptr< ITEM > aNewItem );
315  void Replace( LINE& aOldLine, LINE& aNewLine );
316 
325  NODE* Branch();
326 
336  const LINE AssembleLine( SEGMENT* aSeg, int* aOriginSegmentIndex = NULL,
337  bool aStopAtLockedJoints = false );
338 
340  void Dump( bool aLong = false );
341 
350  void GetUpdatedItems( ITEM_VECTOR& aRemoved, ITEM_VECTOR& aAdded );
351 
359  void Commit( NODE* aNode );
360 
367  JOINT* FindJoint( const VECTOR2I& aPos, int aLayer, int aNet );
368 
369  void LockJoint( const VECTOR2I& aPos, const ITEM* aItem, bool aLock );
370 
377  JOINT* FindJoint( const VECTOR2I& aPos, const ITEM* aItem )
378  {
379  return FindJoint( aPos, aItem->Layers().Start(), aItem->Net() );
380  }
381 
382 #if 0
383  void MapConnectivity( JOINT* aStart, std::vector<JOINT*> & aFoundJoints );
384 
385  ITEM* NearestUnconnectedItem( JOINT* aStart, int* aAnchor = NULL,
386  int aKindMask = ITEM::ANY_T);
387 
388 #endif
389 
391  int FindLinesBetweenJoints( JOINT& aA,
392  JOINT& aB,
393  std::vector<LINE>& aLines );
394 
396  void FindLineEnds( const LINE& aLine, JOINT& aA, JOINT& aB );
397 
399  void KillChildren();
400 
401  void AllItemsInNet( int aNet, std::set<ITEM*>& aItems );
402 
403  void ClearRanks( int aMarkerMask = MK_HEAD | MK_VIOLATION );
404 
405  int FindByMarker( int aMarker, ITEM_SET& aItems );
406  int RemoveByMarker( int aMarker );
407 
408  ITEM* FindItemByParent( const class PNS_HORIZON_PARENT_ITEM* aParent, int net);
409 
410  bool HasChildren() const
411  {
412  return !m_children.empty();
413  }
414 
417  bool Overrides( ITEM* aItem ) const
418  {
419  return m_override.find( aItem ) != m_override.end();
420  }
421 
422 private:
424  typedef std::unordered_multimap<JOINT::HASH_TAG, JOINT, JOINT::JOINT_TAG_HASH> JOINT_MAP;
425  typedef JOINT_MAP::value_type TagJointPair;
426 
428  NODE( const NODE& aB );
429  NODE& operator=( const NODE& aB );
430 
432  JOINT& touchJoint( const VECTOR2I& aPos,
433  const LAYER_RANGE& aLayers,
434  int aNet );
435 
437  void linkJoint( const VECTOR2I& aPos, const LAYER_RANGE& aLayers, int aNet, ITEM* aWhere );
438 
440  void unlinkJoint( const VECTOR2I& aPos, const LAYER_RANGE& aLayers, int aNet, ITEM* aWhere );
441 
443  void addSolid( SOLID* aSeg );
444  void addSegment( SEGMENT* aSeg );
445  void addVia( VIA* aVia );
446 
447  void removeLine( LINE& aLine );
448  void removeSolidIndex( SOLID* aSeg );
449  void removeSegmentIndex( SEGMENT* aSeg );
450  void removeViaIndex( VIA* aVia );
451 
452  void doRemove( ITEM* aItem );
453  void unlinkParent();
454  void releaseChildren();
455  void releaseGarbage();
456 
457  bool isRoot() const
458  {
459  return m_parent == NULL;
460  }
461 
462  SEGMENT* findRedundantSegment( const VECTOR2I& A, const VECTOR2I& B,
463  const LAYER_RANGE & lr, int aNet );
464  SEGMENT* findRedundantSegment( SEGMENT* aSeg );
465 
467  void followLine( SEGMENT* aCurrent,
468  bool aScanDirection,
469  int& aPos,
470  int aLimit,
471  VECTOR2I* aCorners,
472  SEGMENT** aSegments,
473  bool& aGuardHit,
474  bool aStopAtLockedJoints );
475 
478  JOINT_MAP m_joints;
479 
481  NODE* m_parent;
482 
484  NODE* m_root;
485 
487  std::set<NODE*> m_children;
488 
490  std::unordered_set<ITEM*> m_override;
491 
493  int m_maxClearance;
494 
496  RULE_RESOLVER* m_ruleResolver;
497 
499  INDEX* m_index;
500 
502  int m_depth;
503 
504  std::unordered_set<ITEM*> m_garbageItems;
505 };
506 
507 }
508 
509 #endif
Class ITEM.
Definition: pns_item.h:54
ITEM * m_item
Item found to be colliding with m_head
Definition: pns_node.h:83
const NODE * m_node
node we are searching in (either root or a branch)
Definition: pns_node.h:117
Class NODE.
Definition: pns_node.h:138
Definition: pns_itemset.h:39
int m_distFirst
... and the distance thereof
Definition: pns_node.h:93
int m_extraClearance
additional clearance
Definition: pns_node.h:123
int JointCount() const
Returns the number of joints
Definition: pns_node.h:175
Definition: pns_horizon_iface.hpp:26
Definition: pns_solid.h:35
Class RULE_RESOLVER.
Definition: pns_node.h:57
Definition: pns_line.h:60
int Depth() const
Returns the number of nodes in the inheritance chain (wrs to the root node)
Definition: pns_node.h:181
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:417
void SetRuleResolver(RULE_RESOLVER *aFunc)
Assigns a clerance resolution function object
Definition: pns_node.h:164
Definition: pns_node.cpp:180
Definition: pns_via.h:37
void SetMaxClearance(int aClearance)
Sets the worst-case clerance between any pair of items
Definition: pns_node.h:158
Class JOINT.
Definition: pns_joint.h:43
Struct OBSTACLE_VISITOR.
Definition: pns_node.h:99
int Net() const
Function Net()
Definition: pns_item.h:178
JOINT * FindJoint(const VECTOR2I &aPos, const ITEM *aItem)
Function FindJoint()
Definition: pns_node.h:377
const ITEM * m_item
the item we are looking for collisions with
Definition: pns_node.h:114
SHAPE_LINE_CHAIN m_hull
Hull of the colliding m_item
Definition: pns_node.h:86
Class SHAPE_LINE_CHAIN.
Definition: shape_line_chain.h:47
int GetMaxClearance() const
Returns the pre-set worst case clearance between any pair of items
Definition: pns_node.h:152
const ITEM * m_head
Item we search collisions with
Definition: pns_node.h:80
VECTOR2I m_ipFirst
First and last intersection point between the head item and the hull of the colliding m_item ...
Definition: pns_node.h:90
const NODE * m_override
node that overrides root entries
Definition: pns_node.h:120
Definition: pns_segment.h:38
Definition: pns_algo_base.cpp:26
Class INDEX.
Definition: pns_index.h:46
Class LAYER_RANGE.
Definition: pns_layerset.h:32
const LAYER_RANGE & Layers() const
Function Layers()
Definition: pns_item.h:208
Struct OBSTACLE.
Definition: pns_node.h:77