]> Dogcows Code - chaz/yoink/blob - src/Moof/Math.hh
ae747ec37957417ca771fc0543a142d1adaf962d
[chaz/yoink] / src / Moof / Math.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
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.
15
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.
26
27 *******************************************************************************/
28
29 #ifndef _MOOF_MATH_HH_
30 #define _MOOF_MATH_HH_
31
32 /**
33 * @file Math.hh
34 * General math-related types and functions.
35 */
36
37 #include <cmath>
38 #include <cml/cml.h>
39
40 #include <SDL/SDL_opengl.h>
41
42 #if HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46
47 #if USE_DOUBLE_PRECISION
48
49 typedef GLdouble GLscalar;
50 #define GL_SCALAR GL_DOUBLE
51 #define SCALAR(D) (D)
52
53 #else
54
55 typedef GLfloat GLscalar;
56 #define GL_SCALAR GL_FLOAT
57 #define SCALAR(F) (F##f)
58
59 #endif
60
61
62 namespace Mf {
63
64
65 typedef GLscalar Scalar;
66
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;
70
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;
77
78 typedef cml::quaternion< Scalar, cml::fixed<>, cml::vector_first,
79 cml::positive_cross > Quaternion;
80
81 typedef cml::constants<Scalar> Constants;
82
83
84 inline Vector3 demote(const Vector4& vec)
85 {
86 return Vector3(vec[0], vec[1], vec[2]);
87 }
88
89 inline Vector2 demote(const Vector3& vec)
90 {
91 return Vector2(vec[0], vec[1]);
92 }
93
94 inline Vector4 promote(const Vector3& vec, Scalar extra = 1.0)
95 {
96 return Vector4(vec[0], vec[1], vec[2], extra);
97 }
98
99 inline Vector3 promote(const Vector2& vec, Scalar extra = 1.0)
100 {
101 return Vector3(vec[0], vec[1], extra);
102 }
103
104
105
106 const Scalar EPSILON = SCALAR(0.000001);
107
108 /**
109 * Check the equality of scalars with a certain degree of error allowed.
110 */
111
112 inline bool isEqual(Scalar a, Scalar b, Scalar epsilon = EPSILON)
113 {
114 return std::abs(a - b) < epsilon;
115 }
116
117
118
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
122 // these methods:
123 //
124 // void getDerivative(Derivative_Type& derivative, Scalar absoluteTime);
125 // void step(const Derivative_Type& derivative, Scalar deltaTime);
126 //
127 // Additionally, the derivative class must overload a few operators:
128 //
129 // Derivative_Type operator+(const Derivative_Type& other) const
130 // Derivative_Type operator*(const Derivative_Type& other) const
131
132 template<typename S, typename D>
133 inline D evaluate(const S& state, Scalar t)
134 {
135 D derivative;
136 state.getDerivative(derivative, t);
137 return derivative;
138 }
139
140 template<typename S, typename D>
141 inline D evaluate(S state, Scalar t, Scalar dt, const D& derivative)
142 {
143 state.step(derivative, dt);
144 return evaluate<S,D>(state, t + dt);
145 }
146
147
148 template<typename S, typename D>
149 inline void euler(S& state, Scalar t, Scalar dt)
150 {
151 D a = evaluate<S,D>(state, t);
152
153 state.step(a, dt);
154 }
155
156 template<typename S, typename D>
157 inline void rk2(S& state, Scalar t, Scalar dt)
158 {
159 D a = evaluate<S,D>(state, t);
160 D b = evaluate<S,D>(state, t, dt * SCALAR(0.5), a);
161
162 state.step(b, dt);
163 }
164
165 template<typename S, typename D>
166 inline void rk4(S& state, Scalar t, Scalar dt)
167 {
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);
172
173 state.step((a + (b + c) * SCALAR(2.0) + d) * SCALAR(1.0/6.0), dt);
174 }
175
176
177 } // namespace Mf
178
179 #endif // _MOOF_MATH_HH_
180
181 /** vim: set ts=4 sw=4 tw=80: *************************************************/
182
This page took 0.038062 seconds and 3 git commands to generate.