added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / gfx / curves.hpp
1 /*
2 This file is part of the 3dengfx, realtime visualization system.
3 Copyright (c) 2004, 2005 John Tsiombikas <nuclear@siggraph.org>
4
5 the 3dengfx library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 the 3dengfx library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with the 3dengfx library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 /* 3D Curves
21  * 
22  * author: John Tsiombikas 2003
23  * modified:
24  *              John Tsiombikas 2004, 2005, 2006
25  *              Mihalis Georgoulopoulos 2004
26  */
27
28 #ifndef _CURVES_HPP_
29 #define _CURVES_HPP_
30
31 #include "3dengfx_config.h"
32
33 #include <string>
34 #include "n3dmath2/n3dmath2.hpp"
35 #include "common/linkedlist.hpp"
36
37
38 class Curve {
39 protected:
40         LinkedList<Vector3> control_points;
41         Vector2 *samples;       // used for parametrizing by arc length
42         int sample_count;
43         bool arc_parametrize;
44
45         Curve *ease_curve;      // ease in/out curve (1D, x&z discarded)
46         int ease_sample_count, ease_step;
47         
48         void sample_arc_lengths();
49         scalar_t parametrize(scalar_t t) const;
50         scalar_t ease(scalar_t t) const;
51
52         Vector3 (*xform_cv)(const Vector3 &pt);
53         
54         virtual Vector3 interpolate(scalar_t t) const = 0;
55
56 public:
57         std::string name;
58
59         Curve();
60         virtual ~Curve();
61         virtual void add_control_point(const Vector3 &cp);
62         virtual void remove_control_point(int index);
63         virtual Vector3 *get_control_point(int index);
64
65         virtual int get_point_count() const;
66         virtual int get_segment_count() const = 0;
67         virtual void set_arc_parametrization(bool state);
68         virtual void set_ease_curve(Curve *curve);
69         virtual void set_ease_sample_count(int count);
70
71         virtual Vector3 operator ()(scalar_t t) const;
72
73         virtual void set_xform_func(Vector3 (*func)(const Vector3&));
74
75         friend bool save_curve(const char *fname, const Curve *curve);
76 };
77
78 class BSpline : public Curve {
79 protected:
80         virtual Vector3 interpolate(scalar_t t) const;
81
82 public:
83         virtual int get_segment_count() const;
84 };
85
86 typedef BSpline BSplineCurve;
87
88
89 class CatmullRomSpline : public Curve {
90 protected:
91         virtual Vector3 interpolate(scalar_t t) const;
92
93 public:
94         virtual int get_segment_count() const;
95 };
96
97 typedef CatmullRomSpline CatmullRomSplineCurve;
98
99
100 class BezierSpline : public Curve {
101 protected:
102         virtual Vector3 interpolate(scalar_t t) const;
103
104 public:
105         virtual int get_segment_count() const;
106
107         Vector3 get_control_point(int i) const;
108         Vector3 get_tangent(scalar_t t);
109 };
110
111 class PolyLine : public Curve {
112 protected:
113         virtual Vector3 interpolate(scalar_t t) const;
114
115 public:
116         virtual int get_segment_count() const;
117 };
118
119 bool save_curve(const char *fname, const Curve *curve);
120 Curve *load_curve(const char *fname);
121
122 #endif  // _CURVES_HPP_