chalk

kis_perspective_grid.cpp
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; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include "kis_perspective_grid.h"
22
23int KisSubPerspectiveGrid::s_lastIndex = 0;
24
25KisSubPerspectiveGrid::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)
26{
27
28}
29
30bool KisSubPerspectiveGrid::contains(const KisPoint p) const
31{
32 return true;
33 KisPerspectiveMath::LineEquation d1 = KisPerspectiveMath::computeLineEquation( topLeft(), topRight() );
34 kdDebug() << p.y() << " " << (p.x() * d1.a + d1.b) << endl;
35 if( p.y() >= p.x() * d1.a + d1.b)
36 {
37 d1 = KisPerspectiveMath::computeLineEquation( topRight(), bottomRight() );
38 kdDebug() << p.y() << " " << (p.x() * d1.a + d1.b) << endl;
39 if( p.y() >= p.x() * d1.a + d1.b)
40 {
41 d1 = KisPerspectiveMath::computeLineEquation( bottomRight(), bottomLeft() );
42 kdDebug() << p.y() << " " << (p.x() * d1.a + d1.b) << endl;
43 if( p.y() <= p.x() * d1.a + d1.b)
44 {
45 d1 = KisPerspectiveMath::computeLineEquation( bottomLeft(), topLeft() );
46 kdDebug() << p.y() << " " << (p.x() * d1.a + d1.b) << endl;
47 if( p.y() <= p.x() * d1.a + d1.b)
48 {
49 return true;
50 }
51 }
52 }
53 }
54 return false;
55}
56
57
58KisPerspectiveGrid::KisPerspectiveGrid()
59{
60}
61
62
63KisPerspectiveGrid::~KisPerspectiveGrid()
64{
65 clearSubGrids( );
66}
67
68bool KisPerspectiveGrid::addNewSubGrid( KisSubPerspectiveGrid* ng )
69{
70 if(hasSubGrids() && !ng->topGrid() && !ng->bottomGrid() && !ng->leftGrid() && !ng->rightGrid() )
71 {
72 kdError() << "sub grids need a neighbourgh if they are not the first grid to be added" << endl;
73 return false;
74 }
75 m_subGrids.push_back(ng);
76 return true;
77}
78
79
80void KisPerspectiveGrid::clearSubGrids( )
81{
82 for( TQValueList<KisSubPerspectiveGrid*>::const_iterator it = begin(); it != end(); ++it)
83 {
84 delete *it;
85 }
86 m_subGrids.clear();
87}
88
89KisSubPerspectiveGrid* KisPerspectiveGrid::gridAt(KisPoint p)
90{
91 for( TQValueList<KisSubPerspectiveGrid*>::const_iterator it = begin(); it != end(); ++it)
92 {
93 if( (*it)->contains(p) )
94 {
95 return *it;
96 }
97 }
98 return 0;
99}
100
A double-based point class that can return it's coordinates approximated to integers.
Definition kis_point.h:28