krita
kis_perspective_math.h00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _KIS_PERSPECTVE_MATH_H_
00021 #define _KIS_PERSPECTVE_MATH_H_
00022
00023 #include "kis_point.h"
00024
00025 class QRect;
00026
00027 class KisPerspectiveMath {
00028 private:
00029 KisPerspectiveMath() { }
00030 public:
00031 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);
00032 static double* computeMatrixTransfoToPerspective(const KisPoint& topLeft, const KisPoint& topRight, const KisPoint& bottomLeft, const KisPoint& bottomRight, const QRect& r);
00033 static double* computeMatrixTransfoFromPerspective(const QRect& r, const KisPoint& topLeft, const KisPoint& topRight, const KisPoint& bottomLeft, const KisPoint& bottomRight);
00034 struct LineEquation {
00035
00036 double a, b;
00037 };
00039 inline static KisPoint matProd(const double (&m)[3][3], const KisPoint& p)
00040 {
00041 double s = ( p.x() * m[2][0] + p.y() * m[2][1] + 1.0);
00042 s = (s == 0.) ? 1. : 1./s;
00043 return KisPoint( (p.x() * m[0][0] + p.y() * m[0][1] + m[0][2] ) * s,
00044 (p.x() * m[1][0] + p.y() * m[1][1] + m[1][2] ) * s );
00045 }
00046 static inline LineEquation computeLineEquation(const KisPoint* p1, const KisPoint* p2)
00047 {
00048 LineEquation eq;
00049 double x1 = p1->x(); double x2 = p2->x();
00050 if( fabs(x1 - x2) < 0.000001 )
00051 {
00052 x1 += 0.0001;
00053 }
00054 eq.a = (p2->y() - p1->y()) / (double)( x2 - x1 );
00055 eq.b = -eq.a * x1 + p1->y();
00056 return eq;
00057 }
00058 static inline KisPoint computeIntersection(const LineEquation& d1, const LineEquation& d2)
00059 {
00060 double a1 = d1.a; double a2 = d2.a;
00061 if( fabs(a1 - a2) < 0.000001 )
00062 {
00063 a1 += 0.0001;
00064 }
00065 double x = (d1.b - d2.b) / (a2 - a1);
00066 return KisPoint(x, a2 * x + d2.b);
00067 }
00068 };
00069
00070 #endif
|