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 <maciej.suminski@cern.ch>
8  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
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 <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 
72  CN_ITEM* Item() const
73  {
74  return m_item;
75  }
76 
78 
79  const VECTOR2I& Pos() const
80  {
81  return m_pos;
82  }
83 
84  void Move( const VECTOR2I& aPos )
85  {
86  m_pos += aPos;
87  }
88 
89  const unsigned int Dist( const CN_ANCHOR& aSecond )
90  {
91  return ( m_pos - aSecond.Pos() ).EuclideanNorm();
92  }
93 
95  inline int GetTag() const
96  {
97  return m_tag;
98  }
99 
101  inline void SetTag( int aTag )
102  {
103  m_tag = aTag;
104  }
105 
107  inline void SetNoLine( bool aEnable )
108  {
109  m_noline = aEnable;
110  }
111 
113  inline const bool& GetNoLine() const
114  {
115  return m_noline;
116  }
117 
118  inline void SetCluster( std::shared_ptr<CN_CLUSTER> aCluster )
119  {
120  m_cluster = aCluster;
121  }
122 
123  inline const std::shared_ptr<CN_CLUSTER>& GetCluster() const
124  {
125  return m_cluster;
126  }
127 
135  bool IsDangling() const;
136 
140  int ConnectedItemsCount() const;
141 
142  // Tag used for unconnected items.
143  static const int TAG_UNCONNECTED = -1;
144 
145 private:
148 
150  CN_ITEM* m_item = nullptr;
151 
153  int m_tag = -1;
154 
156  bool m_noline = false;
157 
159  std::shared_ptr<CN_CLUSTER> m_cluster;
160 };
161 
162 
163 typedef std::shared_ptr<CN_ANCHOR> CN_ANCHOR_PTR;
164 typedef std::vector<CN_ANCHOR_PTR> CN_ANCHORS;
165 
166 
167 // basic connectivity item
168 class CN_ITEM
169 {
170 public:
171  using CONNECTED_ITEMS = std::vector<CN_ITEM*>;
172 
173  void Dump();
174 
175  CN_ITEM( BOARD_CONNECTED_ITEM* aParent, bool aCanChangeNet, int aAnchorCount = 2 )
176  {
177  m_parent = aParent;
178  m_canChangeNet = aCanChangeNet;
179  m_visited = false;
180  m_valid = true;
181  m_dirty = true;
182  m_anchors.reserve( std::max( 6, aAnchorCount ) );
184  m_connected.reserve( 8 );
185  }
186 
187  virtual ~CN_ITEM() {};
188 
189  void AddAnchor( const VECTOR2I& aPos )
190  {
191  m_anchors.emplace_back( std::make_shared<CN_ANCHOR>( aPos, this ) );
192  }
193 
194  CN_ANCHORS& Anchors() { return m_anchors; }
195 
196  void SetValid( bool aValid ) { m_valid = aValid; }
197  bool Valid() const { return m_valid; }
198 
199  void SetDirty( bool aDirty ) { m_dirty = aDirty; }
200  bool Dirty() const { return m_dirty; }
201 
205  void SetLayers( const LAYER_RANGE& aLayers )
206  {
207  m_layers = aLayers;
208  }
209 
213  void SetLayer( int aLayer )
214  {
215  m_layers = LAYER_RANGE( aLayer, aLayer );
216  }
217 
221  const LAYER_RANGE& Layers() const
222  {
223  return m_layers;
224  }
225 
229  virtual int Layer() const
230  {
231  return Layers().Start();
232  }
233 
234  const BOX2I& BBox()
235  {
236  if( m_dirty && m_valid )
237  {
239  m_bbox = BOX2I( box.GetPosition(), box.GetSize() );
240  }
241  return m_bbox;
242  }
243 
245  {
246  return m_parent;
247  }
248 
249  const CONNECTED_ITEMS& ConnectedItems() const { return m_connected; }
250  void ClearConnections() { m_connected.clear(); }
251 
252  void SetVisited( bool aVisited ) { m_visited = aVisited; }
253  bool Visited() const { return m_visited; }
254 
255  bool CanChangeNet() const { return m_canChangeNet; }
256 
257  void Connect( CN_ITEM* b )
258  {
259  std::lock_guard<std::mutex> lock( m_listLock );
260 
261  auto i = std::lower_bound( m_connected.begin(), m_connected.end(), b );
262 
263  if( i != m_connected.end() && *i == b )
264  return;
265 
266  m_connected.insert( i, b );
267  }
268 
269  void RemoveInvalidRefs();
270 
271  virtual int AnchorCount() const;
272  virtual const VECTOR2I GetAnchor( int n ) const;
273 
274  int Net() const
275  {
276  return ( !m_parent || !m_valid ) ? -1 : m_parent->GetNetCode();
277  }
279 protected:
280  bool m_dirty;
284 
285 private:
287 
290 
292 
293  bool m_visited;
294  bool m_valid;
295 
296  std::mutex m_listLock;
297 };
298 
299 typedef std::shared_ptr<CN_ITEM> CN_ITEM_PTR;
300 
301 class CN_ZONE_LAYER : public CN_ITEM
302 {
303 public:
304  CN_ZONE_LAYER( ZONE* aParent, PCB_LAYER_ID aLayer, bool aCanChangeNet, int aSubpolyIndex ) :
305  CN_ITEM( aParent, aCanChangeNet ),
306  m_subpolyIndex( aSubpolyIndex ),
307  m_layer( aLayer )
308  {
309  SHAPE_LINE_CHAIN outline = aParent->GetFilledPolysList( aLayer ).COutline( aSubpolyIndex );
310 
311  outline.SetClosed( true );
312  outline.Simplify();
313 
314  m_cachedPoly = std::make_unique<POLY_GRID_PARTITION>( outline, 16 );
315  }
316 
317  int SubpolyIndex() const
318  {
319  return m_subpolyIndex;
320  }
321 
322  bool ContainsAnchor( const CN_ANCHOR_PTR anchor ) const
323  {
324  return ContainsPoint( anchor->Pos(), 0 );
325  }
326 
327  bool ContainsPoint( const VECTOR2I p, int aAccuracy = 0 ) const
328  {
329  ZONE* zone = static_cast<ZONE*>( Parent() );
330  int clearance = aAccuracy;
331 
332  if( zone->GetFilledPolysUseThickness() )
333  clearance += ( zone->GetMinThickness() + 1 ) / 2;
334 
335  return m_cachedPoly->ContainsPoint( p, clearance );
336  }
337 
338  const BOX2I& BBox()
339  {
340  if( m_dirty )
341  m_bbox = m_cachedPoly->BBox();
342 
343  return m_bbox;
344  }
345 
346  virtual int AnchorCount() const override;
347  virtual const VECTOR2I GetAnchor( int n ) const override;
348 
349 private:
350  std::vector<VECTOR2I> m_testOutlinePoints;
351  std::unique_ptr<POLY_GRID_PARTITION> m_cachedPoly;
354 };
355 
356 class CN_LIST
357 {
358 protected:
359  std::vector<CN_ITEM*> m_items;
360 
361  void addItemtoTree( CN_ITEM* item )
362  {
363  m_index.Insert( item );
364  }
365 
366 public:
368  {
369  m_dirty = false;
370  m_hasInvalid = false;
371  }
372 
373  void Clear()
374  {
375  for( auto item : m_items )
376  delete item;
377 
378  m_items.clear();
379  m_index.RemoveAll();
380  }
381 
382  using ITER = decltype( m_items )::iterator;
383  using CONST_ITER = decltype( m_items )::const_iterator;
384 
385  ITER begin() { return m_items.begin(); };
386  ITER end() { return m_items.end(); };
387 
389  {
390  return m_items.begin();
391  }
392 
393  CONST_ITER end() const
394  {
395  return m_items.end();
396  }
397 
398  CN_ITEM* operator[] ( int aIndex ) { return m_items[aIndex]; }
399 
400  template <class T>
401  void FindNearby( CN_ITEM *aItem, T aFunc )
402  {
403  m_index.Query( aItem->BBox(), aItem->Layers(), aFunc );
404  }
405 
406  void SetHasInvalid( bool aInvalid = true ) { m_hasInvalid = aInvalid; }
407 
408  void SetDirty( bool aDirty = true ) { m_dirty = aDirty; }
409  bool IsDirty() const { return m_dirty; }
410 
411  void RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage );
412 
414  {
415  for( auto item : m_items )
416  item->SetDirty( false );
417 
418  SetDirty( false );
419  }
420 
422  {
423  for( auto item : m_items )
424  item->SetDirty( true );
425 
426  SetDirty( true );
427  }
428 
429  int Size() const
430  {
431  return m_items.size();
432  }
433 
434  CN_ITEM* Add( PAD* pad );
435 
436  CN_ITEM* Add( TRACK* track );
437 
438  CN_ITEM* Add( ARC* track );
439 
440  CN_ITEM* Add( VIA* via );
441 
442  const std::vector<CN_ITEM*> Add( ZONE* zone, PCB_LAYER_ID aLayer );
443 
444 private:
445  bool m_dirty;
447 
449 };
450 
452 {
453 private:
457  std::vector<CN_ITEM*> m_items;
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:148
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
Definition: track.h:343
BOX2< VECTOR2I > BOX2I
Definition: box2.h:522
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)
Function Simplify()
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()
CN_ITEM * operator[](int aIndex)
LAYER_RANGE m_layers
layer range over which the item exists
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
Function GetFilledPolysList returns a reference to the list of filled polygons.
Definition: zone.h:655
bool Dirty() const
BOARD_CONNECTED_ITEM * Parent() const
static const int TAG_UNCONNECTED
CN_ITEM * Item() const
void SetCluster(std::shared_ptr< CN_CLUSTER > aCluster)
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::vector< CN_ITEM * > m_items
void SetClosed(bool aClosed)
Function SetClosed()
bool m_visited
visited flag for the BFS scan
PCB_LAYER_ID
A quick note on layer IDs:
std::vector< CN_ANCHOR_PTR > CN_ANCHORS
int GetMinThickness() const
Definition: zone.h:247
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:107
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
ZONE handles a list of polygons defining a copper zone.
Definition: zone.h:57
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 ContainsPoint(const VECTOR2I p, int aAccuracy=0) const
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)
Definition: track.h:262
CN_ITEM * m_item
Tag for quick connection resolution.
SHAPE_LINE_CHAIN.
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:89
Definition: pad.h:60
decltype(m_items)::iterator ITER
bool IsOrphaned() const
Represent a contiguous set of PCB layers.
Definition: pns_layerset.h:31
Definition: track.h:83
const BOX2I & BBox()
void RemoveInvalidRefs()
bool m_noline
Cluster to which the anchor belongs.
const wxSize GetSize() const
Definition: eda_rect.h:96
bool Valid() const
CONST_ITER end() const
CN_RTREE< CN_ITEM * > m_index
CN_ANCHORS m_anchors
bool GetFilledPolysUseThickness() const
Definition: zone.h:712