KiCad PCB EDA Suite
BEZIER_POLY Class Reference

Bezier curves to polygon converter. More...

#include <bezier_curves.h>

Public Member Functions

 BEZIER_POLY (const std::vector< wxPoint > &aControlPoints)
 
 BEZIER_POLY (const std::vector< VECTOR2D > &aControlPoints)
 
void GetPoly (std::vector< wxPoint > &aOutput, int aMinSegLen=0)
 Converts Bezier curve to a polygon. More...
 
void GetPoly (std::vector< VECTOR2D > &aOutput, double aMinSegLen=0.0)
 

Private Attributes

double m_minSegLen
 
std::vector< VECTOR2Dm_ctrlPts
 

Control points

More...
 

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/2]

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

Definition at line 34 of file bezier_curves.cpp.

35 {
36  for( unsigned ii = 0; ii < aControlPoints.size(); ++ii )
37  m_ctrlPts.emplace_back( VECTOR2D( aControlPoints[ii] ) );
38 
39  m_minSegLen = 0.0;
40 }
std::vector< VECTOR2D > m_ctrlPts
Control points
Definition: bezier_curves.h:60
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
double m_minSegLen
Definition: bezier_curves.h:57

References m_ctrlPts, and m_minSegLen.

◆ BEZIER_POLY() [2/2]

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

Definition at line 40 of file bezier_curves.h.

41  : m_ctrlPts( aControlPoints )
42  {
43  m_minSegLen = 0.0;
44  }
std::vector< VECTOR2D > m_ctrlPts
Control points
Definition: bezier_curves.h:60
double m_minSegLen
Definition: bezier_curves.h:57

References m_minSegLen.

Member Function Documentation

◆ GetPoly() [1/2]

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

Converts Bezier curve to a polygon.

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

Definition at line 43 of file bezier_curves.cpp.

44 {
45  aOutput.clear();
46  std::vector<VECTOR2D> buffer;
47  GetPoly( buffer, double( aMinSegLen ) );
48 
49  for( unsigned ii = 0; ii < buffer.size(); ++ii )
50  aOutput.emplace_back( wxPoint( int( buffer[ii].x ), int( buffer[ii].y ) ) );
51 }
void GetPoly(std::vector< wxPoint > &aOutput, int aMinSegLen=0)
Converts Bezier curve to a polygon.

Referenced by PLOTTER::BezierCurve(), KIGFX::SCH_PAINTER::draw(), KIGFX::OPENGL_GAL::DrawCurve(), DRAWSEGMENT::Flip(), GRBezier(), LIB_BEZIER::print(), DRAWSEGMENT::RebuildBezierToSegmentsPointsList(), and DRAWSEGMENT::TransformShapeWithClearanceToPolygon().

◆ GetPoly() [2/2]

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

Definition at line 54 of file bezier_curves.cpp.

55 {
56  wxASSERT( m_ctrlPts.size() == 4 );
57  // FIXME Brute force method, use a better (recursive?) algorithm
58  // with a max error value.
59  // to optimize the number of segments
60  #define CURVE_POINTS 32
61  double dt = 1.0 / CURVE_POINTS;
62 
63  aOutput.clear();
64  aOutput.push_back( m_ctrlPts[0] );
65 
66  // If the Bezier curve is degenerated (straight line), skip intermediate points:
67  bool degenerated = m_ctrlPts[0] == m_ctrlPts[1] && m_ctrlPts[2] == m_ctrlPts[3];
68 
69  if( !degenerated )
70  {
71  for( int ii = 1; ii < CURVE_POINTS; ii++ )
72  {
73  double t = dt * ii;
74  double omt = 1.0 - t;
75  double omt2 = omt * omt;
76  double omt3 = omt * omt2;
77  double t2 = t * t;
78  double t3 = t * t2;
79 
80  VECTOR2D vertex = omt3 * m_ctrlPts[0]
81  + 3.0 * t * omt2 * m_ctrlPts[1]
82  + 3.0 * t2 * omt * m_ctrlPts[2]
83  + t3 * m_ctrlPts[3];
84 
85  // a minimal filter on the lenght of the segment being created:
86  // The offset from last point:
87  VECTOR2D delta = vertex - aOutput.back();
88  double dist = delta.EuclideanNorm();
89 
90  if( dist > aMinSegLen )
91  aOutput.push_back( vertex );
92  }
93  }
94 
95  if( aOutput.back() != m_ctrlPts[3] )
96  aOutput.push_back( m_ctrlPts[3] );
97 }
std::vector< VECTOR2D > m_ctrlPts
Control points
Definition: bezier_curves.h:60
VECTOR2 defines a general 2D-vector/point.
Definition: vector2d.h:61
double dist(const double ax, const double ay, const double bx, const double by)
Definition: delauney.h:168
#define CURVE_POINTS
T EuclideanNorm() const
Destructor.
Definition: vector2d.h:299

References CURVE_POINTS, dist(), VECTOR2< T >::EuclideanNorm(), and m_ctrlPts.

Member Data Documentation

◆ m_ctrlPts

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

Control points

Definition at line 60 of file bezier_curves.h.

Referenced by BEZIER_POLY(), and GetPoly().

◆ m_minSegLen

double BEZIER_POLY::m_minSegLen
private

Definition at line 57 of file bezier_curves.h.

Referenced by BEZIER_POLY().


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