You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

409 lines
13 KiB

4 years ago
  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. #ifndef EDA_SHAPE_H
  25. #define EDA_SHAPE_H
  26. #include <trigo.h>
  27. #include <geometry/shape_poly_set.h>
  28. #include <geometry/geometry_utils.h>
  29. #include <properties/property.h>
  30. #include <stroke_params.h>
  31. class LINE_READER;
  32. class EDA_DRAW_FRAME;
  33. class FOOTPRINT;
  34. class MSG_PANEL_ITEM;
  35. using KIGFX::COLOR4D;
  36. enum class SHAPE_T : int
  37. {
  38. SEGMENT = 0,
  39. RECTANGLE, /// use RECTANGLE instead of RECT to avoid collision in a Windows header
  40. ARC,
  41. CIRCLE,
  42. POLY,
  43. BEZIER,
  44. LAST ///< marker for list end
  45. };
  46. // WARNING: Do not change these values without updating dialogs that depend on their position values
  47. enum class FILL_T : int
  48. {
  49. NO_FILL = 1,
  50. FILLED_SHAPE, // Fill with object color
  51. FILLED_WITH_BG_BODYCOLOR, // Fill with background body color
  52. FILLED_WITH_COLOR // Fill with a separate color
  53. };
  54. // Holding struct to keep originating midpoint
  55. struct ARC_MID
  56. {
  57. VECTOR2I mid;
  58. VECTOR2I start;
  59. VECTOR2I end;
  60. VECTOR2I center;
  61. };
  62. class EDA_SHAPE
  63. {
  64. public:
  65. EDA_SHAPE( SHAPE_T aType, int aLineWidth, FILL_T aFill );
  66. // Do not create a copy constructor & operator=.
  67. // The ones generated by the compiler are adequate.
  68. virtual ~EDA_SHAPE();
  69. void SwapShape( EDA_SHAPE* aImage );
  70. wxString ShowShape() const;
  71. wxString SHAPE_T_asString() const;
  72. virtual bool IsProxyItem() const { return m_proxyItem; }
  73. virtual void SetIsProxyItem( bool aIsProxy = true ) { m_proxyItem = aIsProxy; }
  74. bool IsFilled() const
  75. {
  76. return GetFillMode() != FILL_T::NO_FILL;
  77. }
  78. void SetFilled( bool aFlag )
  79. {
  80. m_fill = aFlag ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL;
  81. }
  82. void SetFillMode( FILL_T aFill ) { m_fill = aFill; }
  83. FILL_T GetFillMode() const { return m_fill; }
  84. bool IsClosed() const;
  85. COLOR4D GetFillColor() const { return m_fillColor; }
  86. void SetFillColor( const COLOR4D& aColor ) { m_fillColor = aColor; }
  87. void SetWidth( int aWidth ) { m_stroke.SetWidth( aWidth ); }
  88. virtual int GetWidth() const { return m_stroke.GetWidth(); }
  89. virtual int GetEffectiveWidth() const { return GetWidth(); }
  90. void SetLineStyle( const PLOT_DASH_TYPE aStyle );
  91. PLOT_DASH_TYPE GetLineStyle() const;
  92. void SetShape( SHAPE_T aShape ) { m_shape = aShape; }
  93. SHAPE_T GetShape() const { return m_shape; }
  94. wxString GetFriendlyName() const;
  95. /**
  96. * Return the starting point of the graphic.
  97. */
  98. const VECTOR2I& GetStart() const { return m_start; }
  99. int GetStartY() const { return m_start.y; }
  100. int GetStartX() const { return m_start.x; }
  101. void SetStart( const VECTOR2I& aStart )
  102. {
  103. m_start = aStart;
  104. m_endsSwapped = false;
  105. }
  106. void SetStartY( int y )
  107. {
  108. m_start.y = y;
  109. m_endsSwapped = false;
  110. }
  111. void SetStartX( int x )
  112. {
  113. m_start.x = x;
  114. m_endsSwapped = false;
  115. }
  116. /**
  117. * Return the ending point of the graphic.
  118. */
  119. const VECTOR2I& GetEnd() const { return m_end; }
  120. int GetEndY() const { return m_end.y; }
  121. int GetEndX() const { return m_end.x; }
  122. void SetEnd( const VECTOR2I& aEnd )
  123. {
  124. m_end = aEnd;
  125. m_endsSwapped = false;
  126. }
  127. void SetEndY( int y )
  128. {
  129. m_end.y = y;
  130. m_endsSwapped = false;
  131. }
  132. void SetEndX( int x )
  133. {
  134. m_end.x = x;
  135. m_endsSwapped = false;
  136. }
  137. virtual VECTOR2I GetTopLeft() const { return GetStart(); }
  138. virtual VECTOR2I GetBotRight() const { return GetEnd(); }
  139. virtual void SetTop( int val ) { SetStartY( val ); }
  140. virtual void SetLeft( int val ) { SetStartX( val ); }
  141. virtual void SetRight( int val ) { SetEndX( val ); }
  142. virtual void SetBottom( int val ) { SetEndY( val ); }
  143. void SetBezierC1( const VECTOR2I& aPt ) { m_bezierC1 = aPt; }
  144. const VECTOR2I& GetBezierC1() const { return m_bezierC1; }
  145. void SetBezierC2( const VECTOR2I& aPt ) { m_bezierC2 = aPt; }
  146. const VECTOR2I& GetBezierC2() const { return m_bezierC2; }
  147. VECTOR2I getCenter() const;
  148. void SetCenter( const VECTOR2I& aCenter );
  149. /**
  150. * Set the end point from the angle center and start.
  151. * aAngle is:
  152. * - clockwise in right-down coordinate system
  153. * - counter-clockwise in right-up (libedit) coordinate system.
  154. */
  155. void SetArcAngleAndEnd( const EDA_ANGLE& aAngle, bool aCheckNegativeAngle = false );
  156. EDA_ANGLE GetArcAngle() const;
  157. EDA_ANGLE GetSegmentAngle() const;
  158. /**
  159. * Have the start and end points been swapped since they were set?
  160. * @return true if they have
  161. */
  162. bool EndsSwapped() const { return m_endsSwapped; }
  163. // Some attributes are read only, since they are derived from m_Start, m_End, and m_Angle.
  164. // No Set...() function for these attributes.
  165. VECTOR2I GetArcMid() const;
  166. std::vector<VECTOR2I> GetRectCorners() const;
  167. /**
  168. * Calc arc start and end angles such that aStartAngle < aEndAngle. Each may be between
  169. * -360.0 and 360.0.
  170. */
  171. void CalcArcAngles( EDA_ANGLE& aStartAngle, EDA_ANGLE& aEndAngle ) const;
  172. int GetRadius() const;
  173. /**
  174. * Set the three controlling points for an arc.
  175. *
  176. * NB: these are NOT what's currently stored, so we have to do some calculations behind
  177. * the scenes. However, they are what SHOULD be stored.
  178. */
  179. void SetArcGeometry( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
  180. /**
  181. * Set the data used for mid point caching. If the controlling points remain constant, then
  182. * we keep the midpoint the same as it was when read in. This minimizes VCS churn
  183. *
  184. * @param aStart Cached start point
  185. * @param aMid Cached mid point
  186. * @param aEnd Cached end point
  187. * @param aCenter Calculated center point using the preceeding three
  188. */
  189. void SetCachedArcData( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd, const VECTOR2I& aCenter );
  190. const std::vector<VECTOR2I>& GetBezierPoints() const { return m_bezierPoints; }
  191. /**
  192. * Duplicate the list of corners in a std::vector<VECTOR2I>
  193. *
  194. * It must be used only to convert the SHAPE_POLY_SET internal corner buffer
  195. * to a list of VECTOR2Is, and nothing else, because it duplicates the buffer,
  196. * that is inefficient to know for instance the corner count
  197. */
  198. void DupPolyPointsList( std::vector<VECTOR2I>& aBuffer ) const;
  199. /**
  200. * @return the number of corners of the polygonal shape
  201. */
  202. int GetPointCount() const;
  203. // Accessors to the polygonal shape
  204. SHAPE_POLY_SET& GetPolyShape() { return m_poly; }
  205. const SHAPE_POLY_SET& GetPolyShape() const { return m_poly; }
  206. /**
  207. * @return true if the polygonal shape is valid (has more than 2 points)
  208. */
  209. bool IsPolyShapeValid() const;
  210. void SetPolyShape( const SHAPE_POLY_SET& aShape )
  211. {
  212. m_poly = aShape;
  213. for( int ii = 0; ii < m_poly.OutlineCount(); ++ii )
  214. {
  215. if( m_poly.HoleCount( ii ) )
  216. {
  217. m_poly.Fracture( SHAPE_POLY_SET::PM_FAST );
  218. break;
  219. }
  220. }
  221. }
  222. void SetPolyPoints( const std::vector<VECTOR2I>& aPoints );
  223. /**
  224. * Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of
  225. * segments.
  226. *
  227. * Has meaning only for BEZIER shape.
  228. *
  229. * @param aMinSegLen is the min length of segments approximating the bezier. The shape's last
  230. * segment can be shorter. This parameter avoids having too many very short
  231. * segment in list. Good values are between m_width/2 and m_width.
  232. */
  233. void RebuildBezierToSegmentsPointsList( int aMinSegLen );
  234. /**
  235. * Make a set of SHAPE objects representing the EDA_SHAPE. Caller owns the objects.
  236. *
  237. * @param aEdgeOnly indicates only edges should be generated (even if 0 width), and no fill
  238. * shapes.
  239. */
  240. virtual std::vector<SHAPE*> MakeEffectiveShapes( bool aEdgeOnly = false ) const
  241. {
  242. return makeEffectiveShapes( aEdgeOnly );
  243. }
  244. void ShapeGetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList );
  245. void SetLength( const double& aLength );
  246. void SetRectangle( const long long int& aHeight, const long long int& aWidth );
  247. void SetSegmentAngle( const EDA_ANGLE& aAngle );
  248. /**
  249. * @return the length of the segment using the hypotenuse calculation.
  250. */
  251. double GetLength() const;
  252. int GetRectangleHeight() const;
  253. int GetRectangleWidth() const;
  254. /**
  255. * Convert the shape to a closed polygon. Circles and arcs are approximated by segments.
  256. *
  257. * @param aBuffer is a buffer to store the polygon.
  258. * @param aClearance is the clearance around the pad.
  259. * @param aError is the maximum deviation from a true arc.
  260. * @param aErrorLoc whether any approximation error shoule be placed inside or outside
  261. * @param ignoreLineWidth is used for edge cut items where the line width is only for
  262. * visualization
  263. */
  264. void TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, int aClearance, int aError,
  265. ERROR_LOC aErrorLoc, bool ignoreLineWidth = false ) const;
  266. int Compare( const EDA_SHAPE* aOther ) const;
  267. protected:
  268. void setPosition( const VECTOR2I& aPos );
  269. VECTOR2I getPosition() const;
  270. void move( const VECTOR2I& aMoveVector );
  271. void rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle );
  272. void flip( const VECTOR2I& aCentre, bool aFlipLeftRight );
  273. void scale( double aScale );
  274. const BOX2I getBoundingBox() const;
  275. void computeArcBBox( BOX2I& aBBox ) const;
  276. bool hitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const;
  277. bool hitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const;
  278. const std::vector<VECTOR2I> buildBezierToSegmentsPointsList( int aMinSegLen ) const;
  279. void beginEdit( const VECTOR2I& aStartPoint );
  280. bool continueEdit( const VECTOR2I& aPosition );
  281. void calcEdit( const VECTOR2I& aPosition );
  282. /**
  283. * Finishes editing the shape.
  284. * @param aClosed Should polygon shapes be closed (yes for pcbnew/fpeditor, no for libedit)
  285. */
  286. void endEdit( bool aClosed = true );
  287. void setEditState( int aState ) { m_editState = aState; }
  288. /**
  289. * Make a set of SHAPE objects representing the EDA_SHAPE. Caller owns the objects.
  290. *
  291. * @param aEdgeOnly indicates only edges should be generated (even if 0 width), and no fill
  292. * shapes.
  293. * @param aLineChainOnly indicates SHAPE_POLY_SET is being abused slightly to represent a
  294. * lineChain rather than a closed polygon
  295. */
  296. // fixme: move to shape_compound
  297. std::vector<SHAPE*> makeEffectiveShapes( bool aEdgeOnly, bool aLineChainOnly = false ) const;
  298. protected:
  299. bool m_endsSwapped; // true if start/end were swapped e.g. SetArcAngleAndEnd
  300. SHAPE_T m_shape; // Shape: line, Circle, Arc
  301. STROKE_PARAMS m_stroke; // Line style, width, etc.
  302. FILL_T m_fill;
  303. COLOR4D m_fillColor;
  304. long long int m_rectangleHeight;
  305. long long int m_rectangleWidth;
  306. double m_segmentLength;
  307. EDA_ANGLE m_segmentAngle;
  308. VECTOR2I m_start; // Line start point or Circle center
  309. VECTOR2I m_end; // Line end point or Circle 3 o'clock point
  310. VECTOR2I m_arcCenter; // Used only for Arcs: arc end point
  311. ARC_MID m_arcMidData; // Used to store originating data
  312. VECTOR2I m_bezierC1; // Bezier Control Point 1
  313. VECTOR2I m_bezierC2; // Bezier Control Point 2
  314. std::vector<VECTOR2I> m_bezierPoints;
  315. SHAPE_POLY_SET m_poly; // Stores the S_POLYGON shape
  316. int m_editState;
  317. bool m_proxyItem; // A shape storing proxy information (ie: a pad
  318. // number box, thermal spoke template, etc.)
  319. };
  320. #ifndef SWIG
  321. DECLARE_ENUM_TO_WXANY( SHAPE_T );
  322. DECLARE_ENUM_TO_WXANY( PLOT_DASH_TYPE );
  323. #endif
  324. #endif // EDA_SHAPE_H