2014-10-19 20:20:16 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2019-11-09 10:14:20 +00:00
|
|
|
* Copyright (C) 2014-2019 KiCad Developers, see AUTHORS.txt for contributors.
|
2014-10-19 20:20:16 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2009-06-25 20:45:27 +00:00
|
|
|
/************************************/
|
|
|
|
/* routines to handle bezier curves */
|
|
|
|
/************************************/
|
|
|
|
|
2012-01-23 04:33:36 +00:00
|
|
|
#include <bezier_curves.h>
|
2009-06-25 20:45:27 +00:00
|
|
|
|
2019-11-09 10:14:20 +00:00
|
|
|
BEZIER_POLY::BEZIER_POLY( const std::vector<wxPoint>& aControlPoints )
|
2009-06-25 20:45:27 +00:00
|
|
|
{
|
2019-11-09 10:14:20 +00:00
|
|
|
for( unsigned ii = 0; ii < aControlPoints.size(); ++ii )
|
|
|
|
m_ctrlPts.emplace_back( VECTOR2D( aControlPoints[ii] ) );
|
2009-06-25 20:45:27 +00:00
|
|
|
|
2019-11-09 10:14:20 +00:00
|
|
|
m_minSegLen = 0.0;
|
2009-06-25 20:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-07 11:04:01 +00:00
|
|
|
void BEZIER_POLY::GetPoly( std::vector<wxPoint>& aOutput, int aMinSegLen )
|
2009-06-25 20:45:27 +00:00
|
|
|
{
|
2019-11-09 10:14:20 +00:00
|
|
|
aOutput.clear();
|
|
|
|
std::vector<VECTOR2D> buffer;
|
|
|
|
GetPoly( buffer, double( aMinSegLen ) );
|
2017-05-16 15:38:19 +00:00
|
|
|
|
2019-11-09 10:14:20 +00:00
|
|
|
for( unsigned ii = 0; ii < buffer.size(); ++ii )
|
|
|
|
aOutput.emplace_back( wxPoint( int( buffer[ii].x ), int( buffer[ii].y ) ) );
|
2009-06-25 20:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-09 10:14:20 +00:00
|
|
|
void BEZIER_POLY::GetPoly( std::vector<VECTOR2D>& aOutput, double aMinSegLen )
|
2009-06-25 20:45:27 +00:00
|
|
|
{
|
2019-11-09 10:14:20 +00:00
|
|
|
wxASSERT( m_ctrlPts.size() == 4 );
|
|
|
|
// FIXME Brute force method, use a better (recursive?) algorithm
|
|
|
|
// with a max error value.
|
|
|
|
// to optimize the number of segments
|
|
|
|
#define CURVE_POINTS 32
|
|
|
|
double dt = 1.0 / CURVE_POINTS;
|
2009-06-25 20:45:27 +00:00
|
|
|
|
2019-11-09 10:14:20 +00:00
|
|
|
aOutput.clear();
|
|
|
|
aOutput.push_back( m_ctrlPts[0] );
|
2009-06-25 20:45:27 +00:00
|
|
|
|
2019-11-09 10:37:49 +00:00
|
|
|
// If the Bezier curve is degenerated (straight line), skip intermediate points:
|
2019-11-09 10:14:20 +00:00
|
|
|
bool degenerated = m_ctrlPts[0] == m_ctrlPts[1] && m_ctrlPts[2] == m_ctrlPts[3];
|
2009-06-25 20:45:27 +00:00
|
|
|
|
2019-11-09 10:14:20 +00:00
|
|
|
if( !degenerated )
|
2009-06-25 20:45:27 +00:00
|
|
|
{
|
2019-11-09 10:14:20 +00:00
|
|
|
for( int ii = 1; ii < CURVE_POINTS; ii++ )
|
2009-06-25 20:45:27 +00:00
|
|
|
{
|
2019-11-09 10:14:20 +00:00
|
|
|
double t = dt * ii;
|
|
|
|
double omt = 1.0 - t;
|
|
|
|
double omt2 = omt * omt;
|
|
|
|
double omt3 = omt * omt2;
|
|
|
|
double t2 = t * t;
|
|
|
|
double t3 = t * t2;
|
|
|
|
|
|
|
|
VECTOR2D vertex = omt3 * m_ctrlPts[0]
|
|
|
|
+ 3.0 * t * omt2 * m_ctrlPts[1]
|
|
|
|
+ 3.0 * t2 * omt * m_ctrlPts[2]
|
|
|
|
+ t3 * m_ctrlPts[3];
|
|
|
|
|
|
|
|
// a minimal filter on the lenght of the segment being created:
|
|
|
|
// The offset from last point:
|
|
|
|
VECTOR2D delta = vertex - aOutput.back();
|
|
|
|
double dist = delta.EuclideanNorm();
|
|
|
|
|
|
|
|
if( dist > aMinSegLen )
|
|
|
|
aOutput.push_back( vertex );
|
2009-06-25 20:45:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 10:14:20 +00:00
|
|
|
if( aOutput.back() != m_ctrlPts[3] )
|
|
|
|
aOutput.push_back( m_ctrlPts[3] );
|
2009-06-25 20:45:27 +00:00
|
|
|
}
|