added summerhack
[summerhack] / tools / curve_draw / curves.h
1 /*
2 This file is part of the graphics core library.
3
4 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
5
6 the graphics core library 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 the graphics core library 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 the graphics core library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 /* 3D Curves
22  * 
23  * author: John Tsiombikas 2003
24  * modified:
25  *              John Tsiombikas 2004, 2005
26  *              Mihalis Georgoulopoulos 2004
27  */
28
29 #ifndef _CURVES_HPP_
30 #define _CURVES_HPP_
31
32 #include <string>
33 #include "vmath/vmath.h"
34 #include "linkedlist.h"
35
36
37 class Curve {
38 protected:
39         LinkedList<Vector3> control_points;
40         Vector2 *samples;       // used for parametrizing by arc length
41         int sample_count;
42         bool arc_parametrize;
43
44         Curve *ease_curve;      // ease in/out curve (1D, x&z discarded)
45         int ease_sample_count, ease_step;
46         
47         void sample_arc_lengths();
48         scalar_t parametrize(scalar_t t) const;
49         scalar_t ease(scalar_t t) const;
50
51 public:
52         std::string name;
53
54         Curve();
55         virtual ~Curve();
56         virtual void add_control_point(const Vector3 &cp);
57         virtual void remove_control_point(int index);
58         virtual Vector3 *get_control_point(int index);
59
60         virtual int get_point_count() const;
61         virtual int get_segment_count() const = 0;
62         virtual void set_arc_parametrization(bool state);
63         virtual void set_ease_curve(Curve *curve);
64         virtual void set_ease_sample_count(int count);
65
66         virtual Vector3 interpolate(scalar_t t) const = 0;
67         virtual Vector3 operator ()(scalar_t t) const;
68
69         friend bool save_curve(const char *fname, const Curve *curve);
70 };
71
72 class BSplineCurve : public Curve {
73 public:
74         virtual int get_segment_count() const;
75         virtual Vector3 interpolate(scalar_t t) const;
76 };
77
78 class CatmullRomSplineCurve : public Curve {
79 public:
80         virtual int get_segment_count() const;
81         virtual Vector3 interpolate(scalar_t t) const;
82 };
83
84 class BezierSpline : public Curve {
85 public:
86         virtual int get_segment_count() const;
87         virtual Vector3 interpolate(scalar_t t) const;
88
89         Vector3 get_control_point(int i) const;
90         Vector3 get_tangent(scalar_t t);
91 };
92
93 bool save_curve(const char *fname, const Curve *curve);
94 Curve *load_curve(const char *fname);
95
96 #endif  // _CURVES_HPP_