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 // joints between segments of different widths are not considered trivial.
109 return seg1->Width() == seg2->Width();
110 }
111 else if( m_linkedItems.Size() > 2 && m_linkedItems.Count( SEGMENT_T | ARC_T ) == 2 )
112 {
113 if( !aAllowLockedSegs )
114 {
115 return false;
116 }
117 // There will be multiple VVIAs on joints between two locked segments, because we
118 // naively add a VVIA to each end of a locked segment.
121 {
122 const LINKED_ITEM* seg1 = nullptr;
123 const LINKED_ITEM* seg2 = nullptr;
124 const VIA* via = nullptr;
125 bool hasNonVirtualVia = false;
126
127 for( const ITEM* item : m_linkedItems.CItems() )
128 {
129 if( item->Kind() == VIA_T )
130 {
131 via = static_cast<const VIA*>( item );
132
133 hasNonVirtualVia = !via->IsVirtual();
134 }
135 else if( item->Kind() == SEGMENT_T || item->Kind() == ARC_T )
136 {
137 if( !seg1 )
138 seg1 = static_cast<const LINKED_ITEM*>( item );
139 else
140 seg2 = static_cast<const LINKED_ITEM*>( item );
141 }
142 }
143
144 if( !via || hasNonVirtualVia )
145 return false;
146
147 assert ( seg1 && seg2 );
148
149 return seg1->Width() == seg2->Width();
150 }
151 }
152
153 return false;
154 }
155
156 bool IsNonFanoutVia() const
157 {
158 int vias = m_linkedItems.Count( VIA_T );
159 int segs = m_linkedItems.Count( SEGMENT_T );
160 segs += m_linkedItems.Count( ARC_T );
161
162 return ( m_linkedItems.Size() == 3 && vias == 1 && segs == 2 );
163 }
164
165 bool IsStitchingVia() const
166 {
167 return ( m_linkedItems.Size() == 1 && m_linkedItems.Count( VIA_T ) == 1 );
168 }
169
171 {
172 if( m_linkedItems.Size() != 2 )
173 return false;
174
175 if( m_linkedItems.Count( SEGMENT_T ) != 2)
176 return false;
177
178 SEGMENT* seg1 = static_cast<SEGMENT*>( m_linkedItems[0] );
179 SEGMENT* seg2 = static_cast<SEGMENT*>( m_linkedItems[1] );
180
181 return seg1->Width() != seg2->Width();
182 }
183
185 void Link( ITEM* aItem )
186 {
187 if( m_linkedItems.Contains( aItem ) )
188 return;
189
190 m_linkedItems.Add( aItem );
191 }
192
195 bool Unlink( ITEM* aItem )
196 {
197 m_linkedItems.Erase( aItem );
198 return m_linkedItems.Size() == 0;
199 }
200
203 LINKED_ITEM* NextSegment( ITEM* aCurrent, bool aAllowLockedSegs = false ) const
204 {
205 if( !IsLineCorner( aAllowLockedSegs ) )
206 return nullptr;
207
208 return static_cast<LINKED_ITEM*>( m_linkedItems[m_linkedItems[0] == aCurrent ? 1 : 0] );
209 }
210
211 VIA* Via() const
212 {
213 for( ITEM* item : m_linkedItems.CItems() )
214 {
215 if( item->OfKind( VIA_T ) )
216 return static_cast<VIA*>( item ); // fixme: const correctness
217 }
218
219 return nullptr;
220 }
221
222
224 const HASH_TAG& Tag() const
225 {
226 return m_tag;
227 }
228
229 const VECTOR2I& Pos() const
230 {
231 return m_tag.pos;
232 }
233
234 NET_HANDLE Net() const override
235 {
236 return m_tag.net;
237 }
238
239 const std::vector<ITEM*>& LinkList() const
240 {
241 return m_linkedItems.CItems();
242 }
243
244 const ITEM_SET& CLinks() const
245 {
246 return m_linkedItems;
247 }
248
250 {
251 return m_linkedItems;
252 }
253
254 int LinkCount( int aMask = -1 ) const
255 {
256 return m_linkedItems.Count( aMask );
257 }
258
259 void Dump() const;
260
261 bool operator==( const JOINT& rhs ) const
262 {
263 return m_tag.pos == rhs.m_tag.pos && m_tag.net == rhs.m_tag.net;
264 }
265
266 void Merge( const JOINT& aJoint )
267 {
268 if( !Overlaps( aJoint ) )
269 return;
270
271 m_layers.Merge( aJoint.m_layers );
272
273 if( aJoint.IsLocked() )
274 m_locked = true;
275
276 for( ITEM* item : aJoint.LinkList() )
277 {
278 m_linkedItems.Add( item );
279 }
280 }
281
282 bool Overlaps( const JOINT& rhs ) const
283 {
284 return m_tag.pos == rhs.m_tag.pos &&
285 m_tag.net == rhs.m_tag.net && m_layers.Overlaps( rhs.m_layers );
286 }
287
288 void Lock( bool aLock = true )
289 {
290 m_locked = aLock;
291 }
292
293 bool IsLocked() const
294 {
295 return m_locked;
296 }
297
298private:
301
304
307};
308
309inline bool operator==( JOINT::HASH_TAG const& aP1, JOINT::HASH_TAG const& aP2 )
310{
311 return aP1.pos == aP2.pos && aP1.net == aP2.net;
312}
313
314}
315
316#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
LAYER_RANGE m_layers
Definition: pns_item.h:287
@ SEGMENT_T
Definition: pns_item.h:105
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:239
NET_HANDLE Net() const override
Definition: pns_joint.h:234
ITEM_SET m_linkedItems
locked (non-movable) flag
Definition: pns_joint.h:303
VIA * Via() const
Definition: pns_joint.h:211
bool operator==(const JOINT &rhs) const
Definition: pns_joint.h:261
int LinkCount(int aMask=-1) const
Definition: pns_joint.h:254
ITEM_SET & Links()
Definition: pns_joint.h:249
LINKED_ITEM * NextSegment(ITEM *aCurrent, bool aAllowLockedSegs=false) const
Definition: pns_joint.h:203
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:288
const HASH_TAG & Tag() const
trivial accessors
Definition: pns_joint.h:224
void Link(ITEM *aItem)
Unlink a given board item from the joint (upon its removal from a NODE)
Definition: pns_joint.h:185
bool IsNonFanoutVia() const
Definition: pns_joint.h:156
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:170
void Dump() const
Definition: pns_node.cpp:1276
const ITEM_SET & CLinks() const
Definition: pns_joint.h:244
JOINT(const JOINT &aB)
Definition: pns_joint.h:79
HASH_TAG m_tag
< hash tag for unordered_multimap
Definition: pns_joint.h:300
bool IsLocked() const
Definition: pns_joint.h:293
bool Overlaps(const JOINT &rhs) const
Definition: pns_joint.h:282
bool IsStitchingVia() const
Definition: pns_joint.h:165
void Merge(const JOINT &aJoint)
Definition: pns_joint.h:266
bool Unlink(ITEM *aItem)
For trivial joints, return the segment adjacent to (aCurrent).
Definition: pns_joint.h:195
bool m_locked
Definition: pns_joint.h:306
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:229
virtual int Width() const
int Width() const override
Definition: pns_segment.h:79
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:309
< 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