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-2021 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 class EDA_SHAPE
63 {
64 public:
65  EDA_SHAPE( SHAPE_T aType, int aLineWidth, FILL_T aFill, bool eeWinding );
66 
67  // Do not create a copy constructor & operator=.
68  // The ones generated by the compiler are adequate.
69 
70  ~EDA_SHAPE();
71 
72  void SwapShape( EDA_SHAPE* aImage );
73 
74  wxString ShowShape() const;
75 
76  wxString SHAPE_T_asString() const;
77 
78  void SetFillMode( FILL_T aFill ) { m_fill = aFill; }
79  FILL_T GetFillType() const { return m_fill; }
80 
81  bool IsFilled() const { return GetFillType() != FILL_T::NO_FILL; }
82 
83  void SetFilled( bool aFlag )
84  {
86  }
87 
88  void SetWidth( int aWidth ) { m_width = aWidth; }
89  int GetWidth() const { return m_width; }
90 
91  void SetShape( SHAPE_T aShape ) { m_shape = aShape; }
92  SHAPE_T GetShape() const { return m_shape; }
93 
97  const wxPoint& GetStart() const { return m_start; }
98  int GetStartY() { return m_start.y; }
99  int GetStartX() { return m_start.x; }
100 
101  void SetStart( const wxPoint& aStart )
102  {
103  m_start = aStart;
104  m_endsSwapped = false;
105  }
106 
107  void SetStartY( int y )
108  {
109  m_start.y = y;
110  m_endsSwapped = false;
111  }
112 
113  void SetStartX( int x )
114  {
115  m_start.x = x;
116  m_endsSwapped = false;
117  }
118 
122  const wxPoint& GetEnd() const { return m_end; }
123  int GetEndY() { return m_end.y; }
124  int GetEndX() { return m_end.x; }
125 
126  void SetEnd( const wxPoint& aEnd )
127  {
128  m_end = aEnd;
129  m_endsSwapped = false;
130  }
131 
132  void SetEndY( int y )
133  {
134  m_end.y = y;
135  m_endsSwapped = false;
136  }
137 
138  void SetEndX( int x )
139  {
140  m_end.x = x;
141  m_endsSwapped = false;
142  }
143 
144  void SetBezierC1( const wxPoint& aPt ) { m_bezierC1 = aPt; }
145  const wxPoint& GetBezierC1() const { return m_bezierC1; }
146 
147  void SetBezierC2( const wxPoint& aPt ) { m_bezierC2 = aPt; }
148  const wxPoint& GetBezierC2() const { return m_bezierC2; }
149 
150  wxPoint getCenter() const;
151  void SetCenter( const wxPoint& aCenter );
152 
158  void SetArcAngleAndEnd( double aAngle, bool aCheckNegativeAngle = false );
159 
160  double GetArcAngle() const;
161 
166  bool EndsSwapped() const { return m_endsSwapped; }
167 
168  // Some attributes are read only, since they are derived from m_Start, m_End, and m_Angle.
169  // No Set...() function for these attributes.
170 
171  wxPoint GetArcMid() const;
172  std::vector<wxPoint> GetRectCorners() const;
173 
178  void CalcArcAngles( double& aStartAngle, double& aEndAngle ) const;
179 
180  int GetRadius() const;
181 
188  void SetArcGeometry( const wxPoint& aStart, const wxPoint& aMid, const wxPoint& aEnd );
189 
190  const std::vector<wxPoint>& GetBezierPoints() const { return m_bezierPoints; }
191 
199  void DupPolyPointsList( std::vector<wxPoint>& aBuffer ) const;
200 
204  int GetPointCount() const;
205 
206  // Accessors to the polygonal shape
208  const SHAPE_POLY_SET& GetPolyShape() const { return m_poly; }
209 
213  bool IsPolyShapeValid() const;
214 
215  void SetPolyShape( const SHAPE_POLY_SET& aShape ) { m_poly = aShape; }
216 
217  void SetBezierPoints( const std::vector<wxPoint>& aPoints ) { m_bezierPoints = aPoints; }
218 
229  void RebuildBezierToSegmentsPointsList( int aMinSegLen );
230 
231  void SetPolyPoints( const std::vector<wxPoint>& aPoints );
232 
236  // fixme: move to shape_compound
237  std::vector<SHAPE*> MakeEffectiveShapes() const;
238 
239  void ShapeGetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList );
240 
246  double GetLength() const;
247 
260  void TransformShapeWithClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer, int aClearanceValue,
261  int aError, ERROR_LOC aErrorLoc,
262  bool ignoreLineWidth ) const;
263 
264  int Compare( const EDA_SHAPE* aOther ) const;
265 
266 protected:
267  void setPosition( const wxPoint& aPos );
268  wxPoint getPosition() const;
269 
270  void move( const wxPoint& aMoveVector );
271  void rotate( const wxPoint& aRotCentre, double aAngle );
272  void flip( const wxPoint& aCentre, bool aFlipLeftRight );
273  void scale( double aScale );
274 
275  // To be implemented by concrete classes
276  virtual double getParentOrientation() const = 0;
277  virtual wxPoint getParentPosition() const = 0;
278 
279  const EDA_RECT getBoundingBox() const;
280 
281  void computeArcBBox( EDA_RECT& aBBox ) const;
282 
283  bool hitTest( const wxPoint& aPosition, int aAccuracy = 0 ) const;
284  bool hitTest( const EDA_RECT& aRect, bool aContained, int aAccuracy = 0 ) const;
285 
286  const std::vector<wxPoint> buildBezierToSegmentsPointsList( int aMinSegLen ) const;
287 
288  void beginEdit( const wxPoint& aStartPoint );
289  bool continueEdit( const wxPoint& aPosition );
290  void calcEdit( const wxPoint& aPosition );
291  void endEdit();
292  void setEditState( int aState ) { m_editState = aState; }
293 
294 protected:
295  bool m_endsSwapped; // true if start/end were swapped e.g. SetArcAngleAndEnd
296  SHAPE_T m_shape; // Shape: line, Circle, Arc
297  int m_width; // thickness of lines ...
299  wxPoint m_start; // Line start point or Circle center
300  wxPoint m_end; // Line end point or Circle 3 o'clock point
301 
302  wxPoint m_arcCenter; // Used only for Arcs: arc end point
303 
304  wxPoint m_bezierC1; // Bezier Control Point 1
305  wxPoint m_bezierC2; // Bezier Control Point 2
306 
307  std::vector<wxPoint> m_bezierPoints;
308  SHAPE_POLY_SET m_poly; // Stores the S_POLYGON shape
309 
311  bool m_eeWinding; // Awful hack
312 };
313 
314 #endif // EDA_SHAPE_H
int GetStartY()
Definition: eda_shape.h:98
SHAPE_T m_shape
Definition: eda_shape.h:296
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:190
void endEdit()
Definition: eda_shape.cpp:1375
int GetWidth() const
Definition: eda_shape.h:89
wxPoint m_bezierC1
Definition: eda_shape.h:304
void SetFilled(bool aFlag)
Definition: eda_shape.h:83
void SetEnd(const wxPoint &aEnd)
Definition: eda_shape.h:126
void SetArcGeometry(const wxPoint &aStart, const wxPoint &aMid, const wxPoint &aEnd)
Set the three controlling points for an arc.
Definition: eda_shape.cpp:489
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:97
void SetPolyShape(const SHAPE_POLY_SET &aShape)
Definition: eda_shape.h:215
bool hitTest(const wxPoint &aPosition, int aAccuracy=0) const
Definition: eda_shape.cpp:657
void SetStartY(int y)
Definition: eda_shape.h:107
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:1461
int GetEndY()
Definition: eda_shape.h:123
void calcEdit(const wxPoint &aPosition)
Definition: eda_shape.cpp:1250
void computeArcBBox(EDA_RECT &aBBox) const
Definition: eda_shape.cpp:979
bool EndsSwapped() const
Have the start and end points been swapped since they were set?
Definition: eda_shape.h:166
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:1057
void DupPolyPointsList(std::vector< wxPoint > &aBuffer) const
Duplicate the list of corners in a std::vector<wxPoint>
Definition: eda_shape.cpp:1152
double GetArcAngle() const
Definition: eda_shape.cpp:498
void CalcArcAngles(double &aStartAngle, double &aEndAngle) const
Calc arc start and end angles such that aStartAngle < aEndAngle.
Definition: eda_shape.cpp:445
virtual wxPoint getParentPosition() const =0
void setEditState(int aState)
Definition: eda_shape.h:292
void SetArcAngleAndEnd(double aAngle, bool aCheckNegativeAngle=false)
Set the end point from the angle center and start.
Definition: eda_shape.cpp:509
void beginEdit(const wxPoint &aStartPoint)
Definition: eda_shape.cpp:1192
void SetEndY(int y)
Definition: eda_shape.h:132
void ShapeGetMsgPanelInfo(EDA_DRAW_FRAME *aFrame, std::vector< MSG_PANEL_ITEM > &aList)
Definition: eda_shape.cpp:522
The base class for create windows for drawing purpose.
wxString ShowShape() const
Definition: eda_shape.cpp:56
bool IsPolyShapeValid() const
Definition: eda_shape.cpp:1169
int GetStartX()
Definition: eda_shape.h:99
void SetBezierC1(const wxPoint &aPt)
Definition: eda_shape.h:144
wxPoint m_end
Definition: eda_shape.h:300
FILL_T
Definition: eda_shape.h:53
wxPoint m_arcCenter
Definition: eda_shape.h:302
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:124
wxPoint m_bezierC2
Definition: eda_shape.h:305
bool IsFilled() const
Definition: eda_shape.h:81
void SetStart(const wxPoint &aStart)
Definition: eda_shape.h:101
const wxPoint & GetBezierC1() const
Definition: eda_shape.h:145
void rotate(const wxPoint &aRotCentre, double aAngle)
Definition: eda_shape.cpp:224
int GetPointCount() const
Definition: eda_shape.cpp:1181
std::vector< wxPoint > GetRectCorners() const
Definition: eda_shape.cpp:943
bool m_endsSwapped
Definition: eda_shape.h:295
const wxPoint & GetEnd() const
Return the ending point of the graphic.
Definition: eda_shape.h:122
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:208
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:307
SHAPE_POLY_SET m_poly
Definition: eda_shape.h:308
const wxPoint & GetBezierC2() const
Definition: eda_shape.h:148
int m_editState
Definition: eda_shape.h:310
FILL_T m_fill
Definition: eda_shape.h:298
const std::vector< wxPoint > buildBezierToSegmentsPointsList(int aMinSegLen) const
Definition: eda_shape.cpp:380
void SetWidth(int aWidth)
Definition: eda_shape.h:88
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:1424
SHAPE_POLY_SET & GetPolyShape()
Definition: eda_shape.h:207
wxPoint m_start
Definition: eda_shape.h:299
void SetEndX(int x)
Definition: eda_shape.h:138
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:596
void SetPolyPoints(const std::vector< wxPoint > &aPoints)
Definition: eda_shape.cpp:1047
bool m_eeWinding
Definition: eda_shape.h:311
virtual double getParentOrientation() const =0
void SwapShape(EDA_SHAPE *aImage)
Definition: eda_shape.cpp:1407
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:91
void SetBezierC2(const wxPoint &aPt)
Definition: eda_shape.h:147
wxPoint getPosition() const
Definition: eda_shape.cpp:95
void SetFillMode(FILL_T aFill)
Definition: eda_shape.h:78
void SetBezierPoints(const std::vector< wxPoint > &aPoints)
Definition: eda_shape.h:217
SHAPE_T GetShape() const
Definition: eda_shape.h:92
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:297
int GetRadius() const
Definition: eda_shape.cpp:466
void flip(const wxPoint &aCentre, bool aFlipLeftRight)
Definition: eda_shape.cpp:281
bool continueEdit(const wxPoint &aPosition)
Definition: eda_shape.cpp:1223
FILL_T GetFillType() const
Definition: eda_shape.h:79
void SetStartX(int x)
Definition: eda_shape.h:113