forked from nikropht/FreeRouting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolylineTrace.java
More file actions
1407 lines (1342 loc) · 51.2 KB
/
Copy pathPolylineTrace.java
File metadata and controls
1407 lines (1342 loc) · 51.2 KB
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
/*
* Copyright (C) 2014 Alfons Wirtz
* website www.freerouting.net
*
* 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 3 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 at <http://www.gnu.org/licenses/>
* for more details.
*/
package board;
import datastructures.Signum;
import datastructures.Stoppable;
import geometry.planar.IntBox;
import geometry.planar.IntOctagon;
import geometry.planar.Line;
import geometry.planar.LineSegment;
import geometry.planar.Point;
import geometry.planar.IntPoint;
import geometry.planar.FloatPoint;
import geometry.planar.Polyline;
import geometry.planar.Shape;
import geometry.planar.TileShape;
import geometry.planar.Direction;
import geometry.planar.Vector;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import boardgraphics.GraphicsContext;
/**
*
* Objects of class Trace, whose geometry is described by a Polyline
*
*
* @author Alfons Wirtz
*/
public class PolylineTrace extends Trace implements java.io.Serializable
{
/**
* creates a new instance of a PolylineTrace with the input data
*/
public PolylineTrace(Polyline p_polyline, int p_layer, int p_half_width,
int[] p_net_no_arr, int p_clearance_type, int p_id_no,
int p_group_no, FixedState p_fixed_state, BasicBoard p_board)
{
super(p_layer, p_half_width, p_net_no_arr, p_clearance_type,
p_id_no, p_group_no, p_fixed_state, p_board);
if (p_polyline.arr.length < 3)
{
System.out.println("PolylineTrace: p_polyline.arr.length >= 3 expected");
}
lines = p_polyline;
}
public Item copy(int p_id_no)
{
int[] curr_net_no_arr = new int[this.net_count()];
for (int i = 0; i < curr_net_no_arr.length; ++i)
{
curr_net_no_arr[i] = get_net_no(i);
}
return new PolylineTrace(lines, get_layer(), get_half_width(), curr_net_no_arr, clearance_class_no(),
p_id_no, get_component_no(), get_fixed_state(), board);
}
/**
* checks, if this trace is on layer p_layer
*/
public boolean is_on_layer(int p_layer)
{
return get_layer() == p_layer;
}
/**
* returns the first corner of this trace, which is the intersection
* of the first and second lines of its polyline
*/
public Point first_corner()
{
return lines.corner(0);
}
/**
* returns the last corner of this trace, which is the intersection
* of the last two lines of its polyline
*/
public Point last_corner()
{
return lines.corner(lines.arr.length - 2);
}
/**
* returns the number of corners of this trace, which is the
* number of lines of its polyline minus one
*/
public int corner_count()
{
return lines.arr.length - 1;
}
public double get_length()
{
return lines.length_approx();
}
public IntBox bounding_box()
{
IntBox result = this.lines.bounding_box();
return result.offset(this.get_half_width());
}
public void draw(Graphics p_g, GraphicsContext p_graphics_context, Color[] p_color_arr, double p_intensity)
{
if (p_graphics_context == null)
{
return;
}
int layer = this.get_layer();
Color color = p_color_arr[layer];
double display_width = get_half_width();
double intensity = p_intensity * p_graphics_context.get_layer_visibility(layer);
p_graphics_context.draw(lines.corner_approx_arr(), display_width, color, p_g, intensity);
}
/**
* Returns the polyline of this trace.
*/
public Polyline polyline()
{
return lines;
}
protected TileShape[] calculate_tree_shapes(ShapeSearchTree p_search_tree)
{
return p_search_tree.calculate_tree_shapes(this);
}
/**
* returns the count of tile shapes of this polyline
*/
public int tile_shape_count()
{
return Math.max(lines.arr.length - 2, 0);
}
public void translate_by(Vector p_vector)
{
lines = lines.translate_by(p_vector);
this.clear_derived_data();
}
public void turn_90_degree(int p_factor, IntPoint p_pole)
{
lines = lines.turn_90_degree(p_factor, p_pole);
this.clear_derived_data();
}
public void rotate_approx(double p_angle_in_degree, FloatPoint p_pole)
{
this.lines = this.lines.rotate_approx(Math.toRadians(p_angle_in_degree), p_pole);
}
public void change_placement_side(IntPoint p_pole)
{
lines = lines.mirror_vertical(p_pole);
if (this.board != null)
{
this.set_layer(board.get_layer_count() - this.get_layer() - 1);
}
this.clear_derived_data();
}
/**
* Looks, if other traces can be combined with this trace.
* Returns true, if somthing has been combined.
* This trace will be the combined trace, so that only other traces may be deleted.
*/
public boolean combine()
{
if (!this.is_on_the_board())
{
return false;
}
boolean something_changed;
if (this.combine_at_start(true))
{
something_changed = true;
this.combine();
}
else if (this.combine_at_end(true))
{
something_changed = true;
this.combine();
}
else
{
something_changed = false;
}
if (something_changed)
{
// let the observers syncronize the changes
board.communication.observers.notify_changed(this);
board.additional_update_after_change(this);
}
return something_changed;
}
/**
* looks, if this trace can be combined at its first point with
* an other trace. Returns true, if somthing was combined.
* The corners of the other trace will be inserted in front of thie trace.
* In case of combine the other trace will be deleted and this trace will
* remain.
*/
private boolean combine_at_start(boolean p_ignore_areas)
{
Point start_corner = first_corner();
Collection<Item> contacts = get_normal_contacts(start_corner, false);
if (p_ignore_areas)
{
// remove conduction areas from the list
Iterator<Item> it = contacts.iterator();
while (it.hasNext())
{
if (it.next() instanceof ConductionArea)
{
it.remove();
}
}
}
if (contacts.size() != 1)
{
return false;
}
PolylineTrace other_trace = null;
boolean trace_found = false;
boolean reverse_order = false;
Iterator<Item> it = contacts.iterator();
while (it.hasNext())
{
Item curr_ob = it.next();
if (curr_ob instanceof PolylineTrace)
{
other_trace = (PolylineTrace) curr_ob;
if (other_trace.get_layer() == get_layer() && other_trace.nets_equal(this) && other_trace.get_half_width() == get_half_width() && other_trace.get_fixed_state() == this.get_fixed_state())
{
if (start_corner.equals(other_trace.last_corner()))
{
trace_found = true;
break;
}
else if (start_corner.equals(other_trace.first_corner()))
{
reverse_order = true;
trace_found = true;
break;
}
}
}
}
if (!trace_found)
{
return false;
}
board.item_list.save_for_undo(this);
// create the lines of the joined polyline
Line[] this_lines = lines.arr;
Line[] other_lines;
if (reverse_order)
{
other_lines = new Line[other_trace.lines.arr.length];
for (int i = 0; i < other_lines.length; ++i)
{
other_lines[i] = other_trace.lines.arr[other_lines.length - 1 - i].opposite();
}
}
else
{
other_lines = other_trace.lines.arr;
}
boolean skip_line =
other_lines[other_lines.length - 2].is_equal_or_opposite(this_lines[1]);
int new_line_count = this_lines.length + other_lines.length - 2;
if (skip_line)
{
--new_line_count;
}
Line[] new_lines = new Line[new_line_count];
System.arraycopy(other_lines, 0, new_lines, 0, other_lines.length - 1);
int join_pos = other_lines.length - 1;
if (skip_line)
{
--join_pos;
}
System.arraycopy(this_lines, 1, new_lines, join_pos, this_lines.length - 1);
Polyline joined_polyline = new Polyline(new_lines);
if (joined_polyline.arr.length != new_line_count)
{
// consecutive parallel lines where skipped at the join location
// combine without performance optimation
board.search_tree_manager.remove(this);
this.lines = joined_polyline;
this.clear_derived_data();
board.search_tree_manager.insert(this);
}
else
{
// reuse the tree entries for better performance
// create the changed line shape at the join location
int to_no = other_lines.length;
if (skip_line)
{
--to_no;
}
board.search_tree_manager.merge_entries_in_front(other_trace, this, joined_polyline,
other_lines.length - 3, to_no);
other_trace.clear_search_tree_entries();
this.lines = joined_polyline;
}
if (this.lines.arr.length < 3)
{
board.remove_item(this);
}
board.remove_item(other_trace);
if (board instanceof RoutingBoard)
{
((RoutingBoard) board).join_changed_area(start_corner.to_float(), get_layer());
}
return true;
}
/**
* looks, if this trace can be combined at its last point with
* another trace. Returns true, if somthing was combined.
* The corners of the other trace will be inserted at the end of thie trace.
* In case of combine the other trace will be deleted and this trace will
* remain.
*/
private boolean combine_at_end(boolean p_ignore_areas)
{
Point end_corner = last_corner();
Collection<Item> contacts = get_normal_contacts(end_corner, false);
if (p_ignore_areas)
{
// remove conduction areas from the list
Iterator<Item> it = contacts.iterator();
while (it.hasNext())
{
if (it.next() instanceof ConductionArea)
{
it.remove();
}
}
}
if (contacts.size() != 1)
{
return false;
}
PolylineTrace other_trace = null;
boolean trace_found = false;
boolean reverse_order = false;
Iterator<Item> it = contacts.iterator();
while (it.hasNext())
{
Item curr_ob = it.next();
if (curr_ob instanceof PolylineTrace)
{
other_trace = (PolylineTrace) curr_ob;
if (other_trace.get_layer() == get_layer() && other_trace.nets_equal(this) && other_trace.get_half_width() == get_half_width() && other_trace.get_fixed_state() == this.get_fixed_state())
{
if (end_corner.equals(other_trace.first_corner()))
{
trace_found = true;
break;
}
else if (end_corner.equals(other_trace.last_corner()))
{
reverse_order = true;
trace_found = true;
break;
}
}
}
}
if (!trace_found)
{
return false;
}
board.item_list.save_for_undo(this);
// create the lines of the joined polyline
Line[] this_lines = lines.arr;
Line[] other_lines;
if (reverse_order)
{
other_lines = new Line[other_trace.lines.arr.length];
for (int i = 0; i < other_lines.length; ++i)
{
other_lines[i] = other_trace.lines.arr[other_lines.length - 1 - i].opposite();
}
}
else
{
other_lines = other_trace.lines.arr;
}
boolean skip_line =
this_lines[this_lines.length - 2].is_equal_or_opposite(other_lines[1]);
int new_line_count = this_lines.length + other_lines.length - 2;
if (skip_line)
{
--new_line_count;
}
Line[] new_lines = new Line[new_line_count];
System.arraycopy(this_lines, 0, new_lines, 0, this_lines.length - 1);
int join_pos = this_lines.length - 1;
if (skip_line)
{
--join_pos;
}
System.arraycopy(other_lines, 1, new_lines, join_pos, other_lines.length - 1);
Polyline joined_polyline = new Polyline(new_lines);
if (joined_polyline.arr.length != new_line_count)
{
// consecutive parallel lines where skipped at the join location
// combine without performance optimation
board.search_tree_manager.remove(this);
this.clear_search_tree_entries();
this.lines = joined_polyline;
this.clear_derived_data();
board.search_tree_manager.insert(this);
}
else
{
// reuse tree entries for better performance
// create the changed line shape at the join location
int to_no = this_lines.length;
if (skip_line)
{
--to_no;
}
board.search_tree_manager.merge_entries_at_end(other_trace, this, joined_polyline, this_lines.length - 3, to_no);
other_trace.clear_search_tree_entries();
this.lines = joined_polyline;
}
if (this.lines.arr.length < 3)
{
board.remove_item(this);
}
board.remove_item(other_trace);
if (board instanceof RoutingBoard)
{
((RoutingBoard) board).join_changed_area(end_corner.to_float(), get_layer());
}
return true;
}
/**
* Looks up traces intersecting with this trace and splits them at the intersection points.
* In case of an overlaps, the traces are split at their first and their last common point.
* Returns the pieces resulting from splitting.
* Found cycles are removed.
* If nothing is split, the result will contain just this Trace.
* If p_clip_shape != null, the split may be resticted to p_clip_shape.
*/
public Collection<PolylineTrace> split(IntOctagon p_clip_shape)
{
Collection<PolylineTrace> result = new LinkedList<PolylineTrace>();
if (!this.nets_normal())
{
// only normal nets are split
result.add(this);
return result;
}
boolean own_trace_split = false;
ShapeSearchTree default_tree = board.search_tree_manager.get_default_tree();
for (int i = 0; i < this.lines.arr.length - 2; ++i)
{
if (p_clip_shape != null)
{
LineSegment curr_segment = new LineSegment(this.lines, i + 1);
if (!p_clip_shape.intersects(curr_segment.bounding_box()))
{
continue;
}
}
TileShape curr_shape = this.get_tree_shape(default_tree, i);
LineSegment curr_line_segment = new LineSegment(this.lines, i + 1);
Collection<ShapeSearchTree.TreeEntry> overlapping_tree_entries = new LinkedList<ShapeSearchTree.TreeEntry>();
// look for intersecting traces with the i-th line segment
default_tree.overlapping_tree_entries(curr_shape, get_layer(), overlapping_tree_entries);
Iterator<ShapeSearchTree.TreeEntry> it = overlapping_tree_entries.iterator();
while (it.hasNext())
{
if (!this.is_on_the_board())
{
// this trace has been deleted in a cleanup operation
return result;
}
ShapeSearchTree.TreeEntry found_entry = it.next();
if (!(found_entry.object instanceof Item))
{
continue;
}
Item found_item = (Item) found_entry.object;
if (found_item == this)
{
if (found_entry.shape_index_in_object >= i - 1 && found_entry.shape_index_in_object <= i + 1)
{
// don't split own trace at this line or at neighbour lines
continue;
}
// try to handle intermediate segments of length 0 by comparing end corners
if (i < found_entry.shape_index_in_object)
{
if (lines.corner(i + 1).equals(lines.corner(found_entry.shape_index_in_object)))
{
continue;
}
}
else if (found_entry.shape_index_in_object < i)
{
if (lines.corner(found_entry.shape_index_in_object + 1).equals(lines.corner(i)))
{
continue;
}
}
}
if (!found_item.shares_net(this))
{
continue;
}
if (found_item instanceof PolylineTrace)
{
PolylineTrace found_trace = (PolylineTrace) found_item;
LineSegment found_line_segment =
new LineSegment(found_trace.lines, found_entry.shape_index_in_object + 1);
Line[] intersecting_lines = found_line_segment.intersection(curr_line_segment);
Collection<PolylineTrace> split_pieces = new LinkedList<PolylineTrace>();
// try splitting the found trace first
boolean found_trace_split = false;
if (found_trace != this)
{
for (int j = 0; j < intersecting_lines.length; ++j)
{
int line_no = found_entry.shape_index_in_object + 1;
PolylineTrace[] curr_split_pieces = found_trace.split(line_no, intersecting_lines[j]);
if (curr_split_pieces != null)
{
for (int k = 0; k < 2; ++k)
{
if (curr_split_pieces[k] != null)
{
found_trace_split = true;
split_pieces.add(curr_split_pieces[k]);
}
}
if (found_trace_split)
{
// reread the overlapping tree entries and reset the iterator,
// because the board has changed
default_tree.overlapping_tree_entries(curr_shape, get_layer(), overlapping_tree_entries);
it = overlapping_tree_entries.iterator();
break;
}
}
}
if (!found_trace_split)
{
split_pieces.add(found_trace);
}
}
// now try splitting the own trace
intersecting_lines = curr_line_segment.intersection(found_line_segment);
for (int j = 0; j < intersecting_lines.length; ++j)
{
PolylineTrace[] curr_split_pieces = split(i + 1, intersecting_lines[j]);
if (curr_split_pieces != null)
{
own_trace_split = true;
// this trace was split itself into 2.
if (curr_split_pieces[0] != null)
{
result.addAll(curr_split_pieces[0].split(p_clip_shape));
}
if (curr_split_pieces[1] != null)
{
result.addAll(curr_split_pieces[1].split(p_clip_shape));
}
break;
}
}
if (found_trace_split || own_trace_split)
{
// something was split,
// remove cycles containing a split piece
Iterator<PolylineTrace> it2 = split_pieces.iterator();
for (int j = 0; j < 2; ++j)
{
while (it2.hasNext())
{
PolylineTrace curr_piece = it2.next();
board.remove_if_cycle(curr_piece);
}
// remove cycles in the own split pieces last
// to preserve them, if possible
it2 = result.iterator();
}
}
if (own_trace_split)
{
break;
}
}
else if (found_item instanceof DrillItem)
{
DrillItem curr_drill_item = (DrillItem) found_item;
Point split_point = curr_drill_item.get_center();
if (curr_line_segment.contains(split_point))
{
Direction split_line_direction = curr_line_segment.get_line().direction().turn_45_degree(2);
Line split_line = new Line(split_point, split_line_direction);
split(i + 1, split_line);
}
}
else if (!this.is_user_fixed() && (found_item instanceof ConductionArea))
{
boolean ignore_areas = false;
if (this.net_no_arr.length > 0)
{
rules.Net curr_net = this.board.rules.nets.get(this.net_no_arr[0]);
if (curr_net != null && curr_net.get_class() != null)
{
ignore_areas = curr_net.get_class().get_ignore_cycles_with_areas();
}
}
if (!ignore_areas && this.get_start_contacts().contains(found_item) &&
this.get_end_contacts().contains(found_item))
{
// this trace can be removed because of cycle with conduction area
board.remove_item(this);
return result;
}
}
}
if (own_trace_split)
{
break;
}
}
if (!own_trace_split)
{
result.add(this);
}
if (result.size() > 1)
{
for (Item curr_item : result)
{
board.additional_update_after_change(curr_item);
}
}
return result;
}
/**
* Checks, if the intersection of the p_line_no-th line of this trace with p_line is inside
* the pad of a pin. In this case the trace will be split only, if the intersection
* is at the center of the pin.
* Extending the function to vias leaded to broken connection problems wenn the autorouter connected to a trace.
*/
private boolean split_inside_drill_pad_prohibited(int p_line_no, Line p_line)
{
if (this.board == null)
{
return false;
}
Point intersection = this.lines.arr[p_line_no].intersection(p_line);
java.util.Collection<Item> overlap_items = this.board.pick_items(intersection, this.get_layer(), null);
boolean pad_found = false;
for (Item curr_item : overlap_items)
{
if (!curr_item.shares_net(this))
{
continue;
}
if (curr_item instanceof Pin)
{
DrillItem curr_drill_item = (DrillItem) curr_item;
if (curr_drill_item.get_center().equals(intersection))
{
return false; // split always at the center of a drill item.
}
pad_found = true;
}
else if (curr_item instanceof Trace)
{
Trace curr_trace = (Trace) curr_item;
if (curr_trace != this && curr_trace.first_corner().equals(intersection) || curr_trace.last_corner().equals(intersection))
{
return false;
}
}
}
return pad_found;
}
/**
* Splits this trace into two at p_point.
* Returns the 2 pieces of the splitted trace, or null if nothing was splitted because for example
* p_point is not located on a line segment of the p_polyline of this trace.
*/
public Trace[] split(Point p_point)
{
for (int i = 0; i < this.lines.arr.length - 2; ++i)
{
LineSegment curr_line_segment = new LineSegment(this.lines, i + 1);
if (curr_line_segment.contains(p_point))
{
Direction split_line_direction = curr_line_segment.get_line().direction().turn_45_degree(2);
Line split_line = new Line(p_point, split_line_direction);
Trace[] result = split(i + 1, split_line);
if (result != null)
{
return result;
}
}
}
return null;
}
/**
* Splits this trace at the line with number p_line_no
* into two by inserting p_endline as concluding line of the first split piece
* and as the start line of the second split piece.
* Returns the 2 pieces of the splitted trace, or null, if nothing was splitted.
*/
private PolylineTrace[] split(int p_line_no, Line p_new_end_line)
{
if (!this.is_on_the_board())
{
return null;
}
Polyline[] split_polylines = lines.split(p_line_no, p_new_end_line);
if (split_polylines == null)
{
return null;
}
if (split_polylines.length != 2)
{
System.out.println("PolylineTrace.split: array of length 2 expected for split_polylines");
return null;
}
if (split_inside_drill_pad_prohibited(p_line_no, p_new_end_line))
{
return null;
}
board.remove_item(this);
PolylineTrace[] result = new PolylineTrace[2];
result[0] = board.insert_trace_without_cleaning(split_polylines[0], get_layer(), get_half_width(),
net_no_arr, clearance_class_no(), get_fixed_state());
result[1] = board.insert_trace_without_cleaning(split_polylines[1], get_layer(), get_half_width(),
net_no_arr, clearance_class_no(), get_fixed_state());
return result;
}
/**
* Splits this trace and overlapping traces, and combines this trace.
* Returns true, if something was changed.
* If p_clip_shape != null, splitting is restricted to p_clip_shape.
*/
public boolean normalize(IntOctagon p_clip_shape)
{
boolean observers_activated = false;
BasicBoard routing_board = this.board;
if (this.board != null)
{
// Let the observers know the trace changes.
observers_activated = !routing_board.observers_active();
if (observers_activated)
{
routing_board.start_notify_observers();
}
}
Collection<PolylineTrace> split_pieces = this.split(p_clip_shape);
boolean result = (split_pieces.size() != 1);
Iterator<PolylineTrace> it = split_pieces.iterator();
while (it.hasNext())
{
PolylineTrace curr_split_trace = it.next();
if (curr_split_trace.is_on_the_board())
{
boolean trace_combined = curr_split_trace.combine();
if (curr_split_trace.corner_count() == 2 && curr_split_trace.first_corner().equals(curr_split_trace.last_corner()))
{
// remove trace with only 1 corner
board.remove_item(curr_split_trace);
result = true;
}
else if (trace_combined)
{
curr_split_trace.normalize(p_clip_shape);
result = true;
}
}
}
if (observers_activated)
{
routing_board.end_notify_observers();
}
return result;
}
/**
* Tries to shorten this trace without creating clearance violations
* Returns true, if the trace was changed.
*/
public boolean pull_tight(PullTightAlgo p_pull_tight_algo)
{
if (!this.is_on_the_board())
{
// This trace may have been deleted in a trace split for example
return false;
}
if (this.is_shove_fixed())
{
return false;
}
if (!this.nets_normal())
{
return false;
}
if (p_pull_tight_algo.only_net_no_arr.length > 0 && !this.nets_equal(p_pull_tight_algo.only_net_no_arr))
{
return false;
}
if (this.net_no_arr.length > 0)
{
if (!this.board.rules.nets.get(this.net_no_arr[0]).get_class().get_pull_tight())
{
return false;
}
}
Polyline new_lines =
p_pull_tight_algo.pull_tight(lines, get_layer(), get_half_width(), net_no_arr, clearance_class_no(),
this.touching_pins_at_end_corners());
if (new_lines != lines)
{
change(new_lines);
return true;
}
AngleRestriction angle_restriction = this.board.rules.get_trace_angle_restriction();
if (angle_restriction != AngleRestriction.NINETY_DEGREE && this.board.rules.get_pin_edge_to_turn_dist() > 0)
{
if (this.swap_connection_to_pin(true))
{
pull_tight(p_pull_tight_algo);
return true;
}
if (this.swap_connection_to_pin(false))
{
pull_tight(p_pull_tight_algo);
return true;
}
// optimize algorithm could not improve the trace, try to remove acid traps
if (this.correct_connection_to_pin(true, angle_restriction))
{
pull_tight(p_pull_tight_algo);
return true;
}
if (this.correct_connection_to_pin(false, angle_restriction))
{
pull_tight(p_pull_tight_algo);
return true;
}
}
return false;
}
/**
* Tries to pull this trace tight without creating clearance violations
* Returns true, if the trace was changed.
*/
public boolean pull_tight(boolean p_own_net_only, int p_pull_tight_accuracy, Stoppable p_stoppable_thread)
{
if (!(this.board instanceof RoutingBoard))
{
return false;
}
int[] opt_net_no_arr;
if (p_own_net_only)
{
opt_net_no_arr = this.net_no_arr;
}
else
{
opt_net_no_arr = new int[0];
}
PullTightAlgo pull_tight_algo =
PullTightAlgo.get_instance((RoutingBoard) this.board, opt_net_no_arr,
null, p_pull_tight_accuracy, p_stoppable_thread, -1, null, -1);
return pull_tight(pull_tight_algo);
}
/**
* Tries to smoothen the end corners of this trace, which are at a fork with other traces.
*/
public boolean smoothen_end_corners_fork(boolean p_own_net_only, int p_pull_tight_accuracy, Stoppable p_stoppable_thread)
{
if (!(this.board instanceof RoutingBoard))
{
return false;
}
int[] opt_net_no_arr;
if (p_own_net_only)
{
opt_net_no_arr = this.net_no_arr;
}
else
{
opt_net_no_arr = new int[0];
}
PullTightAlgo pull_tight_algo =
PullTightAlgo.get_instance((RoutingBoard) this.board, opt_net_no_arr,
null, p_pull_tight_accuracy, p_stoppable_thread, -1, null, -1);
return pull_tight_algo.smoothen_end_corners_at_trace(this);
}
public TileShape get_trace_connection_shape(ShapeSearchTree p_search_tree, int p_index)
{
if (p_index < 0 || p_index >= this.tile_shape_count())
{
System.out.println("PolylineTrace.get_trace_connection_shape p_index out of range");
return null;
}
LineSegment curr_line_segment = new LineSegment(this.lines, p_index + 1);
TileShape result = curr_line_segment.to_simplex().simplify();
return result;
}
public boolean write(java.io.ObjectOutputStream p_stream)
{
try
{
p_stream.writeObject(this);
} catch (java.io.IOException e)
{
return false;
}
return true;
}
/**
* changes the geometry of this trace to p_new_polyline
*/
void change(Polyline p_new_polyline)
{
if (!this.is_on_the_board())
{
// Just change the polyline of this trace.
lines = p_new_polyline;
return;
}
board.additional_update_after_change(this);
// The precalculated tile shapes must not be cleared here here because they are used and modified
// in ShapeSearchTree.change_entries.
board.item_list.save_for_undo(this);
// for performance reasons there is some effort to reuse
// ShapeTree entries of the old trace in the changed trace
// look for the first line in p_new_polyline different from
// the lines of the existung trace
int last_index = Math.min(p_new_polyline.arr.length, lines.arr.length);
int index_of_first_different_line = last_index;
for (int i = 0; i < last_index; ++i)
{