Commit 46b755c4 authored by Dmitry Kazakov's avatar Dmitry Kazakov

Implemented transformAsBase() function for KisAlgebra2D

parent 000e69ee
......@@ -55,4 +55,26 @@ void KRITAIMAGE_EXPORT adjustIfOnPolygonBoundary(const QPolygonF &poly, int poly
}
}
QPointF KRITAIMAGE_EXPORT transformAsBase(const QPointF &pt, const QPointF &base1, const QPointF &base2) {
qreal len1 = norm(base1);
if (len1 < 1e-5) return pt;
qreal sin1 = base1.y() / len1;
qreal cos1 = base1.x() / len1;
qreal len2 = norm(base2);
if (len2 < 1e-5) return QPointF();
qreal sin2 = base2.y() / len2;
qreal cos2 = base2.x() / len2;
qreal sinD = sin2 * cos1 - cos2 * sin1;
qreal cosD = cos1 * cos2 + sin1 * sin2;
qreal scaleD = len2 / len1;
QPointF result;
result.rx() = scaleD * (pt.x() * cosD - pt.y() * sinD);
result.ry() = scaleD * (pt.x() * sinD + pt.y() * cosD);
return result;
}
}
......@@ -121,6 +121,14 @@ bool isInRange(T x, T a, T b) {
void KRITAIMAGE_EXPORT adjustIfOnPolygonBoundary(const QPolygonF &poly, int polygonDirection, QPointF *pt);
/**
* Let \p pt, \p base1 are two vectors. \p base1 is uniformly scaled
* and then rotated into \p base2 using transformation matrix S *
* R. The function applies the same transformation to \pt and returns
* the result.
**/
QPointF transformAsBase(const QPointF &pt, const QPointF &base1, const QPointF &base2);
}
#endif /* __KIS_ALGEBRA_2D_H */
......@@ -203,5 +203,52 @@ void KisCageTransformWorkerTest::testUnityGreenCoordinates()
}
}
#include "kis_algebra_2d.h"
void KisCageTransformWorkerTest::testTransformAsBase()
{
QPointF t(1.0, 0.0);
QPointF b1(1.0, 0.0);
QPointF b2(2.0, 0.0);
QPointF result;
t = QPointF(1.0, 0.0);
b1 = QPointF(1.0, 0.0);
b2 = QPointF(2.0, 0.0);
result = KisAlgebra2D::transformAsBase(t, b1, b2);
QCOMPARE(result, QPointF(2.0, 0.0));
t = QPointF(1.0, 0.0);
b1 = QPointF(1.0, 0.0);
b2 = QPointF(0.0, 1.0);
result = KisAlgebra2D::transformAsBase(t, b1, b2);
QCOMPARE(result, QPointF(0.0, 1.0));
t = QPointF(1.0, 0.0);
b1 = QPointF(1.0, 0.0);
b2 = QPointF(0.0, 2.0);
result = KisAlgebra2D::transformAsBase(t, b1, b2);
QCOMPARE(result, QPointF(0.0, 2.0));
t = QPointF(0.0, 1.0);
b1 = QPointF(1.0, 0.0);
b2 = QPointF(2.0, 0.0);
result = KisAlgebra2D::transformAsBase(t, b1, b2);
QCOMPARE(result, QPointF(0.0, 2.0));
t = QPointF(0.0, 1.0);
b1 = QPointF(1.0, 0.0);
b2 = QPointF(0.0, 1.0);
result = KisAlgebra2D::transformAsBase(t, b1, b2);
QCOMPARE(result, QPointF(-1.0, 0.0));
t = QPointF(0.0, 1.0);
b1 = QPointF(1.0, 0.0);
b2 = QPointF(0.0, 2.0);
result = KisAlgebra2D::transformAsBase(t, b1, b2);
QCOMPARE(result, QPointF(-2.0, 0.0));
}
QTEST_KDEMAIN(KisCageTransformWorkerTest, GUI)
......@@ -34,6 +34,8 @@ private slots:
void testCageCounterclockwiseUnity();
void testUnityGreenCoordinates();
void testTransformAsBase();
};
#endif /* __KIS_CAGE_TRANSFORM_WORKER_TEST_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