1 /*****************************************************************************
2  * bezier.hpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id: 532d1932e56e4f63c4d9a49946e77c910a71eaed $
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 
25 #ifndef BEZIER_HPP
26 #define BEZIER_HPP
27 
28 #include "../src/skin_common.hpp"
29 #include "pointer.hpp"
30 #include <vector>
31 
32 #define MAX_BEZIER_POINT 1023
33 
34 
35 /// Class for Bezier curves
36 class Bezier: public SkinObject
37 {
38 public:
39     /// Values to indicate which coordinate(s) must be checked to consider
40     /// that two points are distinct
41     enum Flag_t
42     {
43         kCoordsBoth,    // x or y must be different (default)
44         kCoordsX,       // only x is different
45         kCoordsY        // only y is different
46     };
47 
48     Bezier( intf_thread_t *p_intf,
49             const std::vector<float> &pAbscissas,
50             const std::vector<float> &pOrdinates,
51             Flag_t flag = kCoordsBoth );
~Bezier()52     ~Bezier() { }
53 
54     /// Get the number of control points used to define the curve
getNbCtrlPoints() const55     int getNbCtrlPoints() const { return m_nbCtrlPt; }
56 
57     /// Return the percentage (between 0 and 1) of the curve point nearest
58     /// from (x, y)
59     float getNearestPercent( int x, int y ) const;
60 
61     /// Return the distance of (x, y) to the curve, corrected
62     /// by the (optional) given scale factors
63     float getMinDist( int x, int y, float xScale = 1.0f,
64                       float yScale = 1.0f ) const;
65 
66     /// Get the coordinates of the point at t percent of
67     /// the curve (t must be between 0 and 1)
68     void getPoint( float t, int &x, int &y ) const;
69 
70     /// Get the width (maximum abscissa) of the curve
71     int getWidth() const;
72 
73     /// Get the height (maximum ordinate) of the curve
74     int getHeight() const;
75 
76 private:
77     /// Number of control points
78     int m_nbCtrlPt;
79     /// vectors containing the coordinates of the control points
80     std::vector<float> m_ptx;
81     std::vector<float> m_pty;
82     /// Vector containing precalculated factoriels
83     std::vector<float> m_ft;
84 
85     /// Number of points (=pixels) used by the curve
86     int m_nbPoints;
87     /// Vectors with the coordinates of the different points of the curve
88     std::vector<int> m_leftVect;
89     std::vector<int> m_topVect;
90     /// Vector with the percentages associated with the points of the curve
91     std::vector<float> m_percVect;
92 
93     /// Return the index of the curve point that is the nearest from (x, y)
94     int findNearestPoint( int x, int y ) const;
95     /// Compute the coordinates of a point corresponding to a given
96     /// percentage
97     void computePoint( float t, int &x, int &y ) const;
98     /// Helper function to compute a coefficient of the curve
99     inline float computeCoeff( int i, int n, float t ) const;
100     /// x^n
101     static inline float power( float x, int n );
102 };
103 
104 
105 typedef CountedPtr<Bezier> BezierPtr;
106 
107 #endif
108