KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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
20 * along with this program. If not, see <https://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:
52
54 {
55 std::size_t operator()( const JOINT::HASH_TAG& aP ) const
56 {
57 using std::size_t;
58 using std::hash;
59 using std::string;
60
61 return ( (hash<int>()( aP.pos.x )
62 ^ (hash<int>()( aP.pos.y ) << 1) ) >> 1 )
63 ^ (hash<void*>()( aP.net ) << 1);
64 }
65 };
66
68 ITEM( JOINT_T ), m_tag(), m_locked( false ) {}
69
70 JOINT( const VECTOR2I& aPos, const PNS_LAYER_RANGE& aLayers, NET_HANDLE aNet = nullptr ) :
71 ITEM( JOINT_T )
72 {
73 m_tag.pos = aPos;
74 m_tag.net = aNet;
75 m_layers = aLayers;
76 m_locked = false;
77 }
78
79 JOINT( const JOINT& aB ) :
80 ITEM( JOINT_T )
81 {
82 m_layers = aB.m_layers;
83 m_tag.pos = aB.m_tag.pos;
84 m_tag.net = aB.m_tag.net;
86 m_layers = aB.m_layers;
87 m_locked = aB.m_locked;
88 }
89
90 ITEM* Clone( ) const override
91 {
92 assert( false );
93 return nullptr;
94 }
95
102 bool IsLineCorner( bool aAllowLockedSegs = false ) const
103 {
104 if( m_linkedItems.Size() == 2 && m_linkedItems.Count( SEGMENT_T | ARC_T ) == 2 )
105 {
106 LINKED_ITEM* seg1 = static_cast<LINKED_ITEM*>( m_linkedItems[0] );
107 LINKED_ITEM* seg2 = static_cast<LINKED_ITEM*>( m_linkedItems[1] );
108
109 if( !aAllowLockedSegs && ( seg1->IsLocked() || seg2->IsLocked() ) )
110 return false;
111
112 // joints between segments of different widths are not considered trivial.
113 return seg1->Width() == seg2->Width();
114 }
115 else if( m_linkedItems.Size() > 2 && m_linkedItems.Count( SEGMENT_T | ARC_T ) == 2 )
116 {
117 if( !aAllowLockedSegs )
118 return false;
119
120 // There will be multiple VVIAs on joints between two locked segments, because we
121 // naively add a VVIA to each end of a locked segment.
122 const LINKED_ITEM* seg1 = nullptr;
123 const LINKED_ITEM* seg2 = nullptr;
124
125 for( const ITEM* item : m_linkedItems.CItems() )
126 {
127 if( item->IsVirtual() )
128 continue;
129
130 if( item->Kind() == SEGMENT_T || item->Kind() == ARC_T )
131 {
132 if( !seg1 )
133 seg1 = static_cast<const LINKED_ITEM*>( item );
134 else
135 seg2 = static_cast<const LINKED_ITEM*>( item );
136 }
137 else
138 {
139 return false;
140 }
141 }
142
143 if( seg1 && seg2 )
144 return seg1->Width() == seg2->Width();
145 }
146
147 return false;
148 }
149
150 bool IsNonFanoutVia() const
151 {
152 int vias = 0;
153 int segs = 0;
154 int realItems = 0;
155
156 for( const ITEM* item : m_linkedItems.CItems() )
157 {
158 if( item->IsVirtual() )
159 continue;
160
161 if( item->Kind() == VIA_T )
162 vias++;
163 else if( item->Kind() == SEGMENT_T || item->Kind() == ARC_T )
164 segs++;
165
166 realItems++;
167 }
168
169 return ( realItems == 3 && vias == 1 && segs == 2 );
170 }
171
172 bool IsStitchingVia() const
173 {
174 return ( m_linkedItems.Size() == 1 && m_linkedItems.Count( VIA_T ) == 1 );
175 }
176
177 bool IsTrivialEndpoint() const
178 {
179 // fixme: Arcs & trivial endpoint vias
180 return m_linkedItems.Size() == 1 && m_linkedItems.Count( SEGMENT_T ) == 1;
181 }
182
184 {
185 if( m_linkedItems.Count( SEGMENT_T ) != 2 )
186 return false;
187
188 const LINKED_ITEM* seg1 = nullptr;
189 const LINKED_ITEM* seg2 = nullptr;
190
191 for( const ITEM* item : m_linkedItems.CItems() )
192 {
193 if( item->IsVirtual() )
194 continue;
195
196 if( item->Kind() == VIA_T )
197 {
198 return false;
199 }
200 else if( item->Kind() == SEGMENT_T || item->Kind() == ARC_T )
201 {
202 if( !seg1 )
203 seg1 = static_cast<const LINKED_ITEM*>( item );
204 else
205 seg2 = static_cast<const LINKED_ITEM*>( item );
206 }
207 }
208
209 wxCHECK( seg1 && seg2, false );
210
211 return seg1->Width() != seg2->Width();
212 }
213
215 void Link( ITEM* aItem )
216 {
217 if( m_linkedItems.Contains( aItem ) )
218 return;
219
220 m_linkedItems.Add( aItem );
221 }
222
228 bool Unlink( ITEM* aItem )
229 {
230 m_linkedItems.Erase( aItem );
231
232 if( m_linkedItems.Size() == 0 )
234
235 return m_linkedItems.Size() == 0;
236 }
237
240 LINKED_ITEM* NextSegment( LINKED_ITEM* aCurrent, bool aAllowLockedSegs = false ) const
241 {
242 const std::vector<ITEM*>& citems = m_linkedItems.CItems();
243 const size_t size = citems.size();
244
245 LINKED_ITEM* otherItem = nullptr;
246
247 for( size_t i = 0; i < size; i++ )
248 {
249 ITEM* item = m_linkedItems[i];
250
251 if( item != aCurrent )
252 {
253 if ( item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
254 {
255 if ( item->Net() == aCurrent->Net() && item->Layers().Overlaps( aCurrent->Layers() ) )
256 {
257 if( otherItem )
258 return nullptr;
259
260 if( !item->IsLocked() || aAllowLockedSegs )
261 otherItem = static_cast<LINKED_ITEM*>( item );
262 }
263 }
264 else if ( item->OfKind( ITEM::SOLID_T | ITEM::VIA_T ) )
265 {
266 if( item->Kind() == ITEM::VIA_T && item->IsVirtual() && aAllowLockedSegs )
267 {
268 // Virtual via will be added at the joint between an unlocked and locked seg
269 continue;
270 }
271
272 return nullptr;
273 }
274 }
275 }
276
277 return otherItem;
278 }
279
280 VIA* Via() const
281 {
282 for( ITEM* item : m_linkedItems.CItems() )
283 {
284 if( item->OfKind( VIA_T ) )
285 return static_cast<VIA*>( item ); // fixme: const correctness
286 }
287
288 return nullptr;
289 }
290
292 const HASH_TAG& Tag() const
293 {
294 return m_tag;
295 }
296
297 const VECTOR2I& Pos() const
298 {
299 return m_tag.pos;
300 }
301
302 NET_HANDLE Net() const override
303 {
304 return m_tag.net;
305 }
306
307 const std::vector<ITEM*>& LinkList() const
308 {
309 return m_linkedItems.CItems();
310 }
311
312 const ITEM_SET& CLinks() const
313 {
314 return m_linkedItems;
315 }
316
318 {
319 return m_linkedItems;
320 }
321
322 int LinkCount( int aMask = -1 ) const
323 {
324 return m_linkedItems.Count( aMask );
325 }
326
327 void Dump() const;
328
329 bool operator==( const JOINT& rhs ) const
330 {
331 return m_tag.pos == rhs.m_tag.pos && m_tag.net == rhs.m_tag.net;
332 }
333
334 void Merge( const JOINT& aJoint )
335 {
336 if( !Overlaps( aJoint ) )
337 return;
338
339 m_layers.Merge( aJoint.m_layers );
340
341 if( aJoint.IsLocked() )
342 m_locked = true;
343
344 for( ITEM* item : aJoint.LinkList() )
345 {
346 m_linkedItems.Add( item );
347 }
348 }
349
350 bool Overlaps( const JOINT& rhs ) const
351 {
352 return m_tag.pos == rhs.m_tag.pos &&
353 m_tag.net == rhs.m_tag.net && m_layers.Overlaps( rhs.m_layers );
354 }
355
356 void Lock( bool aLock = true )
357 {
358 m_locked = aLock;
359 }
360
361 bool IsLocked() const
362 {
363 return m_locked;
364 }
365
366private:
369
372
375};
376
377inline bool operator==( JOINT::HASH_TAG const& aP1, JOINT::HASH_TAG const& aP2 )
378{
379 return aP1.pos == aP2.pos && aP1.net == aP2.net;
380}
381
382}
383
384#endif // __PNS_JOINT_H
const PNS_LAYER_RANGE & Layers() const
Definition pns_item.h:212
ITEM(PnsKind aKind)
Definition pns_item.h:116
virtual NET_HANDLE Net() const
Definition pns_item.h:210
PNS_LAYER_RANGE m_layers
Definition pns_item.h:327
PnsKind Kind() const
Return the type (kind) of the item.
Definition pns_item.h:173
bool OfKind(int aKindMask) const
Definition pns_item.h:181
bool IsVirtual() const
Definition pns_item.h:299
bool IsLocked() const
Definition pns_item.h:282
const std::vector< ITEM * > & LinkList() const
Definition pns_joint.h:307
NET_HANDLE Net() const override
Definition pns_joint.h:302
ITEM_SET m_linkedItems
list of items linked to this joint
Definition pns_joint.h:371
VIA * Via() const
Definition pns_joint.h:280
bool operator==(const JOINT &rhs) const
Definition pns_joint.h:329
int LinkCount(int aMask=-1) const
Definition pns_joint.h:322
ITEM_SET & Links()
Definition pns_joint.h:317
bool IsTrivialEndpoint() const
Definition pns_joint.h:177
bool IsLineCorner(bool aAllowLockedSegs=false) const
Check if a joint connects two segments of the same net, layer, and width.
Definition pns_joint.h:102
void Lock(bool aLock=true)
Definition pns_joint.h:356
const HASH_TAG & Tag() const
trivial accessors
Definition pns_joint.h:292
void Link(ITEM *aItem)
Link the joint to a given board item (when it's added to the NODE).
Definition pns_joint.h:215
bool IsNonFanoutVia() const
Definition pns_joint.h:150
ITEM * Clone() const override
Return a deep copy of the item.
Definition pns_joint.h:90
bool IsTraceWidthChange() const
Definition pns_joint.h:183
void Dump() const
LINKED_ITEM * NextSegment(LINKED_ITEM *aCurrent, bool aAllowLockedSegs=false) const
For trivial joints, return the segment adjacent to (aCurrent).
Definition pns_joint.h:240
const ITEM_SET & CLinks() const
Definition pns_joint.h:312
JOINT(const JOINT &aB)
Definition pns_joint.h:79
HASH_TAG m_tag
hash tag for unordered_multimap
Definition pns_joint.h:368
bool IsLocked() const
Definition pns_joint.h:361
bool Overlaps(const JOINT &rhs) const
Definition pns_joint.h:350
bool IsStitchingVia() const
Definition pns_joint.h:172
void Merge(const JOINT &aJoint)
Definition pns_joint.h:334
bool Unlink(ITEM *aItem)
Unlink a given board item from the joint (upon its removal from a NODE)
Definition pns_joint.h:228
bool m_locked
locked (non-movable) flag
Definition pns_joint.h:374
const VECTOR2I & Pos() const
Definition pns_joint.h:297
JOINT(const VECTOR2I &aPos, const PNS_LAYER_RANGE &aLayers, NET_HANDLE aNet=nullptr)
Definition pns_joint.h:70
virtual int Width() const
Represent a contiguous set of PCB layers.
bool Overlaps(const PNS_LAYER_RANGE &aOther) const
Push and Shove diff pair dimensions (gap) settings dialog.
void * NET_HANDLE
Definition pns_item.h:55
bool operator==(JOINT::HASH_TAG const &aP1, JOINT::HASH_TAG const &aP2)
Definition pns_joint.h:377
Joints are hashed by their position, layers and net.
Definition pns_joint.h:48
std::size_t operator()(const JOINT::HASH_TAG &aP) const
Definition pns_joint.h:55
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708