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 <[email protected]>
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
34namespace PNS {
35
42class JOINT : public ITEM
43{
44public:
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
70 ITEM( JOINT_T ), m_tag(), 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 nullptr;
96 }
97
103 bool IsLineCorner( bool aAllowLockedSegs = false ) const
104 {
105 if( m_linkedItems.Size() == 2 && m_linkedItems.Count( SEGMENT_T | ARC_T ) == 2 )
106 {
107 LINKED_ITEM* seg1 = static_cast<LINKED_ITEM*>( m_linkedItems[0] );
108 LINKED_ITEM* seg2 = static_cast<LINKED_ITEM*>( m_linkedItems[1] );
109
110 // joints between segments of different widths are not considered trivial.
111 return seg1->Width() == seg2->Width();
112 }
113 else if( m_linkedItems.Size() > 2 && m_linkedItems.Count( SEGMENT_T | ARC_T ) == 2 )
114 {
115 if( !aAllowLockedSegs )
116 {
117 return false;
118 }
119 // There will be multiple VVIAs on joints between two locked segments, because we
120 // naively add a VVIA to each end of a locked segment.
123 {
124 const LINKED_ITEM* seg1 = nullptr;
125 const LINKED_ITEM* seg2 = nullptr;
126 const VIA* via = nullptr;
127 bool hasNonVirtualVia = false;
128
129 for( const ITEM* item : m_linkedItems.CItems() )
130 {
131 if( item->Kind() == VIA_T )
132 {
133 via = static_cast<const VIA*>( item );
134
135 hasNonVirtualVia = !via->IsVirtual();
136 }
137 else if( item->Kind() == SEGMENT_T || item->Kind() == ARC_T )
138 {
139 if( !seg1 )
140 seg1 = static_cast<const LINKED_ITEM*>( item );
141 else
142 seg2 = static_cast<const LINKED_ITEM*>( item );
143 }
144 }
145
146 if( !via || hasNonVirtualVia )
147 return false;
148
149 assert ( seg1 && seg2 );
150
151 return seg1->Width() == seg2->Width();
152 }
153 }
154
155 return false;
156 }
157
158 bool IsNonFanoutVia() const
159 {
160 int vias = m_linkedItems.Count( VIA_T );
161 int segs = m_linkedItems.Count( SEGMENT_T );
162 segs += m_linkedItems.Count( ARC_T );
163
164 return ( m_linkedItems.Size() == 3 && vias == 1 && segs == 2 );
165 }
166
167 bool IsStitchingVia() const
168 {
169 return ( m_linkedItems.Size() == 1 && m_linkedItems.Count( VIA_T ) == 1 );
170 }
171
173 {
174 if( m_linkedItems.Size() != 2 )
175 return false;
176
177 if( m_linkedItems.Count( SEGMENT_T ) != 2)
178 return false;
179
180 SEGMENT* seg1 = static_cast<SEGMENT*>( m_linkedItems[0] );
181 SEGMENT* seg2 = static_cast<SEGMENT*>( m_linkedItems[1] );
182
183 return seg1->Width() != seg2->Width();
184 }
185
187 void Link( ITEM* aItem )
188 {
189 if( m_linkedItems.Contains( aItem ) )
190 return;
191
192 m_linkedItems.Add( aItem );
193 }
194
197 bool Unlink( ITEM* aItem )
198 {
199 m_linkedItems.Erase( aItem );
200 return m_linkedItems.Size() == 0;
201 }
202
205 LINKED_ITEM* NextSegment( ITEM* aCurrent, bool aAllowLockedSegs = false ) const
206 {
207 if( !IsLineCorner( aAllowLockedSegs ) )
208 return nullptr;
209
210 return static_cast<LINKED_ITEM*>( m_linkedItems[m_linkedItems[0] == aCurrent ? 1 : 0] );
211 }
212
214 {
215 for( ITEM* item : m_linkedItems.Items() )
216 {
217 if( item->OfKind( VIA_T ) )
218 return static_cast<VIA*>( item );
219 }
220
221 return nullptr;
222 }
223
224
226 const HASH_TAG& Tag() const
227 {
228 return m_tag;
229 }
230
231 const VECTOR2I& Pos() const
232 {
233 return m_tag.pos;
234 }
235
236 int Net() const
237 {
238 return m_tag.net;
239 }
240
241 const LINKED_ITEMS& LinkList() const
242 {
243 return m_linkedItems.CItems();
244 }
245
246 const ITEM_SET& CLinks() const
247 {
248 return m_linkedItems;
249 }
250
252 {
253 return m_linkedItems;
254 }
255
256 int LinkCount( int aMask = -1 ) const
257 {
258 return m_linkedItems.Count( aMask );
259 }
260
261 void Dump() const;
262
263 bool operator==( const JOINT& rhs ) const
264 {
265 return m_tag.pos == rhs.m_tag.pos && m_tag.net == rhs.m_tag.net;
266 }
267
268 void Merge( const JOINT& aJoint )
269 {
270 if( !Overlaps( aJoint ) )
271 return;
272
273 m_layers.Merge( aJoint.m_layers );
274
275 if( aJoint.IsLocked() )
276 m_locked = true;
277
278 for( ITEM* item : aJoint.LinkList() )
279 {
280 m_linkedItems.Add( item );
281 }
282 }
283
284 bool Overlaps( const JOINT& rhs ) const
285 {
286 return m_tag.pos == rhs.m_tag.pos &&
287 m_tag.net == rhs.m_tag.net && m_layers.Overlaps( rhs.m_layers );
288 }
289
290 void Lock( bool aLock = true )
291 {
292 m_locked = aLock;
293 }
294
295 bool IsLocked() const
296 {
297 return m_locked;
298 }
299
300private:
303
306
309};
310
311inline bool operator==( JOINT::HASH_TAG const& aP1, JOINT::HASH_TAG const& aP2 )
312{
313 return aP1.pos == aP2.pos && aP1.net == aP2.net;
314}
315
316}
317
318#endif // __PNS_JOINT_H
Represent a contiguous set of PCB layers.
Definition: pns_layerset.h:32
void Merge(const LAYER_RANGE &aOther)
Shortcut for comparisons/overlap tests.
Definition: pns_layerset.h:92
bool Overlaps(const LAYER_RANGE &aOther) const
Definition: pns_layerset.h:67
int Size() const
Definition: pns_itemset.h:160
int Count(int aKindMask=-1) const
Definition: pns_itemset.h:114
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:32
bool Contains(ITEM *aItem) const
Definition: pns_itemset.h:193
ENTRIES & Items()
Definition: pns_itemset.h:135
std::vector< ENTRY > ENTRIES
Definition: pns_itemset.h:93
const ENTRIES & CItems() const
Definition: pns_itemset.h:136
void Erase(ITEM *aItem)
Definition: pns_itemset.h:199
Base class for PNS router board items.
Definition: pns_item.h:56
LAYER_RANGE m_layers
Definition: pns_item.h:256
@ SEGMENT_T
Definition: pns_item.h:66
@ JOINT_T
Definition: pns_item.h:65
A 2D point on a given set of layers and belonging to a certain net, that links together a number of b...
Definition: pns_joint.h:43
ITEM_SET m_linkedItems
locked (non-movable) flag
Definition: pns_joint.h:305
VIA * Via()
Definition: pns_joint.h:213
bool operator==(const JOINT &rhs) const
Definition: pns_joint.h:263
int LinkCount(int aMask=-1) const
Definition: pns_joint.h:256
const LINKED_ITEMS & LinkList() const
Definition: pns_joint.h:241
ITEM_SET & Links()
Definition: pns_joint.h:251
ITEM_SET::ENTRIES LINKED_ITEMS
Joints are hashed by their position, layers and net.
Definition: pns_joint.h:45
LINKED_ITEM * NextSegment(ITEM *aCurrent, bool aAllowLockedSegs=false) const
Definition: pns_joint.h:205
bool IsLineCorner(bool aAllowLockedSegs=false) const
Checks if a joint connects two segments of the same net, layer, and width.
Definition: pns_joint.h:103
void Lock(bool aLock=true)
Definition: pns_joint.h:290
JOINT(const VECTOR2I &aPos, const LAYER_RANGE &aLayers, int aNet=-1)
Definition: pns_joint.h:72
const HASH_TAG & Tag() const
trivial accessors
Definition: pns_joint.h:226
void Link(ITEM *aItem)
Unlink a given board item from the joint (upon its removal from a NODE)
Definition: pns_joint.h:187
int Net() const
Definition: pns_joint.h:236
bool IsNonFanoutVia() const
Definition: pns_joint.h:158
ITEM * Clone() const override
Return a deep copy of the item.
Definition: pns_joint.h:92
bool IsTraceWidthChange() const
Link the joint to a given board item (when it's added to the NODE).
Definition: pns_joint.h:172
void Dump() const
Definition: pns_node.cpp:1269
const ITEM_SET & CLinks() const
Definition: pns_joint.h:246
JOINT(const JOINT &aB)
Definition: pns_joint.h:81
HASH_TAG m_tag
< hash tag for unordered_multimap
Definition: pns_joint.h:302
bool IsLocked() const
Definition: pns_joint.h:295
bool Overlaps(const JOINT &rhs) const
Definition: pns_joint.h:284
bool IsStitchingVia() const
Definition: pns_joint.h:167
void Merge(const JOINT &aJoint)
Definition: pns_joint.h:268
bool Unlink(ITEM *aItem)
For trivial joints, return the segment adjacent to (aCurrent).
Definition: pns_joint.h:197
bool m_locked
Definition: pns_joint.h:308
const VECTOR2I & Pos() const
Definition: pns_joint.h:231
virtual int Width() const
int Width() const override
Definition: pns_segment.h:79
Push and Shove diff pair dimensions (gap) settings dialog.
bool operator==(JOINT::HASH_TAG const &aP1, JOINT::HASH_TAG const &aP2)
Definition: pns_joint.h:311
std::size_t operator()(const JOINT::HASH_TAG &aP) const
Definition: pns_joint.h:57