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.

440 lines
14 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-2024 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. UNDEFINED = -1,
  39. SEGMENT = 0,
  40. RECTANGLE, /// use RECTANGLE instead of RECT to avoid collision in a Windows header
  41. ARC,
  42. CIRCLE,
  43. POLY,
  44. BEZIER
  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. setFilled( aFlag );
  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 LINE_STYLE aStyle );
  91. LINE_STYLE GetLineStyle() const;
  92. void SetLineColor( const COLOR4D& aColor ) { m_stroke.SetColor( aColor ); }
  93. COLOR4D GetLineColor() const { return m_stroke.GetColor(); }
  94. void SetShape( SHAPE_T aShape ) { m_shape = aShape; }
  95. SHAPE_T GetShape() const { return m_shape; }
  96. /**
  97. * Return the starting point of the graphic.
  98. */
  99. const VECTOR2I& GetStart() const { return m_start; }
  100. int GetStartY() const { return m_start.y; }
  101. int GetStartX() const { return m_start.x; }
  102. void SetStart( const VECTOR2I& aStart )
  103. {
  104. m_start = aStart;
  105. m_endsSwapped = false;
  106. }
  107. void SetStartY( int y )
  108. {
  109. m_start.y = y;
  110. m_endsSwapped = false;
  111. }
  112. void SetStartX( int x )
  113. {
  114. m_start.x = x;
  115. m_endsSwapped = false;
  116. }
  117. void SetCenterY( int y )
  118. {
  119. m_end.y += y - m_start.y;
  120. m_start.y = y;
  121. }
  122. void SetCenterX( int x )
  123. {
  124. m_end.x += x - m_start.x;
  125. m_start.x = x;
  126. }
  127. /**
  128. * Return the ending point of the graphic.
  129. */
  130. const VECTOR2I& GetEnd() const { return m_end; }
  131. int GetEndY() const { return m_end.y; }
  132. int GetEndX() const { return m_end.x; }
  133. void SetEnd( const VECTOR2I& aEnd )
  134. {
  135. m_end = aEnd;
  136. m_endsSwapped = false;
  137. }
  138. void SetEndY( int aY )
  139. {
  140. m_end.y = aY;
  141. m_endsSwapped = false;
  142. }
  143. void SetEndX( int aX )
  144. {
  145. m_end.x = aX;
  146. m_endsSwapped = false;
  147. }
  148. void SetRadius( int aX )
  149. {
  150. m_end = m_start + VECTOR2I( aX, 0 );
  151. }
  152. virtual VECTOR2I GetTopLeft() const { return GetStart(); }
  153. virtual VECTOR2I GetBotRight() const { return GetEnd(); }
  154. virtual void SetTop( int val ) { SetStartY( val ); }
  155. virtual void SetLeft( int val ) { SetStartX( val ); }
  156. virtual void SetRight( int val ) { SetEndX( val ); }
  157. virtual void SetBottom( int val ) { SetEndY( val ); }
  158. void SetBezierC1( const VECTOR2I& aPt ) { m_bezierC1 = aPt; }
  159. const VECTOR2I& GetBezierC1() const { return m_bezierC1; }
  160. void SetBezierC2( const VECTOR2I& aPt ) { m_bezierC2 = aPt; }
  161. const VECTOR2I& GetBezierC2() const { return m_bezierC2; }
  162. VECTOR2I getCenter() const;
  163. void SetCenter( const VECTOR2I& aCenter );
  164. /**
  165. * Set the end point from the angle center and start.
  166. * aAngle is:
  167. * - clockwise in right-down coordinate system
  168. * - counter-clockwise in right-up (libedit) coordinate system.
  169. */
  170. void SetArcAngleAndEnd( const EDA_ANGLE& aAngle, bool aCheckNegativeAngle = false );
  171. EDA_ANGLE GetArcAngle() const;
  172. EDA_ANGLE GetSegmentAngle() const;
  173. /**
  174. * Have the start and end points been swapped since they were set?
  175. * @return true if they have
  176. */
  177. bool EndsSwapped() const { return m_endsSwapped; }
  178. // Some attributes are read only, since they are derived from m_Start, m_End, and m_Angle.
  179. // No Set...() function for these attributes.
  180. VECTOR2I GetArcMid() const;
  181. std::vector<VECTOR2I> GetRectCorners() const;
  182. /**
  183. * Calc arc start and end angles such that aStartAngle < aEndAngle. Each may be between
  184. * -360.0 and 360.0.
  185. */
  186. void CalcArcAngles( EDA_ANGLE& aStartAngle, EDA_ANGLE& aEndAngle ) const;
  187. int GetRadius() const;
  188. /**
  189. * Set the three controlling points for an arc.
  190. *
  191. * NB: these are NOT what's currently stored, so we have to do some calculations behind
  192. * the scenes. However, they are what SHOULD be stored.
  193. */
  194. void SetArcGeometry( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd );
  195. /**
  196. * Set the data used for mid point caching. If the controlling points remain constant, then
  197. * we keep the midpoint the same as it was when read in. This minimizes VCS churn
  198. *
  199. * @param aStart Cached start point
  200. * @param aMid Cached mid point
  201. * @param aEnd Cached end point
  202. * @param aCenter Calculated center point using the preceeding three
  203. */
  204. void SetCachedArcData( const VECTOR2I& aStart, const VECTOR2I& aMid, const VECTOR2I& aEnd, const VECTOR2I& aCenter );
  205. const std::vector<VECTOR2I>& GetBezierPoints() const { return m_bezierPoints; }
  206. /**
  207. * Duplicate the list of corners in a std::vector<VECTOR2I>
  208. *
  209. * It must be used only to convert the SHAPE_POLY_SET internal corner buffer
  210. * to a list of VECTOR2Is, and nothing else, because it duplicates the buffer,
  211. * that is inefficient to know for instance the corner count
  212. */
  213. void DupPolyPointsList( std::vector<VECTOR2I>& aBuffer ) const;
  214. /**
  215. * @return the number of corners of the polygonal shape
  216. */
  217. int GetPointCount() const;
  218. // Accessors to the polygonal shape
  219. SHAPE_POLY_SET& GetPolyShape() { return m_poly; }
  220. const SHAPE_POLY_SET& GetPolyShape() const { return m_poly; }
  221. /**
  222. * @return true if the polygonal shape is valid (has more than 2 points)
  223. */
  224. bool IsPolyShapeValid() const;
  225. void SetPolyShape( const SHAPE_POLY_SET& aShape )
  226. {
  227. m_poly = aShape;
  228. for( int ii = 0; ii < m_poly.OutlineCount(); ++ii )
  229. {
  230. if( m_poly.HoleCount( ii ) )
  231. {
  232. m_poly.Fracture( SHAPE_POLY_SET::PM_FAST );
  233. break;
  234. }
  235. }
  236. }
  237. void SetPolyPoints( const std::vector<VECTOR2I>& aPoints );
  238. /**
  239. * Rebuild the m_bezierPoints vertex list that approximate the Bezier curve by a list of
  240. * segments.
  241. *
  242. * Has meaning only for BEZIER shape.
  243. *
  244. * @param aMinSegLen is the min length of segments approximating the bezier. The shape's last
  245. * segment can be shorter. This parameter avoids having too many very short
  246. * segment in list. Good values are between m_width/2 and m_width.
  247. */
  248. void RebuildBezierToSegmentsPointsList( int aMinSegLen );
  249. /**
  250. * Make a set of SHAPE objects representing the EDA_SHAPE. Caller owns the objects.
  251. *
  252. * @param aEdgeOnly indicates only edges should be generated (even if 0 width), and no fill
  253. * shapes.
  254. */
  255. virtual std::vector<SHAPE*> MakeEffectiveShapes( bool aEdgeOnly = false ) const
  256. {
  257. return makeEffectiveShapes( aEdgeOnly );
  258. }
  259. void ShapeGetMsgPanelInfo( EDA_DRAW_FRAME* aFrame, std::vector<MSG_PANEL_ITEM>& aList );
  260. void SetLength( const double& aLength );
  261. void SetRectangle( const long long int& aHeight, const long long int& aWidth );
  262. void SetSegmentAngle( const EDA_ANGLE& aAngle );
  263. bool IsClockwiseArc() const;
  264. /**
  265. * @return the length of the segment using the hypotenuse calculation.
  266. */
  267. double GetLength() const;
  268. int GetRectangleHeight() const;
  269. int GetRectangleWidth() const;
  270. /**
  271. * Convert the shape to a closed polygon. Circles and arcs are approximated by segments.
  272. *
  273. * @param aBuffer is a buffer to store the polygon.
  274. * @param aClearance is the clearance around the pad.
  275. * @param aError is the maximum deviation from a true arc.
  276. * @param aErrorLoc whether any approximation error shoule be placed inside or outside
  277. * @param ignoreLineWidth is used for edge cut items where the line width is only for
  278. * visualization
  279. */
  280. void TransformShapeToPolygon( SHAPE_POLY_SET& aBuffer, int aClearance, int aError,
  281. ERROR_LOC aErrorLoc, bool ignoreLineWidth = false ) const;
  282. int Compare( const EDA_SHAPE* aOther ) const;
  283. double Similarity( const EDA_SHAPE& aOther ) const;
  284. bool operator==( const EDA_SHAPE& aOther ) const;
  285. protected:
  286. wxString getFriendlyName() const;
  287. void setPosition( const VECTOR2I& aPos );
  288. VECTOR2I getPosition() const;
  289. virtual void setFilled( bool aFlag )
  290. {
  291. m_fill = aFlag ? FILL_T::FILLED_SHAPE : FILL_T::NO_FILL;
  292. }
  293. void move( const VECTOR2I& aMoveVector );
  294. void rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle );
  295. void flip( const VECTOR2I& aCentre, bool aFlipLeftRight );
  296. void scale( double aScale );
  297. const BOX2I getBoundingBox() const;
  298. void computeArcBBox( BOX2I& aBBox ) const;
  299. bool hitTest( const VECTOR2I& aPosition, int aAccuracy = 0 ) const;
  300. bool hitTest( const BOX2I& aRect, bool aContained, int aAccuracy = 0 ) const;
  301. const std::vector<VECTOR2I> buildBezierToSegmentsPointsList( int aMinSegLen ) const;
  302. void beginEdit( const VECTOR2I& aStartPoint );
  303. bool continueEdit( const VECTOR2I& aPosition );
  304. void calcEdit( const VECTOR2I& aPosition );
  305. /**
  306. * Finishes editing the shape.
  307. * @param aClosed Should polygon shapes be closed (yes for pcbnew/fpeditor, no for libedit)
  308. */
  309. void endEdit( bool aClosed = true );
  310. void setEditState( int aState ) { m_editState = aState; }
  311. /**
  312. * Make a set of SHAPE objects representing the EDA_SHAPE. Caller owns the objects.
  313. *
  314. * @param aEdgeOnly indicates only edges should be generated (even if 0 width), and no fill
  315. * shapes.
  316. * @param aLineChainOnly indicates SHAPE_POLY_SET is being abused slightly to represent a
  317. * lineChain rather than a closed polygon
  318. */
  319. // fixme: move to shape_compound
  320. std::vector<SHAPE*> makeEffectiveShapes( bool aEdgeOnly, bool aLineChainOnly = false ) const;
  321. protected:
  322. bool m_endsSwapped; // true if start/end were swapped e.g. SetArcAngleAndEnd
  323. SHAPE_T m_shape; // Shape: line, Circle, Arc
  324. STROKE_PARAMS m_stroke; // Line style, width, etc.
  325. FILL_T m_fill;
  326. COLOR4D m_fillColor;
  327. long long int m_rectangleHeight;
  328. long long int m_rectangleWidth;
  329. double m_segmentLength;
  330. EDA_ANGLE m_segmentAngle;
  331. VECTOR2I m_start; // Line start point or Circle center
  332. VECTOR2I m_end; // Line end point or Circle 3 o'clock point
  333. VECTOR2I m_arcCenter; // Used only for Arcs: arc end point
  334. ARC_MID m_arcMidData; // Used to store originating data
  335. VECTOR2I m_bezierC1; // Bezier Control Point 1
  336. VECTOR2I m_bezierC2; // Bezier Control Point 2
  337. std::vector<VECTOR2I> m_bezierPoints;
  338. SHAPE_POLY_SET m_poly; // Stores the S_POLYGON shape
  339. int m_editState;
  340. bool m_proxyItem; // A shape storing proxy information (ie: a pad
  341. // number box, thermal spoke template, etc.)
  342. };
  343. #ifndef SWIG
  344. DECLARE_ENUM_TO_WXANY( SHAPE_T );
  345. DECLARE_ENUM_TO_WXANY( LINE_STYLE );
  346. #endif
  347. #endif // EDA_SHAPE_H