KiCad PCB EDA Suite
eda_shape.h
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) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#ifndef EDA_SHAPE_H
26#define EDA_SHAPE_H
27
28#include <trigo.h>
31#include <stroke_params.h>
32
33class LINE_READER;
34class EDA_DRAW_FRAME;
35class FOOTPRINT;
36class MSG_PANEL_ITEM;
37
38using KIGFX::COLOR4D;
39
40enum class SHAPE_T : int
41{
42 SEGMENT = 0,
43 RECT,
44 ARC,
45 CIRCLE,
46 POLY,
47 BEZIER,
48 LAST
49};
50
51
52// WARNING: Do not change these values without updating dialogs that depend on their position values
53enum class FILL_T : int
54{
55 NO_FILL = 1,
56 FILLED_SHAPE, // Fill with object color
57 FILLED_WITH_BG_BODYCOLOR, // Fill with background body color
58 FILLED_WITH_COLOR // Fill with a separate color
59};
60
61
62// Holding struct to keep originating midpoint
63struct ARC_MID
64{
69};
70
72{
73public:
74 EDA_SHAPE( SHAPE_T aType, int aLineWidth, FILL_T aFill );
75
76 // Do not create a copy constructor & operator=.
77 // The ones generated by the compiler are adequate.
78
79 virtual ~EDA_SHAPE();
80
81 void SwapShape( EDA_SHAPE* aImage );
82
83 wxString ShowShape() const;
84
85 wxString SHAPE_T_asString() const;
86
87 bool IsAnnotationProxy() const { return m_annotationProxy; }
88 void SetIsAnnotationProxy( bool aIsProxy = true ) { m_annotationProxy = aIsProxy; }
89
90 bool IsFilled() const
91 {
92 return GetFillMode() != FILL_T::NO_FILL;
93 }
94
95 void SetFilled( bool aFlag )
96 {
98 }
99
100 void SetFillMode( FILL_T aFill ) { m_fill = aFill; }
101 FILL_T GetFillMode() const { return m_fill; }
102
103 COLOR4D GetFillColor() const { return m_fillColor; }
104 void SetFillColor( const COLOR4D& aColor ) { m_fillColor = aColor; }
105
106 void SetWidth( int aWidth ) { m_stroke.SetWidth( aWidth ); }
107 int GetWidth() const { return m_stroke.GetWidth(); }
108 virtual int GetEffectiveWidth() const { return GetWidth(); }
109
110 void SetShape( SHAPE_T aShape ) { m_shape = aShape; }
111 SHAPE_T GetShape() const { return m_shape; }
112
116 const VECTOR2I& GetStart() const { return m_start; }
117 int GetStartY() const { return m_start.y; }
118 int GetStartX() const { return m_start.x; }
119
120 void SetStart( const VECTOR2I& aStart )
121 {
122 m_start = aStart;
123 m_endsSwapped = false;
124 }
125
126 void SetStartY( int y )
127 {
128 m_start.y = y;
129 m_endsSwapped = false;
130 }
131
132 void SetStartX( int x )
133 {
134 m_start.x = x;
135 m_endsSwapped = false;
136 }
137
141 const VECTOR2I& GetEnd() const { return m_end; }
142 int GetEndY() const { return m_end.y; }
143 int GetEndX() const { return m_end.x; }
144
145 void SetEnd( const VECTOR2I& aEnd )
146 {
147 m_end = aEnd;
148 m_endsSwapped = false;
149 }
150
151 void SetEndY( int y )
152 {
153 m_end.y = y;
154 m_endsSwapped = false;
155 }
156
157 void SetEndX( int x )
158 {
159 m_end.x = x;
160 m_endsSwapped = false;
161 }
162
163 virtual VECTOR2I GetTopLeft() const { return GetStart(); }
164 virtual VECTOR2I GetBotRight() const { return GetEnd(); }
165
166 virtual void SetTop( int val ) { SetStartY( val ); }
167 virtual void SetLeft( int val ) { SetStartX( val ); }
168 virtual void SetRight( int val ) { SetEndX( val ); }
169 virtual void SetBottom( int val ) { SetEndY( val ); }
170
171 void SetBezierC1( const VECTOR2I& aPt ) { m_bezierC1 = aPt; }
172 const VECTOR2I& GetBezierC1() const { return m_bezierC1; }
173
174 void SetBezierC2( const VECTOR2I& aPt ) { m_bezierC2 = aPt; }
175 const VECTOR2I& GetBezierC2() const { return m_bezierC2; }
176
177 VECTOR2I getCenter() const;
178 void SetCenter( const VECTOR2I& aCenter );
179
183 void SetArcAngleAndEnd( const EDA_ANGLE& aAngle, bool aCheckNegativeAngle = false );
184
185 EDA_ANGLE GetArcAngle() const;
186
191 bool EndsSwapped() const { return m_endsSwapped; }
192
193 // Some attributes are read only, since they are derived from m_Start, m_End, and m_Angle.
194 // No Set...() function for these attributes.
195
196 VECTOR2I GetArcMid() const;
197 std::vector<VECTOR2I> GetRectCorners() const;
198
203 void CalcArcAngles( EDA_ANGLE& aStartAngle, EDA_ANGLE& aEndAngle ) const;
204
205 int GetRadius() const;
206
213 void SetArcGeometry( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
214
224 void SetCachedArcData( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd, const VECTOR2I& aCenter );
225
226 const std::vector<VECTOR2I>& GetBezierPoints() const { return m_bezierPoints; }
227
235 void DupPolyPointsList( std::vector<VECTOR2I>& aBuffer ) const;
236
240 int GetPointCount() const;
241
242 // Accessors to the polygonal shape
244 const SHAPE_POLY_SET& GetPolyShape() const { return m_poly; }
245
249 bool IsPolyShapeValid() const;
250
251 void SetPolyShape( const SHAPE_POLY_SET& aShape )
252 {
253 m_poly = aShape;
254
255 for( int ii = 0; ii < m_poly.OutlineCount(); ++ii )
256 {
257 if( m_poly.HoleCount( ii ) )
258 {
260 break;
261 }
262 }
263 }
264
265 void SetPolyPoints( const std::vector<VECTOR2I>& aPoints );
266
277 void RebuildBezierToSegmentsPointsList( int aMinSegLen );
278
285 virtual std::vector<SHAPE*> MakeEffectiveShapes( bool aEdgeOnly = false ) const
286 {
287 return makeEffectiveShapes( aEdgeOnly );
288 }
289
290 void ShapeGetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList );
291
297 double GetLength() const;
298
311 void TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer, int aClearanceValue,
312 int aError, ERROR_LOC aErrorLoc,
313 bool ignoreLineWidth ) const;
314
315 int Compare( const EDA_SHAPE* aOther ) const;
316
317protected:
318 void setPosition( const VECTOR2I& aPos );
319 VECTOR2I getPosition() const;
320
321 void move( const VECTOR2I& aMoveVector );
322 void rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle );
323 void flip( const VECTOR2I& aCentre, bool aFlipLeftRight );
324 void scale( double aScale );
325
326 // To be implemented by concrete classes
327 virtual EDA_ANGLE getParentOrientation() const = 0;
328 virtual VECTOR2I getParentPosition() const = 0;
329
330 const BOX2I getBoundingBox() const;
331
332 void computeArcBBox( BOX2I& aBBox ) const;
333
334 bool hitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const;
335 bool hitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const;
336
337 const std::vector<VECTOR2I> buildBezierToSegmentsPointsList( int aMinSegLen ) const;
338
339 void beginEdit( const VECTOR2I& aStartPoint );
340 bool continueEdit( const VECTOR2I& aPosition );
341 void calcEdit( const VECTOR2I& aPosition );
342
347 void endEdit( bool aClosed = true );
348 void setEditState( int aState ) { m_editState = aState; }
349
358 // fixme: move to shape_compound
359 std::vector<SHAPE*> makeEffectiveShapes( bool aEdgeOnly, bool aLineChainOnly = false ) const;
360
361protected:
362 bool m_endsSwapped; // true if start/end were swapped e.g. SetArcAngleAndEnd
363 SHAPE_T m_shape; // Shape: line, Circle, Arc
364 STROKE_PARAMS m_stroke; // Line style, width, etc.
367
368 VECTOR2I m_start; // Line start point or Circle center
369 VECTOR2I m_end; // Line end point or Circle 3 o'clock point
370
371 VECTOR2I m_arcCenter; // Used only for Arcs: arc end point
372 ARC_MID m_arcMidData; // Used to store originating data
373
374 VECTOR2I m_bezierC1; // Bezier Control Point 1
375 VECTOR2I m_bezierC2; // Bezier Control Point 2
376
377 std::vector<VECTOR2I> m_bezierPoints;
378 SHAPE_POLY_SET m_poly; // Stores the S_POLYGON shape
379
381 bool m_annotationProxy; // A shape storing the position of an annotation
382};
383
384#endif // EDA_SHAPE_H
The base class for create windows for drawing purpose.
EDA_ANGLE GetArcAngle() const
Definition: eda_shape.cpp:538
SHAPE_T m_shape
Definition: eda_shape.h:363
virtual VECTOR2I getParentPosition() const =0
void SetStartX(int x)
Definition: eda_shape.h:132
bool m_endsSwapped
Definition: eda_shape.h:362
const VECTOR2I & GetBezierC2() const
Definition: eda_shape.h:175
void SetBezierC2(const VECTOR2I &aPt)
Definition: eda_shape.h:174
virtual EDA_ANGLE getParentOrientation() const =0
void move(const VECTOR2I &aMoveVector)
Definition: eda_shape.cpp:141
void SetCenter(const VECTOR2I &aCenter)
Definition: eda_shape.cpp:423
VECTOR2I getCenter() const
Definition: eda_shape.cpp:397
int GetStartY() const
Definition: eda_shape.h:117
int m_editState
Definition: eda_shape.h:380
virtual VECTOR2I GetTopLeft() const
Definition: eda_shape.h:163
void rotate(const VECTOR2I &aRotCentre, const EDA_ANGLE &aAngle)
Definition: eda_shape.cpp:228
void flip(const VECTOR2I &aCentre, bool aFlipLeftRight)
Definition: eda_shape.cpp:285
FILL_T GetFillMode() const
Definition: eda_shape.h:101
virtual ~EDA_SHAPE()
Definition: eda_shape.cpp:52
void SetEndY(int y)
Definition: eda_shape.h:151
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of segments.
Definition: eda_shape.cpp:370
virtual int GetEffectiveWidth() const
Definition: eda_shape.h:108
int GetEndX() const
Definition: eda_shape.h:143
const std::vector< VECTOR2I > buildBezierToSegmentsPointsList(int aMinSegLen) const
Definition: eda_shape.cpp:384
void calcEdit(const VECTOR2I &aPosition)
Definition: eda_shape.cpp:1268
void SetStartY(int y)
Definition: eda_shape.h:126
virtual std::vector< SHAPE * > MakeEffectiveShapes(bool aEdgeOnly=false) const
Make a set of SHAPE objects representing the EDA_SHAPE.
Definition: eda_shape.h:285
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:243
bool IsFilled() const
Definition: eda_shape.h:90
void CalcArcAngles(EDA_ANGLE &aStartAngle, EDA_ANGLE &aEndAngle) const
Calc arc start and end angles such that aStartAngle < aEndAngle.
Definition: eda_shape.cpp:455
void ShapeGetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList)
Definition: eda_shape.cpp:564
void SetFilled(bool aFlag)
Definition: eda_shape.h:95
int GetRadius() const
Definition: eda_shape.cpp:476
SHAPE_T GetShape() const
Definition: eda_shape.h:111
void SetPolyShape(const SHAPE_POLY_SET &aShape)
Definition: eda_shape.h:251
virtual VECTOR2I GetBotRight() const
Definition: eda_shape.h:164
virtual void SetBottom(int val)
Definition: eda_shape.h:169
VECTOR2I m_arcCenter
Definition: eda_shape.h:371
virtual void SetTop(int val)
Definition: eda_shape.h:166
bool m_annotationProxy
Definition: eda_shape.h:381
bool continueEdit(const VECTOR2I &aPosition)
Definition: eda_shape.cpp:1241
wxString ShowShape() const
Definition: eda_shape.cpp:57
ARC_MID m_arcMidData
Definition: eda_shape.h:372
void SetFillColor(const COLOR4D &aColor)
Definition: eda_shape.h:104
int GetEndY() const
Definition: eda_shape.h:142
std::vector< SHAPE * > makeEffectiveShapes(bool aEdgeOnly, bool aLineChainOnly=false) const
Make a set of SHAPE objects representing the EDA_SHAPE.
Definition: eda_shape.cpp:1079
bool hitTest(const VECTOR2I &aPosition, int aAccuracy=0) const
Definition: eda_shape.cpp:696
void SetCachedArcData(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd, const VECTOR2I &aCenter)
Set the data used for mid point caching.
Definition: eda_shape.cpp:499
EDA_SHAPE(SHAPE_T aType, int aLineWidth, FILL_T aFill)
Definition: eda_shape.cpp:40
void beginEdit(const VECTOR2I &aStartPoint)
Definition: eda_shape.cpp:1210
void SetEndX(int x)
Definition: eda_shape.h:157
VECTOR2I m_start
Definition: eda_shape.h:368
int GetPointCount() const
Definition: eda_shape.cpp:1199
const VECTOR2I & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:141
void SetStart(const VECTOR2I &aStart)
Definition: eda_shape.h:120
void DupPolyPointsList(std::vector< VECTOR2I > &aBuffer) const
Duplicate the list of corners in a std::vector<VECTOR2I>
Definition: eda_shape.cpp:1170
void endEdit(bool aClosed=true)
Finishes editing the shape.
Definition: eda_shape.cpp:1396
const VECTOR2I & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:116
COLOR4D GetFillColor() const
Definition: eda_shape.h:103
void SetShape(SHAPE_T aShape)
Definition: eda_shape.h:110
void SwapShape(EDA_SHAPE *aImage)
Definition: eda_shape.cpp:1428
std::vector< VECTOR2I > GetRectCorners() const
Definition: eda_shape.cpp:983
std::vector< VECTOR2I > m_bezierPoints
Definition: eda_shape.h:377
int GetWidth() const
Definition: eda_shape.h:107
void setPosition(const VECTOR2I &aPos)
Definition: eda_shape.cpp:93
void TransformShapeWithClearanceToPolygon(SHAPE_POLY_SET &aCornerBuffer, int aClearanceValue, int aError, ERROR_LOC aErrorLoc, bool ignoreLineWidth) const
Convert the shape to a closed polygon.
Definition: eda_shape.cpp:1492
const std::vector< VECTOR2I > & GetBezierPoints() const
Definition: eda_shape.h:226
void computeArcBBox(BOX2I &aBBox) const
Definition: eda_shape.cpp:1019
void SetEnd(const VECTOR2I &aEnd)
Definition: eda_shape.h:145
bool EndsSwapped() const
Have the start and end points been swapped since they were set?
Definition: eda_shape.h:191
const SHAPE_POLY_SET & GetPolyShape() const
Definition: eda_shape.h:244
void SetBezierC1(const VECTOR2I &aPt)
Definition: eda_shape.h:171
void SetArcGeometry(const VECTOR2I &aStart, const VECTOR2I &aMid, const VECTOR2I &aEnd)
Set the three controlling points for an arc.
Definition: eda_shape.cpp:508
virtual void SetLeft(int val)
Definition: eda_shape.h:167
double GetLength() const
Return the length of the track using the hypotenuse calculation.
Definition: eda_shape.cpp:110
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:75
void scale(double aScale)
Definition: eda_shape.cpp:176
int GetStartX() const
Definition: eda_shape.h:118
const VECTOR2I & GetBezierC1() const
Definition: eda_shape.h:172
VECTOR2I m_end
Definition: eda_shape.h:369
const BOX2I getBoundingBox() const
Definition: eda_shape.cpp:635
void SetArcAngleAndEnd(const EDA_ANGLE &aAngle, bool aCheckNegativeAngle=false)
Set the end point from the angle center and start.
Definition: eda_shape.cpp:549
SHAPE_POLY_SET m_poly
Definition: eda_shape.h:378
VECTOR2I getPosition() const
Definition: eda_shape.cpp:99
STROKE_PARAMS m_stroke
Definition: eda_shape.h:364
void setEditState(int aState)
Definition: eda_shape.h:348
bool IsAnnotationProxy() const
Definition: eda_shape.h:87
void SetIsAnnotationProxy(bool aIsProxy=true)
Definition: eda_shape.h:88
void SetPolyPoints(const std::vector< VECTOR2I > &aPoints)
Definition: eda_shape.cpp:1069
VECTOR2I m_bezierC1
Definition: eda_shape.h:374
FILL_T m_fill
Definition: eda_shape.h:365
COLOR4D m_fillColor
Definition: eda_shape.h:366
void SetWidth(int aWidth)
Definition: eda_shape.h:106
virtual void SetRight(int val)
Definition: eda_shape.h:168
void SetFillMode(FILL_T aFill)
Definition: eda_shape.h:100
VECTOR2I m_bezierC2
Definition: eda_shape.h:375
bool IsPolyShapeValid() const
Definition: eda_shape.cpp:1187
int Compare(const EDA_SHAPE *aOther) const
Definition: eda_shape.cpp:1451
VECTOR2I GetArcMid() const
Definition: eda_shape.cpp:441
A color representation with 4 components: red, green, blue, alpha.
Definition: color4d.h:104
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:81
EDA_MSG_PANEL items for displaying messages.
Definition: msgpanel.h:54
Represent a set of closed polygons.
void Fracture(POLYGON_MODE aFastMode)
Convert a single outline slitted ("fractured") polygon into a set ouf outlines with holes.
int HoleCount(int aOutline) const
Return the reference to aIndex-th outline in the set.
int OutlineCount() const
Return the number of vertices in a given outline/hole.
Simple container to manage line stroke parameters.
Definition: stroke_params.h:88
int GetWidth() const
Definition: stroke_params.h:98
void SetWidth(int aWidth)
Definition: stroke_params.h:99
SHAPE_T
Definition: eda_shape.h:41
@ LAST
marker for list end
FILL_T
Definition: eda_shape.h:54
@ FILLED_WITH_COLOR
@ FILLED_WITH_BG_BODYCOLOR
@ FILLED_SHAPE
a few functions useful in geometry calculations.
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
VECTOR2I end
Definition: eda_shape.h:67
VECTOR2I center
Definition: eda_shape.h:68
VECTOR2I start
Definition: eda_shape.h:66
VECTOR2I mid
Definition: eda_shape.h:65