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 <eda_units.h>
29 #include <convert_to_biu.h>
30 #include <trigo.h>
33 
34 class LINE_READER;
35 class EDA_DRAW_FRAME;
36 class FOOTPRINT;
37 class MSG_PANEL_ITEM;
38 
39 
40 enum 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
53 enum 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
63 struct ARC_MID
64 {
65  wxPoint mid;
66  wxPoint start;
67  wxPoint end;
68  wxPoint center;
69 };
70 
71 class EDA_SHAPE
72 {
73 public:
74  EDA_SHAPE( SHAPE_T aType, int aLineWidth, FILL_T aFill, bool eeWinding );
75 
76  // Do not create a copy constructor & operator=.
77  // The ones generated by the compiler are adequate.
78 
79  ~EDA_SHAPE();
80 
81  void SwapShape( EDA_SHAPE* aImage );
82 
83  wxString ShowShape() const;
84 
85  wxString SHAPE_T_asString() const;
86 
87  void SetFillMode( FILL_T aFill ) { m_fill = aFill; }
88  FILL_T GetFillType() const { return m_fill; }
89 
90  bool IsFilled() const { return GetFillType() != FILL_T::NO_FILL; }
91 
92  void SetFilled( bool aFlag )
93  {
95  }
96 
97  void SetWidth( int aWidth ) { m_width = aWidth; }
98  int GetWidth() const { return m_width; }
99 
100  void SetShape( SHAPE_T aShape ) { m_shape = aShape; }
101  SHAPE_T GetShape() const { return m_shape; }
102 
106  const wxPoint& GetStart() const { return m_start; }
107  int GetStartY() { return m_start.y; }
108  int GetStartX() { return m_start.x; }
109 
110  void SetStart( const wxPoint& aStart )
111  {
112  m_start = aStart;
113  m_endsSwapped = false;
114  }
115 
116  void SetStartY( int y )
117  {
118  m_start.y = y;
119  m_endsSwapped = false;
120  }
121 
122  void SetStartX( int x )
123  {
124  m_start.x = x;
125  m_endsSwapped = false;
126  }
127 
131  const wxPoint& GetEnd() const { return m_end; }
132  int GetEndY() { return m_end.y; }
133  int GetEndX() { return m_end.x; }
134 
135  void SetEnd( const wxPoint& aEnd )
136  {
137  m_end = aEnd;
138  m_endsSwapped = false;
139  }
140 
141  void SetEndY( int y )
142  {
143  m_end.y = y;
144  m_endsSwapped = false;
145  }
146 
147  void SetEndX( int x )
148  {
149  m_end.x = x;
150  m_endsSwapped = false;
151  }
152 
153  void SetBezierC1( const wxPoint& aPt ) { m_bezierC1 = aPt; }
154  const wxPoint& GetBezierC1() const { return m_bezierC1; }
155 
156  void SetBezierC2( const wxPoint& aPt ) { m_bezierC2 = aPt; }
157  const wxPoint& GetBezierC2() const { return m_bezierC2; }
158 
159  wxPoint getCenter() const;
160  void SetCenter( const wxPoint& aCenter );
161 
167  void SetArcAngleAndEnd( double aAngle, bool aCheckNegativeAngle = false );
168 
169  double GetArcAngle() const;
170 
175  bool EndsSwapped() const { return m_endsSwapped; }
176 
177  // Some attributes are read only, since they are derived from m_Start, m_End, and m_Angle.
178  // No Set...() function for these attributes.
179 
180  wxPoint GetArcMid() const;
181  std::vector<wxPoint> GetRectCorners() const;
182 
187  void CalcArcAngles( double& aStartAngle, double& aEndAngle ) const;
188 
189  int GetRadius() const;
190 
197  void SetArcGeometry( const wxPoint& aStart, const wxPoint& aMid, const wxPoint& aEnd );
198 
208  void SetCachedArcData( const wxPoint& aStart, const wxPoint& aMid, const wxPoint& aEnd, const wxPoint& aCenter );
209 
210  const std::vector<wxPoint>& GetBezierPoints() const { return m_bezierPoints; }
211 
219  void DupPolyPointsList( std::vector<wxPoint>& aBuffer ) const;
220 
224  int GetPointCount() const;
225 
226  // Accessors to the polygonal shape
228  const SHAPE_POLY_SET& GetPolyShape() const { return m_poly; }
229 
233  bool IsPolyShapeValid() const;
234 
235  void SetPolyShape( const SHAPE_POLY_SET& aShape ) { m_poly = aShape; }
236 
237  void SetPolyPoints( const std::vector<wxPoint>& aPoints );
238 
249  void RebuildBezierToSegmentsPointsList( int aMinSegLen );
250 
254  // fixme: move to shape_compound
255  std::vector<SHAPE*> MakeEffectiveShapes() const;
256 
257  void ShapeGetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList );
258 
264  double GetLength() const;
265 
278  void TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer, int aClearanceValue,
279  int aError, ERROR_LOC aErrorLoc,
280  bool ignoreLineWidth ) const;
281 
282  int Compare( const EDA_SHAPE* aOther ) const;
283 
284 protected:
285  void setPosition( const wxPoint& aPos );
286  wxPoint getPosition() const;
287 
288  void move( const wxPoint& aMoveVector );
289  void rotate( const wxPoint& aRotCentre, double aAngle );
290  void flip( const wxPoint& aCentre, bool aFlipLeftRight );
291  void scale( double aScale );
292 
293  // To be implemented by concrete classes
294  virtual double getParentOrientation() const = 0;
295  virtual wxPoint getParentPosition() const = 0;
296 
297  const EDA_RECT getBoundingBox() const;
298 
299  void computeArcBBox( EDA_RECT& aBBox ) const;
300 
301  bool hitTest( const wxPoint& aPosition, int aAccuracy = 0 ) const;
302  bool hitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy = 0 ) const;
303 
304  const std::vector<wxPoint> buildBezierToSegmentsPointsList( int aMinSegLen ) const;
305 
306  void beginEdit( const wxPoint& aStartPoint );
307  bool continueEdit( const wxPoint& aPosition );
308  void calcEdit( const wxPoint& aPosition );
309 
314  void endEdit( bool aClosed = true );
315  void setEditState( int aState ) { m_editState = aState; }
316 
317 protected:
318  bool m_endsSwapped; // true if start/end were swapped e.g. SetArcAngleAndEnd
319  SHAPE_T m_shape; // Shape: line, Circle, Arc
320  int m_width; // thickness of lines ...
322  wxPoint m_start; // Line start point or Circle center
323  wxPoint m_end; // Line end point or Circle 3 o'clock point
324 
325  wxPoint m_arcCenter; // Used only for Arcs: arc end point
326  ARC_MID m_arcMidData; // Used to store originating data
327 
328  wxPoint m_bezierC1; // Bezier Control Point 1
329  wxPoint m_bezierC2; // Bezier Control Point 2
330 
331  std::vector<wxPoint> m_bezierPoints;
332  SHAPE_POLY_SET m_poly; // Stores the S_POLYGON shape
333 
335  bool m_eeWinding; // Awful hack
336 };
337 
338 #endif // EDA_SHAPE_H
int GetStartY()
Definition: eda_shape.h:107
SHAPE_T m_shape
Definition: eda_shape.h:319
An abstract class from which implementation specific LINE_READERs may be derived to read single lines...
Definition: richio.h:80
const std::vector< wxPoint > & GetBezierPoints() const
Definition: eda_shape.h:210
int GetWidth() const
Definition: eda_shape.h:98
wxPoint m_bezierC1
Definition: eda_shape.h:328
void SetFilled(bool aFlag)
Definition: eda_shape.h:92
void SetEnd(const wxPoint &aEnd)
Definition: eda_shape.h:135
void SetArcGeometry(const wxPoint &aStart, const wxPoint &aMid, const wxPoint &aEnd)
Set the three controlling points for an arc.
Definition: eda_shape.cpp:504
marker for list end
wxPoint getCenter() const
Definition: eda_shape.cpp:393
const wxPoint & GetStart() const
Return the starting point of the graphic.
Definition: eda_shape.h:106
void SetPolyShape(const SHAPE_POLY_SET &aShape)
Definition: eda_shape.h:235
bool hitTest(const wxPoint &aPosition, int aAccuracy=0) const
Definition: eda_shape.cpp:695
void SetStartY(int y)
Definition: eda_shape.h:116
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:1502
int GetEndY()
Definition: eda_shape.h:132
void calcEdit(const wxPoint &aPosition)
Definition: eda_shape.cpp:1291
void computeArcBBox(EDA_RECT &aBBox) const
Definition: eda_shape.cpp:1017
bool EndsSwapped() const
Have the start and end points been swapped since they were set?
Definition: eda_shape.h:175
EDA_SHAPE(SHAPE_T aType, int aLineWidth, FILL_T aFill, bool eeWinding)
Definition: eda_shape.cpp:40
std::vector< SHAPE * > MakeEffectiveShapes() const
Make a set of SHAPE objects representing the EDA_SHAPE.
Definition: eda_shape.cpp:1095
void DupPolyPointsList(std::vector< wxPoint > &aBuffer) const
Duplicate the list of corners in a std::vector<wxPoint>
Definition: eda_shape.cpp:1193
double GetArcAngle() const
Definition: eda_shape.cpp:536
void CalcArcAngles(double &aStartAngle, double &aEndAngle) const
Calc arc start and end angles such that aStartAngle < aEndAngle.
Definition: eda_shape.cpp:451
virtual wxPoint getParentPosition() const =0
void setEditState(int aState)
Definition: eda_shape.h:315
void SetArcAngleAndEnd(double aAngle, bool aCheckNegativeAngle=false)
Set the end point from the angle center and start.
Definition: eda_shape.cpp:547
void beginEdit(const wxPoint &aStartPoint)
Definition: eda_shape.cpp:1233
void SetEndY(int y)
Definition: eda_shape.h:141
void SetCachedArcData(const wxPoint &aStart, const wxPoint &aMid, const wxPoint &aEnd, const wxPoint &aCenter)
Set the data used for mid point caching.
Definition: eda_shape.cpp:495
void ShapeGetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList)
Definition: eda_shape.cpp:560
The base class for create windows for drawing purpose.
wxString ShowShape() const
Definition: eda_shape.cpp:56
bool IsPolyShapeValid() const
Definition: eda_shape.cpp:1210
void endEdit(bool aClosed=true)
Finishes editing the shape.
Definition: eda_shape.cpp:1416
int GetStartX()
Definition: eda_shape.h:108
void SetBezierC1(const wxPoint &aPt)
Definition: eda_shape.h:153
wxPoint m_end
Definition: eda_shape.h:323
FILL_T
Definition: eda_shape.h:53
wxPoint m_arcCenter
Definition: eda_shape.h:325
ERROR_LOC
When approximating an arc or circle, should the error be placed on the outside or inside of the curve...
int GetEndX()
Definition: eda_shape.h:133
wxPoint m_bezierC2
Definition: eda_shape.h:329
bool IsFilled() const
Definition: eda_shape.h:90
void SetStart(const wxPoint &aStart)
Definition: eda_shape.h:110
const wxPoint & GetBezierC1() const
Definition: eda_shape.h:154
void rotate(const wxPoint &aRotCentre, double aAngle)
Definition: eda_shape.cpp:224
int GetPointCount() const
Definition: eda_shape.cpp:1222
std::vector< wxPoint > GetRectCorners() const
Definition: eda_shape.cpp:981
bool m_endsSwapped
Definition: eda_shape.h:318
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:131
double GetLength() const
Return the length of the track using the hypotenuse calculation.
Definition: eda_shape.cpp:106
const SHAPE_POLY_SET & GetPolyShape() const
Definition: eda_shape.h:228
void move(const wxPoint &aMoveVector)
Definition: eda_shape.cpp:137
Represent a set of closed polygons.
SHAPE_T
Definition: eda_shape.h:40
std::vector< wxPoint > m_bezierPoints
Definition: eda_shape.h:331
SHAPE_POLY_SET m_poly
Definition: eda_shape.h:332
const wxPoint & GetBezierC2() const
Definition: eda_shape.h:157
int m_editState
Definition: eda_shape.h:334
FILL_T m_fill
Definition: eda_shape.h:321
const std::vector< wxPoint > buildBezierToSegmentsPointsList(int aMinSegLen) const
Definition: eda_shape.cpp:380
void SetWidth(int aWidth)
Definition: eda_shape.h:97
a few functions useful in geometry calculations.
void SetCenter(const wxPoint &aCenter)
Definition: eda_shape.cpp:419
wxString SHAPE_T_asString() const
Definition: eda_shape.cpp:71
void setPosition(const wxPoint &aPos)
Definition: eda_shape.cpp:89
int Compare(const EDA_SHAPE *aOther) const
Definition: eda_shape.cpp:1465
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:227
wxPoint m_start
Definition: eda_shape.h:322
wxPoint mid
Definition: eda_shape.h:65
void SetEndX(int x)
Definition: eda_shape.h:147
void RebuildBezierToSegmentsPointsList(int aMinSegLen)
Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of segments.
Definition: eda_shape.cpp:366
const EDA_RECT getBoundingBox() const
Definition: eda_shape.cpp:634
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
Definition: eda_shape.cpp:1085
bool m_eeWinding
Definition: eda_shape.h:335
virtual double getParentOrientation() const =0
void SwapShape(EDA_SHAPE *aImage)
Definition: eda_shape.cpp:1448
wxPoint GetArcMid() const
Definition: eda_shape.cpp:437
Handle the component boundary box.
Definition: eda_rect.h:42
void SetShape(SHAPE_T aShape)
Definition: eda_shape.h:100
wxPoint start
Definition: eda_shape.h:66
void SetBezierC2(const wxPoint &aPt)
Definition: eda_shape.h:156
wxPoint getPosition() const
Definition: eda_shape.cpp:95
wxPoint center
Definition: eda_shape.h:68
wxPoint end
Definition: eda_shape.h:67
ARC_MID m_arcMidData
Definition: eda_shape.h:326
void SetFillMode(FILL_T aFill)
Definition: eda_shape.h:87
SHAPE_T GetShape() const
Definition: eda_shape.h:101
void scale(double aScale)
Definition: eda_shape.cpp:172
EDA_MSG_PANEL items for displaying messages.
Definition: msgpanel.h:53
int m_width
Definition: eda_shape.h:320
int GetRadius() const
Definition: eda_shape.cpp:472
void flip(const wxPoint &aCentre, bool aFlipLeftRight)
Definition: eda_shape.cpp:281
bool continueEdit(const wxPoint &aPosition)
Definition: eda_shape.cpp:1264
FILL_T GetFillType() const
Definition: eda_shape.h:88
void SetStartX(int x)
Definition: eda_shape.h:122