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