Commit 2d367a80 authored by Dmitry Kazakov's avatar Dmitry Kazakov
Browse files

Ad utility unittest

parent 4400f849
if (NOT WIN32 AND NOT APPLE)
if (NOT APPLE)
add_subdirectory(tests)
endif()
......
......@@ -9,6 +9,7 @@
#include <QtGlobal>
#include <kritatooltransform_export.h>
#include "kis_coordinates_converter.h"
#include <QTransform>
......@@ -28,7 +29,7 @@ class KisSavedMacroCommand;
class KisStrokeUndoFacade;
class KisStrokeJobData;
class KisTransformUtils
class KRITATOOLTRANSFORM_EXPORT KisTransformUtils
{
public:
......@@ -63,7 +64,7 @@ public:
static QPointF clipInRect(QPointF p, QRectF r);
struct MatricesPack
struct KRITATOOLTRANSFORM_EXPORT MatricesPack
{
MatricesPack(const ToolTransformArgs &args);
......
......@@ -9,6 +9,7 @@ macro_add_unittest_definitions()
########### next target ###############
ecm_add_test(test_animated_transform_parameters.cpp
ecm_add_tests(test_animated_transform_parameters.cpp
KisTransformUtilsTest.cpp
NAME_PREFIX plugins-tooltransform-
LINK_LIBRARIES kritatooltransform kritaui kritaimage Qt5::Test)
/*
* SPDX-FileCopyrightText: 2021 Dmitry Kazakov <dimula73@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "KisTransformUtilsTest.h"
#include <kis_transform_utils.h>
#include <tool_transform_args.h>
#include <Eigen/Dense>
#include <iostream>
#include <kis_algebra_2d.h>
namespace KisAlgebra2D {
inline Eigen::Matrix3d fromQTransformStraight(const QTransform &t)
{
Eigen::Matrix3d m;
m << t.m11() , t.m12() , t.m13()
,t.m21() , t.m22() , t.m23()
,t.m31() , t.m32() , t.m33();
return m;
}
}
void KisTransformUtilsTest::test()
{
ToolTransformArgs args(ToolTransformArgs::FREE_TRANSFORM,
QPointF(/*50, 50*/), QPointF(/*10,10*/),
QPointF(10,15), true,
0, 0, 2,
1.2, 1.4,
0., 0.,
KisWarpTransformWorker::AFFINE_TRANSFORM,
1.0, true, "", 16, 16, 0);
KisTransformUtils::MatricesPack m(args);
ENTER_FUNCTION() << ppVar(m.finalTransform());
const QPointF srcP1(20, 20);
const QPointF srcP2(30, 30);
const QPointF dstP1 = m.finalTransform().map(srcP1);
const QPointF dstP2 = m.finalTransform().map(srcP2);
ENTER_FUNCTION() << ppVar(srcP1) << ppVar(srcP2);
ENTER_FUNCTION() << ppVar(dstP1) << ppVar(dstP2);
QCOMPARE(m.finalTransform().map(srcP1), dstP1);
QCOMPARE(m.finalTransform().map(srcP2), dstP2);
const QPointF dstD2(100, 110);
Eigen::Matrix3d TS_t = KisAlgebra2D::fromQTransformStraight(m.TS.transposed());
Eigen::Matrix3d S_t = KisAlgebra2D::fromQTransformStraight(m.S.transposed());
Eigen::Matrix3d projP_t = KisAlgebra2D::fromQTransformStraight(m.projectedP.transposed());
Eigen::Matrix3d SC_t = KisAlgebra2D::fromQTransformStraight(m.SC.transposed());
Eigen::Matrix3d T_t = KisAlgebra2D::fromQTransformStraight(m.T.transposed());
Eigen::Matrix3d M1 = projP_t * S_t;
Eigen::Matrix3d M2 = TS_t;
std::cout << "M1: " << std::endl << M1 << std::endl;
std::cout << "M2: " << std::endl << M2 << std::endl;
Eigen::Matrix<double, 3, 2> P_src;
P_src << srcP1.x(), srcP2.x(),
srcP1.y(), srcP2.y(),
1, 1;
P_src = M2 * P_src;
Eigen::Matrix<double, 3, 2> P_dst;
P_dst << dstP1.x(), dstD2.x(),
dstP1.y(), dstD2.y(),
1, 1;
Eigen::Matrix<double, 4, 4> A;
A << M1(0,0) * P_src(0,0), M1(0,1) * P_src(1,0), 1, 0,
M1(1,0) * P_src(0,0), M1(1,1) * P_src(1,0), 0, 1,
M1(0,0) * P_src(0,1), M1(0,1) * P_src(1,1), 1, 0,
M1(1,0) * P_src(0,1), M1(1,1) * P_src(1,1), 0, 1;
Eigen::Matrix<double, 4, 1> B;
B << P_dst(0,0), P_dst(1,0), P_dst(0,1), P_dst(1,1);
Eigen::Matrix<double, 4, 1> X = A.inverse() * B;
std::cout << "X: " << std::endl << X << std::endl;
args.setScaleX(X(0));
args.setScaleY(X(1));
args.setTransformedCenter(QPointF(X(2), X(3)));
{
KisTransformUtils::MatricesPack m(args);
const QPointF dstP1 = m.finalTransform().map(srcP1);
const QPointF dstP2 = m.finalTransform().map(srcP2);
ENTER_FUNCTION() << ppVar(srcP1) << ppVar(srcP2);
ENTER_FUNCTION() << ppVar(dstP1) << ppVar(dstP2);
}
#if 0
Eigen::Matrix<double, 3, 2> P_dst;
P_dst << dstP1.x(), dstD2.x(),
dstP1.y(), dstD2.y(),
1, 1;
Eigen::Matrix<double, 3, 2> P_src;
P_src << srcP1.x(), srcP2.x(),
srcP1.y(), srcP2.y(),
1, 1;
P_src = M2 * P_src;
Eigen::Matrix<double, 3, 2> T_dst = T_t * M1 * SC_t * P_src;
std::cout << "T_dst: " << std::endl << T_dst << std::endl;
std::cout << "P_dst: " << std::endl << P_dst << std::endl;
std::cout << "P_src: " << std::endl << P_src << std::endl;
const qreal Ax = M1(0,1) - M1(0,0) * M1(2,1) / M1(2,0);
const qreal Ay = M1(1,1) - M1(1,0) * M1(2,1) / M1(2,0);
const qreal Bx = M1(0,0) - M1(0,1) * M1(2,0) / M1(2,1);
const qreal By = M1(1,0) - M1(1,1) * M1(2,0) / M1(2,1);
ENTER_FUNCTION() << ppVar(Ax) << ppVar(Ay) << ppVar(Bx) << ppVar(By);
qDebug() << ppVar(M1(2,0) * srcP1.x() * args.scaleX());
qDebug() << ppVar(M1(2,1) * srcP1.y() * args.scaleY());
qDebug() << ppVar(srcP1.x() * args.scaleX() * Bx + args.transformedCenter().x());
qDebug() << ppVar(srcP1.x() * args.scaleX() * By + args.transformedCenter().y());
qDebug() << ppVar(srcP1.y() * args.scaleY() * Ax + args.transformedCenter().x());
qDebug() << ppVar(srcP1.y() * args.scaleY() * Ay + args.transformedCenter().y());
Eigen::Matrix<double, 4, 4> A;
// A << Bx * P_src(0,0), 0, 1, 0,
// 0, Ay * P_src(1,0), 0, 1,
// Bx * P_src(0,1), 0, 1, 0,
// 0, Ay * P_src(1,1), 0, 1;
A << 0, Ax * P_src(1,0), 1, 0,
By * P_src(0,0), 0, 0, 1,
0, Ax * P_src(1,1), 1, 0,
By * P_src(0,1), 0, 0, 1;
Eigen::FullPivLU<Eigen::Matrix4d> decomp(A);
qDebug() << ppVar(decomp.rank());
Eigen::Matrix<double, 4, 1> B;
B << P_dst(0,0), P_dst(1,0), P_dst(0,1), P_dst(1,1);
std::cout << "A: " << std::endl << A << std::endl;
std::cout << "B: " << std::endl << B << std::endl;
Eigen::Matrix<double, 4, 1> Xt;
Xt << args.scaleX(), args.scaleY(),
args.transformedCenter().x(), args.transformedCenter().y();
Eigen::Matrix<double, 4, 1> Bt = A * Xt;
std::cout << "Bt: " << std::endl << Bt << std::endl;
Eigen::Matrix<double, 4, 1> X = A.inverse() * B;
std::cout << "X: " << std::endl << X << std::endl;
args.setScaleX(X(0));
args.setScaleY(X(1));
args.setTransformedCenter(QPointF(X(2), X(3)));
{
KisTransformUtils::MatricesPack m(args);
const QPointF dstP1 = m.finalTransform().map(srcP1);
const QPointF dstP2 = m.finalTransform().map(srcP2);
ENTER_FUNCTION() << ppVar(srcP1) << ppVar(srcP2);
ENTER_FUNCTION() << ppVar(dstP1) << ppVar(dstP2);
}
#endif
#if 0
Eigen::Matrix<double, 4, 4> A;
A << P_src(0,0), 0, 1, 0,
0, P_src(1,0), 0, 1,
P_src(0,1), 0, 1, 0,
0, P_src(1,1), 0, 1;
std::cout << "A: " << A << std::endl;
std::cout << "B: " << B << std::endl;
Eigen::Matrix<double, 4, 1> X = A.inverse() * B;
std::cout << "X: " << X << std::endl;
{
ToolTransformArgs args(ToolTransformArgs::FREE_TRANSFORM,
QPointF(X(2), X(3)), QPointF(10,10),
QPointF(10,15), true,
0, 0, 0,
X(0), X(1),
0.5, 0,
KisWarpTransformWorker::AFFINE_TRANSFORM,
1.0, true, "", 16, 16, 0);
KisTransformUtils::MatricesPack m(args);
const QPointF dstP1 = m.finalTransform().map(srcP1);
const QPointF dstP2 = m.finalTransform().map(srcP2);
ENTER_FUNCTION() << ppVar(srcP1) << ppVar(srcP2);
ENTER_FUNCTION() << ppVar(dstP1) << ppVar(dstP2);
}
#endif
}
SIMPLE_TEST_MAIN(KisTransformUtilsTest)
/*
* SPDX-FileCopyrightText: 2021 Dmitry Kazakov <dimula73@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef KISTRANSFORMUTILSTEST_H
#define KISTRANSFORMUTILSTEST_H
#include <simpletest.h>
class KisTransformUtilsTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void test();
};
#endif // KISTRANSFORMUTILSTEST_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment