KiCad PCB EDA Suite
bezier_curves.h
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) 2009-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-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 #ifndef BEZIER_CURVES_H
26 #define BEZIER_CURVES_H
27 
28 #include <vector>
29 #include <wx/gdicmn.h>
30 
36 {
37 public:
39  BEZIER_POLY( int x1, int y1, int x2, int y2, int x3, int y3 )
40  {
41  m_ctrlPts.emplace_back( x1, y1 );
42  m_ctrlPts.emplace_back( x2, y2 );
43  m_ctrlPts.emplace_back( x3, y3 );
44  m_output = nullptr;
45  m_minSegLen = 0;
46  }
47 
49  BEZIER_POLY( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 )
50  {
51  m_ctrlPts.emplace_back( x1, y1 );
52  m_ctrlPts.emplace_back( x2, y2 );
53  m_ctrlPts.emplace_back( x3, y3 );
54  m_ctrlPts.emplace_back( x4, y4 );
55  m_output = nullptr;
56  m_minSegLen = 0;
57  }
58 
59  BEZIER_POLY( const std::vector<wxPoint>& aControlPoints )
60  : m_ctrlPts( aControlPoints )
61  {
62  m_output = nullptr;
63  m_minSegLen = 0;
64  }
65 
73  void GetPoly( std::vector<wxPoint>& aOutput, int aMinSegLen = 0 );
74 
75 private:
77 
79  std::vector<wxPoint> m_ctrlPts;
80 
82  std::vector<wxPoint>* m_output;
83 
84  void addSegment( const wxPoint& aSegment )
85  {
86  int seglen = std::abs( m_output->back().x - aSegment.x )
87  + std::abs( m_output->back().y - aSegment.y );
88 
89  // m_minSegLen is always > 0, so never store a 0 len segment
90  if( seglen >= m_minSegLen )
91  m_output->push_back( aSegment );
92  }
93 
94  void recursiveBezier( int x1, int y1, int x2, int y2, int x3, int y3, unsigned int level );
95  void recursiveBezier( int x1, int y1, int x2, int y2,
96  int x3, int y3, int x4, int y4, unsigned int level );
97 
98 
99  // Conversion parameters
100  constexpr static double angle_tolerance = 0.0;
101  constexpr static double cusp_limit = 0.0;
102  constexpr static unsigned int recursion_limit = 12;
103  constexpr static double approximation_scale = 0.5; // 1
104  constexpr static double distance_tolerance_square = ( 0.5 / approximation_scale ) * ( 0.5 / approximation_scale );
105 
106  constexpr static double curve_collinearity_epsilon = 1e-30;
107  constexpr static double curve_angle_tolerance_epsilon = 0.0001;
108 };
109 
110 #endif // BEZIER_CURVES_H
BEZIER_POLY(const std::vector< wxPoint > &aControlPoints)
Definition: bezier_curves.h:59
static constexpr double distance_tolerance_square
static constexpr double approximation_scale
BEZIER_POLY(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
Quadratic and cubic Bezier curve.
Definition: bezier_curves.h:49
static constexpr double angle_tolerance
void GetPoly(std::vector< wxPoint > &aOutput, int aMinSegLen=0)
Converts Bezier curve to a polygon.
#define abs(a)
Definition: auxiliary.h:84
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:79
void addSegment(const wxPoint &aSegment)
Definition: bezier_curves.h:84
static constexpr double curve_collinearity_epsilon
BEZIER_POLY(int x1, int y1, int x2, int y2, int x3, int y3)
cubic Bezier curve
Definition: bezier_curves.h:39
static constexpr double cusp_limit
std::vector< wxPoint > * m_output
Pointer to the output vector
Definition: bezier_curves.h:82
static constexpr double curve_angle_tolerance_epsilon
Bezier curves to polygon converter.
Definition: bezier_curves.h:35
static constexpr unsigned int recursion_limit