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-2019 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 /************************************/
25 /* routines to handle bezier curves */
26 /************************************/
27 
28 #include <bezier_curves.h>
29 #include <math/vector2d.h> // for VECTOR2D, operator*, VECTOR2
30 #include <wx/debug.h> // for wxASSERT
31 #include <wx/gdicmn.h> // for wxPoint
32 
33 
34 BEZIER_POLY::BEZIER_POLY( const std::vector<wxPoint>& aControlPoints )
35 {
36  for( unsigned ii = 0; ii < aControlPoints.size(); ++ii )
37  m_ctrlPts.emplace_back( VECTOR2D( aControlPoints[ii] ) );
38 
39  m_minSegLen = 0.0;
40 }
41 
42 
43 void BEZIER_POLY::GetPoly( std::vector<wxPoint>& aOutput, int aMinSegLen )
44 {
45  aOutput.clear();
46  std::vector<VECTOR2D> buffer;
47  GetPoly( buffer, double( aMinSegLen ) );
48 
49  for( unsigned ii = 0; ii < buffer.size(); ++ii )
50  aOutput.emplace_back( wxPoint( int( buffer[ii].x ), int( buffer[ii].y ) ) );
51 }
52 
53 
54 void BEZIER_POLY::GetPoly( std::vector<VECTOR2D>& aOutput, double aMinSegLen )
55 {
56  wxASSERT( m_ctrlPts.size() == 4 );
57  // FIXME Brute force method, use a better (recursive?) algorithm
58  // with a max error value.
59  // to optimize the number of segments
60  #define CURVE_POINTS 32
61  double dt = 1.0 / CURVE_POINTS;
62 
63  aOutput.clear();
64  aOutput.push_back( m_ctrlPts[0] );
65 
66  // If the Bezier curve is degenerated (straight line), skip intermediate points:
67  bool degenerated = m_ctrlPts[0] == m_ctrlPts[1] && m_ctrlPts[2] == m_ctrlPts[3];
68 
69  if( !degenerated )
70  {
71  for( int ii = 1; ii < CURVE_POINTS; ii++ )
72  {
73  double t = dt * ii;
74  double omt = 1.0 - t;
75  double omt2 = omt * omt;
76  double omt3 = omt * omt2;
77  double t2 = t * t;
78  double t3 = t * t2;
79 
80  VECTOR2D vertex = omt3 * m_ctrlPts[0]
81  + 3.0 * t * omt2 * m_ctrlPts[1]
82  + 3.0 * t2 * omt * m_ctrlPts[2]
83  + t3 * m_ctrlPts[3];
84 
85  // a minimal filter on the lenght of the segment being created:
86  // The offset from last point:
87  VECTOR2D delta = vertex - aOutput.back();
88  double dist = delta.EuclideanNorm();
89 
90  if( dist > aMinSegLen )
91  aOutput.push_back( vertex );
92  }
93  }
94 
95  if( aOutput.back() != m_ctrlPts[3] )
96  aOutput.push_back( m_ctrlPts[3] );
97 }
BEZIER_POLY(const std::vector< wxPoint > &aControlPoints)
static const int dist[10][10]
Definition: ar_matrix.cpp:326
std::vector< VECTOR2D > m_ctrlPts
Control points
Definition: bezier_curves.h:60
void GetPoly(std::vector< wxPoint > &aOutput, int aMinSegLen=0)
Converts Bezier curve to a polygon.
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
#define CURVE_POINTS
double m_minSegLen
Definition: bezier_curves.h:57
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:299