]>
Dogcows Code - chaz/yoink/blob - src/moof/math.hh
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.
22 #include <SDL/SDL_opengl.h>
24 #include <moof/cml/cml.h>
31 #if USE_DOUBLE_PRECISION
32 typedef GLdouble GLscalar
;
33 #define GL_SCALAR GL_DOUBLE
36 typedef GLfloat GLscalar
;
37 #define GL_SCALAR GL_FLOAT
38 #define SCALAR(F) (F##f)
48 typedef GLscalar scalar
;
50 typedef vector
< scalar
, fixed
<2> > vector2
;
51 typedef vector
< scalar
, fixed
<3> > vector3
;
52 typedef vector
< scalar
, fixed
<4> > vector4
;
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
;
58 typedef quaternion
< scalar
, fixed
<>, vector_first
, positive_cross
> quaternion
;
60 typedef constants
<scalar
> constants
;
63 inline vector3
demote(const vector4
& vec
)
65 return vector3(vec
[0], vec
[1], vec
[2]);
68 inline vector2
demote(const vector3
& vec
)
70 return vector2(vec
[0], vec
[1]);
73 inline vector4
promote(const vector3
& vec
, scalar extra
= SCALAR(0.0))
75 return vector4(vec
[0], vec
[1], vec
[2], extra
);
78 inline vector3
promote(const vector2
& vec
, scalar extra
= SCALAR(0.0))
80 return vector3(vec
[0], vec
[1], extra
);
84 const scalar EPSILON
= SCALAR(0.000001);
87 * Check the equality of scalars with a certain degree of error allowed.
90 inline bool is_equal(scalar a
, scalar b
, scalar epsilon
= EPSILON
)
92 return std::abs(a
- b
) < epsilon
;
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:
101 // void calculate_derivative(Derivative_Type& derivative, scalar absoluteTime);
102 // void step(const Derivative_Type& derivative, scalar deltaTime);
104 // Additionally, the derivative class must overload a few operators:
106 // Derivative_Type operator + (const Derivative_Type& other) const
107 // Derivative_Type operator * (const Derivative_Type& other) const
109 template <class S
, class D
>
110 inline D
evaluate(const S
& state
, scalar t
)
113 state
.calculate_derivative(derivative
, t
);
117 template <class S
, class D
>
118 inline D
evaluate(S state
, scalar t
, scalar dt
, const D
& derivative
)
120 state
.step(derivative
, dt
);
121 return evaluate
<S
,D
>(state
, t
+ dt
);
125 template <class S
, class D
>
126 inline void euler(S
& state
, scalar t
, scalar dt
)
128 D a
= evaluate
<S
,D
>(state
, t
);
133 template <class S
, class D
>
134 inline void rk2(S
& state
, scalar t
, scalar dt
)
136 D a
= evaluate
<S
,D
>(state
, t
);
137 D b
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), a
);
142 template <class S
, class D
>
143 inline void rk4(S
& state
, scalar t
, scalar dt
)
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
);
150 state
.step((a
+ (b
+ c
) * SCALAR(2.0) + d
) * SCALAR(1.0/6.0), dt
);
156 #endif // _MOOF_MATH_HH_
This page took 0.04073 seconds and 5 git commands to generate.