Flipping VPointF.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2016-09-10 16:40:38 +03:00
parent 868fc3db1d
commit 4c47c33c5e
8 changed files with 189 additions and 2 deletions

View File

@ -33,6 +33,7 @@
#include <QPoint>
#include <QPointF>
#include <QRectF>
#include <QTransform>
#include "../vmisc/def.h"
#include "../vmisc/vmath.h"
@ -575,3 +576,39 @@ int VGObject::GetLengthContour(const QVector<QPointF> &contour, const QVector<QP
}
return qFloor(length);
}
//---------------------------------------------------------------------------------------------------------------------
QTransform VGObject::FlippingMatrix(const QLineF &axis)
{
QTransform matrix;
if (axis.isNull())
{
return matrix;
}
const QLineF axisOX = QLineF(axis.x2(), axis.y2(), axis.x2() + 100, axis.y2()); // Ox axis
const qreal angle = axis.angleTo(axisOX);
const QPointF p2 = axis.p2();
QTransform m;
m.translate(p2.x(), p2.y());
m.rotate(-angle);
m.translate(-p2.x(), -p2.y());
matrix *= m;
m.reset();
m.translate(p2.x(), p2.y());
m.scale(m.m11(), m.m22()*-1);
m.translate(-p2.x(), -p2.y());
matrix *= m;
m.reset();
m.translate(p2.x(), p2.y());
m.rotate(-(360-angle));
m.translate(-p2.x(), -p2.y());
matrix *= m;
return matrix;
}

View File

@ -42,6 +42,7 @@ class QPoint;
class QPointF;
class QRectF;
class VGObjectData;
class QTransform;
/**
* @brief The VGObject class keep information graphical objects.
@ -93,6 +94,8 @@ public:
static int GetLengthContour(const QVector<QPointF> &contour, const QVector<QPointF> &newPoints);
static double accuracyPointOnLine;
protected:
static QTransform FlippingMatrix(const QLineF &axis);
private:
QSharedDataPointer<VGObjectData> d;

View File

@ -31,6 +31,7 @@
#include <QLineF>
#include <QPointF>
#include <QString>
#include <QTransform>
//---------------------------------------------------------------------------------------------------------------------
/**
@ -113,6 +114,14 @@ VPointF VPointF::Rotate(const QPointF &originPoint, qreal degrees, const QString
return VPointF(p, name() + prefix, mx(), my());
}
//---------------------------------------------------------------------------------------------------------------------
VPointF VPointF::Flip(const QLineF &axis, const QString &prefix) const
{
const QTransform matrix = FlippingMatrix(axis);
const QPointF p = matrix.map(toQPointF());
return VPointF(p, name() + prefix, mx(), my());
}
//---------------------------------------------------------------------------------------------------------------------
/**
* @brief mx return offset name respect to x

View File

@ -65,6 +65,7 @@ public:
VPointF &operator=(const VPointF &point);
operator QPointF() const;
VPointF Rotate(const QPointF &originPoint, qreal degrees, const QString &prefix = QString()) const;
VPointF Flip(const QLineF &axis, const QString &prefix = QString()) const;
qreal mx() const;
qreal my() const;
void setMx(qreal mx);

View File

@ -52,7 +52,8 @@ SOURCES += \
tst_vellipticalarc.cpp \
tst_vcubicbezierpath.cpp \
tst_vgobject.cpp \
tst_vsplinepath.cpp
tst_vsplinepath.cpp \
tst_vpointf.cpp
win32-msvc*:SOURCES += stable.cpp
@ -75,7 +76,8 @@ HEADERS += \
tst_vellipticalarc.h \
tst_vcubicbezierpath.h \
tst_vgobject.h \
tst_vsplinepath.h
tst_vsplinepath.h \
tst_vpointf.h
# Set using ccache. Function enable_ccache() defined in common.pri.
$$enable_ccache()

View File

@ -46,6 +46,7 @@
#include "tst_vcubicbezierpath.h"
#include "tst_vgobject.h"
#include "tst_vsplinepath.h"
#include "tst_vpointf.h"
#include "../vmisc/def.h"
@ -80,6 +81,7 @@ int main(int argc, char** argv)
ASSERT_TEST(new TST_VAbstractCurve());
ASSERT_TEST(new TST_VCubicBezierPath());
ASSERT_TEST(new TST_VGObject());
ASSERT_TEST(new TST_VPointF());
return status;
}

View File

@ -0,0 +1,86 @@
/************************************************************************
**
** @file
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 10 9, 2016
**
** @brief
** @copyright
** This source code is part of the Valentine project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2016 Valentina project
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "tst_vpointf.h"
#include "../vgeometry/vpointf.h"
#include "../vmisc/logging.h"
#include <QtTest>
//---------------------------------------------------------------------------------------------------------------------
TST_VPointF::TST_VPointF(QObject *parent)
: QObject(parent)
{
}
//---------------------------------------------------------------------------------------------------------------------
void TST_VPointF::TestFlip_data()
{
QTest::addColumn<VPointF>("originPoint");
QTest::addColumn<QLineF>("axis");
QTest::addColumn<QPointF>("flipped");
QTest::addColumn<QString>("prefix");
VPointF originPoint;
QLineF axis = QLineF(QPointF(5, 0), QPointF(5, 10));
QPointF flipped = QPointF(10, 0);
QTest::newRow("Vertical axis") << originPoint << axis << flipped << "a2";
axis = QLineF(QPointF(0, 5), QPointF(10, 5));
flipped = QPointF(0, 10);
QTest::newRow("Horizontal axis") << originPoint << axis << flipped << "a2";
QLineF l = QLineF(QPointF(), QPointF(10, 0));
l.setAngle(315);
flipped = l.p2();
l.setLength(l.length()/2.0);
axis = QLineF(l.p2(), l.p1());
axis.setAngle(axis.angle()+90);
QTest::newRow("Diagonal axis") << originPoint << axis << flipped << "a2";
}
//---------------------------------------------------------------------------------------------------------------------
void TST_VPointF::TestFlip()
{
QFETCH(VPointF, originPoint);
QFETCH(QLineF, axis);
QFETCH(QPointF, flipped);
QFETCH(QString, prefix);
const VPointF res = originPoint.Flip(axis, prefix);
const QString errorMsg = QString("The name doesn't contain the prefix '%1'.").arg(prefix);
QVERIFY2(res.name().endsWith(prefix), qUtf8Printable(errorMsg));
QCOMPARE(flipped.toPoint(), res.toQPointF().toPoint());
}

View File

@ -0,0 +1,47 @@
/************************************************************************
**
** @file
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 10 9, 2016
**
** @brief
** @copyright
** This source code is part of the Valentine project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2016 Valentina project
** <https://bitbucket.org/dismine/valentina> All Rights Reserved.
**
** Valentina is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Valentina is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#ifndef TST_VPOINTF_H
#define TST_VPOINTF_H
#include <QtCore/QObject>
#include <QtCore/qglobal.h>
class TST_VPointF : public QObject
{
Q_OBJECT
public:
explicit TST_VPointF(QObject *parent = nullptr);
private slots:
void TestFlip_data();
void TestFlip();
private:
Q_DISABLE_COPY(TST_VPointF)
};
#endif // TST_VPOINTF_H