]> git.mxchange.org Git - flightgear.git/blob - src/FDM/YASim/Math.cpp
Cleanup and refactoring to better integrate the helicopter code into
[flightgear.git] / src / FDM / YASim / Math.cpp
1 #include <math.h>
2
3 #include "Math.hpp"
4 namespace yasim {
5
6 float Math::clamp(float val, float min, float max)
7 {
8     if(val < min) return min;
9     if(val > max) return max;
10     return val;
11 }
12
13 float Math::abs(float f)
14 {
15     return (float)::fabs(f);
16 }
17
18 float Math::sqrt(float f)
19 {
20     return (float)::sqrt(f);
21 }
22
23 float Math::ceil(float f)
24 {
25     return (float)::ceil(f);
26 }
27
28 float Math::acos(float f)
29 {
30     return (float)::acos(f);
31 }
32
33 float Math::asin(float f)
34 {
35     return (float)::asin(f);
36 }
37
38 float Math::cos(float f)
39 {
40     return (float)::cos(f);
41 }
42
43 float Math::sin(float f)
44 {
45     return (float)::sin(f);
46 }
47
48 float Math::tan(float f)
49 {
50     return (float)::tan(f);
51 }
52
53 float Math::atan(float f)
54 {
55     return (float)::atan(f);
56 }
57
58 float Math::atan2(float y, float x)
59 {
60     return (float)::atan2(y, x);
61 }
62
63 double Math::abs(double f)
64 {
65     return ::fabs(f);
66 }
67
68 double Math::sqrt(double f)
69 {
70     return ::sqrt(f);
71 }
72
73 float Math::pow(double base, double exp)
74 {
75     return (float)::pow(base, exp);
76 }
77
78 double Math::ceil(double f)
79 {
80     return ::ceil(f);
81 }
82
83 double Math::cos(double f)
84 {
85     return ::cos(f);
86 }
87
88 double Math::sin(double f)
89 {
90     return ::sin(f);
91 }
92
93 double Math::tan(double f)
94 {
95     return ::tan(f);
96 }
97
98 double Math::atan2(double y, double x)
99 {
100     return ::atan2(y, x);
101 }
102
103 void Math::set3(float* v, float* out)
104 {
105     out[0] = v[0];
106     out[1] = v[1];
107     out[2] = v[2];
108 }
109
110 float Math::dot3(float* a, float* b)
111 {
112     return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
113 }
114
115 void Math::mul3(float scalar, float* v, float* out)
116 {
117     out[0] = scalar * v[0];
118     out[1] = scalar * v[1];
119     out[2] = scalar * v[2];
120 }
121
122 void Math::add3(float* a, float* b, float* out)
123 {
124     out[0] = a[0] + b[0];
125     out[1] = a[1] + b[1];
126     out[2] = a[2] + b[2];
127 }
128
129 void Math::sub3(float* a, float* b, float* out)
130 {
131     out[0] = a[0] - b[0];
132     out[1] = a[1] - b[1];
133     out[2] = a[2] - b[2];
134 }
135
136 float Math::mag3(float* v)
137 {
138     return sqrt(dot3(v, v));
139 }
140
141 void Math::unit3(float* v, float* out)
142 {
143     float imag = 1/mag3(v);
144     mul3(imag, v, out);
145 }
146
147 void Math::cross3(float* a, float* b, float* out)
148 {
149     float ax=a[0], ay=a[1], az=a[2];
150     float bx=b[0], by=b[1], bz=b[2];
151     out[0] = ay*bz - by*az;
152     out[1] = az*bx - bz*ax;
153     out[2] = ax*by - bx*ay;
154 }
155
156 void Math::mmul33(float* a, float* b, float* out)
157 {
158     float tmp[9];
159     tmp[0] = a[0]*b[0] + a[1]*b[3] + a[2]*b[6];
160     tmp[3] = a[3]*b[0] + a[4]*b[3] + a[5]*b[6];
161     tmp[6] = a[6]*b[0] + a[7]*b[3] + a[8]*b[6];
162
163     tmp[1] = a[0]*b[1] + a[1]*b[4] + a[2]*b[7];
164     tmp[4] = a[3]*b[1] + a[4]*b[4] + a[5]*b[7];
165     tmp[7] = a[6]*b[1] + a[7]*b[4] + a[8]*b[7];
166
167     tmp[2] = a[0]*b[2] + a[1]*b[5] + a[2]*b[8];
168     tmp[5] = a[3]*b[2] + a[4]*b[5] + a[5]*b[8];
169     tmp[8] = a[6]*b[2] + a[7]*b[5] + a[8]*b[8];
170
171     int i;
172     for(i=0; i<9; i++)
173         out[i] = tmp[i];
174 }
175
176 void Math::vmul33(float* m, float* v, float* out)
177 {
178     float x = v[0], y = v[1], z = v[2];
179     out[0] = x*m[0] + y*m[1] + z*m[2];
180     out[1] = x*m[3] + y*m[4] + z*m[5];
181     out[2] = x*m[6] + y*m[7] + z*m[8];
182 }
183
184 void Math::tmul33(float* m, float* v, float* out)
185 {
186     float x = v[0], y = v[1], z = v[2];
187     out[0] = x*m[0] + y*m[3] + z*m[6];
188     out[1] = x*m[1] + y*m[4] + z*m[7];
189     out[2] = x*m[2] + y*m[5] + z*m[8];
190 }
191
192 void Math::invert33(float* m, float* out)
193 {
194     // Compute the inverse as the adjoint matrix times 1/(det M).
195     // A, B ... I are the cofactors of a b c
196     //                                 d e f
197     //                                 g h i
198     float a=m[0], b=m[1], c=m[2];
199     float d=m[3], e=m[4], f=m[5];
200     float g=m[6], h=m[7], i=m[8];
201
202     float A =  (e*i - h*f);
203     float B = -(d*i - g*f);
204     float C =  (d*h - g*e);
205     float D = -(b*i - h*c);
206     float E =  (a*i - g*c);
207     float F = -(a*h - g*b);
208     float G =  (b*f - e*c);
209     float H = -(a*f - d*c);
210     float I =  (a*e - d*b);
211
212     float id = 1/(a*A + b*B + c*C);
213
214     out[0] = id*A; out[1] = id*D; out[2] = id*G;
215     out[3] = id*B; out[4] = id*E; out[5] = id*H;
216     out[6] = id*C; out[7] = id*F; out[8] = id*I;     
217 }
218
219 void Math::trans33(float* m, float* out)
220 {
221     // 0 1 2   Elements 0, 4, and 8 are the same
222     // 3 4 5   Swap elements 1/3, 2/6, and 5/7
223     // 6 7 8
224     out[0] = m[0];
225     out[4] = m[4];
226     out[8] = m[8];
227
228     float tmp = m[1];
229     out[1] = m[3];
230     out[3] = tmp;
231
232     tmp = m[2];
233     out[2] = m[6];
234     out[6] = tmp;
235
236     tmp = m[5];
237     out[5] = m[7];
238     out[7] = tmp;
239 }
240
241 void Math::ortho33(float* x, float* y,
242                    float* xOut, float* yOut, float* zOut)
243 {
244     float x0[3], y0[3];
245     set3(x, x0);
246     set3(y, y0);
247
248     unit3(x0, xOut);
249     cross3(xOut, y0, zOut);
250     unit3(zOut, zOut);
251     cross3(zOut, xOut, yOut);
252 }
253
254 }; // namespace yasim