KiCad PCB EDA Suite
pns_joint.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_JOINT_H
24 #define __PNS_JOINT_H
25 
26 #include <vector>
27 
28 #include <math/vector2d.h>
29 
30 #include "pns_item.h"
31 #include "pns_segment.h"
32 #include "pns_itemset.h"
33 
34 namespace PNS {
35 
42 class JOINT : public ITEM
43 {
44 public:
46 
49  struct HASH_TAG
50  {
52  int net;
53  };
54 
56  {
57  std::size_t operator()( const JOINT::HASH_TAG& aP ) const
58  {
59  using std::size_t;
60  using std::hash;
61  using std::string;
62 
63  return ( (hash<int>()( aP.pos.x )
64  ^ (hash<int>()( aP.pos.y ) << 1) ) >> 1 )
65  ^ (hash<int>()( aP.net ) << 1);
66  }
67  };
68 
69  JOINT() :
70  ITEM( JOINT_T ), m_locked( false ) {}
71 
72  JOINT( const VECTOR2I& aPos, const LAYER_RANGE& aLayers, int aNet = -1 ) :
73  ITEM( JOINT_T )
74  {
75  m_tag.pos = aPos;
76  m_tag.net = aNet;
77  m_layers = aLayers;
78  m_locked = false;
79  }
80 
81  JOINT( const JOINT& aB ) :
82  ITEM( JOINT_T )
83  {
84  m_layers = aB.m_layers;
85  m_tag.pos = aB.m_tag.pos;
86  m_tag.net = aB.m_tag.net;
88  m_layers = aB.m_layers;
89  m_locked = aB.m_locked;
90  }
91 
92  ITEM* Clone( ) const override
93  {
94  assert( false );
95  return NULL;
96  }
97 
100  bool IsLineCorner() const
101  {
102  if( m_linkedItems.Size() != 2 || m_linkedItems.Count( SEGMENT_T | ARC_T ) != 2 )
103  return false;
104 
105  auto seg1 = static_cast<LINKED_ITEM*>( m_linkedItems[0] );
106  auto seg2 = static_cast<LINKED_ITEM*>( m_linkedItems[1] );
107 
108  // joints between segments of different widths are not considered trivial.
109  return seg1->Width() == seg2->Width();
110  }
111 
112  bool IsNonFanoutVia() const
113  {
114  int vias = m_linkedItems.Count( VIA_T );
115  int segs = m_linkedItems.Count( SEGMENT_T );
116  segs += m_linkedItems.Count( ARC_T );
117 
118  return ( m_linkedItems.Size() == 3 && vias == 1 && segs == 2 );
119  }
120 
121  bool IsStitchingVia() const
122  {
123  return ( m_linkedItems.Size() == 1 && m_linkedItems.Count( VIA_T ) == 1 );
124  }
125 
126  bool IsTraceWidthChange() const
127  {
128  if( m_linkedItems.Size() != 2 )
129  return false;
130 
131  if( m_linkedItems.Count( SEGMENT_T ) != 2)
132  return false;
133 
134  SEGMENT* seg1 = static_cast<SEGMENT*>( m_linkedItems[0] );
135  SEGMENT* seg2 = static_cast<SEGMENT*>( m_linkedItems[1] );
136 
137  return seg1->Width() != seg2->Width();
138  }
139 
141  void Link( ITEM* aItem )
142  {
143  if( m_linkedItems.Contains( aItem ) )
144  return;
145 
146  m_linkedItems.Add( aItem );
147  }
148 
151  bool Unlink( ITEM* aItem )
152  {
153  m_linkedItems.Erase( aItem );
154  return m_linkedItems.Size() == 0;
155  }
156 
159  LINKED_ITEM* NextSegment( ITEM* aCurrent ) const
160  {
161  if( !IsLineCorner() )
162  return NULL;
163 
164  return static_cast<LINKED_ITEM*>( m_linkedItems[m_linkedItems[0] == aCurrent ? 1 : 0] );
165  }
166 
167  VIA* Via()
168  {
169  for( ITEM* item : m_linkedItems.Items() )
170  {
171  if( item->OfKind( VIA_T ) )
172  return static_cast<VIA*>( item );
173  }
174 
175  return NULL;
176  }
177 
178 
180  const HASH_TAG& Tag() const
181  {
182  return m_tag;
183  }
184 
185  const VECTOR2I& Pos() const
186  {
187  return m_tag.pos;
188  }
189 
190  int Net() const
191  {
192  return m_tag.net;
193  }
194 
195  const LINKED_ITEMS& LinkList() const
196  {
197  return m_linkedItems.CItems();
198  }
199 
200  const ITEM_SET& CLinks() const
201  {
202  return m_linkedItems;
203  }
204 
206  {
207  return m_linkedItems;
208  }
209 
210  int LinkCount( int aMask = -1 ) const
211  {
212  return m_linkedItems.Count( aMask );
213  }
214 
215  void Dump() const;
216 
217  bool operator==( const JOINT& rhs ) const
218  {
219  return m_tag.pos == rhs.m_tag.pos && m_tag.net == rhs.m_tag.net;
220  }
221 
222  void Merge( const JOINT& aJoint )
223  {
224  if( !Overlaps( aJoint ) )
225  return;
226 
227  m_layers.Merge( aJoint.m_layers );
228 
229  if( aJoint.IsLocked() )
230  m_locked = true;
231 
232  for( ITEM* item : aJoint.LinkList() )
233  {
234  m_linkedItems.Add( item );
235  }
236  }
237 
238  bool Overlaps( const JOINT& rhs ) const
239  {
240  return m_tag.pos == rhs.m_tag.pos &&
241  m_tag.net == rhs.m_tag.net && m_layers.Overlaps( rhs.m_layers );
242  }
243 
244  void Lock( bool aLock = true )
245  {
246  m_locked = aLock;
247  }
248 
249  bool IsLocked() const
250  {
251  return m_locked;
252  }
253 
254 private:
257 
260 
262  bool m_locked;
263 };
264 
265 inline bool operator==( JOINT::HASH_TAG const& aP1, JOINT::HASH_TAG const& aP2 )
266 {
267  return aP1.pos == aP2.pos && aP1.net == aP2.net;
268 }
269 
270 }
271 
272 #endif // __PNS_JOINT_H
Base class for PNS router board items.
Definition: pns_item.h:55
int Count(int aKindMask=-1) const
Definition: pns_itemset.h:117
bool IsTraceWidthChange() const
Link the joint to a given board item (when it's added to the NODE).
Definition: pns_joint.h:126
bool Contains(ITEM *aItem) const
Definition: pns_itemset.h:191
bool Unlink(ITEM *aItem)
For trivial joints, return the segment adjacent to (aCurrent).
Definition: pns_joint.h:151
int Width() const override
Definition: pns_segment.h:79
LINKED_ITEM * NextSegment(ITEM *aCurrent) const
Definition: pns_joint.h:159
ENTRIES & Items()
Definition: pns_itemset.h:138
bool IsLocked() const
Definition: pns_joint.h:249
bool Overlaps(const LAYER_RANGE &aOther) const
Definition: pns_layerset.h:67
LAYER_RANGE m_layers
Definition: pns_item.h:239
ITEM_SET m_linkedItems
locked (non-movable) flag
Definition: pns_joint.h:259
void Lock(bool aLock=true)
Definition: pns_joint.h:244
std::vector< ENTRY > ENTRIES
Definition: pns_itemset.h:96
int Size() const
Definition: pns_itemset.h:163
void Erase(ITEM *aItem)
Definition: pns_itemset.h:197
int Net() const
Definition: pns_joint.h:190
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:32
ITEM_SET & Links()
Definition: pns_joint.h:205
JOINT(const VECTOR2I &aPos, const LAYER_RANGE &aLayers, int aNet=-1)
Definition: pns_joint.h:72
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
bool m_locked
Definition: pns_joint.h:262
bool IsLineCorner() const
Definition: pns_joint.h:100
#define NULL
void Merge(const LAYER_RANGE &aOther)
Shortcut for comparisons/overlap tests.
Definition: pns_layerset.h:92
const HASH_TAG & Tag() const
trivial accessors
Definition: pns_joint.h:180
ITEM_SET::ENTRIES LINKED_ITEMS
Joints are hashed by their position, layers and net.
Definition: pns_joint.h:45
const LINKED_ITEMS & LinkList() const
Definition: pns_joint.h:195
const ITEM_SET & CLinks() const
Definition: pns_joint.h:200
bool operator==(JOINT::HASH_TAG const &aP1, JOINT::HASH_TAG const &aP2)
Definition: pns_joint.h:265
bool operator==(const JOINT &rhs) const
Definition: pns_joint.h:217
std::size_t operator()(const JOINT::HASH_TAG &aP) const
Definition: pns_joint.h:57
JOINT(const JOINT &aB)
Definition: pns_joint.h:81
VIA * Via()
Definition: pns_joint.h:167
void Dump() const
Definition: pns_node.cpp:1115
HASH_TAG m_tag
< hash tag for unordered_multimap
Definition: pns_joint.h:256
bool Overlaps(const JOINT &rhs) const
Definition: pns_joint.h:238
const ENTRIES & CItems() const
Definition: pns_itemset.h:139
void Merge(const JOINT &aJoint)
Definition: pns_joint.h:222
bool IsStitchingVia() const
Definition: pns_joint.h:121
ITEM * Clone() const override
Return true if the joint is a trivial line corner, connecting two segments of the same net,...
Definition: pns_joint.h:92
int LinkCount(int aMask=-1) const
Definition: pns_joint.h:210
const VECTOR2I & Pos() const
Definition: pns_joint.h:185
void Link(ITEM *aItem)
Unlink a given board item from the joint (upon its removal from a NODE)
Definition: pns_joint.h:141
Push and Shove diff pair dimensions (gap) settings dialog.
bool IsNonFanoutVia() const
Definition: pns_joint.h:112
Represent a contiguous set of PCB layers.
Definition: pns_layerset.h:31