Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_perspective_math.cpp
Go to the documentation of this file.
1/*
2 * This file is part of Krita
3 *
4 * SPDX-FileCopyrightText: 2006 Cyrille Berger <cberger@cberger.net>
5 * SPDX-FileCopyrightText: 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
11
12#include <QRect>
13
14#include <Eigen/LU>
15
16Matrix3qreal KisPerspectiveMath::computeMatrixTransfo(const QPointF& topLeft1, const QPointF& topRight1, const QPointF& bottomLeft1, const QPointF& bottomRight1 , const QPointF& topLeft2, const QPointF& topRight2, const QPointF& bottomLeft2, const QPointF& bottomRight2)
17{
18 Matrix9qreal a = Matrix9qreal::Zero();
19 Vector9qreal b = Vector9qreal::Zero();
20
21 // topLeft
22 a.coeffRef(0, 0) = topLeft1.x();
23 a.coeffRef(0, 1) = topLeft1.y();
24 a.coeffRef(0, 2) = 1;
25 a.coeffRef(0, 6) = -topLeft2.x() * topLeft1.x();
26 a.coeffRef(0, 7) = -topLeft2.x() * topLeft1.y();
27 a.coeffRef(0, 8) = -topLeft2.x();
28 a.coeffRef(1, 3) = topLeft1.x();
29 a.coeffRef(1, 4) = topLeft1.y();
30 a.coeffRef(1, 5) = 1;
31 a.coeffRef(1, 6) = -topLeft2.y() * topLeft1.x();
32 a.coeffRef(1, 7) = -topLeft2.y() * topLeft1.y();
33 a.coeffRef(1, 8) = -topLeft2.y();
34 // topRight
35 a.coeffRef(2, 0) = topRight1.x();
36 a.coeffRef(2, 1) = topRight1.y();
37 a.coeffRef(2, 2) = 1;
38 a.coeffRef(2, 6) = -topRight2.x() * topRight1.x();
39 a.coeffRef(2, 7) = -topRight2.x() * topRight1.y();
40 a.coeffRef(2, 8) = -topRight2.x();
41 a.coeffRef(3, 3) = topRight1.x();
42 a.coeffRef(3, 4) = topRight1.y();
43 a.coeffRef(3, 5) = 1;
44 a.coeffRef(3, 6) = -topRight2.y() * topRight1.x();
45 a.coeffRef(3, 7) = -topRight2.y() * topRight1.y();
46 a.coeffRef(3, 8) = -topRight2.y();
47 // bottomLeft
48 a.coeffRef(4, 0) = bottomLeft1.x();
49 a.coeffRef(4, 1) = bottomLeft1.y();
50 a.coeffRef(4, 2) = 1;
51 a.coeffRef(4, 6) = -bottomLeft2.x() * bottomLeft1.x();
52 a.coeffRef(4, 7) = -bottomLeft2.x() * bottomLeft1.y();
53 a.coeffRef(4, 8) = -bottomLeft2.x();
54 a.coeffRef(5, 3) = bottomLeft1.x();
55 a.coeffRef(5, 4) = bottomLeft1.y();
56 a.coeffRef(5, 5) = 1;
57 a.coeffRef(5, 6) = -bottomLeft2.y() * bottomLeft1.x();
58 a.coeffRef(5, 7) = -bottomLeft2.y() * bottomLeft1.y();
59 a.coeffRef(5, 8) = -bottomLeft2.y();
60 // bottomRight
61 a.coeffRef(6, 0) = bottomRight1.x();
62 a.coeffRef(6, 1) = bottomRight1.y();
63 a.coeffRef(6, 2) = 1;
64 a.coeffRef(6, 6) = -bottomRight2.x() * bottomRight1.x();
65 a.coeffRef(6, 7) = -bottomRight2.x() * bottomRight1.y();
66 a.coeffRef(6, 8) = -bottomRight2.x();
67 a.coeffRef(7, 3) = bottomRight1.x();
68 a.coeffRef(7, 4) = bottomRight1.y();
69 a.coeffRef(7, 5) = 1;
70 a.coeffRef(7, 6) = -bottomRight2.y() * bottomRight1.x();
71 a.coeffRef(7, 7) = -bottomRight2.y() * bottomRight1.y();
72 a.coeffRef(7, 8) = -bottomRight2.y();
73 a.coeffRef(8, 8) = 1;
74 b.coeffRef(8) = 1;
75// dbgImage <<" a := { {" << a(0,0) <<" ," << a(0,1) <<" ," << a(0,2) <<" ," << a(0,3) <<" ," << a(0,4) <<" ," << a(0,5) <<" ," << a(0,6) <<" ," << a(0,7) <<" ," << a(0,8) <<" } , {" << a(1,0) <<" ," << a(1,1) <<" ," << a(1,2) <<" ," << a(1,3) <<" ," << a(1,4) <<" ," << a(1,5) <<" ," << a(1,6) <<" ," << a(1,7) <<" ," << a(1,8) <<" } , {" << a(2,0) <<" ," << a(2,1) <<" ," << a(2,2) <<" ," << a(2,3) <<" ," << a(2,4) <<" ," << a(2,5) <<" ," << a(2,6) <<" ," << a(2,7) <<" ," << a(2,8) <<" } , {" << a(3,0) <<" ," << a(3,1) <<" ," << a(3,2) <<" ," << a(3,3) <<" ," << a(3,4) <<" ," << a(3,5) <<" ," << a(3,6) <<" ," << a(3,7) <<" ," << a(3,8) <<" } , {" << a(4,0) <<" ," << a(4,1) <<" ," << a(4,2) <<" ," << a(4,3) <<" ," << a(4,4) <<" ," << a(4,5) <<" ," << a(4,6) <<" ," << a(4,7) <<" ," << a(4,8) <<" } , {" << a(5,0) <<" ," << a(5,1) <<" ," << a(5,2) <<" ," << a(5,3) <<" ," << a(5,4) <<" ," << a(5,5) <<" ," << a(5,6) <<" ," << a(5,7) <<" ," << a(5,8) <<" } , {" << a(6,0) <<" ," << a(6,1) <<" ," << a(6,2) <<" ," << a(6,3) <<" ," << a(6,4) <<" ," << a(6,5) <<" ," << a(6,6) <<" ," << a(6,7) <<" ," << a(6,8) <<" } , {"<< a(7,0) <<" ," << a(7,1) <<" ," << a(7,2) <<" ," << a(7,3) <<" ," << a(7,4) <<" ," << a(7,5) <<" ," << a(7,6) <<" ," << a(7,7) <<" ," << a(7,8) <<" } , {"<< a(8,0) <<" ," << a(8,1) <<" ," << a(8,2) <<" ," << a(8,3) <<" ," << a(8,4) <<" ," << a(8,5) <<" ," << a(8,6) <<" ," << a(8,7) <<" ," << a(8,8) <<" } };";
77 v = a.lu().solve(b);
78 Matrix3qreal matrix;
79 for (int r = 0; r < 3; r++) for (int c = 0; c < 3; c++) matrix.coeffRef(r, c) = v.coeff(3 * r + c);
80 return matrix;
81}
82
83Matrix3qreal KisPerspectiveMath::computeMatrixTransfoToPerspective(const QPointF& topLeft, const QPointF& topRight, const QPointF& bottomLeft, const QPointF& bottomRight, const QRect& r)
84{
85 return KisPerspectiveMath::computeMatrixTransfo(topLeft, topRight, bottomLeft, bottomRight, r.topLeft(), r.topRight(), r.bottomLeft(), r.bottomRight());
86}
87
88Matrix3qreal KisPerspectiveMath::computeMatrixTransfoFromPerspective(const QRect& r, const QPointF& topLeft, const QPointF& topRight, const QPointF& bottomLeft, const QPointF& bottomRight)
89{
90 return KisPerspectiveMath::computeMatrixTransfo(r.topLeft(), r.topRight(), r.bottomLeft(), r.bottomRight(), topLeft, topRight, bottomLeft, bottomRight);
91}
qreal v
static Matrix3qreal computeMatrixTransfoToPerspective(const QPointF &topLeft, const QPointF &topRight, const QPointF &bottomLeft, const QPointF &bottomRight, const QRect &r)
static Matrix3qreal computeMatrixTransfoFromPerspective(const QRect &r, const QPointF &topLeft, const QPointF &topRight, const QPointF &bottomLeft, const QPointF &bottomRight)
static Matrix3qreal computeMatrixTransfo(const QPointF &topLeft1, const QPointF &topRight1, const QPointF &bottomLeft1, const QPointF &bottomRight1, const QPointF &topLeft2, const QPointF &topRight2, const QPointF &bottomLeft2, const QPointF &bottomRight2)
Eigen::Matrix< qreal, 9, 9 > Matrix9qreal
Eigen::Matrix< qreal, 3, 3 > Matrix3qreal
Eigen::Matrix< qreal, 9, 1 > Vector9qreal