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 (C) 2016-2023 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:
47 struct HASH_TAG
48 {
51 };
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
101 bool IsLineCorner( bool aAllowLockedSegs = false ) const
102 {
103 if( m_linkedItems.Size() == 2 && m_linkedItems.Count( SEGMENT_T | ARC_T ) == 2 )
104 {
105 LINKED_ITEM* seg1 = static_cast<LINKED_ITEM*>( m_linkedItems[0] );
106 LINKED_ITEM* seg2 = static_cast<LINKED_ITEM*>( m_linkedItems[1] );
107
108 if( !aAllowLockedSegs && ( seg1->IsLocked() || seg2->IsLocked() ) )
109 return false;
110
111 // joints between segments of different widths are not considered trivial.
112 return seg1->Width() == seg2->Width();
113 }
114 else if( m_linkedItems.Size() > 2 && m_linkedItems.Count( SEGMENT_T | ARC_T ) == 2 )
115 {
116 if( !aAllowLockedSegs )
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.
121 const LINKED_ITEM* seg1 = nullptr;
122 const LINKED_ITEM* seg2 = nullptr;
123
124 for( const ITEM* item : m_linkedItems.CItems() )
125 {
126 if( item->IsVirtual() )
127 continue;
128
129 if( item->Kind() == SEGMENT_T || item->Kind() == ARC_T )
130 {
131 if( !seg1 )
132 seg1 = static_cast<const LINKED_ITEM*>( item );
133 else
134 seg2 = static_cast<const LINKED_ITEM*>( item );
135 }
136 else
137 {
138 return false;
139 }
140 }
141
142 if( seg1 && seg2 )
143 return seg1->Width() == seg2->Width();
144 }
145
146 return false;
147 }
148
149 bool IsNonFanoutVia() const
150 {
151 int vias = 0;
152 int segs = 0;
153 int realItems = 0;
154
155 for( const ITEM* item : m_linkedItems.CItems() )
156 {
157 if( item->IsVirtual() )
158 continue;
159
160 if( item->Kind() == VIA_T )
161 vias++;
162 else if( item->Kind() == SEGMENT_T || item->Kind() == ARC_T )
163 segs++;
164
165 realItems++;
166 }
167
168 return ( realItems == 3 && vias == 1 && segs == 2 );
169 }
170
171 bool IsStitchingVia() const
172 {
173 return ( m_linkedItems.Size() == 1 && m_linkedItems.Count( VIA_T ) == 1 );
174 }
175
176 bool IsTrivialEndpoint() const
177 {
178 // fixme: Arcs & trivial endpoint vias
179 return m_linkedItems.Size() == 1 && m_linkedItems.Count( SEGMENT_T ) == 1;
180 }
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
225 bool Unlink( ITEM* aItem )
226 {
227 m_linkedItems.Erase( aItem );
228 if( m_linkedItems.Size() == 0 )
230 return m_linkedItems.Size() == 0;
231 }
232
235 LINKED_ITEM* NextSegment( LINKED_ITEM* aCurrent, bool aAllowLockedSegs = false ) const
236 {
237 const std::vector<ITEM*>& citems = m_linkedItems.CItems();
238 const size_t size = citems.size();
239
240 LINKED_ITEM* otherItem = nullptr;
241
242 for( size_t i = 0; i < size; i++ )
243 {
244 ITEM* item = m_linkedItems[i];
245
246 if( item != aCurrent )
247 {
248 if ( item->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
249 {
250 if ( item->Net() == aCurrent->Net() && item->Layers().Overlaps( aCurrent->Layers() ) )
251 {
252 if( otherItem )
253 return nullptr;
254
255 if( !item->IsLocked() || aAllowLockedSegs )
256 otherItem = static_cast<LINKED_ITEM*>( item );
257 }
258 }
259 else if ( item->OfKind( ITEM::SOLID_T | ITEM::VIA_T ) )
260 {
261 if( item->Kind() == ITEM::VIA_T && item->IsVirtual() && aAllowLockedSegs )
262 {
263 // Virtual via will be added at the joint between an unlocked and locked seg
264 continue;
265 }
266
267 return nullptr;
268 }
269 }
270 }
271
272 return otherItem;
273 }
274
275 VIA* Via() const
276 {
277 for( ITEM* item : m_linkedItems.CItems() )
278 {
279 if( item->OfKind( VIA_T ) )
280 return static_cast<VIA*>( item ); // fixme: const correctness
281 }
282
283 return nullptr;
284 }
285
286
288 const HASH_TAG& Tag() const
289 {
290 return m_tag;
291 }
292
293 const VECTOR2I& Pos() const
294 {
295 return m_tag.pos;
296 }
297
298 NET_HANDLE Net() const override
299 {
300 return m_tag.net;
301 }
302
303 const std::vector<ITEM*>& LinkList() const
304 {
305 return m_linkedItems.CItems();
306 }
307
308 const ITEM_SET& CLinks() const
309 {
310 return m_linkedItems;
311 }
312
314 {
315 return m_linkedItems;
316 }
317
318 int LinkCount( int aMask = -1 ) const
319 {
320 return m_linkedItems.Count( aMask );
321 }
322
323 void Dump() const;
324
325 bool operator==( const JOINT& rhs ) const
326 {
327 return m_tag.pos == rhs.m_tag.pos && m_tag.net == rhs.m_tag.net;
328 }
329
330 void Merge( const JOINT& aJoint )
331 {
332 if( !Overlaps( aJoint ) )
333 return;
334
335 m_layers.Merge( aJoint.m_layers );
336
337 if( aJoint.IsLocked() )
338 m_locked = true;
339
340 for( ITEM* item : aJoint.LinkList() )
341 {
342 m_linkedItems.Add( item );
343 }
344 }
345
346 bool Overlaps( const JOINT& rhs ) const
347 {
348 return m_tag.pos == rhs.m_tag.pos &&
349 m_tag.net == rhs.m_tag.net && m_layers.Overlaps( rhs.m_layers );
350 }
351
352 void Lock( bool aLock = true )
353 {
354 m_locked = aLock;
355 }
356
357 bool IsLocked() const
358 {
359 return m_locked;
360 }
361
362private:
365
368
371};
372
373inline bool operator==( JOINT::HASH_TAG const& aP1, JOINT::HASH_TAG const& aP2 )
374{
375 return aP1.pos == aP2.pos && aP1.net == aP2.net;
376}
377
378}
379
380#endif // __PNS_JOINT_H
int Size() const
Definition: pns_itemset.h:112
int Count(int aKindMask=-1) const
Definition: pns_itemset.h:66
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:33
bool Contains(ITEM *aItem) const
Definition: pns_itemset.h:151
void Erase(ITEM *aItem)
Definition: pns_itemset.h:156
const std::vector< ITEM * > & CItems() const
Definition: pns_itemset.h:88
Base class for PNS router board items.
Definition: pns_item.h:97
const PNS_LAYER_RANGE & Layers() const
Definition: pns_item.h:199
virtual NET_HANDLE Net() const
Definition: pns_item.h:197
PNS_LAYER_RANGE m_layers
Definition: pns_item.h:306
PnsKind Kind() const
Return the type (kind) of the item.
Definition: pns_item.h:170
@ SEGMENT_T
Definition: pns_item.h:106
bool OfKind(int aKindMask) const
Definition: pns_item.h:178
bool IsVirtual() const
Definition: pns_item.h:282
bool IsLocked() const
Definition: pns_item.h:265
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
const std::vector< ITEM * > & LinkList() const
Definition: pns_joint.h:303
NET_HANDLE Net() const override
Definition: pns_joint.h:298
ITEM_SET m_linkedItems
locked (non-movable) flag
Definition: pns_joint.h:367
VIA * Via() const
Definition: pns_joint.h:275
bool operator==(const JOINT &rhs) const
Definition: pns_joint.h:325
int LinkCount(int aMask=-1) const
Definition: pns_joint.h:318
ITEM_SET & Links()
Definition: pns_joint.h:313
bool IsTrivialEndpoint() const
Definition: pns_joint.h:176
bool IsLineCorner(bool aAllowLockedSegs=false) const
Checks if a joint connects two segments of the same net, layer, and width.
Definition: pns_joint.h:101
void Lock(bool aLock=true)
Definition: pns_joint.h:352
const HASH_TAG & Tag() const
trivial accessors
Definition: pns_joint.h:288
void Link(ITEM *aItem)
Unlink a given board item from the joint (upon its removal from a NODE)
Definition: pns_joint.h:215
bool IsNonFanoutVia() const
Definition: pns_joint.h:149
ITEM * Clone() const override
Return a deep copy of the item.
Definition: pns_joint.h:90
bool IsTraceWidthChange() const
Link the joint to a given board item (when it's added to the NODE).
Definition: pns_joint.h:183
void Dump() const
Definition: pns_node.cpp:1326
LINKED_ITEM * NextSegment(LINKED_ITEM *aCurrent, bool aAllowLockedSegs=false) const
Definition: pns_joint.h:235
const ITEM_SET & CLinks() const
Definition: pns_joint.h:308
JOINT(const JOINT &aB)
Definition: pns_joint.h:79
HASH_TAG m_tag
< hash tag for unordered_multimap
Definition: pns_joint.h:364
bool IsLocked() const
Definition: pns_joint.h:357
bool Overlaps(const JOINT &rhs) const
Definition: pns_joint.h:346
bool IsStitchingVia() const
Definition: pns_joint.h:171
void Merge(const JOINT &aJoint)
Definition: pns_joint.h:330
bool Unlink(ITEM *aItem)
For trivial joints, return the segment adjacent to (aCurrent).
Definition: pns_joint.h:225
bool m_locked
Definition: pns_joint.h:370
const VECTOR2I & Pos() const
Definition: pns_joint.h:293
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.
Definition: pns_layerset.h:32
bool Overlaps(const PNS_LAYER_RANGE &aOther) const
Definition: pns_layerset.h:67
void Merge(const PNS_LAYER_RANGE &aOther)
Definition: pns_layerset.h:96
Push and Shove diff pair dimensions (gap) settings dialog.
void * NET_HANDLE
Definition: pns_item.h:54
bool operator==(JOINT::HASH_TAG const &aP1, JOINT::HASH_TAG const &aP2)
Definition: pns_joint.h:373
< Joints are hashed by their position, layers and net.
Definition: pns_joint.h:48
NET_HANDLE net
Definition: pns_joint.h:50
std::size_t operator()(const JOINT::HASH_TAG &aP) const
Definition: pns_joint.h:55