Creating and evaluating Bezier, B-spline, and Catmull-Rom curves, plus spline fitting and interpolation.
This tutorial draws the same six control points through five different curve constructions so their behavior can be compared visually: a Bezier curve, a B-spline, a Catmull-Rom spline, an approximating spline fit, and a cubic spline interpolant. Everything is rendered as 2D polylines in pixel coordinates with LinesDrawable2D, making it a compact reference for "which curve do I want?".
A parametric curve maps a parameter t in [0,1] to a point C(t). The constructions differ in how the control points influence C(t):
A window opens with the black control polygon and the five colored curves overlaid: red Bezier, green B-spline, blue Catmull-Rom, cyan spline fit, magenta spline interpolation. Press '[' / ']' to decrease/increase the line width and '0' to reset it to 3 pixels.
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/curve.h>
29#include <easy3d/core/spline_curve_fitting.h>
30#include <easy3d/core/spline_curve_interpolation.h>
31#include <easy3d/renderer/drawable_lines_2D.h>
32#include <easy3d/util/initializer.h>
92void create_drawable(
Viewer &viewer,
const std::vector<vec2> &curve_points,
const vec3 &color) {
94 std::vector<unsigned int> indices;
95 for (
unsigned int i = 0; i + 1 < curve_points.size(); ++i) {
97 indices.push_back(i + 1);
100 auto drawable = std::make_shared<LinesDrawable2D>();
101 viewer.add_drawable(drawable);
104 int width = 0, height = 0;
105 viewer.viewer_size(width, height);
106 drawable->update_vertex_buffer(curve_points, width, height);
107 drawable->update_element_buffer(indices);
108 drawable->set_uniform_coloring(
vec4(color, 1.0f));
109 drawable->set_line_width(3.0f);
114void add_Bezier(
Viewer &viewer,
const std::vector<vec2> &control_points,
int bezier_steps,
const vec3 &color) {
116 const auto curve_points = curve.
generate(control_points, bezier_steps);
117 create_drawable(viewer, curve_points, color);
122void add_BSpline(
Viewer &viewer,
const std::vector<vec2> &control_points,
int bezier_steps,
const vec3 &color) {
124 const auto curve_points = curve.
generate(control_points, bezier_steps);
125 create_drawable(viewer, curve_points, color);
130void add_CatmullRom(
Viewer &viewer,
const std::vector<vec2> &control_points,
int bezier_steps,
const vec3 &color) {
132 const auto curve_points = curve.
generate(control_points, bezier_steps);
133 create_drawable(viewer, curve_points, color);
136void create_curves_drawables(
Viewer &viewer) {
137 viewer.set_usage(
"default",
138 "Press '[' / ']' to decrease/increase line width\n" "Press '0' to reset line width to 3 pixels");
140 auto adjust_width = [](
Viewer *host,
float delta,
bool reset) {
141 for (
const auto &drawable : host->drawables()) {
144 lines->set_line_width(reset ? 3.0f : std::
max(1.0f, std::
min(20.0f, lines->line_width() + delta)));
149 viewer.bind_shortcut([adjust_width](
Viewer *host) {
return adjust_width(host, -1,
false); }, Viewer::KEY_LEFT_BRACKET);
150 viewer.bind_shortcut([adjust_width](
Viewer *host) {
return adjust_width(host, 1,
false); }, Viewer::KEY_RIGHT_BRACKET);
151 viewer.bind_shortcut([adjust_width](
Viewer *host) {
return adjust_width(host, 0,
true); }, Viewer::KEY_0);
154 std::vector<vec2> points = {
162 create_drawable(viewer, points,
vec3(0, 0, 0));
164 const int resolution = 100;
167 add_Bezier(viewer, points, resolution,
vec3(1, 0, 0));
168 add_BSpline(viewer, points, resolution,
vec3(0, 1, 0));
169 add_CatmullRom(viewer, points, resolution,
vec3(0, 0, 1));
177 Fitter fitter(order, Fitter::eOPEN_UNIFORM);
178 fitter.set_ctrl_points(points);
180 std::vector<vec2> curve_points;
181 for (
int i = 0; i < resolution; ++i) {
182 const vec2 p = fitter.eval_f(
static_cast<float>(i) /
static_cast<float>(resolution - 1));
183 curve_points.push_back(p);
185 create_drawable(viewer, curve_points,
vec3(0, 1, 1));
193 Interpolator interpolator;
194 interpolator.
set_boundary(Interpolator::second_deriv, 0, Interpolator::second_deriv, 0);
195 interpolator.set_points(points);
196 std::vector<vec2> curve_points;
197 for (
int i = 0; i < resolution; ++i) {
198 const vec2 p = interpolator.eval_f(
static_cast<float>(i) /
static_cast<float>(resolution - 1));
199 curve_points.push_back(p);
201 create_drawable(viewer, curve_points,
vec3(1, 0, 1));
205int main(
int argc,
char **argv) {
208 Viewer viewer(EXAMPLE_TITLE);
211 create_curves_drawables(viewer);
Class for BSpline curve fitting.
Definition curve.h:295
std::vector< Point_t > generate(const std::vector< Point_t > &control_points, size_t num_samples) const override
Generates a curve based on the given control points.
Definition curve.h:309
Class for Bezier curve fitting.
Definition curve.h:239
std::vector< Point_t > generate(const std::vector< Point_t > &control_points, size_t num_samples) const override
Generates a curve based on the given control points.
Definition curve.h:253
Class for uniform Catmull-Rom curve interpolation (N-dimensional).
Definition curve.h:388
std::vector< Point_t > generate(const std::vector< Point_t > &control_points, size_t num_samples) const override
Generates a uniform Catmull-Rom curve through the given keyframes.
Definition curve.h:409
The drawable for rendering a set of line segments in the screen space.
Definition drawable_lines_2D.h:44
Spline curve fitting for arbitrary dimensions.
Definition spline_curve_fitting.h:65
Cubic spline curve interpolation for arbitrary dimensions.
Definition spline_curve_interpolation.h:64
void set_boundary(BoundaryType left, T left_value, BoundaryType right, T right_value)
Set endpoint derivative boundary conditions for each coordinate.
Definition spline_curve_interpolation.h:142
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39
FT min()
Function returning minimum representable value for a given type.
FT max()
Function returning maximum representable value for a given type.