1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 CERN
5  * Copyright (C) 2018-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
24  */
25 
26 #ifdef PROFILE
27 #include <profile.h>
28 #endif
29 
30 #include <thread>
31 #include <algorithm>
32 #include <future>
33 
34 #include <connectivity/connectivity_data.h>
35 #include <connectivity/connectivity_algo.h>
36 #include <connectivity/from_to_cache.h>
37 
38 #include <ratsnest/ratsnest_data.h>
39 #include <trigo.h>
40 
CONNECTIVITY_DATA()41 CONNECTIVITY_DATA::CONNECTIVITY_DATA()
42 {
43     m_connAlgo.reset( new CN_CONNECTIVITY_ALGO );
44     m_progressReporter = nullptr;
45     m_fromToCache.reset( new FROM_TO_CACHE );
46 }
47 
48 
CONNECTIVITY_DATA(const std::vector<BOARD_ITEM * > & aItems,bool aSkipRatsnest)49 CONNECTIVITY_DATA::CONNECTIVITY_DATA( const std::vector<BOARD_ITEM*>& aItems, bool aSkipRatsnest )
50     : m_skipRatsnest( aSkipRatsnest )
51 {
52     Build( aItems );
53     m_progressReporter = nullptr;
54     m_fromToCache.reset( new FROM_TO_CACHE );
55 }
56 
57 
~CONNECTIVITY_DATA()58 CONNECTIVITY_DATA::~CONNECTIVITY_DATA()
59 {
60     Clear();
61 }
62 
63 
Add(BOARD_ITEM * aItem)64 bool CONNECTIVITY_DATA::Add( BOARD_ITEM* aItem )
65 {
66     m_connAlgo->Add( aItem );
67     return true;
68 }
69 
70 
Remove(BOARD_ITEM * aItem)71 bool CONNECTIVITY_DATA::Remove( BOARD_ITEM* aItem )
72 {
73     m_connAlgo->Remove( aItem );
74     return true;
75 }
76 
77 
Update(BOARD_ITEM * aItem)78 bool CONNECTIVITY_DATA::Update( BOARD_ITEM* aItem )
79 {
80     m_connAlgo->Remove( aItem );
81     m_connAlgo->Add( aItem );
82     return true;
83 }
84 
85 
Build(BOARD * aBoard,PROGRESS_REPORTER * aReporter)86 void CONNECTIVITY_DATA::Build( BOARD* aBoard, PROGRESS_REPORTER* aReporter )
87 {
88     std::unique_lock<KISPINLOCK> lock( m_lock, std::try_to_lock );
89 
90     if( !lock )
91         return;
92 
93     m_connAlgo.reset( new CN_CONNECTIVITY_ALGO );
94     m_connAlgo->Build( aBoard, aReporter );
95 
96     m_netclassMap.clear();
97 
98     for( NETINFO_ITEM* net : aBoard->GetNetInfo() )
99         if( net->GetNetClass()->GetName() != NETCLASS::Default )
100             m_netclassMap[ net->GetNetCode() ] = net->GetNetClass()->GetName();
101 
102     RecalculateRatsnest();
103 }
104 
105 
Build(const std::vector<BOARD_ITEM * > & aItems)106 void CONNECTIVITY_DATA::Build( const std::vector<BOARD_ITEM*>& aItems )
107 {
108     std::unique_lock<KISPINLOCK> lock( m_lock, std::try_to_lock );
109 
110     if( !lock )
111         return;
112 
113     m_connAlgo.reset( new CN_CONNECTIVITY_ALGO );
114     m_connAlgo->Build( aItems );
115 
116     RecalculateRatsnest();
117 }
118 
119 
Move(const VECTOR2I & aDelta)120 void CONNECTIVITY_DATA::Move( const VECTOR2I& aDelta )
121 {
122     m_connAlgo->ForEachAnchor( [&aDelta]( CN_ANCHOR& anchor )
123                                {
124                                    anchor.Move( aDelta );
125                                } );
126 }
127 
128 
updateRatsnest()129 void CONNECTIVITY_DATA::updateRatsnest()
130 {
131 #ifdef PROFILE
132     PROF_COUNTER rnUpdate( "update-ratsnest" );
133 #endif
134 
135     std::vector<RN_NET*> dirty_nets;
136 
137     // Start with net 1 as net 0 is reserved for not-connected
138     // Nets without nodes are also ignored
139     std::copy_if( m_nets.begin() + 1, m_nets.end(), std::back_inserter( dirty_nets ),
140             [] ( RN_NET* aNet )
141             {
142                 return aNet->IsDirty() && aNet->GetNodeCount() > 0;
143             } );
144 
145     // We don't want to spin up a new thread for fewer than 8 nets (overhead costs)
146     size_t parallelThreadCount = std::min<size_t>( std::thread::hardware_concurrency(),
147                                                    ( dirty_nets.size() + 7 ) / 8 );
148 
149     std::atomic<size_t> nextNet( 0 );
150     std::vector<std::future<size_t>> returns( parallelThreadCount );
151 
152     auto update_lambda =
153             [&nextNet, &dirty_nets]() -> size_t
154             {
155                 for( size_t i = nextNet++; i < dirty_nets.size(); i = nextNet++ )
156                     dirty_nets[i]->Update();
157 
158                 return 1;
159             };
160 
161     if( parallelThreadCount <= 1 )
162     {
163         update_lambda();
164     }
165     else
166     {
167         for( size_t ii = 0; ii < parallelThreadCount; ++ii )
168             returns[ii] = std::async( std::launch::async, update_lambda );
169 
170         // Finalize the ratsnest threads
171         for( size_t ii = 0; ii < parallelThreadCount; ++ii )
172             returns[ii].wait();
173     }
174 
175 #ifdef PROFILE
176     rnUpdate.Show();
177 #endif
178 }
179 
180 
addRatsnestCluster(const std::shared_ptr<CN_CLUSTER> & aCluster)181 void CONNECTIVITY_DATA::addRatsnestCluster( const std::shared_ptr<CN_CLUSTER>& aCluster )
182 {
183     RN_NET* rnNet = m_nets[ aCluster->OriginNet() ];
184 
185     rnNet->AddCluster( aCluster );
186 }
187 
188 
RecalculateRatsnest(BOARD_COMMIT * aCommit)189 void CONNECTIVITY_DATA::RecalculateRatsnest( BOARD_COMMIT* aCommit  )
190 {
191     m_connAlgo->PropagateNets( aCommit );
192 
193     int lastNet = m_connAlgo->NetCount();
194 
195     if( lastNet >= (int) m_nets.size() )
196     {
197         unsigned int prevSize = m_nets.size();
198         m_nets.resize( lastNet + 1 );
199 
200         for( unsigned int i = prevSize; i < m_nets.size(); i++ )
201             m_nets[i] = new RN_NET;
202     }
203 
204     const std::vector<CN_CLUSTER_PTR>& clusters = m_connAlgo->GetClusters();
205 
206     int dirtyNets = 0;
207 
208     for( int net = 0; net < lastNet; net++ )
209     {
210         if( m_connAlgo->IsNetDirty( net ) )
211         {
212             m_nets[net]->Clear();
213             dirtyNets++;
214         }
215     }
216 
217     for( const CN_CLUSTER_PTR& c : clusters )
218     {
219         int net = c->OriginNet();
220 
221         // Don't add intentionally-kept zone islands to the ratsnest
222         if( c->IsOrphaned() && c->Size() == 1 )
223         {
224             if( dynamic_cast<CN_ZONE_LAYER*>( *c->begin() ) )
225                 continue;
226         }
227 
228         if( m_connAlgo->IsNetDirty( net ) )
229         {
230             addRatsnestCluster( c );
231         }
232     }
233 
234     m_connAlgo->ClearDirtyFlags();
235 
236     if( !m_skipRatsnest )
237         updateRatsnest();
238 }
239 
240 
BlockRatsnestItems(const std::vector<BOARD_ITEM * > & aItems)241 void CONNECTIVITY_DATA::BlockRatsnestItems( const std::vector<BOARD_ITEM*>& aItems )
242 {
243     std::vector<BOARD_CONNECTED_ITEM*> citems;
244 
245     for( BOARD_ITEM* item : aItems )
246     {
247         if( item->Type() == PCB_FOOTPRINT_T )
248         {
249             for( PAD* pad : static_cast<FOOTPRINT*>(item)->Pads() )
250                 citems.push_back( pad );
251         }
252         else
253         {
254             if( BOARD_CONNECTED_ITEM* citem = dynamic_cast<BOARD_CONNECTED_ITEM*>( item ) )
255                 citems.push_back( citem );
256         }
257     }
258 
259     for( const BOARD_CONNECTED_ITEM* item : citems )
260     {
261         if ( m_connAlgo->ItemExists( item ) )
262         {
263             CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY& entry = m_connAlgo->ItemEntry( item );
264 
265             for( CN_ITEM* cnItem : entry.GetItems() )
266             {
267                 for( const std::shared_ptr<CN_ANCHOR>& anchor : cnItem->Anchors() )
268                     anchor->SetNoLine( true );
269             }
270         }
271     }
272 }
273 
274 
GetNetCount() const275 int CONNECTIVITY_DATA::GetNetCount() const
276 {
277     return m_connAlgo->NetCount();
278 }
279 
280 
FindIsolatedCopperIslands(ZONE * aZone,std::vector<int> & aIslands)281 void CONNECTIVITY_DATA::FindIsolatedCopperIslands( ZONE* aZone, std::vector<int>& aIslands )
282 {
283     // TODO(JE) ZONES
284 #if 0
285     m_connAlgo->FindIsolatedCopperIslands( aZone, aIslands );
286 #endif
287 }
288 
FindIsolatedCopperIslands(std::vector<CN_ZONE_ISOLATED_ISLAND_LIST> & aZones)289 void CONNECTIVITY_DATA::FindIsolatedCopperIslands( std::vector<CN_ZONE_ISOLATED_ISLAND_LIST>& aZones )
290 {
291     m_connAlgo->FindIsolatedCopperIslands( aZones );
292 }
293 
294 
ComputeDynamicRatsnest(const std::vector<BOARD_ITEM * > & aItems,const CONNECTIVITY_DATA * aDynamicData,VECTOR2I aInternalOffset)295 void CONNECTIVITY_DATA::ComputeDynamicRatsnest( const std::vector<BOARD_ITEM*>& aItems,
296                                                 const CONNECTIVITY_DATA* aDynamicData,
297                                                 VECTOR2I aInternalOffset )
298 {
299     if( !aDynamicData )
300         return;
301 
302     m_dynamicRatsnest.clear();
303 
304     // This gets connections between the stationary board and the
305     // moving selection
306     for( unsigned int nc = 1; nc < aDynamicData->m_nets.size(); nc++ )
307     {
308         auto dynNet = aDynamicData->m_nets[nc];
309 
310         if( dynNet->GetNodeCount() != 0 )
311         {
312             RN_NET*       ourNet = m_nets[nc];
313             CN_ANCHOR_PTR nodeA, nodeB;
314 
315             if( ourNet->NearestBicoloredPair( *dynNet, nodeA, nodeB ) )
316             {
317                 RN_DYNAMIC_LINE l;
318                 l.a = nodeA->Pos();
319                 l.b = nodeB->Pos();
320                 l.netCode = nc;
321 
322                 m_dynamicRatsnest.push_back( l );
323             }
324         }
325     }
326 
327     // This gets the ratsnest for internal connections in the moving set
328     const std::vector<CN_EDGE>& edges = GetRatsnestForItems( aItems );
329 
330     for( const CN_EDGE& edge : edges )
331     {
332         const CN_ANCHOR_PTR& nodeA = edge.GetSourceNode();
333         const CN_ANCHOR_PTR& nodeB = edge.GetTargetNode();
334         RN_DYNAMIC_LINE      l;
335 
336         // Use the parents' positions
337         l.a = nodeA->Parent()->GetPosition() + (wxPoint) aInternalOffset;
338         l.b = nodeB->Parent()->GetPosition() + (wxPoint) aInternalOffset;
339         l.netCode = 0;
340         m_dynamicRatsnest.push_back( l );
341     }
342 }
343 
344 
ClearDynamicRatsnest()345 void CONNECTIVITY_DATA::ClearDynamicRatsnest()
346 {
347     m_connAlgo->ForEachAnchor( []( CN_ANCHOR& anchor )
348                                {
349                                    anchor.SetNoLine( false );
350                                } );
351     HideDynamicRatsnest();
352 }
353 
354 
HideDynamicRatsnest()355 void CONNECTIVITY_DATA::HideDynamicRatsnest()
356 {
357     m_dynamicRatsnest.clear();
358 }
359 
360 
PropagateNets(BOARD_COMMIT * aCommit,PROPAGATE_MODE aMode)361 void CONNECTIVITY_DATA::PropagateNets( BOARD_COMMIT* aCommit, PROPAGATE_MODE aMode )
362 {
363     m_connAlgo->PropagateNets( aCommit, aMode );
364 }
365 
366 
IsConnectedOnLayer(const BOARD_CONNECTED_ITEM * aItem,int aLayer,std::vector<KICAD_T> aTypes) const367 bool CONNECTIVITY_DATA::IsConnectedOnLayer( const BOARD_CONNECTED_ITEM *aItem, int aLayer,
368                                             std::vector<KICAD_T> aTypes ) const
369 {
370     CN_CONNECTIVITY_ALGO::ITEM_MAP_ENTRY &entry = m_connAlgo->ItemEntry( aItem );
371 
372     auto matchType =
373             [&]( KICAD_T aItemType )
374             {
375                 if( aTypes.empty() )
376                     return true;
377 
378                 return std::count( aTypes.begin(), aTypes.end(), aItemType ) > 0;
379             };
380 
381     for( CN_ITEM* citem : entry.GetItems() )
382     {
383         for( CN_ITEM* connected : citem->ConnectedItems() )
384         {
385             if( connected->Valid()
386                     && connected->Layers().Overlaps( aLayer )
387                     && connected->Net() == aItem->GetNetCode()
388                     && matchType( connected->Parent()->Type() ) )
389             {
390                     return true;
391             }
392         }
393     }
394 
395     return false;
396 }
397 
398 
GetUnconnectedCount() const399 unsigned int CONNECTIVITY_DATA::GetUnconnectedCount() const
400 {
401     unsigned int unconnected = 0;
402 
403     for( RN_NET* net : m_nets )
404     {
405         if( !net )
406             continue;
407 
408         const std::vector<CN_EDGE>& edges = net->GetUnconnected();
409 
410         if( edges.empty() )
411             continue;
412 
413         unconnected += edges.size();
414     }
415 
416     return unconnected;
417 }
418 
419 
Clear()420 void CONNECTIVITY_DATA::Clear()
421 {
422     for( RN_NET* net : m_nets )
423         delete net;
424 
425     m_nets.clear();
426 }
427 
428 
GetConnectedItems(const BOARD_CONNECTED_ITEM * aItem,const KICAD_T aTypes[],bool aIgnoreNetcodes) const429 const std::vector<BOARD_CONNECTED_ITEM*> CONNECTIVITY_DATA::GetConnectedItems(
430         const BOARD_CONNECTED_ITEM* aItem,
431         const KICAD_T aTypes[],
432         bool aIgnoreNetcodes ) const
433 {
434     std::vector<BOARD_CONNECTED_ITEM*> rv;
435     const auto clusters = m_connAlgo->SearchClusters(
436             aIgnoreNetcodes ?
437                     CN_CONNECTIVITY_ALGO::CSM_PROPAGATE :
438                     CN_CONNECTIVITY_ALGO::CSM_CONNECTIVITY_CHECK, aTypes,
439             aIgnoreNetcodes ? -1 : aItem->GetNetCode() );
440 
441     for( auto cl : clusters )
442     {
443         if( cl->Contains( aItem ) )
444         {
445             for( const auto item : *cl )
446             {
447                 if( item->Valid() )
448                     rv.push_back( item->Parent() );
449             }
450         }
451     }
452 
453     return rv;
454 }
455 
456 
GetNetItems(int aNetCode,const KICAD_T aTypes[]) const457 const std::vector<BOARD_CONNECTED_ITEM*> CONNECTIVITY_DATA::GetNetItems( int aNetCode,
458         const KICAD_T aTypes[] ) const
459 {
460     std::vector<BOARD_CONNECTED_ITEM*> items;
461     items.reserve( 32 );
462 
463     std::bitset<MAX_STRUCT_TYPE_ID> type_bits;
464 
465     for( unsigned int i = 0; aTypes[i] != EOT; ++i )
466     {
467         wxASSERT( aTypes[i] < MAX_STRUCT_TYPE_ID );
468         type_bits.set( aTypes[i] );
469     }
470 
471     m_connAlgo->ForEachItem( [&]( CN_ITEM& aItem ) {
472         if( aItem.Valid() && ( aItem.Net() == aNetCode ) && type_bits[aItem.Parent()->Type()] )
473             items.push_back( aItem.Parent() );
474     } );
475 
476     std::sort( items.begin(), items.end() );
477     items.erase( std::unique( items.begin(), items.end() ), items.end() );
478     return items;
479 }
480 
481 
CheckConnectivity(std::vector<CN_DISJOINT_NET_ENTRY> & aReport)482 bool CONNECTIVITY_DATA::CheckConnectivity( std::vector<CN_DISJOINT_NET_ENTRY>& aReport )
483 {
484     RecalculateRatsnest();
485 
486     for( auto net : m_nets )
487     {
488         if( net )
489         {
490             for( const auto& edge : net->GetEdges() )
491             {
492                 CN_DISJOINT_NET_ENTRY ent;
493                 ent.net = edge.GetSourceNode()->Parent()->GetNetCode();
494                 ent.a   = edge.GetSourceNode()->Parent();
495                 ent.b   = edge.GetTargetNode()->Parent();
496                 ent.anchorA = edge.GetSourceNode()->Pos();
497                 ent.anchorB = edge.GetTargetNode()->Pos();
498                 aReport.push_back( ent );
499             }
500         }
501     }
502 
503     return aReport.empty();
504 }
505 
506 
GetConnectedTracks(const BOARD_CONNECTED_ITEM * aItem) const507 const std::vector<PCB_TRACK*> CONNECTIVITY_DATA::GetConnectedTracks(
508                                                         const BOARD_CONNECTED_ITEM* aItem ) const
509 {
510     auto& entry = m_connAlgo->ItemEntry( aItem );
511 
512     std::set<PCB_TRACK*> tracks;
513     std::vector<PCB_TRACK*> rv;
514 
515     for( CN_ITEM* citem : entry.GetItems() )
516     {
517         for( CN_ITEM* connected : citem->ConnectedItems() )
518         {
519             if( connected->Valid() &&
520                     ( connected->Parent()->Type() == PCB_TRACE_T ||
521                             connected->Parent()->Type() == PCB_VIA_T ||
522                             connected->Parent()->Type() == PCB_ARC_T ) )
523                 tracks.insert( static_cast<PCB_TRACK*> ( connected->Parent() ) );
524         }
525     }
526 
527     std::copy( tracks.begin(), tracks.end(), std::back_inserter( rv ) );
528     return rv;
529 }
530 
531 
GetConnectedPads(const BOARD_CONNECTED_ITEM * aItem,std::set<PAD * > * pads) const532 void CONNECTIVITY_DATA::GetConnectedPads( const BOARD_CONNECTED_ITEM* aItem,
533                                           std::set<PAD*>* pads ) const
534 {
535     for( CN_ITEM* citem : m_connAlgo->ItemEntry( aItem ).GetItems() )
536     {
537         for( CN_ITEM* connected : citem->ConnectedItems() )
538         {
539             if( connected->Valid() && connected->Parent()->Type() == PCB_PAD_T )
540                 pads->insert( static_cast<PAD*> ( connected->Parent() ) );
541         }
542     }
543 }
544 
545 
GetConnectedPads(const BOARD_CONNECTED_ITEM * aItem) const546 const std::vector<PAD*> CONNECTIVITY_DATA::GetConnectedPads( const BOARD_CONNECTED_ITEM* aItem )
547 const
548 {
549     std::set<PAD*>    pads;
550     std::vector<PAD*> rv;
551 
552     GetConnectedPads( aItem, &pads );
553 
554     std::copy( pads.begin(), pads.end(), std::back_inserter( rv ) );
555     return rv;
556 }
557 
558 
GetNodeCount(int aNet) const559 unsigned int CONNECTIVITY_DATA::GetNodeCount( int aNet ) const
560 {
561     int sum = 0;
562 
563     if( aNet < 0 )      // Node count for all nets
564     {
565         for( const RN_NET* net : m_nets )
566             sum += net->GetNodeCount();
567     }
568     else if( aNet < (int) m_nets.size() )
569     {
570         sum = m_nets[aNet]->GetNodeCount();
571     }
572 
573     return sum;
574 }
575 
576 
GetPadCount(int aNet) const577 unsigned int CONNECTIVITY_DATA::GetPadCount( int aNet ) const
578 {
579     int n = 0;
580 
581     for( CN_ITEM* pad : m_connAlgo->ItemList() )
582     {
583         if( !pad->Valid() || pad->Parent()->Type() != PCB_PAD_T)
584             continue;
585 
586         PAD* dpad = static_cast<PAD*>( pad->Parent() );
587 
588         if( aNet < 0 || aNet == dpad->GetNetCode() )
589             n++;
590     }
591 
592     return n;
593 }
594 
595 
GetUnconnectedEdges(std::vector<CN_EDGE> & aEdges) const596 void CONNECTIVITY_DATA::GetUnconnectedEdges( std::vector<CN_EDGE>& aEdges) const
597 {
598     for( const RN_NET* rnNet : m_nets )
599     {
600         if( rnNet )
601         {
602             for( const CN_EDGE& edge : rnNet->GetEdges() )
603                 aEdges.push_back( edge );
604         }
605     }
606 }
607 
608 
getMinDist(BOARD_CONNECTED_ITEM * aItem,const wxPoint & aPoint)609 static int getMinDist( BOARD_CONNECTED_ITEM* aItem, const wxPoint& aPoint )
610 {
611     switch( aItem->Type() )
612     {
613     case PCB_TRACE_T:
614     case PCB_ARC_T:
615     {
616         PCB_TRACK* track = static_cast<PCB_TRACK*>( aItem );
617 
618         return std::min( GetLineLength( track->GetStart(), aPoint ),
619                          GetLineLength( track->GetEnd(), aPoint ) );
620     }
621 
622     default:
623         return GetLineLength( aItem->GetPosition(), aPoint );
624     }
625 }
626 
627 
TestTrackEndpointDangling(PCB_TRACK * aTrack,wxPoint * aPos)628 bool CONNECTIVITY_DATA::TestTrackEndpointDangling( PCB_TRACK* aTrack, wxPoint* aPos )
629 {
630     std::list<CN_ITEM*> items = GetConnectivityAlgo()->ItemEntry( aTrack ).GetItems();
631 
632     // Not in the connectivity system.  This is a bug!
633     if( items.empty() )
634     {
635         wxFAIL_MSG( "track not in connectivity system" );
636         return false;
637     }
638 
639     CN_ITEM* citem = items.front();
640 
641     if( !citem->Valid() )
642         return false;
643 
644     if( aTrack->Type() == PCB_TRACE_T || aTrack->Type() == PCB_ARC_T )
645     {
646         // Test if a segment is connected on each end.
647         //
648         // NB: be wary of short segments which can be connected to the *same* other item on
649         // each end.  If that's their only connection then they're still dangling.
650 
651         PCB_LAYER_ID layer = aTrack->GetLayer();
652         int          accuracy = KiROUND( aTrack->GetWidth() / 2 );
653         int          start_count = 0;
654         int          end_count = 0;
655 
656         for( CN_ITEM* connected : citem->ConnectedItems() )
657         {
658             BOARD_CONNECTED_ITEM* item = connected->Parent();
659 
660             if( item->GetFlags() & IS_DELETED )
661                 continue;
662 
663             std::shared_ptr<SHAPE> shape = item->GetEffectiveShape( layer );
664 
665             bool hitStart = shape->Collide( aTrack->GetStart(), accuracy );
666             bool hitEnd = shape->Collide( aTrack->GetEnd(), accuracy );
667 
668             if( hitStart && hitEnd )
669             {
670                 if( getMinDist( item, aTrack->GetStart() ) < getMinDist( item, aTrack->GetEnd() ) )
671                     start_count++;
672                 else
673                     end_count++;
674             }
675             else if( hitStart )
676             {
677                 start_count++;
678             }
679             else if( hitEnd )
680             {
681                 end_count++;
682             }
683 
684             if( start_count > 0 && end_count > 0 )
685                 return false;
686         }
687 
688         if( aPos )
689             *aPos = (start_count == 0 ) ? aTrack->GetStart() : aTrack->GetEnd();
690 
691         return true;
692     }
693     else if( aTrack->Type() == PCB_VIA_T )
694     {
695         // Test if a via is only connected on one layer
696 
697         const std::vector<CN_ITEM*>& connected = citem->ConnectedItems();
698 
699         if( connected.empty() )
700         {
701             if( aPos )
702                 *aPos = aTrack->GetPosition();
703 
704             return true;
705         }
706 
707         // Here, we check if the via is connected only to items on a single layer
708         int first_layer = UNDEFINED_LAYER;
709 
710         for( CN_ITEM* item : connected )
711         {
712             if( item->Parent()->GetFlags() & IS_DELETED )
713                 continue;
714 
715             if( first_layer == UNDEFINED_LAYER )
716                 first_layer = item->Layer();
717             else if( item->Layer() != first_layer )
718                return false;
719         }
720 
721         if( aPos )
722             *aPos = aTrack->GetPosition();
723 
724         return true;
725     }
726     else
727     {
728         wxFAIL_MSG( "CONNECTIVITY_DATA::TestTrackEndpointDangling: unknown track type" );
729     }
730 
731     return false;
732 }
733 
734 
GetConnectedItemsAtAnchor(const BOARD_CONNECTED_ITEM * aItem,const VECTOR2I & aAnchor,const KICAD_T aTypes[],const int & aMaxError) const735 const std::vector<BOARD_CONNECTED_ITEM*> CONNECTIVITY_DATA::GetConnectedItemsAtAnchor(
736         const BOARD_CONNECTED_ITEM* aItem,
737         const VECTOR2I& aAnchor,
738         const KICAD_T aTypes[],
739         const int& aMaxError ) const
740 {
741     auto&                              entry = m_connAlgo->ItemEntry( aItem );
742     std::vector<BOARD_CONNECTED_ITEM*> rv;
743     SEG::ecoord                        maxErrorSq = (SEG::ecoord) aMaxError * aMaxError;
744 
745     for( auto cnItem : entry.GetItems() )
746     {
747         for( auto connected : cnItem->ConnectedItems() )
748         {
749             for( auto anchor : connected->Anchors() )
750             {
751                 if( ( anchor->Pos() - aAnchor ).SquaredEuclideanNorm() <= maxErrorSq )
752                 {
753                     for( int i = 0; aTypes[i] > 0; i++ )
754                     {
755                         if( connected->Valid() && connected->Parent()->Type() == aTypes[i] )
756                         {
757                             rv.push_back( connected->Parent() );
758                             break;
759                         }
760                     }
761 
762                     break;
763                 }
764             }
765         }
766     }
767 
768     return rv;
769 }
770 
771 
GetRatsnestForNet(int aNet)772 RN_NET* CONNECTIVITY_DATA::GetRatsnestForNet( int aNet )
773 {
774     if ( aNet < 0 || aNet >= (int) m_nets.size() )
775     {
776         return nullptr;
777     }
778 
779     return m_nets[ aNet ];
780 }
781 
782 
MarkItemNetAsDirty(BOARD_ITEM * aItem)783 void CONNECTIVITY_DATA::MarkItemNetAsDirty( BOARD_ITEM *aItem )
784 {
785     if ( aItem->Type() == PCB_FOOTPRINT_T)
786     {
787         for( PAD* pad : static_cast<FOOTPRINT*>( aItem )->Pads() )
788             m_connAlgo->MarkNetAsDirty( pad->GetNetCode() );
789     }
790     if (aItem->IsConnected() )
791     {
792         m_connAlgo->MarkNetAsDirty( static_cast<BOARD_CONNECTED_ITEM*>( aItem )->GetNetCode() );
793     }
794 }
795 
796 
SetProgressReporter(PROGRESS_REPORTER * aReporter)797 void CONNECTIVITY_DATA::SetProgressReporter( PROGRESS_REPORTER* aReporter )
798 {
799     m_progressReporter = aReporter;
800     m_connAlgo->SetProgressReporter( m_progressReporter );
801 }
802 
803 
GetRatsnestForItems(std::vector<BOARD_ITEM * > aItems)804 const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForItems( std::vector<BOARD_ITEM*> aItems )
805 {
806     std::set<int> nets;
807     std::vector<CN_EDGE> edges;
808     std::set<BOARD_CONNECTED_ITEM*> item_set;
809 
810     for( BOARD_ITEM* item : aItems )
811     {
812         if( item->Type() == PCB_FOOTPRINT_T )
813         {
814             FOOTPRINT* footprint = static_cast<FOOTPRINT*>( item );
815 
816             for( PAD* pad : footprint->Pads() )
817             {
818                 nets.insert( pad->GetNetCode() );
819                 item_set.insert( pad );
820             }
821         }
822         else if( auto conn_item = dyn_cast<BOARD_CONNECTED_ITEM*>( item ) )
823         {
824             item_set.insert( conn_item );
825             nets.insert( conn_item->GetNetCode() );
826         }
827     }
828 
829     for( int netcode : nets )
830     {
831         RN_NET* net = GetRatsnestForNet( netcode );
832 
833         for( const CN_EDGE& edge : net->GetEdges() )
834         {
835             std::shared_ptr<CN_ANCHOR> srcNode = edge.GetSourceNode();
836             std::shared_ptr<CN_ANCHOR> dstNode = edge.GetTargetNode();
837 
838             BOARD_CONNECTED_ITEM* srcParent = srcNode->Parent();
839             BOARD_CONNECTED_ITEM* dstParent = dstNode->Parent();
840 
841             bool srcFound = ( item_set.find( srcParent ) != item_set.end() );
842             bool dstFound = ( item_set.find( dstParent ) != item_set.end() );
843 
844             if ( srcFound && dstFound )
845                 edges.push_back( edge );
846         }
847     }
848 
849     return edges;
850 }
851 
852 
GetRatsnestForPad(const PAD * aPad)853 const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForPad( const PAD* aPad )
854 {
855     std::vector<CN_EDGE> edges;
856     RN_NET* net = GetRatsnestForNet( aPad->GetNetCode() );
857 
858     for( const CN_EDGE& edge : net->GetEdges() )
859     {
860         if( edge.GetSourceNode()->Parent() == aPad || edge.GetTargetNode()->Parent() == aPad )
861             edges.push_back( edge );
862     }
863 
864     return edges;
865 }
866 
867 
GetRatsnestForComponent(FOOTPRINT * aComponent,bool aSkipInternalConnections)868 const std::vector<CN_EDGE> CONNECTIVITY_DATA::GetRatsnestForComponent( FOOTPRINT* aComponent, bool aSkipInternalConnections )
869 {
870     std::set<int> nets;
871     std::set<const PAD*> pads;
872     std::vector<CN_EDGE> edges;
873 
874     for( auto pad : aComponent->Pads() )
875     {
876         nets.insert( pad->GetNetCode() );
877         pads.insert( pad );
878     }
879 
880     for( const auto& netcode : nets )
881     {
882         const auto& net = GetRatsnestForNet( netcode );
883 
884         for( const auto& edge : net->GetEdges() )
885         {
886             auto srcNode = edge.GetSourceNode();
887             auto dstNode = edge.GetTargetNode();
888 
889             const PAD* srcParent = static_cast<const PAD*>( srcNode->Parent() );
890             const PAD* dstParent = static_cast<const PAD*>( dstNode->Parent() );
891 
892             bool srcFound = ( pads.find(srcParent) != pads.end() );
893             bool dstFound = ( pads.find(dstParent) != pads.end() );
894 
895             if ( srcFound && dstFound && !aSkipInternalConnections )
896             {
897                 edges.push_back( edge );
898             }
899             else if ( srcFound || dstFound )
900             {
901                 edges.push_back( edge );
902             }
903         }
904     }
905 
906     return edges;
907 }
908 
909 
910