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
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
|
/*
* This program source code file is part of KICAD, a free EDA CAD application.
*
* Copyright (C) 2013-2015 CERN
* @author Maciej Suminski <maciej.suminski@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
*/
/**
* @file ratsnest_data.cpp
* @brief Class that computes missing connections on a PCB.
*/
#ifdef USE_OPENMP
#include <omp.h>
#endif /* USE_OPENMP */
#include <ratsnest_data.h>
#include <class_board.h>
#include <class_module.h>
#include <class_pad.h>
#include <class_track.h>
#include <class_zone.h>
#include <boost/range/adaptor/map.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/bind.hpp>
#include <geometry/shape_poly_set.h>
#include <cassert>
#include <algorithm>
#include <limits>
#ifdef PROFILE
#include <profile.h>
#endif
static uint64_t getDistance( const RN_NODE_PTR& aNode1, const RN_NODE_PTR& aNode2 )
{
// Drop the least significant bits to avoid overflow
int64_t x = ( aNode1->GetX() - aNode2->GetX() ) >> 16;
int64_t y = ( aNode1->GetY() - aNode2->GetY() ) >> 16;
// We do not need sqrt() here, as the distance is computed only for comparison
return ( x * x + y * y );
}
static bool sortDistance( const RN_NODE_PTR& aOrigin, const RN_NODE_PTR& aNode1,
const RN_NODE_PTR& aNode2 )
{
return getDistance( aOrigin, aNode1 ) < getDistance( aOrigin, aNode2 );
}
static bool sortWeight( const RN_EDGE_PTR& aEdge1, const RN_EDGE_PTR& aEdge2 )
{
return aEdge1->GetWeight() < aEdge2->GetWeight();
}
bool sortArea( const RN_POLY& aP1, const RN_POLY& aP2 )
{
return aP1.m_bbox.GetArea() < aP2.m_bbox.GetArea();
}
bool operator==( const RN_NODE_PTR& aFirst, const RN_NODE_PTR& aSecond )
{
return aFirst->GetX() == aSecond->GetX() && aFirst->GetY() == aSecond->GetY();
}
bool operator!=( const RN_NODE_PTR& aFirst, const RN_NODE_PTR& aSecond )
{
return aFirst->GetX() != aSecond->GetX() || aFirst->GetY() != aSecond->GetY();
}
RN_NODE_AND_FILTER operator&&( const RN_NODE_FILTER& aFilter1, const RN_NODE_FILTER& aFilter2 )
{
return RN_NODE_AND_FILTER( aFilter1, aFilter2 );
}
RN_NODE_OR_FILTER operator||( const RN_NODE_FILTER& aFilter1, const RN_NODE_FILTER& aFilter2 )
{
return RN_NODE_OR_FILTER( aFilter1, aFilter2 );
}
static bool isEdgeConnectingNode( const RN_EDGE_PTR& aEdge, const RN_NODE_PTR& aNode )
{
return aEdge->GetSourceNode() == aNode || aEdge->GetTargetNode() == aNode;
}
static std::vector<RN_EDGE_MST_PTR>* kruskalMST( RN_LINKS::RN_EDGE_LIST& aEdges,
std::vector<RN_NODE_PTR>& aNodes )
{
unsigned int nodeNumber = aNodes.size();
unsigned int mstExpectedSize = nodeNumber - 1;
unsigned int mstSize = 0;
bool ratsnestLines = false;
// The output
std::vector<RN_EDGE_MST_PTR>* mst = new std::vector<RN_EDGE_MST_PTR>;
mst->reserve( mstExpectedSize );
// Set tags for marking cycles
boost::unordered_map<RN_NODE_PTR, int> tags;
unsigned int tag = 0;
BOOST_FOREACH( RN_NODE_PTR& node, aNodes )
{
node->SetTag( tag );
tags[node] = tag++;
}
// Lists of nodes connected together (subtrees) to detect cycles in the graph
std::vector<std::list<int> > cycles( nodeNumber );
for( unsigned int i = 0; i < nodeNumber; ++i )
cycles[i].push_back( i );
// Kruskal algorithm requires edges to be sorted by their weight
aEdges.sort( sortWeight );
while( mstSize < mstExpectedSize && !aEdges.empty() )
{
RN_EDGE_PTR& dt = aEdges.front();
int srcTag = tags[dt->GetSourceNode()];
int trgTag = tags[dt->GetTargetNode()];
// Check if by adding this edge we are going to join two different forests
if( srcTag != trgTag )
{
// Because edges are sorted by their weight, first we always process connected
// items (weight == 0). Once we stumble upon an edge with non-zero weight,
// it means that the rest of the lines are ratsnest.
if( !ratsnestLines && dt->GetWeight() != 0 )
ratsnestLines = true;
// Update tags
std::list<int>::iterator it, itEnd;
if( ratsnestLines )
{
for( it = cycles[trgTag].begin(), itEnd = cycles[trgTag].end(); it != itEnd; ++it )
tags[aNodes[*it]] = srcTag;
}
else
{
for( it = cycles[trgTag].begin(), itEnd = cycles[trgTag].end(); it != itEnd; ++it ) {
tags[aNodes[*it]] = srcTag;
aNodes[*it]->SetTag( srcTag );
}
}
// Move nodes that were marked with old tag to the list marked with the new tag
cycles[srcTag].splice( cycles[srcTag].end(), cycles[trgTag] );
if( ratsnestLines )
{
// Do a copy of edge, but make it RN_EDGE_MST. In contrary to RN_EDGE,
// RN_EDGE_MST saves both source and target node and does not require any other
// edges to exist for getting source/target nodes
RN_EDGE_MST_PTR newEdge = boost::make_shared<RN_EDGE_MST>( dt->GetSourceNode(),
dt->GetTargetNode(),
dt->GetWeight() );
mst->push_back( newEdge );
++mstSize;
}
else
{
// Processing a connection, decrease the expected size of the ratsnest MST
--mstExpectedSize;
}
}
// Remove the edge that was just processed
aEdges.erase( aEdges.begin() );
}
// Probably we have discarded some of edges, so reduce the size
mst->resize( mstSize );
return mst;
}
void RN_NET::validateEdge( RN_EDGE_MST_PTR& aEdge )
{
RN_NODE_PTR source = aEdge->GetSourceNode();
RN_NODE_PTR target = aEdge->GetTargetNode();
bool valid = true;
// If any of nodes belonging to the edge has the flag set,
// change it to the closest node that has flag cleared
if( source->GetFlag() )
{
valid = false;
std::list<RN_NODE_PTR> closest = GetClosestNodes( source, WITHOUT_FLAG() );
BOOST_FOREACH( RN_NODE_PTR& node, closest )
{
if( node && node != target )
{
source = node;
break;
}
}
}
if( target->GetFlag() )
{
valid = false;
std::list<RN_NODE_PTR> closest = GetClosestNodes( target, WITHOUT_FLAG() );
BOOST_FOREACH( RN_NODE_PTR& node, closest )
{
if( node && node != source )
{
target = node;
break;
}
}
}
// Replace an invalid edge with new, valid one
if( !valid )
aEdge.reset( new RN_EDGE_MST( source, target ) );
}
void RN_NET::removeNode( RN_NODE_PTR& aNode, const BOARD_CONNECTED_ITEM* aParent )
{
aNode->RemoveParent( aParent );
if( m_links.RemoveNode( aNode ) )
{
clearNode( aNode );
m_dirty = true;
}
}
void RN_NET::removeEdge( RN_EDGE_MST_PTR& aEdge, const BOARD_CONNECTED_ITEM* aParent )
{
// Save nodes, so they can be cleared later
RN_NODE_PTR start = aEdge->GetSourceNode();
RN_NODE_PTR end = aEdge->GetTargetNode();
start->RemoveParent( aParent );
end->RemoveParent( aParent );
// Connection has to be removed before running RemoveNode(),
// as RN_NODE influences the reference counter
m_links.RemoveConnection( aEdge );
// Remove nodes associated with the edge. It is done in a safe way, there is a check
// if nodes are not used by other edges.
if( m_links.RemoveNode( start ) )
clearNode( start );
if( m_links.RemoveNode( end ) )
clearNode( end );
m_dirty = true;
}
const RN_NODE_PTR& RN_LINKS::AddNode( int aX, int aY )
{
RN_NODE_SET::iterator node;
bool wasNewElement;
boost::tie( node, wasNewElement ) = m_nodes.emplace( boost::make_shared<RN_NODE>( aX, aY ) );
return *node;
}
bool RN_LINKS::RemoveNode( const RN_NODE_PTR& aNode )
{
if( aNode->GetRefCount() == 0 )
{
m_nodes.erase( aNode );
return true;
}
return false;
}
RN_EDGE_MST_PTR RN_LINKS::AddConnection( const RN_NODE_PTR& aNode1, const RN_NODE_PTR& aNode2,
unsigned int aDistance )
{
assert( aNode1 != aNode2 );
RN_EDGE_MST_PTR edge = boost::make_shared<RN_EDGE_MST>( aNode1, aNode2, aDistance );
m_edges.push_back( edge );
return edge;
}
void RN_NET::compute()
{
const RN_LINKS::RN_NODE_SET& boardNodes = m_links.GetNodes();
const RN_LINKS::RN_EDGE_LIST& boardEdges = m_links.GetConnections();
// Special cases do not need complicated algorithms
if( boardNodes.size() <= 2 )
{
m_rnEdges.reset( new std::vector<RN_EDGE_MST_PTR>( 0 ) );
// Check if the only possible connection exists
if( boardEdges.size() == 0 && boardNodes.size() == 2 )
{
RN_LINKS::RN_NODE_SET::iterator last = ++boardNodes.begin();
// There can be only one possible connection, but it is missing
m_rnEdges->push_back( boost::make_shared<RN_EDGE_MST>( *boardNodes.begin(), *last ) );
}
// Set tags to nodes as connected
BOOST_FOREACH( RN_NODE_PTR node, boardNodes )
node->SetTag( 0 );
return;
}
// Move and sort (sorting speeds up) all nodes to a vector for the Delaunay triangulation
std::vector<RN_NODE_PTR> nodes( boardNodes.size() );
std::partial_sort_copy( boardNodes.begin(), boardNodes.end(), nodes.begin(), nodes.end() );
TRIANGULATOR triangulator;
triangulator.CreateDelaunay( nodes.begin(), nodes.end() );
boost::scoped_ptr<RN_LINKS::RN_EDGE_LIST> triangEdges( triangulator.GetEdges() );
// Compute weight/distance for edges resulting from triangulation
RN_LINKS::RN_EDGE_LIST::iterator eit, eitEnd;
for( eit = (*triangEdges).begin(), eitEnd = (*triangEdges).end(); eit != eitEnd; ++eit )
(*eit)->SetWeight( getDistance( (*eit)->GetSourceNode(), (*eit)->GetTargetNode() ) );
// Add the currently existing connections list to the results of triangulation
std::copy( boardEdges.begin(), boardEdges.end(), std::front_inserter( *triangEdges ) );
// Get the minimal spanning tree
m_rnEdges.reset( kruskalMST( *triangEdges, nodes ) );
}
void RN_NET::clearNode( const RN_NODE_PTR& aNode )
{
if( !m_rnEdges )
return;
std::vector<RN_EDGE_MST_PTR>::iterator newEnd;
// Remove all ratsnest edges for associated with the node
newEnd = std::remove_if( m_rnEdges->begin(), m_rnEdges->end(),
boost::bind( isEdgeConnectingNode, _1, boost::cref( aNode ) ) );
m_rnEdges->resize( std::distance( m_rnEdges->begin(), newEnd ) );
}
RN_POLY::RN_POLY( const SHAPE_POLY_SET* aParent,
int aSubpolygonIndex,
RN_LINKS& aConnections, const BOX2I& aBBox ) :
m_subpolygonIndex( aSubpolygonIndex ),
m_bbox( aBBox ),
m_parentPolyset( aParent )
{
const VECTOR2I& p = aParent->CVertex( 0, aSubpolygonIndex );
m_node = aConnections.AddNode( p.x, p.y );
// Mark it as not appropriate as a destination of ratsnest edges
// (edges coming out from a polygon vertex look weird)
m_node->SetFlag( true );
}
bool RN_POLY::HitTest( const RN_NODE_PTR& aNode ) const
{
VECTOR2I p( aNode->GetX(), aNode->GetY() );
return m_parentPolyset->Contains( p, m_subpolygonIndex );
}
void RN_NET::Update()
{
// Add edges resulting from nodes being connected by zones
processZones();
processPads();
compute();
BOOST_FOREACH( RN_EDGE_MST_PTR& edge, *m_rnEdges )
validateEdge( edge );
m_dirty = false;
}
void RN_NET::AddItem( const D_PAD* aPad )
{
RN_NODE_PTR node = m_links.AddNode( aPad->GetPosition().x, aPad->GetPosition().y );
node->AddParent( aPad );
m_pads[aPad].m_Node = node;
m_dirty = true;
}
void RN_NET::AddItem( const VIA* aVia )
{
RN_NODE_PTR node = m_links.AddNode( aVia->GetPosition().x, aVia->GetPosition().y );
node->AddParent( aVia );
m_vias[aVia] = node;
m_dirty = true;
}
void RN_NET::AddItem( const TRACK* aTrack )
{
if( aTrack->GetStart() == aTrack->GetEnd() )
return;
RN_NODE_PTR start = m_links.AddNode( aTrack->GetStart().x, aTrack->GetStart().y );
RN_NODE_PTR end = m_links.AddNode( aTrack->GetEnd().x, aTrack->GetEnd().y );
start->AddParent( aTrack );
end->AddParent( aTrack );
m_tracks[aTrack] = m_links.AddConnection( start, end );
m_dirty = true;
}
void RN_NET::AddItem( const ZONE_CONTAINER* aZone )
{
// Prepare a list of polygons (every zone can contain one or more polygons)
const SHAPE_POLY_SET& polySet = aZone->GetFilledPolysList();
for( int i = 0; i < polySet.OutlineCount(); ++i )
{
const SHAPE_LINE_CHAIN& path = polySet.COutline( i );
RN_POLY poly = RN_POLY( &polySet, i, m_links, path.BBox() );
m_zones[aZone].m_Polygons.push_back( poly );
}
m_dirty = true;
}
void RN_NET::RemoveItem( const D_PAD* aPad )
{
PAD_NODE_MAP::iterator it = m_pads.find( aPad );
if( it == m_pads.end() )
return;
RN_PAD_DATA& pad_data = it->second;
removeNode( pad_data.m_Node, aPad );
BOOST_FOREACH( RN_EDGE_MST_PTR& edge, pad_data.m_Edges )
removeEdge( edge, aPad );
m_pads.erase( aPad );
}
void RN_NET::RemoveItem( const VIA* aVia )
{
VIA_NODE_MAP::iterator it = m_vias.find( aVia );
if( it == m_vias.end() )
return;
removeNode( it->second, aVia );
m_vias.erase( it );
}
void RN_NET::RemoveItem( const TRACK* aTrack )
{
TRACK_EDGE_MAP::iterator it = m_tracks.find( aTrack );
if( it == m_tracks.end() )
return;
removeEdge( it->second, aTrack );
m_tracks.erase( it );
}
void RN_NET::RemoveItem( const ZONE_CONTAINER* aZone )
{
ZONE_DATA_MAP::iterator it = m_zones.find( aZone );
if( it == m_zones.end() )
return;
RN_ZONE_DATA& zoneData = it->second;
// Remove all subpolygons that make the zone
std::deque<RN_POLY>& polygons = zoneData.m_Polygons;
BOOST_FOREACH( RN_POLY& polygon, polygons )
removeNode( polygon.GetNode(), aZone );
polygons.clear();
// Remove all connections added by the zone
std::deque<RN_EDGE_MST_PTR>& edges = zoneData.m_Edges;
BOOST_FOREACH( RN_EDGE_MST_PTR edge, edges )
removeEdge( edge, aZone );
edges.clear();
m_zones.erase( it );
}
const RN_NODE_PTR RN_NET::GetClosestNode( const RN_NODE_PTR& aNode ) const
{
const RN_LINKS::RN_NODE_SET& nodes = m_links.GetNodes();
RN_LINKS::RN_NODE_SET::const_iterator it, itEnd;
unsigned int minDistance = std::numeric_limits<unsigned int>::max();
RN_NODE_PTR closest;
for( it = nodes.begin(), itEnd = nodes.end(); it != itEnd; ++it )
{
RN_NODE_PTR node = *it;
// Obviously the distance between node and itself is the shortest,
// that's why we have to skip it
if( node != aNode )
{
unsigned int distance = getDistance( node, aNode );
if( distance < minDistance )
{
minDistance = distance;
closest = node;
}
}
}
return closest;
}
const RN_NODE_PTR RN_NET::GetClosestNode( const RN_NODE_PTR& aNode,
const RN_NODE_FILTER& aFilter ) const
{
const RN_LINKS::RN_NODE_SET& nodes = m_links.GetNodes();
RN_LINKS::RN_NODE_SET::const_iterator it, itEnd;
unsigned int minDistance = std::numeric_limits<unsigned int>::max();
RN_NODE_PTR closest;
for( it = nodes.begin(), itEnd = nodes.end(); it != itEnd; ++it )
{
RN_NODE_PTR node = *it;
// Obviously the distance between node and itself is the shortest,
// that's why we have to skip it
if( node != aNode && aFilter( node ) )
{
unsigned int distance = getDistance( node, aNode );
if( distance < minDistance )
{
minDistance = distance;
closest = node;
}
}
}
return closest;
}
std::list<RN_NODE_PTR> RN_NET::GetClosestNodes( const RN_NODE_PTR& aNode, int aNumber ) const
{
std::list<RN_NODE_PTR> closest;
const RN_LINKS::RN_NODE_SET& nodes = m_links.GetNodes();
// Copy nodes
BOOST_FOREACH( const RN_NODE_PTR& node, nodes )
closest.push_back( node );
// Sort by the distance from aNode
closest.sort( boost::bind( sortDistance, boost::cref( aNode ), _1, _2 ) );
// aNode should not be returned in the results
closest.remove( aNode );
// Trim the result to the asked size
if( aNumber > 0 )
closest.resize( std::min( (size_t)aNumber, nodes.size() ) );
return closest;
}
std::list<RN_NODE_PTR> RN_NET::GetClosestNodes( const RN_NODE_PTR& aNode,
const RN_NODE_FILTER& aFilter, int aNumber ) const
{
std::list<RN_NODE_PTR> closest;
const RN_LINKS::RN_NODE_SET& nodes = m_links.GetNodes();
// Copy nodes
BOOST_FOREACH( const RN_NODE_PTR& node, nodes )
closest.push_back( node );
// Sort by the distance from aNode
closest.sort( boost::bind( sortDistance, boost::cref( aNode ), _1, _2 ) );
// aNode should not be returned in the results
closest.remove( aNode );
// Filter out by condition
std::remove_if( closest.begin(), closest.end(), aFilter );
// Trim the result to the asked size
if( aNumber > 0 )
closest.resize( std::min( static_cast<size_t>( aNumber ), nodes.size() ) );
return closest;
}
void RN_NET::AddSimple( const BOARD_CONNECTED_ITEM* aItem )
{
BOOST_FOREACH( RN_NODE_PTR node, GetNodes( aItem ) )
{
// Block all nodes, so they do not become targets for dynamic ratsnest lines
AddBlockedNode( node );
// Filter out junctions
if( node->GetRefCount() == 1 )
m_simpleNodes.insert( node );
}
}
std::list<RN_NODE_PTR> RN_NET::GetNodes( const BOARD_CONNECTED_ITEM* aItem ) const
{
std::list<RN_NODE_PTR> nodes;
try
{
switch( aItem->Type() )
{
case PCB_PAD_T:
{
const D_PAD* pad = static_cast<const D_PAD*>( aItem );
nodes.push_back( m_pads.at( pad ).m_Node );
}
break;
case PCB_VIA_T:
{
const VIA* via = static_cast<const VIA*>( aItem );
nodes.push_back( m_vias.at( via ) );
}
break;
case PCB_TRACE_T:
{
const TRACK* track = static_cast<const TRACK*>( aItem );
const RN_EDGE_MST_PTR& edge = m_tracks.at( track );
nodes.push_back( edge->GetSourceNode() );
nodes.push_back( edge->GetTargetNode() );
}
break;
case PCB_ZONE_AREA_T:
{
const ZONE_CONTAINER* zone = static_cast<const ZONE_CONTAINER*>( aItem );
const std::deque<RN_POLY>& polys = m_zones.at( zone ).m_Polygons;
for( std::deque<RN_POLY>::const_iterator it = polys.begin(); it != polys.end(); ++it )
nodes.push_back( it->GetNode() );
}
break;
default:
break;
}
}
catch( ... )
{
// It is fine, just return empty list of nodes
}
return nodes;
}
void RN_NET::GetAllItems( std::list<BOARD_CONNECTED_ITEM*>& aOutput, RN_ITEM_TYPE aType ) const
{
if( aType & RN_PADS )
{
BOOST_FOREACH( const BOARD_CONNECTED_ITEM* item, m_pads | boost::adaptors::map_keys )
aOutput.push_back( const_cast<BOARD_CONNECTED_ITEM*>( item ) );
}
if( aType & RN_VIAS )
{
BOOST_FOREACH( const BOARD_CONNECTED_ITEM* item, m_vias | boost::adaptors::map_keys )
aOutput.push_back( const_cast<BOARD_CONNECTED_ITEM*>( item ) );
}
if( aType & RN_TRACKS )
{
BOOST_FOREACH( const BOARD_CONNECTED_ITEM* item, m_tracks | boost::adaptors::map_keys )
aOutput.push_back( const_cast<BOARD_CONNECTED_ITEM*>( item ) );
}
if( aType & RN_ZONES )
{
BOOST_FOREACH( const BOARD_CONNECTED_ITEM* item, m_zones | boost::adaptors::map_keys )
aOutput.push_back( const_cast<BOARD_CONNECTED_ITEM*>( item ) );
}
}
void RN_NET::ClearSimple()
{
BOOST_FOREACH( const RN_NODE_PTR& node, m_blockedNodes )
node->SetFlag( false );
m_blockedNodes.clear();
m_simpleNodes.clear();
}
void RN_NET::GetConnectedItems( const BOARD_CONNECTED_ITEM* aItem,
std::list<BOARD_CONNECTED_ITEM*>& aOutput,
RN_ITEM_TYPE aTypes ) const
{
std::list<RN_NODE_PTR> nodes = GetNodes( aItem );
assert( !nodes.empty() );
int tag = nodes.front()->GetTag();
assert( tag >= 0 );
if( aTypes & RN_PADS )
{
for( PAD_NODE_MAP::const_iterator it = m_pads.begin(); it != m_pads.end(); ++it )
{
if( it->second.m_Node->GetTag() == tag )
aOutput.push_back( const_cast<D_PAD*>( it->first ) );
}
}
if( aTypes & RN_VIAS )
{
for( VIA_NODE_MAP::const_iterator it = m_vias.begin(); it != m_vias.end(); ++it )
{
if( it->second->GetTag() == tag )
aOutput.push_back( const_cast<VIA*>( it->first ) );
}
}
if( aTypes & RN_TRACKS )
{
for( TRACK_EDGE_MAP::const_iterator it = m_tracks.begin(); it != m_tracks.end(); ++it )
{
if( it->second->GetTag() == tag )
aOutput.push_back( const_cast<TRACK*>( it->first ) );
}
}
if( aTypes & RN_ZONES )
{
for( ZONE_DATA_MAP::const_iterator it = m_zones.begin(); it != m_zones.end(); ++it )
{
BOOST_FOREACH( const RN_EDGE_MST_PTR& edge, it->second.m_Edges )
{
if( edge->GetTag() == tag )
{
aOutput.push_back( const_cast<ZONE_CONTAINER*>( it->first ) );
break;
}
}
}
}
}
void RN_DATA::AddSimple( const BOARD_ITEM* aItem )
{
int net;
if( aItem->IsConnected() )
{
const BOARD_CONNECTED_ITEM* item = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
net = item->GetNetCode();
if( net < 1 ) // do not process unconnected items
return;
m_nets[net].AddSimple( item );
}
else if( aItem->Type() == PCB_MODULE_T )
{
const MODULE* module = static_cast<const MODULE*>( aItem );
for( const D_PAD* pad = module->Pads().GetFirst(); pad; pad = pad->Next() )
AddSimple( pad );
return;
}
else
return;
}
void RN_DATA::AddBlocked( const BOARD_ITEM* aItem )
{
int net;
if( aItem->IsConnected() )
{
const BOARD_CONNECTED_ITEM* item = static_cast<const BOARD_CONNECTED_ITEM*>( aItem );
net = item->GetNetCode();
if( net < 1 ) // do not process unconnected items
return;
// Block all nodes belonging to the item
BOOST_FOREACH( RN_NODE_PTR node, m_nets[net].GetNodes( item ) )
m_nets[net].AddBlockedNode( node );
}
else if( aItem->Type() == PCB_MODULE_T )
{
const MODULE* module = static_cast<const MODULE*>( aItem );
for( const D_PAD* pad = module->Pads().GetFirst(); pad; pad = pad->Next() )
AddBlocked( pad );
return;
}
else
return;
}
void RN_DATA::GetConnectedItems( const BOARD_CONNECTED_ITEM* aItem,
std::list<BOARD_CONNECTED_ITEM*>& aOutput,
RN_ITEM_TYPE aTypes ) const
{
int net = aItem->GetNetCode();
if( net < 1 )
return;
assert( net < (int) m_nets.size() );
m_nets[net].GetConnectedItems( aItem, aOutput, aTypes );
}
void RN_DATA::GetNetItems( int aNetCode, std::list<BOARD_CONNECTED_ITEM*>& aOutput,
RN_ITEM_TYPE aTypes ) const
{
if( aNetCode < 1 )
return;
assert( aNetCode < (int) m_nets.size() );
m_nets[aNetCode].GetAllItems( aOutput, aTypes );
}
bool RN_DATA::AreConnected( const BOARD_CONNECTED_ITEM* aItem, const BOARD_CONNECTED_ITEM* aOther )
{
int net1 = aItem->GetNetCode();
int net2 = aOther->GetNetCode();
if( net1 < 1 || net2 < 1 || net1 != net2 )
return false;
assert( net1 < (int) m_nets.size() && net2 < (int) m_nets.size() );
// net1 == net2
std::list<RN_NODE_PTR> items1 = m_nets[net1].GetNodes( aItem );
std::list<RN_NODE_PTR> items2 = m_nets[net1].GetNodes( aOther );
assert( !items1.empty() && !items2.empty() );
return ( items1.front()->GetTag() == items2.front()->GetTag() );
}
int RN_DATA::GetUnconnectedCount() const
{
int count = 0;
for( unsigned i = 0; i < m_nets.size(); ++i )
{
const std::vector<RN_EDGE_MST_PTR>* unconnected = m_nets[i].GetUnconnected();
if( unconnected )
count += unconnected->size();
}
return count;
}
void RN_NET::processZones()
{
for( ZONE_DATA_MAP::iterator it = m_zones.begin(); it != m_zones.end(); ++it )
{
const ZONE_CONTAINER* zone = it->first;
RN_ZONE_DATA& zoneData = it->second;
// Reset existing connections
BOOST_FOREACH( RN_EDGE_MST_PTR edge, zoneData.m_Edges )
m_links.RemoveConnection( edge );
zoneData.m_Edges.clear();
LSET layers = zone->GetLayerSet();
// Compute new connections
RN_LINKS::RN_NODE_SET candidates = m_links.GetNodes();
RN_LINKS::RN_NODE_SET::iterator point, pointEnd;
// Sorting by area should speed up the processing, as smaller polygons are computed
// faster and may reduce the number of points for further checks
std::sort( zoneData.m_Polygons.begin(), zoneData.m_Polygons.end(), sortArea );
for( std::deque<RN_POLY>::iterator poly = zoneData.m_Polygons.begin(),
polyEnd = zoneData.m_Polygons.end(); poly != polyEnd; ++poly )
{
const RN_NODE_PTR& node = poly->GetNode();
point = candidates.begin();
pointEnd = candidates.end();
while( point != pointEnd )
{
if( *point != node && ( (*point)->GetLayers() & layers ).any()
&& poly->HitTest( *point ) )
{
//(*point)->AddParent( zone ); // do not assign parent for helper links
RN_EDGE_MST_PTR connection = m_links.AddConnection( node, *point );
zoneData.m_Edges.push_back( connection );
// This point already belongs to a polygon, we do not need to check it anymore
point = candidates.erase( point );
pointEnd = candidates.end();
}
else
{
++point;
}
}
}
}
}
void RN_NET::processPads()
{
for( PAD_NODE_MAP::iterator it = m_pads.begin(); it != m_pads.end(); ++it )
{
const D_PAD* pad = it->first;
RN_NODE_PTR node = it->second.m_Node;
std::deque<RN_EDGE_MST_PTR>& edges = it->second.m_Edges;
// Reset existing connections
BOOST_FOREACH( RN_EDGE_MST_PTR edge, edges )
m_links.RemoveConnection( edge );
LSET layers = pad->GetLayerSet();
RN_LINKS::RN_NODE_SET candidates = m_links.GetNodes();
RN_LINKS::RN_NODE_SET::iterator point, pointEnd;
point = candidates.begin();
pointEnd = candidates.end();
while( point != pointEnd )
{
if( *point != node && ( (*point)->GetLayers() & layers ).any() &&
pad->HitTest( wxPoint( (*point)->GetX(), (*point)->GetY() ) ) )
{
//(*point)->AddParent( pad ); // do not assign parent for helper links
RN_EDGE_MST_PTR connection = m_links.AddConnection( node, *point );
edges.push_back( connection );
}
++point;
}
}
}
void RN_DATA::Add( const BOARD_ITEM* aItem )
{
int net;
if( aItem->IsConnected() )
{
net = static_cast<const BOARD_CONNECTED_ITEM*>( aItem )->GetNetCode();
if( net < 1 ) // do not process unconnected items
return;
if( net >= (int) m_nets.size() ) // Autoresize
m_nets.resize( net + 1 );
}
else if( aItem->Type() == PCB_MODULE_T )
{
const MODULE* module = static_cast<const MODULE*>( aItem );
for( const D_PAD* pad = module->Pads().GetFirst(); pad; pad = pad->Next() )
{
net = pad->GetNetCode();
if( net < 1 ) // do not process unconnected items
continue;
if( net >= (int) m_nets.size() ) // Autoresize
m_nets.resize( net + 1 );
m_nets[net].AddItem( pad );
}
return;
}
else
return;
switch( aItem->Type() )
{
case PCB_PAD_T:
m_nets[net].AddItem( static_cast<const D_PAD*>( aItem ) );
break;
case PCB_TRACE_T:
m_nets[net].AddItem( static_cast<const TRACK*>( aItem ) );
break;
case PCB_VIA_T:
m_nets[net].AddItem( static_cast<const VIA*>( aItem ) );
break;
case PCB_ZONE_AREA_T:
m_nets[net].AddItem( static_cast<const ZONE_CONTAINER*>( aItem ) );
break;
default:
break;
}
}
void RN_DATA::Remove( const BOARD_ITEM* aItem )
{
int net;
if( aItem->IsConnected() )
{
net = static_cast<const BOARD_CONNECTED_ITEM*>( aItem )->GetNetCode();
if( net < 1 ) // do not process unconnected items
return;
#ifdef NDEBUG
if( net >= (int) m_nets.size() ) // Autoresize
{
m_nets.resize( net + 1 );
return; // if it was resized, then surely the item had not been added before
}
#endif
assert( net < (int) m_nets.size() );
}
else if( aItem->Type() == PCB_MODULE_T )
{
const MODULE* module = static_cast<const MODULE*>( aItem );
for( const D_PAD* pad = module->Pads().GetFirst(); pad; pad = pad->Next() )
{
net = pad->GetNetCode();
if( net < 1 ) // do not process unconnected items
continue;
#ifdef NDEBUG
if( net >= (int) m_nets.size() ) // Autoresize
{
m_nets.resize( net + 1 );
return; // if it was resized, then surely the item had not been added before
}
#endif
assert( net < (int) m_nets.size() );
m_nets[net].RemoveItem( pad );
}
return;
}
else
return;
switch( aItem->Type() )
{
case PCB_PAD_T:
m_nets[net].RemoveItem( static_cast<const D_PAD*>( aItem ) );
break;
case PCB_TRACE_T:
m_nets[net].RemoveItem( static_cast<const TRACK*>( aItem ) );
break;
case PCB_VIA_T:
m_nets[net].RemoveItem( static_cast<const VIA*>( aItem ) );
break;
case PCB_ZONE_AREA_T:
m_nets[net].RemoveItem( static_cast<const ZONE_CONTAINER*>( aItem ) );
break;
default:
break;
}
}
void RN_DATA::Update( const BOARD_ITEM* aItem )
{
Remove( aItem );
Add( aItem );
}
void RN_DATA::ProcessBoard()
{
int netCount = m_board->GetNetCount();
m_nets.clear();
m_nets.resize( netCount );
int netCode;
// Iterate over all items that may need to be connected
for( MODULE* module = m_board->m_Modules; module; module = module->Next() )
{
for( D_PAD* pad = module->Pads().GetFirst(); pad; pad = pad->Next() )
{
netCode = pad->GetNetCode();
assert( netCode >= 0 && netCode < netCount );
if( netCode > 0 && netCode < netCount )
m_nets[netCode].AddItem( pad );
}
}
for( TRACK* track = m_board->m_Track; track; track = track->Next() )
{
netCode = track->GetNetCode();
assert( netCode >= 0 && netCode < netCount );
if( netCode > 0 && netCode < netCount )
{
if( track->Type() == PCB_VIA_T )
m_nets[netCode].AddItem( static_cast<VIA*>( track ) );
else if( track->Type() == PCB_TRACE_T )
m_nets[netCode].AddItem( track );
}
}
for( int i = 0; i < m_board->GetAreaCount(); ++i )
{
ZONE_CONTAINER* zone = m_board->GetArea( i );
netCode = zone->GetNetCode();
assert( netCode >= 0 && netCode < netCount );
if( netCode > 0 && netCode < netCount )
m_nets[netCode].AddItem( zone );
}
Recalculate();
}
void RN_DATA::Recalculate( int aNet )
{
unsigned int netCount = m_board->GetNetCount();
if( netCount > m_nets.size() )
m_nets.resize( netCount );
if( aNet < 0 && netCount > 1 ) // Recompute everything
{
#ifdef PROFILE
prof_counter totalRealTime;
prof_start( &totalRealTime );
#endif
unsigned int i;
#ifdef USE_OPENMP
#pragma omp parallel shared(netCount) private(i)
{
#pragma omp for schedule(guided, 1)
#else /* USE_OPENMP */
{
#endif
// Start with net number 1, as 0 stands for not connected
for( i = 1; i < netCount; ++i )
{
if( m_nets[i].IsDirty() )
updateNet( i );
}
} /* end of parallel section */
#ifdef PROFILE
prof_end( &totalRealTime );
wxLogDebug( wxT( "Recalculate all nets: %.1f ms" ), totalRealTime.msecs() );
#endif /* PROFILE */
}
else if( aNet > 0 ) // Recompute only specific net
{
updateNet( aNet );
}
}
void RN_DATA::updateNet( int aNetCode )
{
assert( aNetCode < (int) m_nets.size() );
if( aNetCode < 1 || aNetCode > (int) m_nets.size() )
return;
m_nets[aNetCode].ClearSimple();
m_nets[aNetCode].Update();
}
|