| 1 | #ifndef _MATH_HPP |
| 2 | #define _MATH_HPP |
| 3 | |
| 4 | #include <cmath> |
| 5 | #include <vector> |
| 6 | |
| 7 | namespace yasim { |
| 8 | |
| 9 | class Math |
| 10 | { |
| 11 | public: |
| 12 | // Dumb utilities |
| 13 | static inline float clamp(float val, float min, float max) { |
| 14 | if(val < min) return min; |
| 15 | if(val > max) return max; |
| 16 | return val; |
| 17 | } |
| 18 | static float weightedMean(float x1, float w1, float x2, float w2) { |
| 19 | return (x1*w1 + x2*w2)/(w1+w2); |
| 20 | } |
| 21 | |
| 22 | // calc polynomial |
| 23 | static float polynomial(const std::vector<float>& coefficients, float x) { |
| 24 | float powX {1}; // init = x^0 = 1 |
| 25 | float result {0}; |
| 26 | for (auto &coeff : coefficients) { |
| 27 | result += coeff * powX; |
| 28 | powX *= x; |
| 29 | } |
| 30 | return result; |
| 31 | } |
| 32 | |
| 33 | // Simple wrappers around library routines |
| 34 | static inline float abs(float f) { return (float)::fabs(f); } |
| 35 | static inline float sqrt(float f) { return (float)::sqrt(f); } |
| 36 | static inline float ceil(float f) { return (float)::ceil(f); } |
| 37 | static inline float sin(float f) { return (float)::sin(f); } |
| 38 | static inline float cos(float f) { return (float)::cos(f); } |
| 39 | static inline float tan(float f) { return (float)::tan(f); } |
| 40 | static inline float atan(float f) { return (float)::atan(f); } |
| 41 | static inline float atan2(float y, float x) { return (float)::atan2(y,x); } |
| 42 | static inline float asin(float f) { return (float)::asin(f); } |
| 43 | static inline float acos(float f) { return (float)::acos(f); } |
| 44 | static inline float exp(float f) { return (float)::exp(f); } |
| 45 | static inline float sqr(float f) { return f*f; } |
| 46 | |
| 47 | // Takes two args and runs afoul of the Koenig rules. |
| 48 | static inline float pow(double base, double exp) { return (float)::pow(base, exp); } |
| 49 | |
| 50 | // double variants of the above |
| 51 | static inline double abs(double f) { return ::fabs(f); } |
| 52 | static inline double sqrt(double f) { return ::sqrt(f); } |
| 53 | static inline double ceil(double f) { return ::ceil(f); } |
| 54 | static inline double sin(double f) { return ::sin(f); } |
| 55 | static inline double cos(double f) { return ::cos(f); } |
| 56 | static inline double tan(double f) { return ::tan(f); } |
| 57 | static inline double atan2(double y, double x) { return ::atan2(y,x); } |
| 58 | static inline double floor(double x) { return ::floor(x); } |
| 59 | |
| 60 | // Some 3D vector stuff. In all cases, it is permissible for the |
| 61 | // "out" vector to be the same as one of the inputs. |
| 62 | static inline void set3(const float* v, float* out) { |
| 63 | out[0] = v[0]; |
| 64 | out[1] = v[1]; |
| 65 | out[2] = v[2]; |
| 66 | } |
| 67 | static inline void set3(const double* v, double* out) { |
| 68 | out[0] = v[0]; |
| 69 | out[1] = v[1]; |
| 70 | out[2] = v[2]; |
| 71 | } |
| 72 | |
| 73 | static void zero3(float* out) { out[0] = out[1] = out[2] = 0; } |
| 74 | static void zero3(double* out) { out[0] = out[1] = out[2] = 0; } |
| 75 | |
| 76 | static inline float dot3(const float* a, const float* b) { |
| 77 | return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]; |
| 78 | } |
| 79 | |
| 80 | static inline void cross3(const float* a, const float* b, float* out) { |
| 81 | float ax=a[0], ay=a[1], az=a[2]; |
| 82 | float bx=b[0], by=b[1], bz=b[2]; |
| 83 | out[0] = ay*bz - by*az; |
| 84 | out[1] = az*bx - bz*ax; |
| 85 | out[2] = ax*by - bx*ay; |
| 86 | } |
| 87 | |
| 88 | static inline void mul3(const float scalar, const float* v, float* out) |
| 89 | { |
| 90 | out[0] = scalar * v[0]; |
| 91 | out[1] = scalar * v[1]; |
| 92 | out[2] = scalar * v[2]; |
| 93 | } |
| 94 | |
| 95 | static inline void add3(const float* a, const float* b, float* out){ |
| 96 | out[0] = a[0] + b[0]; |
| 97 | out[1] = a[1] + b[1]; |
| 98 | out[2] = a[2] + b[2]; |
| 99 | } |
| 100 | |
| 101 | static inline void sub3(const float* a, const float* b, float* out) { |
| 102 | out[0] = a[0] - b[0]; |
| 103 | out[1] = a[1] - b[1]; |
| 104 | out[2] = a[2] - b[2]; |
| 105 | } |
| 106 | |
| 107 | static inline float mag3(const float* v) { |
| 108 | return sqrt(dot3(v, v)); |
| 109 | } |
| 110 | |
| 111 | static inline void unit3(const float* v, float* out) { |
| 112 | float imag = 1/mag3(v); |
| 113 | mul3(imag, v, out); |
| 114 | } |
| 115 | |
| 116 | // Matrix array convention: 0 1 2 |
| 117 | // 3 4 5 |
| 118 | // 6 7 8 |
| 119 | |
| 120 | static inline void set33(float* a, float* b) |
| 121 | { |
| 122 | for (int i=0; i < 9; i++) |
| 123 | a[i] = b[i]; |
| 124 | } |
| 125 | |
| 126 | static inline void identity33(double* a) |
| 127 | { |
| 128 | for(int y = 0; y < 3; y++) { |
| 129 | for(int x = 0; x < 3; x++) { |
| 130 | a[y*3 + x] = ((x == y) ? 1.0 : 0.0); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Multiply two matrices |
| 136 | static void mmul33(const float* a, const float* b, float* out) { |
| 137 | float tmp[9]; |
| 138 | tmp[0] = a[0]*b[0] + a[1]*b[3] + a[2]*b[6]; |
| 139 | tmp[3] = a[3]*b[0] + a[4]*b[3] + a[5]*b[6]; |
| 140 | tmp[6] = a[6]*b[0] + a[7]*b[3] + a[8]*b[6]; |
| 141 | |
| 142 | tmp[1] = a[0]*b[1] + a[1]*b[4] + a[2]*b[7]; |
| 143 | tmp[4] = a[3]*b[1] + a[4]*b[4] + a[5]*b[7]; |
| 144 | tmp[7] = a[6]*b[1] + a[7]*b[4] + a[8]*b[7]; |
| 145 | |
| 146 | tmp[2] = a[0]*b[2] + a[1]*b[5] + a[2]*b[8]; |
| 147 | tmp[5] = a[3]*b[2] + a[4]*b[5] + a[5]*b[8]; |
| 148 | tmp[8] = a[6]*b[2] + a[7]*b[5] + a[8]*b[8]; |
| 149 | |
| 150 | for(int i=0; i<9; ++i) |
| 151 | out[i] = tmp[i]; |
| 152 | } |
| 153 | |
| 154 | // Multiply by vector |
| 155 | static inline void vmul33(const float* m, const float* v, float* out) { |
| 156 | float x = v[0], y = v[1], z = v[2]; |
| 157 | out[0] = x*m[0] + y*m[1] + z*m[2]; |
| 158 | out[1] = x*m[3] + y*m[4] + z*m[5]; |
| 159 | out[2] = x*m[6] + y*m[7] + z*m[8]; |
| 160 | } |
| 161 | |
| 162 | static inline void vmul33(float* m, double* v, double* out) { |
| 163 | double x = v[0], y = v[1], z = v[2]; |
| 164 | out[0] = x*m[0] + y*m[1] + z*m[2]; |
| 165 | out[1] = x*m[3] + y*m[4] + z*m[5]; |
| 166 | out[2] = x*m[6] + y*m[7] + z*m[8]; |
| 167 | } |
| 168 | |
| 169 | static inline void vmul33(double* m, double* v, double* out) { |
| 170 | double x = v[0], y = v[1], z = v[2]; |
| 171 | out[0] = x*m[0] + y*m[1] + z*m[2]; |
| 172 | out[1] = x*m[3] + y*m[4] + z*m[5]; |
| 173 | out[2] = x*m[6] + y*m[7] + z*m[8]; |
| 174 | } |
| 175 | |
| 176 | // Multiply the vector by the matrix transpose. Or pre-multiply the |
| 177 | // matrix by v as a row vector. Same thing. |
| 178 | static inline void tmul33(const float* m, const float* v, float* out) { |
| 179 | float x = v[0], y = v[1], z = v[2]; |
| 180 | out[0] = x*m[0] + y*m[3] + z*m[6]; |
| 181 | out[1] = x*m[1] + y*m[4] + z*m[7]; |
| 182 | out[2] = x*m[2] + y*m[5] + z*m[8]; |
| 183 | } |
| 184 | |
| 185 | static inline void tmul33(float* m, double* v, double* out) { |
| 186 | double 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 | static inline void tmul33(double* m, double* v, double* out) { |
| 193 | double x = v[0], y = v[1], z = v[2]; |
| 194 | out[0] = x*m[0] + y*m[3] + z*m[6]; |
| 195 | out[1] = x*m[1] + y*m[4] + z*m[7]; |
| 196 | out[2] = x*m[2] + y*m[5] + z*m[8]; |
| 197 | } |
| 198 | |
| 199 | /// Invert symmetric matrix; ~1/3 less calculations due to symmetry |
| 200 | static void invert33_sym(const float* m, float* out) { |
| 201 | // Compute the inverse as the adjoint matrix times 1/(det M). |
| 202 | // A, B ... I are the cofactors of a b c |
| 203 | // d e f |
| 204 | // g h i |
| 205 | // symetric: d=b, g=c, h=f |
| 206 | float a=m[0], b=m[1], c=m[2]; |
| 207 | float e=m[4], f=m[5]; |
| 208 | float i=m[8]; |
| 209 | |
| 210 | float A = (e*i - f*f); |
| 211 | float B = -(b*i - c*f); |
| 212 | float C = (b*f - c*e); |
| 213 | float E = (a*i - c*c); |
| 214 | float F = -(a*f - c*b); |
| 215 | float I = (a*e - b*b); |
| 216 | |
| 217 | float id = 1/(a*A + b*B + c*C); |
| 218 | |
| 219 | out[0] = id*A; out[1] = id*B; out[2] = id*C; |
| 220 | out[3] = out[1]; out[4] = id*E; out[5] = id*F; |
| 221 | out[6] = out[2]; out[7] = out[5]; out[8] = id*I; |
| 222 | } |
| 223 | |
| 224 | // Transpose matrix (for an orthonormal orientation matrix, this |
| 225 | // is the same as the inverse). |
| 226 | static inline void trans33(const float* m, float* out) { |
| 227 | // 0 1 2 Elements 0, 4, and 8 are the same |
| 228 | // 3 4 5 Swap elements 1/3, 2/6, and 5/7 |
| 229 | // 6 7 8 |
| 230 | out[0] = m[0]; |
| 231 | out[4] = m[4]; |
| 232 | out[8] = m[8]; |
| 233 | |
| 234 | float tmp = m[1]; |
| 235 | out[1] = m[3]; |
| 236 | out[3] = tmp; |
| 237 | |
| 238 | tmp = m[2]; |
| 239 | out[2] = m[6]; |
| 240 | out[6] = tmp; |
| 241 | |
| 242 | tmp = m[5]; |
| 243 | out[5] = m[7]; |
| 244 | out[7] = tmp; |
| 245 | } |
| 246 | |
| 247 | // Generates an orthonormal basis: |
| 248 | // xOut becomes the unit vector in the direction of x |
| 249 | // yOut is perpendicular to xOut in the x/y plane |
| 250 | // zOut becomes the unit vector: (xOut cross yOut) |
| 251 | static void ortho33(const float* x, const float* y, |
| 252 | float* xOut, float* yOut, float* zOut) { |
| 253 | float x0[3], y0[3]; |
| 254 | set3(x, x0); |
| 255 | set3(y, y0); |
| 256 | |
| 257 | unit3(x0, xOut); |
| 258 | cross3(xOut, y0, zOut); |
| 259 | unit3(zOut, zOut); |
| 260 | cross3(zOut, xOut, yOut); |
| 261 | } |
| 262 | }; |
| 263 | |
| 264 | }; // namespace yasim |
| 265 | #endif // _MATH_HPP |