]> git.mxchange.org Git - simgear.git/blob - simgear/math/beziercurve.hxx
Correct finite precision issues.
[simgear.git] / simgear / math / beziercurve.hxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2009 Frederic Bouvier
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18  * MA 02110-1301, USA.
19  *
20  */
21
22 #ifndef SIMGEAR_BEZIERCURVE_HXX
23 #define SIMGEAR_BEZIERCURVE_HXX 1
24
25 #include <list>
26 using std::list;
27
28 namespace simgear
29 {
30   template<class T>
31   class BezierCurve {
32   public:
33     typedef list<T> PointList;
34
35     BezierCurve() : mMaxSubdiv( 3 ) {}
36     BezierCurve( size_t aMaxSubdiv )
37       : mMaxSubdiv( aMaxSubdiv ) {}
38     BezierCurve( const T &p1, const T &p2, const T &p3, size_t aMaxSubdiv = 3 )
39       : mMaxSubdiv( aMaxSubdiv ) {
40         subdivide( p1, p2, p3 );
41       }
42     BezierCurve( const T &p1, const T &p2, const T &p3, const T &p4, size_t aMaxSubdiv = 3 )
43       : mMaxSubdiv( aMaxSubdiv ) {
44         subdivide( p1, p2, p3, p4 );
45       }
46
47     void subdivide( const T &p1, const T &p2, const T &p3 ) {
48       mPointList.clear();
49       mPointList.push_back( p1 );
50       recursiveSubdivide( p1, p2, p3, 1 );
51       mPointList.push_back( p3 );
52     }
53
54     void subdivide( const T &p1, const T &p2, const T &p3, const T &p4 ) {
55       mPointList.clear();
56       mPointList.push_back( p1 );
57       recursiveSubdivide( p1, p2, p3, p4, 1 );
58       mPointList.push_back( p4 );
59     }
60
61     void setMaxSubdiv( size_t aMaxSubdiv ) { mMaxSubdiv = aMaxSubdiv; }
62     void getMaxSubdiv() const { return mMaxSubdiv; }
63     PointList &pointList() { return mPointList; }
64     const PointList &pointList() const { return mPointList; }
65
66   private:
67     T midPoint( const T &p1, const T &p2 ) {
68       return ( p1 + p2 ) / 2;
69     }
70     bool recursiveSubdivide( const T &p1, const T &p2, const T &p3, size_t l ) {
71       if ( l > mMaxSubdiv )
72         return false;
73
74       T p12 = midPoint( p1, p2 ),
75         p23 = midPoint( p2, p3 ),
76         p123 = midPoint( p12, p23 );
77       recursiveSubdivide( p1, p12, p123, l + 1 );
78       mPointList.push_back( p123 );
79       recursiveSubdivide( p123, p23, p3, l + 1 );
80       return true;
81     }
82
83     bool recursiveSubdivide( const T &p1, const T &p2, const T &p3, const T &p4, size_t l ) {
84       if ( l > mMaxSubdiv )
85         return false;
86
87       T p12 = midPoint( p1, p2 ),
88         p23 = midPoint( p2, p3 ),
89         p34 = midPoint( p3, p4 ),
90         p123 = midPoint( p12, p23 ),
91         p234 = midPoint( p23, p34 ),
92         p1234 = midPoint( p123, p234 );
93       recursiveSubdivide( p1, p12, p123, p1234, l + 1 );
94       mPointList.push_back( p1234 );
95       recursiveSubdivide( p1234, p234, p34, p4, l + 1 );
96       return true;
97     }
98
99
100     PointList mPointList;
101     size_t mMaxSubdiv;
102   };
103 }
104
105 #endif