-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
eda_shape.cpp
2356 lines (1895 loc) · 66.1 KB
/
eda_shape.cpp
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
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
* Copyright (C) 2011 Wayne Stambaugh <[email protected]>
* Copyright (C) 2023 CERN
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* 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 <eda_shape.h>
#include <bezier_curves.h>
#include <convert_basic_shapes_to_polygon.h>
#include <eda_draw_frame.h>
#include <geometry/shape_arc.h>
#include <geometry/shape_circle.h>
#include <geometry/shape_simple.h>
#include <geometry/shape_segment.h>
#include <geometry/shape_rect.h>
#include <macros.h>
#include <math/util.h> // for KiROUND
#include <eda_item.h>
#include <plotters/plotter.h>
#include <api/api_enums.h>
#include <api/api_utils.h>
#include <api/common/types/base_types.pb.h>
EDA_SHAPE::EDA_SHAPE( SHAPE_T aType, int aLineWidth, FILL_T aFill ) :
m_endsSwapped( false ),
m_shape( aType ),
m_stroke( aLineWidth, LINE_STYLE::DEFAULT, COLOR4D::UNSPECIFIED ),
m_fill( aFill ),
m_fillColor( COLOR4D::UNSPECIFIED ),
m_rectangleHeight( 0 ),
m_rectangleWidth( 0 ),
m_segmentLength( 0 ),
m_editState( 0 ),
m_proxyItem( false )
{
}
EDA_SHAPE::~EDA_SHAPE()
{
}
EDA_SHAPE::EDA_SHAPE( const SHAPE& aShape ) :
m_endsSwapped( false ),
m_stroke( 0, LINE_STYLE::DEFAULT, COLOR4D::UNSPECIFIED ),
m_fill(),
m_rectangleHeight( 0 ),
m_rectangleWidth( 0 ),
m_segmentLength( 0 ),
m_editState( 0 ),
m_proxyItem( false )
{
switch( aShape.Type() )
{
case SH_RECT:
{
auto rect = static_cast<const SHAPE_RECT&>( aShape );
m_shape = SHAPE_T::RECTANGLE;
SetStart( rect.GetPosition() );
SetEnd( rect.GetPosition() + rect.GetSize() );
break;
}
case SH_SEGMENT:
{
auto seg = static_cast<const SHAPE_SEGMENT&>( aShape );
m_shape = SHAPE_T::SEGMENT;
SetStart( seg.GetSeg().A );
SetEnd( seg.GetSeg().B );
SetWidth( seg.GetWidth() );
break;
}
case SH_LINE_CHAIN:
{
auto line = static_cast<const SHAPE_LINE_CHAIN&>( aShape );
m_shape = SHAPE_T::POLY;
m_poly = SHAPE_POLY_SET();
m_poly.AddOutline( line );
SetWidth( line.Width() );
break;
}
case SH_CIRCLE:
{
auto circle = static_cast<const SHAPE_CIRCLE&>( aShape );
m_shape = SHAPE_T::CIRCLE;
SetStart( circle.GetCenter() );
SetEnd( circle.GetCenter() + circle.GetRadius() );
break;
}
case SH_ARC:
{
auto arc = static_cast<const SHAPE_ARC&>( aShape );
m_shape = SHAPE_T::ARC;
SetArcGeometry( arc.GetP0(), arc.GetArcMid(), arc.GetP1() );
SetWidth( arc.GetWidth() );
break;
}
case SH_SIMPLE:
{
auto poly = static_cast<const SHAPE_SIMPLE&>( aShape );
m_shape = SHAPE_T::POLY;
poly.TransformToPolygon( m_poly, 0, ERROR_INSIDE );
break;
}
// currently unhandled
case SH_POLY_SET:
case SH_COMPOUND:
case SH_NULL:
case SH_POLY_SET_TRIANGLE:
default:
m_shape = SHAPE_T::UNDEFINED;
break;
}
}
void EDA_SHAPE::Serialize( google::protobuf::Any &aContainer ) const
{
using namespace kiapi::common;
types::GraphicShape shape;
types::StrokeAttributes* stroke = shape.mutable_attributes()->mutable_stroke();
types::GraphicFillAttributes* fill = shape.mutable_attributes()->mutable_fill();
stroke->mutable_width()->set_value_nm( GetWidth() );
switch( GetLineStyle() )
{
case LINE_STYLE::DEFAULT: stroke->set_style( types::SLS_DEFAULT ); break;
case LINE_STYLE::SOLID: stroke->set_style( types::SLS_SOLID ); break;
case LINE_STYLE::DASH: stroke->set_style( types::SLS_DASH ); break;
case LINE_STYLE::DOT: stroke->set_style( types::SLS_DOT ); break;
case LINE_STYLE::DASHDOT: stroke->set_style( types::SLS_DASHDOT ); break;
case LINE_STYLE::DASHDOTDOT: stroke->set_style( types::SLS_DASHDOTDOT ); break;
default: break;
}
switch( GetFillMode() )
{
case FILL_T::FILLED_SHAPE: fill->set_fill_type( types::GFT_FILLED ); break;
default: fill->set_fill_type( types::GFT_UNFILLED ); break;
}
switch( GetShape() )
{
case SHAPE_T::SEGMENT:
{
types::GraphicSegmentAttributes* segment = shape.mutable_segment();
PackVector2( *segment->mutable_start(), GetStart() );
PackVector2( *segment->mutable_end(), GetEnd() );
break;
}
case SHAPE_T::RECTANGLE:
{
types::GraphicRectangleAttributes* rectangle = shape.mutable_rectangle();
PackVector2( *rectangle->mutable_top_left(), GetStart() );
PackVector2( *rectangle->mutable_bottom_right(), GetEnd() );
break;
}
case SHAPE_T::ARC:
{
types::GraphicArcAttributes* arc = shape.mutable_arc();
PackVector2( *arc->mutable_start(), GetStart() );
PackVector2( *arc->mutable_mid(), GetArcMid() );
PackVector2( *arc->mutable_end(), GetEnd() );
break;
}
case SHAPE_T::CIRCLE:
{
types::GraphicCircleAttributes* circle = shape.mutable_circle();
PackVector2( *circle->mutable_center(), GetStart() );
PackVector2( *circle->mutable_radius_point(), GetEnd() );
break;
}
case SHAPE_T::POLY:
{
PackPolySet( *shape.mutable_polygon(), GetPolyShape() );
break;
}
case SHAPE_T::BEZIER:
{
types::GraphicBezierAttributes* bezier = shape.mutable_bezier();
PackVector2( *bezier->mutable_start(), GetStart() );
PackVector2( *bezier->mutable_control1(), GetBezierC1() );
PackVector2( *bezier->mutable_control2(), GetBezierC2() );
PackVector2( *bezier->mutable_end(), GetEnd() );
break;
}
default:
wxASSERT_MSG( false, "Unhandled shape in PCB_SHAPE::Serialize" );
}
// TODO m_hasSolderMask and m_solderMaskMargin
aContainer.PackFrom( shape );
}
bool EDA_SHAPE::Deserialize( const google::protobuf::Any &aContainer )
{
using namespace kiapi::common;
types::GraphicShape shape;
if( !aContainer.UnpackTo( &shape ) )
return false;
// Initialize everything to a known state that doesn't get touched by every
// codepath below, to make sure the equality operator is consistent
m_start = {};
m_end = {};
m_arcCenter = {};
m_arcMidData = {};
m_bezierC1 = {};
m_bezierC2 = {};
m_editState = 0;
m_proxyItem = false;
m_endsSwapped = false;
SetFilled( shape.attributes().fill().fill_type() == types::GFT_FILLED );
SetWidth( shape.attributes().stroke().width().value_nm() );
switch( shape.attributes().stroke().style() )
{
case types::SLS_DEFAULT: SetLineStyle( LINE_STYLE::DEFAULT ); break;
case types::SLS_SOLID: SetLineStyle( LINE_STYLE::SOLID ); break;
case types::SLS_DASH: SetLineStyle( LINE_STYLE::DASH ); break;
case types::SLS_DOT: SetLineStyle( LINE_STYLE::DOT ); break;
case types::SLS_DASHDOT: SetLineStyle( LINE_STYLE::DASHDOT ); break;
case types::SLS_DASHDOTDOT: SetLineStyle( LINE_STYLE::DASHDOTDOT ); break;
default: break;
}
if( shape.has_segment() )
{
SetShape( SHAPE_T::SEGMENT );
SetStart( UnpackVector2( shape.segment().start() ) );
SetEnd( UnpackVector2( shape.segment().end() ) );
}
else if( shape.has_rectangle() )
{
SetShape( SHAPE_T::RECTANGLE );
SetStart( UnpackVector2( shape.rectangle().top_left() ) );
SetEnd( UnpackVector2( shape.rectangle().bottom_right() ) );
}
else if( shape.has_arc() )
{
SetShape( SHAPE_T::ARC );
SetArcGeometry( UnpackVector2( shape.arc().start() ),
UnpackVector2( shape.arc().mid() ),
UnpackVector2( shape.arc().end() ) );
}
else if( shape.has_circle() )
{
SetShape( SHAPE_T::CIRCLE );
SetStart( UnpackVector2( shape.circle().center() ) );
SetEnd( UnpackVector2( shape.circle().radius_point() ) );
}
else if( shape.has_polygon() )
{
SetShape( SHAPE_T::POLY );
SetPolyShape( UnpackPolySet( shape.polygon() ) );
}
else if( shape.has_bezier() )
{
SetShape( SHAPE_T::BEZIER );
SetStart( UnpackVector2( shape.bezier().start() ) );
SetBezierC1( UnpackVector2( shape.bezier().control1() ) );
SetBezierC2( UnpackVector2( shape.bezier().control2() ) );
SetEnd( UnpackVector2( shape.bezier().end() ) );
RebuildBezierToSegmentsPointsList( ARC_HIGH_DEF );
}
return true;
}
wxString EDA_SHAPE::ShowShape() const
{
if( IsProxyItem() )
{
switch( m_shape )
{
case SHAPE_T::SEGMENT: return _( "Thermal Spoke" );
case SHAPE_T::RECTANGLE: return _( "Number Box" );
default: return wxT( "??" );
}
}
else
{
switch( m_shape )
{
case SHAPE_T::SEGMENT: return _( "Line" );
case SHAPE_T::RECTANGLE: return _( "Rect" );
case SHAPE_T::ARC: return _( "Arc" );
case SHAPE_T::CIRCLE: return _( "Circle" );
case SHAPE_T::BEZIER: return _( "Bezier Curve" );
case SHAPE_T::POLY: return _( "Polygon" );
default: return wxT( "??" );
}
}
}
wxString EDA_SHAPE::SHAPE_T_asString() const
{
switch( m_shape )
{
case SHAPE_T::SEGMENT: return wxS( "S_SEGMENT" );
case SHAPE_T::RECTANGLE: return wxS( "S_RECT" );
case SHAPE_T::ARC: return wxS( "S_ARC" );
case SHAPE_T::CIRCLE: return wxS( "S_CIRCLE" );
case SHAPE_T::POLY: return wxS( "S_POLYGON" );
case SHAPE_T::BEZIER: return wxS( "S_CURVE" );
case SHAPE_T::UNDEFINED: return wxS( "UNDEFINED" );
}
return wxEmptyString; // Just to quiet GCC.
}
void EDA_SHAPE::setPosition( const VECTOR2I& aPos )
{
move( aPos - getPosition() );
}
VECTOR2I EDA_SHAPE::getPosition() const
{
if( m_shape == SHAPE_T::ARC )
return getCenter();
else if( m_shape == SHAPE_T::POLY )
return m_poly.CVertex( 0 );
else
return m_start;
}
double EDA_SHAPE::GetLength() const
{
double length = 0.0;
switch( m_shape )
{
case SHAPE_T::BEZIER:
for( size_t ii = 1; ii < m_bezierPoints.size(); ++ii )
length += m_bezierPoints[ ii - 1].Distance( m_bezierPoints[ii] );
return length;
case SHAPE_T::SEGMENT:
return GetStart().Distance( GetEnd() );
case SHAPE_T::POLY:
for( int ii = 0; ii < m_poly.COutline( 0 ).SegmentCount(); ii++ )
length += m_poly.COutline( 0 ).CSegment( ii ).Length();
return length;
case SHAPE_T::ARC:
return GetRadius() * GetArcAngle().AsRadians();
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
return 0.0;
}
}
int EDA_SHAPE::GetRectangleHeight() const
{
switch( m_shape )
{
case SHAPE_T::RECTANGLE:
return GetEndY() - GetStartY();
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
return 0;
}
}
int EDA_SHAPE::GetRectangleWidth() const
{
switch( m_shape )
{
case SHAPE_T::RECTANGLE:
return GetEndX() - GetStartX();
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
return 0;
}
}
void EDA_SHAPE::SetLength( const double& aLength )
{
switch( m_shape )
{
case SHAPE_T::SEGMENT:
m_segmentLength = aLength; break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
}
}
void EDA_SHAPE::SetRectangleHeight( const int& aHeight )
{
switch ( m_shape )
{
case SHAPE_T::RECTANGLE:
m_rectangleHeight = aHeight;
SetEndY( GetStartY() + m_rectangleHeight );
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
}
}
void EDA_SHAPE::SetRectangleWidth( const int& aWidth )
{
switch ( m_shape )
{
case SHAPE_T::RECTANGLE:
m_rectangleWidth = aWidth;
SetEndX( GetStartX() + m_rectangleWidth );
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
}
}
void EDA_SHAPE::SetRectangle( const long long int& aHeight, const long long int& aWidth )
{
switch ( m_shape )
{
case SHAPE_T::RECTANGLE:
m_rectangleHeight = aHeight;
m_rectangleWidth = aWidth;
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
}
}
void EDA_SHAPE::SetSegmentAngle( const EDA_ANGLE& aAngle )
{
switch( m_shape )
{
case SHAPE_T::SEGMENT:
m_segmentAngle = aAngle;
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
}
}
bool EDA_SHAPE::IsClosed() const
{
switch( m_shape )
{
case SHAPE_T::CIRCLE:
case SHAPE_T::RECTANGLE:
return true;
case SHAPE_T::ARC:
case SHAPE_T::SEGMENT:
return false;
case SHAPE_T::POLY:
if( m_poly.IsEmpty() )
return false;
else
return m_poly.Outline( 0 ).IsClosed();
case SHAPE_T::BEZIER:
if( m_bezierPoints.size() < 3 )
return false;
else
return m_bezierPoints[0] == m_bezierPoints[ m_bezierPoints.size() - 1 ];
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
return false;
}
}
void EDA_SHAPE::move( const VECTOR2I& aMoveVector )
{
switch ( m_shape )
{
case SHAPE_T::ARC:
m_arcCenter += aMoveVector;
KI_FALLTHROUGH;
case SHAPE_T::SEGMENT:
case SHAPE_T::RECTANGLE:
case SHAPE_T::CIRCLE:
m_start += aMoveVector;
m_end += aMoveVector;
break;
case SHAPE_T::POLY:
m_poly.Move( aMoveVector );
break;
case SHAPE_T::BEZIER:
m_start += aMoveVector;
m_end += aMoveVector;
m_bezierC1 += aMoveVector;
m_bezierC2 += aMoveVector;
for( VECTOR2I& pt : m_bezierPoints )
pt += aMoveVector;
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
break;
}
}
void EDA_SHAPE::scale( double aScale )
{
auto scalePt = [&]( VECTOR2I& pt )
{
pt.x = KiROUND( pt.x * aScale );
pt.y = KiROUND( pt.y * aScale );
};
switch( m_shape )
{
case SHAPE_T::ARC:
scalePt( m_arcCenter );
KI_FALLTHROUGH;
case SHAPE_T::SEGMENT:
case SHAPE_T::RECTANGLE:
scalePt( m_start );
scalePt( m_end );
break;
case SHAPE_T::CIRCLE: // ring or circle
scalePt( m_start );
m_end.x = m_start.x + KiROUND( GetRadius() * aScale );
m_end.y = m_start.y;
break;
case SHAPE_T::POLY: // polygon
{
std::vector<VECTOR2I> pts;
for( int ii = 0; ii < m_poly.OutlineCount(); ++ ii )
{
for( const VECTOR2I& pt : m_poly.Outline( ii ).CPoints() )
{
pts.emplace_back( pt );
scalePt( pts.back() );
}
}
SetPolyPoints( pts );
}
break;
case SHAPE_T::BEZIER:
scalePt( m_start );
scalePt( m_end );
scalePt( m_bezierC1 );
scalePt( m_bezierC2 );
RebuildBezierToSegmentsPointsList( m_stroke.GetWidth() / 2 );
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
break;
}
}
void EDA_SHAPE::rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
{
switch( m_shape )
{
case SHAPE_T::SEGMENT:
case SHAPE_T::CIRCLE:
RotatePoint( m_start, aRotCentre, aAngle );
RotatePoint( m_end, aRotCentre, aAngle );
break;
case SHAPE_T::ARC:
RotatePoint( m_start, aRotCentre, aAngle );
RotatePoint( m_end, aRotCentre, aAngle );
RotatePoint( m_arcCenter, aRotCentre, aAngle );
break;
case SHAPE_T::RECTANGLE:
if( aAngle.IsCardinal() )
{
RotatePoint( m_start, aRotCentre, aAngle );
RotatePoint( m_end, aRotCentre, aAngle );
break;
}
// Convert non-cardinally-rotated rect to a diamond
m_shape = SHAPE_T::POLY;
m_poly.RemoveAllContours();
m_poly.NewOutline();
m_poly.Append( m_start );
m_poly.Append( m_end.x, m_start.y );
m_poly.Append( m_end );
m_poly.Append( m_start.x, m_end.y );
KI_FALLTHROUGH;
case SHAPE_T::POLY:
m_poly.Rotate( aAngle, aRotCentre );
break;
case SHAPE_T::BEZIER:
RotatePoint( m_start, aRotCentre, aAngle );
RotatePoint( m_end, aRotCentre, aAngle );
RotatePoint( m_bezierC1, aRotCentre, aAngle );
RotatePoint( m_bezierC2, aRotCentre, aAngle );
for( VECTOR2I& pt : m_bezierPoints )
RotatePoint( pt, aRotCentre, aAngle);
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
break;
}
}
void EDA_SHAPE::flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
{
switch ( m_shape )
{
case SHAPE_T::SEGMENT:
case SHAPE_T::RECTANGLE:
MIRROR( m_start, aCentre, aFlipDirection );
MIRROR( m_end, aCentre, aFlipDirection );
break;
case SHAPE_T::CIRCLE:
MIRROR( m_start, aCentre, aFlipDirection );
MIRROR( m_end, aCentre, aFlipDirection );
break;
case SHAPE_T::ARC:
MIRROR( m_start, aCentre, aFlipDirection );
MIRROR( m_end, aCentre, aFlipDirection );
MIRROR( m_arcCenter, aCentre, aFlipDirection );
std::swap( m_start, m_end );
break;
case SHAPE_T::POLY:
m_poly.Mirror( aCentre, aFlipDirection );
break;
case SHAPE_T::BEZIER:
MIRROR( m_start, aCentre, aFlipDirection );
MIRROR( m_end, aCentre, aFlipDirection );
MIRROR( m_bezierC1, aCentre, aFlipDirection );
MIRROR( m_bezierC2, aCentre, aFlipDirection );
RebuildBezierToSegmentsPointsList( m_stroke.GetWidth() / 2 );
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
break;
}
}
void EDA_SHAPE::RebuildBezierToSegmentsPointsList( int aMaxError )
{
// Has meaning only for SHAPE_T::BEZIER
if( m_shape != SHAPE_T::BEZIER )
{
m_bezierPoints.clear();
return;
}
// Rebuild the m_BezierPoints vertex list that approximate the Bezier curve
m_bezierPoints = buildBezierToSegmentsPointsList( aMaxError );
}
const std::vector<VECTOR2I> EDA_SHAPE::buildBezierToSegmentsPointsList( int aMaxError ) const
{
std::vector<VECTOR2I> bezierPoints;
// Rebuild the m_BezierPoints vertex list that approximate the Bezier curve
std::vector<VECTOR2I> ctrlPoints = { m_start, m_bezierC1, m_bezierC2, m_end };
BEZIER_POLY converter( ctrlPoints );
converter.GetPoly( bezierPoints, aMaxError );
return bezierPoints;
}
VECTOR2I EDA_SHAPE::getCenter() const
{
switch( m_shape )
{
case SHAPE_T::ARC:
return m_arcCenter;
case SHAPE_T::CIRCLE:
return m_start;
case SHAPE_T::SEGMENT:
// Midpoint of the line
return ( m_start + m_end ) / 2;
case SHAPE_T::POLY:
case SHAPE_T::RECTANGLE:
case SHAPE_T::BEZIER:
return getBoundingBox().Centre();
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
return VECTOR2I();
}
}
void EDA_SHAPE::SetCenter( const VECTOR2I& aCenter )
{
switch( m_shape )
{
case SHAPE_T::ARC:
m_arcCenter = aCenter;
break;
case SHAPE_T::CIRCLE:
m_start = aCenter;
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
}
}
VECTOR2I EDA_SHAPE::GetArcMid() const
{
// If none of the input data have changed since we loaded the arc,
// keep the original mid point data to minimize churn
if( m_arcMidData.start == m_start && m_arcMidData.end == m_end
&& m_arcMidData.center == m_arcCenter )
return m_arcMidData.mid;
VECTOR2I mid = m_start;
RotatePoint( mid, m_arcCenter, -GetArcAngle() / 2.0 );
return mid;
}
void EDA_SHAPE::CalcArcAngles( EDA_ANGLE& aStartAngle, EDA_ANGLE& aEndAngle ) const
{
VECTOR2D startRadial( GetStart() - getCenter() );
VECTOR2D endRadial( GetEnd() - getCenter() );
aStartAngle = EDA_ANGLE( startRadial );
aEndAngle = EDA_ANGLE( endRadial );
if( aEndAngle == aStartAngle )
aEndAngle = aStartAngle + ANGLE_360; // ring, not null
while( aEndAngle < aStartAngle )
aEndAngle += ANGLE_360;
}
int EDA_SHAPE::GetRadius() const
{
double radius = 0.0;
switch( m_shape )
{
case SHAPE_T::ARC:
radius = m_arcCenter.Distance( m_start );
break;
case SHAPE_T::CIRCLE:
radius = m_start.Distance( m_end );
break;
default:
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
}
// don't allow degenerate circles/arcs
return std::max( 1, KiROUND( radius ) );
}
void EDA_SHAPE::SetCachedArcData( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd, const VECTOR2I& aCenter )
{
m_arcMidData.start = aStart;
m_arcMidData.end = aEnd;
m_arcMidData.center = aCenter;
m_arcMidData.mid = aMid;
}
void EDA_SHAPE::SetArcGeometry( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd )
{
m_arcMidData = {};
m_start = aStart;
m_end = aEnd;
m_arcCenter = CalcArcCenter( aStart, aMid, aEnd );
VECTOR2I new_mid = GetArcMid();
m_endsSwapped = false;
// Watch the ordering here. GetArcMid above needs to be called prior to initializing the
// m_arcMidData structure in order to ensure we get the calculated variant, not the cached
SetCachedArcData( aStart, aMid, aEnd, m_arcCenter );
/*
* If the input winding doesn't match our internal winding, the calculated midpoint will end
* up on the other side of the arc. In this case, we need to flip the start/end points and
* flag this change for the system.
*/
VECTOR2D dist( new_mid - aMid );
VECTOR2D dist2( new_mid - m_arcCenter );
if( dist.SquaredEuclideanNorm() > dist2.SquaredEuclideanNorm() )
{
std::swap( m_start, m_end );
m_endsSwapped = true;
}
}
EDA_ANGLE EDA_SHAPE::GetSegmentAngle() const
{
EDA_ANGLE angle( atan2( static_cast<double>( GetStart().y - GetEnd().y ),
static_cast<double>( GetEnd().x - GetStart().x ) ), RADIANS_T );
return angle;
}
EDA_ANGLE EDA_SHAPE::GetArcAngle() const
{
EDA_ANGLE startAngle;
EDA_ANGLE endAngle;
CalcArcAngles( startAngle, endAngle );
return endAngle - startAngle;
}
bool EDA_SHAPE::IsClockwiseArc() const
{
if( m_shape == SHAPE_T::ARC )
{
VECTOR2D mid = GetArcMid();
double orient = ( mid.x - m_start.x ) * ( m_end.y - m_start.y )
- ( mid.y - m_start.y ) * ( m_end.x - m_start.x );
return orient < 0;
}
UNIMPLEMENTED_FOR( SHAPE_T_asString() );
return false;
}
void EDA_SHAPE::SetArcAngleAndEnd( const EDA_ANGLE& aAngle, bool aCheckNegativeAngle )
{
EDA_ANGLE angle( aAngle );
m_end = m_start;
RotatePoint( m_end, m_arcCenter, -angle.Normalize720() );
if( aCheckNegativeAngle && aAngle < ANGLE_0 )
{
std::swap( m_start, m_end );
m_endsSwapped = true;
}
}
wxString EDA_SHAPE::getFriendlyName() const
{
if( IsProxyItem() )
{
switch( m_shape )
{
case SHAPE_T::RECTANGLE: return _( "Pad Number Box" );
case SHAPE_T::SEGMENT: return _( "Thermal Spoke Template" );
default: return _( "Unrecognized" );
}
}
else
{
switch( m_shape )
{
case SHAPE_T::CIRCLE: return _( "Circle" );
case SHAPE_T::ARC: return _( "Arc" );
case SHAPE_T::BEZIER: return _( "Curve" );
case SHAPE_T::POLY: return _( "Polygon" );
case SHAPE_T::RECTANGLE: return _( "Rectangle" );
case SHAPE_T::SEGMENT: return _( "Segment" );
default: return _( "Unrecognized" );
}
}
}
void EDA_SHAPE::ShapeGetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
{
wxString msg;
wxString shape = _( "Shape" );
aList.emplace_back( shape, getFriendlyName() );
switch( m_shape )
{
case SHAPE_T::CIRCLE:
aList.emplace_back( _( "Radius" ), aFrame->MessageTextFromValue( GetRadius() ) );
break;
case SHAPE_T::ARC:
msg = EDA_UNIT_UTILS::UI::MessageTextFromValue( GetArcAngle() );
aList.emplace_back( _( "Angle" ), msg );
aList.emplace_back( _( "Radius" ), aFrame->MessageTextFromValue( GetRadius() ) );
break;
case SHAPE_T::BEZIER:
aList.emplace_back( _( "Length" ), aFrame->MessageTextFromValue( GetLength() ) );
break;
case SHAPE_T::POLY:
msg.Printf( wxS( "%d" ), GetPolyShape().Outline(0).PointCount() );
aList.emplace_back( _( "Points" ), msg );
break;
case SHAPE_T::RECTANGLE:
aList.emplace_back( _( "Width" ),
aFrame->MessageTextFromValue( std::abs( GetEnd().x - GetStart().x ) ) );
aList.emplace_back( _( "Height" ),
aFrame->MessageTextFromValue( std::abs( GetEnd().y - GetStart().y ) ) );