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 30 of file bezier_curves.cpp.

31 {
32  for( unsigned ii = 0; ii < aControlPoints.size(); ++ii )
33  m_ctrlPts.emplace_back( VECTOR2D( aControlPoints[ii] ) );
34 
35  m_minSegLen = 0.0;
36 }
std::vector< VECTOR2D > m_ctrlPts
Control points
Definition: bezier_curves.h:60
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 39 of file bezier_curves.cpp.

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

Referenced by PLOTTER::BezierCurve(), D_PAD::buildCustomPadPolygon(), 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 50 of file bezier_curves.cpp.

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

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: