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 The 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 <pcb_shape.h>
37#include <zone.h>
38
40
41#include <algorithm>
42#include <atomic>
43#include <deque>
44#include <functional>
45#include <memory>
46#include <vector>
47
50
51class CN_ITEM;
52class CN_CLUSTER;
53
54
60{
61public:
62 CN_ANCHOR( const VECTOR2I& aPos, CN_ITEM* aItem ) :
63 m_pos( aPos ),
64 m_item( aItem ),
65 m_tag( -1 ),
66 m_noline( false )
67 { }
68
69 bool Valid() const;
70
71 bool Dirty() const;
72
73 CN_ITEM* Item() const { return m_item; }
74 void SetItem( CN_ITEM* aItem ) { m_item = aItem; }
75
77
78 const VECTOR2I& Pos() const { return m_pos; }
79
80 void Move( const VECTOR2I& aPos )
81 {
82 m_pos += aPos;
83 }
84
85 unsigned int Dist( const CN_ANCHOR& aSecond )
86 {
87 return ( m_pos - aSecond.Pos() ).EuclideanNorm();
88 }
89
91 int GetTag() const { return m_tag; }
92 void SetTag( int aTag ) { m_tag = aTag; }
93
95 const bool& GetNoLine() const { return m_noline; }
96 void SetNoLine( bool aEnable ) { m_noline = aEnable; }
97
98 const std::shared_ptr<CN_CLUSTER>& GetCluster() const { return m_cluster; }
99 void SetCluster( std::shared_ptr<CN_CLUSTER>& aCluster ) { m_cluster = aCluster; }
100
108 bool IsDangling() const;
109
113 int ConnectedItemsCount() const;
114
115 // Tag used for unconnected items.
116 static const int TAG_UNCONNECTED = -1;
117
118private:
121 int m_tag;
122 bool m_noline;
123
124 std::shared_ptr<CN_CLUSTER> m_cluster;
125};
126
127
128
134{
135public:
136 void Dump();
137
138 CN_ITEM( BOARD_CONNECTED_ITEM* aParent, bool aCanChangeNet, int aAnchorCount = 2 )
139 {
140 m_parent = aParent;
141 m_canChangeNet = aCanChangeNet;
142 m_valid = true;
143 m_dirty = true;
144 m_anchors.reserve( std::max( 6, aAnchorCount ) );
145 m_start_layer = 0;
146 m_end_layer = std::numeric_limits<int>::max();
147 m_connected.reserve( 8 );
148 }
149
150 virtual ~CN_ITEM()
151 {
152 for( const std::shared_ptr<CN_ANCHOR>& anchor : m_anchors )
153 anchor->SetItem( nullptr );
154 };
155
156 std::shared_ptr<CN_ANCHOR> AddAnchor( const VECTOR2I& aPos )
157 {
158 m_anchors.emplace_back( std::make_shared<CN_ANCHOR>( aPos, this ) );
159 return m_anchors.at( m_anchors.size() - 1 );
160 }
161
162 std::vector<std::shared_ptr<CN_ANCHOR>>& Anchors() { return m_anchors; }
163
164 void SetValid( bool aValid ) { m_valid = aValid; }
165 bool Valid() const { return m_valid; }
166
167 void SetDirty( bool aDirty ) { m_dirty.store( aDirty, std::memory_order_release ); }
168 bool Dirty() const { return m_dirty.load( std::memory_order_acquire ); }
169
173 void SetLayers( int aStartLayer, int aEndLayer )
174 {
175 // B_Cu is nominally layer 2 but we reset it to INT_MAX to ensure that it is
176 // always greater than any other layer in the RTree
177 if( aStartLayer == B_Cu )
178 aStartLayer = std::numeric_limits<int>::max();
179
180 if( aEndLayer == B_Cu )
181 aEndLayer = std::numeric_limits<int>::max();
182
183 m_start_layer = aStartLayer;
184 m_end_layer = aEndLayer;
185 }
186
190 void SetLayer( int aLayer ) { SetLayers( aLayer, aLayer ); }
191
195 int StartLayer() const { return m_start_layer; }
196 int EndLayer() const { return m_end_layer; }
197
203 virtual int Layer() const
204 {
205 return StartLayer();
206 }
207
213 {
214 int layer = Layer();
215
216 if( layer == std::numeric_limits<int>::max() )
217 layer = B_Cu;
218
219 return ToLAYER_ID( layer );
220 }
221
222 const BOX2I& BBox()
223 {
224 if( m_dirty.load( std::memory_order_acquire ) && m_valid )
225 {
226 std::lock_guard<std::mutex> lock( m_listLock );
227
228 if( m_dirty.load( std::memory_order_relaxed ) && m_valid )
229 {
230 m_bbox = m_parent->GetBoundingBox();
231 m_dirty.store( false, std::memory_order_release );
232 }
233 }
234
235 return m_bbox;
236 }
237
239
240 const std::vector<CN_ITEM*>& ConnectedItems() const { return m_connected; }
241 void ClearConnections() { m_connected.clear(); }
242
243 bool CanChangeNet() const { return m_canChangeNet; }
244
245 void Connect( CN_ITEM* b )
246 {
247 std::lock_guard<std::mutex> lock( m_listLock );
248
249 auto i = std::lower_bound( m_connected.begin(), m_connected.end(), b );
250
251 if( i != m_connected.end() && *i == b )
252 return;
253
254 m_connected.insert( i, b );
255 }
256
257 void RemoveInvalidRefs();
258
259 virtual int AnchorCount() const;
260 virtual const VECTOR2I GetAnchor( int n ) const;
261
262 int Net() const
263 {
264 return ( !m_parent || !m_valid ) ? -1 : m_parent->GetNetCode();
265 }
266
267protected:
268 std::atomic<bool> m_dirty;
273
274private:
276
277 std::vector<CN_ITEM*> m_connected;
278 std::vector<std::shared_ptr<CN_ANCHOR>> m_anchors;
279
281
282 bool m_valid;
283
284 std::mutex m_listLock;
285};
286
287
288/*
289 * Represents a single outline of a zone fill on a particular layer. \a aSubpolyIndex indicates
290 * which outline in the fill's SHAPE_POLY_SET.
291 */
292class CN_ZONE_LAYER : public CN_ITEM
293{
294public:
295 CN_ZONE_LAYER( ZONE* aParent, PCB_LAYER_ID aLayer, int aSubpolyIndex ) :
296 CN_ITEM( aParent, false ),
297 m_zone( aParent ),
298 m_subpolyIndex( aSubpolyIndex ),
299 m_layer( aLayer )
300 {
301 std::shared_ptr<SHAPE_POLY_SET> fillPoly = aParent->GetFilledPolysList( aLayer );
302
303 if( fillPoly && aSubpolyIndex < fillPoly->OutlineCount() )
304 m_outline = fillPoly->Outline( aSubpolyIndex );
305
306 SetLayers( aLayer, aLayer );
307 }
308
310 {
311 if( m_zone->IsTeardropArea() )
312 return;
313
314 m_triangulatedPolys.clear();
315 m_rTree.RemoveAll();
316
317 std::shared_ptr<SHAPE_POLY_SET> fillPoly = m_zone->GetFilledPolysList( m_layer );
318
319 if( !fillPoly )
320 return;
321
322 for( unsigned int ii = 0; ii < fillPoly->TriangulatedPolyCount(); ++ii )
323 {
324 const auto* triangleSet = fillPoly->TriangulatedPolygon( ii );
325
326 if( triangleSet->GetSourceOutlineIndex() != m_subpolyIndex )
327 continue;
328
329 // Deep copy the triangulated polygon. The copy constructor copies the vertex storage
330 // and updates all TRI parent pointers to reference our owned copy. This ensures the
331 // triangles remain valid even if the zone is refilled on another thread.
332 m_triangulatedPolys.push_back(
333 std::make_unique<SHAPE_POLY_SET::TRIANGULATED_POLYGON>( *triangleSet ) );
334 }
335
336 for( const auto& triPoly : m_triangulatedPolys )
337 {
338 for( const SHAPE_POLY_SET::TRIANGULATED_POLYGON::TRI& tri : triPoly->Triangles() )
339 {
340 BOX2I bbox = tri.BBox();
341 const int mmin[2] = { bbox.GetX(), bbox.GetY() };
342 const int mmax[2] = { bbox.GetRight(), bbox.GetBottom() };
343
344 m_rTree.Insert( mmin, mmax, &tri );
345 }
346 }
347 }
348
349 int SubpolyIndex() const { return m_subpolyIndex; }
350
351 PCB_LAYER_ID GetLayer() const { return m_layer; }
352
353 bool ContainsPoint( const VECTOR2I& p ) const
354 {
355 if( m_outline.PointCount() == 0 )
356 return false;
357
358 if( m_zone->IsTeardropArea() )
359 return m_outline.Collide( p );
360
361 int min[2] = { p.x, p.y };
362 int max[2] = { p.x, p.y };
363 bool collision = false;
364
365 auto visitor =
366 [&]( const SHAPE* aShape ) -> bool
367 {
368 if( aShape->Collide( p ) )
369 {
370 collision = true;
371 return false;
372 }
373
374 return true;
375 };
376
377 m_rTree.Search( min, max, visitor );
378
379 return collision;
380 }
381
383
384 virtual int AnchorCount() const override;
385 virtual const VECTOR2I GetAnchor( int n ) const override;
386
387 bool HasValidOutline() const
388 {
389 return m_outline.PointCount() > 0;
390 }
391
393 {
394 return m_outline;
395 }
396
398 {
399 return m_outline.PointCount();
400 }
401
402 const VECTOR2I& OutlinePoint( int aIndex ) const
403 {
404 return m_outline.CPoint( aIndex );
405 }
406
407 bool Collide( SHAPE* aRefShape ) const
408 {
409 if( m_outline.PointCount() == 0 )
410 return false;
411
412 if( m_zone->IsTeardropArea() )
413 return aRefShape->Collide( &m_outline, 0 );
414
415 BOX2I bbox = aRefShape->BBox();
416 int min[2] = { bbox.GetX(), bbox.GetY() };
417 int max[2] = { bbox.GetRight(), bbox.GetBottom() };
418 bool collision = false;
419
420 auto visitor =
421 [&]( const SHAPE* aShape ) -> bool
422 {
423 if( aRefShape->Collide( aShape ) )
424 {
425 collision = true;
426 return false;
427 }
428
429 return true;
430 };
431
432 m_rTree.Search( min, max, visitor );
433
434 return collision;
435 }
436
437 bool HasSingleConnection();
438
439private:
445 std::vector<std::unique_ptr<SHAPE_POLY_SET::TRIANGULATED_POLYGON>> m_triangulatedPolys;
446 RTree<const SHAPE*, int, 2, double> m_rTree;
447};
448
449
450
451
453{
454public:
456 {
457 m_dirty = false;
458 m_hasInvalid = false;
459 }
460
461 void Clear()
462 {
463 for( CN_ITEM* item : m_items )
464 delete item;
465
466 m_items.clear();
467 m_index.RemoveAll();
468 }
469
470 std::vector<CN_ITEM*>::iterator begin() { return m_items.begin(); };
471 std::vector<CN_ITEM*>::iterator end() { return m_items.end(); };
472
473 std::vector<CN_ITEM*>::const_iterator begin() const { return m_items.begin(); }
474 std::vector<CN_ITEM*>::const_iterator end() const { return m_items.end(); }
475
476 CN_ITEM* operator[] ( int aIndex ) { return m_items[aIndex]; }
477
478 template <class T>
479 void FindNearby( CN_ITEM* aItem, T aFunc )
480 {
481 m_index.Query( aItem->BBox(), aItem->StartLayer(), aItem->EndLayer(), aFunc );
482 }
483
484 void SetHasInvalid( bool aInvalid = true ) { m_hasInvalid = aInvalid; }
485
486 void SetDirty( bool aDirty = true ) { m_dirty = aDirty; }
487 bool IsDirty() const { return m_dirty; }
488
489 void RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage );
490
492 {
493 for( CN_ITEM* item : m_items )
494 item->SetDirty( false );
495
496 SetDirty( false );
497 }
498
499 int Size() const { return m_items.size(); }
500
501 CN_ITEM* Add( PAD* pad );
502 CN_ITEM* Add( PCB_TRACK* track );
503 CN_ITEM* Add( PCB_ARC* track );
504 CN_ITEM* Add( PCB_VIA* via );
505 CN_ITEM* Add( CN_ZONE_LAYER* zitem );
506 CN_ITEM* Add( PCB_SHAPE* shape );
507
508 const std::vector<CN_ITEM*> Add( ZONE* zone, PCB_LAYER_ID aLayer );
509
510protected:
511 void addItemtoTree( CN_ITEM* item )
512 {
513 m_index.Insert( item );
514 }
515
516protected:
517 std::vector<CN_ITEM*> m_items;
518
519private:
523};
524
525
527{
528public:
529 CN_CLUSTER();
530 ~CN_CLUSTER();
531
532 bool HasValidNet() const { return m_originNet > 0; }
533 int OriginNet() const { return m_originNet; }
534
535 wxString OriginNetName() const;
536
537 bool Contains( const CN_ITEM* aItem );
538 bool Contains( const BOARD_CONNECTED_ITEM* aItem );
539 void Dump();
540
541 int Size() const { return m_items.size(); }
542
543 bool IsOrphaned() const
544 {
545 return m_originPad == nullptr;
546 }
547
548 bool IsConflicting() const { return m_conflicting; }
549
550 void Add( CN_ITEM* item );
551
552 std::vector<CN_ITEM*>::iterator begin() { return m_items.begin(); };
553 std::vector<CN_ITEM*>::iterator end() { return m_items.end(); };
554
555private:
559 std::vector<CN_ITEM*> m_items;
560 std::unordered_map<int, int> m_netRanks;
561};
562
563
564
565#endif /* PCBNEW_CONNECTIVITY_ITEMS_H */
BOX2< VECTOR2I > BOX2I
Definition box2.h:922
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
constexpr coord_type GetY() const
Definition box2.h:208
constexpr coord_type GetX() const
Definition box2.h:207
constexpr coord_type GetRight() const
Definition box2.h:217
constexpr coord_type GetBottom() const
Definition box2.h:222
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 Connect(CN_ITEM *b)
virtual ~CN_ITEM()
const BOX2I & BBox()
virtual int AnchorCount() const
void RemoveInvalidRefs()
std::vector< CN_ITEM * > m_connected
list of physically touching items
const std::vector< CN_ITEM * > & ConnectedItems() const
int Net() const
int m_start_layer
start layer of the item N.B. B_Cu is set to INT_MAX
void SetValid(bool aValid)
std::atomic< bool > m_dirty
used to identify recently added item not yet scanned into the connectivity search
BOARD_CONNECTED_ITEM * m_parent
int m_end_layer
end layer of the item N.B. B_Cu is set to INT_MAX
bool Valid() const
int StartLayer() const
Return the contiguous set of layers spanned by the item.
std::shared_ptr< CN_ANCHOR > AddAnchor(const VECTOR2I &aPos)
void SetLayers(int aStartLayer, int aEndLayer)
Set the layers spanned by the item to aStartLayer and aEndLayer.
void ClearConnections()
BOX2I m_bbox
bounding box for the item
virtual const VECTOR2I GetAnchor(int n) const
PCB_LAYER_ID GetBoardLayer() const
When using CN_ITEM layers to compare against board items, use this function which correctly remaps th...
bool m_canChangeNet
can the net propagator modify the netcode?
void SetDirty(bool aDirty)
bool CanChangeNet() const
CN_ITEM(BOARD_CONNECTED_ITEM *aParent, bool aCanChangeNet, int aAnchorCount=2)
int EndLayer() const
void SetLayer(int aLayer)
Set the layers spanned by the item to a single layer aLayer.
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
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.
virtual int AnchorCount() const override
const SHAPE_LINE_CHAIN & GetOutline() const
CN_ZONE_LAYER(ZONE *aParent, PCB_LAYER_ID aLayer, int aSubpolyIndex)
std::vector< std::unique_ptr< SHAPE_POLY_SET::TRIANGULATED_POLYGON > > m_triangulatedPolys
virtual const VECTOR2I GetAnchor(int n) const override
const VECTOR2I & OutlinePoint(int aIndex) const
PCB_LAYER_ID GetLayer() const
RTree< const SHAPE *, int, 2, double > m_rTree
bool Collide(SHAPE *aRefShape) const
int SubpolyIndex() const
int OutlinePointCount() const
PCB_LAYER_ID m_layer
SHAPE_LINE_CHAIN m_outline
Cached copy of the zone outline.
PCB_LAYER_ID GetLayer()
bool ContainsPoint(const VECTOR2I &p) const
bool HasValidOutline() const
Definition pad.h:55
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:126
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:181
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:74
std::shared_ptr< SHAPE_POLY_SET > GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition zone.h:607
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ B_Cu
Definition layer_ids.h:65
PCB_LAYER_ID ToLAYER_ID(int aLayer)
Definition lset.cpp:754
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:695