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)
18 float Math::sqrt(float f)
23 float Math::pow(float base, float exp)
25 return ::pow(base, exp);
28 float Math::ceil(float f)
33 float Math::cos(float f)
38 float Math::sin(float f)
43 float Math::tan(float f)
48 float Math::atan2(float y, float x)
53 double Math::abs(double f)
58 double Math::sqrt(double f)
63 double Math::pow(double base, double exp)
65 return ::pow(base, exp);
68 double Math::ceil(double f)
73 double Math::cos(double f)
78 double Math::sin(double f)
83 double Math::tan(double f)
88 double Math::atan2(double y, double x)
93 void Math::set3(float* v, float* out)
100 float Math::dot3(float* a, float* b)
102 return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
105 void Math::mul3(float scalar, float* v, float* out)
107 out[0] = scalar * v[0];
108 out[1] = scalar * v[1];
109 out[2] = scalar * v[2];
112 void Math::add3(float* a, float* b, float* out)
114 out[0] = a[0] + b[0];
115 out[1] = a[1] + b[1];
116 out[2] = a[2] + b[2];
119 void Math::sub3(float* a, float* b, float* out)
121 out[0] = a[0] - b[0];
122 out[1] = a[1] - b[1];
123 out[2] = a[2] - b[2];
126 float Math::mag3(float* v)
128 return sqrt(dot3(v, v));
132 void Math::unit3(float* v, float* out)
134 float imag = 1/mag3(v);
138 void Math::cross3(float* a, float* b, float* out)
140 float ax=a[0], ay=a[1], az=a[2];
141 float bx=b[0], by=b[1], bz=b[2];
142 out[0] = ay*bz - by*az;
143 out[1] = az*bx - bz*ax;
144 out[2] = ax*by - bx*ay;
147 void Math::mmul33(float* a, float* b, float* out)
150 tmp[0] = a[0]*b[0] + a[1]*b[3] + a[2]*b[6];
151 tmp[3] = a[3]*b[0] + a[4]*b[3] + a[5]*b[6];
152 tmp[6] = a[6]*b[0] + a[7]*b[3] + a[8]*b[6];
154 tmp[1] = a[0]*b[1] + a[1]*b[4] + a[2]*b[7];
155 tmp[4] = a[3]*b[1] + a[4]*b[4] + a[5]*b[7];
156 tmp[7] = a[6]*b[1] + a[7]*b[4] + a[8]*b[7];
158 tmp[2] = a[0]*b[2] + a[1]*b[5] + a[2]*b[8];
159 tmp[5] = a[3]*b[2] + a[4]*b[5] + a[5]*b[8];
160 tmp[8] = a[6]*b[2] + a[7]*b[5] + a[8]*b[8];
167 void Math::vmul33(float* m, float* v, float* out)
169 float x = v[0], y = v[1], z = v[2];
170 out[0] = x*m[0] + y*m[1] + z*m[2];
171 out[1] = x*m[3] + y*m[4] + z*m[5];
172 out[2] = x*m[6] + y*m[7] + z*m[8];
175 void Math::tmul33(float* m, float* v, float* out)
177 float x = v[0], y = v[1], z = v[2];
178 out[0] = x*m[0] + y*m[3] + z*m[6];
179 out[1] = x*m[1] + y*m[4] + z*m[7];
180 out[2] = x*m[2] + y*m[5] + z*m[8];
183 void Math::invert33(float* m, float* out)
185 // Compute the inverse as the adjoint matrix times 1/(det M).
186 // A, B ... I are the cofactors of a b c
189 float a=m[0], b=m[1], c=m[2];
190 float d=m[3], e=m[4], f=m[5];
191 float g=m[6], h=m[7], i=m[8];
193 float A = (e*i - h*f);
194 float B = -(d*i - g*f);
195 float C = (d*h - g*e);
196 float D = -(b*i - h*c);
197 float E = (a*i - g*c);
198 float F = -(a*h - g*b);
199 float G = (b*f - e*c);
200 float H = -(a*f - d*c);
201 float I = (a*e - d*b);
203 float id = 1/(a*A + b*B + c*C);
205 out[0] = id*A; out[1] = id*D; out[2] = id*G;
206 out[3] = id*B; out[4] = id*E; out[5] = id*H;
207 out[6] = id*C; out[7] = id*F; out[8] = id*I;
210 void Math::trans33(float* m, float* out)
212 // 0 1 2 Elements 0, 4, and 8 are the same
213 // 3 4 5 Swap elements 1/3, 2/6, and 5/7
232 void Math::ortho33(float* x, float* y,
233 float* xOut, float* yOut, float* zOut)
240 cross3(xOut, y0, zOut);
242 cross3(zOut, xOut, yOut);
245 }; // namespace yasim