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>
31 #include <geometry/shape_poly_set.h>
32 #include <geometry/geometry_utils.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          ///< marker for list end
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 
SetFillMode(FILL_T aFill)78     void SetFillMode( FILL_T aFill ) { m_fill = aFill; }
GetFillType()79     FILL_T GetFillType() const { return m_fill; }
80 
IsFilled()81     bool IsFilled() const { return GetFillType() != FILL_T::NO_FILL; }
82 
SetFilled(bool aFlag)83     void SetFilled( bool aFlag )
84     {
85         m_fill = aFlag ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL;
86     }
87 
SetWidth(int aWidth)88     void SetWidth( int aWidth )             { m_width = aWidth; }
GetWidth()89     int GetWidth() const                    { return m_width; }
90 
SetShape(SHAPE_T aShape)91     void SetShape( SHAPE_T aShape )         { m_shape = aShape; }
GetShape()92     SHAPE_T GetShape() const                { return m_shape; }
93 
94     /**
95      * Return the starting point of the graphic.
96      */
GetStart()97     const wxPoint& GetStart() const         { return m_start; }
GetStartY()98     int GetStartY()                         { return m_start.y; }
GetStartX()99     int GetStartX()                         { return m_start.x; }
100 
SetStart(const wxPoint & aStart)101     void SetStart( const wxPoint& aStart )
102     {
103         m_start = aStart;
104         m_endsSwapped = false;
105     }
106 
SetStartY(int y)107     void SetStartY( int y )
108     {
109         m_start.y = y;
110         m_endsSwapped = false;
111     }
112 
SetStartX(int x)113     void SetStartX( int x )
114     {
115         m_start.x = x;
116         m_endsSwapped = false;
117     }
118 
119     /**
120      * Return the ending point of the graphic.
121      */
GetEnd()122     const wxPoint& GetEnd() const           { return m_end; }
GetEndY()123     int GetEndY()                           { return m_end.y; }
GetEndX()124     int GetEndX()                           { return m_end.x; }
125 
SetEnd(const wxPoint & aEnd)126     void SetEnd( const wxPoint& aEnd )
127     {
128         m_end = aEnd;
129         m_endsSwapped = false;
130     }
131 
SetEndY(int y)132     void SetEndY( int y )
133     {
134         m_end.y = y;
135         m_endsSwapped = false;
136     }
137 
SetEndX(int x)138     void SetEndX( int x )
139     {
140         m_end.x = x;
141         m_endsSwapped = false;
142     }
143 
SetBezierC1(const wxPoint & aPt)144     void SetBezierC1( const wxPoint& aPt )  { m_bezierC1 = aPt; }
GetBezierC1()145     const wxPoint& GetBezierC1() const      { return m_bezierC1; }
146 
SetBezierC2(const wxPoint & aPt)147     void SetBezierC2( const wxPoint& aPt )  { m_bezierC2 = aPt; }
GetBezierC2()148     const wxPoint& GetBezierC2() const      { return m_bezierC2; }
149 
150     wxPoint getCenter() const;
151     void SetCenter( const wxPoint& aCenter );
152 
153     /**
154      * Set the end point from the angle center and start.
155      *
156      * @param aAngle is tenths of degrees.
157      */
158     void SetArcAngleAndEnd( double aAngle, bool aCheckNegativeAngle = false );
159 
160     double GetArcAngle() const;
161 
162     /**
163      * Have the start and end points been swapped since they were set?
164      * @return true if they have
165     */
EndsSwapped()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 
174     /**
175      * Calc arc start and end angles such that aStartAngle < aEndAngle.  Each may be between
176      * -360.0 and 360.0.
177      */
178     void CalcArcAngles( double& aStartAngle, double& aEndAngle ) const;
179 
180     int GetRadius() const;
181 
182     /**
183      * Set the three controlling points for an arc.
184      *
185      * NB: these are NOT what's currently stored, so we have to do some calculations behind
186      * the scenes.  However, they are what SHOULD be stored.
187      */
188     void SetArcGeometry( const wxPoint& aStart, const wxPoint& aMid, const wxPoint& aEnd );
189 
GetBezierPoints()190     const std::vector<wxPoint>& GetBezierPoints() const { return m_bezierPoints; }
191 
192     /**
193      * Duplicate the list of corners in a std::vector<wxPoint>
194      *
195      * It must be used only to convert the SHAPE_POLY_SET internal corner buffer
196      * to a list of wxPoints, and nothing else, because it duplicates the buffer,
197      * that is inefficient to know for instance the corner count
198      */
199     void DupPolyPointsList( std::vector<wxPoint>& aBuffer ) const;
200 
201     /**
202      * @return the number of corners of the polygonal shape
203      */
204     int GetPointCount() const;
205 
206     // Accessors to the polygonal shape
GetPolyShape()207     SHAPE_POLY_SET& GetPolyShape() { return m_poly; }
GetPolyShape()208     const SHAPE_POLY_SET& GetPolyShape() const { return m_poly; }
209 
210     /**
211      * @return true if the polygonal shape is valid (has more than 2 points)
212      */
213     bool IsPolyShapeValid() const;
214 
SetPolyShape(const SHAPE_POLY_SET & aShape)215     void SetPolyShape( const SHAPE_POLY_SET& aShape ) { m_poly = aShape; }
216 
SetBezierPoints(const std::vector<wxPoint> & aPoints)217     void SetBezierPoints( const std::vector<wxPoint>& aPoints ) { m_bezierPoints = aPoints; }
218 
219     /**
220      * Rebuild the m_BezierPoints vertex list that approximate the Bezier curve
221      * by a list of segments.
222      *
223      * Has meaning only for BEZIER shape.
224      *
225      * @param aMinSegLen is the min length of segments approximating the bezier. The shape's last
226      *                   segment can be shorter.  This parameter avoids having too many very short
227      *                   segment in list. Good values are between m_width/2 and m_width.
228      */
229     void RebuildBezierToSegmentsPointsList( int aMinSegLen );
230 
231     void SetPolyPoints( const std::vector<wxPoint>& aPoints );
232 
233     /**
234      * Make a set of SHAPE objects representing the EDA_SHAPE.  Caller owns the objects.
235      */
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 
241     /**
242      * Return the length of the track using the hypotenuse calculation.
243      *
244      * @return the length of the track
245      */
246     double GetLength() const;
247 
248     /**
249      * Convert the shape to a closed polygon.
250      *
251      * Used in filling zones calculations.  Circles and arcs are approximated by segments.
252      *
253      * @param aCornerBuffer is a buffer to store the polygon.
254      * @param aClearanceValue is the clearance around the pad.
255      * @param aError is the maximum deviation from a true arc.
256      * @param aErrorLoc whether any approximation error shoule be placed inside or outside
257      * @param ignoreLineWidth is used for edge cut items where the line width is only
258      *        for visualization
259      */
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();
setEditState(int aState)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 ...
298     FILL_T               m_fill;
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 
310     int                  m_editState;
311     bool                 m_eeWinding;    // Awful hack
312 };
313 
314 #endif  // EDA_SHAPE_H
315