From 290028f07bed4719867a2f04f4571dab70790d84 Mon Sep 17 00:00:00 2001 From: vss <28676655+vss2sn@users.noreply.github.com> Date: Sat, 9 Apr 2022 11:33:22 -0700 Subject: [PATCH] Minor refactor to ensure the number of points specified is correctly used --- bezier_curve.hpp | 12 ++++++------ catmull_rom_spline.hpp | 20 ++++++++++---------- cubic_hermite_spline.hpp | 12 ++++++------ hermite_splines.hpp | 8 ++++---- main.cpp | 18 +++++++++--------- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/bezier_curve.hpp b/bezier_curve.hpp index d811f6b..0a72956 100644 --- a/bezier_curve.hpp +++ b/bezier_curve.hpp @@ -9,8 +9,8 @@ class BezierCurve { public: explicit BezierCurve(const std::array, degree +1>& weights) noexcept : weights(weights) { std::array binomial_coeffs = find_all_binomial_coefficients(); - for (int i = 0; i < n_points + 1; i++) { - BinomialParamterValues p( i * 1./n_points); + for (int i = 0; i < n_points; i++) { + BinomialParamterValues p( i * 1./(n_points - 1)); for (int k = 0; k < dimensions; k++) { points[i][k] = 0; for (int j = 0; j < degree + 1; j++) { @@ -20,8 +20,8 @@ class BezierCurve { } } -void print() { - for (int i = 0; i < n_points + 1; i++) { +void print() const { + for (int i = 0; i < n_points; i++) { for (int j = 0; j < dimensions; j++) { std::cout << points[i][j] << ", "; } @@ -31,8 +31,8 @@ void print() { private: std::array, degree + 1> weights; - std::array, n_points + 1> points; - std::array coefficients; + std::array, n_points> points; + std::array coefficients; }; #endif // BEZIER_CURVE_HPP diff --git a/catmull_rom_spline.hpp b/catmull_rom_spline.hpp index 2fd05db..ac8e874 100644 --- a/catmull_rom_spline.hpp +++ b/catmull_rom_spline.hpp @@ -4,16 +4,16 @@ #include // Note: a catmull-rum spline iss basically -// a set of hermite splines connecting sequental points, -// using tao to calculate the slopes at each ot the intermediate points +// a set of cubic hermite splines connecting sequental points, +// using tao to calculate the slopes at each of the intermediate points // n_points here is n_points per segment not total number of points template class CatmullRomSpline { public: explicit CatmullRomSpline(const std::array, N>& p, const double tao) : p(p), tao(tao) { - const double interval = (1.)/n_points; - std::array, n_points+1> coefficients; + const double interval = (1.)/(n_points - 1); + std::array, n_points> coefficients; const std::array, 4> tao_matrix { std::array{0., 1., 0., 0.}, std::array{-tao, 0., tao, 0.}, @@ -21,7 +21,7 @@ class CatmullRomSpline { std::array{-tao, 2.-tao, tao-2., tao} }; double u = 0; - for (int i = 0; i < n_points + 1; i++) { + for (int i = 0; i < n_points; i++) { const std::array u_matrix {1, u, u*u, u*u*u}; for (int j = 0; j < 4; j++) { coefficients[i][j] = 0; @@ -33,18 +33,18 @@ class CatmullRomSpline { } for (int i = 0; i <= N-4; i++) { - for (int j = 0; j < n_points + 1; j++) { + for (int j = 0; j < n_points; j++) { for (int m = 0; m < dimensions; m++) { - points[(n_points + 1) * i+j][m] = 0; + points[(n_points) * i+j][m] = 0; for (int k = 0; k < 4; k++) { - points[(n_points + 1) * i+j][m] += coefficients[j][k] * p[i+k][m]; + points[(n_points) * i+j][m] += coefficients[j][k] * p[i+k][m]; } } } } } - void print() { + void print() const { for (const auto& p : points) { for (int j = 0; j < dimensions; j++) { std::cout << p[j] << ", "; @@ -55,7 +55,7 @@ class CatmullRomSpline { private: std::array, N> p; - std::array, (N-3)*(n_points+1)> points; + std::array, (N-3)*(n_points)> points; double tao; }; diff --git a/cubic_hermite_spline.hpp b/cubic_hermite_spline.hpp index 46f7ae4..2f0a9ae 100644 --- a/cubic_hermite_spline.hpp +++ b/cubic_hermite_spline.hpp @@ -12,10 +12,10 @@ using Point = std::array; template class CubicHermiteSpline { public: - explicit CubicHermiteSpline(std::array, 4>& p) noexcept : p(p) { - const double delta_u = 1./n_points; + explicit CubicHermiteSpline(const std::array, 4>& p) noexcept : p(p) { + const double delta_u = 1./(n_points - 1); double u = 0; - for (int i = 0; i <= n_points; i++) { + for (int i = 0; i <= n_points - 1; i++) { const auto u2 = u * u; const auto u3 = u2 * u; coefficients[i][0] = 2 * u3 - 3 * u2 + 1; @@ -32,7 +32,7 @@ class CubicHermiteSpline { } } - void print() { + void print() const { for (const auto& p : points) { for (int j = 0; j < dimensions; j++) { std::cout << p[j] << ", "; @@ -41,8 +41,8 @@ class CubicHermiteSpline { } } private: - std::array, n_points + 1> coefficients; - std::array, n_points + 1> points; + std::array, n_points> coefficients; + std::array, n_points> points; std::array, 4> p; }; diff --git a/hermite_splines.hpp b/hermite_splines.hpp index 9e21a8a..e27bc92 100644 --- a/hermite_splines.hpp +++ b/hermite_splines.hpp @@ -9,10 +9,10 @@ class HermiteSplines { public: explicit HermiteSplines(const std::array, degree + 1> & p) : p(p) { calculate_coefficients(); - const double delta_u = 1./n_points; + const double delta_u = 1./(n_points - 1); double u = 0; std::array powers_of_u; - for (int i = 0; i <= n_points; i++) { + for (int i = 0; i <= (n_points - 1); i++) { powers_of_u[degree] = 1; for (int i = degree-1; i >=0; i--) { powers_of_u[i] = powers_of_u[i+1] * u; @@ -44,9 +44,9 @@ class HermiteSplines { private: std::array, degree + 1> p; - std::array, n_points + 1> coefficients; + std::array, n_points> coefficients; std::array, degree+1> coefficients_of_basis_curves; - std::array, n_points + 1> points; + std::array, n_points> points; void calculate_coefficients() { std::array coeffs_for_ploy; diff --git a/main.cpp b/main.cpp index a7d79ee..59493c0 100644 --- a/main.cpp +++ b/main.cpp @@ -36,7 +36,7 @@ // } // int main() { -// constexpr int n_points = 100; +// constexpr int n_points = 5; // constexpr int dimensions = 2; // std::array, 4> weights = { // Point{110,150},//0}, @@ -62,7 +62,7 @@ // b_returned.print(); // std::cout << '\n'; // -// return 0 +// return 0; // } // int main() { @@ -90,7 +90,7 @@ // } int main () { - constexpr size_t degree = 5; + constexpr size_t degree = 3; constexpr size_t dimensions = 2; constexpr size_t n_points = 10; constexpr std::array, degree+1> p = { @@ -98,14 +98,14 @@ int main () { Point{4., 1.}, Point{0., 1.}, Point{0., 1.}, - Point{0., 1.}, - Point{0., 1.}, + // Point{0., 1.}, + // Point{0., 1.}, }; - // auto chs = CubicHermiteSpline(p); - // std::cout << "Cubic Hermite Spline" << '\n'; - // chs.print(); - // std::cout << '\n'; + const auto chs = CubicHermiteSpline(p); + std::cout << "Cubic Hermite Spline" << '\n'; + chs.print(); + std::cout << '\n'; const auto hs = HermiteSplines(p); std::cout << "Hermite Spline" << '\n';