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.

436 lines
15 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. // To build the polygonal shape outside the actual shape, we use a bigger
  66. // radius to build rounded ends.
  67. // However, the width of the segment is too big.
  68. // so, later, we will clamp the polygonal shape with the bounding box
  69. // of the segment.
  70. int radius = aWidth / 2;
  71. // Note if we want to compensate the radius reduction of a circle due to
  72. // the segment approx, aCorrectionFactor must be calculated like this:
  73. // For a circle the min radius is radius * cos( 2PI / s_CircleToSegmentsCount / 2)
  74. // aCorrectionFactor is 1 /cos( PI/s_CircleToSegmentsCount )
  75. radius = radius * aCorrectionFactor; // make segments outside the circles
  76. // end point is the coordinate relative to aStart
  77. wxPoint endp = aEnd - aStart;
  78. wxPoint startp = aStart;
  79. wxPoint corner;
  80. SHAPE_POLY_SET polyshape;
  81. polyshape.NewOutline();
  82. // normalize the position in order to have endp.x >= 0
  83. // it makes calculations more easy to understand
  84. if( endp.x < 0 )
  85. {
  86. endp = aStart - aEnd;
  87. startp = aEnd;
  88. }
  89. // delta_angle is in radian
  90. double delta_angle = atan2( (double)endp.y, (double)endp.x );
  91. int seg_len = KiROUND( EuclideanNorm( endp ) );
  92. double delta = 3600.0 / aCircleToSegmentsCount; // rot angle in 0.1 degree
  93. // Compute the outlines of the segment, and creates a polygon
  94. // Note: the polygonal shape is built from the equivalent horizontal
  95. // segment starting ar 0,0, and ending at seg_len,0
  96. // add right rounded end:
  97. for( int ii = 0; ii < aCircleToSegmentsCount/2; ii++ )
  98. {
  99. corner = wxPoint( 0, radius );
  100. RotatePoint( &corner, delta*ii );
  101. corner.x += seg_len;
  102. polyshape.Append( corner.x, corner.y );
  103. }
  104. // Finish arc:
  105. corner = wxPoint( seg_len, -radius );
  106. polyshape.Append( corner.x, corner.y );
  107. // add left rounded end:
  108. for( int ii = 0; ii < aCircleToSegmentsCount/2; ii++ )
  109. {
  110. corner = wxPoint( 0, -radius );
  111. RotatePoint( &corner, delta*ii );
  112. polyshape.Append( corner.x, corner.y );
  113. }
  114. // Finish arc:
  115. corner = wxPoint( 0, radius );
  116. polyshape.Append( corner.x, corner.y );
  117. // Now, clamp the polygonal shape (too big) with the segment bounding box
  118. // the polygonal shape bbox equivalent to the segment has a too big height,
  119. // and the right width
  120. if( aCorrectionFactor > 1.0 )
  121. {
  122. SHAPE_POLY_SET bbox;
  123. bbox.NewOutline();
  124. // Build the bbox (a horizontal rectangle).
  125. int halfwidth = aWidth / 2; // Use the exact segment width for the bbox height
  126. corner.x = -radius - 2; // use a bbox width slightly bigger to avoid
  127. // creating useless corner at segment ends
  128. corner.y = halfwidth;
  129. bbox.Append( corner.x, corner.y );
  130. corner.y = -halfwidth;
  131. bbox.Append( corner.x, corner.y );
  132. corner.x = radius + seg_len + 2;
  133. bbox.Append( corner.x, corner.y );
  134. corner.y = halfwidth;
  135. bbox.Append( corner.x, corner.y );
  136. // Now, clamp the shape
  137. polyshape.BooleanIntersection( bbox, SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
  138. // Note the final polygon is a simple, convex polygon with no hole
  139. // due to the shape of initial polygons
  140. }
  141. // Rotate and move the polygon to its right location
  142. polyshape.Rotate( delta_angle, VECTOR2I( 0, 0 ) );
  143. polyshape.Move( startp );
  144. aCornerBuffer.Append( polyshape);
  145. }
  146. /* Returns the centers of the rounded corners of a rect.
  147. */
  148. void GetRoundRectCornerCenters( wxPoint aCenters[4], int aRadius,
  149. const wxPoint& aPosition, const wxSize& aSize, double aRotation )
  150. {
  151. wxSize size( aSize/2 );
  152. size.x -= aRadius;
  153. size.y -= aRadius;
  154. // Ensure size is > 0, to avoid generating unusable shapes
  155. // which can crash kicad.
  156. if( size.x <= 1 )
  157. size.x = 1;
  158. if( size.y <= 1 )
  159. size.y = 1;
  160. aCenters[0].x = -size.x;
  161. aCenters[0].y = size.y;
  162. aCenters[1].x = size.x;
  163. aCenters[1].y = size.y;
  164. aCenters[2].x = size.x;
  165. aCenters[2].y = -size.y;
  166. aCenters[3].x = -size.x;
  167. aCenters[3].y = -size.y;
  168. // Rotate the polygon
  169. if( aRotation )
  170. {
  171. for( int ii = 0; ii < 4; ii++ )
  172. RotatePoint( &aCenters[ii], aRotation );
  173. }
  174. // move the polygon to the position
  175. for( int ii = 0; ii < 4; ii++ )
  176. aCenters[ii] += aPosition;
  177. }
  178. /**
  179. * Function TransformRoundRectToPolygon
  180. * convert a rectangle with rounded corners to a polygon
  181. * Convert arcs to multiple straight lines
  182. * @param aCornerBuffer = a buffer to store the polygon
  183. * @param aPosition = the coordinate of the center of the rectangle
  184. * @param aSize = the size of the rectangle
  185. * @param aCornerRadius = radius of rounded corners
  186. * @param aRotation = rotation in 0.1 degrees of the rectangle
  187. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  188. */
  189. void TransformRoundRectToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  190. const wxPoint& aPosition, const wxSize& aSize,
  191. double aRotation, int aCornerRadius,
  192. int aCircleToSegmentsCount )
  193. {
  194. wxPoint corners[4];
  195. GetRoundRectCornerCenters( corners, aCornerRadius, aPosition, aSize, aRotation );
  196. SHAPE_POLY_SET outline;
  197. outline.NewOutline();
  198. for( int ii = 0; ii < 4; ++ii )
  199. outline.Append( corners[ii].x, corners[ii].y );
  200. outline.Inflate( aCornerRadius, aCircleToSegmentsCount );
  201. // Add the outline:
  202. aCornerBuffer.Append( outline );
  203. }
  204. /**
  205. * Function TransformRoundedEndsSegmentToPolygon
  206. * convert a segment with rounded ends to a polygon
  207. * Convert arcs to multiple straight lines
  208. * @param aCornerBuffer = a buffer to store the polygon
  209. * @param aStart = the segment start point coordinate
  210. * @param aEnd = the segment end point coordinate
  211. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  212. * @param aWidth = the segment width
  213. * Note: the polygon is inside the arc ends, so if you want to have the polygon
  214. * outside the circle, you should give aStart and aEnd calculated with a correction factor
  215. */
  216. void TransformRoundedEndsSegmentToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  217. wxPoint aStart, wxPoint aEnd,
  218. int aCircleToSegmentsCount,
  219. int aWidth )
  220. {
  221. int radius = aWidth / 2;
  222. wxPoint endp = aEnd - aStart; // end point coordinate for the same segment starting at (0,0)
  223. wxPoint startp = aStart;
  224. wxPoint corner;
  225. VECTOR2I polypoint;
  226. aCornerBuffer.NewOutline();
  227. // normalize the position in order to have endp.x >= 0;
  228. if( endp.x < 0 )
  229. {
  230. endp = aStart - aEnd;
  231. startp = aEnd;
  232. }
  233. double delta_angle = ArcTangente( endp.y, endp.x ); // delta_angle is in 0.1 degrees
  234. int seg_len = KiROUND( EuclideanNorm( endp ) );
  235. int delta = 3600 / aCircleToSegmentsCount; // rot angle in 0.1 degree
  236. // Compute the outlines of the segment, and creates a polygon
  237. // add right rounded end:
  238. for( int ii = 0; ii < 1800; ii += delta )
  239. {
  240. corner = wxPoint( 0, radius );
  241. RotatePoint( &corner, ii );
  242. corner.x += seg_len;
  243. RotatePoint( &corner, -delta_angle );
  244. corner += startp;
  245. polypoint.x = corner.x;
  246. polypoint.y = corner.y;
  247. aCornerBuffer.Append( polypoint.x, polypoint.y );
  248. }
  249. // Finish arc:
  250. corner = wxPoint( seg_len, -radius );
  251. RotatePoint( &corner, -delta_angle );
  252. corner += startp;
  253. polypoint.x = corner.x;
  254. polypoint.y = corner.y;
  255. aCornerBuffer.Append( polypoint.x, polypoint.y );
  256. // add left rounded end:
  257. for( int ii = 0; ii < 1800; ii += delta )
  258. {
  259. corner = wxPoint( 0, -radius );
  260. RotatePoint( &corner, ii );
  261. RotatePoint( &corner, -delta_angle );
  262. corner += startp;
  263. polypoint.x = corner.x;
  264. polypoint.y = corner.y;
  265. aCornerBuffer.Append( polypoint.x, polypoint.y );
  266. }
  267. // Finish arc:
  268. corner = wxPoint( 0, radius );
  269. RotatePoint( &corner, -delta_angle );
  270. corner += startp;
  271. polypoint.x = corner.x;
  272. polypoint.y = corner.y;
  273. aCornerBuffer.Append( polypoint.x, polypoint.y );
  274. }
  275. /**
  276. * Function TransformArcToPolygon
  277. * Creates a polygon from an Arc
  278. * Convert arcs to multiple straight segments
  279. * @param aCornerBuffer = a buffer to store the polygon
  280. * @param aCentre = centre of the arc or circle
  281. * @param aStart = start point of the arc, or a point on the circle
  282. * @param aArcAngle = arc angle in 0.1 degrees. For a circle, aArcAngle = 3600
  283. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  284. * @param aWidth = width (thickness) of the line
  285. */
  286. void TransformArcToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  287. wxPoint aCentre, wxPoint aStart, double aArcAngle,
  288. int aCircleToSegmentsCount, int aWidth )
  289. {
  290. wxPoint arc_start, arc_end;
  291. int delta = 3600 / aCircleToSegmentsCount; // rotate angle in 0.1 degree
  292. arc_end = arc_start = aStart;
  293. if( aArcAngle != 3600 )
  294. {
  295. RotatePoint( &arc_end, aCentre, -aArcAngle );
  296. }
  297. if( aArcAngle < 0 )
  298. {
  299. std::swap( arc_start, arc_end );
  300. aArcAngle = -aArcAngle;
  301. }
  302. // Compute the ends of segments and creates poly
  303. wxPoint curr_end = arc_start;
  304. wxPoint curr_start = arc_start;
  305. for( int ii = delta; ii < aArcAngle; ii += delta )
  306. {
  307. curr_end = arc_start;
  308. RotatePoint( &curr_end, aCentre, -ii );
  309. TransformRoundedEndsSegmentToPolygon( aCornerBuffer, curr_start, curr_end,
  310. aCircleToSegmentsCount, aWidth );
  311. curr_start = curr_end;
  312. }
  313. if( curr_end != arc_end )
  314. TransformRoundedEndsSegmentToPolygon( aCornerBuffer,
  315. curr_end, arc_end,
  316. aCircleToSegmentsCount, aWidth );
  317. }
  318. /**
  319. * Function TransformRingToPolygon
  320. * Creates a polygon from a ring
  321. * Convert arcs to multiple straight segments
  322. * @param aCornerBuffer = a buffer to store the polygon
  323. * @param aCentre = centre of the arc or circle
  324. * @param aRadius = radius of the circle
  325. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  326. * @param aWidth = width (thickness) of the ring
  327. */
  328. void TransformRingToPolygon( SHAPE_POLY_SET& aCornerBuffer,
  329. wxPoint aCentre, int aRadius,
  330. int aCircleToSegmentsCount, int aWidth )
  331. {
  332. // Compute the corners positions and creates the poly
  333. wxPoint curr_point;
  334. int inner_radius = aRadius - ( aWidth / 2 );
  335. int outer_radius = inner_radius + aWidth;
  336. if( inner_radius <= 0 )
  337. { //In this case, the ring is just a circle (no hole inside)
  338. TransformCircleToPolygon( aCornerBuffer, aCentre, aRadius + ( aWidth / 2 ),
  339. aCircleToSegmentsCount );
  340. return;
  341. }
  342. aCornerBuffer.NewOutline();
  343. // Draw the inner circle of the ring
  344. int delta = 3600 / aCircleToSegmentsCount; // rotate angle in 0.1 degree
  345. for( int ii = 0; ii < 3600; ii += delta )
  346. {
  347. curr_point.x = inner_radius;
  348. curr_point.y = 0;
  349. RotatePoint( &curr_point, ii );
  350. curr_point += aCentre;
  351. aCornerBuffer.Append( curr_point.x, curr_point.y );
  352. }
  353. // Draw the last point of inner circle
  354. aCornerBuffer.Append( aCentre.x + inner_radius, aCentre.y );
  355. // Draw the outer circle of the ring
  356. // the first point creates also a segment from the inner to the outer polygon
  357. for( int ii = 0; ii < 3600; ii += delta )
  358. {
  359. curr_point.x = outer_radius;
  360. curr_point.y = 0;
  361. RotatePoint( &curr_point, -ii );
  362. curr_point += aCentre;
  363. aCornerBuffer.Append( curr_point.x, curr_point.y );
  364. }
  365. // Draw the last point of outer circle
  366. aCornerBuffer.Append( aCentre.x + outer_radius, aCentre.y );
  367. // And connect the outer polygon to the inner polygon,.
  368. // because a segment from inner to the outer polygon was already created,
  369. // the final polygon is the inner and the outer outlines connected by
  370. // 2 overlapping segments
  371. aCornerBuffer.Append( aCentre.x + inner_radius, aCentre.y );
  372. }