krita

kis_perspective_math.h

00001 /*
00002  * This file is part of Krita
00003  *
00004  *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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             // y = a*x + b
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; // Introduce a small perturbation
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; // Introduce a small perturbation
00064       }
00065       double x = (d1.b - d2.b) / (a2 - a1);
00066       return KisPoint(x, a2 * x + d2.b);
00067     }
00068 };
00069 
00070 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys