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.

384 lines
12 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2014-2017 KiCad Developers, see CHANGELOG.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. /* routines to handle bezier curves */
  26. /************************************/
  27. #include <fctsys.h>
  28. #include <bezier_curves.h>
  29. static inline double calc_sq_distance( int x1, int y1, int x2, int y2 )
  30. {
  31. int dx = x2 - x1;
  32. int dy = y2 - y1;
  33. return (double)dx * dx + (double)dy * dy;
  34. }
  35. static inline double sqrt_len( int dx, int dy )
  36. {
  37. return ((double)dx * dx) + ((double)dy * dy);
  38. }
  39. void BEZIER_POLY::GetPoly( std::vector<wxPoint>& aOutput )
  40. {
  41. wxCHECK( !m_ctrlPts.empty(), /* void */ );
  42. m_output = &aOutput;
  43. m_output->clear();
  44. m_output->push_back( wxPoint( m_ctrlPts.front() ) );
  45. // Only quadratic and cubic Bezier curves are handled
  46. if( m_ctrlPts.size() == 3 )
  47. recursiveBezier( m_ctrlPts[0].x, m_ctrlPts[0].y,
  48. m_ctrlPts[1].x, m_ctrlPts[1].y,
  49. m_ctrlPts[2].x, m_ctrlPts[2].y, 0 );
  50. else if( m_ctrlPts.size() == 4 )
  51. recursiveBezier( m_ctrlPts[0].x, m_ctrlPts[0].y,
  52. m_ctrlPts[1].x, m_ctrlPts[1].y,
  53. m_ctrlPts[2].x, m_ctrlPts[2].y,
  54. m_ctrlPts[3].x, m_ctrlPts[3].y, 0 );
  55. m_output->push_back( wxPoint( m_ctrlPts.back() ) );
  56. }
  57. void BEZIER_POLY::recursiveBezier( int x1, int y1, int x2, int y2, int x3, int y3, unsigned int level )
  58. {
  59. if( level > recursion_limit )
  60. return;
  61. // Calculate all the mid-points of the line segments
  62. //----------------------
  63. int x12 = (x1 + x2) / 2;
  64. int y12 = (y1 + y2) / 2;
  65. int x23 = (x2 + x3) / 2;
  66. int y23 = (y2 + y3) / 2;
  67. int x123 = (x12 + x23) / 2;
  68. int y123 = (y12 + y23) / 2;
  69. int dx = x3 - x1;
  70. int dy = y3 - y1;
  71. double d = fabs( ((double) (x2 - x3) * dy) - ((double) (y2 - y3) * dx ) );
  72. double da;
  73. if( d > curve_collinearity_epsilon )
  74. {
  75. // Regular case
  76. //-----------------
  77. if( d * d <= distance_tolerance_square * (dx * dx + dy * dy) )
  78. {
  79. // If the curvature doesn't exceed the distance_tolerance value
  80. // we tend to finish subdivisions.
  81. //----------------------
  82. if( angle_tolerance < curve_angle_tolerance_epsilon )
  83. {
  84. addSegment( wxPoint( x123, y123 ) );
  85. return;
  86. }
  87. // Angle & Cusp Condition
  88. //----------------------
  89. da = fabs( atan2( (double) ( y3 - y2 ), (double) ( x3 - x2 ) ) -
  90. atan2( (double) ( y2 - y1 ), (double) ( x2 - x1 ) ) );
  91. if( da >=M_PI )
  92. da = 2 * M_PI - da;
  93. if( da < angle_tolerance )
  94. {
  95. // Finally we can stop the recursion
  96. //----------------------
  97. addSegment( wxPoint( x123, y123 ) );
  98. return;
  99. }
  100. }
  101. }
  102. else
  103. {
  104. // Collinear case
  105. //------------------
  106. da = sqrt_len(dx, dy);
  107. if( da == 0 )
  108. {
  109. d = calc_sq_distance( x1, y1, x2, y2 );
  110. }
  111. else
  112. {
  113. d = ( (double)(x2 - x1) * dx + (double)(y2 - y1) * dy ) / da;
  114. if( d > 0 && d < 1 )
  115. {
  116. // Simple collinear case, 1---2---3
  117. // We can leave just two endpoints
  118. return;
  119. }
  120. if( d <= 0 )
  121. d = calc_sq_distance( x2, y2, x1, y1 );
  122. else if( d >= 1 )
  123. d = calc_sq_distance( x2, y2, x3, y3 );
  124. else
  125. d = calc_sq_distance( x2, y2, x1 + (int) d * dx,
  126. y1 + (int) d * dy );
  127. }
  128. if( d < distance_tolerance_square )
  129. {
  130. addSegment( wxPoint( x2, y2 ) );
  131. return;
  132. }
  133. }
  134. // Continue subdivision
  135. //----------------------
  136. recursiveBezier( x1, y1, x12, y12, x123, y123, level + 1 );
  137. recursiveBezier( x123, y123, x23, y23, x3, y3, -(level + 1) );
  138. }
  139. void BEZIER_POLY::recursiveBezier( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, unsigned int level )
  140. {
  141. if( level > recursion_limit )
  142. return;
  143. // Calculate all the mid-points of the line segments
  144. //----------------------
  145. int x12 = (x1 + x2) / 2;
  146. int y12 = (y1 + y2) / 2;
  147. int x23 = (x2 + x3) / 2;
  148. int y23 = (y2 + y3) / 2;
  149. int x34 = (x3 + x4) / 2;
  150. int y34 = (y3 + y4) / 2;
  151. int x123 = (x12 + x23) / 2;
  152. int y123 = (y12 + y23) / 2;
  153. int x234 = (x23 + x34) / 2;
  154. int y234 = (y23 + y34) / 2;
  155. int x1234 = (x123 + x234) / 2;
  156. int y1234 = (y123 + y234) / 2;
  157. // Try to approximate the full cubic curve by a single straight line
  158. //------------------
  159. int dx = x4 - x1;
  160. int dy = y4 - y1;
  161. double d2 = fabs( (double) ( (x2 - x4) * dy - (y2 - y4) * dx ) );
  162. double d3 = fabs( (double) ( (x3 - x4) * dy - (y3 - y4) * dx ) );
  163. double da1, da2, k;
  164. switch( (int(d2 > curve_collinearity_epsilon) << 1) +
  165. int(d3 > curve_collinearity_epsilon) )
  166. {
  167. case 0:
  168. // All collinear OR p1==p4
  169. //----------------------
  170. k = dx * dx + dy * dy;
  171. if( k == 0 )
  172. {
  173. d2 = calc_sq_distance( x1, y1, x2, y2 );
  174. d3 = calc_sq_distance( x4, y4, x3, y3 );
  175. }
  176. else
  177. {
  178. k = 1 / k;
  179. da1 = x2 - x1;
  180. da2 = y2 - y1;
  181. d2 = k * (da1 * dx + da2 * dy);
  182. da1 = x3 - x1;
  183. da2 = y3 - y1;
  184. d3 = k * (da1 * dx + da2 * dy);
  185. if( d2 > 0 && d2 < 1 && d3 > 0 && d3 < 1 )
  186. {
  187. // Simple collinear case, 1---2---3---4
  188. // We can leave just two endpoints
  189. return;
  190. }
  191. if( d2 <= 0 )
  192. d2 = calc_sq_distance( x2, y2, x1, y1 );
  193. else if( d2 >= 1 )
  194. d2 = calc_sq_distance( x2, y2, x4, y4 );
  195. else
  196. d2 = calc_sq_distance( x2, y2, x1 + (int) d2 * dx,
  197. y1 + (int) d2 * dy );
  198. if( d3 <= 0 )
  199. d3 = calc_sq_distance( x3, y3, x1, y1 );
  200. else if( d3 >= 1 )
  201. d3 = calc_sq_distance( x3, y3, x4, y4 );
  202. else
  203. d3 = calc_sq_distance( x3, y3, x1 + (int) d3 * dx,
  204. y1 + (int) d3 * dy );
  205. }
  206. if( d2 > d3 )
  207. {
  208. if( d2 < distance_tolerance_square )
  209. {
  210. addSegment( wxPoint( x2, y2 ) );
  211. return;
  212. }
  213. }
  214. else
  215. {
  216. if( d3 < distance_tolerance_square )
  217. {
  218. addSegment( wxPoint( x3, y3 ) );
  219. return;
  220. }
  221. }
  222. break;
  223. case 1:
  224. // p1,p2,p4 are collinear, p3 is significant
  225. //----------------------
  226. if( d3 * d3 <= distance_tolerance_square * sqrt_len(dx, dy) )
  227. {
  228. if( angle_tolerance < curve_angle_tolerance_epsilon )
  229. {
  230. addSegment( wxPoint( x23, y23 ) );
  231. return;
  232. }
  233. // Angle Condition
  234. //----------------------
  235. da1 = fabs( atan2( (double) ( y4 - y3 ), (double) ( x4 - x3 ) ) -
  236. atan2( (double) ( y3 - y2 ), (double) ( x3 - x2 ) ) );
  237. if( da1 >= M_PI )
  238. da1 = 2 * M_PI - da1;
  239. if( da1 < angle_tolerance )
  240. {
  241. addSegment( wxPoint( x2, y2 ) );
  242. addSegment( wxPoint( x3, y3 ) );
  243. return;
  244. }
  245. if( cusp_limit != 0.0 )
  246. {
  247. if( da1 > cusp_limit )
  248. {
  249. addSegment( wxPoint( x3, y3 ) );
  250. return;
  251. }
  252. }
  253. }
  254. break;
  255. case 2:
  256. // p1,p3,p4 are collinear, p2 is significant
  257. //----------------------
  258. if( d2 * d2 <= distance_tolerance_square * sqrt_len(dx, dy) )
  259. {
  260. if( angle_tolerance < curve_angle_tolerance_epsilon )
  261. {
  262. addSegment( wxPoint( x23, y23 ) );
  263. return;
  264. }
  265. // Angle Condition
  266. //----------------------
  267. da1 = fabs( atan2( (double) ( y3 - y2 ), (double) ( x3 - x2 ) ) -
  268. atan2( (double) ( y2 - y1 ), (double) ( x2 - x1 ) ) );
  269. if( da1 >= M_PI )
  270. da1 = 2 * M_PI - da1;
  271. if( da1 < angle_tolerance )
  272. {
  273. addSegment( wxPoint( x2, y2 ) );
  274. addSegment( wxPoint( x3, y3 ) );
  275. return;
  276. }
  277. if( cusp_limit != 0.0 )
  278. {
  279. if( da1 > cusp_limit )
  280. {
  281. addSegment( wxPoint( x2, y2 ) );
  282. return;
  283. }
  284. }
  285. }
  286. break;
  287. case 3:
  288. // Regular case
  289. //-----------------
  290. if( (d2 + d3) * (d2 + d3) <= distance_tolerance_square * sqrt_len(dx, dy) )
  291. {
  292. // If the curvature doesn't exceed the distance_tolerance value
  293. // we tend to finish subdivisions.
  294. //----------------------
  295. if( angle_tolerance < curve_angle_tolerance_epsilon )
  296. {
  297. addSegment( wxPoint( x23, y23 ) );
  298. return;
  299. }
  300. // Angle & Cusp Condition
  301. //----------------------
  302. k = atan2( (double) ( y3 - y2 ), (double) ( x3 - x2 ) );
  303. da1 = fabs( k - atan2( (double) ( y2 - y1 ),
  304. (double) ( x2 - x1 ) ) );
  305. da2 = fabs( atan2( (double) ( y4 - y3 ),
  306. (double) ( x4 - x3 ) ) - k );
  307. if( da1 >= M_PI )
  308. da1 = 2 * M_PI - da1;
  309. if( da2 >= M_PI )
  310. da2 = 2 * M_PI - da2;
  311. if( da1 + da2 < angle_tolerance )
  312. {
  313. // Finally we can stop the recursion
  314. //----------------------
  315. addSegment( wxPoint( x23, y23 ) );
  316. return;
  317. }
  318. if( cusp_limit != 0.0 )
  319. {
  320. if( da1 > cusp_limit )
  321. {
  322. addSegment( wxPoint( x2, y2 ) );
  323. return;
  324. }
  325. if( da2 > cusp_limit )
  326. {
  327. addSegment( wxPoint( x3, y3 ) );
  328. return;
  329. }
  330. }
  331. }
  332. break;
  333. }
  334. // Continue subdivision
  335. //----------------------
  336. recursiveBezier( x1, y1, x12, y12, x123, y123, x1234, y1234, level + 1 );
  337. recursiveBezier( x1234, y1234, x234, y234, x34, y34, x4, y4, level + 1 );
  338. }