KiCad PCB EDA Suite
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-2021 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 
40 
41 #include <memory>
42 #include <algorithm>
43 #include <functional>
44 #include <vector>
45 #include <deque>
46 #include <intrusive_list.h>
47 
50 
51 class CN_ITEM;
52 class CN_CLUSTER;
53 
54 class CN_ANCHOR
55 {
56 public:
58  {
59  m_item = nullptr;
60  }
61 
62  CN_ANCHOR( const VECTOR2I& aPos, CN_ITEM* aItem )
63  {
64  m_pos = aPos;
65  m_item = aItem;
66  assert( m_item );
67  }
68 
69  bool Valid() const;
70 
71  CN_ITEM* Item() const
72  {
73  return m_item;
74  }
75 
77 
78  const VECTOR2I& Pos() const
79  {
80  return m_pos;
81  }
82 
83  void Move( const VECTOR2I& aPos )
84  {
85  m_pos += aPos;
86  }
87 
88  const unsigned int Dist( const CN_ANCHOR& aSecond )
89  {
90  return ( m_pos - aSecond.Pos() ).EuclideanNorm();
91  }
92 
94  inline int GetTag() const
95  {
96  return m_tag;
97  }
98 
100  inline void SetTag( int aTag )
101  {
102  m_tag = aTag;
103  }
104 
106  inline void SetNoLine( bool aEnable )
107  {
108  m_noline = aEnable;
109  }
110 
112  inline const bool& GetNoLine() const
113  {
114  return m_noline;
115  }
116 
117  inline void SetCluster( std::shared_ptr<CN_CLUSTER>& aCluster )
118  {
119  m_cluster = aCluster;
120  }
121 
122  inline const std::shared_ptr<CN_CLUSTER>& GetCluster() const
123  {
124  return m_cluster;
125  }
126 
134  bool IsDangling() const;
135 
139  int ConnectedItemsCount() const;
140 
141  // Tag used for unconnected items.
142  static const int TAG_UNCONNECTED = -1;
143 
144 private:
147 
149  CN_ITEM* m_item = nullptr;
150 
152  int m_tag = -1;
153 
155  bool m_noline = false;
156 
158  std::shared_ptr<CN_CLUSTER> m_cluster;
159 };
160 
161 
162 typedef std::shared_ptr<CN_ANCHOR> CN_ANCHOR_PTR;
163 typedef std::vector<CN_ANCHOR_PTR> CN_ANCHORS;
164 
165 
166 // basic connectivity item
167 class CN_ITEM
168 {
169 public:
170  using CONNECTED_ITEMS = std::vector<CN_ITEM*>;
171 
172  void Dump();
173 
174  CN_ITEM( BOARD_CONNECTED_ITEM* aParent, bool aCanChangeNet, int aAnchorCount = 2 )
175  {
176  m_parent = aParent;
177  m_canChangeNet = aCanChangeNet;
178  m_visited = false;
179  m_valid = true;
180  m_dirty = true;
181  m_anchors.reserve( std::max( 6, aAnchorCount ) );
183  m_connected.reserve( 8 );
184  }
185 
186  virtual ~CN_ITEM() {};
187 
188  void AddAnchor( const VECTOR2I& aPos )
189  {
190  m_anchors.emplace_back( std::make_shared<CN_ANCHOR>( aPos, this ) );
191  }
192 
193  CN_ANCHORS& Anchors() { return m_anchors; }
194 
195  void SetValid( bool aValid ) { m_valid = aValid; }
196  bool Valid() const { return m_valid; }
197 
198  void SetDirty( bool aDirty ) { m_dirty = aDirty; }
199  bool Dirty() const { return m_dirty; }
200 
204  void SetLayers( const LAYER_RANGE& aLayers )
205  {
206  m_layers = aLayers;
207  }
208 
212  void SetLayer( int aLayer )
213  {
214  m_layers = LAYER_RANGE( aLayer, aLayer );
215  }
216 
220  const LAYER_RANGE& Layers() const
221  {
222  return m_layers;
223  }
224 
228  virtual int Layer() const
229  {
230  return Layers().Start();
231  }
232 
233  const BOX2I& BBox()
234  {
235  if( m_dirty && m_valid )
236  {
238  m_bbox = BOX2I( box.GetPosition(), box.GetSize() );
239  }
240  return m_bbox;
241  }
242 
244  {
245  return m_parent;
246  }
247 
248  const CONNECTED_ITEMS& ConnectedItems() const { return m_connected; }
249  void ClearConnections() { m_connected.clear(); }
250 
251  void SetVisited( bool aVisited ) { m_visited = aVisited; }
252  bool Visited() const { return m_visited; }
253 
254  bool CanChangeNet() const { return m_canChangeNet; }
255 
256  void Connect( CN_ITEM* b )
257  {
258  std::lock_guard<std::mutex> lock( m_listLock );
259 
260  auto i = std::lower_bound( m_connected.begin(), m_connected.end(), b );
261 
262  if( i != m_connected.end() && *i == b )
263  return;
264 
265  m_connected.insert( i, b );
266  }
267 
268  void RemoveInvalidRefs();
269 
270  virtual int AnchorCount() const;
271  virtual const VECTOR2I GetAnchor( int n ) const;
272 
273  int Net() const
274  {
275  return ( !m_parent || !m_valid ) ? -1 : m_parent->GetNetCode();
276  }
278 protected:
279  bool m_dirty;
283 
284 private:
286 
289 
291 
292  bool m_visited;
293  bool m_valid;
294 
295  std::mutex m_listLock;
296 };
297 
298 typedef std::shared_ptr<CN_ITEM> CN_ITEM_PTR;
299 
300 class CN_ZONE_LAYER : public CN_ITEM
301 {
302 public:
303  CN_ZONE_LAYER( ZONE* aParent, PCB_LAYER_ID aLayer, bool aCanChangeNet, int aSubpolyIndex ) :
304  CN_ITEM( aParent, aCanChangeNet ),
305  m_subpolyIndex( aSubpolyIndex ),
306  m_layer( aLayer )
307  {
308  SHAPE_LINE_CHAIN outline = aParent->GetFilledPolysList( aLayer ).COutline( aSubpolyIndex );
309 
310  outline.SetClosed( true );
311  outline.Simplify();
312 
313  m_cachedPoly = std::make_unique<POLY_GRID_PARTITION>( outline, 16 );
314  }
315 
316  int SubpolyIndex() const
317  {
318  return m_subpolyIndex;
319  }
320 
321  bool ContainsAnchor( const CN_ANCHOR_PTR anchor ) const
322  {
323  return ContainsPoint( anchor->Pos(), 0 );
324  }
325 
326  bool ContainsPoint( const VECTOR2I& p, int aAccuracy = 0 ) const
327  {
328  ZONE* zone = static_cast<ZONE*>( Parent() );
329  int clearance = aAccuracy;
330 
331  if( zone->GetFilledPolysUseThickness() )
332  clearance += ( zone->GetMinThickness() + 1 ) / 2;
333 
334  return m_cachedPoly->ContainsPoint( p, clearance );
335  }
336 
337  const BOX2I& BBox()
338  {
339  if( m_dirty )
340  m_bbox = m_cachedPoly->BBox();
341 
342  return m_bbox;
343  }
344 
345  virtual int AnchorCount() const override;
346  virtual const VECTOR2I GetAnchor( int n ) const override;
347 
348 private:
349  std::vector<VECTOR2I> m_testOutlinePoints;
350  std::unique_ptr<POLY_GRID_PARTITION> m_cachedPoly;
353 };
354 
355 class CN_LIST
356 {
357 protected:
358  std::vector<CN_ITEM*> m_items;
359 
360  void addItemtoTree( CN_ITEM* item )
361  {
362  m_index.Insert( item );
363  }
364 
365 public:
367  {
368  m_dirty = false;
369  m_hasInvalid = false;
370  }
371 
372  void Clear()
373  {
374  for( auto item : m_items )
375  delete item;
376 
377  m_items.clear();
378  m_index.RemoveAll();
379  }
380 
381  using ITER = decltype( m_items )::iterator;
382  using CONST_ITER = decltype( m_items )::const_iterator;
383 
384  ITER begin() { return m_items.begin(); };
385  ITER end() { return m_items.end(); };
386 
388  {
389  return m_items.begin();
390  }
391 
392  CONST_ITER end() const
393  {
394  return m_items.end();
395  }
396 
397  CN_ITEM* operator[] ( int aIndex ) { return m_items[aIndex]; }
398 
399  template <class T>
400  void FindNearby( CN_ITEM* aItem, T aFunc )
401  {
402  m_index.Query( aItem->BBox(), aItem->Layers(), aFunc );
403  }
404 
405  void SetHasInvalid( bool aInvalid = true ) { m_hasInvalid = aInvalid; }
406 
407  void SetDirty( bool aDirty = true ) { m_dirty = aDirty; }
408  bool IsDirty() const { return m_dirty; }
409 
410  void RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage );
411 
413  {
414  for( auto item : m_items )
415  item->SetDirty( false );
416 
417  SetDirty( false );
418  }
419 
421  {
422  for( auto item : m_items )
423  item->SetDirty( true );
424 
425  SetDirty( true );
426  }
427 
428  int Size() const
429  {
430  return m_items.size();
431  }
432 
433  CN_ITEM* Add( PAD* pad );
434 
435  CN_ITEM* Add( PCB_TRACK* track );
436 
437  CN_ITEM* Add( PCB_ARC* track );
438 
439  CN_ITEM* Add( PCB_VIA* via );
440 
441  const std::vector<CN_ITEM*> Add( ZONE* zone, PCB_LAYER_ID aLayer );
442 
443 private:
444  bool m_dirty;
446 
448 };
449 
451 {
452 private:
456  std::vector<CN_ITEM*> m_items;
457  std::unordered_map<int, int> m_netRanks;
458 
459 public:
460  CN_CLUSTER();
461  ~CN_CLUSTER();
462 
463  bool HasValidNet() const { return m_originNet > 0; }
464  int OriginNet() const { return m_originNet; }
465 
466  wxString OriginNetName() const;
467 
468  bool Contains( const CN_ITEM* aItem );
469  bool Contains( const BOARD_CONNECTED_ITEM* aItem );
470  void Dump();
471 
472  int Size() const { return m_items.size(); }
473 
474  bool IsOrphaned() const { return m_originPad == nullptr; }
475 
476  bool IsConflicting() const { return m_conflicting; }
477 
478  void Add( CN_ITEM* item );
479 
480  using ITER = decltype(m_items)::iterator;
481 
482  ITER begin() { return m_items.begin(); };
483  ITER end() { return m_items.end(); };
484 };
485 
486 typedef std::shared_ptr<CN_CLUSTER> CN_CLUSTER_PTR;
487 
488 
489 #endif /* PCBNEW_CONNECTIVITY_ITEMS_H */
std::vector< CN_ITEM * > CONNECTED_ITEMS
double EuclideanNorm(const wxPoint &vector)
Euclidean norm of a 2D vector.
Definition: trigo.h:146
void RemoveInvalidItems(std::vector< CN_ITEM * > &aGarbage)
const CONNECTED_ITEMS & ConnectedItems() const
bool Contains(const CN_ITEM *aItem)
int GetTag() const
Set tag, common identifier for connected nodes.
std::vector< CN_ITEM * > m_items
std::vector< VECTOR2I > m_testOutlinePoints
BOX2< VECTOR2I > BOX2I
Definition: box2.h:506
decltype(m_items)::iterator ITER
BOARD_CONNECTED_ITEM * m_parent
int Size() const
void SetHasInvalid(bool aInvalid=true)
std::shared_ptr< CN_CLUSTER > m_cluster
SHAPE_LINE_CHAIN & Simplify(bool aRemoveColinear=true)
Simplify the line chain by removing colinear adjacent segments and duplicate vertices.
CN_ITEM * Add(PAD *pad)
void addItemtoTree(CN_ITEM *item)
bool m_valid
used to identify garbage items (we use lazy removal)
const BOX2I & BBox()
void ClearDirtyFlags()
const bool & GetNoLine() const
void ClearConnections()
void SetCluster(std::shared_ptr< CN_CLUSTER > &aCluster)
CN_ITEM * operator[](int aIndex)
LAYER_RANGE m_layers
layer range over which the item exists
bool ContainsPoint(const VECTOR2I &p, int aAccuracy=0) const
bool ContainsAnchor(const CN_ANCHOR_PTR anchor) const
void SetVisited(bool aVisited)
bool IsDirty() const
const unsigned int Dist(const CN_ANCHOR &aSecond)
Return tag, common identifier for connected nodes.
const SHAPE_POLY_SET & GetFilledPolysList(PCB_LAYER_ID aLayer) const
Definition: zone.h:635
bool Dirty() const
BOARD_CONNECTED_ITEM * Parent() const
static const int TAG_UNCONNECTED
CN_ITEM * Item() const
bool IsDangling() const
The anchor point is dangling if the parent is a track and this anchor point is not connected to anoth...
CN_ANCHORS & Anchors()
void Insert(T aItem)
Function Insert() Inserts an item into the tree.
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
decltype(m_items)::const_iterator CONST_ITER
void SetDirty(bool aDirty=true)
void RemoveAll()
Function RemoveAll() Removes all items from the RTree.
int ConnectedItemsCount() const
int Net() const
allow parallel connection threads
bool Visited() const
void MarkAllAsDirty()
VECTOR2I m_pos
< Position of the anchor.
virtual int AnchorCount() const
CONST_ITER begin() const
int Start() const
Definition: pns_layerset.h:82
void SetValid(bool aValid)
std::unordered_map< int, int > m_netRanks
std::vector< CN_ITEM * > m_items
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
bool m_visited
visited flag for the BFS scan
std::vector< CN_ANCHOR_PTR > CN_ANCHORS
int GetMinThickness() const
Definition: zone.h:244
int OriginNet() const
CN_ANCHOR(const VECTOR2I &aPos, CN_ITEM *aItem)
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...
virtual int AnchorCount() const override
const wxPoint GetPosition() const
Definition: eda_rect.h:102
int Size() const
bool m_dirty
used to identify recently added item not yet scanned into the connectivity search
void SetLayers(const LAYER_RANGE &aLayers)
Set the layers spanned by the item to aLayers.
std::mutex m_listLock
mutex protecting this item's connected_items set to
Handle a list of polygons defining a copper zone.
Definition: zone.h:56
virtual ~CN_ITEM()
BOX2I m_bbox
bounding box for the item
int SubpolyIndex() const
void SetTag(int aTag)
Decide whether this node can be a ratsnest line target.
void Add(CN_ITEM *item)
bool m_canChangeNet
can the net propagator modify the netcode?
PCB_LAYER_ID m_layer
const std::shared_ptr< CN_CLUSTER > & GetCluster() const
bool Valid() const
CN_ZONE_LAYER(ZONE *aParent, PCB_LAYER_ID aLayer, bool aCanChangeNet, int aSubpolyIndex)
bool IsConflicting() const
virtual const VECTOR2I GetAnchor(int n) const
void SetDirty(bool aDirty)
int m_tag
Whether it the node can be a target for ratsnest lines.
bool CanChangeNet() const
wxString OriginNetName() const
bool HasValidNet() const
void AddAnchor(const VECTOR2I &aPos)
void FindNearby(CN_ITEM *aItem, T aFunc)
CN_ITEM * m_item
Tag for quick connection resolution.
Represent a polyline (an zero-thickness chain of connected line segments).
PCB_LAYER_ID
A quick note on layer IDs:
Definition: layer_ids.h:65
void SetLayer(int aLayer)
Set the layers spanned by the item to a single layer aLayer.
CN_ITEM * m_originPad
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
const LAYER_RANGE & Layers() const
Return the contiguous set of layers spanned by the item.
Handle the component boundary box.
Definition: eda_rect.h:42
BOARD_CONNECTED_ITEM * Parent() const
std::shared_ptr< CN_CLUSTER > CN_CLUSTER_PTR
virtual const VECTOR2I GetAnchor(int n) const override
std::shared_ptr< CN_ANCHOR > CN_ANCHOR_PTR
CN_ITEM(BOARD_CONNECTED_ITEM *aParent, bool aCanChangeNet, int aAnchorCount=2)
std::unique_ptr< POLY_GRID_PARTITION > m_cachedPoly
virtual int Layer() const
Return the item's layer, for single-layered items only.
std::shared_ptr< CN_ITEM > CN_ITEM_PTR
void SetNoLine(bool aEnable)
Return true if this node can be a target for ratsnest lines.
const VECTOR2I & Pos() const
CONNECTED_ITEMS m_connected
list of items physically connected (touching)
void Connect(CN_ITEM *b)
void Move(const VECTOR2I &aPos)
virtual const EDA_RECT GetBoundingBox() const
Return the orthogonal bounding box of this object for display purposes.
Definition: eda_item.cpp:75
Definition: pad.h:57
decltype(m_items)::iterator ITER
bool IsOrphaned() const
Represent a contiguous set of PCB layers.
Definition: pns_layerset.h:31
const BOX2I & BBox()
void RemoveInvalidRefs()
bool m_noline
Cluster to which the anchor belongs.
const wxSize GetSize() const
Definition: eda_rect.h:91
bool Valid() const
CONST_ITER end() const
CN_RTREE< CN_ITEM * > m_index
CN_ANCHORS m_anchors
bool GetFilledPolysUseThickness() const
Definition: zone.h:689