]>
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
);
105 template <typename R
, typename P
>
106 inline R
convert(const P
& p
)
112 inline Vector3 convert
<Vector3
,Vector4
>(const Vector4
& vec
)
114 return Vector3(vec
[0], vec
[1], vec
[2]);
118 inline Vector2 convert
<Vector2
,Vector3
>(const Vector3
& vec
)
120 return Vector2(vec
[0], vec
[1]);
124 inline Vector4 convert
<Vector4
,Vector3
>(const Vector3
& vec
)
126 return Vector4(vec
[0], vec
[1], vec
[2], SCALAR(0.0));
130 inline Vector3 convert
<Vector3
,Vector2
>(const Vector2
& vec
)
132 return Vector3(vec
[0], vec
[1], SCALAR(0.0));
135 template <typename P
>
138 cast(const P
& p
) : param(p
) {}
139 template <typename R
>
140 operator R() { return convert
<R
,P
>(param
); }
147 const Scalar EPSILON
= SCALAR(0.000001);
150 * Check the equality of scalars with a certain degree of error allowed.
153 inline bool isEqual(Scalar a
, Scalar b
, Scalar epsilon
= EPSILON
)
155 return std::abs(a
- b
) < epsilon
;
160 // Here are some generic implementations of a few simple integrators. To use,
161 // you need one type representing the state and another containing the
162 // derivatives of the primary state variables. The state class must implement
165 // void getDerivative(Derivative_Type& derivative, Scalar absoluteTime);
166 // void step(const Derivative_Type& derivative, Scalar deltaTime);
168 // Additionally, the derivative class must overload a few operators:
170 // Derivative_Type operator+(const Derivative_Type& other) const
171 // Derivative_Type operator*(const Derivative_Type& other) const
173 template<typename S
, typename D
>
174 inline D
evaluate(const S
& state
, Scalar t
)
177 state
.getDerivative(derivative
, t
);
181 template<typename S
, typename D
>
182 inline D
evaluate(S state
, Scalar t
, Scalar dt
, const D
& derivative
)
184 state
.step(derivative
, dt
);
185 return evaluate
<S
,D
>(state
, t
+ dt
);
189 template<typename S
, typename D
>
190 inline void euler(S
& state
, Scalar t
, Scalar dt
)
192 D a
= evaluate
<S
,D
>(state
, t
);
197 template<typename S
, typename D
>
198 inline void rk2(S
& state
, Scalar t
, Scalar dt
)
200 D a
= evaluate
<S
,D
>(state
, t
);
201 D b
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), a
);
206 template<typename S
, typename D
>
207 inline void rk4(S
& state
, Scalar t
, Scalar dt
)
209 D a
= evaluate
<S
,D
>(state
, t
);
210 D b
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), a
);
211 D c
= evaluate
<S
,D
>(state
, t
, dt
* SCALAR(0.5), b
);
212 D d
= evaluate
<S
,D
>(state
, t
, dt
, c
);
214 state
.step((a
+ (b
+ c
) * SCALAR(2.0) + d
) * SCALAR(1.0/6.0), dt
);
220 #endif // _MOOF_MATH_HH_
222 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.045862 seconds and 4 git commands to generate.