KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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) 2014-2021 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#ifndef BEZIER_CURVES_H
25#define BEZIER_CURVES_H
26
27#include <vector>
28#include <math/vector2d.h>
29
30template <typename T> class ELLIPSE;
31
38{
39public:
40 BEZIER_POLY( const VECTOR2I& aStart, const VECTOR2I& aCtrl1,
41 const VECTOR2I& aCtrl2, const VECTOR2I& aEnd );
42
43 BEZIER_POLY( const std::vector<VECTOR2I>& aControlPoints );
44
45 BEZIER_POLY( const std::vector<VECTOR2D>& aControlPoints )
46 : m_ctrlPts( aControlPoints )
47 {
48 m_minSegLen = 0.0;
49 }
50
57 void GetPoly( std::vector<VECTOR2I>& aOutput, int aMaxError = 10 );
58 void GetPoly( std::vector<VECTOR2D>& aOutput, double aMaxError = 10.0 );
59
60private:
61
62 void getQuadPoly( std::vector<VECTOR2D>& aOutput, double aMaxError );
63 void getCubicPoly( std::vector<VECTOR2D>& aOutput, double aMaxError );
64
65 int findInflectionPoints( double& aT1, double& aT2 );
67
69
70 void subdivide( double aT, BEZIER_POLY& aLeft, BEZIER_POLY& aRight );
71 void recursiveSegmentation( std::vector<VECTOR2D>& aOutput, double aMaxError );
72
73 void cubicParabolicApprox( std::vector<VECTOR2D>& aOutput, double aMaxError );
74
75 bool isNaN() const;
76
77 bool isFlat( double aMaxError ) const;
78
79 VECTOR2D eval( double t );
80
82
84 std::vector<VECTOR2D> m_ctrlPts;
85};
86
87
88// TODO: Refactor BEZIER_POLY to use BEZIER
89
93template <typename NumericType>
94class BEZIER
95{
96public:
97 BEZIER() = default;
98
99 constexpr BEZIER( const VECTOR2<NumericType>& aStart, const VECTOR2<NumericType>& aC1,
100 const VECTOR2<NumericType>& aC2, const VECTOR2<NumericType>& aEnd ) :
101 Start( aStart ), C1( aC1 ), C2( aC2 ), End( aEnd )
102 {
103 }
104
114 constexpr VECTOR2<NumericType> PointAt( double aT ) const
115 {
116 const double t2 = aT * aT;
117 const double t3 = t2 * aT;
118 const double t_m1 = 1.0 - aT;
119 const double t_m1_2 = t_m1 * t_m1;
120 const double t_m1_3 = t_m1_2 * t_m1;
121
122 return ( t_m1_3 * Start ) + ( 3.0 * aT * t_m1_2 * C1 ) + ( 3.0 * t2 * t_m1 * C2 )
123 + ( t3 * End );
124 }
125
130};
131
135template<typename T>
136void TransformEllipseToBeziers( const ELLIPSE<T>& aEllipse, std::vector<BEZIER<T>>& aBeziers );
137
138#endif // BEZIER_CURVES_H
void TransformEllipseToBeziers(const ELLIPSE< T > &aEllipse, std::vector< BEZIER< T > > &aBeziers)
Transforms an ellipse or elliptical arc into a set of quadratic Bezier curves that approximate it.
Bezier curves to polygon converter.
Definition: bezier_curves.h:38
void recursiveSegmentation(std::vector< VECTOR2D > &aOutput, double aMaxError)
void getQuadPoly(std::vector< VECTOR2D > &aOutput, double aMaxError)
int numberOfInflectionPoints()
double thirdControlPointDeviation()
void cubicParabolicApprox(std::vector< VECTOR2D > &aOutput, double aMaxError)
double m_minSegLen
Control points.
Definition: bezier_curves.h:81
void getCubicPoly(std::vector< VECTOR2D > &aOutput, double aMaxError)
void subdivide(double aT, BEZIER_POLY &aLeft, BEZIER_POLY &aRight)
bool isFlat(double aMaxError) const
void GetPoly(std::vector< VECTOR2I > &aOutput, int aMaxError=10)
Convert a Bezier curve to a polygon.
int findInflectionPoints(double &aT1, double &aT2)
bool isNaN() const
VECTOR2D eval(double t)
BEZIER_POLY(const std::vector< VECTOR2D > &aControlPoints)
Definition: bezier_curves.h:45
std::vector< VECTOR2D > m_ctrlPts
Definition: bezier_curves.h:84
Generic cubic Bezier representation.
Definition: bezier_curves.h:95
VECTOR2< NumericType > Start
VECTOR2< NumericType > C1
BEZIER()=default
VECTOR2< NumericType > C2
VECTOR2< NumericType > End
constexpr VECTOR2< NumericType > PointAt(double aT) const
Evaluate the Bezier curve at a given t value.
constexpr BEZIER(const VECTOR2< NumericType > &aStart, const VECTOR2< NumericType > &aC1, const VECTOR2< NumericType > &aC2, const VECTOR2< NumericType > &aEnd)
Definition: bezier_curves.h:99
This class was created to handle importing ellipses from other file formats that support them nativel...
Definition: ellipse.h:34