3 * Copyright (C) 2009 Frederic Bouvier
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.
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.
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,
22 #ifndef SIMGEAR_BEZIERCURVE_HXX
23 #define SIMGEAR_BEZIERCURVE_HXX 1
33 typedef list<T> PointList;
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 );
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 );
47 void subdivide( const T &p1, const T &p2, const T &p3 ) {
49 mPointList.push_back( p1 );
50 recursiveSubdivide( p1, p2, p3, 1 );
51 mPointList.push_back( p3 );
54 void subdivide( const T &p1, const T &p2, const T &p3, const T &p4 ) {
56 mPointList.push_back( p1 );
57 recursiveSubdivide( p1, p2, p3, p4, 1 );
58 mPointList.push_back( p4 );
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; }
67 T midPoint( const T &p1, const T &p2 ) {
68 return ( p1 + p2 ) / 2;
70 bool recursiveSubdivide( const T &p1, const T &p2, const T &p3, size_t l ) {
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 );
83 bool recursiveSubdivide( const T &p1, const T &p2, const T &p3, const T &p4, size_t l ) {
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 );
100 PointList mPointList;