From 6f9ee3d7f87af4f635c2ee188d456fd2cda140db Mon Sep 17 00:00:00 2001 From: stribor14 Date: Fri, 20 Sep 2024 08:45:21 +0200 Subject: [PATCH] Remove manipulateCurvature method. Close #38 --- example/customscene.cpp | 106 +++++++++++++++++----------------------- example/customscene.h | 4 +- include/Bezier/bezier.h | 10 ---- src/bezier.cpp | 31 ------------ 4 files changed, 45 insertions(+), 106 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/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_;