krita

kis_perspective_grid.cpp

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; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "kis_perspective_grid.h"
00022 
00023 int KisSubPerspectiveGrid::s_lastIndex = 0;
00024 
00025 KisSubPerspectiveGrid::KisSubPerspectiveGrid(KisPerspectiveGridNodeSP topLeft, KisPerspectiveGridNodeSP topRight, KisPerspectiveGridNodeSP bottomRight, KisPerspectiveGridNodeSP bottomLeft) : m_topLeft(topLeft), m_topRight(topRight), m_bottomRight(bottomRight), m_bottomLeft(bottomLeft), m_subdivisions(5), m_leftGrid(0), m_rightGrid(0), m_topGrid(0), m_bottomGrid(0), m_index(++s_lastIndex)
00026 {
00027     
00028 }
00029 
00030 bool KisSubPerspectiveGrid::contains(const KisPoint p) const
00031 {
00032     return true;
00033     KisPerspectiveMath::LineEquation d1 = KisPerspectiveMath::computeLineEquation( topLeft(), topRight() );
00034     kdDebug() << p.y() << " " << (p.x() * d1.a + d1.b) << endl;
00035     if( p.y() >= p.x() * d1.a + d1.b)
00036     {
00037         d1 = KisPerspectiveMath::computeLineEquation( topRight(), bottomRight() );
00038         kdDebug() << p.y() << " " << (p.x() * d1.a + d1.b) << endl;
00039         if( p.y() >= p.x() * d1.a + d1.b)
00040         {
00041             d1 = KisPerspectiveMath::computeLineEquation( bottomRight(), bottomLeft() );
00042             kdDebug() << p.y() << " " << (p.x() * d1.a + d1.b) << endl;
00043             if( p.y() <= p.x() * d1.a + d1.b)
00044             {
00045                 d1 = KisPerspectiveMath::computeLineEquation( bottomLeft(), topLeft() );
00046                 kdDebug() << p.y() << " " << (p.x() * d1.a + d1.b) << endl;
00047                 if( p.y() <= p.x() * d1.a + d1.b)
00048                 {
00049                     return true;
00050                 }
00051             }
00052         }
00053     }
00054     return false;
00055 }
00056 
00057 
00058 KisPerspectiveGrid::KisPerspectiveGrid()
00059 {
00060 }
00061 
00062 
00063 KisPerspectiveGrid::~KisPerspectiveGrid()
00064 {
00065     clearSubGrids( );
00066 }
00067 
00068 bool KisPerspectiveGrid::addNewSubGrid( KisSubPerspectiveGrid* ng )
00069 {
00070     if(hasSubGrids() && !ng->topGrid() && !ng->bottomGrid() && !ng->leftGrid() && !ng->rightGrid() )
00071     {
00072         kdError() << "sub grids need a neighbourgh if they are not the first grid to be added" << endl;
00073         return false;
00074     }
00075     m_subGrids.push_back(ng);
00076     return true;
00077 }
00078 
00079 
00080 void KisPerspectiveGrid::clearSubGrids( )
00081 {
00082     for( QValueList<KisSubPerspectiveGrid*>::const_iterator it = begin(); it != end(); ++it)
00083     {
00084         delete *it;
00085     }
00086     m_subGrids.clear();
00087 }
00088 
00089 KisSubPerspectiveGrid* KisPerspectiveGrid::gridAt(KisPoint p)
00090 {
00091     for( QValueList<KisSubPerspectiveGrid*>::const_iterator it = begin(); it != end(); ++it)
00092     {
00093         if( (*it)->contains(p) )
00094         {
00095             return *it;
00096         }
00097     }
00098     return 0;
00099 }
00100 
KDE Home | KDE Accessibility Home | Description of Access Keys