]> Dogcows Code - chaz/yoink/blob - src/Moof/Math.hh
port to NetBSD
[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 <Moof/OpenGL.hh> // GLscalar
41
42
43 namespace Mf {
44
45
46 typedef GLscalar Scalar;
47
48 typedef cml::vector< Scalar, cml::fixed<2> > Vector2;
49 typedef cml::vector< Scalar, cml::fixed<3> > Vector3;
50 typedef cml::vector< Scalar, cml::fixed<4> > Vector4;
51
52 typedef cml::matrix< Scalar, cml::fixed<2,2>,
53 cml::col_basis, cml::col_major > Matrix2;
54 typedef cml::matrix< Scalar, cml::fixed<3,3>,
55 cml::col_basis, cml::col_major > Matrix3;
56 typedef cml::matrix< Scalar, cml::fixed<4,4>,
57 cml::col_basis, cml::col_major > Matrix4;
58
59 typedef cml::quaternion< Scalar, cml::fixed<>, cml::vector_first,
60 cml::positive_cross > Quaternion;
61
62 typedef cml::constants<Scalar> Constants;
63
64
65 inline Vector3 demote(const Vector4& vec)
66 {
67 return Vector3(vec[0], vec[1], vec[2]);
68 }
69
70 inline Vector2 demote(const Vector3& vec)
71 {
72 return Vector2(vec[0], vec[1]);
73 }
74
75 inline Vector4 promote(const Vector3& vec, Scalar extra = 1.0)
76 {
77 return Vector4(vec[0], vec[1], vec[2], extra);
78 }
79
80 inline Vector3 promote(const Vector2& vec, Scalar extra = 1.0)
81 {
82 return Vector3(vec[0], vec[1], extra);
83 }
84
85
86
87 const Scalar EPSILON = SCALAR(0.000001);
88
89 /**
90 * Check the equality of scalars with a certain degree of error allowed.
91 */
92
93 inline bool isEqual(Scalar a, Scalar b, Scalar epsilon = EPSILON)
94 {
95 return std::abs(a - b) < epsilon;
96 }
97
98
99
100 // Here are some generic implementations of a few simple integrators. To use,
101 // you need one type representing the state and another containing the
102 // derivatives of the primary state variables. The state class must implement
103 // these methods:
104 //
105 // void getDerivative(Derivative_Type& derivative, Scalar absoluteTime);
106 // void step(const Derivative_Type& derivative, Scalar deltaTime);
107 //
108 // Additionally, the derivative class must overload a few operators:
109 //
110 // Derivative_Type operator+(const Derivative_Type& other) const
111 // Derivative_Type operator*(const Derivative_Type& other) const
112
113 template<typename S, typename D>
114 inline D evaluate(const S& state, Scalar t)
115 {
116 D derivative;
117 state.getDerivative(derivative, t);
118 return derivative;
119 }
120
121 template<typename S, typename D>
122 inline D evaluate(S state, Scalar t, Scalar dt, const D& derivative)
123 {
124 state.step(derivative, dt);
125 return evaluate<S,D>(state, t + dt);
126 }
127
128
129 template<typename S, typename D>
130 inline void euler(S& state, Scalar t, Scalar dt)
131 {
132 D a = evaluate<S,D>(state, t);
133
134 state.step(a, dt);
135 }
136
137 template<typename S, typename D>
138 inline void rk2(S& state, Scalar t, Scalar dt)
139 {
140 D a = evaluate<S,D>(state, t);
141 D b = evaluate<S,D>(state, t, dt * SCALAR(0.5), a);
142
143 state.step(b, dt);
144 }
145
146 template<typename S, typename D>
147 inline void rk4(S& state, Scalar t, Scalar dt)
148 {
149 D a = evaluate<S,D>(state, t);
150 D b = evaluate<S,D>(state, t, dt * SCALAR(0.5), a);
151 D c = evaluate<S,D>(state, t, dt * SCALAR(0.5), b);
152 D d = evaluate<S,D>(state, t, dt, c);
153
154 state.step((a + (b + c) * SCALAR(2.0) + d) * SCALAR(1.0/6.0), dt);
155 }
156
157
158 } // namespace Mf
159
160 #endif // _MOOF_MATH_HH_
161
162 /** vim: set ts=4 sw=4 tw=80: *************************************************/
163
This page took 0.045697 seconds and 4 git commands to generate.