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 
30 BEZIER_POLY::BEZIER_POLY( const std::vector<wxPoint>& aControlPoints )
31 {
32  for( unsigned ii = 0; ii < aControlPoints.size(); ++ii )
33  m_ctrlPts.emplace_back( VECTOR2D( aControlPoints[ii] ) );
34 
35  m_minSegLen = 0.0;
36 }
37 
38 
39 void BEZIER_POLY::GetPoly( std::vector<wxPoint>& aOutput, int aMinSegLen )
40 {
41  aOutput.clear();
42  std::vector<VECTOR2D> buffer;
43  GetPoly( buffer, double( aMinSegLen ) );
44 
45  for( unsigned ii = 0; ii < buffer.size(); ++ii )
46  aOutput.emplace_back( wxPoint( int( buffer[ii].x ), int( buffer[ii].y ) ) );
47 }
48 
49 
50 void BEZIER_POLY::GetPoly( std::vector<VECTOR2D>& aOutput, double aMinSegLen )
51 {
52  wxASSERT( m_ctrlPts.size() == 4 );
53  // FIXME Brute force method, use a better (recursive?) algorithm
54  // with a max error value.
55  // to optimize the number of segments
56  #define CURVE_POINTS 32
57  double dt = 1.0 / CURVE_POINTS;
58 
59  aOutput.clear();
60  aOutput.push_back( m_ctrlPts[0] );
61 
62  // If the Bezier curve is degenerated (straight line), skip intermediate points:
63  bool degenerated = m_ctrlPts[0] == m_ctrlPts[1] && m_ctrlPts[2] == m_ctrlPts[3];
64 
65  if( !degenerated )
66  {
67  for( int ii = 1; ii < CURVE_POINTS; ii++ )
68  {
69  double t = dt * ii;
70  double omt = 1.0 - t;
71  double omt2 = omt * omt;
72  double omt3 = omt * omt2;
73  double t2 = t * t;
74  double t3 = t * t2;
75 
76  VECTOR2D vertex = omt3 * m_ctrlPts[0]
77  + 3.0 * t * omt2 * m_ctrlPts[1]
78  + 3.0 * t2 * omt * m_ctrlPts[2]
79  + t3 * m_ctrlPts[3];
80 
81  // a minimal filter on the lenght of the segment being created:
82  // The offset from last point:
83  VECTOR2D delta = vertex - aOutput.back();
84  double dist = delta.EuclideanNorm();
85 
86  if( dist > aMinSegLen )
87  aOutput.push_back( vertex );
88  }
89  }
90 
91  if( aOutput.back() != m_ctrlPts[3] )
92  aOutput.push_back( m_ctrlPts[3] );
93 }
BEZIER_POLY(const std::vector< wxPoint > &aControlPoints)
static const int dist[10][10]
Definition: ar_matrix.cpp:320
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.
#define CURVE_POINTS
double m_minSegLen
Definition: bezier_curves.h:57
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:292