added summerhack
[summerhack] / tools / curve_draw / vmath / vmath.h
1 /*
2 This file is part of XRay, a photorealistic 3D rendering library.
3 Copyright (C) 2005 John Tsiombikas
4
5 XRay 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 XRay 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 XRay; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 /** 
21  * @file vmath.h
22  * @author John Tsiombikas
23  * @date 28 June 2005
24  *
25  * vmath provides the mathematical foundations for 3D graphics.
26  * This specific file contains various constants and basic operations
27  * as well as the Basis class, which represents an orthogonal basis in
28  * 3 dimensional space.
29  */
30
31 #ifndef VMATH_H_
32 #define VMATH_H_
33
34 #include "vmath_types.h"
35
36 extern const scalar_t e;
37
38 extern const scalar_t pi;
39 extern const scalar_t half_pi;
40 extern const scalar_t quarter_pi;
41 extern const scalar_t two_pi;
42 extern const scalar_t three_half_pi;
43
44 extern const scalar_t xsmall_number;
45 extern const scalar_t small_number;
46
47 extern const scalar_t error_margin;
48
49
50 // angle conversion
51 #define RAD_TO_DEG(a) ((((scalar_t)a) * 360.0) / two_pi)
52 #define DEG_TO_RAD(a) (((scalar_t)a) * (pi / 180.0))
53
54 #define SQ(x) ((x) * (x))
55
56 #ifndef __GNUC__
57 #define round(x)        ((x) >= 0 ? (x) + 0.5 : (x) - 0.5)
58 #endif  // __GNUC__
59
60 // -- mathematical & helper functions --
61 scalar_t frand(scalar_t range);
62 scalar_t integral(scalar_t (*f)(scalar_t), scalar_t low, scalar_t high, int samples);
63 scalar_t gaussian(scalar_t x, scalar_t mean, scalar_t sdev);
64
65 // -- interpolation and approximation --
66 inline scalar_t lerp(scalar_t a, scalar_t b, scalar_t t);
67
68 scalar_t bspline(const Vector4 &cpvec, scalar_t t);
69 inline scalar_t bspline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t);
70
71 scalar_t catmull_rom_spline(const Vector4 &cpvec, scalar_t t);
72 inline scalar_t catmull_rom_spline(scalar_t a, scalar_t b, scalar_t c, scalar_t d, scalar_t t);
73
74 scalar_t bezier(const Vector4 &cp, scalar_t t);
75 Vector3 bezier(const Vector3 &p0, const Vector3 &p1, const Vector3 &p2, const Vector3 &p3, scalar_t t);
76 Vector3 bezier_tangent(const Vector3 &p0, const Vector3 &p1, const Vector3 &p2, const Vector3 &p3, scalar_t t);
77
78 // -- actual class definitions --
79 #include "vector.h"
80 #include "matrix.h"
81 #include "quaternion.h"
82 #include "sphvec.h"
83
84 /** Orthogonal basis in 3-space */
85 class Basis {
86 public:
87         Vector3 i, j, k;
88
89         Basis();
90         Basis(const Vector3 &i, const Vector3 &j, const Vector3 &k);
91         Basis(const Vector3 &dir, bool left_handed = true);
92
93         void rotate(scalar_t x, scalar_t y, scalar_t z);
94         void rotate(const Vector3 &axis, scalar_t angle);
95         void rotate(const Matrix4x4 &mat);
96         void rotate(const Quaternion &quat);
97
98         Matrix3x3 create_rotation_matrix() const;
99 };
100
101 #include "vmath.inl"
102
103 #endif  // VMATH_H_