KiCad PCB EDA Suite
Loading...
Searching...
No Matches
connectivity_items.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KICAD, a free EDA CAD application.
3 *
4 * Copyright (C) 2013-2017 CERN
5 * Copyright (C) 2018-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Maciej Suminski <[email protected]>
8 * @author Tomasz Wlostowski <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, you may find one here:
22 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
23 * or you may search the http://www.gnu.org website for the version 2 license,
24 * or you may write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 */
27
28
29#ifndef PCBNEW_CONNECTIVITY_ITEMS_H
30#define PCBNEW_CONNECTIVITY_ITEMS_H
31
32#include <board.h>
33#include <pad.h>
34#include <footprint.h>
35#include <pcb_track.h>
36#include <zone.h>
37
39
40#include <memory>
41#include <algorithm>
42#include <functional>
43#include <vector>
44#include <deque>
45
48
49class CN_ITEM;
50class CN_CLUSTER;
51
52
58{
59public:
60 CN_ANCHOR( const VECTOR2I& aPos, CN_ITEM* aItem ) :
61 m_pos( aPos ),
62 m_item( aItem ),
63 m_tag( -1 ),
64 m_noline( false )
65 { }
66
67 bool Valid() const;
68
69 bool Dirty() const;
70
71 CN_ITEM* Item() const { return m_item; }
72 void SetItem( CN_ITEM* aItem ) { m_item = aItem; }
73
75
76 const VECTOR2I& Pos() const { return m_pos; }
77
78 void Move( const VECTOR2I& aPos )
79 {
80 m_pos += aPos;
81 }
82
83 unsigned int Dist( const CN_ANCHOR& aSecond )
84 {
85 return ( m_pos - aSecond.Pos() ).EuclideanNorm();
86 }
87
89 int GetTag() const { return m_tag; }
90 void SetTag( int aTag ) { m_tag = aTag; }
91
93 const bool& GetNoLine() const { return m_noline; }
94 void SetNoLine( bool aEnable ) { m_noline = aEnable; }
95
96 const std::shared_ptr<CN_CLUSTER>& GetCluster() const { return m_cluster; }
97 void SetCluster( std::shared_ptr<CN_CLUSTER>& aCluster ) { m_cluster = aCluster; }
98
106 bool IsDangling() const;
107
111 int ConnectedItemsCount() const;
112
113 // Tag used for unconnected items.
114 static const int TAG_UNCONNECTED = -1;
115
116private:
119 int m_tag;
120 bool m_noline;
121
122 std::shared_ptr<CN_CLUSTER> m_cluster;
123};
124
125
126
132{
133public:
134 void Dump();
135
136 CN_ITEM( BOARD_CONNECTED_ITEM* aParent, bool aCanChangeNet, int aAnchorCount = 2 )
137 {
138 m_parent = aParent;
139 m_canChangeNet = aCanChangeNet;
140 m_visited = false;
141 m_valid = true;
142 m_dirty = true;
143 m_anchors.reserve( std::max( 6, aAnchorCount ) );
145 m_connected.reserve( 8 );
146 }
147
148 virtual ~CN_ITEM()
149 {
150 for( const std::shared_ptr<CN_ANCHOR>& anchor : m_anchors )
151 anchor->SetItem( nullptr );
152 };
153
154 std::shared_ptr<CN_ANCHOR> AddAnchor( const VECTOR2I& aPos )
155 {
156 m_anchors.emplace_back( std::make_shared<CN_ANCHOR>( aPos, this ) );
157 return m_anchors.at( m_anchors.size() - 1 );
158 }
159
160 std::vector<std::shared_ptr<CN_ANCHOR>>& Anchors() { return m_anchors; }
161
162 void SetValid( bool aValid ) { m_valid = aValid; }
163 bool Valid() const { return m_valid; }
164
165 void SetDirty( bool aDirty ) { m_dirty = aDirty; }
166 bool Dirty() const { return m_dirty; }
167
171 void SetLayers( const LAYER_RANGE& aLayers ) { m_layers = aLayers; }
172
176 void SetLayer( int aLayer ) { m_layers = LAYER_RANGE( aLayer, aLayer ); }
177
181 const LAYER_RANGE& Layers() const { return m_layers; }
182
186 virtual int Layer() const
187 {
188 return Layers().Start();
189 }
190
191 const BOX2I& BBox()
192 {
193 if( m_dirty && m_valid )
195
196 return m_bbox;
197 }
198
200
201 const std::vector<CN_ITEM*>& ConnectedItems() const { return m_connected; }
202 void ClearConnections() { m_connected.clear(); }
203
204 void SetVisited( bool aVisited ) { m_visited = aVisited; }
205 bool Visited() const { return m_visited; }
206
207 bool CanChangeNet() const { return m_canChangeNet; }
208
209 void Connect( CN_ITEM* b )
210 {
211 std::lock_guard<std::mutex> lock( m_listLock );
212
213 auto i = std::lower_bound( m_connected.begin(), m_connected.end(), b );
214
215 if( i != m_connected.end() && *i == b )
216 return;
217
218 m_connected.insert( i, b );
219 }
220
221 void RemoveInvalidRefs();
222
223 virtual int AnchorCount() const;
224 virtual const VECTOR2I GetAnchor( int n ) const;
225
226 int Net() const
227 {
228 return ( !m_parent || !m_valid ) ? -1 : m_parent->GetNetCode();
229 }
230
231protected:
232 bool m_dirty;
236
237private:
239
240 std::vector<CN_ITEM*> m_connected;
241 std::vector<std::shared_ptr<CN_ANCHOR>> m_anchors;
242
244
246 bool m_valid;
247
248 std::mutex m_listLock;
249};
250
251
252/*
253 * Represents a single outline of a zone fill on a particular layer. \a aSubpolyIndex indicates
254 * which outline in the fill's SHAPE_POLY_SET.
255 */
256class CN_ZONE_LAYER : public CN_ITEM
257{
258public:
259 CN_ZONE_LAYER( ZONE* aParent, PCB_LAYER_ID aLayer, int aSubpolyIndex ) :
260 CN_ITEM( aParent, false ),
261 m_zone( aParent ),
262 m_subpolyIndex( aSubpolyIndex ),
263 m_layer( aLayer )
264 {
265 m_fillPoly = aParent->GetFilledPolysList( aLayer );
266 SetLayers( aLayer );
267 }
268
270 {
271 if( m_zone->IsTeardropArea() )
272 return;
273
274 for( unsigned int ii = 0; ii < m_fillPoly->TriangulatedPolyCount(); ++ii )
275 {
276 const auto* triangleSet = m_fillPoly->TriangulatedPolygon( ii );
277
278 if( triangleSet->GetSourceOutlineIndex() != m_subpolyIndex )
279 continue;
280
281 for( const SHAPE_POLY_SET::TRIANGULATED_POLYGON::TRI& tri : triangleSet->Triangles() )
282 {
283 BOX2I bbox = tri.BBox();
284 const int mmin[2] = { bbox.GetX(), bbox.GetY() };
285 const int mmax[2] = { bbox.GetRight(), bbox.GetBottom() };
286
287 m_rTree.Insert( mmin, mmax, &tri );
288 }
289 }
290 }
291
292 int SubpolyIndex() const { return m_subpolyIndex; }
293
294 PCB_LAYER_ID GetLayer() const { return m_layer; }
295
296 bool ContainsPoint( const VECTOR2I& p ) const
297 {
298 if( m_zone->IsTeardropArea() )
299 return m_fillPoly->Outline( m_subpolyIndex ).Collide( p ) ;
300
301 int min[2] = { p.x, p.y };
302 int max[2] = { p.x, p.y };
303 bool collision = false;
304
305 auto visitor =
306 [&]( const SHAPE* aShape ) -> bool
307 {
308 if( aShape->Collide( p ) )
309 {
310 collision = true;
311 return false;
312 }
313
314 return true;
315 };
316
317 m_rTree.Search( min, max, visitor );
318
319 return collision;
320 }
321
323
324 virtual int AnchorCount() const override;
325 virtual const VECTOR2I GetAnchor( int n ) const override;
326
328 {
329 return m_fillPoly->Outline( m_subpolyIndex );
330 }
331
332 bool Collide( SHAPE* aRefShape ) const
333 {
334 if( m_zone->IsTeardropArea() )
335 return m_fillPoly->Collide( aRefShape );
336
337 BOX2I bbox = aRefShape->BBox();
338 int min[2] = { bbox.GetX(), bbox.GetY() };
339 int max[2] = { bbox.GetRight(), bbox.GetBottom() };
340 bool collision = false;
341
342 auto visitor =
343 [&]( const SHAPE* aShape ) -> bool
344 {
345 if( aRefShape->Collide( aShape ) )
346 {
347 collision = true;
348 return false;
349 }
350
351 return true;
352 };
353
354 m_rTree.Search( min, max, visitor );
355
356 return collision;
357 }
358
359 bool HasSingleConnection();
360
361private:
365 std::shared_ptr<SHAPE_POLY_SET> m_fillPoly;
366 RTree<const SHAPE*, int, 2, double> m_rTree;
367};
368
369
370
371
373{
374public:
376 {
377 m_dirty = false;
378 m_hasInvalid = false;
379 }
380
381 void Clear()
382 {
383 for( CN_ITEM* item : m_items )
384 delete item;
385
386 m_items.clear();
388 }
389
390 std::vector<CN_ITEM*>::iterator begin() { return m_items.begin(); };
391 std::vector<CN_ITEM*>::iterator end() { return m_items.end(); };
392
393 std::vector<CN_ITEM*>::const_iterator begin() const { return m_items.begin(); }
394 std::vector<CN_ITEM*>::const_iterator end() const { return m_items.end(); }
395
396 CN_ITEM* operator[] ( int aIndex ) { return m_items[aIndex]; }
397
398 template <class T>
399 void FindNearby( CN_ITEM* aItem, T aFunc )
400 {
401 m_index.Query( aItem->BBox(), aItem->Layers(), aFunc );
402 }
403
404 void SetHasInvalid( bool aInvalid = true ) { m_hasInvalid = aInvalid; }
405
406 void SetDirty( bool aDirty = true ) { m_dirty = aDirty; }
407 bool IsDirty() const { return m_dirty; }
408
409 void RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage );
410
412 {
413 for( CN_ITEM* item : m_items )
414 item->SetDirty( false );
415
416 SetDirty( false );
417 }
418
419 int Size() const { return m_items.size(); }
420
421 CN_ITEM* Add( PAD* pad );
422 CN_ITEM* Add( PCB_TRACK* track );
423 CN_ITEM* Add( PCB_ARC* track );
424 CN_ITEM* Add( PCB_VIA* via );
425 CN_ITEM* Add( CN_ZONE_LAYER* zitem );
426
427 const std::vector<CN_ITEM*> Add( ZONE* zone, PCB_LAYER_ID aLayer );
428
429protected:
430 void addItemtoTree( CN_ITEM* item )
431 {
432 m_index.Insert( item );
433 }
434
435protected:
436 std::vector<CN_ITEM*> m_items;
437
438private:
442};
443
444
446{
447public:
448 CN_CLUSTER();
449 ~CN_CLUSTER();
450
451 bool HasValidNet() const { return m_originNet > 0; }
452 int OriginNet() const { return m_originNet; }
453
454 wxString OriginNetName() const;
455
456 bool Contains( const CN_ITEM* aItem );
457 bool Contains( const BOARD_CONNECTED_ITEM* aItem );
458 void Dump();
459
460 int Size() const { return m_items.size(); }
461
462 bool IsOrphaned() const
463 {
464 return m_originPad == nullptr;
465 }
466
467 bool IsConflicting() const { return m_conflicting; }
468
469 void Add( CN_ITEM* item );
470
471 std::vector<CN_ITEM*>::iterator begin() { return m_items.begin(); };
472 std::vector<CN_ITEM*>::iterator end() { return m_items.end(); };
473
474private:
478 std::vector<CN_ITEM*> m_items;
479 std::unordered_map<int, int> m_netRanks;
480};
481
482
483
484#endif /* PCBNEW_CONNECTIVITY_ITEMS_H */
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
coord_type GetY() const
Definition: box2.h:182
coord_type GetX() const
Definition: box2.h:181
coord_type GetRight() const
Definition: box2.h:190
coord_type GetBottom() const
Definition: box2.h:191
CN_ANCHOR represents a physical location that can be connected: a pad or a track/arc/via endpoint.
void SetTag(int aTag)
CN_ITEM * m_item
Pad or track/arc/via owning the anchor.
static const int TAG_UNCONNECTED
VECTOR2I m_pos
Position of the anchor.
void Move(const VECTOR2I &aPos)
bool Valid() const
void SetNoLine(bool aEnable)
int ConnectedItemsCount() const
std::shared_ptr< CN_CLUSTER > m_cluster
Cluster to which the anchor belongs.
CN_ANCHOR(const VECTOR2I &aPos, CN_ITEM *aItem)
const bool & GetNoLine() const
int GetTag() const
const std::shared_ptr< CN_CLUSTER > & GetCluster() const
unsigned int Dist(const CN_ANCHOR &aSecond)
void SetCluster(std::shared_ptr< CN_CLUSTER > &aCluster)
const VECTOR2I & Pos() const
bool Dirty() const
CN_ITEM * Item() const
int m_tag
Tag for quick connection resolution.
bool m_noline
Whether it the node can be a target for ratsnest lines.
BOARD_CONNECTED_ITEM * Parent() const
bool IsDangling() const
The anchor point is dangling if the parent is a track and this anchor point is not connected to anoth...
void SetItem(CN_ITEM *aItem)
int Size() const
std::vector< CN_ITEM * >::iterator end()
std::vector< CN_ITEM * > m_items
bool IsConflicting() const
bool IsOrphaned() const
bool Contains(const CN_ITEM *aItem)
void Add(CN_ITEM *item)
CN_ITEM * m_originPad
std::vector< CN_ITEM * >::iterator begin()
wxString OriginNetName() const
std::unordered_map< int, int > m_netRanks
bool HasValidNet() const
int OriginNet() const
CN_ITEM represents a BOARD_CONNETED_ITEM in the connectivity system (ie: a pad, track/arc/via,...
virtual int Layer() const
Return the item's layer, for single-layered items only.
void SetLayers(const LAYER_RANGE &aLayers)
Set the layers spanned by the item to aLayers.
void Connect(CN_ITEM *b)
virtual ~CN_ITEM()
const BOX2I & BBox()
virtual int AnchorCount() const
void RemoveInvalidRefs()
bool m_visited
visited flag for the BFS scan
std::vector< CN_ITEM * > m_connected
list of physically touching items
const std::vector< CN_ITEM * > & ConnectedItems() const
int Net() const
void SetValid(bool aValid)
BOARD_CONNECTED_ITEM * m_parent
bool Valid() const
std::shared_ptr< CN_ANCHOR > AddAnchor(const VECTOR2I &aPos)
void ClearConnections()
BOX2I m_bbox
bounding box for the item
virtual const VECTOR2I GetAnchor(int n) const
LAYER_RANGE m_layers
layer range over which the item exists
bool m_canChangeNet
can the net propagator modify the netcode?
void SetDirty(bool aDirty)
bool CanChangeNet() const
bool m_dirty
used to identify recently added item not yet scanned into the connectivity search
CN_ITEM(BOARD_CONNECTED_ITEM *aParent, bool aCanChangeNet, int aAnchorCount=2)
void SetLayer(int aLayer)
Set the layers spanned by the item to a single layer aLayer.
void SetVisited(bool aVisited)
const LAYER_RANGE & Layers() const
Return the contiguous set of layers spanned by the item.
std::vector< std::shared_ptr< CN_ANCHOR > > m_anchors
bool m_valid
used to identify garbage items (we use lazy removal)
bool Dirty() const
std::vector< std::shared_ptr< CN_ANCHOR > > & Anchors()
BOARD_CONNECTED_ITEM * Parent() const
bool Visited() const
std::mutex m_listLock
mutex protecting this item's connected_items set to
CN_RTREE< CN_ITEM * > m_index
CN_ITEM * operator[](int aIndex)
CN_ITEM * Add(PAD *pad)
std::vector< CN_ITEM * >::iterator begin()
std::vector< CN_ITEM * >::const_iterator begin() const
void SetDirty(bool aDirty=true)
int Size() const
std::vector< CN_ITEM * >::const_iterator end() const
bool IsDirty() const
std::vector< CN_ITEM * >::iterator end()
std::vector< CN_ITEM * > m_items
void FindNearby(CN_ITEM *aItem, T aFunc)
void ClearDirtyFlags()
void addItemtoTree(CN_ITEM *item)
void SetHasInvalid(bool aInvalid=true)
void RemoveInvalidItems(std::vector< CN_ITEM * > &aGarbage)
CN_RTREE - Implements an R-tree for fast spatial indexing of connectivity items.
void Insert(T aItem)
Function Insert() Inserts an item into the tree.
void Query(const BOX2I &aBounds, const LAYER_RANGE &aRange, Visitor &aVisitor) const
Function Query() Executes a function object aVisitor for each item whose bounding box intersects with...
void RemoveAll()
Function RemoveAll() Removes all items from the RTree.
virtual int AnchorCount() const override
const SHAPE_LINE_CHAIN & GetOutline() const
CN_ZONE_LAYER(ZONE *aParent, PCB_LAYER_ID aLayer, int aSubpolyIndex)
virtual const VECTOR2I GetAnchor(int n) const override
PCB_LAYER_ID GetLayer() const
RTree< const SHAPE *, int, 2, double > m_rTree
bool Collide(SHAPE *aRefShape) const
int SubpolyIndex() const
PCB_LAYER_ID m_layer
PCB_LAYER_ID GetLayer()
std::shared_ptr< SHAPE_POLY_SET > m_fillPoly
bool ContainsPoint(const VECTOR2I &p) const
virtual const BOX2I GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition: eda_item.cpp:73
Represent a contiguous set of PCB layers.
Definition: pns_layerset.h:32
int Start() const
Definition: pns_layerset.h:82
Definition: pad.h:59
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
An abstract shape on 2D plane.
Definition: shape.h:124
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition: shape.h:179
virtual const BOX2I BBox(int aClearance=0) const =0
Compute a bounding box of the shape, with a margin of aClearance a collision.
Handle a list of polygons defining a copper zone.
Definition: zone.h:72
const std::shared_ptr< SHAPE_POLY_SET > & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition: zone.h:615
bool IsTeardropArea() const
Definition: zone.h:694
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:59
@ PCB_LAYER_ID_COUNT
Definition: layer_ids.h:137