Skip to content

Commit

Permalink
Minor refactor to ensure the number of points specified is correctly …
Browse files Browse the repository at this point in the history
…used
  • Loading branch information
vss2sn committed Apr 9, 2022
1 parent e1afab8 commit 290028f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
12 changes: 6 additions & 6 deletions bezier_curve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class BezierCurve {
public:
explicit BezierCurve(const std::array<Point<dimensions>, degree +1>& weights) noexcept : weights(weights) {
std::array<int, degree + 1> binomial_coeffs = find_all_binomial_coefficients<degree>();
for (int i = 0; i < n_points + 1; i++) {
BinomialParamterValues<degree> p( i * 1./n_points);
for (int i = 0; i < n_points; i++) {
BinomialParamterValues<degree> 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++) {
Expand All @@ -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] << ", ";
}
Expand All @@ -31,8 +31,8 @@ void print() {

private:
std::array<Point<dimensions>, degree + 1> weights;
std::array<Point<dimensions>, n_points + 1> points;
std::array<double, n_points + 1> coefficients;
std::array<Point<dimensions>, n_points> points;
std::array<double, n_points> coefficients;
};

#endif // BEZIER_CURVE_HPP
20 changes: 10 additions & 10 deletions catmull_rom_spline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
#include <array>

// 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<size_t N, size_t n_points, size_t dimensions>
class CatmullRomSpline {
public:
explicit CatmullRomSpline(const std::array<Point<dimensions>, N>& p, const double tao) : p(p), tao(tao) {
const double interval = (1.)/n_points;
std::array<std::array<double, 4>, n_points+1> coefficients;
const double interval = (1.)/(n_points - 1);
std::array<std::array<double, 4>, n_points> coefficients;
const std::array<std::array<double,4>, 4> tao_matrix {
std::array<double,4>{0., 1., 0., 0.},
std::array<double,4>{-tao, 0., tao, 0.},
std::array<double,4>{2.*tao, tao-3., 3.-2.*tao, -tao},
std::array<double,4>{-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<double, 4> u_matrix {1, u, u*u, u*u*u};
for (int j = 0; j < 4; j++) {
coefficients[i][j] = 0;
Expand All @@ -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] << ", ";
Expand All @@ -55,7 +55,7 @@ class CatmullRomSpline {

private:
std::array<Point<dimensions>, N> p;
std::array<Point<dimensions>, (N-3)*(n_points+1)> points;
std::array<Point<dimensions>, (N-3)*(n_points)> points;
double tao;
};

Expand Down
12 changes: 6 additions & 6 deletions cubic_hermite_spline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ using Point = std::array<double, N>;
template<size_t n_points, size_t dimensions>
class CubicHermiteSpline {
public:
explicit CubicHermiteSpline(std::array<Point<dimensions>, 4>& p) noexcept : p(p) {
const double delta_u = 1./n_points;
explicit CubicHermiteSpline(const std::array<Point<dimensions>, 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;
Expand All @@ -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] << ", ";
Expand All @@ -41,8 +41,8 @@ class CubicHermiteSpline {
}
}
private:
std::array<std::array<double, 4>, n_points + 1> coefficients;
std::array<Point<dimensions>, n_points + 1> points;
std::array<std::array<double, 4>, n_points> coefficients;
std::array<Point<dimensions>, n_points> points;
std::array<Point<dimensions>, 4> p;
};

Expand Down
8 changes: 4 additions & 4 deletions hermite_splines.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class HermiteSplines {
public:
explicit HermiteSplines(const std::array<Point<dimensions>, 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<double, degree+1> 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;
Expand Down Expand Up @@ -44,9 +44,9 @@ class HermiteSplines {

private:
std::array<Point<dimensions>, degree + 1> p;
std::array<std::array<double, degree+1>, n_points + 1> coefficients;
std::array<std::array<double, degree+1>, n_points> coefficients;
std::array<std::array<double, degree+1>, degree+1> coefficients_of_basis_curves;
std::array<Point<dimensions>, n_points + 1> points;
std::array<Point<dimensions>, n_points> points;

void calculate_coefficients() {
std::array<double, degree+1> coeffs_for_ploy;
Expand Down
18 changes: 9 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
// }

// int main() {
// constexpr int n_points = 100;
// constexpr int n_points = 5;
// constexpr int dimensions = 2;
// std::array<Point<dimensions>, 4> weights = {
// Point<dimensions>{110,150},//0},
Expand All @@ -62,7 +62,7 @@
// b_returned.print();
// std::cout << '\n';
//
// return 0
// return 0;
// }

// int main() {
Expand Down Expand Up @@ -90,22 +90,22 @@
// }

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<Point<dimensions>, degree+1> p = {
Point<dimensions>{0., 0.},
Point<dimensions>{4., 1.},
Point<dimensions>{0., 1.},
Point<dimensions>{0., 1.},
Point<dimensions>{0., 1.},
Point<dimensions>{0., 1.},
// Point<dimensions>{0., 1.},
// Point<dimensions>{0., 1.},
};

// auto chs = CubicHermiteSpline<n_points, dimensions>(p);
// std::cout << "Cubic Hermite Spline" << '\n';
// chs.print();
// std::cout << '\n';
const auto chs = CubicHermiteSpline<n_points, dimensions>(p);
std::cout << "Cubic Hermite Spline" << '\n';
chs.print();
std::cout << '\n';

const auto hs = HermiteSplines<degree, n_points, dimensions>(p);
std::cout << "Hermite Spline" << '\n';
Expand Down

0 comments on commit 290028f

Please sign in to comment.