KiCad PCB EDA Suite
BEZIER_POLY Class Reference

Bezier curves to polygon converter. More...

#include <bezier_curves.h>

Public Member Functions

 BEZIER_POLY (const VECTOR2I &aStart, const VECTOR2I &aCtrl1, const VECTOR2I &aCtrl2, const VECTOR2I &aEnd)
 
 BEZIER_POLY (const std::vector< VECTOR2I > &aControlPoints)
 
 BEZIER_POLY (const std::vector< VECTOR2D > &aControlPoints)
 
void GetPoly (std::vector< VECTOR2I > &aOutput, int aMinSegLen=0, int aMaxSegCount=32)
 Convert a Bezier curve to a polygon. More...
 
void GetPoly (std::vector< VECTOR2D > &aOutput, double aMinSegLen=0.0, int aMaxSegCount=32)
 

Private Attributes

double m_minSegLen
 Control points. More...
 
std::vector< VECTOR2Dm_ctrlPts
 

Detailed Description

Bezier curves to polygon converter.

Only quadratic and cubic Bezier curves are handled

Definition at line 35 of file bezier_curves.h.

Constructor & Destructor Documentation

◆ BEZIER_POLY() [1/3]

BEZIER_POLY::BEZIER_POLY ( const VECTOR2I aStart,
const VECTOR2I aCtrl1,
const VECTOR2I aCtrl2,
const VECTOR2I aEnd 
)

Definition at line 33 of file bezier_curves.cpp.

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}
double m_minSegLen
Control points.
Definition: bezier_curves.h:62
std::vector< VECTOR2D > m_ctrlPts
Definition: bezier_curves.h:65
VECTOR2< double > VECTOR2D
Definition: vector2d.h:617

References m_ctrlPts, and m_minSegLen.

◆ BEZIER_POLY() [2/3]

BEZIER_POLY::BEZIER_POLY ( const std::vector< VECTOR2I > &  aControlPoints)

Definition at line 45 of file bezier_curves.cpp.

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}

References m_ctrlPts, and m_minSegLen.

◆ BEZIER_POLY() [3/3]

BEZIER_POLY::BEZIER_POLY ( const std::vector< VECTOR2D > &  aControlPoints)
inline

Definition at line 43 of file bezier_curves.h.

44 : m_ctrlPts( aControlPoints )
45 {
46 m_minSegLen = 0.0;
47 }

References m_minSegLen.

Member Function Documentation

◆ GetPoly() [1/2]

void BEZIER_POLY::GetPoly ( std::vector< VECTOR2D > &  aOutput,
double  aMinSegLen = 0.0,
int  aMaxSegCount = 32 
)

Definition at line 65 of file bezier_curves.cpp.

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}
constexpr int delta

References delta, and m_ctrlPts.

◆ GetPoly() [2/2]

void BEZIER_POLY::GetPoly ( std::vector< VECTOR2I > &  aOutput,
int  aMinSegLen = 0,
int  aMaxSegCount = 32 
)

Convert a Bezier curve to a polygon.

Parameters
aOutputwill be used as an output vector storing polygon points.
aMinSegLenis the min dist between 2 successive points. It can be used to reduce the number of points. (the last point is always generated) aMaxSegCount is the max number of segments created

Definition at line 54 of file bezier_curves.cpp.

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}
void GetPoly(std::vector< VECTOR2I > &aOutput, int aMinSegLen=0, int aMaxSegCount=32)
Convert a Bezier curve to a polygon.

References GetPoly().

Referenced by KIFONT::OUTLINE_DECOMPOSER::approximateCubicBezierCurve(), PLOTTER::BezierCurve(), EDA_SHAPE::buildBezierToSegmentsPointsList(), TEARDROP_MANAGER::computeCurvedForRectShape(), TEARDROP_MANAGER::computeCurvedForRoundShape(), KIGFX::PCB_PAINTER::draw(), KIGFX::OPENGL_GAL::DrawCurve(), EDA_SHAPE::flip(), GetPoly(), SCH_ALTIUM_PLUGIN::ParseBezier(), and EDA_SHAPE::TransformShapeToPolygon().

Member Data Documentation

◆ m_ctrlPts

std::vector<VECTOR2D> BEZIER_POLY::m_ctrlPts
private

Definition at line 65 of file bezier_curves.h.

Referenced by BEZIER_POLY(), and GetPoly().

◆ m_minSegLen

double BEZIER_POLY::m_minSegLen
private

Control points.

Definition at line 62 of file bezier_curves.h.

Referenced by BEZIER_POLY().


The documentation for this class was generated from the following files: