KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_shape.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
6 * Copyright (C) 2011 Wayne Stambaugh <[email protected]>
7 * Copyright (C) 2023 CERN
8 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24#include <eda_shape.h>
25
26#include <base_units.h>
27#include <bezier_curves.h>
29#include <eda_draw_frame.h>
30#include <geometry/shape_arc.h>
37#include <geometry/shape_rect.h>
39#include <geometry/roundrect.h>
41#include <geometry/roundrect.h>
42#include <macros.h>
43#include <algorithm>
44#include <iterator>
46#include <properties/property.h>
48#include <math/util.h> // for KiROUND
49#include <eda_item.h>
50#include <plotters/plotter.h>
51#include <api/api_enums.h>
52#include <api/api_utils.h>
53#include <api/common/types/base_types.pb.h>
54
55
56EDA_SHAPE::EDA_SHAPE( SHAPE_T aType, int aLineWidth, FILL_T aFill ) :
57 m_endsSwapped( false ),
58 m_shape( aType ),
62 m_fill( aFill ),
64 m_hatchingDirty( true ),
67 m_cornerRadius( 0 ),
68 m_editState( 0 ),
69 m_proxyItem( false )
70{
71}
72
73
77
78
79EDA_SHAPE::EDA_SHAPE( const SHAPE& aShape ) :
80 m_endsSwapped( false ),
84 m_fill(),
85 m_hatchingDirty( true ),
88 m_cornerRadius( 0 ),
89 m_editState( 0 ),
90 m_proxyItem( false )
91{
92 switch( aShape.Type() )
93 {
94 case SH_RECT:
95 {
96 auto rect = static_cast<const SHAPE_RECT&>( aShape );
97 m_shape = SHAPE_T::RECTANGLE;
98 SetStart( rect.GetPosition() );
99 SetEnd( rect.GetPosition() + rect.GetSize() );
100 break;
101 }
102
103 case SH_SEGMENT:
104 {
105 auto seg = static_cast<const SHAPE_SEGMENT&>( aShape );
106 m_shape = SHAPE_T::SEGMENT;
107 SetStart( seg.GetSeg().A );
108 SetEnd( seg.GetSeg().B );
109 SetWidth( seg.GetWidth() );
110 break;
111 }
112
113 case SH_LINE_CHAIN:
114 {
115 auto line = static_cast<const SHAPE_LINE_CHAIN&>( aShape );
116 m_shape = SHAPE_T::POLY;
117 GetPolyShape() = SHAPE_POLY_SET();
118 GetPolyShape().AddOutline( line );
119 SetWidth( line.Width() );
120 break;
121 }
122
123 case SH_CIRCLE:
124 {
125 auto circle = static_cast<const SHAPE_CIRCLE&>( aShape );
126 m_shape = SHAPE_T::CIRCLE;
127 SetStart( circle.GetCenter() );
128 SetEnd( circle.GetCenter() + circle.GetRadius() );
129 break;
130 }
131
132 case SH_ARC:
133 {
134 auto arc = static_cast<const SHAPE_ARC&>( aShape );
135 m_shape = SHAPE_T::ARC;
136
137 if( arc.IsClockwise() )
138 SetArcGeometry( arc.GetP0(), arc.GetArcMid(), arc.GetP1() );
139 else
140 SetArcGeometry( arc.GetP1(), arc.GetArcMid(), arc.GetP0() );
141
142 SetWidth( arc.GetWidth() );
143 break;
144 }
145
146 case SH_SIMPLE:
147 {
148 auto poly = static_cast<const SHAPE_SIMPLE&>( aShape );
149 m_shape = SHAPE_T::POLY;
150 poly.TransformToPolygon( GetPolyShape(), 0, ERROR_INSIDE );
151 break;
152 }
153
154 case SH_ELLIPSE:
155 {
156 auto ellipse = static_cast<const SHAPE_ELLIPSE&>( aShape );
157 m_shape = ellipse.IsArc() ? SHAPE_T::ELLIPSE_ARC : SHAPE_T::ELLIPSE;
158 SetEllipseCenter( ellipse.GetCenter() );
159 SetEllipseMajorRadius( ellipse.GetMajorRadius() );
160 SetEllipseMinorRadius( ellipse.GetMinorRadius() );
161 SetEllipseRotation( ellipse.GetRotation() );
162
163 if( ellipse.IsArc() )
164 {
165 SetEllipseStartAngle( ellipse.GetStartAngle() );
166 SetEllipseEndAngle( ellipse.GetEndAngle() );
167 }
168 break;
169 }
170
171 // currently unhandled
172 case SH_POLY_SET:
173 case SH_COMPOUND:
174 case SH_NULL:
176 default:
177 m_shape = SHAPE_T::UNDEFINED;
178 break;
179 }
180}
181
182
185 m_shape( aOther.m_shape ),
186 m_stroke( aOther.m_stroke ),
188 m_endEnding( aOther.m_endEnding ),
189 m_fill( aOther.m_fill ),
190 m_fillColor( aOther.m_fillColor ),
191 m_hatchingDirty( true ),
195 m_start( aOther.m_start ),
196 m_end( aOther.m_end ),
197 m_arcCenter( aOther.m_arcCenter ),
198 m_arcMidData( aOther.m_arcMidData ),
199 m_bezierC1( aOther.m_bezierC1 ),
200 m_bezierC2( aOther.m_bezierC2 ),
202 m_ellipse( aOther.m_ellipse ),
203 m_editState( aOther.m_editState ),
204 m_proxyItem( aOther.m_proxyItem )
205{
206 if( aOther.m_poly )
207 m_poly = std::make_unique<SHAPE_POLY_SET>( *aOther.m_poly );
208}
209
210
212{
213 if( this == &aOther )
214 return *this;
215
217 m_shape = aOther.m_shape;
218 m_stroke = aOther.m_stroke;
219 m_fill = aOther.m_fill;
220 m_fillColor = aOther.m_fillColor;
221 m_hatchingCache.reset();
222 m_hatchingDirty = true;
226 m_start = aOther.m_start;
227 m_end = aOther.m_end;
228 m_arcCenter = aOther.m_arcCenter;
229 m_arcMidData = aOther.m_arcMidData;
230 m_bezierC1 = aOther.m_bezierC1;
231 m_bezierC2 = aOther.m_bezierC2;
233 m_ellipse = aOther.m_ellipse;
234 if( aOther.m_poly )
235 m_poly = std::make_unique<SHAPE_POLY_SET>( *aOther.m_poly );
236 else
237 m_poly.reset();
239 m_endEnding = aOther.m_endEnding;
240 m_editState = aOther.m_editState;
241 m_proxyItem = aOther.m_proxyItem;
242
243 return *this;
244}
245
246
247void EDA_SHAPE::Serialize( google::protobuf::Any &aContainer ) const
248{
249 Serialize( aContainer, pcbIUScale );
250}
251
252
253void EDA_SHAPE::Serialize( kiapi::common::types::GraphicShape& shape, const EDA_IU_SCALE &aScale ) const
254{
255 using namespace kiapi::common;
256
257 types::GraphicFillAttributes* fill = shape.mutable_attributes()->mutable_fill();
258
259 PackStroke( *shape.mutable_attributes()->mutable_stroke(), m_stroke, aScale );
260
262
264 PackColor( *fill->mutable_color(), m_fillColor );
265
266 switch( GetShape() )
267 {
268 case SHAPE_T::SEGMENT:
269 {
270 types::GraphicSegmentAttributes* segment = shape.mutable_segment();
271 PackVector2( *segment->mutable_start(), GetStart(), aScale );
272 PackVector2( *segment->mutable_end(), GetEnd(), aScale );
273 break;
274 }
275
277 {
278 types::GraphicRectangleAttributes* rectangle = shape.mutable_rectangle();
279 PackVector2( *rectangle->mutable_top_left(), GetStart(), aScale );
280 PackVector2( *rectangle->mutable_bottom_right(), GetEnd(), aScale );
281 PackDistance( *rectangle->mutable_corner_radius(), GetCornerRadius(), aScale );
282 break;
283 }
284
285 case SHAPE_T::ARC:
286 {
287 types::GraphicArcAttributes* arc = shape.mutable_arc();
288 PackVector2( *arc->mutable_start(), GetStart(), aScale );
289 PackVector2( *arc->mutable_mid(), GetArcMid(), aScale );
290 PackVector2( *arc->mutable_end(), GetEnd(), aScale );
291 break;
292 }
293
294 case SHAPE_T::CIRCLE:
295 {
296 types::GraphicCircleAttributes* circle = shape.mutable_circle();
297 PackVector2( *circle->mutable_center(), GetStart(), aScale );
298 PackVector2( *circle->mutable_radius_point(), GetEnd(), aScale );
299 break;
300 }
301
302 case SHAPE_T::POLY:
303 {
304 PackPolySet( *shape.mutable_polygon(), GetPolyShape(), aScale );
305 break;
306 }
307
308 case SHAPE_T::BEZIER:
309 {
310 types::GraphicBezierAttributes* bezier = shape.mutable_bezier();
311 PackVector2( *bezier->mutable_start(), GetStart(), aScale );
312 PackVector2( *bezier->mutable_control1(), GetBezierC1(), aScale );
313 PackVector2( *bezier->mutable_control2(), GetBezierC2(), aScale );
314 PackVector2( *bezier->mutable_end(), GetEnd(), aScale );
315 break;
316 }
317
318 case SHAPE_T::ELLIPSE:
319 {
320 types::GraphicEllipseAttributes* ellipse = shape.mutable_ellipse();
321 PackVector2( *ellipse->mutable_center(), GetEllipseCenter(), aScale );
322 PackDistance( *ellipse->mutable_major_radius(), GetEllipseMajorRadius(), aScale );
323 PackDistance( *ellipse->mutable_minor_radius(), GetEllipseMinorRadius(), aScale );
324 ellipse->mutable_rotation()->set_value_degrees( GetEllipseRotation().AsDegrees() );
325 break;
326 }
327
329 {
330 types::GraphicEllipseArcAttributes* arc = shape.mutable_ellipse_arc();
331 PackVector2( *arc->mutable_center(), GetEllipseCenter(), aScale );
332 PackDistance( *arc->mutable_major_radius(), GetEllipseMajorRadius(), aScale );
333 PackDistance( *arc->mutable_minor_radius(), GetEllipseMinorRadius(), aScale );
334 arc->mutable_rotation()->set_value_degrees( GetEllipseRotation().AsDegrees() );
335 arc->mutable_start_angle()->set_value_degrees( GetEllipseStartAngle().AsDegrees() );
336 arc->mutable_end_angle()->set_value_degrees( GetEllipseEndAngle().AsDegrees() );
337 break;
338 }
339
340 default:
341 wxASSERT_MSG( false, "Unhandled shape in EDA_SHAPE::Serialize" );
342 }
343
344 if( m_startEnding.GetStyle() != LINE_ENDING_STYLE::NONE )
345 PackLineEnding( *shape.mutable_start_ending(), m_startEnding, aScale );
346
347 if( m_endEnding.GetStyle() != LINE_ENDING_STYLE::NONE )
348 PackLineEnding( *shape.mutable_end_ending(), m_endEnding, aScale );
349
350 // TODO m_hasSolderMask and m_solderMaskMargin
351
352}
353
354
355void EDA_SHAPE::Serialize( google::protobuf::Any &aContainer, const EDA_IU_SCALE &aScale ) const
356{
357 kiapi::common::types::GraphicShape shape;
358 Serialize( shape, aScale );
359
360 if( !aContainer.PackFrom( shape ) )
361 wxFAIL_MSG( wxT( "Failed to pack EDA_SHAPE" ) );
362}
363
364
365bool EDA_SHAPE::Deserialize( const google::protobuf::Any &aContainer )
366{
367 return Deserialize( aContainer, pcbIUScale );
368}
369
370
371bool EDA_SHAPE::Deserialize( const kiapi::common::types::GraphicShape& shape, const EDA_IU_SCALE &aScale )
372{
373 using namespace kiapi::common;
374
375 // Initialize everything to a known state that doesn't get touched by every
376 // codepath below, to make sure the equality operator is consistent
377 m_start = {};
378 m_end = {};
379 m_arcCenter = {};
380 m_arcMidData = {};
381 m_startEnding = {};
382 m_endEnding = {};
383 m_bezierC1 = {};
384 m_bezierC2 = {};
385 m_editState = 0;
386 m_proxyItem = false;
387 m_endsSwapped = false;
389
390 UnpackStroke( m_stroke, shape.attributes().stroke(), aScale );
391
392 if( shape.attributes().has_fill() )
393 {
394 SetFillMode( FromProtoEnum<FILL_T, types::GraphicFillType>( shape.attributes().fill().fill_type() ) );
395
396 if( shape.attributes().fill().has_color() )
397 SetFillColor( UnpackColor( shape.attributes().fill().color() ) );
398 }
399
400 if( shape.has_segment() )
401 {
403 SetStart( UnpackVector2( shape.segment().start(), aScale ) );
404 SetEnd( UnpackVector2( shape.segment().end(), aScale ) );
405 }
406 else if( shape.has_rectangle() )
407 {
409 SetStart( UnpackVector2( shape.rectangle().top_left(), aScale ) );
410 SetEnd( UnpackVector2( shape.rectangle().bottom_right(), aScale ) );
411 SetCornerRadius( UnpackDistance( shape.rectangle().corner_radius(), aScale ) );
412 }
413 else if( shape.has_arc() )
414 {
416 SetArcGeometry( UnpackVector2( shape.arc().start(), aScale ),
417 UnpackVector2( shape.arc().mid(), aScale ),
418 UnpackVector2( shape.arc().end(), aScale ) );
419 }
420 else if( shape.has_circle() )
421 {
423 SetStart( UnpackVector2( shape.circle().center(), aScale ) );
424 SetEnd( UnpackVector2( shape.circle().radius_point(), aScale ) );
425 }
426 else if( shape.has_polygon() )
427 {
429 SetPolyShape( UnpackPolySet( shape.polygon(), aScale ) );
430 }
431 else if( shape.has_bezier() )
432 {
434 SetStart( UnpackVector2( shape.bezier().start(), aScale ) );
435 SetBezierC1( UnpackVector2( shape.bezier().control1(), aScale ) );
436 SetBezierC2( UnpackVector2( shape.bezier().control2(), aScale ) );
437 SetEnd( UnpackVector2( shape.bezier().end(), aScale ) );
439 }
440 else if( shape.has_ellipse() )
441 {
443 SetEllipseCenter( UnpackVector2( shape.ellipse().center(), aScale ) );
444 SetEllipseMajorRadius( UnpackDistance( shape.ellipse().major_radius(), aScale ) );
445 SetEllipseMinorRadius( UnpackDistance( shape.ellipse().minor_radius(), aScale ) );
446 SetEllipseRotation( EDA_ANGLE( shape.ellipse().rotation().value_degrees(), DEGREES_T ) );
447 }
448 else if( shape.has_ellipse_arc() )
449 {
451 SetEllipseCenter( UnpackVector2( shape.ellipse_arc().center(), aScale ) );
452 SetEllipseMajorRadius( UnpackDistance( shape.ellipse_arc().major_radius(), aScale ) );
453 SetEllipseMinorRadius( UnpackDistance( shape.ellipse_arc().minor_radius(), aScale ) );
454 SetEllipseRotation( EDA_ANGLE( shape.ellipse_arc().rotation().value_degrees(), DEGREES_T ) );
455 SetEllipseStartAngle( EDA_ANGLE( shape.ellipse_arc().start_angle().value_degrees(), DEGREES_T ) );
456 SetEllipseEndAngle( EDA_ANGLE( shape.ellipse_arc().end_angle().value_degrees(), DEGREES_T ) );
457 }
458
459 if( shape.has_start_ending() )
460 m_startEnding = UnpackLineEnding( shape.start_ending(), aScale );
461
462 if( shape.has_end_ending() )
463 m_endEnding = UnpackLineEnding( shape.end_ending(), aScale );
464
465 return true;
466}
467
468bool EDA_SHAPE::Deserialize( const google::protobuf::Any &aContainer, const EDA_IU_SCALE &aScale )
469{
470 kiapi::common::types::GraphicShape shape;
471
472 if( !aContainer.UnpackTo( &shape ) )
473 return false;
474
475 return Deserialize( shape, aScale );
476}
477
478
479wxString EDA_SHAPE::ShowShape() const
480{
481 if( IsProxyItem() )
482 {
483 switch( m_shape )
484 {
485 case SHAPE_T::SEGMENT: return _( "Thermal Spoke" );
486 case SHAPE_T::RECTANGLE: return _( "Number Box" );
487 default: return wxT( "??" );
488 }
489 }
490 else
491 {
492 switch( m_shape )
493 {
494 case SHAPE_T::SEGMENT: return _( "Line" );
495 case SHAPE_T::RECTANGLE: return _( "Rect" );
496 case SHAPE_T::ARC: return _( "Arc" );
497 case SHAPE_T::CIRCLE: return _( "Circle" );
498 case SHAPE_T::BEZIER: return _( "Bezier Curve" );
499 case SHAPE_T::POLY: return _( "Polygon" );
500 case SHAPE_T::ELLIPSE: return _( "Ellipse" );
501 case SHAPE_T::ELLIPSE_ARC: return _( "Elliptical Arc" );
502 default: return wxT( "??" );
503 }
504 }
505}
506
507
509{
510 switch( m_shape )
511 {
512 case SHAPE_T::SEGMENT: return wxS( "S_SEGMENT" );
513 case SHAPE_T::RECTANGLE: return wxS( "S_RECT" );
514 case SHAPE_T::ARC: return wxS( "S_ARC" );
515 case SHAPE_T::CIRCLE: return wxS( "S_CIRCLE" );
516 case SHAPE_T::POLY: return wxS( "S_POLYGON" );
517 case SHAPE_T::BEZIER: return wxS( "S_CURVE" );
518 case SHAPE_T::ELLIPSE: return wxS( "S_ELLIPSE" );
519 case SHAPE_T::ELLIPSE_ARC: return wxS( "S_ELLIPSE_ARC" );
520 case SHAPE_T::UNDEFINED: return wxS( "UNDEFINED" );
521 }
522
523 return wxEmptyString; // Just to quiet GCC.
524}
525
526
528{
529 move( aPos - getPosition() );
530}
531
532
534{
536 return getCenter();
537 else if( m_shape == SHAPE_T::POLY )
538 return GetPolyShape().CVertex( 0 );
539 else
540 return m_start;
541}
542
543
545{
546 double length = 0.0;
547
548 switch( m_shape )
549 {
550 case SHAPE_T::BEZIER:
551 for( size_t ii = 1; ii < m_bezierPoints.size(); ++ii )
552 length += m_bezierPoints[ ii - 1].Distance( m_bezierPoints[ii] );
553
554 return length;
555
556 case SHAPE_T::SEGMENT:
557 return GetStart().Distance( GetEnd() );
558
559 case SHAPE_T::POLY:
560 for( int ii = 0; ii < GetPolyShape().COutline( 0 ).SegmentCount(); ii++ )
561 length += GetPolyShape().COutline( 0 ).CSegment( ii ).Length();
562
563 return length;
564
565 case SHAPE_T::ARC:
566 return GetRadius() * GetArcAngle().AsRadians();
567
568 case SHAPE_T::ELLIPSE:
570 return buildShapeEllipse().GetLength();
571
572 default:
574 return 0.0;
575 }
576}
577
578
580{
581 switch( m_shape )
582 {
584 return GetEndY() - GetStartY();
585
586 default:
588 return 0;
589 }
590}
591
592
594{
595 switch( m_shape )
596 {
598 return GetEndX() - GetStartX();
599
600 default:
602 return 0;
603 }
604}
605
606
608{
609 return m_cornerRadius;
610}
611
612
613void EDA_SHAPE::SetCornerRadius( int aRadius )
614{
616 {
617 int width = std::abs( GetRectangleWidth() );
618 int height = std::abs( GetRectangleHeight() );
619 int maxRadius = std::min( width, height ) / 2;
620
621 m_cornerRadius = std::clamp( aRadius, 0, maxRadius );
622 }
623 else
624 {
625 m_cornerRadius = aRadius;
626 }
627
628 m_hatchingDirty = true;
629}
630
631
632void EDA_SHAPE::SetRectangleHeight( const int& aHeight )
633{
634 switch ( m_shape )
635 {
637 m_rectangleHeight = aHeight;
639 break;
640
641 default:
643 }
644}
645
646
647void EDA_SHAPE::SetRectangleWidth( const int& aWidth )
648{
649 switch ( m_shape )
650 {
652 m_rectangleWidth = aWidth;
654 break;
655
656 default:
658 }
659}
660
661
662void EDA_SHAPE::SetRectangle( const long long int& aHeight, const long long int& aWidth )
663{
664 switch ( m_shape )
665 {
667 SetRectangleHeight( aHeight );
668 SetRectangleWidth( aWidth );
669 break;
670
671 default:
673 }
674}
675
676
678{
679 switch( m_shape )
680 {
681 case SHAPE_T::CIRCLE:
683 case SHAPE_T::ELLIPSE:
684 return true;
685
686 case SHAPE_T::ARC:
687 case SHAPE_T::SEGMENT:
689 return false;
690
691 case SHAPE_T::POLY:
692 if( GetPolyShape().IsEmpty() )
693 return false;
694 else
695 return GetPolyShape().Outline( 0 ).IsClosed();
696
697 case SHAPE_T::BEZIER:
698 if( m_bezierPoints.size() < 3 )
699 return false;
700 else
701 return m_bezierPoints[0] == m_bezierPoints[ m_bezierPoints.size() - 1 ];
702
703 default:
705 return false;
706 }
707}
708
709
711{
712 m_fill = aFill;
713 m_hatchingDirty = true;
714}
715
716
718{
719 switch( aFill )
720 {
725 default: SetFilled( true ); break;
726 }
727}
728
729
741
742
744{
746
747 if( !m_hatchingCache )
748 m_hatchingCache = std::make_unique<EDA_SHAPE_HATCH_CACHE_DATA>();
749
750 return m_hatchingCache->hatching;
751}
752
753
754const std::vector<SEG>& EDA_SHAPE::GetHatchLines() const
755{
757
758 if( !m_hatchingCache )
759 m_hatchingCache = std::make_unique<EDA_SHAPE_HATCH_CACHE_DATA>();
760
761 return m_hatchingCache->hatchLines;
762}
763
764
766{
767 if( !m_hatchingCache )
768 m_hatchingCache = std::make_unique<EDA_SHAPE_HATCH_CACHE_DATA>();
769
770 return m_hatchingCache->hatching;
771}
772
773
774std::vector<SEG>& EDA_SHAPE::hatchLines() const
775{
776 if( !m_hatchingCache )
777 m_hatchingCache = std::make_unique<EDA_SHAPE_HATCH_CACHE_DATA>();
778
779 return m_hatchingCache->hatchLines;
780}
781
782
784{
785 if( !m_hatchingDirty )
786 return;
787
788 std::vector<double> slopes;
789 int lineWidth = GetHatchLineWidth();
790 int spacing = GetHatchLineSpacing();
791 SHAPE_POLY_SET shapeBuffer;
792
793 // Validate state before clearing cached hatching. If we can't regenerate, keep existing cache.
794 if( isMoving() )
795 return;
796
798 slopes = { 1.0, -1.0 };
799 else if( GetFillMode() == FILL_T::HATCH )
800 slopes = { -1.0 };
801 else if( GetFillMode() == FILL_T::REVERSE_HATCH )
802 slopes = { 1.0 };
803 else
804 return;
805
806 if( spacing == 0 )
807 return;
808
809 switch( m_shape )
810 {
811 case SHAPE_T::ARC:
812 case SHAPE_T::SEGMENT:
813 case SHAPE_T::BEZIER:
815 return;
816
818 {
820 rr.TransformToPolygon( shapeBuffer, getMaxError() );
821 }
822 break;
823
824 case SHAPE_T::CIRCLE:
826 break;
827
828 case SHAPE_T::POLY:
829 if( GetPolyShape().OutlineCount() == 0 )
830 return;
831
832 shapeBuffer = GetPolyShape().CloneDropTriangulation();
833
834 for( int ii = 0; ii < shapeBuffer.OutlineCount(); ++ii )
835 {
836 SHAPE_LINE_CHAIN& outline = shapeBuffer.Outline( ii );
837
838 if( outline.IsClosed() )
839 continue;
840
841 if( outline.PointCount() < 3 )
842 continue;
843
844 outline.SetClosed( true );
845 }
846
847 break;
848
849 case SHAPE_T::ELLIPSE:
850 {
851 // Hatching only applies to closed, fillable shapes.
854 chain.SetClosed( true );
855 shapeBuffer.AddOutline( chain );
856 break;
857 }
858
859 default:
861 return;
862 }
863
864 shapeBuffer.ClearArcs();
865
866 // Clear cached hatching only after all validation passes.
867 // This prevents flickering when early returns would otherwise leave empty hatching.
869 hatchLines().clear();
870
871 BOX2I extents = shapeBuffer.BBox();
872 int majorAxis = std::max( extents.GetWidth(), extents.GetHeight() );
873
874 if( majorAxis / spacing > 100 )
875 spacing = majorAxis / 100;
876
878
879 if( !knockouts.IsEmpty() )
880 {
881 shapeBuffer.BooleanSubtract( knockouts );
882 shapeBuffer.Fracture();
883 }
884
885 // Generate hatch lines for stroke-based rendering. All hatch types use line segments.
886 std::vector<SEG> hatchSegs = shapeBuffer.GenerateHatchLines( slopes, spacing, -1 );
887 hatchLines() = hatchSegs;
888
889 // Also generate polygon representation for exports, 3D viewer, and hit testing
891 {
892 for( const SEG& seg : hatchSegs )
893 {
894 // We don't really need the rounded ends at all, so don't spend any extra time on them
895 int maxError = lineWidth;
896
897 TransformOvalToPolygon( hatching(), seg.A, seg.B, lineWidth, maxError,
898 ERROR_INSIDE );
899 }
900
901 hatching().Fracture();
902 m_hatchingDirty = false;
903 }
904 else
905 {
906 // Generate a grid of holes for a cross-hatch polygon representation.
907 // This is used for exports, 3D viewer, and hit testing.
908
909 int gridsize = spacing;
910 int hole_size = gridsize - GetHatchLineWidth();
911
912 hatching() = shapeBuffer.CloneDropTriangulation();
914
915 // Build hole shape
916 SHAPE_LINE_CHAIN hole_base;
917 VECTOR2I corner( 0, 0 );;
918 hole_base.Append( corner );
919 corner.x += hole_size;
920 hole_base.Append( corner );
921 corner.y += hole_size;
922 hole_base.Append( corner );
923 corner.x = 0;
924 hole_base.Append( corner );
925 hole_base.SetClosed( true );
926
927 // Build holes
928 BOX2I bbox = hatching().BBox( 0 );
929 SHAPE_POLY_SET holes;
930
931 int x_offset = bbox.GetX() - ( bbox.GetX() ) % gridsize - gridsize;
932 int y_offset = bbox.GetY() - ( bbox.GetY() ) % gridsize - gridsize;
933
934 for( int xx = x_offset; xx <= bbox.GetRight(); xx += gridsize )
935 {
936 for( int yy = y_offset; yy <= bbox.GetBottom(); yy += gridsize )
937 {
938 SHAPE_LINE_CHAIN hole( hole_base );
939 hole.Move( VECTOR2I( xx, yy ) );
940 holes.AddOutline( hole );
941 }
942 }
943
944 hatching().BooleanSubtract( holes );
945 hatching().Fracture();
946
947 // Must re-rotate after Fracture(). Clipper struggles mightily with fracturing
948 // 45-degree holes.
950
951 if( !knockouts.IsEmpty() )
952 {
953 hatching().BooleanSubtract( knockouts );
954 hatching().Fracture();
955 }
956
957 m_hatchingDirty = false;
958 }
959}
960
961
962void EDA_SHAPE::move( const VECTOR2I& aMoveVector )
963{
964 switch ( m_shape )
965 {
966 case SHAPE_T::ARC:
967 m_arcCenter += aMoveVector;
968 m_arcMidData.center += aMoveVector;
969 m_arcMidData.start += aMoveVector;
970 m_arcMidData.end += aMoveVector;
971 m_arcMidData.mid += aMoveVector;
973
974 case SHAPE_T::SEGMENT:
976 case SHAPE_T::CIRCLE:
977 m_start += aMoveVector;
978 m_end += aMoveVector;
979 break;
980
981 case SHAPE_T::POLY:
982 GetPolyShape().Move( aMoveVector );
983 break;
984
985 case SHAPE_T::BEZIER:
986 m_start += aMoveVector;
987 m_end += aMoveVector;
988 m_bezierC1 += aMoveVector;
989 m_bezierC2 += aMoveVector;
990
991 for( VECTOR2I& pt : m_bezierPoints )
992 pt += aMoveVector;
993
994 break;
995
996 case SHAPE_T::ELLIPSE:
998 m_ellipse.Center += aMoveVector;
999 m_start += aMoveVector;
1000 m_end += aMoveVector;
1001 break;
1002
1003 default:
1005 break;
1006 }
1007
1008 // Translate the cached hatch geometry instead of leaving it stale. The hatch pattern is
1009 // invariant under translation, so shifting line endpoints is sufficient and keeps the
1010 // display correct during interactive moves without hitting GenerateHatchLines().
1011 if( m_hatchingCache )
1012 {
1013 for( SEG& seg : m_hatchingCache->hatchLines )
1014 {
1015 seg.A += aMoveVector;
1016 seg.B += aMoveVector;
1017 }
1018
1019 m_hatchingCache->hatching.Move( aMoveVector );
1020 }
1021
1022 m_hatchingDirty = true;
1023}
1024
1025
1026void EDA_SHAPE::scale( double aScale )
1027{
1028 auto scalePt =
1029 [&]( VECTOR2I& pt )
1030 {
1031 pt.x = KiROUND( pt.x * aScale );
1032 pt.y = KiROUND( pt.y * aScale );
1033 };
1034
1035 switch( m_shape )
1036 {
1037 case SHAPE_T::ARC:
1038 scalePt( m_arcCenter );
1040
1041 case SHAPE_T::SEGMENT:
1042 case SHAPE_T::RECTANGLE:
1043 case SHAPE_T::CIRCLE:
1044 scalePt( m_start );
1045 scalePt( m_end );
1046 break;
1047
1048 case SHAPE_T::POLY: // polygon
1049 {
1050 std::vector<VECTOR2I> pts;
1051
1052 for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ ii )
1053 {
1054 for( const VECTOR2I& pt : GetPolyShape().Outline( ii ).CPoints() )
1055 {
1056 pts.emplace_back( pt );
1057 scalePt( pts.back() );
1058 }
1059 }
1060
1061 SetPolyPoints( pts );
1062 }
1063 break;
1064
1065 case SHAPE_T::BEZIER:
1066 scalePt( m_start );
1067 scalePt( m_end );
1068 scalePt( m_bezierC1 );
1069 scalePt( m_bezierC2 );
1071 break;
1072
1073 case SHAPE_T::ELLIPSE:
1075 scalePt( m_ellipse.Center );
1076 m_ellipse.MajorRadius = KiROUND( std::abs( m_ellipse.MajorRadius * aScale ) );
1077 m_ellipse.MinorRadius = KiROUND( std::abs( m_ellipse.MinorRadius * aScale ) );
1079 break;
1080
1081 default:
1083 break;
1084 }
1085
1086 m_hatchingDirty = true;
1087}
1088
1089
1090void EDA_SHAPE::rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
1091{
1092 switch( m_shape )
1093 {
1094 case SHAPE_T::SEGMENT:
1095 case SHAPE_T::CIRCLE:
1096 RotatePoint( m_start, aRotCentre, aAngle );
1097 RotatePoint( m_end, aRotCentre, aAngle );
1098 break;
1099
1100 case SHAPE_T::ARC:
1101 RotatePoint( m_start, aRotCentre, aAngle );
1102 RotatePoint( m_end, aRotCentre, aAngle );
1103 RotatePoint( m_arcCenter, aRotCentre, aAngle );
1104 RotatePoint( m_arcMidData.start, aRotCentre, aAngle );
1105 RotatePoint( m_arcMidData.end, aRotCentre, aAngle );
1106 RotatePoint( m_arcMidData.mid, aRotCentre, aAngle );
1107 RotatePoint( m_arcMidData.center, aRotCentre, aAngle );
1108 break;
1109
1110 case SHAPE_T::RECTANGLE:
1111 if( aAngle.IsCardinal() )
1112 {
1113 RotatePoint( m_start, aRotCentre, aAngle );
1114 RotatePoint( m_end, aRotCentre, aAngle );
1115 }
1116 else
1117 {
1118 // Convert non-cardinally-rotated rect to a polygon.
1122 GetPolyShape().Rotate( aAngle, aRotCentre );
1123 }
1124
1125 break;
1126
1127 case SHAPE_T::POLY:
1128 GetPolyShape().Rotate( aAngle, aRotCentre );
1129 break;
1130
1131 case SHAPE_T::BEZIER:
1132 RotatePoint( m_start, aRotCentre, aAngle );
1133 RotatePoint( m_end, aRotCentre, aAngle );
1134 RotatePoint( m_bezierC1, aRotCentre, aAngle );
1135 RotatePoint( m_bezierC2, aRotCentre, aAngle );
1136
1137 for( VECTOR2I& pt : m_bezierPoints )
1138 RotatePoint( pt, aRotCentre, aAngle);
1139
1140 break;
1141
1142 case SHAPE_T::ELLIPSE:
1144 RotatePoint( m_ellipse.Center, aRotCentre, aAngle );
1145
1146 // Ellipse rotation is the CCW angle of the major axis in standard math
1147 // coordinates (Y-up). RotatePoint uses KiCad's Y-down screen convention,
1148 // so a positive aAngle rotates visually CCW on screen but corresponds to
1149 // a negative rotation in the math frame. Hence -= rather than +=.
1150 m_ellipse.Rotation -= aAngle;
1152 break;
1153
1154 default:
1156 break;
1157 }
1158
1159 m_hatchingDirty = true;
1160}
1161
1162
1163void EDA_SHAPE::flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
1164{
1165 switch ( m_shape )
1166 {
1167 case SHAPE_T::SEGMENT:
1168 case SHAPE_T::RECTANGLE:
1169 MIRROR( m_start, aCentre, aFlipDirection );
1170 MIRROR( m_end, aCentre, aFlipDirection );
1171 break;
1172
1173 case SHAPE_T::CIRCLE:
1174 MIRROR( m_start, aCentre, aFlipDirection );
1175 MIRROR( m_end, aCentre, aFlipDirection );
1176 break;
1177
1178 case SHAPE_T::ARC:
1179 MIRROR( m_start, aCentre, aFlipDirection );
1180 MIRROR( m_end, aCentre, aFlipDirection );
1181 MIRROR( m_arcCenter, aCentre, aFlipDirection );
1182
1183 std::swap( m_start, m_end );
1184 break;
1185
1186 case SHAPE_T::POLY:
1187 GetPolyShape().Mirror( aCentre, aFlipDirection );
1188 break;
1189
1190 case SHAPE_T::BEZIER:
1191 MIRROR( m_start, aCentre, aFlipDirection );
1192 MIRROR( m_end, aCentre, aFlipDirection );
1193 MIRROR( m_bezierC1, aCentre, aFlipDirection );
1194 MIRROR( m_bezierC2, aCentre, aFlipDirection );
1195
1197 break;
1198
1199 case SHAPE_T::ELLIPSE:
1201 m_ellipse.Mirror( aCentre, aFlipDirection );
1203 break;
1204
1205 default:
1207 break;
1208 }
1209
1210 m_hatchingDirty = true;
1211}
1212
1213
1214void EDA_SHAPE::mirror( const SEG& aAxis )
1215{
1216 switch( m_shape )
1217 {
1218 case SHAPE_T::SEGMENT:
1219 case SHAPE_T::CIRCLE:
1220 m_start = aAxis.ReflectPoint( m_start );
1221 m_end = aAxis.ReflectPoint( m_end );
1222 break;
1223
1224 case SHAPE_T::RECTANGLE:
1225 if( aAxis.A.x == aAxis.B.x || aAxis.A.y == aAxis.B.y )
1226 {
1227 // An axis-aligned rectangle stays a rectangle.
1228 m_start = aAxis.ReflectPoint( m_start );
1229 m_end = aAxis.ReflectPoint( m_end );
1230 }
1231 else
1232 {
1233 // Convert non-cardinally-mirrored rect to a polygon.
1237 GetPolyShape().Mirror( aAxis );
1238 }
1239
1240 break;
1241
1242 case SHAPE_T::ARC:
1244 aAxis.ReflectPoint( m_end ) );
1245 break;
1246
1247 case SHAPE_T::POLY:
1248 GetPolyShape().Mirror( aAxis );
1249 break;
1250
1251 case SHAPE_T::BEZIER:
1252 m_start = aAxis.ReflectPoint( m_start );
1253 m_end = aAxis.ReflectPoint( m_end );
1256
1258 break;
1259
1260 case SHAPE_T::ELLIPSE:
1262 {
1263 const EDA_ANGLE axisAngle = EDA_ANGLE( aAxis.B - aAxis.A );
1264 const EDA_ANGLE oldStart = m_ellipse.StartAngle;
1265
1266 m_ellipse.Center = aAxis.ReflectPoint( m_ellipse.Center );
1267 m_ellipse.Rotation = m_ellipse.Rotation.Reflect( axisAngle );
1268 m_ellipse.Rotation.Normalize();
1269
1270 // The mirrored parameterisation runs the other way round.
1271 m_ellipse.StartAngle = -m_ellipse.EndAngle;
1272 m_ellipse.EndAngle = -oldStart;
1273
1275 break;
1276 }
1277
1278 default:
1280 break;
1281 }
1282
1283 m_hatchingDirty = true;
1284}
1285
1286
1288{
1289 // Has meaning only for SHAPE_T::BEZIER
1290 if( m_shape != SHAPE_T::BEZIER )
1291 {
1292 m_bezierPoints.clear();
1293 return;
1294 }
1295
1296 // Rebuild the m_BezierPoints vertex list that approximate the Bezier curve
1298}
1299
1300
1301const std::vector<VECTOR2I> EDA_SHAPE::buildBezierToSegmentsPointsList( int aMaxError ) const
1302{
1303 std::vector<VECTOR2I> bezierPoints;
1304
1305 // Rebuild the m_BezierPoints vertex list that approximate the Bezier curve
1306 std::vector<VECTOR2I> ctrlPoints = { m_start, m_bezierC1, m_bezierC2, m_end };
1307 BEZIER_POLY converter( ctrlPoints );
1308 converter.GetPoly( bezierPoints, aMaxError );
1309
1310 return bezierPoints;
1311}
1312
1313
1315{
1317 {
1318 return SHAPE_ELLIPSE( m_ellipse.Center, m_ellipse.MajorRadius, m_ellipse.MinorRadius, m_ellipse.Rotation,
1319 m_ellipse.StartAngle, m_ellipse.EndAngle );
1320 }
1321
1322 return SHAPE_ELLIPSE( m_ellipse.Center, m_ellipse.MajorRadius, m_ellipse.MinorRadius, m_ellipse.Rotation );
1323}
1324
1325
1327{
1328 if( m_editState != 0 )
1329 return;
1330
1331 if( m_shape == SHAPE_T::ELLIPSE )
1332 {
1333 const double phi = m_ellipse.Rotation.AsRadians();
1334 m_start = m_ellipse.Center;
1335 m_end = m_start
1336 + VECTOR2I( KiROUND( m_ellipse.MajorRadius * std::cos( phi ) ),
1337 KiROUND( m_ellipse.MajorRadius * std::sin( phi ) ) );
1338 return;
1339 }
1340
1342 return;
1343
1344 m_arcCenter = m_ellipse.Center;
1345
1346 const double a = m_ellipse.MajorRadius;
1347 const double b = m_ellipse.MinorRadius;
1348 const double phi = m_ellipse.Rotation.AsRadians();
1349 const double cosPhi = std::cos( phi );
1350 const double sinPhi = std::sin( phi );
1351 const VECTOR2I c = m_ellipse.Center;
1352
1353 auto eval = [&]( double theta ) -> VECTOR2I
1354 {
1355 const double lx = a * std::cos( theta );
1356 const double ly = b * std::sin( theta );
1357 return c + VECTOR2I( KiROUND( lx * cosPhi - ly * sinPhi ), KiROUND( lx * sinPhi + ly * cosPhi ) );
1358 };
1359
1360 m_start = eval( m_ellipse.StartAngle.AsRadians() );
1361 m_end = eval( m_ellipse.EndAngle.AsRadians() );
1362}
1363
1364
1366{
1367 switch( m_shape )
1368 {
1369 case SHAPE_T::ARC:
1370 return m_arcCenter;
1371
1372 case SHAPE_T::CIRCLE:
1373 return m_start;
1374
1375 case SHAPE_T::SEGMENT:
1376 // Midpoint of the line
1377 return ( m_start + m_end ) / 2;
1378
1379 case SHAPE_T::POLY:
1380 case SHAPE_T::RECTANGLE:
1381 case SHAPE_T::BEZIER:
1382 return getBoundingBox().Centre();
1383
1384 case SHAPE_T::ELLIPSE:
1386 return m_ellipse.Center;
1387
1388 default:
1390 return VECTOR2I();
1391 }
1392}
1393
1394
1395void EDA_SHAPE::SetCenter( const VECTOR2I& aCenter )
1396{
1397 switch( m_shape )
1398 {
1399 case SHAPE_T::ARC:
1400 m_arcCenter = aCenter;
1401 break;
1402
1403 case SHAPE_T::CIRCLE:
1404 {
1405 // Route through SetStart / SetEnd so subclasses sync lib coords.
1406 const VECTOR2I delta = aCenter - m_start;
1407 SetEnd( m_end + delta );
1408 SetStart( aCenter );
1409 m_hatchingDirty = true;
1410 break;
1411 }
1412
1413 case SHAPE_T::ELLIPSE:
1415 m_ellipse.Center = aCenter;
1416 m_hatchingDirty = true;
1418 break;
1419
1420 default:
1422 }
1423}
1424
1425
1427{
1428 // If none of the input data have changed since we loaded the arc, keep the original mid point data
1429 // to minimize churn
1430 if( m_arcMidData.start == m_start && m_arcMidData.end == m_end && m_arcMidData.center == m_arcCenter )
1431 return m_arcMidData.mid;
1432
1433 VECTOR2I mid = m_start;
1434 RotatePoint( mid, m_arcCenter, -GetArcAngle() / 2.0 );
1435 return mid;
1436}
1437
1438
1439void EDA_SHAPE::CalcArcAngles( EDA_ANGLE& aStartAngle, EDA_ANGLE& aEndAngle ) const
1440{
1441 VECTOR2D startRadial( GetStart() - getCenter() );
1442 VECTOR2D endRadial( GetEnd() - getCenter() );
1443
1444 aStartAngle = EDA_ANGLE( startRadial );
1445 aEndAngle = EDA_ANGLE( endRadial );
1446
1447 if( aEndAngle == aStartAngle )
1448 aEndAngle = aStartAngle + ANGLE_360; // ring, not null
1449
1450 while( aEndAngle < aStartAngle )
1451 aEndAngle += ANGLE_360;
1452}
1453
1454
1456{
1457 double radius = 0.0;
1458
1459 switch( m_shape )
1460 {
1461 case SHAPE_T::ARC:
1462 radius = m_arcCenter.Distance( m_start );
1463 break;
1464
1465 case SHAPE_T::CIRCLE:
1466 radius = m_start.Distance( m_end );
1467 break;
1468
1469 default:
1471 }
1472
1473 // don't allow degenerate circles/arcs
1474 if( radius > (double) INT_MAX / 2.0 )
1475 radius = (double) INT_MAX / 2.0;
1476
1477 return std::max( 1, KiROUND( radius ) );
1478}
1479
1480
1481void EDA_SHAPE::SetCachedArcData( const VECTOR2I& aStart, const VECTOR2I& aMid,
1482 const VECTOR2I& aEnd, const VECTOR2I& aCenter )
1483{
1484 m_arcMidData.start = aStart;
1485 m_arcMidData.end = aEnd;
1486 m_arcMidData.center = aCenter;
1487 m_arcMidData.mid = aMid;
1488}
1489
1490
1491void EDA_SHAPE::SetArcOrSegment( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd )
1492{
1493 if( IsStraightArc( aStart, aMid, aEnd ) )
1494 {
1496 SetStart( aStart );
1497 SetEnd( aEnd );
1498 }
1499 else
1500 {
1501 SetArcGeometry( aStart, aMid, aEnd );
1502 }
1503}
1504
1505
1506void EDA_SHAPE::SetArcGeometry( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd )
1507{
1508 m_arcMidData = {};
1509 m_start = aStart;
1510 m_end = aEnd;
1511 m_arcCenter = CalcArcCenter( aStart, aMid, aEnd );
1512 VECTOR2I new_mid = GetArcMid();
1513
1514 m_endsSwapped = false;
1515
1516 /*
1517 * If the input winding doesn't match our internal winding, the calculated midpoint will end
1518 * up on the other side of the arc. In this case, we need to flip the start/end points and
1519 * flag this change for the system.
1520 */
1521 VECTOR2D dist( new_mid - aMid );
1522 VECTOR2D dist2( new_mid - m_arcCenter );
1523
1524 if( dist.SquaredEuclideanNorm() > dist2.SquaredEuclideanNorm() )
1525 {
1526 std::swap( m_start, m_end );
1527 m_endsSwapped = true;
1528 }
1529
1530 // Watch the ordering here. GetArcMid above needs to be called prior to initializing the
1531 // m_arcMidData structure in order to ensure we get the calculated variant, not the cached.
1532 // The cache is keyed on the stored ends, so it must follow any swap or GetArcMid never hits it
1534}
1535
1536
1538{
1539 EDA_ANGLE angle( atan2( static_cast<double>( GetStart().y - GetEnd().y ),
1540 static_cast<double>( GetEnd().x - GetStart().x ) ), RADIANS_T );
1541
1542 return angle;
1543}
1544
1545
1547{
1548 EDA_ANGLE startAngle;
1549 EDA_ANGLE endAngle;
1550
1551 CalcArcAngles( startAngle, endAngle );
1552
1553 return endAngle - startAngle;
1554}
1555
1556
1558{
1559 if( m_shape == SHAPE_T::ARC )
1560 {
1561 VECTOR2D mid = GetArcMid();
1562
1563 double orient = ( mid.x - m_start.x ) * ( m_end.y - m_start.y )
1564 - ( mid.y - m_start.y ) * ( m_end.x - m_start.x );
1565
1566 return orient < 0;
1567 }
1568
1570 return false;
1571}
1572
1573
1574void EDA_SHAPE::SetArcAngleAndEnd( const EDA_ANGLE& aAngle, bool aCheckNegativeAngle )
1575{
1576 EDA_ANGLE angle( aAngle );
1577
1578 m_end = m_start;
1580
1581 if( aCheckNegativeAngle && aAngle < ANGLE_0 )
1582 {
1583 std::swap( m_start, m_end );
1584 m_endsSwapped = true;
1585 }
1586}
1587
1588
1589wxString EDA_SHAPE::getFriendlyName( FRAME_T aFrameType ) const
1590{
1591 if( IsProxyItem() )
1592 {
1593 switch( m_shape )
1594 {
1595 case SHAPE_T::RECTANGLE: return _( "Pad Number Box" );
1596 case SHAPE_T::SEGMENT: return _( "Thermal Spoke Template" );
1597 default: return _( "Unrecognized" );
1598 }
1599 }
1600 else if( aFrameType == FRAME_SCH_SYMBOL_EDITOR && m_shape == SHAPE_T::POLY )
1601 {
1602 return _( "Connected Lines" );
1603 }
1604 else
1605 {
1606 switch( m_shape )
1607 {
1608 case SHAPE_T::CIRCLE: return _( "Circle" );
1609 case SHAPE_T::ARC: return _( "Arc" );
1610 case SHAPE_T::BEZIER: return _( "Curve" );
1611 case SHAPE_T::POLY: return _( "Polygon" );
1612 case SHAPE_T::RECTANGLE: return _( "Rectangle" );
1613 case SHAPE_T::SEGMENT: return _( "Segment" );
1614 case SHAPE_T::ELLIPSE: return _( "Ellipse" );
1615 case SHAPE_T::ELLIPSE_ARC: return _( "Elliptical Arc" );
1616 default: return _( "Unrecognized" );
1617 }
1618 }
1619}
1620
1621
1622void EDA_SHAPE::ShapeGetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList )
1623{
1624 wxString msg;
1625
1626 wxString shape = _( "Shape" );
1627 aList.emplace_back( shape, getFriendlyName( aFrame->GetFrameType() ) );
1628
1629 switch( m_shape )
1630 {
1631 case SHAPE_T::CIRCLE:
1632 aList.emplace_back( _( "Radius" ), aFrame->MessageTextFromValue( GetRadius() ) );
1633 break;
1634
1635 case SHAPE_T::ARC:
1636 aList.emplace_back( _( "Length" ), aFrame->MessageTextFromValue( GetLength() ) );
1637
1639 aList.emplace_back( _( "Angle" ), msg );
1640
1641 aList.emplace_back( _( "Radius" ), aFrame->MessageTextFromValue( GetRadius() ) );
1642 break;
1643
1644 case SHAPE_T::BEZIER:
1645 aList.emplace_back( _( "Length" ), aFrame->MessageTextFromValue( GetLength() ) );
1646 break;
1647
1648 case SHAPE_T::ELLIPSE:
1649 aList.emplace_back( _( "Length" ), aFrame->MessageTextFromValue( GetLength() ) );
1650 aList.emplace_back( _( "Major Radius" ), aFrame->MessageTextFromValue( GetEllipseMajorRadius() ) );
1651 aList.emplace_back( _( "Minor Radius" ), aFrame->MessageTextFromValue( GetEllipseMinorRadius() ) );
1652 aList.emplace_back( _( "Rotation" ), EDA_UNIT_UTILS::UI::MessageTextFromValue( GetEllipseRotation() ) );
1653 break;
1654
1656 aList.emplace_back( _( "Length" ), aFrame->MessageTextFromValue( GetLength() ) );
1657 aList.emplace_back( _( "Major Radius" ), aFrame->MessageTextFromValue( GetEllipseMajorRadius() ) );
1658 aList.emplace_back( _( "Minor Radius" ), aFrame->MessageTextFromValue( GetEllipseMinorRadius() ) );
1659 aList.emplace_back( _( "Rotation" ), EDA_UNIT_UTILS::UI::MessageTextFromValue( GetEllipseRotation() ) );
1660 aList.emplace_back( _( "Start Angle" ), EDA_UNIT_UTILS::UI::MessageTextFromValue( GetEllipseStartAngle() ) );
1661 aList.emplace_back( _( "End Angle" ), EDA_UNIT_UTILS::UI::MessageTextFromValue( GetEllipseEndAngle() ) );
1662 break;
1663
1664 case SHAPE_T::POLY:
1665 {
1666 int pointCount = 0;
1667
1668 if( GetPolyShape().OutlineCount() > 0 )
1669 pointCount = GetPolyShape().Outline( 0 ).PointCount();
1670
1671 msg.Printf( wxS( "%d" ), pointCount );
1672 aList.emplace_back( _( "Points" ), msg );
1673 break;
1674 }
1675 case SHAPE_T::RECTANGLE:
1676 aList.emplace_back( _( "Width" ), aFrame->MessageTextFromValue( std::abs( GetEnd().x - GetStart().x ) ) );
1677 aList.emplace_back( _( "Height" ), aFrame->MessageTextFromValue( std::abs( GetEnd().y - GetStart().y ) ) );
1678 break;
1679
1680 case SHAPE_T::SEGMENT:
1681 {
1682 aList.emplace_back( _( "Length" ), aFrame->MessageTextFromValue( GetStart().Distance( GetEnd() ) ));
1683
1684 // angle counter-clockwise from 3'o-clock
1685 EDA_ANGLE angle( atan2( (double)( GetStart().y - GetEnd().y ), (double)( GetEnd().x - GetStart().x ) ),
1686 RADIANS_T );
1687 aList.emplace_back( _( "Angle" ), EDA_UNIT_UTILS::UI::MessageTextFromValue( angle ) );
1688 break;
1689 }
1690
1691 default:
1692 break;
1693 }
1694
1695 m_stroke.GetMsgPanelInfo( aFrame, aList );
1696}
1697
1698
1700{
1701 BOX2I bbox;
1702
1703 switch( m_shape )
1704 {
1705 case SHAPE_T::RECTANGLE:
1706 for( VECTOR2I& pt : GetRectCorners() )
1707 bbox.Merge( pt );
1708
1709 break;
1710
1711 case SHAPE_T::SEGMENT:
1712 bbox.SetOrigin( GetStart() );
1713 bbox.SetEnd( GetEnd() );
1714 break;
1715
1716 case SHAPE_T::CIRCLE:
1717 bbox.SetOrigin( GetStart() );
1718 bbox.Inflate( GetRadius() );
1719 break;
1720
1721 case SHAPE_T::ARC:
1722 computeArcBBox( bbox );
1723 break;
1724
1725 case SHAPE_T::ELLIPSE:
1727 bbox = buildShapeEllipse().BBox( 0 ); break;
1728
1729 case SHAPE_T::POLY:
1730 if( GetPolyShape().IsEmpty() )
1731 break;
1732
1733 for( auto iter = GetPolyShape().CIterate(); iter; iter++ )
1734 bbox.Merge( *iter );
1735
1736 break;
1737
1738 case SHAPE_T::BEZIER:
1739 // Bezier BBoxes are not trivial to compute, so we approximate it by
1740 // using the bounding box of the curve (not control!) points.
1741 for( const VECTOR2I& pt : m_bezierPoints )
1742 bbox.Merge( pt );
1743
1744 break;
1745
1746 default:
1748 break;
1749 }
1750
1751 bbox.Inflate( std::max( 0, GetWidth() ) / 2 );
1752 bbox.Normalize();
1753
1754 return bbox;
1755}
1756
1757
1758static bool hasLineEnding( const LINE_ENDING& aStartEnding, const LINE_ENDING& aEndEnding );
1759
1760
1761bool EDA_SHAPE::hitTest( const VECTOR2I& aPosition, int aAccuracy ) const
1762{
1764 {
1766 const SHAPE& hitShape = shape;
1767
1768 return hitShape.Collide( aPosition, std::max( 0, aAccuracy ) );
1769 }
1770
1771 double maxdist = aAccuracy;
1772
1773 if( GetWidth() > 0 )
1774 maxdist += GetWidth() / 2.0;
1775
1776 switch( m_shape )
1777 {
1778 case SHAPE_T::CIRCLE:
1779 {
1780 double radius = GetRadius();
1781 double dist = aPosition.Distance( getCenter() );
1782
1783 if( IsFilledForHitTesting() )
1784 return dist <= radius + maxdist; // Filled circle hit-test
1785 else if( abs( radius - dist ) <= maxdist ) // Ring hit-test
1786 return true;
1787
1788 if( IsHatchedFill() && GetHatching().Collide( aPosition, maxdist ) )
1789 return true;
1790
1791 return false;
1792 }
1793
1794 case SHAPE_T::ARC:
1795 {
1796 if( aPosition.Distance( m_start ) <= maxdist )
1797 return true;
1798
1799 if( aPosition.Distance( m_end ) <= maxdist )
1800 return true;
1801
1802 double radius = GetRadius();
1803 VECTOR2D relPos( VECTOR2D( aPosition ) - getCenter() );
1804 double dist = relPos.EuclideanNorm();
1805
1806 if( IsFilledForHitTesting() )
1807 {
1808 // Check distance from arc center
1809 if( dist > radius + maxdist )
1810 return false;
1811 }
1812 else
1813 {
1814 // Check distance from arc circumference
1815 if( abs( radius - dist ) > maxdist )
1816 return false;
1817 }
1818
1819 // Finally, check to see if it's within arc's swept angle.
1820 EDA_ANGLE startAngle;
1821 EDA_ANGLE endAngle;
1822 CalcArcAngles( startAngle, endAngle );
1823
1824 EDA_ANGLE relPosAngle( relPos );
1825
1826 startAngle.Normalize();
1827 endAngle.Normalize();
1828 relPosAngle.Normalize();
1829
1830 if( endAngle > startAngle )
1831 return relPosAngle >= startAngle && relPosAngle <= endAngle;
1832 else
1833 return relPosAngle >= startAngle || relPosAngle <= endAngle;
1834 }
1835
1836 case SHAPE_T::BEZIER:
1837 {
1838 const std::vector<VECTOR2I>* pts = &m_bezierPoints;
1839 std::vector<VECTOR2I> updatedBezierPoints;
1840
1841 if( m_bezierPoints.empty() )
1842 {
1844 converter.GetPoly( updatedBezierPoints, aAccuracy / 2 );
1845 pts = &updatedBezierPoints;
1846 }
1847
1848 for( unsigned int i = 1; i < pts->size(); i++ )
1849 {
1850 if( TestSegmentHit( aPosition, ( *pts )[i - 1], ( *pts )[i], maxdist ) )
1851 return true;
1852 }
1853
1854 return false;
1855 }
1856 case SHAPE_T::SEGMENT:
1857 return TestSegmentHit( aPosition, GetStart(), GetEnd(), maxdist );
1858
1859 case SHAPE_T::RECTANGLE:
1860 if( IsProxyItem() || IsFilledForHitTesting() ) // Filled rect hit-test
1861 {
1862 SHAPE_POLY_SET poly;
1863 poly.NewOutline();
1864
1865 for( const VECTOR2I& pt : GetRectCorners() )
1866 poly.Append( pt );
1867
1868 return poly.Collide( aPosition, maxdist );
1869 }
1870 else if( m_cornerRadius > 0 )
1871 {
1873 SHAPE_POLY_SET poly;
1874 rr.TransformToPolygon( poly, getMaxError() );
1875
1876 if( poly.CollideEdge( aPosition, nullptr, maxdist ) )
1877 return true;
1878 }
1879 else
1880 {
1881 std::vector<VECTOR2I> pts = GetRectCorners();
1882
1883 if( TestSegmentHit( aPosition, pts[0], pts[1], maxdist )
1884 || TestSegmentHit( aPosition, pts[1], pts[2], maxdist )
1885 || TestSegmentHit( aPosition, pts[2], pts[3], maxdist )
1886 || TestSegmentHit( aPosition, pts[3], pts[0], maxdist ) )
1887 {
1888 return true;
1889 }
1890 }
1891
1892 if( IsHatchedFill() && GetHatching().Collide( aPosition, maxdist ) )
1893 return true;
1894
1895 return false;
1896
1897 case SHAPE_T::POLY:
1898 if( GetPolyShape().OutlineCount() < 1 ) // empty poly
1899 return false;
1900
1901 if( IsFilledForHitTesting() )
1902 {
1903 if( !GetPolyShape().COutline( 0 ).IsClosed() )
1904 {
1905 // Only one outline is expected
1906 SHAPE_LINE_CHAIN copy( GetPolyShape().COutline( 0 ) );
1907 copy.SetClosed( true );
1908 return copy.Collide( aPosition, maxdist );
1909 }
1910 else
1911 {
1912 return GetPolyShape().Collide( aPosition, maxdist );
1913 }
1914 }
1915 else
1916 {
1917 if( GetPolyShape().CollideEdge( aPosition, nullptr, maxdist ) )
1918 return true;
1919
1920 if( IsHatchedFill() && GetHatching().Collide( aPosition, maxdist ) )
1921 return true;
1922
1923 return false;
1924 }
1925
1926 case SHAPE_T::ELLIPSE:
1928 {
1930
1931 const double maxdistSq = maxdist * maxdist;
1932
1934 {
1935 // Filled closed ellipse
1936 if( static_cast<double>( e.SquaredDistance( aPosition, false ) ) <= maxdistSq )
1937 return true;
1938 }
1939 else
1940 {
1941 // Unfilled ring or arc
1942 if( static_cast<double>( e.SquaredDistance( aPosition, true ) ) <= maxdistSq )
1943 return true;
1944 }
1945
1946 if( IsHatchedFill() && GetHatching().Collide( aPosition, maxdist ) )
1947 return true;
1948
1949 return false;
1950 }
1951
1952 default:
1954 return false;
1955 }
1956}
1957
1958
1959bool EDA_SHAPE::hitTest( const BOX2I& aRect, bool aContained, int aAccuracy ) const
1960{
1961 BOX2I rect = aRect;
1962 rect.Normalize();
1963 rect.Inflate( aAccuracy );
1964
1966 {
1969
1970 return KIGEOM::ShapeHitTest( selection, shape, aContained );
1971 }
1972
1973 BOX2I bbox = getBoundingBox();
1974
1975 auto checkOutline =
1976 [&]( const SHAPE_LINE_CHAIN& outline )
1977 {
1978 int count = (int) outline.GetPointCount();
1979
1980 for( int ii = 0; ii < count; ii++ )
1981 {
1982 VECTOR2I vertex = outline.GetPoint( ii );
1983
1984 // Test if the point is within rect
1985 if( rect.Contains( vertex ) )
1986 return true;
1987
1988 if( ii + 1 < count )
1989 {
1990 VECTOR2I vertexNext = outline.GetPoint( ii + 1 );
1991
1992 // Test if this edge intersects rect
1993 if( rect.Intersects( vertex, vertexNext ) )
1994 return true;
1995 }
1996 else if( outline.IsClosed() )
1997 {
1998 VECTOR2I vertexNext = outline.GetPoint( 0 );
1999
2000 // Test if this edge intersects rect
2001 if( rect.Intersects( vertex, vertexNext ) )
2002 return true;
2003 }
2004 }
2005
2006 return false;
2007 };
2008
2009 switch( m_shape )
2010 {
2011 case SHAPE_T::CIRCLE:
2012 // Test if area intersects or contains the circle:
2013 if( aContained )
2014 {
2015 return rect.Contains( bbox );
2016 }
2017 else
2018 {
2019 // If the rectangle does not intersect the bounding box, this is a much quicker test
2020 if( !rect.Intersects( bbox ) )
2021 return false;
2022 else
2023 return rect.IntersectsCircleEdge( getCenter(), GetRadius(), GetWidth() );
2024 }
2025
2026 case SHAPE_T::ARC:
2027 // Test for full containment of this arc in the rect
2028 if( aContained )
2029 {
2030 return rect.Contains( bbox );
2031 }
2032 // Test if the rect crosses the arc
2033 else
2034 {
2035 if( !rect.Intersects( bbox ) )
2036 return false;
2037
2038 if( IsAnyFill() )
2039 {
2040 return ( rect.Intersects( getCenter(), GetStart() )
2041 || rect.Intersects( getCenter(), GetEnd() )
2042 || rect.IntersectsCircleEdge( getCenter(), GetRadius(), GetWidth() ) );
2043 }
2044 else
2045 {
2046 return rect.IntersectsCircleEdge( getCenter(), GetRadius(), GetWidth() );
2047 }
2048 }
2049
2050 case SHAPE_T::RECTANGLE:
2051 if( aContained )
2052 {
2053 return rect.Contains( bbox );
2054 }
2055 else if( m_cornerRadius > 0 )
2056 {
2058 SHAPE_POLY_SET poly;
2059 rr.TransformToPolygon( poly, getMaxError() );
2060
2061 // Account for the width of the line
2062 rect.Inflate( GetWidth() / 2 );
2063
2064 return checkOutline( poly.Outline( 0 ) );
2065 }
2066 else
2067 {
2068 std::vector<VECTOR2I> pts = GetRectCorners();
2069
2070 // Account for the width of the lines
2071 rect.Inflate( GetWidth() / 2 );
2072 return ( rect.Intersects( pts[0], pts[1] )
2073 || rect.Intersects( pts[1], pts[2] )
2074 || rect.Intersects( pts[2], pts[3] )
2075 || rect.Intersects( pts[3], pts[0] ) );
2076 }
2077
2078 case SHAPE_T::SEGMENT:
2079 if( aContained )
2080 {
2081 return rect.Contains( GetStart() ) && rect.Contains( GetEnd() );
2082 }
2083 else
2084 {
2085 // Account for the width of the line
2086 rect.Inflate( GetWidth() / 2 );
2087 return rect.Intersects( GetStart(), GetEnd() );
2088 }
2089
2090 case SHAPE_T::POLY:
2091 if( aContained )
2092 {
2093 return rect.Contains( bbox );
2094 }
2095 else
2096 {
2097 // Fast test: if rect is outside the polygon bounding box,
2098 // rectangles cannot intersect
2099 if( !rect.Intersects( bbox ) )
2100 return false;
2101
2102 // Account for the width of the line
2103 rect.Inflate( GetWidth() / 2 );
2104
2105 for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
2106 {
2107 if( checkOutline( GetPolyShape().Outline( ii ) ) )
2108 return true;
2109 }
2110
2111 return false;
2112 }
2113
2114 case SHAPE_T::BEZIER:
2115 if( aContained )
2116 {
2117 return rect.Contains( bbox );
2118 }
2119 else
2120 {
2121 // Fast test: if rect is outside the polygon bounding box,
2122 // rectangles cannot intersect
2123 if( !rect.Intersects( bbox ) )
2124 return false;
2125
2126 // Account for the width of the line
2127 rect.Inflate( GetWidth() / 2 );
2128 const std::vector<VECTOR2I>* pts = &m_bezierPoints;
2129 std::vector<VECTOR2I> updatedBezierPoints;
2130
2131 if( m_bezierPoints.empty() )
2132 {
2134 converter.GetPoly( updatedBezierPoints, aAccuracy / 2 );
2135 pts = &updatedBezierPoints;
2136 }
2137
2138 for( unsigned ii = 1; ii < pts->size(); ii++ )
2139 {
2140 VECTOR2I vertex = ( *pts )[ii - 1];
2141 VECTOR2I vertexNext = ( *pts )[ii];
2142
2143 // Test if the point is within rect
2144 if( rect.Contains( vertex ) )
2145 return true;
2146
2147 // Test if this edge intersects rect
2148 if( rect.Intersects( vertex, vertexNext ) )
2149 return true;
2150 }
2151
2152 return false;
2153 }
2154
2155 case SHAPE_T::ELLIPSE:
2157 if( aContained )
2158 {
2159 return rect.Contains( bbox );
2160 }
2161 else
2162 {
2163 if( !rect.Intersects( bbox ) )
2164 return false;
2165
2167
2168 const int tessError = std::max( 1, aAccuracy / 2 );
2169 const SHAPE_LINE_CHAIN chain = e.ConvertToPolyline( tessError );
2170
2171 // Account for the width of the line
2172 rect.Inflate( GetWidth() / 2 );
2173 return checkOutline( chain );
2174 }
2175
2176 default:
2178 return false;
2179 }
2180}
2181
2182
2183bool EDA_SHAPE::hitTest( const SHAPE_LINE_CHAIN& aPoly, bool aContained ) const
2184{
2187 : MakeEffectiveShapes() );
2188
2189 return KIGEOM::ShapeHitTest( aPoly, shape, aContained );
2190}
2191
2192
2193std::vector<VECTOR2I> EDA_SHAPE::GetRectCorners() const
2194{
2195 std::vector<VECTOR2I> pts;
2196 VECTOR2I topLeft = GetStart();
2197 VECTOR2I botRight = GetEnd();
2198
2199 pts.emplace_back( topLeft );
2200 pts.emplace_back( botRight.x, topLeft.y );
2201 pts.emplace_back( botRight );
2202 pts.emplace_back( topLeft.x, botRight.y );
2203
2204 return pts;
2205}
2206
2207
2208std::vector<VECTOR2I> EDA_SHAPE::GetCornersInSequence( EDA_ANGLE angle ) const
2209{
2210 std::vector<VECTOR2I> pts;
2211
2212 angle.Normalize();
2213
2214 BOX2I bbox = getBoundingBox();
2215 bbox.Normalize();
2216
2217 if( angle.IsCardinal() )
2218 {
2219 if( angle == ANGLE_0 )
2220 {
2221 pts.emplace_back( VECTOR2I( bbox.GetLeft(), bbox.GetTop() ) );
2222 pts.emplace_back( VECTOR2I( bbox.GetRight(), bbox.GetTop() ) );
2223 pts.emplace_back( VECTOR2I( bbox.GetRight(), bbox.GetBottom() ) );
2224 pts.emplace_back( VECTOR2I( bbox.GetLeft(), bbox.GetBottom() ) );
2225 }
2226 else if( angle == ANGLE_90 )
2227 {
2228 pts.emplace_back( VECTOR2I( bbox.GetLeft(), bbox.GetBottom() ) );
2229 pts.emplace_back( VECTOR2I( bbox.GetLeft(), bbox.GetTop() ) );
2230 pts.emplace_back( VECTOR2I( bbox.GetRight(), bbox.GetTop() ) );
2231 pts.emplace_back( VECTOR2I( bbox.GetRight(), bbox.GetBottom() ) );
2232 }
2233 else if( angle == ANGLE_180 )
2234 {
2235 pts.emplace_back( VECTOR2I( bbox.GetRight(), bbox.GetBottom() ) );
2236 pts.emplace_back( VECTOR2I( bbox.GetLeft(), bbox.GetBottom() ) );
2237 pts.emplace_back( VECTOR2I( bbox.GetLeft(), bbox.GetTop() ) );
2238 pts.emplace_back( VECTOR2I( bbox.GetRight(), bbox.GetTop() ) );
2239 }
2240 else if( angle == ANGLE_270 )
2241 {
2242 pts.emplace_back( VECTOR2I( bbox.GetRight(), bbox.GetTop() ) );
2243 pts.emplace_back( VECTOR2I( bbox.GetRight(), bbox.GetBottom() ) );
2244 pts.emplace_back( VECTOR2I( bbox.GetLeft(), bbox.GetBottom() ) );
2245 pts.emplace_back( VECTOR2I( bbox.GetLeft(), bbox.GetTop() ) );
2246 }
2247 }
2248 else if( m_shape == SHAPE_T::RECTANGLE )
2249 {
2250 // Axis-aligned rectangle with non-cardinal rotation (used by textboxes).
2251 VECTOR2I center = bbox.GetCenter();
2252
2253 VECTOR2I tl( bbox.GetLeft(), bbox.GetTop() );
2254 VECTOR2I tr( bbox.GetRight(), bbox.GetTop() );
2255 VECTOR2I br( bbox.GetRight(), bbox.GetBottom() );
2256 VECTOR2I bl( bbox.GetLeft(), bbox.GetBottom() );
2257
2258 RotatePoint( tl, center, angle );
2259 RotatePoint( tr, center, angle );
2260 RotatePoint( br, center, angle );
2261 RotatePoint( bl, center, angle );
2262
2263 pts.emplace_back( tl );
2264 pts.emplace_back( tr );
2265 pts.emplace_back( br );
2266 pts.emplace_back( bl );
2267 }
2268 else
2269 {
2270 // This function was originally located in pcb_textbox.cpp and was later moved to eda_shape.cpp.
2271 // As a result of this move, access to getCorners was lost, since it is defined in the PCB_SHAPE
2272 // class within pcb_shape.cpp and is not available in the current context.
2273 //
2274 // Additionally, GetRectCorners() cannot be used here, as it assumes the rectangle is rotated by
2275 // a cardinal angle. In non-cardinal cases, it returns incorrect values (e.g., (0, 0)).
2276 //
2277 // To address this, a portion of the getCorners implementation for SHAPE_T::POLY elements
2278 // has been replicated here to restore the correct behavior.
2279 std::vector<VECTOR2I> corners;
2280
2281 for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
2282 {
2283 for( const VECTOR2I& pt : GetPolyShape().Outline( ii ).CPoints() )
2284 corners.emplace_back( pt );
2285 }
2286
2287 if( corners.empty() )
2288 return pts;
2289
2290 while( corners.size() < 4 )
2291 corners.emplace_back( corners.back() + VECTOR2I( 10, 10 ) );
2292
2293 VECTOR2I minX = corners[0];
2294 VECTOR2I maxX = corners[0];
2295 VECTOR2I minY = corners[0];
2296 VECTOR2I maxY = corners[0];
2297
2298 for( const VECTOR2I& corner : corners )
2299 {
2300 if( corner.x < minX.x )
2301 minX = corner;
2302
2303 if( corner.x > maxX.x )
2304 maxX = corner;
2305
2306 if( corner.y < minY.y )
2307 minY = corner;
2308
2309 if( corner.y > maxY.y )
2310 maxY = corner;
2311 }
2312
2313 if( angle < ANGLE_90 )
2314 {
2315 pts.emplace_back( minX );
2316 pts.emplace_back( minY );
2317 pts.emplace_back( maxX );
2318 pts.emplace_back( maxY );
2319 }
2320 else if( angle < ANGLE_180 )
2321 {
2322 pts.emplace_back( maxY );
2323 pts.emplace_back( minX );
2324 pts.emplace_back( minY );
2325 pts.emplace_back( maxX );
2326 }
2327 else if( angle < ANGLE_270 )
2328 {
2329 pts.emplace_back( maxX );
2330 pts.emplace_back( maxY );
2331 pts.emplace_back( minX );
2332 pts.emplace_back( minY );
2333 }
2334 else
2335 {
2336 pts.emplace_back( minY );
2337 pts.emplace_back( maxX );
2338 pts.emplace_back( maxY );
2339 pts.emplace_back( minX );
2340 }
2341 }
2342
2343 return pts;
2344}
2345
2346
2348{
2349 // Start, end, and each inflection point the arc crosses will enclose the entire arc.
2350 // Only include the center when filled; it's not necessarily inside the BB of an unfilled
2351 // arc with a small included angle.
2352 aBBox.SetOrigin( m_start );
2353 aBBox.Merge( m_end );
2354
2355 if( IsAnyFill() )
2356 aBBox.Merge( m_arcCenter );
2357
2358 int radius = GetRadius();
2359 EDA_ANGLE t1, t2;
2360
2361 CalcArcAngles( t1, t2 );
2362
2363 t1.Normalize();
2364 t2.Normalize();
2365
2366 if( t2 > t1 )
2367 {
2368 if( t1 < ANGLE_0 && t2 > ANGLE_0 )
2369 aBBox.Merge( VECTOR2I( m_arcCenter.x + radius, m_arcCenter.y ) ); // right
2370
2371 if( t1 < ANGLE_90 && t2 > ANGLE_90 )
2372 aBBox.Merge( VECTOR2I( m_arcCenter.x, m_arcCenter.y + radius ) ); // down
2373
2374 if( t1 < ANGLE_180 && t2 > ANGLE_180 )
2375 aBBox.Merge( VECTOR2I( m_arcCenter.x - radius, m_arcCenter.y ) ); // left
2376
2377 if( t1 < ANGLE_270 && t2 > ANGLE_270 )
2378 aBBox.Merge( VECTOR2I( m_arcCenter.x, m_arcCenter.y - radius ) ); // up
2379 }
2380 else
2381 {
2382 if( t1 < ANGLE_0 || t2 > ANGLE_0 )
2383 aBBox.Merge( VECTOR2I( m_arcCenter.x + radius, m_arcCenter.y ) ); // right
2384
2385 if( t1 < ANGLE_90 || t2 > ANGLE_90 )
2386 aBBox.Merge( VECTOR2I( m_arcCenter.x, m_arcCenter.y + radius ) ); // down
2387
2388 if( t1 < ANGLE_180 || t2 > ANGLE_180 )
2389 aBBox.Merge( VECTOR2I( m_arcCenter.x - radius, m_arcCenter.y ) ); // left
2390
2391 if( t1 < ANGLE_270 || t2 > ANGLE_270 )
2392 aBBox.Merge( VECTOR2I( m_arcCenter.x, m_arcCenter.y - radius ) ); // up
2393 }
2394}
2395
2396
2397void EDA_SHAPE::SetPolyPoints( const std::vector<VECTOR2I>& aPoints )
2398{
2401
2402 for( const VECTOR2I& p : aPoints )
2403 GetPolyShape().Append( p.x, p.y );
2404
2405 m_hatchingDirty = true;
2406}
2407
2408
2409std::vector<SHAPE*> EDA_SHAPE::MakeEffectiveShapesForStroking( int aLineWidth ) const
2410{
2411 if( aLineWidth < 0 )
2412 aLineWidth = GetEffectiveWidth();
2413
2414 if( m_startEnding.GetShortenDepth( aLineWidth ) > 0 || m_endEnding.GetShortenDepth( aLineWidth ) > 0 )
2415 {
2416 switch( m_shape )
2417 {
2418 // Stroke() has no Bezier primitive, so it gets the flattened polyline. One chain,
2419 // not loose segments, or the pattern restarts at every vertex (same reason as the
2420 // unshortened case below).
2421 case SHAPE_T::BEZIER:
2422 {
2423 std::vector<VECTOR2I> pts;
2424
2425 for( const VECTOR2D& pt : ShortenedBezierPolyline( aLineWidth ) )
2426 pts.emplace_back( VECTOR2I( pt ) );
2427
2428 return { new SHAPE_LINE_CHAIN( pts ) };
2429 }
2430
2431 case SHAPE_T::SEGMENT:
2432 case SHAPE_T::ARC:
2433 case SHAPE_T::POLY:
2435 return makeShortenedBodyShapes( aLineWidth, true );
2436
2437 // Other shapes have no body shortening; fall through to the standard cases.
2438 default:
2439 break;
2440 }
2441 }
2442
2443 switch( m_shape )
2444 {
2445 // Stroke() has no Bezier primitive, so it gets the flattened polyline. One chain, not
2446 // loose segments, or the pattern restarts at every vertex. This case goes away if
2447 // Bezier ever becomes a SHAPE of its own.
2448 case SHAPE_T::BEZIER:
2450
2451 case SHAPE_T::ELLIPSE:
2453 return { new SHAPE_ELLIPSE( buildShapeEllipse() ) };
2454
2455 default:
2456 return MakeEffectiveShapes( true );
2457 }
2458}
2459
2460
2461std::vector<SHAPE*> EDA_SHAPE::makeEffectiveShapes( bool aEdgeOnly, bool aLineChainOnly, bool aHittesting ) const
2462{
2463 std::vector<SHAPE*> effectiveShapes;
2464 int width = GetEffectiveWidth();
2465 bool solidFill = IsSolidFill()
2466 || IsHatchedFill()
2467 || IsProxyItem()
2468 || ( aHittesting && IsFilledForHitTesting() );
2469
2470 if( aEdgeOnly )
2471 solidFill = false;
2472
2473 switch( m_shape )
2474 {
2475 case SHAPE_T::ARC:
2476 effectiveShapes.emplace_back( new SHAPE_ARC( m_arcCenter, m_start, GetArcAngle(), width ) );
2477 break;
2478
2479 case SHAPE_T::SEGMENT:
2480 effectiveShapes.emplace_back( new SHAPE_SEGMENT( m_start, m_end, width ) );
2481 break;
2482
2483 case SHAPE_T::RECTANGLE:
2484 {
2485 if( m_cornerRadius > 0 )
2486 {
2488 SHAPE_POLY_SET poly;
2489 rr.TransformToPolygon( poly, getMaxError() );
2490 SHAPE_LINE_CHAIN outline = poly.Outline( 0 );
2491
2492 if( solidFill )
2493 effectiveShapes.emplace_back( new SHAPE_SIMPLE( outline ) );
2494
2495 if( width > 0 || !solidFill )
2496 {
2497 std::set<size_t> arcsHandled;
2498
2499 for( int ii = 0; ii < outline.SegmentCount(); ++ii )
2500 {
2501 if( outline.IsArcSegment( ii ) )
2502 {
2503 size_t arcIndex = outline.ArcIndex( ii );
2504
2505 if( !arcsHandled.contains( arcIndex ) )
2506 {
2507 arcsHandled.insert( arcIndex );
2508 effectiveShapes.emplace_back( new SHAPE_ARC( outline.Arc( arcIndex ), width ) );
2509 }
2510 }
2511 else
2512 {
2513 effectiveShapes.emplace_back( new SHAPE_SEGMENT( outline.Segment( ii ), width ) );
2514 }
2515 }
2516 }
2517 }
2518 else
2519 {
2520 std::vector<VECTOR2I> pts = GetRectCorners();
2521
2522 if( solidFill )
2523 effectiveShapes.emplace_back( new SHAPE_SIMPLE( pts ) );
2524
2525 if( width > 0 || !solidFill )
2526 {
2527 effectiveShapes.emplace_back( new SHAPE_SEGMENT( pts[0], pts[1], width ) );
2528 effectiveShapes.emplace_back( new SHAPE_SEGMENT( pts[1], pts[2], width ) );
2529 effectiveShapes.emplace_back( new SHAPE_SEGMENT( pts[2], pts[3], width ) );
2530 effectiveShapes.emplace_back( new SHAPE_SEGMENT( pts[3], pts[0], width ) );
2531 }
2532 }
2533 break;
2534 }
2535
2536 case SHAPE_T::CIRCLE:
2537 {
2538 if( solidFill )
2539 effectiveShapes.emplace_back( new SHAPE_CIRCLE( getCenter(), GetRadius() ) );
2540
2541 if( width > 0 || !solidFill )
2542 effectiveShapes.emplace_back( new SHAPE_ARC( getCenter(), GetEnd(), ANGLE_360, width ) );
2543
2544 break;
2545 }
2546
2547 case SHAPE_T::BEZIER:
2548 {
2549 std::vector<VECTOR2I> bezierPoints = buildBezierToSegmentsPointsList( getMaxError() );
2550 VECTOR2I start_pt = bezierPoints[0];
2551
2552 for( unsigned int jj = 1; jj < bezierPoints.size(); jj++ )
2553 {
2554 VECTOR2I end_pt = bezierPoints[jj];
2555 effectiveShapes.emplace_back( new SHAPE_SEGMENT( start_pt, end_pt, width ) );
2556 start_pt = end_pt;
2557 }
2558
2559 break;
2560 }
2561
2562 case SHAPE_T::POLY:
2563 if( GetPolyShape().OutlineCount() == 0 ) // malformed/empty polygon
2564 break;
2565
2566 for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
2567 {
2568 const SHAPE_LINE_CHAIN& l = GetPolyShape().COutline( ii );
2569
2570 if( solidFill )
2571 effectiveShapes.emplace_back( new SHAPE_SIMPLE( l ) );
2572
2573 if( width > 0 || !IsSolidFill() || aEdgeOnly )
2574 {
2575 int segCount = l.SegmentCount();
2576
2577 if( aLineChainOnly && l.IsClosed() )
2578 segCount--; // Treat closed chain as open
2579
2580 for( int jj = 0; jj < segCount; jj++ )
2581 effectiveShapes.emplace_back( new SHAPE_SEGMENT( l.CSegment( jj ), width ) );
2582 }
2583 }
2584
2585 break;
2586
2587 case SHAPE_T::ELLIPSE:
2589 {
2590 if( solidFill && m_shape == SHAPE_T::ELLIPSE )
2591 {
2592 // Filled closed ellipse: emit a SHAPE_SIMPLE for the filled interior.
2595 std::vector<VECTOR2I> pts;
2596
2597 for( int ii = 0; ii < chain.PointCount(); ++ii )
2598 pts.emplace_back( chain.CPoint( ii ) );
2599
2600 effectiveShapes.emplace_back( new SHAPE_SIMPLE( pts ) );
2601 }
2602
2603 if( width > 0 || !solidFill )
2604 {
2607
2608 for( int ii = 0; ii < chain.SegmentCount(); ++ii )
2609 effectiveShapes.emplace_back( new SHAPE_SEGMENT( chain.CSegment( ii ), width ) );
2610 }
2611
2612 break;
2613 }
2614
2615 default:
2617 break;
2618 }
2619
2620 return effectiveShapes;
2621}
2622
2623
2624static SHAPE_LINE_CHAIN lineEndingClosedChain( const std::vector<VECTOR2I>& aPolygon )
2625{
2627
2628 for( size_t ii = 0; ii < aPolygon.size(); ++ii )
2629 {
2630 if( ii == aPolygon.size() - 1 && aPolygon[ii] == aPolygon.front() )
2631 continue;
2632
2633 chain.Append( aPolygon[ii] );
2634 }
2635
2636 chain.SetClosed( true );
2637 return chain;
2638}
2639
2640
2641static bool hasLineEnding( const LINE_ENDING& aStartEnding, const LINE_ENDING& aEndEnding )
2642{
2643 return aStartEnding.GetStyle() != LINE_ENDING_STYLE::NONE || aEndEnding.GetStyle() != LINE_ENDING_STYLE::NONE;
2644}
2645
2646
2647static void addLineEndingEffectiveShapes( std::vector<SHAPE*>& aShapes, const LINE_ENDING& aEnding,
2648 const VECTOR2I& aPoint, const EDA_ANGLE& aTangent, int aLineWidth )
2649{
2650 if( aEnding.GetStyle() == LINE_ENDING_STYLE::NONE )
2651 return;
2652
2653 std::vector<VECTOR2I> polygon;
2654 aEnding.GetShapes( aPoint, aTangent, aLineWidth, polygon );
2655
2656 if( polygon.empty() )
2657 return;
2658
2659 if( aEnding.GetStyle() == LINE_ENDING_STYLE::ARROW_OPEN )
2660 {
2661 int width = aEnding.GetStrokeWidth() > 0 ? aEnding.GetStrokeWidth() : aLineWidth;
2662
2663 if( polygon.size() >= 3 )
2664 {
2665 aShapes.emplace_back( new SHAPE_SEGMENT( polygon[0], polygon[1], std::max( 0, width ) ) );
2666 aShapes.emplace_back( new SHAPE_SEGMENT( polygon[1], polygon[2], std::max( 0, width ) ) );
2667 }
2668
2669 return;
2670 }
2671
2672 if( polygon.size() < 3 )
2673 return;
2674
2676
2677 if( outline.PointCount() >= 3 )
2678 aShapes.emplace_back( new SHAPE_SIMPLE( outline ) );
2679
2680 if( aEnding.GetStrokeWidth() > 0 )
2681 {
2682 for( int ii = 0; ii < outline.SegmentCount(); ++ii )
2683 aShapes.emplace_back( new SHAPE_SEGMENT( outline.CSegment( ii ), aEnding.GetStrokeWidth() ) );
2684 }
2685}
2686
2687
2688std::vector<SHAPE*> EDA_SHAPE::MakeLineEndingEffectiveShapes( int aLineWidth ) const
2689{
2690 std::vector<SHAPE*> effectiveShapes;
2691 VECTOR2I startPoint;
2692 VECTOR2I endPoint;
2693
2695 return effectiveShapes;
2696
2697 if( !GetLineEndingEndpoints( startPoint, endPoint ) )
2698 return effectiveShapes;
2699
2700 EDA_ANGLE startTangent;
2701 EDA_ANGLE endTangent;
2702
2703 GetEndingTangents( startTangent, endTangent, aLineWidth );
2704
2705 addLineEndingEffectiveShapes( effectiveShapes, m_startEnding, startPoint, startTangent, aLineWidth );
2706 addLineEndingEffectiveShapes( effectiveShapes, m_endEnding, endPoint, endTangent, aLineWidth );
2707
2708 return effectiveShapes;
2709}
2710
2711
2712std::vector<SHAPE*> EDA_SHAPE::makeShortenedBodyShapes( int aLineWidth, bool aEdgeOnly ) const
2713{
2714 std::vector<SHAPE*> effectiveShapes;
2715 bool shortenBody = m_startEnding.GetShortenDepth( aLineWidth ) > 0 || m_endEnding.GetShortenDepth( aLineWidth ) > 0;
2716
2717 if( !shortenBody )
2718 {
2719 effectiveShapes = makeEffectiveShapes( aEdgeOnly );
2720 }
2721 else
2722 {
2723 int width = GetEffectiveWidth();
2724
2725 switch( m_shape )
2726 {
2727 case SHAPE_T::SEGMENT:
2728 {
2729 VECTOR2I start = GetStart();
2730 VECTOR2I end = GetEnd();
2731
2732 if( ShortenSegmentForEndings( start, end, m_startEnding, m_endEnding, aLineWidth ) )
2733 effectiveShapes.emplace_back( new SHAPE_SEGMENT( start, end, width ) );
2734
2735 break;
2736 }
2737
2738 case SHAPE_T::ARC:
2739 {
2740 EDA_ANGLE startAngle;
2741 EDA_ANGLE endAngle;
2742 CalcArcAngles( startAngle, endAngle );
2743
2744 EDA_ANGLE originalStartAngle = startAngle;
2745 EDA_ANGLE arcAngle = endAngle - startAngle;
2746
2747 if( ShortenArcForEndings( startAngle, arcAngle, GetRadius(), aLineWidth ) )
2748 {
2749 VECTOR2I startPoint = GetStart();
2750 RotatePoint( startPoint, m_arcCenter, -( startAngle - originalStartAngle ) );
2751 effectiveShapes.emplace_back( new SHAPE_ARC( m_arcCenter, startPoint, arcAngle, width ) );
2752 }
2753
2754 break;
2755 }
2756
2757 case SHAPE_T::BEZIER:
2758 {
2759 std::vector<VECTOR2D> pts = ShortenedBezierPolyline( aLineWidth );
2760
2761 for( size_t ii = 1; ii < pts.size(); ++ii )
2762 {
2763 effectiveShapes.emplace_back( new SHAPE_SEGMENT( VECTOR2I( pts[ii - 1] ), VECTOR2I( pts[ii] ),
2764 width ) );
2765 }
2766
2767 break;
2768 }
2769
2770 case SHAPE_T::POLY:
2771 {
2772 if( ( !aEdgeOnly && ( IsSolidFill() || IsHatchedFill() || IsProxyItem() ) )
2773 || GetPolyShape().OutlineCount() == 0 )
2774 {
2775 effectiveShapes = makeEffectiveShapes( aEdgeOnly );
2776 break;
2777 }
2778
2779 for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
2780 {
2781 const SHAPE_LINE_CHAIN& outline = GetPolyShape().COutline( ii );
2782
2783 if( outline.PointCount() < 2 )
2784 continue;
2785
2786 std::vector<VECTOR2I> pts;
2787
2788 if( GetShortenedBodyPolyPoints( outline, ii, pts, aLineWidth ) )
2789 {
2790 for( size_t jj = 1; jj < pts.size(); ++jj )
2791 effectiveShapes.emplace_back( new SHAPE_SEGMENT( pts[jj - 1], pts[jj], width ) );
2792
2793 if( outline.IsClosed() )
2794 effectiveShapes.emplace_back( new SHAPE_SEGMENT( pts.back(), pts.front(), width ) );
2795 }
2796 }
2797
2798 break;
2799 }
2800
2802 {
2804 std::vector<VECTOR2I> pts;
2805
2806 if( GetShortenedBodyPolyPoints( chain, 0, pts, width ) )
2807 {
2808 for( size_t jj = 1; jj < pts.size(); ++jj )
2809 effectiveShapes.emplace_back( new SHAPE_SEGMENT( pts[jj - 1], pts[jj], width ) );
2810 }
2811
2812 break;
2813 }
2814
2815 default:
2816 effectiveShapes = makeEffectiveShapes( aEdgeOnly );
2817 break;
2818 }
2819 }
2820
2821 return effectiveShapes;
2822}
2823
2824
2825std::vector<SHAPE*> EDA_SHAPE::MakeEffectiveShapesWithLineEndings( int aLineWidth ) const
2826{
2827 std::vector<SHAPE*> effectiveShapes = makeShortenedBodyShapes( aLineWidth );
2828
2829 for( SHAPE* shape : MakeLineEndingEffectiveShapes( aLineWidth ) )
2830 effectiveShapes.emplace_back( shape );
2831
2832 return effectiveShapes;
2833}
2834
2835
2836static void addLineEndingPolygon( SHAPE_POLY_SET& aBuffer, const LINE_ENDING& aEnding, const VECTOR2I& aPoint,
2837 const EDA_ANGLE& aTangent, int aClearance, int aError, ERROR_LOC aErrorLoc,
2838 int aLineWidth )
2839{
2840 if( aEnding.GetStyle() == LINE_ENDING_STYLE::NONE )
2841 return;
2842
2843 std::vector<VECTOR2I> polygon;
2844 aEnding.GetShapes( aPoint, aTangent, aLineWidth, polygon );
2845
2846 if( polygon.empty() )
2847 return;
2848
2849 auto addStrokedSegment = [&]( const VECTOR2I& aStart, const VECTOR2I& aEnd, int aStrokeWidth )
2850 {
2851 int width = std::max( 0, aStrokeWidth ) + 2 * aClearance;
2852
2853 if( width > 0 )
2854 TransformOvalToPolygon( aBuffer, aStart, aEnd, width, aError, aErrorLoc );
2855 };
2856
2857 if( aEnding.GetStyle() == LINE_ENDING_STYLE::ARROW_OPEN )
2858 {
2859 int strokeWidth = aEnding.GetStrokeWidth() > 0 ? aEnding.GetStrokeWidth() : aLineWidth;
2860
2861 if( polygon.size() >= 3 )
2862 {
2863 addStrokedSegment( polygon[0], polygon[1], strokeWidth );
2864 addStrokedSegment( polygon[1], polygon[2], strokeWidth );
2865 }
2866
2867 return;
2868 }
2869
2870 if( polygon.size() < 3 )
2871 return;
2872
2874
2875 if( outline.PointCount() >= 3 )
2876 {
2877 SHAPE_POLY_SET fill;
2878 fill.NewOutline();
2879
2880 for( int ii = 0; ii < outline.PointCount(); ++ii )
2881 fill.Append( outline.CPoint( ii ) );
2882
2883 if( aClearance > 0 )
2884 {
2885 int inflate = aClearance;
2886
2887 if( aErrorLoc == ERROR_OUTSIDE )
2888 inflate += aError;
2889
2890 fill.Inflate( inflate, CORNER_STRATEGY::ROUND_ALL_CORNERS, aError );
2891 }
2892
2893 aBuffer.Append( fill );
2894 }
2895
2896 if( aEnding.GetStrokeWidth() > 0 )
2897 {
2898 for( int ii = 0; ii < outline.SegmentCount(); ++ii )
2899 {
2900 const SEG& seg = outline.CSegment( ii );
2901 addStrokedSegment( seg.A, seg.B, aEnding.GetStrokeWidth() );
2902 }
2903 }
2904}
2905
2906
2907void EDA_SHAPE::TransformLineEndingsToPolygon( SHAPE_POLY_SET& aBuffer, int aClearance, int aError, ERROR_LOC aErrorLoc,
2908 int aLineWidth ) const
2909{
2910 VECTOR2I startPoint;
2911 VECTOR2I endPoint;
2912
2914 return;
2915
2916 if( !GetLineEndingEndpoints( startPoint, endPoint ) )
2917 return;
2918
2919 EDA_ANGLE startTangent;
2920 EDA_ANGLE endTangent;
2921
2922 GetEndingTangents( startTangent, endTangent, aLineWidth );
2923
2924 addLineEndingPolygon( aBuffer, m_startEnding, startPoint, startTangent, aClearance, aError, aErrorLoc, aLineWidth );
2925 addLineEndingPolygon( aBuffer, m_endEnding, endPoint, endTangent, aClearance, aError, aErrorLoc, aLineWidth );
2926}
2927
2928
2929void EDA_SHAPE::TransformWithLineEndingsToPolygon( SHAPE_POLY_SET& aBuffer, int aClearance, int aError,
2930 ERROR_LOC aErrorLoc, bool ignoreLineWidth ) const
2931{
2932 int lineWidth = ignoreLineWidth ? 0 : GetEffectiveWidth();
2933 bool shortenBody = m_startEnding.GetShortenDepth( lineWidth ) > 0 || m_endEnding.GetShortenDepth( lineWidth ) > 0;
2934
2935 if( !shortenBody )
2936 {
2937 TransformShapeToPolygon( aBuffer, aClearance, aError, aErrorLoc, ignoreLineWidth );
2938 }
2939 else
2940 {
2941 int width = ignoreLineWidth ? 0 : GetWidth();
2942 width += 2 * aClearance;
2943
2944 switch( m_shape )
2945 {
2946 case SHAPE_T::SEGMENT:
2947 {
2948 VECTOR2I start = GetStart();
2949 VECTOR2I end = GetEnd();
2950
2951 if( ShortenSegmentForEndings( start, end, m_startEnding, m_endEnding, lineWidth ) )
2952 TransformOvalToPolygon( aBuffer, start, end, width, aError, aErrorLoc );
2953
2954 break;
2955 }
2956
2957 case SHAPE_T::ARC:
2958 {
2959 EDA_ANGLE startAngle;
2960 EDA_ANGLE endAngle;
2961 CalcArcAngles( startAngle, endAngle );
2962
2963 EDA_ANGLE originalStartAngle = startAngle;
2964 EDA_ANGLE arcAngle = endAngle - startAngle;
2965
2966 if( ShortenArcForEndings( startAngle, arcAngle, GetRadius(), lineWidth ) )
2967 {
2968 VECTOR2I startPoint = GetStart();
2969 RotatePoint( startPoint, m_arcCenter, -( startAngle - originalStartAngle ) );
2970 SHAPE_ARC arc( m_arcCenter, startPoint, arcAngle, width );
2971
2972 TransformArcToPolygon( aBuffer, arc.GetP0(), arc.GetArcMid(), arc.GetP1(), width, aError, aErrorLoc );
2973 }
2974
2975 break;
2976 }
2977
2978 case SHAPE_T::BEZIER:
2979 {
2980 std::vector<VECTOR2D> pts = ShortenedBezierPolyline( lineWidth );
2981
2982 for( size_t ii = 1; ii < pts.size(); ++ii )
2983 {
2984 TransformOvalToPolygon( aBuffer, VECTOR2I( pts[ii - 1] ), VECTOR2I( pts[ii] ), width, aError,
2985 aErrorLoc );
2986 }
2987
2988 break;
2989 }
2990
2991 case SHAPE_T::POLY:
2992 {
2993 bool solidFill = IsSolidFill() || IsHatchedFill() || IsProxyItem();
2994
2995 if( solidFill || !IsPolyShapeValid() )
2996 {
2997 TransformShapeToPolygon( aBuffer, aClearance, aError, aErrorLoc, ignoreLineWidth );
2998 break;
2999 }
3000
3001 for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
3002 {
3003 const SHAPE_LINE_CHAIN& outline = GetPolyShape().COutline( ii );
3004
3005 if( outline.PointCount() < 2 )
3006 continue;
3007
3008 std::vector<VECTOR2I> pts;
3009
3010 if( !GetShortenedBodyPolyPoints( outline, ii, pts, lineWidth ) )
3011 continue;
3012
3013 for( size_t jj = 1; jj < pts.size(); ++jj )
3014 {
3015 TransformOvalToPolygon( aBuffer, pts[jj - 1], pts[jj], width, aError, aErrorLoc );
3016 }
3017
3018 if( outline.IsClosed() )
3019 {
3020 TransformOvalToPolygon( aBuffer, pts.back(), pts.front(), width, aError, aErrorLoc );
3021 }
3022 }
3023
3024 break;
3025 }
3026
3027 case SHAPE_T::ELLIPSE:
3029 {
3031 std::vector<VECTOR2I> pts;
3032
3033 if( GetShortenedBodyPolyPoints( chain, 0, pts, width ) )
3034 {
3035 for( size_t jj = 1; jj < pts.size(); ++jj )
3036 TransformOvalToPolygon( aBuffer, pts[jj - 1], pts[jj], width, aError, aErrorLoc );
3037
3038 if( chain.IsClosed() )
3039 TransformOvalToPolygon( aBuffer, pts.back(), pts.front(), width, aError, aErrorLoc );
3040 }
3041
3042 break;
3043 }
3044
3045 default:
3046 TransformShapeToPolygon( aBuffer, aClearance, aError, aErrorLoc, ignoreLineWidth );
3047 break;
3048 }
3049 }
3050
3051 TransformLineEndingsToPolygon( aBuffer, aClearance, aError, aErrorLoc, lineWidth );
3052}
3053
3054
3055bool EDA_SHAPE::GetLineEndingsBoundingBox( BOX2I& aBBox, int aLineWidth ) const
3056{
3057 auto mergeEnding =
3058 [&]( const LINE_ENDING& aEnding, const VECTOR2I& aPoint, const EDA_ANGLE& aTangent, bool& aHasBox )
3059 {
3060 if( aEnding.GetStyle() == LINE_ENDING_STYLE::NONE )
3061 return;
3062
3063 std::vector<VECTOR2I> polygon;
3064 aEnding.GetShapes( aPoint, aTangent, aLineWidth, polygon );
3065
3066 if( polygon.empty() )
3067 return;
3068
3069 BOX2I endingBBox( polygon.front(), VECTOR2I( 0, 0 ) );
3070
3071 for( const VECTOR2I& point : polygon )
3072 endingBBox.Merge( point );
3073
3074 int stroke = aEnding.GetStrokeWidth();
3075
3076 if( aEnding.GetStyle() == LINE_ENDING_STYLE::ARROW_OPEN && stroke <= 0 )
3077 stroke = aLineWidth;
3078
3079 endingBBox.Inflate( std::max( 0, stroke ) / 2 );
3080
3081 if( !aHasBox )
3082 {
3083 aBBox = endingBBox;
3084 aHasBox = true;
3085 }
3086 else
3087 {
3088 aBBox.Merge( endingBBox );
3089 }
3090 };
3091
3092 VECTOR2I startPoint;
3093 VECTOR2I endPoint;
3094
3096 return false;
3097
3098 if( !GetLineEndingEndpoints( startPoint, endPoint ) )
3099 return false;
3100
3101 bool hasBox = false;
3102 EDA_ANGLE startTangent;
3103 EDA_ANGLE endTangent;
3104
3105 GetEndingTangents( startTangent, endTangent, aLineWidth );
3106
3107 mergeEnding( m_startEnding, startPoint, startTangent, hasBox );
3108 mergeEnding( m_endEnding, endPoint, endTangent, hasBox );
3109
3110 if( hasBox )
3111 {
3112 aBBox.Normalize();
3113 }
3114
3115 return hasBox;
3116}
3117
3118
3119std::vector<VECTOR2I> EDA_SHAPE::GetPolyPoints() const
3120{
3121 const SHAPE_POLY_SET& poly = GetPolyShape();
3122 std::vector<VECTOR2I> points;
3123 int totalCount = 0;
3124
3125 for( int ii = 0; ii < poly.OutlineCount(); ++ii )
3126 totalCount += poly.COutline( ii ).PointCount();
3127
3128 points.reserve( totalCount );
3129
3130 for( int ii = 0; ii < poly.OutlineCount(); ++ii )
3131 {
3132 for( const VECTOR2I& pt : poly.COutline( ii ).CPoints() )
3133 points.emplace_back( pt );
3134 }
3135
3136 return points;
3137}
3138
3139
3141{
3142 if( !m_poly )
3143 m_poly = std::make_unique<SHAPE_POLY_SET>();
3144
3145 return *m_poly;
3146}
3147
3149{
3150 if( !m_poly )
3151 m_poly = std::make_unique<SHAPE_POLY_SET>();
3152
3153 return *m_poly;
3154}
3155
3156
3158{
3159 // return true if the polygonal shape is valid (has more than 2 points)
3160 return GetPolyShape().OutlineCount() > 0 && GetPolyShape().Outline( 0 ).PointCount() > 2;
3161}
3162
3163
3165{
3166 // return the number of corners of the polygonal shape
3167 // this shape is expected to be only one polygon without hole
3168 return GetPolyShape().OutlineCount() ? GetPolyShape().VertexCount( 0 ) : 0;
3169}
3170
3171
3172void EDA_SHAPE::beginEdit( const VECTOR2I& aPosition )
3173{
3174 switch( GetShape() )
3175 {
3176 case SHAPE_T::SEGMENT:
3177 case SHAPE_T::CIRCLE:
3178 case SHAPE_T::RECTANGLE:
3179 SetStart( aPosition );
3180 SetEnd( aPosition );
3181 break;
3182
3183 case SHAPE_T::ARC:
3184 SetArcGeometry( aPosition, aPosition, aPosition );
3185 m_editState = 1;
3186 break;
3187
3188 case SHAPE_T::BEZIER:
3189 SetStart( aPosition );
3190 SetEnd( aPosition );
3191 SetBezierC1( aPosition );
3192 SetBezierC2( aPosition );
3193 m_editState = 1;
3194
3196 break;
3197
3198 case SHAPE_T::POLY:
3200 GetPolyShape().Outline( 0 ).SetClosed( false );
3201
3202 // Start and end of the first segment (co-located for now)
3203 GetPolyShape().Outline( 0 ).Append( aPosition );
3204 GetPolyShape().Outline( 0 ).Append( aPosition, true );
3205 break;
3206
3207 case SHAPE_T::ELLIPSE:
3208 // m_start holds the first bbox corner and calcEdit derives the ellipse from it.
3209 m_editState = 1;
3210 SetStart( aPosition );
3211 SetEnd( aPosition );
3212 SetEllipseCenter( aPosition );
3216 break;
3217
3219 // State 1: drag bbox. States 2-3: pick start then end angle.
3220 SetStart( aPosition );
3221 SetEnd( aPosition );
3222 SetEllipseCenter( aPosition );
3228 m_editState = 1;
3229 break;
3230
3231 default: UNIMPLEMENTED_FOR( SHAPE_T_asString() );
3232 }
3233}
3234
3235
3236bool EDA_SHAPE::continueEdit( const VECTOR2I& aPosition )
3237{
3238 switch( GetShape() )
3239 {
3240 case SHAPE_T::ARC:
3241 case SHAPE_T::SEGMENT:
3242 case SHAPE_T::CIRCLE:
3243 case SHAPE_T::RECTANGLE:
3244 case SHAPE_T::ELLIPSE:
3245 return false;
3246
3247 case SHAPE_T::BEZIER:
3248 if( m_editState == 3 )
3249 return false;
3250
3251 m_editState++;
3252 return true;
3253
3255 if( m_editState == 3 )
3256 return false;
3257
3258 m_editState++;
3259 return true;
3260
3261 case SHAPE_T::POLY:
3262 {
3263 SHAPE_LINE_CHAIN& poly = GetPolyShape().Outline( 0 );
3264
3265 // do not add zero-length segments
3266 if( poly.CPoint( (int) poly.GetPointCount() - 2 ) != poly.CLastPoint() )
3267 poly.Append( aPosition, true );
3268 }
3269 return true;
3270
3271 default: UNIMPLEMENTED_FOR( SHAPE_T_asString() ); return false;
3272 }
3273}
3274
3275
3276void EDA_SHAPE::calcEdit( const VECTOR2I& aPosition )
3277{
3278#define sq( x ) pow( x, 2 )
3279
3280 switch( GetShape() )
3281 {
3282 case SHAPE_T::SEGMENT:
3283 case SHAPE_T::CIRCLE:
3284 case SHAPE_T::RECTANGLE:
3285 SetEnd( aPosition );
3286 break;
3287
3288 case SHAPE_T::BEZIER:
3289 {
3290 switch( m_editState )
3291 {
3292 case 0:
3293 SetStart( aPosition );
3294 SetEnd( aPosition );
3295 SetBezierC1( aPosition );
3296 SetBezierC2( aPosition );
3297 break;
3298
3299 case 1:
3300 SetBezierC2( aPosition );
3301 SetEnd( aPosition );
3302 break;
3303
3304 case 2: SetBezierC1( aPosition );
3305 break;
3306
3307 case 3: SetBezierC2( aPosition );
3308 break;
3309 }
3310
3312 }
3313 break;
3314
3315 case SHAPE_T::ARC:
3316 {
3317 double radius = GetRadius();
3318 EDA_ANGLE lastAngle = GetArcAngle();
3319
3320 // Edit state 0: drawing: place start
3321 // Edit state 1: drawing: place end (center calculated for 90-degree subtended angle)
3322 // Edit state 2: point edit: move start (center calculated for invariant subtended angle)
3323 // Edit state 3: point edit: move end (center calculated for invariant subtended angle)
3324 // Edit state 4: point edit: move center
3325 // Edit state 5: point edit: move arc-mid-point
3326
3327 switch( m_editState )
3328 {
3329 case 0:
3330 SetArcGeometry( aPosition, aPosition, aPosition );
3331 return;
3332
3333 case 1:
3334 m_end = aPosition;
3335 radius = m_start.Distance( m_end ) * M_SQRT1_2;
3336 break;
3337
3338 case 2:
3339 case 3:
3340 {
3341 VECTOR2I v = m_start - m_end;
3342 double chordBefore = v.SquaredEuclideanNorm();
3343
3344 if( m_editState == 2 )
3345 m_start = aPosition;
3346 else
3347 m_end = aPosition;
3348
3349 v = m_start - m_end;
3350
3351 double chordAfter = v.SquaredEuclideanNorm();
3352 double ratio = 0.0;
3353
3354 if( chordBefore > 0 )
3355 ratio = chordAfter / chordBefore;
3356
3357 if( ratio != 0 )
3358 radius = std::max( sqrt( sq( radius ) * ratio ), sqrt( chordAfter ) / 2 );
3359
3360 break;
3361 }
3362
3363 case 4:
3364 {
3365 double radialA = m_start.Distance( aPosition );
3366 double radialB = m_end.Distance( aPosition );
3367 radius = ( radialA + radialB ) / 2.0;
3368 break;
3369 }
3370
3371 case 5:
3372 // A straight arc has no center, so hold the last valid geometry
3373 if( !IsStraightArc( GetStart(), aPosition, GetEnd() ) )
3374 SetArcGeometry( GetStart(), aPosition, GetEnd() );
3375
3376 return;
3377 }
3378
3379 // Calculate center based on start, end, and radius
3380 //
3381 // Let 'l' be the length of the chord and 'm' the middle point of the chord
3382 double l = m_start.Distance( m_end );
3383 VECTOR2D m = ( m_start + m_end ) / 2;
3384 double sqRadDiff = ( radius * radius ) - ( l * l ) / 4.0;
3385
3386 // Calculate 'd', the vector from the chord midpoint to the center
3387 VECTOR2D d;
3388
3389 if( l > 0 && sqRadDiff >= 0 )
3390 {
3391 d.x = sqrt( sqRadDiff ) * ( m_start.y - m_end.y ) / l;
3392 d.y = sqrt( sqRadDiff ) * ( m_end.x - m_start.x ) / l;
3393 }
3394
3395 VECTOR2I c1 = KiROUND( m + d );
3396 VECTOR2I c2 = KiROUND( m - d );
3397
3398 // Solution gives us 2 centers; we need to pick one:
3399 switch( m_editState )
3400 {
3401 case 1:
3402 // Keep arc clockwise while drawing i.e. arc angle = 90 deg.
3403 // it can be 90 or 270 deg depending on the arc center choice (c1 or c2)
3404 m_arcCenter = c1; // first trial
3405
3406 if( GetArcAngle() > ANGLE_180 )
3407 m_arcCenter = c2;
3408
3409 break;
3410
3411 case 2:
3412 case 3:
3413 // Pick the one of c1, c2 to keep arc on the same side
3414 m_arcCenter = c1; // first trial
3415
3416 if( ( lastAngle < ANGLE_180 ) != ( GetArcAngle() < ANGLE_180 ) )
3417 m_arcCenter = c2;
3418
3419 break;
3420
3421 case 4:
3422 // Pick the one closer to the mouse position
3423 m_arcCenter = c1.Distance( aPosition ) < c2.Distance( aPosition ) ? c1 : c2;
3424 break;
3425 }
3426
3427 break;
3428 }
3429
3430 case SHAPE_T::POLY:
3431 GetPolyShape().Outline( 0 ).SetPoint( GetPolyShape().Outline( 0 ).GetPointCount() - 1, aPosition );
3432 break;
3433
3434 case SHAPE_T::ELLIPSE:
3435 {
3436 const VECTOR2I firstCorner = GetStart();
3437 const VECTOR2I secondCorner = aPosition;
3438 const VECTOR2I center = ( firstCorner + secondCorner ) / 2;
3439 const int halfW = std::abs( secondCorner.x - firstCorner.x ) / 2;
3440 const int halfH = std::abs( secondCorner.y - firstCorner.y ) / 2;
3441
3442 int majorRadius;
3443 int minorRadius;
3444 EDA_ANGLE rotation;
3445
3446 if( halfW >= halfH )
3447 {
3448 majorRadius = std::max( halfW, 1 );
3449 minorRadius = std::max( halfH, 1 );
3450 rotation = ANGLE_0;
3451 }
3452 else
3453 {
3454 majorRadius = std::max( halfH, 1 );
3455 minorRadius = std::max( halfW, 1 );
3456 rotation = ANGLE_90;
3457 }
3458
3460 SetEllipseMajorRadius( majorRadius );
3461 SetEllipseMinorRadius( minorRadius );
3462 SetEllipseRotation( rotation );
3463 SetEnd( aPosition );
3464 break;
3465 }
3466
3468 {
3469 switch( m_editState )
3470 {
3471 case 0:
3472 case 1:
3473 {
3474 // Bbox
3475 const VECTOR2I firstCorner = GetStart();
3476 const VECTOR2I secondCorner = aPosition;
3477 const VECTOR2I center = ( firstCorner + secondCorner ) / 2;
3478 const int halfW = std::abs( secondCorner.x - firstCorner.x ) / 2;
3479 const int halfH = std::abs( secondCorner.y - firstCorner.y ) / 2;
3480
3481 int majorRadius;
3482 int minorRadius;
3483 EDA_ANGLE rotation;
3484
3485 if( halfW >= halfH )
3486 {
3487 majorRadius = std::max( halfW, 1 );
3488 minorRadius = std::max( halfH, 1 );
3489 rotation = ANGLE_0;
3490 }
3491 else
3492 {
3493 majorRadius = std::max( halfH, 1 );
3494 minorRadius = std::max( halfW, 1 );
3495 rotation = ANGLE_90;
3496 }
3497
3499 SetEllipseMajorRadius( majorRadius );
3500 SetEllipseMinorRadius( minorRadius );
3501 SetEllipseRotation( rotation );
3502 SetEnd( aPosition );
3503
3504 // Keep the preview rendering as a full closed ellipse during bbox build.
3507
3508 break;
3509 }
3510
3511 case 2:
3512 case 3:
3513 {
3514 // Project cursor onto the parametric form (a * cos t, b * sin t) to get t.
3515 const VECTOR2I center = m_ellipse.Center;
3516 const double a = std::max( 1, m_ellipse.MajorRadius );
3517 const double b = std::max( 1, m_ellipse.MinorRadius );
3518 const EDA_ANGLE rotation = m_ellipse.Rotation;
3519
3520 const double dx = aPosition.x - center.x;
3521 const double dy = aPosition.y - center.y;
3522
3523 const double cosRot = rotation.Cos();
3524 const double sinRot = rotation.Sin();
3525 const double lx = dx * cosRot + dy * sinRot;
3526 const double ly = -dx * sinRot + dy * cosRot;
3527
3528 const EDA_ANGLE paramAngle( std::atan2( ly / b, lx / a ), RADIANS_T );
3529
3530 if( m_editState == 2 )
3531 {
3532 SetEllipseStartAngle( paramAngle );
3533 SetEllipseEndAngle( paramAngle + ANGLE_360 );
3534 }
3535 else
3536 {
3537 // Force end > start
3538 EDA_ANGLE cursorAngle = paramAngle;
3539
3540 while( cursorAngle <= m_ellipse.StartAngle )
3541 cursorAngle = cursorAngle + ANGLE_360;
3542
3543 SetEllipseEndAngle( cursorAngle );
3544 }
3545
3546 break;
3547 }
3548 }
3549
3550 break;
3551 }
3552
3553 default: UNIMPLEMENTED_FOR( SHAPE_T_asString() );
3554 }
3555}
3556
3557
3558void EDA_SHAPE::endEdit( bool aClosed )
3559{
3560 switch( GetShape() )
3561 {
3562 case SHAPE_T::ARC:
3563 case SHAPE_T::SEGMENT:
3564 case SHAPE_T::CIRCLE:
3565 case SHAPE_T::RECTANGLE:
3566 case SHAPE_T::BEZIER: break;
3567
3568 case SHAPE_T::ELLIPSE:
3570 m_editState = 0;
3572 break;
3573
3574 case SHAPE_T::POLY:
3575 {
3576 SHAPE_LINE_CHAIN& poly = GetPolyShape().Outline( 0 );
3577
3578 // do not include last point twice
3579 if( poly.GetPointCount() > 2 && poly.CPoint( (int) poly.GetPointCount() - 2 ) == poly.CLastPoint() )
3580 poly.Remove( (int) poly.GetPointCount() - 1 );
3581
3582 poly.SetClosed( aClosed );
3583
3584 break;
3585 }
3586
3587 default: UNIMPLEMENTED_FOR( SHAPE_T_asString() );
3588 }
3589}
3590
3591
3593{
3594 EDA_SHAPE* image = dynamic_cast<EDA_SHAPE*>( aImage );
3595 assert( image );
3596
3597#define SWAPITEM( x ) std::swap( x, image->x )
3598 SWAPITEM( m_stroke );
3599 SWAPITEM( m_start );
3600 SWAPITEM( m_end );
3602 SWAPITEM( m_shape );
3606 SWAPITEM( m_poly );
3609 SWAPITEM( m_fill );
3615#undef SWAPITEM
3616
3617 m_hatchingDirty = true;
3618}
3619
3620
3621int EDA_SHAPE::Compare( const EDA_SHAPE* aOther ) const
3622{
3623#define EPSILON 2 // Should be enough for rounding errors on calculated items
3624
3625#define TEST( a, b ) \
3626 { \
3627 if( a != b ) \
3628 return a - b; \
3629 }
3630#define TEST_E( a, b ) \
3631 { \
3632 if( abs( a - b ) > EPSILON ) \
3633 return a - b; \
3634 }
3635#define TEST_PT( a, b ) \
3636 { \
3637 TEST_E( a.x, b.x ); \
3638 TEST_E( a.y, b.y ); \
3639 }
3640
3641 TEST_PT( m_start, aOther->m_start );
3642 TEST_PT( m_end, aOther->m_end );
3643
3644 TEST( (int) m_shape, (int) aOther->m_shape );
3645
3647 {
3649 }
3650 else if( m_shape == SHAPE_T::ARC )
3651 {
3652 TEST_PT( GetArcMid(), aOther->GetArcMid() );
3653 }
3654 else if( m_shape == SHAPE_T::BEZIER )
3655 {
3656 TEST_PT( m_bezierC1, aOther->m_bezierC1 );
3657 TEST_PT( m_bezierC2, aOther->m_bezierC2 );
3658 }
3660 {
3661 TEST_PT( m_ellipse.Center, aOther->m_ellipse.Center );
3662 TEST_E( m_ellipse.MajorRadius, aOther->m_ellipse.MajorRadius );
3663 TEST_E( m_ellipse.MinorRadius, aOther->m_ellipse.MinorRadius );
3664 TEST_E( m_ellipse.Rotation.AsTenthsOfADegree(), aOther->m_ellipse.Rotation.AsTenthsOfADegree() );
3665
3667 {
3668 TEST_E( m_ellipse.StartAngle.AsTenthsOfADegree(), aOther->m_ellipse.StartAngle.AsTenthsOfADegree() );
3669 TEST_E( m_ellipse.EndAngle.AsTenthsOfADegree(), aOther->m_ellipse.EndAngle.AsTenthsOfADegree() );
3670 }
3671 }
3672 else if( m_shape == SHAPE_T::POLY )
3673 {
3674 TEST( GetPolyShape().TotalVertices(), aOther->GetPolyShape().TotalVertices() );
3675 }
3676
3677 if( m_bezierPoints.size() != aOther->m_bezierPoints.size() )
3678 return m_bezierPoints.size() < aOther->m_bezierPoints.size() ? -1 : 1;
3679
3680 for( size_t ii = 0; ii < m_bezierPoints.size(); ++ii )
3681 TEST_PT( m_bezierPoints[ii], aOther->m_bezierPoints[ii] );
3682
3683 for( int ii = 0; ii < GetPolyShape().TotalVertices(); ++ii )
3684 TEST_PT( GetPolyShape().CVertex( ii ), aOther->GetPolyShape().CVertex( ii ) );
3685
3686 TEST_E( m_stroke.GetWidth(), aOther->m_stroke.GetWidth() );
3687 TEST( (int) m_stroke.GetLineStyle(), (int) aOther->m_stroke.GetLineStyle() );
3688 TEST( (int) m_fill, (int) aOther->m_fill );
3689
3690 return 0;
3691}
3692
3693
3694void EDA_SHAPE::TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, int aClearance, int aError, ERROR_LOC aErrorLoc,
3695 bool ignoreLineWidth, bool includeFill ) const
3696{
3697 bool solidFill = IsSolidFill() || ( IsHatchedFill() && !includeFill ) || IsProxyItem();
3698 int width = ignoreLineWidth ? 0 : GetWidth();
3699
3700 width += 2 * aClearance;
3701
3702 switch( m_shape )
3703 {
3704 case SHAPE_T::CIRCLE:
3705 {
3706 int r = GetRadius();
3707
3708 if( solidFill )
3709 TransformCircleToPolygon( aBuffer, getCenter(), r + width / 2, aError, aErrorLoc );
3710 else
3711 TransformRingToPolygon( aBuffer, getCenter(), r, width, aError, aErrorLoc );
3712
3713 break;
3714 }
3715
3716 case SHAPE_T::RECTANGLE:
3717 {
3718 if( GetCornerRadius() > 0 )
3719 {
3721 BOX2I bbox = getBoundingBox();
3722 VECTOR2I position = bbox.GetCenter();
3723
3724 if( solidFill )
3725 {
3726 TransformRoundChamferedRectToPolygon( aBuffer, position, size, ANGLE_0, GetCornerRadius(), 0.0, 0,
3727 width / 2, aError, aErrorLoc );
3728 }
3729 else
3730 {
3732 SHAPE_POLY_SET poly;
3733 rr.TransformToPolygon( poly, aError );
3734 SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
3735 outline.SetClosed( true );
3736
3737 std::set<size_t> arcsHandled;
3738
3739 for( int ii = 0; ii < outline.SegmentCount(); ++ii )
3740 {
3741 if( outline.IsArcSegment( ii ) )
3742 {
3743 size_t arcIndex = outline.ArcIndex( ii );
3744
3745 if( arcsHandled.contains( arcIndex ) )
3746 continue;
3747
3748 arcsHandled.insert( arcIndex );
3749
3750 const SHAPE_ARC& arc = outline.Arc( arcIndex );
3751 TransformArcToPolygon( aBuffer, arc.GetP0(), arc.GetArcMid(), arc.GetP1(), width, aError,
3752 aErrorLoc );
3753 }
3754 else
3755 {
3756 const SEG& seg = outline.GetSegment( ii );
3757 TransformOvalToPolygon( aBuffer, seg.A, seg.B, width, aError, aErrorLoc );
3758 }
3759 }
3760 }
3761 }
3762 else
3763 {
3764 std::vector<VECTOR2I> pts = GetRectCorners();
3765
3766 if( solidFill )
3767 {
3768 aBuffer.NewOutline();
3769
3770 for( const VECTOR2I& pt : pts )
3771 aBuffer.Append( pt );
3772 }
3773
3774 if( width > 0 || !solidFill )
3775 {
3776 // Add in segments
3777 TransformOvalToPolygon( aBuffer, pts[0], pts[1], width, aError, aErrorLoc );
3778 TransformOvalToPolygon( aBuffer, pts[1], pts[2], width, aError, aErrorLoc );
3779 TransformOvalToPolygon( aBuffer, pts[2], pts[3], width, aError, aErrorLoc );
3780 TransformOvalToPolygon( aBuffer, pts[3], pts[0], width, aError, aErrorLoc );
3781 }
3782 }
3783
3784 break;
3785 }
3786
3787 case SHAPE_T::ARC:
3788 TransformArcToPolygon( aBuffer, GetStart(), GetArcMid(), GetEnd(), width, aError, aErrorLoc );
3789 break;
3790
3791 case SHAPE_T::SEGMENT:
3792 TransformOvalToPolygon( aBuffer, GetStart(), GetEnd(), width, aError, aErrorLoc );
3793 break;
3794
3795 case SHAPE_T::POLY:
3796 {
3797 if( !IsPolyShapeValid() )
3798 break;
3799
3800 if( solidFill )
3801 {
3802 for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
3803 {
3804 const SHAPE_LINE_CHAIN& poly = GetPolyShape().Outline( ii );
3805 SHAPE_POLY_SET tmp;
3806 tmp.NewOutline();
3807
3808 for( int jj = 0; jj < (int) poly.GetPointCount(); ++jj )
3809 tmp.Append( poly.GetPoint( jj ) );
3810
3811 if( width > 0 )
3812 {
3813 int inflate = width / 2;
3814
3815 if( aErrorLoc == ERROR_OUTSIDE )
3816 inflate += aError;
3817
3818 tmp.Inflate( inflate, CORNER_STRATEGY::ROUND_ALL_CORNERS, aError );
3819 }
3820
3821 aBuffer.Append( tmp );
3822 }
3823 }
3824 else
3825 {
3826 for( int ii = 0; ii < GetPolyShape().OutlineCount(); ++ii )
3827 {
3828 const SHAPE_LINE_CHAIN& poly = GetPolyShape().Outline( ii );
3829
3830 for( int jj = 0; jj < (int) poly.SegmentCount(); ++jj )
3831 {
3832 const SEG& seg = poly.GetSegment( jj );
3833 TransformOvalToPolygon( aBuffer, seg.A, seg.B, width, aError, aErrorLoc );
3834 }
3835 }
3836 }
3837
3838 break;
3839 }
3840
3841 case SHAPE_T::BEZIER:
3842 {
3843 std::vector<VECTOR2I> ctrlPts = { GetStart(), GetBezierC1(), GetBezierC2(), GetEnd() };
3844 BEZIER_POLY converter( ctrlPts );
3845 std::vector<VECTOR2I> poly;
3846 converter.GetPoly( poly, aError );
3847
3848 for( unsigned ii = 1; ii < poly.size(); ii++ )
3849 TransformOvalToPolygon( aBuffer, poly[ii - 1], poly[ii], width, aError, aErrorLoc );
3850
3851 break;
3852 }
3853
3854 case SHAPE_T::ELLIPSE:
3856 {
3858
3860
3861 if( solidFill && m_shape == SHAPE_T::ELLIPSE )
3862 {
3863 // Filled closed ellipse, build the outline, inflate for stroke width.
3864 SHAPE_POLY_SET tmp;
3865 tmp.NewOutline();
3866
3867 for( int ii = 0; ii < chain.PointCount(); ++ii )
3868 tmp.Append( chain.CPoint( ii ) );
3869
3870 if( width > 0 )
3871 {
3872 int inflate = width / 2;
3873
3874 if( aErrorLoc == ERROR_OUTSIDE )
3875 inflate += aError;
3876
3877 tmp.Inflate( inflate, CORNER_STRATEGY::ROUND_ALL_CORNERS, aError );
3878 }
3879
3880 aBuffer.Append( tmp );
3881 }
3882 else
3883 {
3884 // stroke each tessellated segment as an oval.
3885 for( int ii = 0; ii < chain.SegmentCount(); ++ii )
3886 {
3887 const SEG& seg = chain.CSegment( ii );
3888 TransformOvalToPolygon( aBuffer, seg.A, seg.B, width, aError, aErrorLoc );
3889 }
3890 }
3891
3892 break;
3893 }
3894
3895 default:
3897 break;
3898 }
3899
3900 if( IsHatchedFill() && includeFill )
3901 {
3902 for( int ii = 0; ii < GetHatching().OutlineCount(); ++ii )
3903 aBuffer.AddOutline( GetHatching().COutline( ii ) );
3904 }
3905}
3906
3907
3908void EDA_SHAPE::SetWidth( int aWidth )
3909{
3910 m_stroke.SetWidth( aWidth );
3911 m_hatchingDirty = true;
3912}
3913
3914
3916{
3917 m_stroke.SetLineStyle( aStyle );
3918}
3919
3920
3922{
3923 if( m_stroke.GetLineStyle() != LINE_STYLE::DEFAULT )
3924 return m_stroke.GetLineStyle();
3925
3926 return LINE_STYLE::SOLID;
3927}
3928
3929
3930bool EDA_SHAPE::operator==( const EDA_SHAPE& aOther ) const
3931{
3932 if( GetShape() != aOther.GetShape() )
3933 return false;
3934
3935 if( m_fill != aOther.m_fill )
3936 return false;
3937
3938 // Effective width, so pcbnew's clamped -1 sentinel equals 0
3939 if( GetWidth() != aOther.GetWidth() )
3940 return false;
3941
3942 if( m_stroke.GetLineStyle() != aOther.m_stroke.GetLineStyle() )
3943 return false;
3944
3945 if( m_fillColor != aOther.m_fillColor )
3946 return false;
3947
3948 switch( GetShape() )
3949 {
3950 case SHAPE_T::SEGMENT:
3951 case SHAPE_T::RECTANGLE:
3952 case SHAPE_T::CIRCLE:
3953 if( m_start != aOther.m_start )
3954 return false;
3955
3956 if( m_end != aOther.m_end )
3957 return false;
3958
3959 break;
3960
3961 case SHAPE_T::ARC:
3962 if( m_start != aOther.m_start )
3963 return false;
3964
3965 if( m_end != aOther.m_end )
3966 return false;
3967
3968 if( m_arcCenter != aOther.m_arcCenter )
3969 return false;
3970
3971 break;
3972
3973 case SHAPE_T::POLY:
3974 if( GetPolyShape().TotalVertices() != aOther.GetPolyShape().TotalVertices() )
3975 return false;
3976
3977 for( int ii = 0; ii < GetPolyShape().TotalVertices(); ++ii )
3978 {
3979 if( GetPolyShape().CVertex( ii ) != aOther.GetPolyShape().CVertex( ii ) )
3980 return false;
3981 }
3982
3983 break;
3984
3985 case SHAPE_T::BEZIER:
3986 if( m_start != aOther.m_start )
3987 return false;
3988
3989 if( m_end != aOther.m_end )
3990 return false;
3991
3992 if( m_bezierC1 != aOther.m_bezierC1 )
3993 return false;
3994
3995 if( m_bezierC2 != aOther.m_bezierC2 )
3996 return false;
3997
3998 if( m_bezierPoints != aOther.m_bezierPoints )
3999 return false;
4000
4001 break;
4002
4003 case SHAPE_T::ELLIPSE:
4005 if( m_ellipse.Center != aOther.m_ellipse.Center )
4006 return false;
4007
4008 if( m_ellipse.MajorRadius != aOther.m_ellipse.MajorRadius )
4009 return false;
4010 if( m_ellipse.MinorRadius != aOther.m_ellipse.MinorRadius )
4011 return false;
4012
4013 if( m_ellipse.Rotation != aOther.m_ellipse.Rotation )
4014 return false;
4015
4017 {
4018 if( m_ellipse.StartAngle != aOther.m_ellipse.StartAngle )
4019 return false;
4020
4021 if( m_ellipse.EndAngle != aOther.m_ellipse.EndAngle )
4022 return false;
4023 }
4024
4025 break;
4026
4027 default: return false;
4028 }
4029
4030 if( m_startEnding != aOther.m_startEnding )
4031 return false;
4032
4033 if( m_endEnding != aOther.m_endEnding )
4034 return false;
4035
4036 return true;
4037}
4038
4039
4040double EDA_SHAPE::Similarity( const EDA_SHAPE& aOther ) const
4041{
4042 if( GetShape() != aOther.GetShape() )
4043 return 0.0;
4044
4045 double similarity = 1.0;
4046
4047 if( m_fill != aOther.m_fill )
4048 similarity *= 0.9;
4049
4050 if( m_stroke.GetWidth() != aOther.m_stroke.GetWidth() )
4051 similarity *= 0.9;
4052
4053 if( m_stroke.GetLineStyle() != aOther.m_stroke.GetLineStyle() )
4054 similarity *= 0.9;
4055
4056 if( m_fillColor != aOther.m_fillColor )
4057 similarity *= 0.9;
4058
4059 if( m_start != aOther.m_start )
4060 similarity *= 0.9;
4061
4062 if( m_end != aOther.m_end )
4063 similarity *= 0.9;
4064
4065 if( m_arcCenter != aOther.m_arcCenter )
4066 similarity *= 0.9;
4067
4068 if( m_bezierC1 != aOther.m_bezierC1 )
4069 similarity *= 0.9;
4070
4071 if( m_bezierC2 != aOther.m_bezierC2 )
4072 similarity *= 0.9;
4073
4074 {
4075 int m = m_bezierPoints.size();
4076 int n = aOther.m_bezierPoints.size();
4077
4078 size_t longest = alg::longest_common_subset( m_bezierPoints, aOther.m_bezierPoints );
4079
4080 similarity *= std::pow( 0.9, m + n - 2 * longest );
4081 }
4082
4083 {
4084 int m = GetPolyShape().TotalVertices();
4085 int n = aOther.GetPolyShape().TotalVertices();
4086 std::vector<VECTOR2I> poly;
4087 std::vector<VECTOR2I> otherPoly;
4088 VECTOR2I lastPt( 0, 0 );
4089
4090 // We look for the longest common subset of the two polygons, but we need to
4091 // offset each point because we're actually looking for overall similarity, not just
4092 // exact matches. So if the zone is moved by 1IU, we only want one point to be
4093 // considered "moved" rather than the entire polygon. In this case, the first point
4094 // will not be a match but the rest of the sequence will.
4095 for( int ii = 0; ii < m; ++ii )
4096 {
4097 poly.emplace_back( lastPt - GetPolyShape().CVertex( ii ) );
4098 lastPt = GetPolyShape().CVertex( ii );
4099 }
4100
4101 lastPt = VECTOR2I( 0, 0 );
4102
4103 for( int ii = 0; ii < n; ++ii )
4104 {
4105 otherPoly.emplace_back( lastPt - aOther.GetPolyShape().CVertex( ii ) );
4106 lastPt = aOther.GetPolyShape().CVertex( ii );
4107 }
4108
4109 size_t longest = alg::longest_common_subset( poly, otherPoly );
4110
4111 similarity *= std::pow( 0.9, m + n - 2 * longest );
4112 }
4113
4114 if( m_startEnding != aOther.m_startEnding )
4115 similarity *= 0.9;
4116
4117 if( m_endEnding != aOther.m_endEnding )
4118 similarity *= 0.9;
4119
4120 return similarity;
4121}
4122
4123
4124static double bezierLength( const BEZIER<double>& aBezier, double aT0, double aT1 );
4125
4126static double findBezierTAtLength( const BEZIER<double>& aBezier, double aTargetLength, double aTotalLength );
4127
4128
4129void EDA_SHAPE::GetEndingTangents( EDA_ANGLE& aStartTangent, EDA_ANGLE& aEndTangent, int aLineWidth ) const
4130{
4131 aStartTangent = ANGLE_0;
4132 aEndTangent = ANGLE_0;
4133
4134 switch( m_shape )
4135 {
4136 case SHAPE_T::SEGMENT:
4137 {
4138 EDA_ANGLE lineAngle( GetEnd() - GetStart() );
4139 aStartTangent = lineAngle + ANGLE_180;
4140 aEndTangent = lineAngle;
4141 break;
4142 }
4143
4145 {
4146 // Don't factor in the shortening. If I have a cardinal-section arc, then I expect my arrowhead
4147 // to be cardinally rotated. Having the arrow be on the tangent of the arc at the back of the
4148 // arrowhead looks prettier, but can be misleading as to intent.
4149
4150 EDA_ANGLE startAngle( GetStart() - getCenter() );
4151 EDA_ANGLE endAngle( GetEnd() - getCenter() );
4152
4153 if( m_ellipse.StartAngle - m_ellipse.EndAngle > ANGLE_0 )
4154 {
4155 aStartTangent = startAngle + ANGLE_90;
4156 aEndTangent = endAngle - ANGLE_90;
4157 }
4158 else
4159 {
4160 aStartTangent = startAngle - ANGLE_90;
4161 aEndTangent = endAngle + ANGLE_90;
4162 }
4163
4164 break;
4165 }
4166
4167 case SHAPE_T::ARC:
4168 {
4169 // Don't factor in the shortening. If I have a cardinal-section arc, then I expect my arrowhead
4170 // to be cardinally rotated. Having the arrow be on the tangent of the arc at the back of the
4171 // arrowhead looks prettier, but can be misleading as to intent.
4172
4173 SHAPE_ARC arc( GetStart(), GetArcMid(), GetEnd(), 0 );
4174
4175 if( arc.GetCentralAngle() < ANGLE_0 )
4176 {
4177 aStartTangent = arc.GetStartAngle() + ANGLE_90;
4178 aEndTangent = arc.GetEndAngle() - ANGLE_90;
4179 }
4180 else
4181 {
4182 aStartTangent = arc.GetStartAngle() - ANGLE_90;
4183 aEndTangent = arc.GetEndAngle() + ANGLE_90;
4184 }
4185
4186 break;
4187 }
4188
4189 case SHAPE_T::BEZIER:
4190 {
4191 // Tangent = (original endpoint - shortened endpoint) so that the end shape aligns with the visible
4192 // curve endpoint.
4193 //
4194 // Does this make more sense here then in the arc case? Possibly.
4195
4197 VECTOR2D( m_end ) };
4198 std::optional<double> totalLength;
4199 std::optional<BEZIER<double>> shortened = ShortenedBezierCurve( aLineWidth );
4200 const std::vector<VECTOR2I>& bpts = GetBezierPoints();
4201
4202 auto fallbackStartTangent =
4203 [&]() -> EDA_ANGLE
4204 {
4205 if( GetStart() != GetBezierC1() )
4206 return EDA_ANGLE( GetStart() - GetBezierC1() );
4207
4208 if( bpts.size() >= 2 )
4209 return EDA_ANGLE( bpts.front() - bpts[1] );
4210
4211 return ANGLE_0;
4212 };
4213
4214 auto fallbackEndTangent =
4215 [&]() -> EDA_ANGLE
4216 {
4217 if( GetEnd() != GetBezierC2() )
4218 return EDA_ANGLE( GetEnd() - GetBezierC2() );
4219
4220 if( bpts.size() >= 2 )
4221 return EDA_ANGLE( bpts.back() - bpts[bpts.size() - 2] );
4222
4223 return ANGLE_0;
4224 };
4225
4226 auto openArrowFlexTangent =
4227 [&]( const LINE_ENDING& aEnding, bool aStart ) -> std::optional<EDA_ANGLE>
4228 {
4229 if( aEnding.GetStyle() != LINE_ENDING_STYLE::ARROW_OPEN )
4230 return std::nullopt;
4231
4232 int depth = aEnding.GetCurveOrientationDepth( aLineWidth );
4233
4234 if( depth <= 0 )
4235 return std::nullopt;
4236
4237 if( !totalLength )
4238 totalLength = bezierLength( sourceCurve, 0.0, 1.0 );
4239
4240 if( *totalLength <= 0.0 )
4241 return std::nullopt;
4242
4243 double distanceFromStart = std::min<double>( depth, *totalLength );
4244
4245 if( !aStart )
4246 distanceFromStart = *totalLength - distanceFromStart;
4247
4248 double t = findBezierTAtLength( sourceCurve, distanceFromStart, *totalLength );
4249 VECTOR2D sample = sourceCurve.PointAt( t );
4250 VECTOR2D endpoint = aStart ? VECTOR2D( GetStart() ) : VECTOR2D( GetEnd() );
4251 VECTOR2D delta = endpoint - sample;
4252
4253 if( delta.EuclideanNorm() <= 0.0 )
4254 return std::nullopt;
4255
4256 return EDA_ANGLE( delta );
4257 };
4258
4259 if( shortened )
4260 {
4261 VECTOR2D startDelta = VECTOR2D( GetStart() ) - shortened->Start;
4262
4263 if( startDelta.EuclideanNorm() > 0 )
4264 aStartTangent = EDA_ANGLE( startDelta );
4265 else
4266 aStartTangent = fallbackStartTangent();
4267
4268 VECTOR2D endDelta = VECTOR2D( GetEnd() ) - shortened->End;
4269
4270 if( endDelta.EuclideanNorm() > 0 )
4271 aEndTangent = EDA_ANGLE( endDelta );
4272 else
4273 aEndTangent = fallbackEndTangent();
4274 }
4275 else
4276 {
4277 aStartTangent = fallbackStartTangent();
4278 aEndTangent = fallbackEndTangent();
4279 }
4280
4281 if( std::optional<EDA_ANGLE> startFlex = openArrowFlexTangent( m_startEnding, true ) )
4282 aStartTangent = *startFlex;
4283
4284 if( std::optional<EDA_ANGLE> endFlex = openArrowFlexTangent( m_endEnding, false ) )
4285 aEndTangent = *endFlex;
4286
4287 break;
4288 }
4289
4290 case SHAPE_T::POLY:
4291 {
4292 const SHAPE_POLY_SET& poly = GetPolyShape();
4293
4294 if( poly.OutlineCount() > 0 )
4295 {
4296 const SHAPE_LINE_CHAIN& outline = poly.Outline( 0 );
4297 int ptCount = outline.PointCount();
4298
4299 if( ptCount >= 2 )
4300 {
4301 aStartTangent = EDA_ANGLE( outline.CPoint( 0 ) - outline.CPoint( 1 ) );
4302 aEndTangent = EDA_ANGLE( outline.CPoint( ptCount - 1 ) - outline.CPoint( ptCount - 2 ) );
4303 }
4304 }
4305
4306 break;
4307 }
4308
4309 default:
4310 aStartTangent = ANGLE_0;
4311 aEndTangent = ANGLE_0;
4312 break;
4313 }
4314}
4315
4316
4317bool EDA_SHAPE::GetLineEndingEndpoints( VECTOR2I& aStartPoint, VECTOR2I& aEndPoint ) const
4318{
4319 switch( m_shape )
4320 {
4321 case SHAPE_T::SEGMENT:
4322 case SHAPE_T::ARC:
4324 aStartPoint = GetStart();
4325 aEndPoint = GetEnd();
4326 return true;
4327
4328 case SHAPE_T::POLY:
4329 {
4330 const SHAPE_POLY_SET& poly = GetPolyShape();
4331
4332 if( poly.OutlineCount() == 0 )
4333 return false;
4334
4335 const SHAPE_LINE_CHAIN& outline = poly.COutline( 0 );
4336
4337 if( outline.PointCount() < 2 )
4338 return false;
4339
4340 aStartPoint = outline.CPoint( 0 );
4341 aEndPoint = outline.CPoint( outline.PointCount() - 1 );
4342 return true;
4343 }
4344
4345 case SHAPE_T::BEZIER:
4346 {
4347 const std::vector<VECTOR2I>& bpts = GetBezierPoints();
4348
4349 if( bpts.size() < 2 )
4350 return false;
4351
4352 aStartPoint = bpts.front();
4353 aEndPoint = bpts.back();
4354 return true;
4355 }
4356
4357 default:
4358 return false;
4359 }
4360}
4361
4362
4363bool EDA_SHAPE::ShortenSegmentForEndings( VECTOR2I& aStart, VECTOR2I& aEnd, const LINE_ENDING& aStartEnding,
4364 const LINE_ENDING& aEndEnding, int aLineWidth )
4365{
4366 VECTOR2I delta = aEnd - aStart;
4367 double len = delta.EuclideanNorm();
4368
4369 if( len == 0 )
4370 return true;
4371
4372 int startDepth = aStartEnding.GetShortenDepth( aLineWidth );
4373 int endDepth = aEndEnding.GetShortenDepth( aLineWidth );
4374 int totalDepth = std::max( 0, startDepth ) + std::max( 0, endDepth );
4375
4376 if( totalDepth <= 0 )
4377 return true;
4378
4379 if( totalDepth >= len )
4380 {
4381 aEnd = aStart;
4382 return false;
4383 }
4384
4385 VECTOR2D dir( delta.x / len, delta.y / len );
4386
4387 if( startDepth > 0 )
4388 {
4389 aStart.x += KiROUND( dir.x * startDepth );
4390 aStart.y += KiROUND( dir.y * startDepth );
4391 }
4392
4393 if( endDepth > 0 )
4394 {
4395 aEnd.x -= KiROUND( dir.x * endDepth );
4396 aEnd.y -= KiROUND( dir.y * endDepth );
4397 }
4398
4399 return true;
4400}
4401
4402
4403bool EDA_SHAPE::ShortenArcForEndings( EDA_ANGLE& aStartAngle, EDA_ANGLE& aArcAngle, double aRadius,
4404 int aLineWidth ) const
4405{
4406 if( aRadius <= 0 )
4407 return true;
4408
4409 int startDepth = m_startEnding.GetShortenDepth( aLineWidth );
4410 int endDepth = m_endEnding.GetShortenDepth( aLineWidth );
4411 int totalDepth = std::max( 0, startDepth ) + std::max( 0, endDepth );
4412
4413 if( totalDepth <= 0 )
4414 return true;
4415
4416 double arcLength = std::abs( aRadius * aArcAngle.AsRadians() );
4417
4418 if( totalDepth >= arcLength )
4419 {
4420 aArcAngle = ANGLE_0;
4421 return false;
4422 }
4423
4424 EDA_ANGLE startOffset = EDA_ANGLE( RAD2DEG( std::max( 0, startDepth ) / aRadius ), DEGREES_T );
4425 EDA_ANGLE totalOffset = EDA_ANGLE( RAD2DEG( totalDepth / aRadius ), DEGREES_T );
4426
4427 if( aArcAngle > ANGLE_0 )
4428 {
4429 aStartAngle += startOffset;
4430 aArcAngle -= totalOffset;
4431 }
4432 else
4433 {
4434 aStartAngle -= startOffset;
4435 aArcAngle += totalOffset;
4436 }
4437
4438 return true;
4439}
4440
4441
4442static double bezierSpeedAt( const BEZIER<double>& aBezier, double aT )
4443{
4444 double tInv = 1.0 - aT;
4445
4446 VECTOR2D derivative = 3.0 * tInv * tInv * ( aBezier.C1 - aBezier.Start )
4447 + 6.0 * tInv * aT * ( aBezier.C2 - aBezier.C1 )
4448 + 3.0 * aT * aT * ( aBezier.End - aBezier.C2 );
4449
4450 return derivative.EuclideanNorm();
4451}
4452
4453
4454static double bezierLength( const BEZIER<double>& aBezier, double aT0, double aT1 )
4455{
4456 if( aT1 <= aT0 )
4457 return 0.0;
4458
4459 // 16-point Gauss-Legendre integration. This avoids allocating and
4460 // flattening a sub-curve on every binary-search step during redraw.
4461 static constexpr double nodes[] = {
4462 0.0950125098376374, 0.2816035507792590, 0.4580167776572274, 0.6178762444026438,
4463 0.7554044083550030, 0.8656312023878318, 0.9445750230732326, 0.9894009349916499
4464 };
4465
4466 static constexpr double weights[] = { 0.1894506104550685, 0.1826034150449236, 0.1691565193950025,
4467 0.1495959888165767, 0.1246289712555339, 0.0951585116824928,
4468 0.0622535239386479, 0.0271524594117541 };
4469
4470 static_assert( std::size( nodes ) == std::size( weights ) );
4471
4472 double halfWidth = ( aT1 - aT0 ) / 2.0;
4473 double center = ( aT0 + aT1 ) / 2.0;
4474 double length = 0.0;
4475
4476 for( size_t ii = 0; ii < std::size( nodes ); ++ii )
4477 {
4478 double offset = halfWidth * nodes[ii];
4479 length +=
4480 weights[ii] * ( bezierSpeedAt( aBezier, center - offset ) + bezierSpeedAt( aBezier, center + offset ) );
4481 }
4482
4483 return halfWidth * length;
4484}
4485
4486
4487static double findBezierTAtLength( const BEZIER<double>& aBezier, double aTargetLength, double aTotalLength )
4488{
4489 if( aTargetLength <= 0.0 )
4490 return 0.0;
4491
4492 if( aTargetLength >= aTotalLength )
4493 return 1.0;
4494
4495 double low = 0.0;
4496 double high = 1.0;
4497
4498 // Keep the parameter search tighter than the flattened-length tolerance.
4499 static constexpr int paramSearchIterations = 24;
4500
4501 for( int ii = 0; ii < paramSearchIterations; ++ii )
4502 {
4503 double mid = ( low + high ) / 2.0;
4504 double len = bezierLength( aBezier, 0.0, mid );
4505
4506 if( len < aTargetLength )
4507 low = mid;
4508 else
4509 high = mid;
4510 }
4511
4512 return ( low + high ) / 2.0;
4513}
4514
4515
4516std::optional<BEZIER<double>> EDA_SHAPE::ShortenedBezierCurve( int aLineWidth ) const
4517{
4518 if( m_shape != SHAPE_T::BEZIER )
4519 return std::nullopt;
4520
4522
4523 if( IsClosed() )
4524 return curve;
4525
4526 int startDepth = GetStartEnding().GetShortenDepth( aLineWidth );
4527 int endDepth = GetEndEnding().GetShortenDepth( aLineWidth );
4528
4529 if( startDepth <= 0 && endDepth <= 0 )
4530 return curve;
4531
4532 double totalLength = bezierLength( curve, 0.0, 1.0 );
4533
4534 if( totalLength <= 0.0 || startDepth + endDepth >= totalLength )
4535 return std::nullopt;
4536
4537 double t0 = findBezierTAtLength( curve, startDepth, totalLength );
4538 double t1 = findBezierTAtLength( curve, totalLength - endDepth, totalLength );
4539
4540 if( t1 <= t0 )
4541 return std::nullopt;
4542
4543 return curve.SubCurve( t0, t1 );
4544}
4545
4546
4547std::vector<VECTOR2D> EDA_SHAPE::ShortenedBezierPolyline( int aLineWidth ) const
4548{
4549 std::vector<VECTOR2D> pts;
4550 std::optional<BEZIER<double>> curve = ShortenedBezierCurve( aLineWidth );
4551
4552 if( !curve )
4553 {
4554 return pts;
4555 }
4556
4557 std::vector<VECTOR2D> ctrlPts = { curve->Start, curve->C1, curve->C2, curve->End };
4558 BEZIER_POLY converter( ctrlPts );
4559
4560 converter.GetPoly( pts, std::max( 1, getMaxError() ) );
4561
4562 return pts;
4563}
4564
4565
4566bool EDA_SHAPE::ShortenPolyForEndings( VECTOR2D& aFirst, VECTOR2D& aLast, const VECTOR2D& aSecond,
4567 const VECTOR2D& aPenultimate, int aLineWidth ) const
4568{
4569 int startDepth = m_startEnding.GetShortenDepth( aLineWidth );
4570 int endDepth = m_endEnding.GetShortenDepth( aLineWidth );
4571 int totalDepth = std::max( 0, startDepth ) + std::max( 0, endDepth );
4572
4573 if( totalDepth <= 0 )
4574 return true;
4575
4576 double startSegLen = ( aSecond - aFirst ).EuclideanNorm();
4577 double endSegLen = ( aLast - aPenultimate ).EuclideanNorm();
4578 bool twoPointPoly = aSecond == aLast && aPenultimate == aFirst;
4579
4580 if( twoPointPoly && startSegLen <= totalDepth )
4581 {
4582 aLast = aFirst;
4583 return false;
4584 }
4585
4586 if( startDepth > 0 )
4587 {
4588 VECTOR2D dir = aSecond - aFirst;
4589
4590 if( startSegLen > startDepth )
4591 aFirst = aFirst + dir * ( startDepth / startSegLen );
4592 else
4593 aFirst = aSecond;
4594 }
4595
4596 if( endDepth > 0 )
4597 {
4598 VECTOR2D dir = aLast - aPenultimate;
4599
4600 if( endSegLen > endDepth )
4601 aLast = aLast - dir * ( endDepth / endSegLen );
4602 else
4603 aLast = aPenultimate;
4604 }
4605
4606 return true;
4607}
4608
4609
4610bool EDA_SHAPE::ShortenBodyPolyPoints( std::vector<VECTOR2I>& aPoints, int aOutlineIdx, int aLineWidth ) const
4611{
4612 if( aPoints.size() < 2 )
4613 return false;
4614
4615 if( IsClosed() || aOutlineIdx != 0 )
4616 return true;
4617
4618 VECTOR2D first = aPoints.front();
4619 VECTOR2D last = aPoints.back();
4620
4621 if( !ShortenPolyForEndings( first, last, VECTOR2D( aPoints[1] ), VECTOR2D( aPoints[aPoints.size() - 2] ),
4622 aLineWidth ) )
4623 {
4624 aPoints.clear();
4625 return false;
4626 }
4627
4628 aPoints.front() = VECTOR2I( first );
4629 aPoints.back() = VECTOR2I( last );
4630
4631 return true;
4632}
4633
4634
4635bool EDA_SHAPE::GetShortenedBodyPolyPoints( const SHAPE_LINE_CHAIN& aOutline, int aOutlineIdx,
4636 std::vector<VECTOR2I>& aPoints, int aLineWidth ) const
4637{
4638 aPoints.clear();
4639
4640 if( aOutline.PointCount() < 2 )
4641 return false;
4642
4643 aPoints.reserve( aOutline.PointCount() );
4644
4645 for( const VECTOR2I& pt : aOutline.CPoints() )
4646 aPoints.emplace_back( pt );
4647
4648 return ShortenBodyPolyPoints( aPoints, aOutlineIdx, aLineWidth );
4649}
4650
4651
4656
4657
4658static struct EDA_SHAPE_DESC
4659{
4661 {
4663 .Map( SHAPE_T::SEGMENT, _HKI( "Segment" ) )
4664 .Map( SHAPE_T::RECTANGLE, _HKI( "Rectangle" ) )
4665 .Map( SHAPE_T::ARC, _HKI( "Arc" ) )
4666 .Map( SHAPE_T::CIRCLE, _HKI( "Circle" ) )
4667 .Map( SHAPE_T::POLY, _HKI( "Polygon" ) )
4668 .Map( SHAPE_T::BEZIER, _HKI( "Bezier" ) )
4669 .Map( SHAPE_T::ELLIPSE, _HKI( "Ellipse" ) )
4670 .Map( SHAPE_T::ELLIPSE_ARC, _HKI( "Elliptical Arc" ) );
4671
4673
4674 if( lineStyleEnum.Choices().GetCount() == 0 )
4675 {
4676 lineStyleEnum.Map( LINE_STYLE::SOLID, _HKI( "Solid" ) )
4677 .Map( LINE_STYLE::DASH, _HKI( "Dashed" ) )
4678 .Map( LINE_STYLE::DOT, _HKI( "Dotted" ) )
4679 .Map( LINE_STYLE::DASHDOT, _HKI( "Dash-Dot" ) )
4680 .Map( LINE_STYLE::DASHDOTDOT, _HKI( "Dash-Dot-Dot" ) );
4681 }
4682
4684
4685 if( hatchModeEnum.Choices().GetCount() == 0 )
4686 {
4687 hatchModeEnum.Map( UI_FILL_MODE::NONE, _HKI( "None" ) );
4688 hatchModeEnum.Map( UI_FILL_MODE::SOLID, _HKI( "Solid" ) );
4689 hatchModeEnum.Map( UI_FILL_MODE::HATCH, _HKI( "Hatch" ) );
4690 hatchModeEnum.Map( UI_FILL_MODE::REVERSE_HATCH, _HKI( "Reverse Hatch" ) );
4691 hatchModeEnum.Map( UI_FILL_MODE::CROSS_HATCH, _HKI( "Cross-hatch" ) );
4692 }
4693
4696
4697 auto isNotPolygonOrCircle =
4698 []( INSPECTABLE* aItem ) -> bool
4699 {
4700 // Polygons, unlike other shapes, have no meaningful start or end coordinates
4701 if( EDA_SHAPE* shape = dynamic_cast<EDA_SHAPE*>( aItem ) )
4702 return shape->GetShape() != SHAPE_T::POLY && shape->GetShape() != SHAPE_T::CIRCLE;
4703
4704 return false;
4705 };
4706
4707 auto isCircle =
4708 []( INSPECTABLE* aItem ) -> bool
4709 {
4710 // Polygons, unlike other shapes, have no meaningful start or end coordinates
4711 if( EDA_SHAPE* shape = dynamic_cast<EDA_SHAPE*>( aItem ) )
4712 return shape->GetShape() == SHAPE_T::CIRCLE;
4713
4714 return false;
4715 };
4716
4717 auto isRectangle =
4718 []( INSPECTABLE* aItem ) -> bool
4719 {
4720 // Polygons, unlike other shapes, have no meaningful start or end coordinates
4721 if( EDA_SHAPE* shape = dynamic_cast<EDA_SHAPE*>( aItem ) )
4722 return shape->GetShape() == SHAPE_T::RECTANGLE;
4723
4724 return false;
4725 };
4726
4727 auto isEllipseOrEllipseArc =
4728 []( INSPECTABLE* aItem ) -> bool
4729 {
4730 if( EDA_SHAPE* shape = dynamic_cast<EDA_SHAPE*>( aItem ) )
4731 {
4732 return shape->GetShape() == SHAPE_T::ELLIPSE || shape->GetShape() == SHAPE_T::ELLIPSE_ARC;
4733 }
4734
4735 return false;
4736 };
4737
4738 auto isEllipseArc =
4739 []( INSPECTABLE* aItem ) -> bool
4740 {
4741 if( EDA_SHAPE* shape = dynamic_cast<EDA_SHAPE*>( aItem ) )
4742 return shape->GetShape() == SHAPE_T::ELLIPSE_ARC;
4743
4744 return false;
4745 };
4746
4747 const wxString shapeProps = _HKI( "Shape Properties" );
4748
4749 propMgr.AddProperty( new PROPERTY_ENUM<EDA_SHAPE, SHAPE_T>( _HKI( "Shape" ),
4751 shapeProps );
4752
4753 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Start X" ),
4756 shapeProps )
4757 .SetAvailableFunc( isNotPolygonOrCircle );
4758 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Start Y" ),
4761 shapeProps )
4762 .SetAvailableFunc( isNotPolygonOrCircle );
4763
4764 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Center X" ),
4767 shapeProps )
4768 .SetAvailableFunc( isCircle );
4769
4770 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Center Y" ),
4773 shapeProps )
4774 .SetAvailableFunc( isCircle );
4775
4776 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Radius" ),
4779 shapeProps )
4780 .SetAvailableFunc( isCircle );
4781
4782 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "End X" ),
4785 shapeProps )
4786 .SetAvailableFunc( isNotPolygonOrCircle );
4787
4788 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "End Y" ),
4791 shapeProps )
4792 .SetAvailableFunc( isNotPolygonOrCircle );
4793
4794 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Width" ),
4797 shapeProps )
4798 .SetAvailableFunc( isRectangle );
4799
4800 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Height" ),
4803 shapeProps )
4804 .SetAvailableFunc( isRectangle );
4805
4806 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Corner Radius" ),
4809 shapeProps )
4810 .SetAvailableFunc( isRectangle )
4811 .SetValidator( []( const wxAny&& aValue, EDA_ITEM* aItem ) -> VALIDATOR_RESULT
4812 {
4813 wxASSERT_MSG( aValue.CheckType<int>(),
4814 "Expecting int-containing value" );
4815
4816 int radius = aValue.As<int>();
4817
4818 EDA_SHAPE* prop_shape = dynamic_cast<EDA_SHAPE*>( aItem );
4819
4820 if( !prop_shape )
4821 return std::nullopt;
4822
4823 int maxRadius = std::min( prop_shape->GetRectangleWidth(),
4824 prop_shape->GetRectangleHeight() ) / 2;
4825
4826 if( radius > maxRadius )
4827 return std::make_unique<VALIDATION_ERROR_TOO_LARGE<int>>( radius, maxRadius );
4828 else if( radius < 0 )
4829 return std::make_unique<VALIDATION_ERROR_TOO_SMALL<int>>( radius, 0 );
4830
4831 return std::nullopt;
4832 } );
4833
4834 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Major Radius" ),
4837 shapeProps )
4838 .SetAvailableFunc( isEllipseOrEllipseArc );
4839
4840 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Minor Radius" ),
4843 shapeProps )
4844 .SetAvailableFunc( isEllipseOrEllipseArc );
4845
4846 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, EDA_ANGLE>( _HKI( "Ellipse Rotation" ),
4848 shapeProps )
4849 .SetAvailableFunc( isEllipseOrEllipseArc );
4850
4851 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, EDA_ANGLE>( _HKI( "Arc Start Angle" ),
4854 shapeProps )
4855 .SetAvailableFunc( isEllipseArc );
4856
4857 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, EDA_ANGLE>( _HKI( "Arc End Angle" ),
4859 shapeProps )
4860 .SetAvailableFunc( isEllipseArc );
4861
4862 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Line Width" ),
4864 shapeProps )
4865 .SetIsCopyable();
4866
4867 propMgr.AddProperty( new PROPERTY_ENUM<EDA_SHAPE, LINE_STYLE>( _HKI( "Line Style" ),
4869 shapeProps )
4870 .SetIsCopyable();
4871
4872 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, COLOR4D>( _HKI( "Line Color" ),
4874 shapeProps )
4876
4880 shapeProps )
4882 [=]( INSPECTABLE* aItem ) -> bool
4883 {
4884 if( EDA_SHAPE* curr_shape = dynamic_cast<EDA_SHAPE*>( aItem ) )
4885 return curr_shape->GetShape() == SHAPE_T::ARC;
4886
4887 return false;
4888 } )
4889 .SetValidator(
4890 []( const wxAny&& aValue, EDA_ITEM* aItem ) -> VALIDATOR_RESULT
4891 {
4892 double degrees = 0.0;
4893
4894 if( aValue.GetAs( &degrees ) && degrees == 0.0 )
4895 {
4896 return std::make_unique<VALIDATION_ERROR_MSG>( _( "Arc angle must not be zero." ) );
4897 }
4898
4899 return std::nullopt;
4900 } );
4901
4902 auto fillAvailable =
4903 [=]( INSPECTABLE* aItem ) -> bool
4904 {
4905 if( EDA_ITEM* edaItem = dynamic_cast<EDA_ITEM*>( aItem ) )
4906 {
4907 // For some reason masking "Filled" and "Fill Color" at the
4908 // PCB_TABLECELL level doesn't work.
4909 if( edaItem->Type() == PCB_TABLECELL_T || edaItem->Type() == PCB_TEXTBOX_T )
4910 return false;
4911 }
4912
4913 if( EDA_SHAPE* edaShape = dynamic_cast<EDA_SHAPE*>( aItem ) )
4914 {
4915 switch( edaShape->GetShape() )
4916 {
4917 case SHAPE_T::POLY:
4918 case SHAPE_T::RECTANGLE:
4919 case SHAPE_T::CIRCLE:
4920 case SHAPE_T::BEZIER:
4921 case SHAPE_T::ELLIPSE: return true;
4922
4923 default:
4924 return false;
4925 }
4926 }
4927
4928 return false;
4929 };
4930
4933 shapeProps )
4934 .SetAvailableFunc( fillAvailable )
4935 .SetIsCopyable();
4936
4937 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, COLOR4D>( _HKI( "Fill Color" ),
4939 shapeProps )
4940 .SetAvailableFunc( fillAvailable )
4942
4943 auto isOpenShape =
4944 []( INSPECTABLE* aItem ) -> bool
4945 {
4946 if( EDA_SHAPE* shape = dynamic_cast<EDA_SHAPE*>( aItem ) )
4947 return !shape->IsClosed();
4948
4949 return false;
4950 };
4951
4953
4954 if( endingStyleEnum.Choices().GetCount() == 0 )
4955 {
4956 endingStyleEnum.Map( LINE_ENDING_STYLE::NONE, _HKI( "None" ) )
4957 .Map( LINE_ENDING_STYLE::ARROW, _HKI( "Arrow" ) )
4958 .Map( LINE_ENDING_STYLE::CIRCLE, _HKI( "Circle" ) )
4959 .Map( LINE_ENDING_STYLE::SQUARE, _HKI( "Square" ) )
4960 .Map( LINE_ENDING_STYLE::ARROW_OPEN, _HKI( "Open Arrow" ) );
4961 }
4962
4963 propMgr.AddProperty( new PROPERTY_ENUM<EDA_SHAPE, LINE_ENDING_STYLE>( _HKI( "Start Shape" ),
4965 shapeProps )
4966 .SetAvailableFunc( isOpenShape );
4967
4968 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Start Length" ),
4970 shapeProps )
4971 .SetAvailableFunc( isOpenShape );
4972
4973 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Start Width" ),
4975 shapeProps )
4976 .SetAvailableFunc( isOpenShape );
4977
4978 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "Start Stroke Width" ),
4981 shapeProps )
4982 .SetAvailableFunc( isOpenShape );
4983
4986 shapeProps )
4987 .SetAvailableFunc( isOpenShape );
4988
4989 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "End Length" ),
4991 shapeProps )
4992 .SetAvailableFunc( isOpenShape );
4993
4994 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "End Width" ),
4996 shapeProps )
4997 .SetAvailableFunc( isOpenShape );
4998
4999 propMgr.AddProperty( new PROPERTY<EDA_SHAPE, int>( _HKI( "End Stroke Width" ),
5002 shapeProps )
5003 .SetAvailableFunc( isOpenShape );
5004 }
KICOMMON_API types::KiCadObjectType ToProtoEnum(KICAD_T aValue)
KICOMMON_API KICAD_T FromProtoEnum(types::KiCadObjectType aValue)
Definition api_enums.cpp:56
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
@ ERROR_OUTSIDE
@ ERROR_INSIDE
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:128
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
constexpr BOX2I KiROUND(const BOX2D &aBoxD)
Definition box2.h:985
Bezier curves to polygon converter.
void GetPoly(std::vector< VECTOR2I > &aOutput, int aMaxError=10)
Convert a Bezier curve to a polygon.
Generic cubic Bezier representation.
VECTOR2< NumericType > Start
VECTOR2< NumericType > C1
VECTOR2< NumericType > C2
VECTOR2< NumericType > End
constexpr BOX2< Vec > & Inflate(coord_type dx, coord_type dy)
Inflates the rectangle horizontally by dx and vertically by dy.
Definition box2.h:556
constexpr void SetOrigin(const Vec &pos)
Definition box2.h:240
constexpr BOX2< Vec > & Normalize()
Ensure that the height and width are positive.
Definition box2.h:149
constexpr coord_type GetY() const
Definition box2.h:211
constexpr size_type GetWidth() const
Definition box2.h:217
constexpr Vec Centre() const
Definition box2.h:100
constexpr coord_type GetX() const
Definition box2.h:210
constexpr BOX2< Vec > & Merge(const BOX2< Vec > &aRect)
Modify the position and size of the rectangle in order to contain aRect.
Definition box2.h:597
constexpr const Vec GetCenter() const
Definition box2.h:233
constexpr size_type GetHeight() const
Definition box2.h:218
constexpr coord_type GetLeft() const
Definition box2.h:231
constexpr coord_type GetRight() const
Definition box2.h:220
constexpr void SetEnd(coord_type x, coord_type y)
Definition box2.h:300
constexpr coord_type GetTop() const
Definition box2.h:232
constexpr coord_type GetBottom() const
Definition box2.h:225
static const COLOR4D UNSPECIFIED
For legacy support; used as a value to indicate color hasn't been set yet.
Definition color4d.h:400
EDA_ANGLE Normalize()
Definition eda_angle.h:228
double Sin() const
Definition eda_angle.h:177
int AsTenthsOfADegree() const
Definition eda_angle.h:117
bool IsCardinal() const
Definition eda_angle.cpp:40
EDA_ANGLE Normalize720()
Definition eda_angle.h:286
double AsRadians() const
Definition eda_angle.h:119
double Cos() const
Definition eda_angle.h:196
FRAME_T GetFrameType() const
The base class for create windows for drawing purpose.
A base class for most all the KiCad significant classes used in schematics and boards.
Definition eda_item.h:98
UI_FILL_MODE GetFillModeProp() const
static bool ShortenSegmentForEndings(VECTOR2I &aStart, VECTOR2I &aEnd, const LINE_ENDING &aStartEnding, const LINE_ENDING &aEndEnding, int aLineWidth)
Shorten a segment body for line endings.
virtual int GetHatchLineSpacing() const
Definition eda_shape.h:169
EDA_ANGLE GetArcAngle() const
int GetStartEndingLength() const
Definition eda_shape.h:198
SHAPE_T m_shape
Definition eda_shape.h:754
virtual void SetEnd(const VECTOR2I &aEnd)
Definition eda_shape.h:338
void TransformShapeToPolygon(SHAPE_POLY_SET &aBuffer, int aClearance, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false, bool includeFill=false) const
Convert the shape to a closed polygon.
void SetStartX(int x)
Definition eda_shape.h:302
int GetEllipseMinorRadius() const
Definition eda_shape.h:404
bool m_proxyItem
Definition eda_shape.h:783
int m_cornerRadius
Definition eda_shape.h:767
void SetArcOrSegment(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Set the shape from three points, falling back to a SEGMENT from aStart to aEnd when the points are co...
bool m_hatchingDirty
Definition eda_shape.h:763
bool m_endsSwapped
Definition eda_shape.h:753
const VECTOR2I & GetBezierC2() const
Definition eda_shape.h:377
const VECTOR2I & GetEllipseCenter() const
Definition eda_shape.h:386
bool GetLineEndingsBoundingBox(BOX2I &aBBox, int aLineWidth) const
void move(const VECTOR2I &aMoveVector)
virtual void SetCenter(const VECTOR2I &aCenter)
VECTOR2I getCenter() const
int GetStartY() const
Definition eda_shape.h:285
void SetFillModeProp(UI_FILL_MODE)
void mirror(const SEG &aAxis)
int m_editState
Definition eda_shape.h:782
virtual int getMaxError() const
Definition eda_shape.h:746
void rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle)
std::vector< SHAPE * > MakeEffectiveShapesWithLineEndings(int aLineWidth) const
Make effective geometry for the shape body shortened for line endings plus the line-ending geometry i...
const std::vector< VECTOR2I > buildBezierToSegmentsPointsList(int aMaxError) const
void SetEndEndingLength(int aLength)
Definition eda_shape.h:204
void SetStartEndingStyle(LINE_ENDING_STYLE aStyle)
Definition eda_shape.h:193
const SHAPE_POLY_SET & GetHatching() const
EDA_ANGLE GetEllipseEndAngle() const
Definition eda_shape.h:432
FILL_T GetFillMode() const
Definition eda_shape.h:151
virtual ~EDA_SHAPE()
Definition eda_shape.cpp:74
void SetEndEndingWidth(int aWidth)
Definition eda_shape.h:206
virtual void SetCornerRadius(int aRadius)
long long int m_rectangleHeight
Definition eda_shape.h:765
int GetEllipseMajorRadius() const
Definition eda_shape.h:395
std::unique_ptr< EDA_SHAPE_HATCH_CACHE_DATA > m_hatchingCache
Definition eda_shape.h:762
void SetEndY(int aY)
Definition eda_shape.h:345
virtual int GetEffectiveWidth() const
Definition eda_shape.h:167
std::vector< VECTOR2I > GetPolyPoints() const
Duplicate the polygon outlines into a flat list of VECTOR2I points.
ELLIPSE< int > m_ellipse
Definition eda_shape.h:779
void TransformWithLineEndingsToPolygon(SHAPE_POLY_SET &aBuffer, int aClearance, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth=false) const
Convert the shape body shortened for line endings plus line-ending geometry to polygons.
COLOR4D GetLineColor() const
Definition eda_shape.h:175
int GetEndX() const
Definition eda_shape.h:336
bool ShortenBodyPolyPoints(std::vector< VECTOR2I > &aPoints, int aOutlineIdx, int aLineWidth) const
Apply line-ending body shortening to copied/generated polyline points.
SHAPE_ELLIPSE buildShapeEllipse() const
void TransformLineEndingsToPolygon(SHAPE_POLY_SET &aBuffer, int aClearance, int aError, ERROR_LOC aErrorLoc, int aLineWidth) const
Append only the line-ending geometry associated with this shape to a polygon set.
std::vector< SHAPE * > makeEffectiveShapes(bool aEdgeOnly, bool aLineChainOnly=false, bool aHittesting=false) const
Make a set of SHAPE objects representing the EDA_SHAPE.
int GetRectangleWidth() const
void SetLineStyle(const LINE_STYLE aStyle)
std::vector< SHAPE * > makeShortenedBodyShapes(int aLineWidth, bool aEdgeOnly=false) const
Make effective geometry for the shape body shortened for line endings, without the line-ending geomet...
void recalcEllipseArcEndpoints()
When m_shape == ELLIPSE_ARC, recompute m_start/m_end from m_ellipse.
void calcEdit(const VECTOR2I &aPosition)
void SetStartY(int y)
Definition eda_shape.h:295
virtual std::vector< SHAPE * > MakeEffectiveShapes(bool aEdgeOnly=false) const
Make a set of SHAPE objects representing the EDA_SHAPE.
Definition eda_shape.h:566
bool ShortenPolyForEndings(VECTOR2D &aFirst, VECTOR2D &aLast, const VECTOR2D &aSecond, const VECTOR2D &aPenultimate, int aLineWidth) const
SHAPE_POLY_SET & GetPolyShape()
void SetCenterY(int y)
Definition eda_shape.h:309
void GetEndingTangents(EDA_ANGLE &aStartTangent, EDA_ANGLE &aEndTangent, int aLineWidth=0) const
Compute outward-facing tangent angles at the start and end of the shape.
int GetStartEndingStrokeWidth() const
Definition eda_shape.h:208
void CalcArcAngles(EDA_ANGLE &aStartAngle, EDA_ANGLE &aEndAngle) const
Calc arc start and end angles such that aStartAngle < aEndAngle.
virtual std::vector< VECTOR2I > GetCornersInSequence(EDA_ANGLE angle) const
EDA_ANGLE GetEllipseRotation() const
Definition eda_shape.h:413
void ShapeGetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList)
void SetEndEndingStyle(LINE_ENDING_STYLE aStyle)
Definition eda_shape.h:196
virtual bool isMoving() const
Definition eda_shape.h:714
virtual void SetEllipseEndAngle(const EDA_ANGLE &aA)
Definition eda_shape.h:425
int GetEndEndingStrokeWidth() const
Definition eda_shape.h:210
bool operator==(const EDA_SHAPE &aOther) const
int GetRadius() const
SHAPE_T GetShape() const
Definition eda_shape.h:178
std::vector< SHAPE * > MakeEffectiveShapesForStroking(int aLineWidth=-1) const
Make a set of SHAPE objects to hand to STROKE_PARAMS::Stroke().
virtual void SetBezierC2(const VECTOR2I &aPt)
Definition eda_shape.h:376
bool GetShortenedBodyPolyPoints(const SHAPE_LINE_CHAIN &aOutline, int aOutlineIdx, std::vector< VECTOR2I > &aPoints, int aLineWidth) const
Copy an outline and apply line-ending body shortening when applicable.
bool Deserialize(const google::protobuf::Any &aContainer) override
Deserializes the given protobuf message into this object.
const std::vector< SEG > & GetHatchLines() const
void SetRectangleHeight(const int &aHeight)
SHAPE_POLY_SET & hatching() const
bool IsHatchedFill() const
Definition eda_shape.h:133
virtual SHAPE_POLY_SET getHatchingKnockouts() const
Definition eda_shape.h:679
VECTOR2I m_arcCenter
Definition eda_shape.h:772
void SetCenterX(int x)
Definition eda_shape.h:320
virtual void SetBezierC1(const VECTOR2I &aPt)
Definition eda_shape.h:373
int GetStartEndingWidth() const
Definition eda_shape.h:200
bool GetLineEndingEndpoints(VECTOR2I &aStartPoint, VECTOR2I &aEndPoint) const
Return the source endpoints used to place line endings.
virtual void SetFilled(bool aFlag)
Definition eda_shape.h:145
virtual bool IsFilledForHitTesting() const
Definition eda_shape.h:140
virtual void SetEllipseRotation(const EDA_ANGLE &aA)
Definition eda_shape.h:406
bool continueEdit(const VECTOR2I &aPosition)
virtual void SetArcAngle(const EDA_ANGLE &aAngle)
Definition eda_shape.h:450
wxString ShowShape() const
LINE_ENDING_STYLE GetStartEndingStyle() const
Definition eda_shape.h:192
ARC_MID m_arcMidData
Definition eda_shape.h:773
void SetFillColor(const COLOR4D &aColor)
Definition eda_shape.h:163
int GetEndY() const
Definition eda_shape.h:335
bool hitTest(const VECTOR2I &aPosition, int aAccuracy=0) const
void SetCachedArcData(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd, const VECTOR2I &aCenter)
Set the data used for mid point caching.
void SetEndX(int aX)
Definition eda_shape.h:352
virtual int GetHatchLineWidth() const
Definition eda_shape.h:168
bool IsSolidFill() const
Definition eda_shape.h:126
void flip(const VECTOR2I &aCentre, FLIP_DIRECTION aFlipDirection)
int GetEndEndingWidth() const
Definition eda_shape.h:205
EDA_SHAPE(SHAPE_T aType, int aLineWidth, FILL_T aFill)
Definition eda_shape.cpp:56
std::vector< SEG > & hatchLines() const
void beginEdit(const VECTOR2I &aStartPoint)
int GetEndEndingLength() const
Definition eda_shape.h:203
VECTOR2I m_start
Definition eda_shape.h:769
int GetPointCount() const
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition eda_shape.h:334
bool IsClosed() const
void SetRadius(int aX)
Definition eda_shape.h:359
LINE_STYLE GetLineStyle() const
void endEdit(bool aClosed=true)
Finish editing the shape.
virtual void SetEllipseCenter(const VECTOR2I &aPt)
Definition eda_shape.h:379
void SetStartEndingLength(int aLength)
Definition eda_shape.h:199
void SetEndEndingStrokeWidth(int aWidth)
Definition eda_shape.h:211
virtual void SetEllipseMinorRadius(int aR)
Definition eda_shape.h:397
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition eda_shape.h:284
virtual void SetEllipseMajorRadius(int aR)
Definition eda_shape.h:388
void SetLineColor(const COLOR4D &aColor)
Definition eda_shape.h:174
COLOR4D GetFillColor() const
Definition eda_shape.h:162
void SetRectangle(const long long int &aHeight, const long long int &aWidth)
virtual void SetShape(SHAPE_T aShape)
Definition eda_shape.h:177
void SwapShape(EDA_SHAPE *aImage)
std::vector< VECTOR2I > GetRectCorners() const
std::vector< SHAPE * > MakeLineEndingEffectiveShapes(int aLineWidth) const
Make the line-ending geometry associated with this shape.
void SetStartEndingWidth(int aWidth)
Definition eda_shape.h:201
std::vector< VECTOR2I > m_bezierPoints
Definition eda_shape.h:778
bool IsAnyFill() const
Definition eda_shape.h:121
void setPosition(const VECTOR2I &aPos)
EDA_ANGLE GetEllipseStartAngle() const
Definition eda_shape.h:423
const std::vector< VECTOR2I > & GetBezierPoints() const
Definition eda_shape.h:506
virtual bool IsProxyItem() const
Definition eda_shape.h:118
void computeArcBBox(BOX2I &aBBox) const
const LINE_ENDING & GetStartEnding() const
Definition eda_shape.h:180
virtual void UpdateHatching() const
LINE_ENDING m_startEnding
Definition eda_shape.h:756
void SetRectangleWidth(const int &aWidth)
virtual void SetEllipseStartAngle(const EDA_ANGLE &aA)
Definition eda_shape.h:416
virtual void SetArcGeometry(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Set the three controlling points for an arc.
double GetLength() const
wxString SHAPE_T_asString() const
void scale(double aScale)
int GetStartX() const
Definition eda_shape.h:286
double Similarity(const EDA_SHAPE &aOther) const
const VECTOR2I & GetBezierC1() const
Definition eda_shape.h:374
std::optional< BEZIER< double > > ShortenedBezierCurve(int aLineWidth) const
Return the cubic Bezier curve shortened for line endings.
VECTOR2I m_end
Definition eda_shape.h:770
const BOX2I getBoundingBox() const
void SetArcAngleAndEnd(const EDA_ANGLE &aAngle, bool aCheckNegativeAngle=false)
Set the end point from the angle center and start.
int GetRectangleHeight() const
virtual int GetWidth() const
Definition eda_shape.h:166
bool ShortenArcForEndings(EDA_ANGLE &aStartAngle, EDA_ANGLE &aArcAngle, double aRadius, int aLineWidth) const
Shorten an arc body for line endings.
std::vector< VECTOR2D > ShortenedBezierPolyline(int aLineWidth) const
Return the flattened Bezier polyline after line-ending shortening.
VECTOR2I getPosition() const
bool IsClockwiseArc() const
STROKE_PARAMS m_stroke
Definition eda_shape.h:755
const LINE_ENDING & GetEndEnding() const
Definition eda_shape.h:186
LINE_ENDING m_endEnding
Definition eda_shape.h:757
LINE_ENDING_STYLE GetEndEndingStyle() const
Definition eda_shape.h:195
void RebuildBezierToSegmentsPointsList()
Definition eda_shape.h:556
void SetPolyPoints(const std::vector< VECTOR2I > &aPoints)
EDA_SHAPE & operator=(const EDA_SHAPE &aOther)
VECTOR2I m_bezierC1
Definition eda_shape.h:775
FILL_T m_fill
Definition eda_shape.h:759
COLOR4D m_fillColor
Definition eda_shape.h:760
virtual void SetWidth(int aWidth)
EDA_ANGLE GetSegmentAngle() const
void SetStartEndingStrokeWidth(int aWidth)
Definition eda_shape.h:209
int GetCornerRadius() const
void SetFillMode(FILL_T aFill)
std::unique_ptr< SHAPE_POLY_SET > m_poly
Definition eda_shape.h:780
virtual void SetPolyShape(const SHAPE_POLY_SET &aShape)
Definition eda_shape.h:529
virtual void SetStart(const VECTOR2I &aStart)
Definition eda_shape.h:288
wxString getFriendlyName(FRAME_T aFrameType) const
long long int m_rectangleWidth
Definition eda_shape.h:766
VECTOR2I m_bezierC2
Definition eda_shape.h:776
void Serialize(google::protobuf::Any &aContainer) const override
Serializes this object to the given Any message.
bool IsPolyShapeValid() const
int Compare(const EDA_SHAPE *aOther) const
VECTOR2I GetArcMid() const
NumericType MinorRadius
Definition ellipse.h:103
EDA_ANGLE Rotation
Definition ellipse.h:104
EDA_ANGLE StartAngle
Definition ellipse.h:105
NumericType MajorRadius
Definition ellipse.h:102
EDA_ANGLE EndAngle
Definition ellipse.h:106
VECTOR2< NumericType > Center
Definition ellipse.h:101
ENUM_MAP & Map(T aValue, const wxString &aName)
Definition property.h:817
static ENUM_MAP< T > & Instance()
Definition property.h:811
wxPGChoices & Choices()
Definition property.h:862
Class that other classes need to inherit from, in order to be inspectable.
Definition inspectable.h:39
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
Decorative shape (arrowhead, circle, square) at the start or end of a graphic line,...
Definition line_ending.h:62
int GetShortenDepth(int aLineWidth) const
Return how far the line should be shortened at this ending.
int GetCurveOrientationDepth(int aLineWidth) const
Return how far along a curved body to look when orienting this ending.
LINE_ENDING_STYLE GetStyle() const
Definition line_ending.h:81
void GetShapes(const VECTOR2I &aPoint, const EDA_ANGLE &aTangent, int aLineWidth, std::vector< VECTOR2I > &aPolygon) const
Generate ending geometry as polygon vertices at the given point and direction.
int GetStrokeWidth() const
Outline stroke width.
PROPERTY_BASE & SetAvailableFunc(std::function< bool(INSPECTABLE *)> aFunc)
Set a callback function to determine whether an object provides this property.
Definition property.h:275
PROPERTY_BASE & SetValidator(PROPERTY_VALIDATOR_FN &&aValidator)
Definition property.h:397
PROPERTY_BASE & SetIsCopyable(bool aIsCopyable=true)
Definition property.h:388
PROPERTY_BASE & SetIsHiddenFromRulesEditor(bool aHide=true)
Definition property.h:361
Provide class metadata.
static PROPERTY_MANAGER & Instance()
PROPERTY_BASE & AddProperty(PROPERTY_BASE *aProperty, const wxString &aGroup=wxEmptyString)
Register a property.
A round rectangle shape, based on a rectangle and a radius.
Definition roundrect.h:32
void TransformToPolygon(SHAPE_POLY_SET &aBuffer, int aMaxError) const
Get the polygonal representation of the roundrect.
Definition roundrect.cpp:79
Definition seg.h:38
const VECTOR2I ReflectPoint(const VECTOR2I &aP) const
Reflect a point using this segment as axis.
Definition seg.cpp:630
VECTOR2I A
Definition seg.h:45
VECTOR2I B
Definition seg.h:46
int Length() const
Return the length (this).
Definition seg.h:361
EDA_ANGLE GetCentralAngle() const
Get the "central angle" of the arc - this is the angle at the point of the "pie slice".
const VECTOR2I & GetArcMid() const
Definition shape_arc.h:116
EDA_ANGLE GetEndAngle() const
const VECTOR2I & GetP1() const
Definition shape_arc.h:115
EDA_ANGLE GetStartAngle() const
const VECTOR2I & GetP0() const
Definition shape_arc.h:114
SHAPE_TYPE Type() const
Return the type of the shape.
Definition shape.h:96
SHAPE_LINE_CHAIN ConvertToPolyline(int aMaxError) const
Build a polyline approximation of the ellipse or arc.
SEG::ecoord SquaredDistance(const VECTOR2I &aP, bool aOutlineOnly=false) const override
double GetLength() const
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
void Move(const VECTOR2I &aVector) override
const SHAPE_ARC & Arc(size_t aArc) const
bool IsClosed() const override
virtual const VECTOR2I GetPoint(int aIndex) const override
void SetPoint(int aIndex, const VECTOR2I &aPos)
Move a point to a specific location.
void SetClosed(bool aClosed)
Mark the line chain as closed (i.e.
int PointCount() const
Return the number of points (vertices) in this line chain.
ssize_t ArcIndex(size_t aSegment) const
Return the arc index for the given segment index.
SEG Segment(int aIndex) const
Return a copy of the aIndex-th segment in the line chain.
virtual size_t GetPointCount() const override
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
virtual const SEG GetSegment(int aIndex) const override
const VECTOR2I & CPoint(int aIndex) const
Return a reference to a given point in the line chain.
int SegmentCount() const
Return the number of segments in this line chain.
const VECTOR2I & CLastPoint() const
Return the last point in the line chain.
void Remove(int aStartIndex, int aEndIndex)
Remove the range of points [start_index, end_index] from the line chain.
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
bool IsArcSegment(size_t aSegment) const
const std::vector< VECTOR2I > & CPoints() const
Represent a set of closed polygons.
void Rotate(const EDA_ANGLE &aAngle, const VECTOR2I &aCenter={ 0, 0 }) override
Rotate all vertices by a given angle.
void RemoveAllContours()
Remove all outlines & holes (clears) the polygon set.
bool CollideEdge(const VECTOR2I &aPoint, VERTEX_INDEX *aClosestVertex=nullptr, int aClearance=0) const
Check whether aPoint collides with any edge of any of the contours of the polygon.
void ClearArcs()
Removes all arc references from all the outlines and holes in the polyset.
int AddOutline(const SHAPE_LINE_CHAIN &aOutline)
Adds a new outline to the set and returns its index.
int VertexCount(int aOutline=-1, int aHole=-1) const
Return the number of vertices in a given outline/hole.
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
bool Collide(const SHAPE *aShape, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const override
Check if the boundary of shape (this) lies closer to the shape aShape than aClearance,...
int TotalVertices() const
Return total number of vertices stored in the set.
void Inflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError, bool aSimplify=false)
Perform outline inflation/deflation.
int Append(int x, int y, int aOutline=-1, int aHole=-1, bool aAllowDuplication=false)
Appends a vertex at the end of the given outline/hole (default: the last outline)
const std::vector< SEG > GenerateHatchLines(const std::vector< double > &aSlopes, int aSpacing, int aLineLength) const
SHAPE_LINE_CHAIN & Outline(int aIndex)
Return the reference to aIndex-th outline in the set.
int NewOutline()
Creates a new empty polygon in the set and returns its index.
void Mirror(const VECTOR2I &aRef, FLIP_DIRECTION aFlipDirection)
Mirror the line points about y or x (or both)
const VECTOR2I & CVertex(int aIndex, int aOutline, int aHole) const
Return the index-th vertex in a given hole outline within a given outline.
int OutlineCount() const
Return the number of outlines in the set.
void Move(const VECTOR2I &aVector) override
void Fracture(bool aSimplify=true)
Convert a set of polygons with holes to a single outline with "slits"/"fractures" connecting the oute...
SHAPE_POLY_SET CloneDropTriangulation() const
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
const SHAPE_LINE_CHAIN & COutline(int aIndex) const
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
Represent a simple polygon consisting of a zero-thickness closed chain of connected line segments.
An abstract shape on 2D plane.
Definition shape.h:124
virtual bool Collide(const VECTOR2I &aP, int aClearance=0, int *aActual=nullptr, VECTOR2I *aLocation=nullptr) const
Check if the boundary of shape (this) lies closer to the point aP than aClearance,...
Definition shape.h:181
int GetWidth() const
LINE_STYLE GetLineStyle() const
wxString MessageTextFromValue(double aValue, bool aAddUnitLabel=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE) const
A lower-precision version of StringFromValue().
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
Definition vector2d.h:574
constexpr extended_type SquaredEuclideanNorm() const
Compute the squared euclidean norm of the vector, which is defined as (x ** 2 + y ** 2).
Definition vector2d.h:312
T EuclideanNorm() const
Compute the Euclidean norm of the vector, which is defined as sqrt(x ** 2 + y ** 2).
Definition vector2d.h:281
void TransformRingToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aCentre, int aRadius, int aWidth, int aError, ERROR_LOC aErrorLoc)
Convert arcs to multiple straight segments.
void TransformCircleToPolygon(SHAPE_LINE_CHAIN &aBuffer, const VECTOR2I &aCenter, int aRadius, int aError, ERROR_LOC aErrorLoc, int aMinSegCount=0)
Convert a circle to a polygon, using multiple straight lines.
void TransformArcToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd, int aWidth, int aError, ERROR_LOC aErrorLoc)
Convert arc to multiple straight segments.
void TransformRoundChamferedRectToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aPosition, const VECTOR2I &aSize, const EDA_ANGLE &aRotation, int aCornerRadius, double aChamferRatio, int aChamferCorners, int aInflate, int aError, ERROR_LOC aErrorLoc)
Convert a rectangle with rounded corners and/or chamfered corners to a polygon.
void TransformOvalToPolygon(SHAPE_POLY_SET &aBuffer, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aWidth, int aError, ERROR_LOC aErrorLoc, int aMinSegCount=0)
Convert a oblong shape to a polygon, using multiple segments.
@ ROUND_ALL_CORNERS
All angles are rounded.
#define UNSPECIFIED
#define _(s)
static constexpr EDA_ANGLE ANGLE_0
Definition eda_angle.h:472
static constexpr EDA_ANGLE ANGLE_90
Definition eda_angle.h:474
@ RADIANS_T
Definition eda_angle.h:31
@ DEGREES_T
Definition eda_angle.h:30
static constexpr EDA_ANGLE ANGLE_45
Definition eda_angle.h:473
static constexpr EDA_ANGLE ANGLE_270
Definition eda_angle.h:477
static constexpr EDA_ANGLE ANGLE_360
Definition eda_angle.h:478
static constexpr EDA_ANGLE ANGLE_180
Definition eda_angle.h:476
UI_FILL_MODE
Definition eda_fill.h:41
@ REVERSE_HATCH
Definition eda_fill.h:45
@ SOLID
Definition eda_fill.h:43
@ HATCH
Definition eda_fill.h:44
@ NONE
Definition eda_fill.h:42
@ CROSS_HATCH
Definition eda_fill.h:46
FILL_T
Definition eda_fill.h:29
@ NO_FILL
Definition eda_fill.h:30
@ REVERSE_HATCH
Definition eda_fill.h:35
@ HATCH
Definition eda_fill.h:34
@ CROSS_HATCH
Definition eda_fill.h:36
static void addLineEndingEffectiveShapes(std::vector< SHAPE * > &aShapes, const LINE_ENDING &aEnding, const VECTOR2I &aPoint, const EDA_ANGLE &aTangent, int aLineWidth)
static void addLineEndingPolygon(SHAPE_POLY_SET &aBuffer, const LINE_ENDING &aEnding, const VECTOR2I &aPoint, const EDA_ANGLE &aTangent, int aClearance, int aError, ERROR_LOC aErrorLoc, int aLineWidth)
#define TEST_PT(a, b)
static bool hasLineEnding(const LINE_ENDING &aStartEnding, const LINE_ENDING &aEndEnding)
#define TEST(a, b)
#define TEST_E(a, b)
static double bezierSpeedAt(const BEZIER< double > &aBezier, double aT)
static double findBezierTAtLength(const BEZIER< double > &aBezier, double aTargetLength, double aTotalLength)
static struct EDA_SHAPE_DESC _EDA_SHAPE_DESC
static double bezierLength(const BEZIER< double > &aBezier, double aT0, double aT1)
#define SWAPITEM(x)
#define sq(x)
static SHAPE_LINE_CHAIN lineEndingClosedChain(const std::vector< VECTOR2I > &aPolygon)
SHAPE_T
Definition eda_shape.h:55
@ UNDEFINED
Definition eda_shape.h:56
@ ELLIPSE
Definition eda_shape.h:63
@ SEGMENT
Definition eda_shape.h:57
@ RECTANGLE
Use RECTANGLE instead of RECT to avoid collision in a Windows header.
Definition eda_shape.h:58
@ ELLIPSE_ARC
Definition eda_shape.h:64
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition frame_type.h:29
@ FRAME_SCH_SYMBOL_EDITOR
Definition frame_type.h:31
a few functions useful in geometry calculations.
LINE_ENDING_STYLE
Line ending styles for graphic lines, arcs, and beziers.
Definition line_ending.h:44
This file contains miscellaneous commonly used macros and functions.
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition macros.h:79
#define UNIMPLEMENTED_FOR(type)
Definition macros.h:92
constexpr void MIRROR(T &aPoint, const T &aMirrorRef)
Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
Definition mirror.h:44
FLIP_DIRECTION
Definition mirror.h:26
KICOMMON_API wxString MessageTextFromValue(const EDA_IU_SCALE &aIuScale, EDA_UNITS aUnits, double aValue, bool aAddUnitsText=true, EDA_DATA_TYPE aType=EDA_DATA_TYPE::DISTANCE)
A helper to convert the double length aValue to a string in inches, millimeters, or unscaled units.
bool ShapeHitTest(const SHAPE_LINE_CHAIN &aHitter, const SHAPE &aHittee, bool aHitteeContained)
Perform a shape-to-shape hit test.
SHAPE_LINE_CHAIN BoxToLineChain(const BOX2I &aBox)
Get a SHAPE_LINE_CHAIN representing the outline of a box.
size_t longest_common_subset(const _Container &__c1, const _Container &__c2)
Returns the length of the longest common subset of values between two containers.
Definition kicad_algo.h:182
KICOMMON_API void PackLineEnding(types::LineEnding &aOutput, const LINE_ENDING &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackColor(types::Color &aOutput, const KIGFX::COLOR4D &aInput)
KICOMMON_API void UnpackStroke(STROKE_PARAMS &aOutput, const types::StrokeAttributes &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API int UnpackDistance(const types::Distance &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackPolySet(types::PolySet &aOutput, const SHAPE_POLY_SET &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API LINE_ENDING UnpackLineEnding(const types::LineEnding &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API KIGFX::COLOR4D UnpackColor(const types::Color &aInput)
KICOMMON_API VECTOR2I UnpackVector2(const types::Vector2 &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackDistance(types::Distance &aOutput, int aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackVector2(types::Vector2 &aOutput, const VECTOR2I &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API void PackStroke(types::StrokeAttributes &aOutput, const STROKE_PARAMS &aInput, const EDA_IU_SCALE &aScale)
KICOMMON_API SHAPE_POLY_SET UnpackPolySet(const types::PolySet &aInput, const EDA_IU_SCALE &aScale)
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:461
#define _HKI(x)
Definition page_info.cpp:40
#define IMPLEMENT_ENUM_TO_WXANY(type)
Definition property.h:916
#define NO_SETTER(owner, type)
Macro to define read-only fields (no setter method available)
Definition property.h:923
@ PT_COORD
Coordinate expressed in distance units (mm/inch)
Definition property.h:65
@ PT_DECIDEGREE
Angle expressed in decidegrees.
Definition property.h:67
@ PT_SIZE
Size expressed in distance units (mm/inch)
Definition property.h:63
#define REGISTER_TYPE(x)
Helper macro to map type hashes to names.
std::optional< std::unique_ptr< VALIDATION_ERROR > > VALIDATOR_RESULT
Null optional means validation succeeded.
@ SH_POLY_SET
set of polygons (with holes, etc.)
Definition shape.h:48
@ SH_CIRCLE
circle
Definition shape.h:46
@ SH_SIMPLE
simple polygon
Definition shape.h:47
@ SH_ELLIPSE
ellipse or elliptical arc
Definition shape.h:53
@ SH_NULL
empty shape (no shape...),
Definition shape.h:51
@ SH_SEGMENT
line segment
Definition shape.h:44
@ SH_ARC
circular arc
Definition shape.h:50
@ SH_POLY_SET_TRIANGLE
a single triangle belonging to a POLY_SET triangulation
Definition shape.h:52
@ SH_LINE_CHAIN
line chain (polyline)
Definition shape.h:45
@ SH_COMPOUND
compound shape, consisting of multiple simple shapes
Definition shape.h:49
static bool Collide(const SHAPE_CIRCLE &aA, const SHAPE_CIRCLE &aB, int aClearance, int *aActual, VECTOR2I *aLocation, VECTOR2I *aMTV)
Utility functions for working with shapes.
LINE_STYLE
Dashed line types.
VECTOR2I center
const SHAPE_LINE_CHAIN chain
int radius
VECTOR2I end
SHAPE_CIRCLE circle(c.m_circle_center, c.m_circle_radius)
int delta
bool TestSegmentHit(const VECTOR2I &aRefPoint, const VECTOR2I &aStart, const VECTOR2I &aEnd, int aDist)
Test if aRefPoint is with aDistance on the line defined by aStart and aEnd.
Definition trigo.cpp:173
void RotatePoint(int *pX, int *pY, const EDA_ANGLE &aAngle)
Calculate the new point of coord coord pX, pY, for a rotation center 0, 0.
Definition trigo.cpp:227
double RAD2DEG(double rad)
Definition trigo.h:179
bool IsStraightArc(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Definition trigo.cpp:549
const VECTOR2I CalcArcCenter(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Determine the center of an arc or circle given three points on its circumference.
Definition trigo.cpp:540
@ PCB_TEXTBOX_T
class PCB_TEXTBOX, wrapped text on a layer
Definition typeinfo.h:85
@ PCB_TABLECELL_T
class PCB_TABLECELL, PCB_TEXTBOX for use in tables
Definition typeinfo.h:87
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707
static PIECE rect(double aLeft, double aTop, double aRight, double aBottom, bool aMixed=false)
static PIECE polygon(std::vector< VIA_CROSS_SECTION_GEOMETRY::POINT > aPoints, bool aMixed=false)