]>
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.
23 #include <SDL/SDL_opengl.h>
30 #if USE_DOUBLE_PRECISION
32 typedef GLdouble GLscalar
;
33 #define GL_SCALAR GL_DOUBLE
38 typedef GLfloat GLscalar
;
39 #define GL_SCALAR GL_FLOAT
40 #define SCALAR(F) (F##f)
48 typedef GLscalar Scalar
;
50 typedef cml::vector
< Scalar
, cml::fixed
<2> > Vector2
;
51 typedef cml::vector
< Scalar
, cml::fixed
<3> > Vector3
;
52 typedef cml::vector
< Scalar
, cml::fixed
<4> > Vector4
;
54 typedef cml::matrix
< Scalar
, cml::fixed
<2,2>,
55 cml::col_basis
, cml::col_major
> Matrix2
;
56 typedef cml::matrix
< Scalar
, cml::fixed
<3,3>,
57 cml::col_basis
, cml::col_major
> Matrix3
;
58 typedef cml::matrix
< Scalar
, cml::fixed
<4,4>,
59 cml::col_basis
, cml::col_major
> Matrix4
;
61 typedef cml::quaternion
< Scalar
, cml::fixed
<>, cml::vector_first
,
62 cml::positive_cross
> Quaternion
;
64 typedef cml::constants
<Scalar
> Constants
;
67 inline Vector3
demote(const Vector4
& vec
)
69 return Vector3(vec
[0], vec
[1], vec
[2]);
72 inline Vector2
demote(const Vector3
& vec
)
74 return Vector2(vec
[0], vec
[1]);
77 inline Vector4
promote(const Vector3
& vec
, Scalar extra
= 0.0)
79 return Vector4(vec
[0], vec
[1], vec
[2], extra
);
82 inline Vector3
promote(const Vector2
& vec
, Scalar extra
= 0.0)
84 return Vector3(vec
[0], vec
[1], extra
);
88 template <class R
, class P
>
89 inline R
convert(const P
& p
)
95 inline Vector3 convert
<Vector3
,Vector4
>(const Vector4
& vec
)
97 return Vector3(vec
[0], vec
[1], vec
[2]);
101 inline Vector2 convert
<Vector2
,Vector3
>(const Vector3
& vec
)
103 return Vector2(vec
[0], vec
[1]);
107 inline Vector4 convert
<Vector4
,Vector3
>(const Vector3
& vec
)
109 return Vector4(vec
[0], vec
[1], vec
[2], SCALAR(0.0));
113 inline Vector3 convert
<Vector3
,Vector2
>(const Vector2
& vec
)
115 return Vector3(vec
[0], vec
[1], SCALAR(0.0));
121 cast(const P
& p
) : param(p
) {}
123 operator R() { return convert
<R
,P
>(param
); }
130 const Scalar EPSILON
= SCALAR(0.000001);
133 * Check the equality of scalars with a certain degree of error allowed.
136 inline bool isEqual(Scalar a
, Scalar b
, Scalar epsilon
= EPSILON
)
138 return std::abs(a
- b
) < epsilon
;
143 // Here are some generic implementations of a few simple integrators. To
144 // use, you need one type representing the state and another containing the
145 // derivatives of the primary state variables. The state class must
146 // implement these methods:
148 // void getDerivative(Derivative_Type& derivative, Scalar absoluteTime);
149 // void step(const Derivative_Type& derivative, Scalar deltaTime);
151 // Additionally, the derivative class must overload a few operators:
153 // Derivative_Type operator+(const Derivative_Type& other) const
154 // Derivative_Type operator*(const Derivative_Type& other) const
156 template <class S
, class D
>
157 inline D
evaluate(const S
& state
, Scalar t
)
160 state
.getDerivative(derivative
, t
);
164 template <class S
, class D
>
165 inline D
evaluate(S state
, Scalar t
, Scalar dt
, const D
& derivative
)
167 state
.step(derivative
, dt
);
168 return evaluate
<S
,D
>(state
, t
+ dt
);
172 template <class S
, class D
>
173 inline void euler(S
& state
, Scalar t
, Scalar dt
)
175 D a
= evaluate
<S
,D
>(state
, t
);
180 template <class S
, class D
>
181 inline void rk2(S
& state
, Scalar t
, Scalar dt
)
183 D a
= evaluate
<S
,D
>(state
, t
);
184 D b
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), a
);
189 template <class S
, class D
>
190 inline void rk4(S
& state
, Scalar t
, Scalar dt
)
192 D a
= evaluate
<S
,D
>(state
, t
);
193 D b
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), a
);
194 D c
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), b
);
195 D d
= evaluate
<S
,D
>(state
, t
, dt
, c
);
197 state
.step((a
+ (b
+ c
) * SCALAR(2.0) + d
) * SCALAR(1.0/6.0), dt
);
203 #endif // _MOOF_MATH_HH_
This page took 0.047587 seconds and 4 git commands to generate.