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