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