From b58675d511a42de21aaab093579ef44c5ff890e3 Mon Sep 17 00:00:00 2001 From: stribor14 Date: Fri, 20 Sep 2024 08:45:21 +0200 Subject: [PATCH] Remove manipulateCurvature method. Run formatter on example. Close #38 --- example/customscene.cpp | 106 ++++++++++++++-------------------- example/customscene.h | 4 +- example/mainwindow.cpp | 13 +---- example/qcurve.cpp | 10 +--- example/qcurve.h | 2 +- example/qgraphicsviewzoom.cpp | 2 +- example/qgraphicsviewzoom.h | 2 +- example/qpolycurve.cpp | 4 +- example/qpolycurve.h | 3 +- include/Bezier/bezier.h | 10 ---- src/bezier.cpp | 31 ---------- 11 files changed, 57 insertions(+), 130 deletions(-) diff --git a/example/customscene.cpp b/example/customscene.cpp index 393a32e..54121af 100644 --- a/example/customscene.cpp +++ b/example/customscene.cpp @@ -77,7 +77,8 @@ void CustomScene::mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent) QPen(Qt::blue))); auto t2 = c_curve->iterateByLength(t1, 50); auto a = c_curve->valueAt(t2); - byLength.insert(curve, addEllipse(QRectF(QPointF(a.x() - 3, a.y() - 3), QSizeF(6, 6)), QPen(Qt::yellow), QBrush(Qt::red, Qt::SolidPattern))); + byLength.insert(curve, addEllipse(QRectF(QPointF(a.x() - 3, a.y() - 3), QSizeF(6, 6)), QPen(Qt::yellow), + QBrush(Qt::red, Qt::SolidPattern))); } if (is_poly) { @@ -90,7 +91,8 @@ void CustomScene::mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent) QPen(Qt::blue))); auto t2 = c_poly->iterateByLength(t1, 50); auto a = c_poly->valueAt(t2); - byLength.insert(curve, addEllipse(QRectF(QPointF(a.x() - 3, a.y() - 3), QSizeF(6, 6)), QPen(Qt::yellow), QBrush(Qt::red, Qt::SolidPattern))); + byLength.insert(curve, addEllipse(QRectF(QPointF(a.x() - 3, a.y() - 3), QSizeF(6, 6)), QPen(Qt::yellow), + QBrush(Qt::red, Qt::SolidPattern))); } } show_projection = true; @@ -145,18 +147,6 @@ void CustomScene::mousePressEvent(QGraphicsSceneMouseEvent* mouseEvent) } if (update_cp) break; - if (is_curve) - { - double t = c_curve->projectPoint(p); - auto pt = c_curve->valueAt(t); - auto ep = c_curve->endPoints(); - if ((pt - p).norm() < 10 && (pt - ep.first).norm() > 20 && (pt - ep.second).norm() > 20) - { - update_curvature = true; - t_to_update = std::make_pair(c_curve, t); - break; - } - } } } } @@ -235,20 +225,6 @@ void CustomScene::mouseMoveEvent(QGraphicsSceneMouseEvent* mouseEvent) } update(); } - if (update_curvature) - { - try - { - t_to_update.first->prepareGeometryChange(); - t_to_update.first->manipulateCurvature(t_to_update.second, p); - update(); - } - catch (char const* err) - { - update_curvature = false; - QMessageBox::warning(nullptr, "Warning", QString().sprintf("%s", err)); - } - } QGraphicsScene::mouseMoveEvent(mouseEvent); } @@ -273,10 +249,7 @@ void CustomScene::mouseReleaseEvent(QGraphicsSceneMouseEvent* mouseEvent) } } if (mouseEvent->button() == Qt::LeftButton) - { update_cp = false; - update_curvature = false; - } } void CustomScene::keyPressEvent(QKeyEvent* keyEvent) @@ -290,7 +263,6 @@ Ctrl + Scroll - zoom in/out\n\ Ctrl + Left click - select curves\n\ Left click - deselect curves\n\ Left click + drag control point - manipulate control point\n\ -Left click + drag curve - manipulate curve (only for 2nd and 3rd order)\n\ \n\ Keyboard shortcuts:\n\ H - display help\n\ @@ -377,43 +349,53 @@ Delete - delete curve/polycurve"); } update(); } - if (keyEvent->key() == Qt::Key_L) { - for (auto&& curve : selectedItems()) { - if (is_curve) { - c_curve->setLocked(!c_curve->getLocked()); - } + if (keyEvent->key() == Qt::Key_L) + { + for (auto&& curve : selectedItems()) + { + if (is_curve) + { + c_curve->setLocked(!c_curve->getLocked()); } - update(); + } + update(); } - if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) { - if (selectedItems().size() != 2) { - return; - } + if (keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) + { + if (selectedItems().size() != 2) + { + return; + } - qCurve* curve_locked = nullptr; - qCurve* curve_unlocked = nullptr; + qCurve* curve_locked = nullptr; + qCurve* curve_unlocked = nullptr; - if (c_curve_item(selectedItems().first())->getLocked()) { - curve_locked = c_curve_item(selectedItems().first()); - curve_unlocked = c_curve_item(selectedItems().last()); + if (c_curve_item(selectedItems().first())->getLocked()) + { + curve_locked = c_curve_item(selectedItems().first()); + curve_unlocked = c_curve_item(selectedItems().last()); + } + else if (c_curve_item(selectedItems().last())->getLocked()) + { + curve_locked = c_curve_item(selectedItems().last()); + curve_unlocked = c_curve_item(selectedItems().first()); + } + + if (curve_locked != nullptr && curve_unlocked != nullptr) + { + while (curve_locked->order() < 5) + { + curve_locked->elevateOrder(); } - else if (c_curve_item(selectedItems().last())->getLocked()) { - curve_locked = c_curve_item(selectedItems().last()); - curve_unlocked = c_curve_item(selectedItems().first()); + while (curve_unlocked->order() < 5) + { + curve_unlocked->elevateOrder(); } - if (curve_locked != nullptr && curve_unlocked != nullptr) { - while (curve_locked->order() < 5) { - curve_locked->elevateOrder(); - } - while (curve_unlocked->order() < 5) { - curve_unlocked->elevateOrder(); - } - - std::vector beta_coeffs = {1, 0, 0}; - curve_unlocked->applyContinuity(*dynamic_cast(curve_locked), beta_coeffs); - update(); - } + std::vector beta_coeffs = {1, 0, 0}; + curve_unlocked->applyContinuity(*dynamic_cast(curve_locked), beta_coeffs); + update(); + } } // if (keyEvent->key() >= 48 && keyEvent->key() <= 59) // num keys diff --git a/example/customscene.h b/example/customscene.h index 0288c02..58dc991 100644 --- a/example/customscene.h +++ b/example/customscene.h @@ -4,8 +4,8 @@ #include #include "qcurve.h" -#include "qpolycurve.h" #include "qgraphicsviewzoom.h" +#include "qpolycurve.h" namespace Ui { @@ -28,8 +28,6 @@ class CustomScene : public QGraphicsScene bool draw_box_ = false; bool draw_inter_ = false; bool show_projection = false; - bool update_curvature = false; - std::pair t_to_update; bool update_cp = false; std::pair cp_to_update; diff --git a/example/mainwindow.cpp b/example/mainwindow.cpp index cdd27ff..f1a4369 100644 --- a/example/mainwindow.cpp +++ b/example/mainwindow.cpp @@ -13,16 +13,9 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi Eigen::MatrixX2d cp1, cp2; cp1.resize(4, 2); cp2.resize(5, 2); - cp1 << 84, 162, - 246, 30, - 48, 236, - 180, 110; - - cp2 << 180, 110, - 175, 160, - 60, 48, - 164, 165, - 124, 134; + cp1 << 84, 162, 246, 30, 48, 236, 180, 110; + + cp2 << 180, 110, 175, 160, 60, 48, 164, 165, 124, 134; scene->addItem(new qCurve(cp1 * 5)); scene->addItem(new qCurve(cp2 * 5)); diff --git a/example/qcurve.cpp b/example/qcurve.cpp index b6dd19a..d661d0c 100644 --- a/example/qcurve.cpp +++ b/example/qcurve.cpp @@ -11,15 +11,9 @@ bool qCurve::getDraw_control_points() const { return draw_control_points; } bool qCurve::getDraw_curvature_radious() const { return draw_curvature_radious; } -bool qCurve::getLocked() const -{ - return locked; -} +bool qCurve::getLocked() const { return locked; } -void qCurve::setLocked(bool value) -{ - locked = value; -} +void qCurve::setLocked(bool value) { locked = value; } int qCurve::type() const { return QGraphicsItem::UserType + 1; } diff --git a/example/qcurve.h b/example/qcurve.h index 23efadc..22f6890 100644 --- a/example/qcurve.h +++ b/example/qcurve.h @@ -3,8 +3,8 @@ #include -#include "Bezier/declarations.h" #include "Bezier/bezier.h" +#include "Bezier/declarations.h" class qCurve : public QGraphicsItem, public Bezier::Curve { diff --git a/example/qgraphicsviewzoom.cpp b/example/qgraphicsviewzoom.cpp index ba114a3..9ac3bfc 100644 --- a/example/qgraphicsviewzoom.cpp +++ b/example/qgraphicsviewzoom.cpp @@ -1,7 +1,7 @@ #include "qgraphicsviewzoom.h" -#include #include +#include #include #include diff --git a/example/qgraphicsviewzoom.h b/example/qgraphicsviewzoom.h index ea39aaf..f234c01 100644 --- a/example/qgraphicsviewzoom.h +++ b/example/qgraphicsviewzoom.h @@ -1,9 +1,9 @@ #ifndef QGRAPHICSVIEWZOOM_H #define QGRAPHICSVIEWZOOM_H -#include #include #include +#include /*! * \brief This class adds ability to zoom QGraphicsView using mouse wheel. The point under cursor diff --git a/example/qpolycurve.cpp b/example/qpolycurve.cpp index 80c3702..0aaef65 100644 --- a/example/qpolycurve.cpp +++ b/example/qpolycurve.cpp @@ -55,8 +55,8 @@ void qPolyCurve::paint(QPainter* painter, const QStyleOptionGraphicsItem* option painter->setPen(Qt::green); for (double t = 0; t <= size(); t += 1.0 / 500) { - painter->setPen(QColor(static_cast(std::fabs(255 * (0.5 - t / size()))), - static_cast(255 * t / size()), static_cast(255 * (1 - t / size())))); + 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) * curvatureDerivativeAt(t); auto n2 = p - normalAt(t, false) * curvatureDerivativeAt(t); diff --git a/example/qpolycurve.h b/example/qpolycurve.h index 0b0529c..294fea2 100644 --- a/example/qpolycurve.h +++ b/example/qpolycurve.h @@ -3,15 +3,16 @@ #include +#include "Bezier/bezier.h" #include "Bezier/declarations.h" #include "Bezier/polycurve.h" -#include "Bezier/bezier.h" class qPolyCurve : public QGraphicsItem, public Bezier::PolyCurve { private: bool draw_control_points = false; bool draw_curvature_radious = false; + public: qPolyCurve(const std::deque& curve_list) : QGraphicsItem(), Bezier::PolyCurve(curve_list) {} qPolyCurve(const Bezier::Curve& curve) : QGraphicsItem(), Bezier::PolyCurve(std::deque{curve}) {} diff --git a/include/Bezier/bezier.h b/include/Bezier/bezier.h index 33df198..d9e9744 100644 --- a/include/Bezier/bezier.h +++ b/include/Bezier/bezier.h @@ -127,16 +127,6 @@ class Curve */ void setControlPoint(unsigned idx, const Point& point); - /*! - * \brief Manipulate the curve so that it passes through wanted point for given 't' - * \param t Curve parameter - * \param point Point where curve should pass for a given t - * - * \warning CAN THROW: Only works for quadratic and cubic curves - * \warning Resets cached data - */ - void manipulateCurvature(double t, const Point& point); - /*! * \brief Raise the curve order by 1 * diff --git a/src/bezier.cpp b/src/bezier.cpp index e78941d..46afa22 100644 --- a/src/bezier.cpp +++ b/src/bezier.cpp @@ -235,37 +235,6 @@ void Curve::setControlPoint(unsigned idx, const Point& point) resetCache(); } -void Curve::manipulateCurvature(double t, const Point& point) -{ - if (N_ < 3 || N_ > 4) - throw std::logic_error{"Only quadratic and cubic curves can be manipulated"}; - - double r = std::fabs((_pow(t, N_ - 1) + _pow(1 - t, N_ - 1) - 1) / (_pow(t, N_ - 1) + _pow(1 - t, N_ - 1))); - double u = _pow(1 - t, N_ - 1) / (_pow(t, N_ - 1) + _pow(1 - t, N_ - 1)); - Point C = u * control_points_.row(0) + (1 - u) * control_points_.row(N_ - 1); - const Point& B = point; - Point A = B - (C - B) / r; - - switch (N_) - { - case 3: - control_points_.row(1) = A; - break; - case 4: - Point e1 = control_points_.row(0) * _pow(1 - t, 2) + control_points_.row(1) * 2 * t * (1 - t) + - control_points_.row(2) * _pow(t, 2); - Point e2 = control_points_.row(1) * _pow(1 - t, 2) + control_points_.row(2) * 2 * t * (1 - t) + - control_points_.row(3) * _pow(t, 2); - e1 = B + e1 - valueAt(t); - e2 = B + e2 - valueAt(t); - Point v1 = A - (A - e1) / (1 - t); - Point v2 = A + (e2 - A) / t; - control_points_.row(1).noalias() = control_points_.row(0) + (v1.transpose() - control_points_.row(0)) / t; - control_points_.row(2).noalias() = control_points_.row(3) - (control_points_.row(3) - v2.transpose()) / (1 - t); - } - resetCache(); -} - void Curve::elevateOrder() { control_points_ = elevateOrderCoeffs(N_) * control_points_;