Skip to content

Commit

Permalink
Remove manipulateCurvature method. Run formatter on example. Close #38
Browse files Browse the repository at this point in the history
  • Loading branch information
stribor14 committed Oct 1, 2024
1 parent f79db79 commit b58675d
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 130 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
13 changes: 3 additions & 10 deletions example/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
10 changes: 2 additions & 8 deletions example/qcurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
2 changes: 1 addition & 1 deletion example/qcurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include <QGraphicsItem>

#include "Bezier/declarations.h"
#include "Bezier/bezier.h"
#include "Bezier/declarations.h"

class qCurve : public QGraphicsItem, public Bezier::Curve
{
Expand Down
2 changes: 1 addition & 1 deletion example/qgraphicsviewzoom.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "qgraphicsviewzoom.h"

#include <QMouseEvent>
#include <QApplication>
#include <QMouseEvent>
#include <QScrollBar>
#include <qmath.h>

Expand Down
2 changes: 1 addition & 1 deletion example/qgraphicsviewzoom.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef QGRAPHICSVIEWZOOM_H
#define QGRAPHICSVIEWZOOM_H

#include <QObject>
#include <QDebug>
#include <QGraphicsView>
#include <QObject>

/*!
* \brief This class adds ability to zoom QGraphicsView using mouse wheel. The point under cursor
Expand Down
4 changes: 2 additions & 2 deletions example/qpolycurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(std::fabs(255 * (0.5 - t / size()))),
static_cast<int>(255 * t / size()), static_cast<int>(255 * (1 - t / size()))));
painter->setPen(QColor(static_cast<int>(std::fabs(255 * (0.5 - t / size()))), static_cast<int>(255 * t / size()),
static_cast<int>(255 * (1 - t / size()))));
auto p = valueAt(t);
auto n1 = p + normalAt(t, false) * curvatureDerivativeAt(t);
auto n2 = p - normalAt(t, false) * curvatureDerivativeAt(t);
Expand Down
3 changes: 2 additions & 1 deletion example/qpolycurve.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

#include <QGraphicsItem>

#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<Bezier::Curve>& curve_list) : QGraphicsItem(), Bezier::PolyCurve(curve_list) {}
qPolyCurve(const Bezier::Curve& curve) : QGraphicsItem(), Bezier::PolyCurve(std::deque<Bezier::Curve>{curve}) {}
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 @@ -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_;
Expand Down

0 comments on commit b58675d

Please sign in to comment.