diff --git a/CMakeLists.txt b/CMakeLists.txt index ad9c7f6..ab57fb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.1) -project(BezierCpp +project(Bezier LANGUAGES CXX VERSION 0.2.0) @@ -8,16 +8,16 @@ add_compile_options(-std=c++11 -march=native -fPIC) find_package(Eigen3 REQUIRED) include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR}) -set(BezierCpp_SRC +set(Bezier_SRC src/bezier.cpp src/polycurve.cpp ) -set(BezierCpp_INC - include/BezierCpp/declarations.h - include/BezierCpp/legendre_gauss.h - include/BezierCpp/bezier.h - include/BezierCpp/polycurve.h +set(Bezier_INC + include/Bezier/declarations.h + include/Bezier/legendre_gauss.h + include/Bezier/bezier.h + include/Bezier/polycurve.h ) # Options @@ -32,23 +32,23 @@ option(BUILD_SHARED_LIBS "Build shared library (.so) instead of static one (/.a) # build rules if(BUILD_SHARED_LIBS) - add_library(BezierCpp SHARED ${BezierCpp_SRC}) + add_library(bezier SHARED ${Bezier_SRC}) else() - add_library(BezierCpp STATIC ${BezierCpp_SRC}) + add_library(bezier STATIC ${Bezier_SRC}) endif() -target_include_directories(BezierCpp PUBLIC - $ +target_include_directories(bezier PUBLIC + $ $ ) -target_compile_definitions(BezierCpp PRIVATE LEGENDRE_GAUSS_N=${LEGENDRE_GAUSS_PRECISION}) +target_compile_definitions(bezier PRIVATE LEGENDRE_GAUSS_N=${LEGENDRE_GAUSS_PRECISION}) -set_target_properties(BezierCpp PROPERTIES VERSION ${PROJECT_VERSION}) -set_target_properties(BezierCpp PROPERTIES PUBLIC_HEADER "${BezierCpp_INC}") +set_target_properties(bezier PROPERTIES VERSION ${PROJECT_VERSION}) +set_target_properties(bezier PROPERTIES PUBLIC_HEADER "${Bezier_INC}") # install rules -install(TARGETS BezierCpp - EXPORT BezierCpp-export DESTINATION "lib" - PUBLIC_HEADER DESTINATION "include/BezierCpp") -install(EXPORT BezierCpp-export DESTINATION "lib/cmake/BezierCpp" FILE BezierCppConfig.cmake) +install(TARGETS bezier + EXPORT bezier-export DESTINATION "lib" + PUBLIC_HEADER DESTINATION "include/Bezier") +install(EXPORT bezier-export DESTINATION "lib/cmake/Bezier" FILE BezierConfig.cmake) diff --git a/README.md b/README.md index 3ad9413..8eb5cf0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Bezier-Cpp [![Build Status](https://travis-ci.com/stribor14/Bezier-cpp.svg?branch=master)](https://travis-ci.com/stribor14/Bezier-cpp) -![v0.2.0](https://img.shields.io/badge/version-0.2.0-blue.svg) +![v0.2](https://img.shields.io/badge/version-0.2-blue.svg) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/aceb46ce7de1407abd56cfc127dba5f1)](https://www.codacy.com/app/stribor14/Bezier-cpp?utm_source=github.com&utm_medium=referral&utm_content=stribor14/Bezier-cpp&utm_campaign=Badge_Grade) Fast and lightweight class for using the Bezier curves of any order in C++ @@ -13,6 +13,12 @@ Fast and lightweight class for using the Bezier curves of any order in C++ - Dynamic manipulation - Composite Bezier curves (polycurves) +CMake *find_package()* compatible! +``` +find_package(Bezier) +target_link_libraries(target bezier) +``` + ## Implemented methods - Get value, derivative, curvature, tangent and normal for parameter *t* - Get t from projection any point onto a curve @@ -25,28 +31,22 @@ Fast and lightweight class for using the Bezier curves of any order in C++ - Apply parametric and geometric continuities - etc. -## In development - - v0.2.1 indev Bezier polycurves +## Wish list - [ ] Polycurve - oversee continuities between consecutive sub-curves - [ ] Polycurve - propagation of sub-curve manipulation depending on continutiy + - [ ] Bezier shapes - [ ] More sophisticated example - - v0.3 planned Bezier shapes ## Dependencies - c++11 - Eigen3 ## Instalation -CMake *find_package()* compatible! -``` -find_package(BezierCpp) -target_link_libraries(target BezierCpp) -``` ### System-wide installation ``` git clone https://github.com/stribor14/Bezier-cpp -mkdir Bezicer-cpp/build -cd Bezicer-cpp/build +mkdir Bezier-cpp/build +cd Bezier-cpp/build cmake .. make make install diff --git a/example/bezier_example.pro b/example/bezier_example.pro index b3f4240..1185753 100644 --- a/example/bezier_example.pro +++ b/example/bezier_example.pro @@ -44,10 +44,10 @@ HEADERS += \ customscene.h \ qcurve.h \ qpolycurve.h \ - ../include/BezierCpp/bezier.h \ - ../include/BezierCpp/polycurve.h \ - ../include/BezierCpp/declarations.h \ - ../include/BezierCpp/legendre_gauss.h \ + ../include/Bezier/bezier.h \ + ../include/Bezier/polycurve.h \ + ../include/Bezier/declarations.h \ + ../include/Bezier/legendre_gauss.h \ FORMS += \ mainwindow.ui diff --git a/example/customscene.cpp b/example/customscene.cpp index d1bed0d..5c3eceb 100644 --- a/example/customscene.cpp +++ b/example/customscene.cpp @@ -21,7 +21,7 @@ void CustomScene::drawForeground(QPainter* painter, const QRectF& rect) painter->setPen(Qt::blue); for (auto&& curve : items()) { - Bezier::BBox bbox; + Bezier::BoundingBox bbox; if (is_curve) bbox = c_curve->boundingBox(true); if (is_poly) @@ -359,7 +359,7 @@ Delete - delete curve/polycurve"); for (auto&& curve : selectedItems()) if (is_curve) { - Bezier::CurvePtr ptr(c_curve); + std::shared_ptr ptr(c_curve); if (new_poly) new_poly->insertBack(ptr); else diff --git a/example/qcurve.h b/example/qcurve.h index 852c555..23efadc 100644 --- a/example/qcurve.h +++ b/example/qcurve.h @@ -3,8 +3,8 @@ #include -#include "BezierCpp/declarations.h" -#include "BezierCpp/bezier.h" +#include "Bezier/declarations.h" +#include "Bezier/bezier.h" class qCurve : public QGraphicsItem, public Bezier::Curve { @@ -27,7 +27,7 @@ class qCurve : public QGraphicsItem, public Bezier::Curve void setDraw_curvature_radious(bool value); bool getDraw_control_points() const; bool getDraw_curvature_radious() const; - Bezier::CurvePtr getSharedPtr(); + std::shared_ptr getSharedPtr(); bool getLocked() const; void setLocked(bool value); }; diff --git a/example/qpolycurve.cpp b/example/qpolycurve.cpp index aee6949..88139dd 100644 --- a/example/qpolycurve.cpp +++ b/example/qpolycurve.cpp @@ -3,7 +3,7 @@ #include #include -#include "BezierCpp/bezier.h" +#include "Bezier/bezier.h" bool qPolyCurve::getDraw_control_points() const { return draw_control_points; } @@ -15,26 +15,6 @@ void qPolyCurve::setDraw_curvature_radious(bool value) { draw_curvature_radious int qPolyCurve::type() const { return QGraphicsItem::UserType + 2; } -inline double kappaDerived(qPolyCurve* pcurve, double t) -{ - auto det = [](Bezier::Vec2 a, Bezier::Vec2 b) { return a.x() * b.y() - a.y() * b.x(); }; - - uint idx = t; - t = t - idx; - if (idx == pcurve->size()) - { - idx--; - t = 1; - } - auto curve = pcurve->curvePtr(idx); - - auto d1 = curve->derivativeAt(t); - auto d2 = curve->derivativeAt(2, t); - auto d3 = curve->derivativeAt(3, t); - - return (det(d1, d3) * d1.squaredNorm() - 3 * d1.dot(d2) * det(d1, d2)) / std::pow(d1.norm(), 5); -} - void qPolyCurve::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) { Q_UNUSED(option) @@ -78,8 +58,8 @@ void qPolyCurve::paint(QPainter* painter, const QStyleOptionGraphicsItem* option painter->setPen(QColor(static_cast(std::fabs(255 * (0.5 - t / size()))), static_cast(255 * t / size()), static_cast(255 * (1 - t / size())))); auto p = valueAt(t); - auto n1 = p + normalAt(t, false) * kappaDerived(this, t); - auto n2 = p - normalAt(t, false) * kappaDerived(this, t); + auto n1 = p + normalAt(t, false) * curvatureDerivativeAt(t); + auto n2 = p - normalAt(t, false) * curvatureDerivativeAt(t); painter->drawLine(QLineF(n1.x(), n1.y(), n2.x(), n2.y())); } } diff --git a/example/qpolycurve.h b/example/qpolycurve.h index 191845c..95a5fcb 100644 --- a/example/qpolycurve.h +++ b/example/qpolycurve.h @@ -3,8 +3,8 @@ #include -#include "BezierCpp/declarations.h" -#include "BezierCpp/polycurve.h" +#include "Bezier/declarations.h" +#include "Bezier/polycurve.h" class qPolyCurve : public QGraphicsItem, public Bezier::PolyCurve { @@ -12,8 +12,8 @@ class qPolyCurve : public QGraphicsItem, public Bezier::PolyCurve bool draw_control_points = false; bool draw_curvature_radious = false; public: - qPolyCurve(std::vector& curve_list) : QGraphicsItem(), Bezier::PolyCurve(curve_list) {} - qPolyCurve(Bezier::CurvePtr& curve) : QGraphicsItem(), Bezier::PolyCurve(curve) {} + qPolyCurve(std::vector>& curve_list) : QGraphicsItem(), Bezier::PolyCurve(curve_list) {} + qPolyCurve(std::shared_ptr& curve) : QGraphicsItem(), Bezier::PolyCurve(curve) {} int type() const Q_DECL_OVERRIDE; void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) Q_DECL_OVERRIDE; QRectF boundingRect() const Q_DECL_OVERRIDE; diff --git a/include/BezierCpp/bezier.h b/include/Bezier/bezier.h similarity index 94% rename from include/BezierCpp/bezier.h rename to include/Bezier/bezier.h index 72d4843..7d4faf4 100644 --- a/include/BezierCpp/bezier.h +++ b/include/Bezier/bezier.h @@ -17,6 +17,8 @@ #ifndef BEZIER_H #define BEZIER_H +#include + #include "declarations.h" namespace Bezier @@ -31,53 +33,6 @@ namespace Bezier */ class Curve { -private: - /*! - * \brief Coefficients for matrix operations - */ - typedef Eigen::MatrixXd Coeffs; - /*! - * \brief Map of different coefficient matrices, depending on the order of the curve - */ - typedef std::map CoeffsMap; - - /// Number of control points (order + 1) - uint N_; - /// N x 2 matrix where each row corresponds to control Point - Eigen::MatrixX2d control_points_; - - // private caching - ConstCurvePtr cached_derivative_; /*! If generated, stores derivative for later use */ - std::unique_ptr cached_roots_; /*! If generated, stores roots for later use */ - std::tuple cached_roots_params_{0, 0, 0}; /*! epsilon and max_iter of cached roots */ - std::unique_ptr - cached_bounding_box_tight_; /*! If generated, stores bounding box (use_roots = true) for later use */ - std::unique_ptr - cached_bounding_box_relaxed_; /*! If generated, stores bounding box (use_roots = false) for later use */ - std::unique_ptr cached_polyline_; /*! If generated, stores polyline for later use */ - std::tuple cached_polyline_params_{0, 0}; /*! Smootheness and precision of cached polyline */ - - /// Reset all privately cached data - inline void resetCache(); - - // static caching - static CoeffsMap bernstein_coeffs_; /*! Map of Bernstein coefficients */ - static CoeffsMap splitting_coeffs_left_; /*! Map of coefficients to get subcurve for t = [0, 0.5] */ - static CoeffsMap splitting_coeffs_right_; /*! Map of coefficients to get subcurve for t = [0.5, 1] */ - static CoeffsMap elevate_order_coeffs_; /*! Map of coefficients for elevating the order of curve */ - static CoeffsMap lower_order_coeffs_; /*! Map of coefficients for lowering the order of curve */ - - /// Private getter function for Bernstein coefficients - Coeffs bernsteinCoeffs() const; - /// Private getter function for coefficients to get a subcurve t = [0, z]; - Coeffs splittingCoeffsLeft(double z = 0.5) const; - /// Private getter function for coefficients to get a subcurve t = [z, 1]; - Coeffs splittingCoeffsRight(double z = 0.5) const; - /// Private getter function for coefficients to elevate order of curve - Coeffs elevateOrderCoeffs(uint n) const; - /// Private getter function for coefficients to lower order of curve - Coeffs lowerOrderCoeffs(uint n) const; - public: /*! * \brief Create the Bezier curve @@ -222,7 +177,7 @@ class Curve * \param normalize If the resulting tangent should be normalized * \return Tangent of a curve for a given t */ - Vec2 tangentAt(double t, bool normalize = true) const; + Vector tangentAt(double t, bool normalize = true) const; /*! * \brief Get the normal of curve for a given t @@ -230,13 +185,13 @@ class Curve * \param normalize If the resulting normal should be normalized * \return Normal of a curve for given t */ - Vec2 normalAt(double t, bool normalize = true) const; + Vector normalAt(double t, bool normalize = true) const; /*! * \brief Get the derivative of a curve * \return Derivative curve */ - ConstCurvePtr derivative() const; + std::shared_ptr derivative() const; /*! * \brief Get the nth derivative of a curve @@ -244,7 +199,7 @@ class Curve * \return Derivative curve * \warning Parameter n cannot be zero */ - ConstCurvePtr derivative(uint n) const; + std::shared_ptr derivative(uint n) const; /*! * \brief Get value of a derivative for a given t @@ -275,7 +230,7 @@ class Curve * \param use_roots If algorithm should use roots * \return Bounding box (if use_roots is false, returns the bounding box of control points) */ - BBox boundingBox(bool use_roots = true) const; + BoundingBox boundingBox(bool use_roots = true) const; /*! * \brief Split the curve into two subcurves @@ -310,6 +265,53 @@ class Curve * \param beta_coeffs Beta-constraints used to calculate continuity. Size defines continuity order. */ void applyContinuity(const Curve& source_curve, std::vector& beta_coeffs); + +private: + /*! + * \brief Coefficients for matrix operations + */ + typedef Eigen::MatrixXd Coeffs; + /*! + * \brief Map of different coefficient matrices, depending on the order of the curve + */ + typedef std::map CoeffsMap; + + /// Number of control points (order + 1) + uint N_; + /// N x 2 matrix where each row corresponds to control Point + Eigen::MatrixX2d control_points_; + + // private caching + std::shared_ptr cached_derivative_; /*! If generated, stores derivative for later use */ + std::unique_ptr cached_roots_; /*! If generated, stores roots for later use */ + std::tuple cached_roots_params_{0, 0, 0}; /*! epsilon and max_iter of cached roots */ + std::unique_ptr + cached_bounding_box_tight_; /*! If generated, stores bounding box (use_roots = true) for later use */ + std::unique_ptr + cached_bounding_box_relaxed_; /*! If generated, stores bounding box (use_roots = false) for later use */ + std::unique_ptr cached_polyline_; /*! If generated, stores polyline for later use */ + std::tuple cached_polyline_params_{0, 0}; /*! Smootheness and precision of cached polyline */ + + /// Reset all privately cached data + inline void resetCache(); + + // static caching + static CoeffsMap bernstein_coeffs_; /*! Map of Bernstein coefficients */ + static CoeffsMap splitting_coeffs_left_; /*! Map of coefficients to get subcurve for t = [0, 0.5] */ + static CoeffsMap splitting_coeffs_right_; /*! Map of coefficients to get subcurve for t = [0.5, 1] */ + static CoeffsMap elevate_order_coeffs_; /*! Map of coefficients for elevating the order of curve */ + static CoeffsMap lower_order_coeffs_; /*! Map of coefficients for lowering the order of curve */ + + /// Private getter function for Bernstein coefficients + Coeffs bernsteinCoeffs() const; + /// Private getter function for coefficients to get a subcurve t = [0, z]; + Coeffs splittingCoeffsLeft(double z = 0.5) const; + /// Private getter function for coefficients to get a subcurve t = [z, 1]; + Coeffs splittingCoeffsRight(double z = 0.5) const; + /// Private getter function for coefficients to elevate order of curve + Coeffs elevateOrderCoeffs(uint n) const; + /// Private getter function for coefficients to lower order of curve + Coeffs lowerOrderCoeffs(uint n) const; }; } // namespace Bezier diff --git a/include/BezierCpp/declarations.h b/include/Bezier/declarations.h similarity index 77% rename from include/BezierCpp/declarations.h rename to include/Bezier/declarations.h index 0a734a5..6fe720f 100644 --- a/include/BezierCpp/declarations.h +++ b/include/Bezier/declarations.h @@ -19,9 +19,7 @@ #include #include -#include #include -#include /*! * Nominal namespace containing class pre-definitions and typedefs @@ -56,14 +54,6 @@ class PolyLine; */ class PolyCurve; -/*! - * \brief Shared pointer of Curve - */ -typedef std::shared_ptr CurvePtr; -/*! - * \brief Shared pointer of const Curve - */ -typedef std::shared_ptr ConstCurvePtr; /*! * \brief Point in xy plane */ @@ -71,22 +61,14 @@ typedef Eigen::Vector2d Point; /*! * \brief A Vector in xy plane */ -typedef Eigen::Vector2d Vec2; +typedef Eigen::Vector2d Vector; /*! * \brief A vector of Points */ typedef std::vector PointVector; /*! - * \brief Bounding box - */ -typedef Eigen::AlignedBox2d BBox; -/*! - * \brief Shared pointer of Polycurve - */ -typedef std::shared_ptr PolyCurvePtr; -/*! - * \brief Shared pointer of const Polycurve + * \brief Bounding box class */ -typedef std::shared_ptr ConstPolyCurvePtr; +typedef Eigen::AlignedBox2d BoundingBox; } #endif // DECLARATIONS_H diff --git a/include/BezierCpp/legendre_gauss.h b/include/Bezier/legendre_gauss.h similarity index 99% rename from include/BezierCpp/legendre_gauss.h rename to include/Bezier/legendre_gauss.h index 54ca052..2c39b36 100644 --- a/include/BezierCpp/legendre_gauss.h +++ b/include/Bezier/legendre_gauss.h @@ -2,6 +2,8 @@ #define LEGENDRE_GAUSS_H /* + * Legendre-Gauss coefficients used for calculating exact length of parametric curves + * * Abcissae and weights taken from: * https://pomax.github.io/bezierinfo/legendre-gauss.html */ diff --git a/include/BezierCpp/polycurve.h b/include/Bezier/polycurve.h similarity index 91% rename from include/BezierCpp/polycurve.h rename to include/Bezier/polycurve.h index ebddca1..a24b4ce 100644 --- a/include/BezierCpp/polycurve.h +++ b/include/Bezier/polycurve.h @@ -17,6 +17,8 @@ #ifndef POLYCURVE_H #define POLYCURVE_H +#include + #include "declarations.h" namespace Bezier @@ -33,16 +35,6 @@ namespace Bezier */ class PolyCurve { -private: - /// Structure for holding underlying Bezier curves - std::deque curves_; - - /*! - * \brief Constructor for easier creation of sub-polycurve - * \param curve_list A list of continuus sub-curves - */ - PolyCurve(const std::deque& curve_list); - public: /*! * \brief Create the empty Bezier polycurve @@ -53,13 +45,13 @@ class PolyCurve * \brief Create the Bezier polycurve with only one subcurve * \param curve A single curve */ - PolyCurve(CurvePtr& curve); + PolyCurve(std::shared_ptr& curve); /*! * \brief Create the Bezier polycurve from vector of curves * \param curve_list A list of curves */ - PolyCurve(std::vector& curve_list); + PolyCurve(std::vector>& curve_list); /*! * \brief Create a copy of Bezier polycurve @@ -72,19 +64,19 @@ class PolyCurve * \param idx Index where to insert new curve * \param curve A curve to insert */ - void insertAt(uint idx, CurvePtr& curve); + void insertAt(uint idx, std::shared_ptr& curve); /*! * \brief Insert new curve at the beginning of polycurve * \param curve A curve to insert */ - void insertFront(CurvePtr& curve); + void insertFront(std::shared_ptr& curve); /*! * \brief Insert new curve at the end of polycurve * \param curve A curve to insert */ - void insertBack(CurvePtr& curve); + void insertBack(std::shared_ptr& curve); /*! * \brief Remove a subcurve from polycurve @@ -127,13 +119,13 @@ class PolyCurve * \param idx Subcurve index * \return A shared pointer */ - CurvePtr curvePtr(uint idx) const; + std::shared_ptr curvePtr(uint idx) const; /*! * \brief Get list of all subcurves * \return A vector of pointers */ - std::vector curveList() const; + std::vector> curveList() const; /*! * \brief Get a polyline representation of polycurve as a vector of points on curve @@ -223,7 +215,7 @@ class PolyCurve * \param normalize If the resulting tangent should be normalized * \return Tangent of a polycurve for a given t */ - Vec2 tangentAt(double t, bool normalize = true) const; + Vector tangentAt(double t, bool normalize = true) const; /*! * \brief Get the normal of polycurve for a given t @@ -231,7 +223,7 @@ class PolyCurve * \param normalize If the resulting normal should be normalized * \return Normal of a polycurve for given t */ - Vec2 normalAt(double t, bool normalize = true) const; + Vector normalAt(double t, bool normalize = true) const; /*! * \brief Get value of a derivative for a given t @@ -253,7 +245,7 @@ class PolyCurve * \param use_roots If algorithm should use roots * \return Bounding box (if use_roots is false, returns the bounding box of control points) */ - BBox boundingBox(bool use_roots = true) const; + BoundingBox boundingBox(bool use_roots = true) const; /*! * \brief Get the points of intersection with another curve or polycurve @@ -276,6 +268,16 @@ class PolyCurve * \return Parameter t */ double projectPoint(const Point& point, double step = 0.01, double epsilon = 0.001) const; + +private: + /// Structure for holding underlying Bezier curves + std::deque> curves_; + + /*! + * \brief Constructor for easier creation of sub-polycurve + * \param curve_list A list of continuus sub-curves + */ + PolyCurve(const std::deque>& curve_list); }; } // namespace Bezier diff --git a/src/bezier.cpp b/src/bezier.cpp index 7c32166..86d0ff7 100644 --- a/src/bezier.cpp +++ b/src/bezier.cpp @@ -1,5 +1,5 @@ -#include "BezierCpp/bezier.h" -#include "BezierCpp/legendre_gauss.h" +#include "Bezier/bezier.h" +#include "Bezier/legendre_gauss.h" #include @@ -302,7 +302,7 @@ double Curve::curvatureDerivativeAt(double t) const 3 * d1.dot(d2) * (d1.x() * d2.y() - d1.y() * d2.x()) / std::pow(d1.norm(), 5); } -Vec2 Curve::tangentAt(double t, bool normalize) const +Vector Curve::tangentAt(double t, bool normalize) const { Point p(derivativeAt(t)); if (normalize && p.norm() > 0) @@ -310,13 +310,13 @@ Vec2 Curve::tangentAt(double t, bool normalize) const return p; } -Vec2 Curve::normalAt(double t, bool normalize) const +Vector Curve::normalAt(double t, bool normalize) const { Point tangent = tangentAt(t, normalize); - return Vec2(-tangent.y(), tangent.x()); + return Vector(-tangent.y(), tangent.x()); } -ConstCurvePtr Curve::derivative() const +std::shared_ptr Curve::derivative() const { if (!cached_derivative_) { @@ -328,11 +328,11 @@ ConstCurvePtr Curve::derivative() const return cached_derivative_; } -ConstCurvePtr Curve::derivative(uint n) const +std::shared_ptr Curve::derivative(uint n) const { if (n == 0) throw std::invalid_argument{"Parameter 'n' cannot be zero."}; - ConstCurvePtr nth_derivative = derivative(); + std::shared_ptr nth_derivative = derivative(); for (uint k = 1; k < n; k++) nth_derivative = nth_derivative->derivative(); return nth_derivative; @@ -397,7 +397,7 @@ PointVector Curve::roots(double step, double epsilon, std::size_t max_iter) cons return *cached_roots_; } -BBox Curve::boundingBox(bool use_roots) const +BoundingBox Curve::boundingBox(bool use_roots) const { if (!(use_roots ? cached_bounding_box_tight_ : cached_bounding_box_relaxed_)) { @@ -421,7 +421,7 @@ BBox Curve::boundingBox(bool use_roots) const [](const Point& lhs, const Point& rhs) { return lhs.y() < rhs.y(); }); (use_roots ? (const_cast(this))->cached_bounding_box_tight_ : (const_cast(this))->cached_bounding_box_relaxed_) - .reset(new BBox(Point(x_extremes.first->x(), y_extremes.first->y()), + .reset(new BoundingBox(Point(x_extremes.first->x(), y_extremes.first->y()), Point(x_extremes.second->x(), y_extremes.second->y()))); } return *(use_roots ? cached_bounding_box_tight_ : cached_bounding_box_relaxed_); @@ -479,7 +479,7 @@ PointVector Curve::pointsOfIntersection(const Curve& curve, bool stop_at_first, } auto bbox = [](Eigen::MatrixX2d cp) { - return BBox(Point(cp.col(0).minCoeff(), cp.col(1).minCoeff()), Point(cp.col(0).maxCoeff(), cp.col(1).maxCoeff())); + return BoundingBox(Point(cp.col(0).minCoeff(), cp.col(1).minCoeff()), Point(cp.col(0).maxCoeff(), cp.col(1).maxCoeff())); }; while (!subcurve_pairs.empty()) @@ -488,8 +488,8 @@ PointVector Curve::pointsOfIntersection(const Curve& curve, bool stop_at_first, Eigen::MatrixX2d part_b = std::get<1>(subcurve_pairs.back()); subcurve_pairs.pop_back(); - BBox bbox1 = bbox(part_a); - BBox bbox2 = bbox(part_b); + BoundingBox bbox1 = bbox(part_a); + BoundingBox bbox2 = bbox(part_b); if (!bbox1.intersects(bbox2)) { // no intersection diff --git a/src/polycurve.cpp b/src/polycurve.cpp index b87372f..4e849dd 100644 --- a/src/polycurve.cpp +++ b/src/polycurve.cpp @@ -1,5 +1,5 @@ -#include "BezierCpp/polycurve.h" -#include "BezierCpp/bezier.h" +#include "Bezier/polycurve.h" +#include "Bezier/bezier.h" #include @@ -7,13 +7,13 @@ inline double binomial(uint n, uint k) { return tgamma(n + 1) / (tgamma(k + 1) * using namespace Bezier; -PolyCurve::PolyCurve(const std::deque& part_list) : curves_(part_list) {} +PolyCurve::PolyCurve(const std::deque>& part_list) : curves_(part_list) {} PolyCurve::PolyCurve() {} -PolyCurve::PolyCurve(CurvePtr& curve) { curves_.push_back(curve); } +PolyCurve::PolyCurve(std::shared_ptr &curve) { curves_.push_back(curve); } -PolyCurve::PolyCurve(std::vector& curve_list) +PolyCurve::PolyCurve(std::vector>& curve_list) { for (auto&& curve : curve_list) insertBack(curve); @@ -21,7 +21,7 @@ PolyCurve::PolyCurve(std::vector& curve_list) PolyCurve::PolyCurve(const PolyCurve& poly_curve) : PolyCurve(poly_curve.curves_) {} -void PolyCurve::insertAt(uint idx, CurvePtr& curve) +void PolyCurve::insertAt(uint idx, std::shared_ptr& curve) { Point s_1, s_2, e_1, e_2; std::tie(s_1, e_1) = curve->endPoints(); @@ -61,9 +61,9 @@ void PolyCurve::insertAt(uint idx, CurvePtr& curve) curves_.insert(curves_.begin() + idx, curve); } -void PolyCurve::insertFront(CurvePtr& curve) { insertAt(0, curve); } +void PolyCurve::insertFront(std::shared_ptr& curve) { insertAt(0, curve); } -void PolyCurve::insertBack(CurvePtr& curve) { insertAt(size(), curve); } +void PolyCurve::insertBack(std::shared_ptr& curve) { insertAt(size(), curve); } void PolyCurve::removeAt(uint idx) { @@ -88,7 +88,7 @@ void PolyCurve::removeBack() { curves_.pop_back(); } PolyCurve PolyCurve::subPolyCurve(uint idx_l, uint idx_r) const { - return PolyCurve(std::deque(curves_.begin() + idx_l, curves_.begin() + idx_r)); + return PolyCurve(std::deque>(curves_.begin() + idx_l, curves_.begin() + idx_r)); } uint PolyCurve::size() const { return static_cast(curves_.size()); } @@ -99,9 +99,9 @@ uint PolyCurve::curveIdx(double t) const return idx - (idx == size()); } -CurvePtr PolyCurve::curvePtr(uint idx) const { return curves_.at(idx); } +std::shared_ptr PolyCurve::curvePtr(uint idx) const { return curves_.at(idx); } -std::vector PolyCurve::curveList() const { return std::vector(curves_.begin(), curves_.end()); } +std::vector> PolyCurve::curveList() const { return std::vector>(curves_.begin(), curves_.end()); } PointVector PolyCurve::polyline(double smoothness, double precision) const { @@ -131,7 +131,7 @@ double PolyCurve::length(double t1, double t2) const else return std::accumulate(begin(curves_) + idx1 + 1, begin(curves_) + idx2, curves_.at(idx1)->length(t1 - idx1, 1.0) + curves_.at(idx2)->length(0.0, t2 - idx2), - [](double sum, Bezier::ConstCurvePtr curve) { return sum + curve->length(); }); + [](double sum, std::shared_ptr curve) { return sum + curve->length(); }); } double PolyCurve::iterateByLength(double t, double s, double epsilon, std::size_t max_iter) const @@ -210,13 +210,13 @@ double PolyCurve::curvatureDerivativeAt(double t) const return curvePtr(idx)->curvatureDerivativeAt(t - idx); } -Vec2 PolyCurve::tangentAt(double t, bool normalize) const +Vector PolyCurve::tangentAt(double t, bool normalize) const { uint idx = curveIdx(t); return curvePtr(idx)->tangentAt(t - idx, normalize); } -Vec2 PolyCurve::normalAt(double t, bool normalize) const +Vector PolyCurve::normalAt(double t, bool normalize) const { uint idx = curveIdx(t); return curvePtr(idx)->normalAt(t - idx, normalize); @@ -234,9 +234,9 @@ Point PolyCurve::derivativeAt(uint n, double t) const return curvePtr(idx)->derivativeAt(n, t - idx); } -BBox PolyCurve::boundingBox(bool use_roots) const +BoundingBox PolyCurve::boundingBox(bool use_roots) const { - BBox bbox; + BoundingBox bbox; for (uint k = 0; k < size(); k++) bbox.extend(curves_.at(k)->boundingBox(use_roots)); return bbox;