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 return nullptr;
262 }
263 }
264 }
265
266 return otherItem;
267 }
268
269 VIA* Via() const
270 {
271 for( ITEM* item : m_linkedItems.CItems() )
272 {
273 if( item->OfKind( VIA_T ) )
274 return static_cast<VIA*>( item ); // fixme: const correctness
275 }
276
277 return nullptr;
278 }
279
280
282 const HASH_TAG& Tag() const
283 {
284 return m_tag;
285 }
286
287 const VECTOR2I& Pos() const
288 {
289 return m_tag.pos;
290 }
291
292 NET_HANDLE Net() const override
293 {
294 return m_tag.net;
295 }
296
297 const std::vector<ITEM*>& LinkList() const
298 {
299 return m_linkedItems.CItems();
300 }
301
302 const ITEM_SET& CLinks() const
303 {
304 return m_linkedItems;
305 }
306
308 {
309 return m_linkedItems;
310 }
311
312 int LinkCount( int aMask = -1 ) const
313 {
314 return m_linkedItems.Count( aMask );
315 }
316
317 void Dump() const;
318
319 bool operator==( const JOINT& rhs ) const
320 {
321 return m_tag.pos == rhs.m_tag.pos && m_tag.net == rhs.m_tag.net;
322 }
323
324 void Merge( const JOINT& aJoint )
325 {
326 if( !Overlaps( aJoint ) )
327 return;
328
329 m_layers.Merge( aJoint.m_layers );
330
331 if( aJoint.IsLocked() )
332 m_locked = true;
333
334 for( ITEM* item : aJoint.LinkList() )
335 {
336 m_linkedItems.Add( item );
337 }
338 }
339
340 bool Overlaps( const JOINT& rhs ) const
341 {
342 return m_tag.pos == rhs.m_tag.pos &&
343 m_tag.net == rhs.m_tag.net && m_layers.Overlaps( rhs.m_layers );
344 }
345
346 void Lock( bool aLock = true )
347 {
348 m_locked = aLock;
349 }
350
351 bool IsLocked() const
352 {
353 return m_locked;
354 }
355
356private:
359
362
365};
366
367inline bool operator==( JOINT::HASH_TAG const& aP1, JOINT::HASH_TAG const& aP2 )
368{
369 return aP1.pos == aP2.pos && aP1.net == aP2.net;
370}
371
372}
373
374#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
@ SEGMENT_T
Definition: pns_item.h:106
bool OfKind(int aKindMask) const
Definition: pns_item.h:178
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:297
NET_HANDLE Net() const override
Definition: pns_joint.h:292
ITEM_SET m_linkedItems
locked (non-movable) flag
Definition: pns_joint.h:361
VIA * Via() const
Definition: pns_joint.h:269
bool operator==(const JOINT &rhs) const
Definition: pns_joint.h:319
int LinkCount(int aMask=-1) const
Definition: pns_joint.h:312
ITEM_SET & Links()
Definition: pns_joint.h:307
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:346
const HASH_TAG & Tag() const
trivial accessors
Definition: pns_joint.h:282
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:302
JOINT(const JOINT &aB)
Definition: pns_joint.h:79
HASH_TAG m_tag
< hash tag for unordered_multimap
Definition: pns_joint.h:358
bool IsLocked() const
Definition: pns_joint.h:351
bool Overlaps(const JOINT &rhs) const
Definition: pns_joint.h:340
bool IsStitchingVia() const
Definition: pns_joint.h:171
void Merge(const JOINT &aJoint)
Definition: pns_joint.h:324
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:364
const VECTOR2I & Pos() const
Definition: pns_joint.h:287
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:367
< 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