added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / n3dmath2 / n3dmath2.hpp
1 /*
2 Copyright 2004 John Tsiombikas <nuclear@siggraph.org>
3
4 This file is part of the n3dmath2 library.
5
6 The n3dmath2 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 n3dmath2 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 n3dmath2 library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #ifndef _N3DMATH2_HPP_
22 #define _N3DMATH2_HPP_
23
24 #include <math.h>
25 #include "n3dmath2_types.hpp"
26
27 extern const scalar_t e;
28
29 extern const scalar_t pi;
30 extern const scalar_t half_pi;
31 extern const scalar_t quarter_pi;
32 extern const scalar_t two_pi;
33 extern const scalar_t three_half_pi;
34
35 extern const scalar_t xsmall_number;
36 extern const scalar_t small_number;
37
38 extern const scalar_t error_margin;
39
40
41 // basis vectors
42 #define VECTOR3_I       (Vector3(1.0, 0.0, 0.0))
43 #define VECTOR3_J       (Vector3(0.0, 1.0, 0.0))
44 #define VECTOR3_K       (Vector3(0.0, 0.0, 1.0))
45
46 #define VECTOR2_I       (Vector2(1.0, 0.0))
47 #define VECTOR2_J       (Vector2(0.0, 1.0))
48
49 // angle conversion
50 #define RAD_TO_DEG(a) ((((scalar_t)a) * 360.0) / two_pi)
51 //#define DEG_TO_RAD(a) ((((scalar_t)a) * two_pi) / 360.0)
52 #define DEG_TO_RAD(a) (((scalar_t)a) * (pi / 180.0))
53
54 #define SQ(x) ((x) * (x))
55
56 #ifndef __GLIBC__
57 #define round(x)        ((x) >= 0 ? (x) + 0.5 : (x) - 0.5)
58 #endif  // __GLIBC__
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 // -- point / line distance in 2D --
79 scalar_t dist_line(const Vector2 &p1, const Vector2 &p2, const Vector2 &p);
80
81 // -- actual class definitions --
82 #include "n3dmath2_vec.hpp"
83 #include "n3dmath2_mat.hpp"
84 #include "n3dmath2_qua.hpp"
85 #include "n3dmath2_sph.hpp"
86 #include "n3dmath2_ray.hpp"
87 #include "n3dmath2_qdr.hpp"
88
89 class Basis {
90 public:
91         Vector3 i, j, k;
92
93         Basis();
94         Basis(const Vector3 &i, const Vector3 &j, const Vector3 &k);
95         Basis(const Vector3 &dir, bool LeftHanded=true);
96
97         void rotate(scalar_t x, scalar_t y, scalar_t z);
98         void rotate(const Vector3 &axis, scalar_t angle);
99         void rotate(const Matrix4x4 &mat);
100         void rotate(const Quaternion &quat);
101
102         Matrix3x3 create_rotation_matrix() const;
103 };
104
105
106 // bad ugly hack
107 size_t size_of_scalar_type();
108
109 #include "n3dmath2.inl"
110
111 #endif  // _N3DMATH2_HPP_