Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_604_Curves

Creating and evaluating Bezier, B-spline, and Catmull-Rom curves, plus spline fitting and interpolation.

Overview

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?".

The technique

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):

Easy3D API

Running it

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.

1
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>
33#include <cmath>
34#include <algorithm>
35
89using namespace easy3d;
90
91// Turn a sampled polyline (a list of 2D points) into a LinesDrawable2D and add it to the viewer.
92void create_drawable(Viewer &viewer, const std::vector<vec2> &curve_points, const vec3 &color) {
93 // Build line-segment indices: each consecutive pair of points (i, i+1) forms one segment.
94 std::vector<unsigned int> indices;
95 for (unsigned int i = 0; i + 1 < curve_points.size(); ++i) {
96 indices.push_back(i);
97 indices.push_back(i + 1);
98 }
99
100 auto drawable = std::make_shared<LinesDrawable2D>();
101 viewer.add_drawable(drawable);
102 // LinesDrawable2D works in pixel coordinates, so it needs the viewport size to map the
103 // 2D points onto the screen.
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);
110}
111
112// Sample a Bezier curve through the control points and draw it. A Bezier curve passes through the
113// first and last control point and is pulled toward (but generally misses) the interior ones.
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);
118}
119
120// Sample a B-spline and draw it. A B-spline has local control and is smoother; with an open-uniform
121// knot vector it also starts/ends at the first/last control point.
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);
126}
127
128// Sample a Catmull-Rom spline and draw it. Unlike the previous two, it interpolates (passes through)
129// every control point.
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);
134}
135
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");
139 // A shared helper that nudges (or resets) the width of every 2D line drawable, clamped to [1, 20].
140 auto adjust_width = [](Viewer *host, float delta, bool reset) {
141 for (const auto &drawable : host->drawables()) {
142 auto *lines = dynamic_cast<LinesDrawable2D *>(drawable.get());
143 if (lines)
144 lines->set_line_width(reset ? 3.0f : std::max(1.0f, std::min(20.0f, lines->line_width() + delta)));
145 }
146 return true;
147 };
148 // Bind the helper to keys (these are viewer-wide drawables, not tied to a model).
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);
152 // the control points
153 // Six points in pixel coordinates forming a zig-zag; every curve below is built from these.
154 std::vector<vec2> points = {
155 vec2(100, 100), // 0
156 vec2(100, 500), // 1
157 vec2(400, 500), // 2
158 vec2(400, 100), // 3
159 vec2(700, 100), // 4
160 vec2(700, 500), // 5
161 };
162 create_drawable(viewer, points, vec3(0, 0, 0)); // this is the control polygon
163
164 const int resolution = 100; // Number of line subdivisions to display the spline
165
166 // The three classic control-point curves, each in a distinct color for comparison.
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));
170
171 {
172 // spline curve fitting
173 // An approximating B-spline: order 3 (quadratic) with an open-uniform knot vector, so the
174 // curve starts/ends at the first/last control point but only approximates the interior ones.
175 const int order = 3; // Smoothness of the spline (min 2)
177 Fitter fitter(order, Fitter::eOPEN_UNIFORM);
178 fitter.set_ctrl_points(points);
179 // Sample the curve at 'resolution' evenly spaced parameter values in [0, 1].
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);
184 }
185 create_drawable(viewer, curve_points, vec3(0, 1, 1));
186 }
187
188 {
189 // spline curve interpolation
190 // A natural cubic spline: it is forced through every data point, with zero second derivative
191 // (zero curvature) at both ends. set_boundary() must come before set_points().
192 typedef SplineCurveInterpolation<Vec, 2, float> Interpolator;
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);
200 }
201 create_drawable(viewer, curve_points, vec3(1, 0, 1));
202 }
203}
204
205int main(int argc, char **argv) {
206 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
207 initialize();
208 Viewer viewer(EXAMPLE_TITLE);
209
210 // Build all the curve drawables (and their key bindings), then enter the interactive loop.
211 create_curves_drawables(viewer);
212 return viewer.run();
213}
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.
See also
Bezier, BSpline, CatmullRom, SplineCurveFitting, SplineCurveInterpolation, LinesDrawable2D, Viewer