1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
|
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2016-2018 CERN
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <connectivity_algo.h>
#include <widgets/progress_reporter.h>
#include <geometry/geometry_utils.h>
#include <thread>
#include <mutex>
#include <future>
#include <algorithm>
#ifdef PROFILE
#include <profile.h>
#endif
using namespace std::placeholders;
bool operator<( const CN_ANCHOR_PTR& a, const CN_ANCHOR_PTR& b )
{
if( a->Pos().x == b->Pos().x )
return a->Pos().y < b->Pos().y;
else
return a->Pos().x < b->Pos().x;
}
bool CN_ANCHOR::IsDirty() const
{
return m_item->Dirty();
}
CN_CLUSTER::CN_CLUSTER()
{
m_items.reserve( 64 );
m_originPad = nullptr;
m_originNet = -1;
m_conflicting = false;
}
CN_CLUSTER::~CN_CLUSTER()
{
}
wxString CN_CLUSTER::OriginNetName() const
{
if( !m_originPad || !m_originPad->Valid() )
return "<none>";
else
return m_originPad->Parent()->GetNetname();
}
bool CN_CLUSTER::Contains( const CN_ITEM* aItem )
{
return std::find( m_items.begin(), m_items.end(), aItem ) != m_items.end();
}
bool CN_CLUSTER::Contains( const BOARD_CONNECTED_ITEM* aItem )
{
for( auto item : m_items )
{
if( item->Valid() && item->Parent() == aItem )
return true;
}
return false;
}
void CN_ITEM::Dump()
{
printf(" valid: %d, connected: \n", !!Valid());
for( auto i : m_connected )
{
TRACK* t = static_cast<TRACK*>( i->Parent() );
printf( " - %p %d\n", t, t->Type() );
}
}
void CN_CLUSTER::Dump()
{
for( auto item : m_items )
{
wxLogTrace( "CN", " - item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
printf( "- item : %p bitem : %p type : %d inet %s\n", item, item->Parent(),
item->Parent()->Type(), (const char*) item->Parent()->GetNetname().c_str() );
item->Dump();
}
}
void CN_CLUSTER::Add( CN_ITEM* item )
{
m_items.push_back( item );
if( m_originNet < 0 )
{
m_originNet = item->Net();
}
if( item->Parent()->Type() == PCB_PAD_T )
{
if( !m_originPad )
{
m_originPad = item;
m_originNet = item->Net();
}
if( m_originPad && item->Net() != m_originNet )
{
m_conflicting = true;
}
}
}
CN_CONNECTIVITY_ALGO::CN_CONNECTIVITY_ALGO()
{
}
CN_CONNECTIVITY_ALGO::~CN_CONNECTIVITY_ALGO()
{
Clear();
}
bool CN_CONNECTIVITY_ALGO::Remove( BOARD_ITEM* aItem )
{
markItemNetAsDirty( aItem );
switch( aItem->Type() )
{
case PCB_MODULE_T:
for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
{
m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( pad ) ].MarkItemsAsInvalid();
m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( pad ) );
}
m_itemList.SetDirty( true );
break;
case PCB_PAD_T:
m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
m_itemList.SetDirty( true );
break;
case PCB_TRACE_T:
m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
m_itemList.SetDirty( true );
break;
case PCB_VIA_T:
m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
m_itemMap.erase( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
m_itemList.SetDirty( true );
break;
case PCB_ZONE_AREA_T:
{
m_itemMap[ static_cast<BOARD_CONNECTED_ITEM*>( aItem ) ].MarkItemsAsInvalid();
m_itemMap.erase ( static_cast<BOARD_CONNECTED_ITEM*>( aItem ) );
m_itemList.SetDirty( true );
break;
}
case PCB_SEGZONE_T:
default:
return false;
}
// Once we delete an item, it may connect between lists, so mark both as potentially invalid
m_itemList.SetHasInvalid( true );
return true;
}
void CN_CONNECTIVITY_ALGO::markItemNetAsDirty( const BOARD_ITEM* aItem )
{
if( aItem->IsConnected() )
{
auto citem = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
MarkNetAsDirty( citem->GetNetCode() );
}
else
{
if( aItem->Type() == PCB_MODULE_T )
{
auto mod = static_cast <const MODULE*>( aItem );
for( D_PAD* pad = mod->PadsList(); pad; pad = pad->Next() )
MarkNetAsDirty( pad->GetNetCode() );
}
}
}
bool CN_CONNECTIVITY_ALGO::Add( BOARD_ITEM* aItem )
{
if( !IsCopperLayer( aItem->GetLayer() ) )
return false;
markItemNetAsDirty ( aItem );
switch( aItem->Type() )
{
case PCB_NETINFO_T:
{
MarkNetAsDirty( static_cast<NETINFO_ITEM*>( aItem )->GetNet() );
break;
}
case PCB_MODULE_T:
for( auto pad : static_cast<MODULE*>( aItem ) -> Pads() )
{
if( m_itemMap.find( pad ) != m_itemMap.end() )
return false;
add( m_itemList, pad );
}
break;
case PCB_PAD_T:
if( m_itemMap.find ( static_cast<D_PAD*>( aItem ) ) != m_itemMap.end() )
return false;
add( m_itemList, static_cast<D_PAD*>( aItem ) );
break;
case PCB_TRACE_T:
{
if( m_itemMap.find( static_cast<TRACK*>( aItem ) ) != m_itemMap.end() )
return false;
add( m_itemList, static_cast<TRACK*>( aItem ) );
break;
}
case PCB_VIA_T:
if( m_itemMap.find( static_cast<VIA*>( aItem ) ) != m_itemMap.end() )
return false;
add( m_itemList, static_cast<VIA*>( aItem ) );
break;
case PCB_ZONE_AREA_T:
{
auto zone = static_cast<ZONE_CONTAINER*>( aItem );
if( m_itemMap.find( static_cast<ZONE_CONTAINER*>( aItem ) ) != m_itemMap.end() )
return false;
m_itemMap[zone] = ITEM_MAP_ENTRY();
for( auto zitem : m_itemList.Add( zone ) )
m_itemMap[zone].Link(zitem);
break;
}
//N.B. SEGZONE items are deprecated and not to used for connectivity
case PCB_SEGZONE_T:
default:
return false;
}
return true;
}
void CN_CONNECTIVITY_ALGO::searchConnections()
{
#ifdef CONNECTIVITY_DEBUG
printf("Search start\n");
#endif
#ifdef PROFILE
PROF_COUNTER garbage_collection( "garbage-collection" );
#endif
std::vector<CN_ITEM*> garbage;
garbage.reserve( 1024 );
m_itemList.RemoveInvalidItems( garbage );
for( auto item : garbage )
delete item;
#ifdef PROFILE
garbage_collection.Show();
PROF_COUNTER search_basic( "search-basic" );
#endif
std::vector<CN_ITEM*> dirtyItems;
std::copy_if( m_itemList.begin(), m_itemList.end(), std::back_inserter( dirtyItems ),
[] ( CN_ITEM* aItem ) { return aItem->Dirty(); } );
if( m_progressReporter )
{
m_progressReporter->SetMaxProgress( dirtyItems.size() );
m_progressReporter->KeepRefreshing();
}
if( m_itemList.IsDirty() )
{
size_t parallelThreadCount = std::min<size_t>( std::thread::hardware_concurrency(),
( dirtyItems.size() + 7 ) / 8 );
std::atomic<size_t> nextItem( 0 );
std::vector<std::future<size_t>> returns( parallelThreadCount );
auto conn_lambda = [&nextItem, &dirtyItems]
( CN_LIST* aItemList, PROGRESS_REPORTER* aReporter) -> size_t
{
for( size_t i = nextItem++; i < dirtyItems.size(); i = nextItem++ )
{
CN_VISITOR visitor( dirtyItems[i] );
aItemList->FindNearby( dirtyItems[i], visitor );
if( aReporter )
aReporter->AdvanceProgress();
}
return 1;
};
if( parallelThreadCount <= 1 )
conn_lambda( &m_itemList, m_progressReporter );
else
{
for( size_t ii = 0; ii < parallelThreadCount; ++ii )
returns[ii] = std::async( std::launch::async, conn_lambda,
&m_itemList, m_progressReporter );
for( size_t ii = 0; ii < parallelThreadCount; ++ii )
{
// Here we balance returns with a 100ms timeout to allow UI updating
std::future_status status;
do
{
if( m_progressReporter )
m_progressReporter->KeepRefreshing();
status = returns[ii].wait_for( std::chrono::milliseconds( 100 ) );
} while( status != std::future_status::ready );
}
}
if( m_progressReporter )
m_progressReporter->KeepRefreshing();
}
#ifdef PROFILE
search_basic.Show();
#endif
m_itemList.ClearDirtyFlags();
#ifdef CONNECTIVITY_DEBUG
printf("Search end\n");
#endif
}
void CN_ITEM::RemoveInvalidRefs()
{
for( auto it = m_connected.begin(); it != m_connected.end(); )
{
if( !(*it)->Valid() )
it = m_connected.erase( it );
else
++it;
}
}
void CN_LIST::RemoveInvalidItems( std::vector<CN_ITEM*>& aGarbage )
{
if( !m_hasInvalid )
return;
auto lastItem = std::remove_if(m_items.begin(), m_items.end(), [&aGarbage] ( CN_ITEM* item )
{
if( !item->Valid() )
{
aGarbage.push_back ( item );
return true;
}
return false;
} );
m_items.resize( lastItem - m_items.begin() );
// fixme: mem leaks
for( auto item : m_items )
item->RemoveInvalidRefs();
for( auto item : aGarbage )
m_index.Remove( item );
m_hasInvalid = false;
}
bool CN_CONNECTIVITY_ALGO::isDirty() const
{
return m_itemList.IsDirty();
}
const CN_CONNECTIVITY_ALGO::CLUSTERS CN_CONNECTIVITY_ALGO::SearchClusters( CLUSTER_SEARCH_MODE aMode )
{
constexpr KICAD_T types[] = { PCB_TRACE_T, PCB_PAD_T, PCB_VIA_T, PCB_ZONE_AREA_T, PCB_MODULE_T, EOT };
constexpr KICAD_T no_zones[] = { PCB_TRACE_T, PCB_PAD_T, PCB_VIA_T, PCB_MODULE_T, EOT };
if( aMode == CSM_PROPAGATE )
return SearchClusters( aMode, no_zones, -1 );
else
return SearchClusters( aMode, types, -1 );
}
const CN_CONNECTIVITY_ALGO::CLUSTERS CN_CONNECTIVITY_ALGO::SearchClusters( CLUSTER_SEARCH_MODE aMode,
const KICAD_T aTypes[], int aSingleNet )
{
bool withinAnyNet = ( aMode != CSM_PROPAGATE );
std::deque<CN_ITEM*> Q;
CN_ITEM* head = nullptr;
CLUSTERS clusters;
if( isDirty() )
searchConnections();
auto addToSearchList = [&head, withinAnyNet, aSingleNet, aTypes] ( CN_ITEM *aItem )
{
if( withinAnyNet && aItem->Net() <= 0 )
return;
if( !aItem->Valid() )
return;
if( aSingleNet >=0 && aItem->Net() != aSingleNet )
return;
bool found = false;
for( int i = 0; aTypes[i] != EOT; i++ )
{
if( aItem->Parent()->Type() == aTypes[i] )
{
found = true;
break;
}
}
if( !found )
return;
aItem->ListClear();
aItem->SetVisited( false );
if( !head )
head = aItem;
else
head->ListInsert( aItem );
};
std::for_each( m_itemList.begin(), m_itemList.end(), addToSearchList );
while( head )
{
CN_CLUSTER_PTR cluster ( new CN_CLUSTER() );
Q.clear();
CN_ITEM* root = head;
root->SetVisited ( true );
head = root->ListRemove();
Q.push_back( root );
while( Q.size() )
{
CN_ITEM* current = Q.front();
Q.pop_front();
cluster->Add( current );
for( auto n : current->ConnectedItems() )
{
if( withinAnyNet && n->Net() != root->Net() )
continue;
if( !n->Visited() && n->Valid() )
{
n->SetVisited( true );
Q.push_back( n );
head = n->ListRemove();
}
}
}
clusters.push_back( cluster );
}
std::sort( clusters.begin(), clusters.end(), []( CN_CLUSTER_PTR a, CN_CLUSTER_PTR b ) {
return a->OriginNet() < b->OriginNet();
} );
#ifdef CONNECTIVITY_DEBUG
printf("Active clusters: %d\n", clusters.size() );
for( auto cl : clusters )
{
printf( "Net %d\n", cl->OriginNet() );
cl->Dump();
}
#endif
return clusters;
}
void CN_CONNECTIVITY_ALGO::Build( BOARD* aBoard )
{
for( int i = 0; i<aBoard->GetAreaCount(); i++ )
{
auto zone = aBoard->GetArea( i );
Add( zone );
}
for( auto tv : aBoard->Tracks() )
Add( tv );
for( auto mod : aBoard->Modules() )
{
for( auto pad : mod->Pads() )
Add( pad );
}
/*wxLogTrace( "CN", "zones : %lu, pads : %lu vias : %lu tracks : %lu\n",
m_zoneList.Size(), m_padList.Size(),
m_viaList.Size(), m_trackList.Size() );*/
}
void CN_CONNECTIVITY_ALGO::Build( const std::vector<BOARD_ITEM*>& aItems )
{
for( auto item : aItems )
{
switch( item->Type() )
{
case PCB_TRACE_T:
case PCB_VIA_T:
case PCB_PAD_T:
Add( item );
break;
case PCB_MODULE_T:
{
for( auto pad : static_cast<MODULE*>( item )->Pads() )
{
Add( pad );
}
break;
}
//N.B. SEGZONE items are deprecated and not to used for connectivity
case PCB_SEGZONE_T:
default:
break;
}
}
}
void CN_CONNECTIVITY_ALGO::propagateConnections()
{
for( const auto& cluster : m_connClusters )
{
if( cluster->IsConflicting() )
{
wxLogTrace( "CN", "Conflicting nets in cluster %p\n", cluster.get() );
}
else if( cluster->IsOrphaned() )
{
wxLogTrace( "CN", "Skipping orphaned cluster %p [net: %s]\n", cluster.get(),
(const char*) cluster->OriginNetName().c_str() );
}
else if( cluster->HasValidNet() )
{
// normal cluster: just propagate from the pads
int n_changed = 0;
for( auto item : *cluster )
{
if( item->CanChangeNet() )
{
if( item->Valid() && item->Parent()->GetNetCode() != cluster->OriginNet() )
{
MarkNetAsDirty( item->Parent()->GetNetCode() );
MarkNetAsDirty( cluster->OriginNet() );
item->Parent()->SetNetCode( cluster->OriginNet() );
n_changed++;
}
}
}
if( n_changed )
wxLogTrace( "CN", "Cluster %p : net : %d %s\n", cluster.get(),
cluster->OriginNet(), (const char*) cluster->OriginNetName().c_str() );
else
wxLogTrace( "CN", "Cluster %p : nothing to propagate\n", cluster.get() );
}
else
{
wxLogTrace( "CN", "Cluster %p : connected to unused net\n", cluster.get() );
}
}
}
void CN_CONNECTIVITY_ALGO::PropagateNets()
{
m_connClusters = SearchClusters( CSM_PROPAGATE );
propagateConnections();
}
void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands( ZONE_CONTAINER* aZone, std::vector<int>& aIslands )
{
if( aZone->GetFilledPolysList().IsEmpty() )
return;
aIslands.clear();
Remove( aZone );
Add( aZone );
m_connClusters = SearchClusters( CSM_CONNECTIVITY_CHECK );
for( const auto& cluster : m_connClusters )
{
if( cluster->Contains( aZone ) && cluster->IsOrphaned() )
{
for( auto z : *cluster )
{
if( z->Parent() == aZone )
{
aIslands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
}
}
}
}
wxLogTrace( "CN", "Found %u isolated islands\n", (unsigned)aIslands.size() );
}
void CN_CONNECTIVITY_ALGO::FindIsolatedCopperIslands( std::vector<CN_ZONE_ISOLATED_ISLAND_LIST>& aZones )
{
for ( auto& z : aZones )
Remove( z.m_zone );
for ( auto& z : aZones )
{
if( !z.m_zone->GetFilledPolysList().IsEmpty() )
Add( z.m_zone );
}
m_connClusters = SearchClusters( CSM_CONNECTIVITY_CHECK );
for ( auto& zone : aZones )
{
if( zone.m_zone->GetFilledPolysList().IsEmpty() )
continue;
for( const auto& cluster : m_connClusters )
{
if( cluster->Contains( zone.m_zone ) && cluster->IsOrphaned() )
{
for( auto z : *cluster )
{
if( z->Parent() == zone.m_zone )
{
zone.m_islands.push_back( static_cast<CN_ZONE*>(z)->SubpolyIndex() );
}
}
}
}
}
}
const CN_CONNECTIVITY_ALGO::CLUSTERS& CN_CONNECTIVITY_ALGO::GetClusters()
{
m_ratsnestClusters = SearchClusters( CSM_RATSNEST );
return m_ratsnestClusters;
}
void CN_CONNECTIVITY_ALGO::MarkNetAsDirty( int aNet )
{
if( aNet < 0 )
return;
if( (int) m_dirtyNets.size() <= aNet )
{
int lastNet = m_dirtyNets.size() - 1;
if( lastNet < 0 )
lastNet = 0;
m_dirtyNets.resize( aNet + 1 );
for( int i = lastNet; i < aNet + 1; i++ )
m_dirtyNets[i] = true;
}
m_dirtyNets[aNet] = true;
}
void CN_VISITOR::checkZoneItemConnection( CN_ZONE* aZone, CN_ITEM* aItem )
{
auto zoneItem = static_cast<CN_ZONE*> ( aZone );
if( zoneItem->Net() != aItem->Net() && !aItem->CanChangeNet() )
return;
if( zoneItem->ContainsPoint( aItem->GetAnchor( 0 ) ) ||
( aItem->Parent()->Type() == PCB_TRACE_T &&
zoneItem->ContainsPoint( aItem->GetAnchor( 1 ) ) ) )
{
zoneItem->Connect( aItem );
aItem->Connect( zoneItem );
}
}
void CN_VISITOR::checkZoneZoneConnection( CN_ZONE* aZoneA, CN_ZONE* aZoneB )
{
const auto refParent = static_cast<const ZONE_CONTAINER*>( aZoneA->Parent() );
const auto testedParent = static_cast<const ZONE_CONTAINER*>( aZoneB->Parent() );
if( testedParent->Type () != PCB_ZONE_AREA_T )
return;
if( aZoneB == aZoneA || refParent == testedParent )
return;
if( aZoneB->Net() != aZoneA->Net() )
return; // we only test zones belonging to the same net
const auto& outline = refParent->GetFilledPolysList().COutline( aZoneA->SubpolyIndex() );
for( int i = 0; i < outline.PointCount(); i++ )
{
if( aZoneB->ContainsPoint( outline.CPoint( i ) ) )
{
aZoneA->Connect( aZoneB );
aZoneB->Connect( aZoneA );
return;
}
}
const auto& outline2 = testedParent->GetFilledPolysList().COutline( aZoneB->SubpolyIndex() );
for( int i = 0; i < outline2.PointCount(); i++ )
{
if( aZoneA->ContainsPoint( outline2.CPoint( i ) ) )
{
aZoneA->Connect( aZoneB );
aZoneB->Connect( aZoneA );
return;
}
}
}
bool CN_VISITOR::operator()( CN_ITEM* aCandidate )
{
const auto parentA = aCandidate->Parent();
const auto parentB = m_item->Parent();
if( !aCandidate->Valid() || !m_item->Valid() )
return true;
if( parentA == parentB )
return true;
if( !( parentA->GetLayerSet() & parentB->GetLayerSet() ).any() )
return true;
// If both m_item and aCandidate are marked dirty, they will both be searched
// Since we are reciprocal in our connection, we arbitrarily pick one of the connections
// to conduct the expensive search
if( aCandidate->Dirty() && aCandidate < m_item )
return true;
// We should handle zone-zone connection separately
if ( parentA->Type() == PCB_ZONE_AREA_T && parentB->Type() == PCB_ZONE_AREA_T )
{
checkZoneZoneConnection( static_cast<CN_ZONE*>( m_item ),
static_cast<CN_ZONE*>( aCandidate ) );
return true;
}
if( parentA->Type() == PCB_ZONE_AREA_T )
{
checkZoneItemConnection( static_cast<CN_ZONE*>( aCandidate ), m_item );
return true;
}
if( parentB->Type() == PCB_ZONE_AREA_T )
{
checkZoneItemConnection( static_cast<CN_ZONE*>( m_item ), aCandidate );
return true;
}
// Items do not necessarily have reciprocity as we only check for anchors
// therefore, we check HitTest both directions A->B & B->A
// TODO: Check for collision geometry on extended features
wxPoint ptA1( aCandidate->GetAnchor( 0 ).x, aCandidate->GetAnchor( 0 ).y );
wxPoint ptA2( aCandidate->GetAnchor( 1 ).x, aCandidate->GetAnchor( 1 ).y );
wxPoint ptB1( m_item->GetAnchor( 0 ).x, m_item->GetAnchor( 0 ).y );
wxPoint ptB2( m_item->GetAnchor( 1 ).x, m_item->GetAnchor( 1 ).y );
if( parentA->HitTest( ptB1 ) || parentB->HitTest( ptA1 ) ||
( parentA->Type() == PCB_TRACE_T && parentB->HitTest( ptA2 ) ) ||
( parentB->Type() == PCB_TRACE_T && parentA->HitTest( ptB2 ) ) )
{
m_item->Connect( aCandidate );
aCandidate->Connect( m_item );
}
return true;
};
int CN_ITEM::AnchorCount() const
{
if( !m_valid )
return 0;
return m_parent->Type() == PCB_TRACE_T ? 2 : 1;
}
const VECTOR2I CN_ITEM::GetAnchor( int n ) const
{
if( !m_valid )
return VECTOR2I();
switch( m_parent->Type() )
{
case PCB_PAD_T:
return static_cast<const D_PAD*>( m_parent )->ShapePos();
break;
case PCB_TRACE_T:
{
auto tr = static_cast<const TRACK*>( m_parent );
return ( n == 0 ? tr->GetStart() : tr->GetEnd() );
break;
}
case PCB_VIA_T:
return static_cast<const VIA*>( m_parent )->GetStart();
default:
assert( false );
return VECTOR2I();
}
}
int CN_ZONE::AnchorCount() const
{
if( !Valid() )
return 0;
const auto zone = static_cast<const ZONE_CONTAINER*>( Parent() );
const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
return outline.PointCount() ? 1 : 0;
}
const VECTOR2I CN_ZONE::GetAnchor( int n ) const
{
if( !Valid() )
return VECTOR2I();
const auto zone = static_cast<const ZONE_CONTAINER*> ( Parent() );
const auto& outline = zone->GetFilledPolysList().COutline( m_subpolyIndex );
return outline.CPoint( 0 );
}
int CN_ITEM::Net() const
{
if( !m_parent || !m_valid )
return -1;
return m_parent->GetNetCode();
}
BOARD_CONNECTED_ITEM* CN_ANCHOR::Parent() const
{
assert( m_item->Valid() );
return m_item->Parent();
}
bool CN_ANCHOR::Valid() const
{
if( !m_item )
return false;
return m_item->Valid();
}
void CN_CONNECTIVITY_ALGO::Clear()
{
m_ratsnestClusters.clear();
m_connClusters.clear();
m_itemMap.clear();
m_itemList.Clear();
}
void CN_CONNECTIVITY_ALGO::ForEachItem( const std::function<void( CN_ITEM& )>& aFunc )
{
for( auto item : m_itemList )
aFunc( *item );
}
void CN_CONNECTIVITY_ALGO::ForEachAnchor( const std::function<void( CN_ANCHOR& )>& aFunc )
{
ForEachItem( [aFunc] ( CN_ITEM& item ) {
for( const auto& anchor : item.Anchors() )
aFunc( *anchor );
}
);
}
bool CN_ANCHOR::IsDangling() const
{
if( !m_cluster )
return true;
// Calculate the item count connected to this anchor.
// m_cluster groups all items connected, but they are not necessary connected
// at this coordinate point (they are only candidates)
BOARD_CONNECTED_ITEM* item_ref = Parent();
LSET layers = item_ref->GetLayerSet() & LSET::AllCuMask();
// the number of items connected to item_ref at ths anchor point
int connected_items_count = 0;
// the minimal number of items connected to item_ref
// at this anchor point to decide the anchor is *not* dangling
int minimal_count = 1;
// a via can be removed if connected to only one other item.
// the minimal_count is therefore 2
if( item_ref->Type() == PCB_VIA_T )
minimal_count = 2;
for( CN_ITEM* item : *m_cluster )
{
if( !item->Valid() )
continue;
BOARD_CONNECTED_ITEM* brd_item = item->Parent();
if( brd_item == item_ref )
continue;
// count only items on the same layer at this coordinate (especially for zones)
if( !( brd_item->GetLayerSet() & layers ).any() )
continue;
if( brd_item->Type() == PCB_ZONE_AREA_T )
{
ZONE_CONTAINER* zone = static_cast<ZONE_CONTAINER*>( brd_item );
if( zone->HitTestInsideZone( wxPoint( Pos() ) ) )
connected_items_count++;
}
else if( brd_item->HitTest( wxPoint( Pos() ) ) )
connected_items_count++;
}
return connected_items_count < minimal_count;
}
void CN_CONNECTIVITY_ALGO::SetProgressReporter( PROGRESS_REPORTER* aReporter )
{
m_progressReporter = aReporter;
}
|