Skip to content

Commit

Permalink
Remove manipulateCurvature method. Close #38
Browse files Browse the repository at this point in the history
  • Loading branch information
stribor14 committed Sep 20, 2024
1 parent 15c3cb6 commit bcb5e90
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 106 deletions.
106 changes: 44 additions & 62 deletions example/customscene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
}
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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)
Expand All @@ -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\
Expand Down Expand Up @@ -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<double> beta_coeffs = {1, 0, 0};
curve_unlocked->applyContinuity(*dynamic_cast<Bezier::Curve*>(curve_locked), beta_coeffs);
update();
}
std::vector<double> beta_coeffs = {1, 0, 0};
curve_unlocked->applyContinuity(*dynamic_cast<Bezier::Curve*>(curve_locked), beta_coeffs);
update();
}
}

// if (keyEvent->key() >= 48 && keyEvent->key() <= 59) // num keys
Expand Down
4 changes: 1 addition & 3 deletions example/customscene.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <QGraphicsScene>

#include "qcurve.h"
#include "qpolycurve.h"
#include "qgraphicsviewzoom.h"
#include "qpolycurve.h"

namespace Ui
{
Expand All @@ -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<qCurve*, double> t_to_update;
bool update_cp = false;
std::pair<QGraphicsItem*, uint> cp_to_update;

Expand Down
10 changes: 0 additions & 10 deletions include/Bezier/bezier.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
31 changes: 0 additions & 31 deletions src/bezier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,37 +287,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_;
Expand Down

0 comments on commit bcb5e90

Please sign in to comment.