chalk

kis_perspective_math.h
1/*
2 * This file is part of Chalk
3 *
4 * Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#ifndef _KIS_PERSPECTVE_MATH_H_
21#define _KIS_PERSPECTVE_MATH_H_
22
23#include "kis_point.h"
24
25class TQRect;
26
27class KisPerspectiveMath {
28 private:
29 KisPerspectiveMath() { }
30 public:
31 static double* computeMatrixTransfo( const KisPoint& topLeft1, const KisPoint& topRight1, const KisPoint& bottomLeft1, const KisPoint& bottomRight1 , const KisPoint& topLeft2, const KisPoint& topRight2, const KisPoint& bottomLeft2, const KisPoint& bottomRight2);
32 static double* computeMatrixTransfoToPerspective(const KisPoint& topLeft, const KisPoint& topRight, const KisPoint& bottomLeft, const KisPoint& bottomRight, const TQRect& r);
33 static double* computeMatrixTransfoFromPerspective(const TQRect& r, const KisPoint& topLeft, const KisPoint& topRight, const KisPoint& bottomLeft, const KisPoint& bottomRight);
34 struct LineEquation {
35 // y = a*x + b
36 double a, b;
37 };
39 inline static KisPoint matProd(const double (&m)[3][3], const KisPoint& p)
40 {
41 double s = ( p.x() * m[2][0] + p.y() * m[2][1] + 1.0);
42 s = (s == 0.) ? 1. : 1./s;
43 return KisPoint( (p.x() * m[0][0] + p.y() * m[0][1] + m[0][2] ) * s,
44 (p.x() * m[1][0] + p.y() * m[1][1] + m[1][2] ) * s );
45 }
46 static inline LineEquation computeLineEquation(const KisPoint* p1, const KisPoint* p2)
47 {
48 LineEquation eq;
49 double x1 = p1->x(); double x2 = p2->x();
50 if( fabs(x1 - x2) < 0.000001 )
51 {
52 x1 += 0.0001; // Introduce a small perturbation
53 }
54 eq.a = (p2->y() - p1->y()) / (double)( x2 - x1 );
55 eq.b = -eq.a * x1 + p1->y();
56 return eq;
57 }
58 static inline KisPoint computeIntersection(const LineEquation& d1, const LineEquation& d2)
59 {
60 double a1 = d1.a; double a2 = d2.a;
61 if( fabs(a1 - a2) < 0.000001 )
62 {
63 a1 += 0.0001; // Introduce a small perturbation
64 }
65 double x = (d1.b - d2.b) / (a2 - a1);
66 return KisPoint(x, a2 * x + d2.b);
67 }
68};
69
70#endif
A double-based point class that can return it's coordinates approximated to integers.
Definition kis_point.h:28