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 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
177 {
178 if( m_linkedItems.Count( SEGMENT_T ) != 2 )
179 return false;
180
181 const LINKED_ITEM* seg1 = nullptr;
182 const LINKED_ITEM* seg2 = nullptr;
183
184 for( const ITEM* item : m_linkedItems.CItems() )
185 {
186 if( item->IsVirtual() )
187 continue;
188
189 if( item->Kind() == VIA_T )
190 {
191 return false;
192 }
193 else if( item->Kind() == SEGMENT_T || item->Kind() == ARC_T )
194 {
195 if( !seg1 )
196 seg1 = static_cast<const LINKED_ITEM*>( item );
197 else
198 seg2 = static_cast<const LINKED_ITEM*>( item );
199 }
200 }
201
202 wxCHECK( seg1 && seg2, false );
203
204 return seg1->Width() != seg2->Width();
205 }
206
208 void Link( ITEM* aItem )
209 {
210 if( m_linkedItems.Contains( aItem ) )
211 return;
212
213 m_linkedItems.Add( aItem );
214 }
215
218 bool Unlink( ITEM* aItem )
219 {
220 m_linkedItems.Erase( aItem );
221 return m_linkedItems.Size() == 0;
222 }
223
226 LINKED_ITEM* NextSegment( ITEM* aCurrent, bool aAllowLockedSegs = false ) const
227 {
228 if( !IsLineCorner( aAllowLockedSegs ) )
229 return nullptr;
230
231 const std::vector<ITEM*>& citems = m_linkedItems.CItems();
232 const size_t size = citems.size();
233
234 for( size_t i = 0; i < size; i++ )
235 {
236 ITEM* item = m_linkedItems[i];
237
238 if( item != aCurrent && item->Kind() != VIA_T )
239 return static_cast<LINKED_ITEM*>( item );
240 }
241
242 return nullptr;
243 }
244
245 VIA* Via() const
246 {
247 for( ITEM* item : m_linkedItems.CItems() )
248 {
249 if( item->OfKind( VIA_T ) )
250 return static_cast<VIA*>( item ); // fixme: const correctness
251 }
252
253 return nullptr;
254 }
255
256
258 const HASH_TAG& Tag() const
259 {
260 return m_tag;
261 }
262
263 const VECTOR2I& Pos() const
264 {
265 return m_tag.pos;
266 }
267
268 NET_HANDLE Net() const override
269 {
270 return m_tag.net;
271 }
272
273 const std::vector<ITEM*>& LinkList() const
274 {
275 return m_linkedItems.CItems();
276 }
277
278 const ITEM_SET& CLinks() const
279 {
280 return m_linkedItems;
281 }
282
284 {
285 return m_linkedItems;
286 }
287
288 int LinkCount( int aMask = -1 ) const
289 {
290 return m_linkedItems.Count( aMask );
291 }
292
293 void Dump() const;
294
295 bool operator==( const JOINT& rhs ) const
296 {
297 return m_tag.pos == rhs.m_tag.pos && m_tag.net == rhs.m_tag.net;
298 }
299
300 void Merge( const JOINT& aJoint )
301 {
302 if( !Overlaps( aJoint ) )
303 return;
304
305 m_layers.Merge( aJoint.m_layers );
306
307 if( aJoint.IsLocked() )
308 m_locked = true;
309
310 for( ITEM* item : aJoint.LinkList() )
311 {
312 m_linkedItems.Add( item );
313 }
314 }
315
316 bool Overlaps( const JOINT& rhs ) const
317 {
318 return m_tag.pos == rhs.m_tag.pos &&
319 m_tag.net == rhs.m_tag.net && m_layers.Overlaps( rhs.m_layers );
320 }
321
322 void Lock( bool aLock = true )
323 {
324 m_locked = aLock;
325 }
326
327 bool IsLocked() const
328 {
329 return m_locked;
330 }
331
332private:
335
338
341};
342
343inline bool operator==( JOINT::HASH_TAG const& aP1, JOINT::HASH_TAG const& aP2 )
344{
345 return aP1.pos == aP2.pos && aP1.net == aP2.net;
346}
347
348}
349
350#endif // __PNS_JOINT_H
Represent a contiguous set of PCB layers.
Definition: pns_layerset.h:32
void Merge(const LAYER_RANGE &aOther)
Definition: pns_layerset.h:92
bool Overlaps(const LAYER_RANGE &aOther) const
Definition: pns_layerset.h:67
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:32
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
PnsKind Kind() const
Return the type (kind) of the item.
Definition: pns_item.h:167
LAYER_RANGE m_layers
Definition: pns_item.h:288
@ SEGMENT_T
Definition: pns_item.h:106
bool IsLocked() const
Definition: pns_item.h:247
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:273
NET_HANDLE Net() const override
Definition: pns_joint.h:268
ITEM_SET m_linkedItems
locked (non-movable) flag
Definition: pns_joint.h:337
VIA * Via() const
Definition: pns_joint.h:245
bool operator==(const JOINT &rhs) const
Definition: pns_joint.h:295
int LinkCount(int aMask=-1) const
Definition: pns_joint.h:288
ITEM_SET & Links()
Definition: pns_joint.h:283
LINKED_ITEM * NextSegment(ITEM *aCurrent, bool aAllowLockedSegs=false) const
Definition: pns_joint.h:226
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:322
const HASH_TAG & Tag() const
trivial accessors
Definition: pns_joint.h:258
void Link(ITEM *aItem)
Unlink a given board item from the joint (upon its removal from a NODE)
Definition: pns_joint.h:208
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:176
void Dump() const
Definition: pns_node.cpp:1302
const ITEM_SET & CLinks() const
Definition: pns_joint.h:278
JOINT(const JOINT &aB)
Definition: pns_joint.h:79
HASH_TAG m_tag
< hash tag for unordered_multimap
Definition: pns_joint.h:334
bool IsLocked() const
Definition: pns_joint.h:327
bool Overlaps(const JOINT &rhs) const
Definition: pns_joint.h:316
bool IsStitchingVia() const
Definition: pns_joint.h:171
void Merge(const JOINT &aJoint)
Definition: pns_joint.h:300
bool Unlink(ITEM *aItem)
For trivial joints, return the segment adjacent to (aCurrent).
Definition: pns_joint.h:218
bool m_locked
Definition: pns_joint.h:340
JOINT(const VECTOR2I &aPos, const LAYER_RANGE &aLayers, NET_HANDLE aNet=nullptr)
Definition: pns_joint.h:70
const VECTOR2I & Pos() const
Definition: pns_joint.h:263
virtual int Width() const
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:343
< 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