struct vec { union { struct { float x, y, z; }; float v[3]; int i[3]; }; vec() {} vec(float a, float b, float c) : x(a), y(b), z(c) {} vec(float *v) : x(v[0]), y(v[1]), z(v[2]) {} float &operator[](int i) { return v[i]; } float operator[](int i) const { return v[i]; } bool iszero() const { return x==0 && y==0 && z==0; } bool operator==(const vec &o) const { return x == o.x && y == o.y && z == o.z; } bool operator!=(const vec &o) const { return x != o.x || y != o.y || z != o.z; } vec operator-() const { return vec(-x, -y, -z); } vec &mul(float f) { x *= f; y *= f; z *= f; return *this; } vec &div(float f) { x /= f; y /= f; z /= f; return *this; } vec &add(float f) { x += f; y += f; z += f; return *this; } vec &sub(float f) { x -= f; y -= f; z -= f; return *this; } vec &add(const vec &o) { x += o.x; y += o.y; z += o.z; return *this; } vec &sub(const vec &o) { x -= o.x; y -= o.y; z -= o.z; return *this; } float squaredlen() const { return x*x + y*y + z*z; } float dot(const vec &o) const { return x*o.x + y*o.y + z*o.z; } float magnitude() const { return sqrtf(squaredlen()); } vec &normalize() { div(magnitude()); return *this; } float dist(const vec &e) const { vec t; return dist(e, t); } float dist(const vec &e, vec &t) const { t = *this; t.sub(e); return t.magnitude(); } float distxy(const vec &e) const { float dx = e.x - x, dy = e.y - y; return sqrtf(dx*dx + dy*dy); } float magnitudexy() const { return sqrtf(x*x + y*y); } bool reject(const vec &o, float max) const { return x>o.x+max || xo.y+max || y fabs(d.y) ? (fabs(d.x) > fabs(d.z) ? 0 : 2) : (fabs(d.y) > fabs(d.z) ? 1 : 2); v[i] = d[(i+1)%3]; v[(i+1)%3] = -d[i]; v[(i+2)%3] = 0; } }; static inline bool htcmp(const vec &x, const vec &y) { return x == y; } static inline uint hthash(const vec &k) { return k.i[0]^k.i[1]^k.i[2]; } struct vec4 { union { struct { float x, y, z, w; }; float v[4]; }; vec4() {} explicit vec4(const vec &p, float w = 0) : x(p.x), y(p.y), z(p.z), w(w) {} vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {} float &operator[](int i) { return v[i]; } float operator[](int i) const { return v[i]; } }; struct ivec { union { struct { int x, y, z; }; int v[3]; }; ivec() {} ivec(const vec &v) : x(int(v.x)), y(int(v.y)), z(int(v.z)) {} ivec(int a, int b, int c) : x(a), y(b), z(c) {} vec tovec() const { return vec(x, y, z); } int &operator[](int i) { return v[i]; } int operator[](int i) const { return v[i]; } bool operator==(const ivec &v) const { return x==v.x && y==v.y && z==v.z; } bool operator!=(const ivec &v) const { return x!=v.x || y!=v.y || z!=v.z; } ivec &mul(int n) { x *= n; y *= n; z *= n; return *this; } ivec &div(int n) { x /= n; y /= n; z /= n; return *this; } ivec &add(int n) { x += n; y += n; z += n; return *this; } ivec &sub(int n) { x -= n; y -= n; z -= n; return *this; } ivec &add(const ivec &v) { x += v.x; y += v.y; z += v.z; return *this; } ivec &sub(const ivec &v) { x -= v.x; y -= v.y; z -= v.z; return *this; } ivec &mask(int n) { x &= n; y &= n; z &= n; return *this; } ivec &cross(const ivec &a, const ivec &b) { x = a.y*b.z-a.z*b.y; y = a.z*b.x-a.x*b.z; z = a.x*b.y-a.y*b.x; return *this; } int dot(const ivec &o) const { return x*o.x + y*o.y + z*o.z; } }; static inline bool htcmp(const ivec &x, const ivec &y) { return x == y; } static inline uint hthash(const ivec &k) { return k.x^k.y^k.z; } #ifndef STANDALONE struct glmatrixf { GLfloat v[16]; #define ROTVEC(A, B) \ { \ float a = A, b = B; \ A = a*c + b*s; \ B = b*c - a*s; \ } void rotate_around_x(float angle) { float c = cosf(angle), s = sinf(angle); ROTVEC(v[4], v[8]); ROTVEC(v[5], v[9]); ROTVEC(v[6], v[10]); } void rotate_around_y(float angle) { float c = cosf(angle), s = sinf(angle); ROTVEC(v[8], v[0]); ROTVEC(v[9], v[1]); ROTVEC(v[10], v[2]); } void rotate_around_z(float angle) { float c = cosf(angle), s = sinf(angle); ROTVEC(v[0], v[4]); ROTVEC(v[1], v[5]); ROTVEC(v[2], v[6]); } #undef ROTVEC #define MULMAT(row, col) \ v[col + row] = x[row]*y[col] + x[row + 4]*y[col + 1] + x[row + 8]*y[col + 2] + x[row + 12]*y[col + 3]; template void mul(const XT x[16], const YT y[16]) { MULMAT(0, 0); MULMAT(1, 0); MULMAT(2, 0); MULMAT(3, 0); MULMAT(0, 4); MULMAT(1, 4); MULMAT(2, 4); MULMAT(3, 4); MULMAT(0, 8); MULMAT(1, 8); MULMAT(2, 8); MULMAT(3, 8); MULMAT(0, 12); MULMAT(1, 12); MULMAT(2, 12); MULMAT(3, 12); } #undef MULMAT void mul(const glmatrixf &x, const glmatrixf &y) { mul(x.v, y.v); } void identity() { static const GLfloat m[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; memcpy(v, m, sizeof(v)); } void translate(float x, float y, float z) { v[12] += x; v[13] += y; v[14] += z; } void translate(const vec &o) { translate(o.x, o.y, o.z); } void scale(float x, float y, float z) { v[0] *= x; v[1] *= x; v[2] *= x; v[3] *= x; v[4] *= y; v[5] *= y; v[6] *= y; v[7] *= y; v[8] *= z; v[9] *= z; v[10] *= z; v[11] *= z; } void invertnormal(vec &dir) const { vec n(dir); dir.x = n.x*v[0] + n.y*v[1] + n.z*v[2]; dir.y = n.x*v[4] + n.y*v[5] + n.z*v[6]; dir.z = n.x*v[8] + n.y*v[9] + n.z*v[10]; } void invertvertex(vec &pos) const { vec p(pos); p.x -= v[12]; p.y -= v[13]; p.z -= v[14]; pos.x = p.x*v[0] + p.y*v[1] + p.z*v[2]; pos.y = p.x*v[4] + p.y*v[5] + p.z*v[6]; pos.z = p.x*v[8] + p.y*v[9] + p.z*v[10]; } void transform(const vec &in, vec4 &out) const { out.x = in.x*v[0] + in.y*v[4] + in.z*v[8] + v[12]; out.y = in.x*v[1] + in.y*v[5] + in.z*v[9] + v[13]; out.z = in.x*v[2] + in.y*v[6] + in.z*v[10] + v[14]; out.w = in.x*v[3] + in.y*v[7] + in.z*v[11] + v[15]; } vec gettranslation() const { return vec(v[12], v[13], v[14]); } }; #endif