Easy3D 2.6.1
Loading...
Searching...
No Matches
Tutorial_603_Curves/main.cpp

This example shows how to create/evaluate several types of curves and curve fitting/interpolation methods in Easy3D.

1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
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
39
40using namespace easy3d;
41
42void create_drawable(Viewer& viewer, const std::vector<vec2>& curve_points, const vec3& color) {
43 std::vector<unsigned int> indices;
44 for (unsigned int i = 0; i < curve_points.size() - 1; ++i) {
45 indices.push_back(i);
46 indices.push_back(i + 1);
47 }
48
49 LinesDrawable2D *drawable = new LinesDrawable2D;
50 drawable->update_vertex_buffer(curve_points, viewer.width(), viewer.height());
51 drawable->update_element_buffer(indices);
52 drawable->set_uniform_coloring(vec4(color, 1.0f));
53 viewer.add_drawable(drawable);
54}
55
56void add_Bezier(Viewer& viewer, const std::vector<vec2>& control_points, int bezier_steps, const vec3& color) {
58 const auto curve_points = curve.generate(control_points, bezier_steps);
59 create_drawable(viewer, curve_points, color);
60}
61
62void add_BSpline(Viewer& viewer, const std::vector<vec2>& control_points, int bezier_steps, const vec3& color) {
64 const auto curve_points = curve.generate(control_points, bezier_steps);
65 create_drawable(viewer, curve_points, color);
66}
67
68void add_CatmullRom(Viewer& viewer, const std::vector<vec2>& control_points, int bezier_steps, const vec3& color) {
70 const auto curve_points = curve.generate(control_points, bezier_steps);
71 create_drawable(viewer, curve_points, color);
72}
73
74
75int main(int argc, char** argv) {
76 // initialize Easy3D.
77 initialize();
78
79 // create the default Easy3D viewer.
80 // (a viewer must be created before creating any drawables).
81 Viewer viewer(EXAMPLE_TITLE);
82
83 // the control points
84 std::vector<vec2> points = {
85 vec2(100, 100), // 0
86 vec2(100, 500), // 1
87 vec2(400, 500), // 2
88 vec2(400, 100), // 3
89 vec2(700, 100), // 4
90 vec2(700, 500), // 5
91 };
92 create_drawable(viewer, points, vec3(0, 0, 0)); // this is the control polygon
93
94 const int resolution = 100; // Number of line subdivisions to display the spline
95
96 add_Bezier(viewer, points, resolution, vec3(1, 0, 0));
97 add_BSpline(viewer, points, resolution, vec3(0, 1, 0));
98 add_CatmullRom(viewer, points, resolution, vec3(0, 0, 1));
99
100 {
101 // spline curve fitting
102 const int order = 3; // Smoothness of the spline (min 2)
104 Fitter fitter(order, Fitter::eOPEN_UNIFORM);
105 fitter.set_ctrl_points(points);
106 std::vector<vec2> curve_points;
107 for(int i = 0; i < resolution; ++i) {
108 const vec2 p = fitter.eval_f( static_cast<float>(i) / static_cast<float>(resolution - 1) );
109 curve_points.push_back(p);
110 }
111 create_drawable(viewer, curve_points, vec3(0, 1, 1));
112 }
113
114 {
115 // spline curve interpolation
116 typedef SplineCurveInterpolation<Vec, 2, float> Interpolator;
117 Interpolator interpolator;
118 interpolator.set_boundary(Interpolator::second_deriv, 0, Interpolator::second_deriv, 0);
119 interpolator.set_points(points);
120 std::vector<vec2> curve_points;
121 for (int i = 0; i < resolution; ++i) {
122 const vec2 p = interpolator.eval_f(static_cast<float>(i) / static_cast<float>(resolution - 1));
123 curve_points.push_back(p);
124 }
125 create_drawable(viewer, curve_points, vec3(1, 0, 1));
126 }
127
128 // run the viewer
129 return viewer.run();
130}
Class for BSpline curve fitting.
Definition curve.h:293
Class for Bezier curve fitting.
Definition curve.h:238
Class for CatmullRom curve interpolation.
Definition curve.h:368
The drawable for rendering a set of line segments in the screen space.
Definition drawable_lines_2D.h:43
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)
Definition spline_curve_interpolation.h:137
The built-in Easy3D viewer.
Definition viewer.h:63
Algorithms for evaluating curves.
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
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
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46
Vec< 2, float > vec2
A 2D point/vector of float type.
Definition types.h:42