File indexing completed on 2024-06-23 04:25:56

0001 /*
0002  *  SPDX-FileCopyrightText: 2022 Agata Cacko <cacko.azh@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #include "TestPerspectiveBasedAssistantHelper.h"
0008 
0009 #include <testui.h>
0010 #include <testutil.h>
0011 
0012 
0013 #include <kis_painting_assistant.h>
0014 #include <PerspectiveBasedAssistantHelper.h>
0015 #include <kis_algebra_2d.h>
0016 #include <kis_global.h>
0017 
0018 
0019 
0020 void TestPerspectiveBasedAssistantHelper::testDistanceInGrid()
0021 {
0022     QList<KisPaintingAssistantHandleSP> handles = getHandles({QPointF(-4, 4), QPointF(4, 4), QPointF(8, 8), QPointF(-8, 8)});
0023     QList<QPointF> pointsToCheck;
0024     for (int i = 0; i <= 8; i++) {
0025         pointsToCheck << QPointF(0, i);
0026     }
0027 
0028     QPolygonF poly;
0029     bool correct = PerspectiveBasedAssistantHelper::getTetragon(handles, true, poly);
0030     ENTER_FUNCTION() << correct;
0031 
0032     PerspectiveBasedAssistantHelper::CacheData cache;
0033     PerspectiveBasedAssistantHelper::updateCacheData(cache, poly);
0034 
0035     auto fuzzyCompare = [] (double a, double b) {
0036         return qAbs(a - b) < 0.0000001;
0037     };
0038 
0039 
0040     for (int i = 0; i < pointsToCheck.size(); i++) {
0041         qreal response = PerspectiveBasedAssistantHelper::distanceInGrid(cache, pointsToCheck[i]);
0042         QVERIFY(fuzzyCompare(response, i/8.0));
0043     }
0044 
0045     // let's say: vps in (-10, 0) and (10, 0)
0046     // small point:
0047     ENTER_FUNCTION() << ppVar(6.0/19) << ppVar(4.0/19);
0048     QLineF first = QLineF(QPointF(10, 0), QPointF(-10, 8));
0049     QLineF second = QLineF(QPointF(-10, 0), QPointF(0, 13));
0050     QPointF inters;
0051     if (first.intersect(second, &inters) != QLineF::NoIntersection) {
0052         ENTER_FUNCTION() << "Intersection is: " << inters << "and was supposed to be: " << QPointF(-5 - 15.0/19, 6 + 6.0/19);
0053     }
0054 
0055 
0056     handles = getHandles({QPointF(0, 4), inters, QPointF(0, 13), QPointF(-inters.x(), inters.y())});
0057     pointsToCheck.clear();
0058     for (int i = 0; i <= 13; i++) {
0059         pointsToCheck << QPointF(0, i);
0060     }
0061 
0062     correct = PerspectiveBasedAssistantHelper::getTetragon(handles, true, poly);
0063     ENTER_FUNCTION() << correct;
0064 
0065     PerspectiveBasedAssistantHelper::updateCacheData(cache, poly);
0066 
0067     for (int i = 0; i < pointsToCheck.size(); i++) {
0068         qreal response = PerspectiveBasedAssistantHelper::distanceInGrid(cache, pointsToCheck[i]);
0069         QVERIFY(fuzzyCompare(response, i/13.0));
0070 
0071     }
0072 
0073 }
0074 
0075 QList<KisPaintingAssistantHandleSP> TestPerspectiveBasedAssistantHelper::getHandles(QList<QPointF> points)
0076 {
0077     QList<KisPaintingAssistantHandleSP> handles;
0078     for(int i = 0; i < points.size(); i++) {
0079         handles << KisPaintingAssistantHandleSP(new KisPaintingAssistantHandle(points[i]));
0080     }
0081     return handles;
0082 }
0083 
0084 KISTEST_MAIN(TestPerspectiveBasedAssistantHelper)