]>
Dogcows Code - chaz/yoink/blob - src/Moof/Math.hh
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
29 #ifndef _MOOF_MATH_HH_
30 #define _MOOF_MATH_HH_
34 * General math-related types and functions.
40 #include <SDL/SDL_opengl.h>
47 #if USE_DOUBLE_PRECISION
49 typedef GLdouble GLscalar
;
50 #define GL_SCALAR GL_DOUBLE
55 typedef GLfloat GLscalar
;
56 #define GL_SCALAR GL_FLOAT
57 #define SCALAR(F) (F##f)
65 typedef GLscalar Scalar
;
67 typedef cml::vector
< Scalar
, cml::fixed
<2> > Vector2
;
68 typedef cml::vector
< Scalar
, cml::fixed
<3> > Vector3
;
69 typedef cml::vector
< Scalar
, cml::fixed
<4> > Vector4
;
71 typedef cml::matrix
< Scalar
, cml::fixed
<2,2>,
72 cml::col_basis
, cml::col_major
> Matrix2
;
73 typedef cml::matrix
< Scalar
, cml::fixed
<3,3>,
74 cml::col_basis
, cml::col_major
> Matrix3
;
75 typedef cml::matrix
< Scalar
, cml::fixed
<4,4>,
76 cml::col_basis
, cml::col_major
> Matrix4
;
78 typedef cml::quaternion
< Scalar
, cml::fixed
<>, cml::vector_first
,
79 cml::positive_cross
> Quaternion
;
81 typedef cml::constants
<Scalar
> Constants
;
84 inline Vector3
demote(const Vector4
& vec
)
86 return Vector3(vec
[0], vec
[1], vec
[2]);
89 inline Vector2
demote(const Vector3
& vec
)
91 return Vector2(vec
[0], vec
[1]);
94 inline Vector4
promote(const Vector3
& vec
, Scalar extra
= 0.0)
96 return Vector4(vec
[0], vec
[1], vec
[2], extra
);
99 inline Vector3
promote(const Vector2
& vec
, Scalar extra
= 0.0)
101 return Vector3(vec
[0], vec
[1], extra
);
106 const Scalar EPSILON
= SCALAR(0.000001);
109 * Check the equality of scalars with a certain degree of error allowed.
112 inline bool isEqual(Scalar a
, Scalar b
, Scalar epsilon
= EPSILON
)
114 return std::abs(a
- b
) < epsilon
;
119 // Here are some generic implementations of a few simple integrators. To use,
120 // you need one type representing the state and another containing the
121 // derivatives of the primary state variables. The state class must implement
124 // void getDerivative(Derivative_Type& derivative, Scalar absoluteTime);
125 // void step(const Derivative_Type& derivative, Scalar deltaTime);
127 // Additionally, the derivative class must overload a few operators:
129 // Derivative_Type operator+(const Derivative_Type& other) const
130 // Derivative_Type operator*(const Derivative_Type& other) const
132 template<typename S
, typename D
>
133 inline D
evaluate(const S
& state
, Scalar t
)
136 state
.getDerivative(derivative
, t
);
140 template<typename S
, typename D
>
141 inline D
evaluate(S state
, Scalar t
, Scalar dt
, const D
& derivative
)
143 state
.step(derivative
, dt
);
144 return evaluate
<S
,D
>(state
, t
+ dt
);
148 template<typename S
, typename D
>
149 inline void euler(S
& state
, Scalar t
, Scalar dt
)
151 D a
= evaluate
<S
,D
>(state
, t
);
156 template<typename S
, typename D
>
157 inline void rk2(S
& state
, Scalar t
, Scalar dt
)
159 D a
= evaluate
<S
,D
>(state
, t
);
160 D b
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), a
);
165 template<typename S
, typename D
>
166 inline void rk4(S
& state
, Scalar t
, Scalar dt
)
168 D a
= evaluate
<S
,D
>(state
, t
);
169 D b
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), a
);
170 D c
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), b
);
171 D d
= evaluate
<S
,D
>(state
, t
, dt
, c
);
173 state
.step((a
+ (b
+ c
) * SCALAR(2.0) + d
) * SCALAR(1.0/6.0), dt
);
179 #endif // _MOOF_MATH_HH_
181 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.041991 seconds and 4 git commands to generate.