6 float Math::clamp(float val, float min, float max)
8 if(val < min) return min;
9 if(val > max) return max;
13 float Math::abs(float f)
15 return (float)::fabs(f);
18 float Math::sqrt(float f)
20 return (float)::sqrt(f);
23 float Math::ceil(float f)
25 return (float)::ceil(f);
28 float Math::cos(float f)
30 return (float)::cos(f);
33 float Math::sin(float f)
35 return (float)::sin(f);
38 float Math::tan(float f)
40 return (float)::tan(f);
43 float Math::atan2(float y, float x)
45 return (float)::atan2(y, x);
48 double Math::abs(double f)
53 double Math::sqrt(double f)
58 float Math::pow(double base, double exp)
60 return (float)::pow(base, exp);
63 double Math::ceil(double f)
68 double Math::cos(double f)
73 double Math::sin(double f)
78 double Math::tan(double f)
83 double Math::atan2(double y, double x)
88 void Math::set3(float* v, float* out)
95 float Math::dot3(float* a, float* b)
97 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
100 void Math::mul3(float scalar, float* v, float* out)
102 out[0] = scalar * v[0];
103 out[1] = scalar * v[1];
104 out[2] = scalar * v[2];
107 void Math::add3(float* a, float* b, float* out)
109 out[0] = a[0] + b[0];
110 out[1] = a[1] + b[1];
111 out[2] = a[2] + b[2];
114 void Math::sub3(float* a, float* b, float* out)
116 out[0] = a[0] - b[0];
117 out[1] = a[1] - b[1];
118 out[2] = a[2] - b[2];
121 float Math::mag3(float* v)
123 return sqrt(dot3(v, v));
127 void Math::unit3(float* v, float* out)
129 float imag = 1/mag3(v);
133 void Math::cross3(float* a, float* b, float* out)
135 float ax=a[0], ay=a[1], az=a[2];
136 float bx=b[0], by=b[1], bz=b[2];
137 out[0] = ay*bz - by*az;
138 out[1] = az*bx - bz*ax;
139 out[2] = ax*by - bx*ay;
142 void Math::mmul33(float* a, float* b, float* out)
145 tmp[0] = a[0]*b[0] + a[1]*b[3] + a[2]*b[6];
146 tmp[3] = a[3]*b[0] + a[4]*b[3] + a[5]*b[6];
147 tmp[6] = a[6]*b[0] + a[7]*b[3] + a[8]*b[6];
149 tmp[1] = a[0]*b[1] + a[1]*b[4] + a[2]*b[7];
150 tmp[4] = a[3]*b[1] + a[4]*b[4] + a[5]*b[7];
151 tmp[7] = a[6]*b[1] + a[7]*b[4] + a[8]*b[7];
153 tmp[2] = a[0]*b[2] + a[1]*b[5] + a[2]*b[8];
154 tmp[5] = a[3]*b[2] + a[4]*b[5] + a[5]*b[8];
155 tmp[8] = a[6]*b[2] + a[7]*b[5] + a[8]*b[8];
162 void Math::vmul33(float* m, float* v, float* out)
164 float x = v[0], y = v[1], z = v[2];
165 out[0] = x*m[0] + y*m[1] + z*m[2];
166 out[1] = x*m[3] + y*m[4] + z*m[5];
167 out[2] = x*m[6] + y*m[7] + z*m[8];
170 void Math::tmul33(float* m, float* v, float* out)
172 float x = v[0], y = v[1], z = v[2];
173 out[0] = x*m[0] + y*m[3] + z*m[6];
174 out[1] = x*m[1] + y*m[4] + z*m[7];
175 out[2] = x*m[2] + y*m[5] + z*m[8];
178 void Math::invert33(float* m, float* out)
180 // Compute the inverse as the adjoint matrix times 1/(det M).
181 // A, B ... I are the cofactors of a b c
184 float a=m[0], b=m[1], c=m[2];
185 float d=m[3], e=m[4], f=m[5];
186 float g=m[6], h=m[7], i=m[8];
188 float A = (e*i - h*f);
189 float B = -(d*i - g*f);
190 float C = (d*h - g*e);
191 float D = -(b*i - h*c);
192 float E = (a*i - g*c);
193 float F = -(a*h - g*b);
194 float G = (b*f - e*c);
195 float H = -(a*f - d*c);
196 float I = (a*e - d*b);
198 float id = 1/(a*A + b*B + c*C);
200 out[0] = id*A; out[1] = id*D; out[2] = id*G;
201 out[3] = id*B; out[4] = id*E; out[5] = id*H;
202 out[6] = id*C; out[7] = id*F; out[8] = id*I;
205 void Math::trans33(float* m, float* out)
207 // 0 1 2 Elements 0, 4, and 8 are the same
208 // 3 4 5 Swap elements 1/3, 2/6, and 5/7
227 void Math::ortho33(float* x, float* y,
228 float* xOut, float* yOut, float* zOut)
235 cross3(xOut, y0, zOut);
237 cross3(zOut, xOut, yOut);
240 }; // namespace yasim