the massive refactoring effort
[chaz/yoink] / src / moof / math.hh
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #ifndef _MOOF_MATH_HH_
13 #define _MOOF_MATH_HH_
14
15 /**
16 * \file math.hh
17 * General math-related types and functions.
18 */
19
20 #include <cmath>
21
22 #include <SDL/SDL_opengl.h>
23
24 #include <moof/cml/cml.h>
25
26 #if HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30
31 #if USE_DOUBLE_PRECISION
32 typedef GLdouble GLscalar;
33 #define GL_SCALAR GL_DOUBLE
34 #define SCALAR(D) (D)
35 #else
36 typedef GLfloat GLscalar;
37 #define GL_SCALAR GL_FLOAT
38 #define SCALAR(F) (F##f)
39 #endif
40
41
42 namespace moof {
43
44
45 using namespace cml;
46
47
48 typedef GLscalar scalar;
49
50 typedef vector< scalar, fixed<2> > vector2;
51 typedef vector< scalar, fixed<3> > vector3;
52 typedef vector< scalar, fixed<4> > vector4;
53
54 typedef matrix< scalar, fixed<2,2>, col_basis, col_major > matrix2;
55 typedef matrix< scalar, fixed<3,3>, col_basis, col_major > matrix3;
56 typedef matrix< scalar, fixed<4,4>, col_basis, col_major > matrix4;
57
58 typedef quaternion< scalar, fixed<>, vector_first, positive_cross > quaternion;
59
60 typedef constants<scalar> constants;
61
62
63 inline vector3 demote(const vector4& vec)
64 {
65 return vector3(vec[0], vec[1], vec[2]);
66 }
67
68 inline vector2 demote(const vector3& vec)
69 {
70 return vector2(vec[0], vec[1]);
71 }
72
73 inline vector4 promote(const vector3& vec, scalar extra = SCALAR(0.0))
74 {
75 return vector4(vec[0], vec[1], vec[2], extra);
76 }
77
78 inline vector3 promote(const vector2& vec, scalar extra = SCALAR(0.0))
79 {
80 return vector3(vec[0], vec[1], extra);
81 }
82
83
84 const scalar EPSILON = SCALAR(0.000001);
85
86 /**
87 * Check the equality of scalars with a certain degree of error allowed.
88 */
89
90 inline bool is_equal(scalar a, scalar b, scalar epsilon = EPSILON)
91 {
92 return std::abs(a - b) < epsilon;
93 }
94
95
96 // Here are some generic implementations of a few simple integrators. To
97 // use, you need one type representing the state and another containing the
98 // derivatives of the primary state variables. The state class must
99 // implement these methods:
100 //
101 // void calculate_derivative(Derivative_Type& derivative, scalar absoluteTime);
102 // void step(const Derivative_Type& derivative, scalar deltaTime);
103 //
104 // Additionally, the derivative class must overload a few operators:
105 //
106 // Derivative_Type operator + (const Derivative_Type& other) const
107 // Derivative_Type operator * (const Derivative_Type& other) const
108
109 template <class S, class D>
110 inline D evaluate(const S& state, scalar t)
111 {
112 D derivative;
113 state.calculate_derivative(derivative, t);
114 return derivative;
115 }
116
117 template <class S, class D>
118 inline D evaluate(S state, scalar t, scalar dt, const D& derivative)
119 {
120 state.step(derivative, dt);
121 return evaluate<S,D>(state, t + dt);
122 }
123
124
125 template <class S, class D>
126 inline void euler(S& state, scalar t, scalar dt)
127 {
128 D a = evaluate<S,D>(state, t);
129
130 state.step(a, dt);
131 }
132
133 template <class S, class D>
134 inline void rk2(S& state, scalar t, scalar dt)
135 {
136 D a = evaluate<S,D>(state, t);
137 D b = evaluate<S,D>(state, t, dt * SCALAR(0.5), a);
138
139 state.step(b, dt);
140 }
141
142 template <class S, class D>
143 inline void rk4(S& state, scalar t, scalar dt)
144 {
145 D a = evaluate<S,D>(state, t);
146 D b = evaluate<S,D>(state, t, dt * SCALAR(0.5), a);
147 D c = evaluate<S,D>(state, t, dt * SCALAR(0.5), b);
148 D d = evaluate<S,D>(state, t, dt, c);
149
150 state.step((a + (b + c) * SCALAR(2.0) + d) * SCALAR(1.0/6.0), dt);
151 }
152
153
154 } // namespace moof
155
156 #endif // _MOOF_MATH_HH_
157
This page took 0.042582 seconds and 5 git commands to generate.