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.

394 lines
13 KiB

8 years ago
8 years ago
8 years ago
  1. /**
  2. * @file convert_basic_shapes_to_polygon.cpp
  3. */
  4. /*
  5. * This program source code file is part of KiCad, a free EDA CAD application.
  6. *
  7. * Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wanadoo.fr
  8. * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, you may find one here:
  22. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. * or you may search the http://www.gnu.org website for the version 2 license,
  24. * or you may write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  26. */
  27. #include <vector>
  28. #include <fctsys.h>
  29. #include <trigo.h>
  30. #include <macros.h>
  31. #include <common.h>
  32. #include <convert_basic_shapes_to_polygon.h>
  33. /**
  34. * Function TransformCircleToPolygon
  35. * convert a circle to a polygon, using multiple straight lines
  36. * @param aCornerBuffer = a buffer to store the polygon
  37. * @param aCenter = the center of the circle
  38. * @param aRadius = the radius of the circle
  39. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  40. * Note: the polygon is inside the circle, so if you want to have the polygon
  41. * outside the circle, you should give aRadius calculated with a correction factor
  42. */
  43. void TransformCircleToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  44. wxPoint aCenter, int aRadius,
  45. int aCircleToSegmentsCount )
  46. {
  47. wxPoint corner_position;
  48. double delta = 3600.0 / aCircleToSegmentsCount; // rot angle in 0.1 degree
  49. double halfstep = delta/2; // the starting value for rot angles
  50. aCornerBuffer.NewOutline();
  51. for( int ii = 0; ii < aCircleToSegmentsCount; ii++ )
  52. {
  53. corner_position.x = aRadius;
  54. corner_position.y = 0;
  55. double angle = (ii * delta) + halfstep;
  56. RotatePoint( &corner_position, angle );
  57. corner_position += aCenter;
  58. aCornerBuffer.Append( corner_position.x, corner_position.y );
  59. }
  60. }
  61. void TransformOvalClearanceToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  62. wxPoint aStart, wxPoint aEnd, int aWidth,
  63. int aCircleToSegmentsCount, double aCorrectionFactor )
  64. {
  65. int radius = aWidth / 2;
  66. wxPoint endp = aEnd - aStart; // end point coordinate for the same segment starting at (0,0)
  67. wxPoint startp = aStart;
  68. wxPoint corner;
  69. VECTOR2I polypoint;
  70. SHAPE_LINE_CHAIN polyshape;
  71. radius = radius * aCorrectionFactor;
  72. // normalize the position in order to have endp.x >= 0;
  73. if( endp.x < 0 )
  74. {
  75. endp = aStart - aEnd;
  76. startp = aEnd;
  77. }
  78. // delta_angle is in rd
  79. double delta_angle = atan2( (double)endp.y, (double)endp.x );
  80. int seg_len = KiROUND( EuclideanNorm( endp ) );
  81. int delta = 3600 / aCircleToSegmentsCount; // rot angle in 0.1 degree
  82. // Compute the outlines of the segment, and creates a polygon
  83. // add right rounded end:
  84. for( int ii = 0; ii < 1800; ii += delta )
  85. {
  86. corner = wxPoint( 0, radius );
  87. RotatePoint( &corner, ii );
  88. corner.x += seg_len;
  89. polyshape.Append( corner.x, corner.y );
  90. }
  91. // Finish arc:
  92. corner = wxPoint( seg_len, -radius );
  93. polyshape.Append( corner.x, corner.y );
  94. // add left rounded end:
  95. for( int ii = 0; ii < 1800; ii += delta )
  96. {
  97. corner = wxPoint( 0, -radius );
  98. RotatePoint( &corner, ii );
  99. polyshape.Append( corner.x, corner.y );
  100. }
  101. // Finish arc:
  102. corner = wxPoint( 0, radius );
  103. polyshape.Append( corner.x, corner.y );
  104. polyshape.SetClosed( true );
  105. // Rotate and move the polygon to its right location
  106. polyshape.Rotate( delta_angle, VECTOR2I( 0, 0 ) );
  107. polyshape.Move( startp );
  108. aCornerBuffer.AddOutline( polyshape);
  109. }
  110. /* Returns the centers of the rounded corners of a rect.
  111. */
  112. void GetRoundRectCornerCenters( wxPoint aCenters[4], int aRadius,
  113. const wxPoint& aPosition, const wxSize& aSize, double aRotation )
  114. {
  115. wxSize size( aSize/2 );
  116. size.x -= aRadius;
  117. size.y -= aRadius;
  118. // Ensure size is > 0, to avoid generating unusable shapes
  119. // which can crash kicad.
  120. if( size.x <= 1 )
  121. size.x = 1;
  122. if( size.y <= 1 )
  123. size.y = 1;
  124. aCenters[0].x = -size.x;
  125. aCenters[0].y = size.y;
  126. aCenters[1].x = size.x;
  127. aCenters[1].y = size.y;
  128. aCenters[2].x = size.x;
  129. aCenters[2].y = -size.y;
  130. aCenters[3].x = -size.x;
  131. aCenters[3].y = -size.y;
  132. // Rotate the polygon
  133. if( aRotation )
  134. {
  135. for( int ii = 0; ii < 4; ii++ )
  136. RotatePoint( &aCenters[ii], aRotation );
  137. }
  138. // move the polygon to the position
  139. for( int ii = 0; ii < 4; ii++ )
  140. aCenters[ii] += aPosition;
  141. }
  142. /**
  143. * Function TransformRoundRectToPolygon
  144. * convert a rectangle with rounded corners to a polygon
  145. * Convert arcs to multiple straight lines
  146. * @param aCornerBuffer = a buffer to store the polygon
  147. * @param aPosition = the coordinate of the center of the rectangle
  148. * @param aSize = the size of the rectangle
  149. * @param aCornerRadius = radius of rounded corners
  150. * @param aRotation = rotation in 0.1 degrees of the rectangle
  151. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  152. */
  153. void TransformRoundRectToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  154. const wxPoint& aPosition, const wxSize& aSize,
  155. double aRotation, int aCornerRadius,
  156. int aCircleToSegmentsCount )
  157. {
  158. wxPoint corners[4];
  159. GetRoundRectCornerCenters( corners, aCornerRadius, aPosition, aSize, aRotation );
  160. SHAPE_POLY_SET outline;
  161. outline.NewOutline();
  162. for( int ii = 0; ii < 4; ++ii )
  163. outline.Append( corners[ii].x, corners[ii].y );
  164. outline.Inflate( aCornerRadius, aCircleToSegmentsCount );
  165. // Add the outline:
  166. aCornerBuffer.Append( outline );
  167. }
  168. /**
  169. * Function TransformRoundedEndsSegmentToPolygon
  170. * convert a segment with rounded ends to a polygon
  171. * Convert arcs to multiple straight lines
  172. * @param aCornerBuffer = a buffer to store the polygon
  173. * @param aStart = the segment start point coordinate
  174. * @param aEnd = the segment end point coordinate
  175. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  176. * @param aWidth = the segment width
  177. * Note: the polygon is inside the arc ends, so if you want to have the polygon
  178. * outside the circle, you should give aStart and aEnd calculated with a correction factor
  179. */
  180. void TransformRoundedEndsSegmentToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  181. wxPoint aStart, wxPoint aEnd,
  182. int aCircleToSegmentsCount,
  183. int aWidth )
  184. {
  185. int radius = aWidth / 2;
  186. wxPoint endp = aEnd - aStart; // end point coordinate for the same segment starting at (0,0)
  187. wxPoint startp = aStart;
  188. wxPoint corner;
  189. VECTOR2I polypoint;
  190. aCornerBuffer.NewOutline();
  191. // normalize the position in order to have endp.x >= 0;
  192. if( endp.x < 0 )
  193. {
  194. endp = aStart - aEnd;
  195. startp = aEnd;
  196. }
  197. double delta_angle = ArcTangente( endp.y, endp.x ); // delta_angle is in 0.1 degrees
  198. int seg_len = KiROUND( EuclideanNorm( endp ) );
  199. int delta = 3600 / aCircleToSegmentsCount; // rot angle in 0.1 degree
  200. // Compute the outlines of the segment, and creates a polygon
  201. // add right rounded end:
  202. for( int ii = 0; ii < 1800; ii += delta )
  203. {
  204. corner = wxPoint( 0, radius );
  205. RotatePoint( &corner, ii );
  206. corner.x += seg_len;
  207. RotatePoint( &corner, -delta_angle );
  208. corner += startp;
  209. polypoint.x = corner.x;
  210. polypoint.y = corner.y;
  211. aCornerBuffer.Append( polypoint.x, polypoint.y );
  212. }
  213. // Finish arc:
  214. corner = wxPoint( seg_len, -radius );
  215. RotatePoint( &corner, -delta_angle );
  216. corner += startp;
  217. polypoint.x = corner.x;
  218. polypoint.y = corner.y;
  219. aCornerBuffer.Append( polypoint.x, polypoint.y );
  220. // add left rounded end:
  221. for( int ii = 0; ii < 1800; ii += delta )
  222. {
  223. corner = wxPoint( 0, -radius );
  224. RotatePoint( &corner, ii );
  225. RotatePoint( &corner, -delta_angle );
  226. corner += startp;
  227. polypoint.x = corner.x;
  228. polypoint.y = corner.y;
  229. aCornerBuffer.Append( polypoint.x, polypoint.y );
  230. }
  231. // Finish arc:
  232. corner = wxPoint( 0, radius );
  233. RotatePoint( &corner, -delta_angle );
  234. corner += startp;
  235. polypoint.x = corner.x;
  236. polypoint.y = corner.y;
  237. aCornerBuffer.Append( polypoint.x, polypoint.y );
  238. }
  239. /**
  240. * Function TransformArcToPolygon
  241. * Creates a polygon from an Arc
  242. * Convert arcs to multiple straight segments
  243. * @param aCornerBuffer = a buffer to store the polygon
  244. * @param aCentre = centre of the arc or circle
  245. * @param aStart = start point of the arc, or a point on the circle
  246. * @param aArcAngle = arc angle in 0.1 degrees. For a circle, aArcAngle = 3600
  247. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  248. * @param aWidth = width (thickness) of the line
  249. */
  250. void TransformArcToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  251. wxPoint aCentre, wxPoint aStart, double aArcAngle,
  252. int aCircleToSegmentsCount, int aWidth )
  253. {
  254. wxPoint arc_start, arc_end;
  255. int delta = 3600 / aCircleToSegmentsCount; // rotate angle in 0.1 degree
  256. arc_end = arc_start = aStart;
  257. if( aArcAngle != 3600 )
  258. {
  259. RotatePoint( &arc_end, aCentre, -aArcAngle );
  260. }
  261. if( aArcAngle < 0 )
  262. {
  263. std::swap( arc_start, arc_end );
  264. aArcAngle = -aArcAngle;
  265. }
  266. // Compute the ends of segments and creates poly
  267. wxPoint curr_end = arc_start;
  268. wxPoint curr_start = arc_start;
  269. for( int ii = delta; ii < aArcAngle; ii += delta )
  270. {
  271. curr_end = arc_start;
  272. RotatePoint( &curr_end, aCentre, -ii );
  273. TransformRoundedEndsSegmentToPolygon( aCornerBuffer, curr_start, curr_end,
  274. aCircleToSegmentsCount, aWidth );
  275. curr_start = curr_end;
  276. }
  277. if( curr_end != arc_end )
  278. TransformRoundedEndsSegmentToPolygon( aCornerBuffer,
  279. curr_end, arc_end,
  280. aCircleToSegmentsCount, aWidth );
  281. }
  282. /**
  283. * Function TransformRingToPolygon
  284. * Creates a polygon from a ring
  285. * Convert arcs to multiple straight segments
  286. * @param aCornerBuffer = a buffer to store the polygon
  287. * @param aCentre = centre of the arc or circle
  288. * @param aRadius = radius of the circle
  289. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  290. * @param aWidth = width (thickness) of the ring
  291. */
  292. void TransformRingToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  293. wxPoint aCentre, int aRadius,
  294. int aCircleToSegmentsCount, int aWidth )
  295. {
  296. // Compute the corners positions and creates the poly
  297. wxPoint curr_point;
  298. int inner_radius = aRadius - ( aWidth / 2 );
  299. int outer_radius = inner_radius + aWidth;
  300. if( inner_radius <= 0 )
  301. { //In this case, the ring is just a circle (no hole inside)
  302. TransformCircleToPolygon( aCornerBuffer, aCentre, aRadius + ( aWidth / 2 ),
  303. aCircleToSegmentsCount );
  304. return;
  305. }
  306. aCornerBuffer.NewOutline();
  307. // Draw the inner circle of the ring
  308. int delta = 3600 / aCircleToSegmentsCount; // rotate angle in 0.1 degree
  309. for( int ii = 0; ii < 3600; ii += delta )
  310. {
  311. curr_point.x = inner_radius;
  312. curr_point.y = 0;
  313. RotatePoint( &curr_point, ii );
  314. curr_point += aCentre;
  315. aCornerBuffer.Append( curr_point.x, curr_point.y );
  316. }
  317. // Draw the last point of inner circle
  318. aCornerBuffer.Append( aCentre.x + inner_radius, aCentre.y );
  319. // Draw the outer circle of the ring
  320. // the first point creates also a segment from the inner to the outer polygon
  321. for( int ii = 0; ii < 3600; ii += delta )
  322. {
  323. curr_point.x = outer_radius;
  324. curr_point.y = 0;
  325. RotatePoint( &curr_point, -ii );
  326. curr_point += aCentre;
  327. aCornerBuffer.Append( curr_point.x, curr_point.y );
  328. }
  329. // Draw the last point of outer circle
  330. aCornerBuffer.Append( aCentre.x + outer_radius, aCentre.y );
  331. // And connect the outer polygon to the inner polygon,.
  332. // because a segment from inner to the outer polygon was already created,
  333. // the final polygon is the inner and the outer outlines connected by
  334. // 2 overlapping segments
  335. aCornerBuffer.Append( aCentre.x + inner_radius, aCentre.y );
  336. }