Easy3D 2.6.1
Loading...
Searching...
No Matches
easy3d::curve Namespace Reference

Algorithms for evaluating curves. More...

Functions

template<template< size_t, class > class Point, size_t N, typename T>
void quadratic (const Point< N, T > &A, const Point< N, T > &B, const Point< N, T > &C, std::vector< Point< N, T > > &curve, unsigned int bezier_steps=4, bool include_end=false)
 Computes a quadratic Bézier curve using De Casteljau’s algorithm.
 
template<template< size_t, class > class Point, size_t N, typename T>
void cubic (const Point< N, T > &A, const Point< N, T > &B, const Point< N, T > &C, const Point< N, T > &D, std::vector< Point< N, T > > &curve, unsigned int bezier_steps=4, bool include_end=false)
 Evaluates a cubic Bézier curve using De Casteljau’s algorithm.
 

Detailed Description

Algorithms for evaluating curves.

Function Documentation

◆ cubic()

template<template< size_t, class > class Point, size_t N, typename T>
void cubic ( const Point< N, T > & A,
const Point< N, T > & B,
const Point< N, T > & C,
const Point< N, T > & D,
std::vector< Point< N, T > > & curve,
unsigned int bezier_steps = 4,
bool include_end = false )

Evaluates a cubic Bézier curve using De Casteljau’s algorithm.

Given four control points A, B, C, and D, this function computes a sequence of points that approximate the cubic Bézier curve defined by these control points. The method works in both 2D and 3D spaces, depending on the template instantiation of Point.

Template Parameters
PointA templated point class that supports basic arithmetic operations (addition and scalar multiplication). It must be parameterized as Point<N, T>, where N is the number of dimensions, and T is the data type.
NThe number of dimensions (e.g., 2 for 2D, 3 for 3D).
TThe scalar type (e.g., float or double).
Parameters
[in]AThe first control point (curve start).
[in]BThe second control point (controls curvature).
[in]CThe third control point (controls curvature).
[in]DThe fourth control point (curve end).
[out]curveOutput vector that will store the computed points along the Bézier curve.
[in]bezier_stepsThe number of subdivisions used to approximate the curve. A higher value results in a smoother curve but increases computation time. The default value is 4.
[in]include_endIf true, the function ensures that the last point in the curve is exactly D.

The following code demonstrates how to visualize a cubic Bézier curve as a polyline:

{
vec3 a(0, 0, 0);
vec3 b(400, 0, 0);
vec3 c(400, 800, 0);
vec3 d(800, 800, 0);
unsigned int steps = 20;
std::vector<vec3> points;
curve::cubic(a, b, c, d, points, steps);
std::cout << "First point: " << points.front() << ", Last point: " << points.back() << std::endl;
std::vector<unsigned int> indices;
for (unsigned int i = 0; i < points.size() - 1; ++i) {
indices.push_back(i);
indices.push_back(i + 1);
}
curve->update_vertex_buffer(points);
curve->update_element_buffer(indices);
curve->set_impostor_type(LinesDrawable::CYLINDER);
curve->set_line_width(5);
curve->set_uniform_coloring(vec3(0, 1, 0, 1));
viewer.add_drawable(curve);
}
The drawable for rendering a set of line segments, e.g., edges of a mesh, vector fields.
Definition drawable_lines.h:40
@ CYLINDER
The lines will be drawn as cylinders.
Definition drawable_lines.h:60
Algorithms for evaluating curves.
void cubic(const Point< N, T > &A, const Point< N, T > &B, const Point< N, T > &C, const Point< N, T > &D, std::vector< Point< N, T > > &curve, unsigned int bezier_steps=4, bool include_end=false)
Evaluates a cubic Bézier curve using De Casteljau’s algorithm.
Definition curve.h:172
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44

◆ quadratic()

template<template< size_t, class > class Point, size_t N, typename T>
void quadratic ( const Point< N, T > & A,
const Point< N, T > & B,
const Point< N, T > & C,
std::vector< Point< N, T > > & curve,
unsigned int bezier_steps = 4,
bool include_end = false )

Computes a quadratic Bézier curve using De Casteljau’s algorithm.

This function evaluates a second-degree Bézier curve (also known as a conic curve) given three control points: A, B, and C. The computed curve can be used for smooth interpolation between points in both 2D and 3D spaces, depending on the dimensionality of the Point type.

Template Parameters
PointA templated point class that supports basic arithmetic operations (addition and scalar multiplication). It must be parameterized as Point<N, T>, where N is the number of dimensions, and T is the data type.
NThe number of dimensions (e.g., 2 for 2D, 3 for 3D).
TThe scalar type (e.g., float or double).
Parameters
[in]AThe first control point (start of the curve).
[in]BThe second control point (influences the curvature).
[in]CThe third control point (end of the curve).
[out]curveA vector storing the computed points along the curve.
[in]bezier_stepsThe number of segments used to approximate the curve. A higher value results in a smoother curve at the cost of more points. Suggested default is 4.
[in]include_endIf true, the endpoint C is included in the output (i.e., extend the curve to the end point).

The Bézier curve is computed using De Casteljau’s algorithm, which recursively interpolates between the control points. The interpolation formula is:

\[ P(t) = (1 - t)^2 A + 2(1 - t)t B + t^2 C, \quad t \in [0,1] \]

The function iterates from \( t = 0 \) to \( t = 1 \) in steps of \( 1/\text{bezier_steps} \), generating points along the curve.

The following example computes and visualizes a quadratic Bézier curve:

{
vec3 A(0, 0, 0);
vec3 B(800, 0, 0);
vec3 C(800, 800, 0);
unsigned int steps = 20;
std::vector<vec3> curvePoints;
curve::quadratic(A, B, C, curvePoints, steps);
std::cout << "First point: " << curvePoints.front()
<< ", Last point: " << curvePoints.back() << std::endl;
std::vector<unsigned int> indices;
for (unsigned int i = 0; i < curvePoints.size() - 1; ++i) {
indices.push_back(i);
indices.push_back(i + 1);
}
LinesDrawable *curveDrawable = new LinesDrawable;
curveDrawable->update_vertex_buffer(curvePoints);
curveDrawable->update_element_buffer(indices);
curveDrawable->set_line_width(5);
curveDrawable->set_uniform_coloring(vec3(0, 0, 1, 1));
viewer.add_drawable(curveDrawable);
}
void update_vertex_buffer(const std::vector< vec3 > &vertices, bool dynamic=false)
Creates/Updates the vertex buffer.
Definition drawable.cpp:139
void update_element_buffer(const std::vector< unsigned int > &elements)
Updates the element buffer.
Definition drawable.cpp:190
void set_impostor_type(ImposterType t)
Sets the type of the line imposter.
Definition drawable_lines.h:72
void set_line_width(float w)
Sets the width of the lines.
Definition drawable_lines.h:83
void set_uniform_coloring(const vec4 &color)
Constructs a uniform coloring scheme.
Definition state.cpp:92
void quadratic(const Point< N, T > &A, const Point< N, T > &B, const Point< N, T > &C, std::vector< Point< N, T > > &curve, unsigned int bezier_steps=4, bool include_end=false)
Computes a quadratic Bézier curve using De Casteljau’s algorithm.
Definition curve.h:105