]>
Dogcows Code - chaz/yoink/blob - math.hh
14e8c78626acdb41bfedac1b8609e19d9588818b
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
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.
10 **************************************************************************/
12 #ifndef _MOOF_MATH_HH_
13 #define _MOOF_MATH_HH_
17 * General math-related types and functions.
25 #include <SDL/SDL_opengl.h>
28 #if ENABLE_DOUBLE_PRECISION
29 typedef GLdouble GLscalar
;
30 #define GL_SCALAR GL_DOUBLE
33 typedef GLfloat GLscalar
;
34 #define GL_SCALAR GL_FLOAT
35 #define SCALAR(F) (F##f)
45 typedef GLscalar scalar
;
47 typedef vector
< scalar
, fixed
<2> > vector2
;
48 typedef vector
< scalar
, fixed
<3> > vector3
;
49 typedef vector
< scalar
, fixed
<4> > vector4
;
51 typedef matrix
< scalar
, fixed
<2,2>, col_basis
, col_major
> matrix2
;
52 typedef matrix
< scalar
, fixed
<3,3>, col_basis
, col_major
> matrix3
;
53 typedef matrix
< scalar
, fixed
<4,4>, col_basis
, col_major
> matrix4
;
55 typedef quaternion
< scalar
, fixed
<>, vector_first
, positive_cross
> quaternion
;
57 typedef constants
<scalar
> constants
;
60 inline vector3
demote(const vector4
& vec
)
62 return vector3(vec
[0], vec
[1], vec
[2]);
65 inline vector2
demote(const vector3
& vec
)
67 return vector2(vec
[0], vec
[1]);
70 inline vector4
promote(const vector3
& vec
, scalar extra
= SCALAR(0.0))
72 return vector4(vec
[0], vec
[1], vec
[2], extra
);
75 inline vector3
promote(const vector2
& vec
, scalar extra
= SCALAR(0.0))
77 return vector3(vec
[0], vec
[1], extra
);
81 const scalar EPSILON
= SCALAR(0.000001);
84 * Check the equality of scalars with a certain degree of error allowed.
87 inline bool is_equal(scalar a
, scalar b
, scalar epsilon
= EPSILON
)
89 return std::abs(a
- b
) < epsilon
;
93 // Here are some generic implementations of a few simple integrators. To
94 // use, you need one type representing the state and another containing the
95 // derivatives of the primary state variables. The state class must
96 // implement these methods:
98 // void calculate_derivative(Derivative_Type& derivative, scalar absoluteTime);
99 // void step(const Derivative_Type& derivative, scalar deltaTime);
101 // Additionally, the derivative class must overload a few operators:
103 // Derivative_Type operator + (const Derivative_Type& other) const
104 // Derivative_Type operator * (const Derivative_Type& other) const
106 template <class S
, class D
>
107 inline D
evaluate(const S
& state
, scalar t
)
110 state
.calculate_derivative(derivative
, t
);
114 template <class S
, class D
>
115 inline D
evaluate(S state
, scalar t
, scalar dt
, const D
& derivative
)
117 state
.step(derivative
, dt
);
118 return evaluate
<S
,D
>(state
, t
+ dt
);
122 template <class S
, class D
>
123 inline void euler(S
& state
, scalar t
, scalar dt
)
125 D a
= evaluate
<S
,D
>(state
, t
);
130 template <class S
, class D
>
131 inline void rk2(S
& state
, scalar t
, scalar dt
)
133 D a
= evaluate
<S
,D
>(state
, t
);
134 D b
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), a
);
139 template <class S
, class D
>
140 inline void rk4(S
& state
, scalar t
, scalar dt
)
142 D a
= evaluate
<S
,D
>(state
, t
);
143 D b
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), a
);
144 D c
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), b
);
145 D d
= evaluate
<S
,D
>(state
, t
, dt
, c
);
147 state
.step((a
+ (b
+ c
) * SCALAR(2.0) + d
) * SCALAR(1.0/6.0), dt
);
153 #endif // _MOOF_MATH_HH_
This page took 0.041636 seconds and 3 git commands to generate.