1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: bezier.cpp,v 1.5 2004/03/03 22:57:15 asmax Exp $
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
29 Bezier::Bezier( intf_thread_t *p_intf, const vector<float> &rAbscissas,
30 const vector<float> &rOrdinates, Flag_t flag )
31 : SkinObject( p_intf )
33 // Copy the control points coordinates
34 m_ptx.assign( rAbscissas.begin(), rAbscissas.end() );
35 m_pty.assign( rOrdinates.begin(), rOrdinates.end() );
37 // We expect m_ptx and m_pty to have the same size, of course
38 m_nbCtrlPt = m_ptx.size();
40 // Precalculate the factoriels
42 for( int i = 1; i < m_nbCtrlPt; i++ )
44 m_ft.push_back( i * m_ft[i - 1] );
47 // Calculate the first point
49 computePoint( 0, oldx, oldy );
50 m_leftVect.push_back( oldx );
51 m_topVect.push_back( oldy );
52 m_percVect.push_back( 0 );
54 // Calculate the other points
57 for( float j = 1; j <= MAX_BEZIER_POINT; j++ )
59 percentage = j / MAX_BEZIER_POINT;
60 computePoint( percentage, cx, cy );
61 if( ( flag == kCoordsBoth && ( cx != oldx || cy != oldy ) ) ||
62 ( flag == kCoordsX && cx != oldx ) ||
63 ( flag == kCoordsY && cy != oldy ) )
65 m_percVect.push_back( percentage );
66 m_leftVect.push_back( cx );
67 m_topVect.push_back( cy );
72 m_nbPoints = m_leftVect.size();
74 // If we have only one control point, we duplicate it
75 // This allows to simplify the algorithms used in the class
78 m_leftVect.push_back( m_leftVect[0] );
79 m_topVect.push_back( m_topVect[0] );
80 m_percVect.push_back( 1 );
84 // Ensure that the percentage of the last point is always 1
85 m_percVect[m_nbPoints - 1] = 1;
89 float Bezier::getNearestPercent( int x, int y ) const
91 int nearest = findNearestPoint( x, y );
92 return m_percVect[nearest];
96 float Bezier::getMinDist( int x, int y ) const
98 int nearest = findNearestPoint( x, y );
99 return sqrt( (m_leftVect[nearest] - x) * (m_leftVect[nearest] - x) +
100 (m_topVect[nearest] - y) * (m_topVect[nearest] - y) );
104 void Bezier::getPoint( float t, int &x, int &y ) const
106 // Find the precalculated point whose percentage is nearest from t
108 float minDiff = fabs( m_percVect[0] - t );
110 // The percentages are stored in increasing order, so we can stop the loop
111 // as soon as 'diff' starts increasing
113 while( refPoint < m_nbPoints &&
114 (diff = fabs( m_percVect[refPoint] - t )) <= minDiff )
120 // The searched point is then (refPoint - 1)
121 // We know that refPoint > 0 because we looped at least once
122 x = m_leftVect[refPoint - 1];
123 y = m_topVect[refPoint - 1];
127 int Bezier::getWidth() const
130 for( int i = 0; i < m_nbPoints; i++ )
132 if( m_leftVect[i] > width )
134 width = m_leftVect[i];
141 int Bezier::getHeight() const
144 for( int i = 0; i < m_nbPoints; i++ )
146 if( m_topVect[i] > height )
148 height = m_topVect[i];
155 int Bezier::findNearestPoint( int x, int y ) const
157 // The distance to the first point is taken as the reference
159 int minDist = (m_leftVect[0] - x) * (m_leftVect[0] - x) +
160 (m_topVect[0] - y) * (m_topVect[0] - y);
163 for( int i = 1; i < m_nbPoints; i++ )
165 dist = (m_leftVect[i] - x) * (m_leftVect[i] - x) +
166 (m_topVect[i] - y) * (m_topVect[i] - y);
178 void Bezier::computePoint( float t, int &x, int &y ) const
180 // See http://astronomy.swin.edu.au/~pbourke/curves/bezier/ for a simple
181 // explanation of the algorithm
185 for( int i = 0; i < m_nbCtrlPt; i++ )
187 coeff = computeCoeff( i, m_nbCtrlPt - 1, t );
188 xPos += m_ptx[i] * coeff;
189 yPos += m_pty[i] * coeff;
197 inline float Bezier::computeCoeff( int i, int n, float t ) const
199 return (power( t, i ) * power( 1 - t, (n - i) ) *
200 (m_ft[n] / m_ft[i] / m_ft[n - i]));
204 inline float Bezier::power( float x, int n ) const
207 return x * power( x, n - 1);