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
33
34#include <algorithm>
35#include <atomic>
36#include <deque>
37#include <functional>
38#include <memory>
39#include <vector>
40
43
44class CN_ITEM;
45class CN_CLUSTER;
46class PCB_SHAPE;
47
48
54{
55public:
56 CN_ANCHOR( const VECTOR2I& aPos, CN_ITEM* aItem ) :
57 m_pos( aPos ),
58 m_item( aItem ),
59 m_tag( -1 ),
60 m_noline( false )
61 { }
62
63 bool Valid() const;
64
65 bool Dirty() const;
66
67 CN_ITEM* Item() const { return m_item; }
68 void SetItem( CN_ITEM* aItem ) { m_item = aItem; }
69
71
72 const VECTOR2I& Pos() const { return m_pos; }
73
74 void Move( const VECTOR2I& aPos )
75 {
76 m_pos += aPos;
77 }
78
79 unsigned int Dist( const CN_ANCHOR& aSecond )
80 {
81 return ( m_pos - aSecond.Pos() ).EuclideanNorm();
82 }
83
85 int GetTag() const { return m_tag; }
86 void SetTag( int aTag ) { m_tag = aTag; }
87
89 const bool& GetNoLine() const { return m_noline; }
90 void SetNoLine( bool aEnable ) { m_noline = aEnable; }
91
92 const std::shared_ptr<CN_CLUSTER>& GetCluster() const { return m_cluster; }
93 void SetCluster( std::shared_ptr<CN_CLUSTER>& aCluster ) { m_cluster = aCluster; }
94
102 bool IsDangling() const;
103
107 int ConnectedItemsCount() const;
108
109 // Tag used for unconnected items.
110 static const int TAG_UNCONNECTED = -1;
111
112private:
115 int m_tag;
116 bool m_noline;
117
118 std::shared_ptr<CN_CLUSTER> m_cluster;
119};
120
121
122
128{
129public:
130 void Dump();
131
132 CN_ITEM( BOARD_CONNECTED_ITEM* aParent, bool aCanChangeNet, int aAnchorCount = 2 )
133 {
134 m_parent = aParent;
135 m_canChangeNet = aCanChangeNet;
136 m_valid = true;
137 m_dirty = true;
138 m_anchors.reserve( std::max( 6, aAnchorCount ) );
139 m_start_layer = 0;
140 m_end_layer = std::numeric_limits<int>::max();
141 m_connected.reserve( 8 );
142 }
143
144 virtual ~CN_ITEM()
145 {
146 for( const std::shared_ptr<CN_ANCHOR>& anchor : m_anchors )
147 anchor->SetItem( nullptr );
148 };
149
150 std::shared_ptr<CN_ANCHOR> AddAnchor( const VECTOR2I& aPos )
151 {
152 m_anchors.emplace_back( std::make_shared<CN_ANCHOR>( aPos, this ) );
153 return m_anchors.at( m_anchors.size() - 1 );
154 }
155
156 std::vector<std::shared_ptr<CN_ANCHOR>>& Anchors() { return m_anchors; }
157
158 void SetValid( bool aValid ) { m_valid = aValid; }
159 bool Valid() const { return m_valid; }
160
161 void SetDirty( bool aDirty ) { m_dirty.store( aDirty, std::memory_order_release ); }
162 bool Dirty() const { return m_dirty.load( std::memory_order_acquire ); }
163
167 void SetLayers( int aStartLayer, int aEndLayer )
168 {
169 // B_Cu is nominally layer 2 but we reset it to INT_MAX to ensure that it is
170 // always greater than any other layer in the RTree
171 if( aStartLayer == B_Cu )
172 aStartLayer = std::numeric_limits<int>::max();
173
174 if( aEndLayer == B_Cu )
175 aEndLayer = std::numeric_limits<int>::max();
176
177 m_start_layer = aStartLayer;
178 m_end_layer = aEndLayer;
179 }
180
184 void SetLayer( int aLayer ) { SetLayers( aLayer, aLayer ); }
185
189 int StartLayer() const { return m_start_layer; }
190 int EndLayer() const { return m_end_layer; }
191
197 virtual int Layer() const
198 {
199 return StartLayer();
200 }
201
207 {
208 int layer = Layer();
209
210 if( layer == std::numeric_limits<int>::max() )
211 layer = B_Cu;
212
213 return ToLAYER_ID( layer );
214 }
215
216 const BOX2I& BBox()
217 {
218 if( m_dirty.load( std::memory_order_acquire ) && m_valid )
219 {
220 std::lock_guard<std::mutex> lock( m_listLock );
221
222 if( m_dirty.load( std::memory_order_relaxed ) && m_valid )
223 {
224 m_bbox = m_parent->GetBoundingBox();
225 m_dirty.store( false, std::memory_order_release );
226 }
227 }
228
229 return m_bbox;
230 }
231
233
234 const std::vector<CN_ITEM*>& ConnectedItems() const { return m_connected; }
235 void ClearConnections() { m_connected.clear(); }
236
237 bool CanChangeNet() const { return m_canChangeNet; }
238
239 void Connect( CN_ITEM* b )
240 {
241 std::lock_guard<std::mutex> lock( m_listLock );
242
243 auto i = std::lower_bound( m_connected.begin(), m_connected.end(), b );
244
245 if( i != m_connected.end() && *i == b )
246 return;
247
248 m_connected.insert( i, b );
249 }
250
251 void RemoveInvalidRefs();
252
253 virtual int AnchorCount() const;
254 virtual const VECTOR2I GetAnchor( int n ) const;
255
256 int Net() const
257 {
258 return ( !m_parent || !m_valid ) ? -1 : m_parent->GetNetCode();
259 }
260
261protected:
262 std::atomic<bool> m_dirty;
267
268private:
270
271 std::vector<CN_ITEM*> m_connected;
272 std::vector<std::shared_ptr<CN_ANCHOR>> m_anchors;
273
275
276 bool m_valid;
277
278 std::mutex m_listLock;
279};
280
281
282/*
283 * Represents a single outline of a zone fill on a particular layer. \a aSubpolyIndex indicates
284 * which outline in the fill's SHAPE_POLY_SET.
285 */
286class CN_ZONE_LAYER : public CN_ITEM
287{
288public:
289 CN_ZONE_LAYER( ZONE* aParent, PCB_LAYER_ID aLayer, int aSubpolyIndex ) :
290 CN_ITEM( aParent, false ),
291 m_zone( aParent ),
292 m_subpolyIndex( aSubpolyIndex ),
293 m_layer( aLayer )
294 {
295 std::shared_ptr<SHAPE_POLY_SET> fillPoly = aParent->GetFilledPolysList( aLayer );
296
297 if( fillPoly && aSubpolyIndex < fillPoly->OutlineCount() )
298 m_outline = fillPoly->Outline( aSubpolyIndex );
299
300 SetLayers( aLayer, aLayer );
301 }
302
304 {
305 if( m_zone->IsTeardropArea() )
306 return;
307
308 m_triangulatedPolys.clear();
309 m_rTree.RemoveAll();
310
311 std::shared_ptr<SHAPE_POLY_SET> fillPoly = m_zone->GetFilledPolysList( m_layer );
312
313 if( !fillPoly )
314 return;
315
316 for( unsigned int ii = 0; ii < fillPoly->TriangulatedPolyCount(); ++ii )
317 {
318 const auto* triangleSet = fillPoly->TriangulatedPolygon( ii );
319
320 if( triangleSet->GetSourceOutlineIndex() != m_subpolyIndex )
321 continue;
322
323 // Deep copy the triangulated polygon. The copy constructor copies the vertex storage
324 // and updates all TRI parent pointers to reference our owned copy. This ensures the
325 // triangles remain valid even if the zone is refilled on another thread.
326 m_triangulatedPolys.push_back(
327 std::make_unique<SHAPE_POLY_SET::TRIANGULATED_POLYGON>( *triangleSet ) );
328 }
329
330 for( const auto& triPoly : m_triangulatedPolys )
331 {
332 for( const SHAPE_POLY_SET::TRIANGULATED_POLYGON::TRI& tri : triPoly->Triangles() )
333 {
334 BOX2I bbox = tri.BBox();
335 const int mmin[2] = { bbox.GetX(), bbox.GetY() };
336 const int mmax[2] = { bbox.GetRight(), bbox.GetBottom() };
337
338 m_rTree.Insert( mmin, mmax, &tri );
339 }
340 }
341 }
342
343 int SubpolyIndex() const { return m_subpolyIndex; }
344
345 PCB_LAYER_ID GetLayer() const { return m_layer; }
346
347 bool ContainsPoint( const VECTOR2I& p ) const
348 {
349 if( m_outline.PointCount() == 0 )
350 return false;
351
352 if( m_zone->IsTeardropArea() )
353 return m_outline.Collide( p );
354
355 int min[2] = { p.x, p.y };
356 int max[2] = { p.x, p.y };
357 bool collision = false;
358
359 auto visitor =
360 [&]( const SHAPE* aShape ) -> bool
361 {
362 if( aShape->Collide( p ) )
363 {
364 collision = true;
365 return false;
366 }
367
368 return true;
369 };
370
371 m_rTree.Search( min, max, visitor );
372
373 return collision;
374 }
375
377
378 virtual int AnchorCount() const override;
379 virtual const VECTOR2I GetAnchor( int n ) const override;
380
381 bool HasValidOutline() const
382 {
383 return m_outline.PointCount() > 0;
384 }
385
387 {
388 return m_outline;
389 }
390
392 {
393 return m_outline.PointCount();
394 }
395
396 const VECTOR2I& OutlinePoint( int aIndex ) const
397 {
398 return m_outline.CPoint( aIndex );
399 }
400
401 bool Collide( SHAPE* aRefShape ) const
402 {
403 if( m_outline.PointCount() == 0 )
404 return false;
405
406 if( m_zone->IsTeardropArea() )
407 return aRefShape->Collide( &m_outline, 0 );
408
409 BOX2I bbox = aRefShape->BBox();
410 int min[2] = { bbox.GetX(), bbox.GetY() };
411 int max[2] = { bbox.GetRight(), bbox.GetBottom() };
412 bool collision = false;
413
414 auto visitor =
415 [&]( const SHAPE* aShape ) -> bool
416 {
417 if( aRefShape->Collide( aShape ) )
418 {
419 collision = true;
420 return false;
421 }
422
423 return true;
424 };
425
426 m_rTree.Search( min, max, visitor );
427
428 return collision;
429 }
430
431 bool HasSingleConnection();
432
433private:
439 std::vector<std::unique_ptr<SHAPE_POLY_SET::TRIANGULATED_POLYGON>> m_triangulatedPolys;
440 RTree<const SHAPE*, int, 2, double> m_rTree;
441};
442
443
444
445
447{
448public:
450 {
451 m_dirty = false;
452 m_hasInvalid = false;
453 }
454
455 void Clear()
456 {
457 for( CN_ITEM* item : m_items )
458 delete item;
459
460 m_items.clear();
461 m_index.RemoveAll();
462 }
463
464 std::vector<CN_ITEM*>::iterator begin() { return m_items.begin(); };
465 std::vector<CN_ITEM*>::iterator end() { return m_items.end(); };
466
467 std::vector<CN_ITEM*>::const_iterator begin() const { return m_items.begin(); }
468 std::vector<CN_ITEM*>::const_iterator end() const { return m_items.end(); }
469
470 CN_ITEM* operator[] ( int aIndex ) { return m_items[aIndex]; }
471
472 template <class T>
473 void FindNearby( CN_ITEM* aItem, T aFunc )
474 {
475 m_index.Query( aItem->BBox(), aItem->StartLayer(), aItem->EndLayer(), aFunc );
476 }
477
478 void SetHasInvalid( bool aInvalid = true ) { m_hasInvalid = aInvalid; }
479
480 void SetDirty( bool aDirty = true ) { m_dirty = aDirty; }
481 bool IsDirty() const { return m_dirty; }
482
483 void RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage );
484
486 {
487 for( CN_ITEM* item : m_items )
488 item->SetDirty( false );
489
490 SetDirty( false );
491 }
492
493 int Size() const { return m_items.size(); }
494
495 CN_ITEM* Add( PAD* pad );
496 CN_ITEM* Add( PCB_TRACK* track );
497 CN_ITEM* Add( PCB_ARC* track );
498 CN_ITEM* Add( PCB_VIA* via );
499 CN_ITEM* Add( CN_ZONE_LAYER* zitem );
500 CN_ITEM* Add( PCB_SHAPE* shape );
501
502 const std::vector<CN_ITEM*> Add( ZONE* zone, PCB_LAYER_ID aLayer );
503
504protected:
505 void addItemtoTree( CN_ITEM* item )
506 {
507 m_index.Insert( item );
508 }
509
510protected:
511 std::vector<CN_ITEM*> m_items;
512
513private:
517};
518
519
521{
522public:
523 CN_CLUSTER();
524 ~CN_CLUSTER();
525
526 bool HasValidNet() const { return m_originNet > 0; }
527 int OriginNet() const { return m_originNet; }
528
529 wxString OriginNetName() const;
530
531 bool Contains( const CN_ITEM* aItem );
532 bool Contains( const BOARD_CONNECTED_ITEM* aItem );
533 void Dump();
534
535 int Size() const { return m_items.size(); }
536
537 bool IsOrphaned() const
538 {
539 return m_originPad == nullptr;
540 }
541
542 bool IsConflicting() const { return m_conflicting; }
543
544 void Add( CN_ITEM* item );
545
546 std::vector<CN_ITEM*>::iterator begin() { return m_items.begin(); };
547 std::vector<CN_ITEM*>::iterator end() { return m_items.end(); };
548
549private:
553 std::vector<CN_ITEM*> m_items;
554 std::unordered_map<int, int> m_netRanks;
555};
556
557
558
559#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:73
std::shared_ptr< SHAPE_POLY_SET > GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition zone.h:606
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