5
struct { float x, y, z; };
11
vec(float a, float b, float c) : x(a), y(b), z(c) {}
12
vec(float *v) : x(v[0]), y(v[1]), z(v[2]) {}
14
float &operator[](int i) { return v[i]; }
15
float operator[](int i) const { return v[i]; }
17
bool iszero() const { return x==0 && y==0 && z==0; }
19
bool operator==(const vec &o) const { return x == o.x && y == o.y && z == o.z; }
20
bool operator!=(const vec &o) const { return x != o.x || y != o.y || z != o.z; }
21
vec operator-() const { return vec(-x, -y, -z); }
23
vec &mul(float f) { x *= f; y *= f; z *= f; return *this; }
24
vec &div(float f) { x /= f; y /= f; z /= f; return *this; }
25
vec &add(float f) { x += f; y += f; z += f; return *this; }
26
vec &sub(float f) { x -= f; y -= f; z -= f; return *this; }
28
vec &add(const vec &o) { x += o.x; y += o.y; z += o.z; return *this; }
29
vec &sub(const vec &o) { x -= o.x; y -= o.y; z -= o.z; return *this; }
31
float squaredlen() const { return x*x + y*y + z*z; }
32
float dot(const vec &o) const { return x*o.x + y*o.y + z*o.z; }
34
float magnitude() const { return sqrtf(squaredlen()); }
35
vec &normalize() { div(magnitude()); return *this; }
37
float dist(const vec &e) const { vec t; return dist(e, t); }
38
float dist(const vec &e, vec &t) const { t = *this; t.sub(e); return t.magnitude(); }
40
float distxy(const vec &e) const { float dx = e.x - x, dy = e.y - y; return sqrtf(dx*dx + dy*dy); }
41
float magnitudexy() const { return sqrtf(x*x + y*y); }
43
bool reject(const vec &o, float max) const { return x>o.x+max || x<o.x-max || y>o.y+max || y<o.y-max; }
45
vec &cross(const vec &a, const vec &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; }
47
void rotate_around_z(float angle) { *this = vec(cosf(angle)*x-sinf(angle)*y, cosf(angle)*y+sinf(angle)*x, z); }
48
void rotate_around_x(float angle) { *this = vec(x, cosf(angle)*y-sinf(angle)*z, cosf(angle)*z+sinf(angle)*y); }
49
void rotate_around_y(float angle) { *this = vec(cosf(angle)*x-sinf(angle)*z, y, cosf(angle)*z+sinf(angle)*x); }
51
vec &rotate(float angle, const vec &d)
53
float c = cosf(angle), s = sinf(angle);
54
return rotate(c, s, d);
57
vec &rotate(float c, float s, const vec &d)
59
*this = vec(x*(d.x*d.x*(1-c)+c) + y*(d.x*d.y*(1-c)-d.z*s) + z*(d.x*d.z*(1-c)+d.y*s),
60
x*(d.y*d.x*(1-c)+d.z*s) + y*(d.y*d.y*(1-c)+c) + z*(d.y*d.z*(1-c)-d.x*s),
61
x*(d.x*d.z*(1-c)-d.y*s) + y*(d.y*d.z*(1-c)+d.x*s) + z*(d.z*d.z*(1-c)+c));
65
void orthogonal(const vec &d)
67
int i = fabs(d.x) > fabs(d.y) ? (fabs(d.x) > fabs(d.z) ? 0 : 2) : (fabs(d.y) > fabs(d.z) ? 1 : 2);
74
static inline bool htcmp(const vec &x, const vec &y)
79
static inline uint hthash(const vec &k)
81
return k.i[0]^k.i[1]^k.i[2];
88
struct { float x, y, z, w; };
93
explicit vec4(const vec &p, float w = 0) : x(p.x), y(p.y), z(p.z), w(w) {}
94
vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
96
float &operator[](int i) { return v[i]; }
97
float operator[](int i) const { return v[i]; }
104
struct { int x, y, z; };
109
ivec(const vec &v) : x(int(v.x)), y(int(v.y)), z(int(v.z)) {}
110
ivec(int a, int b, int c) : x(a), y(b), z(c) {}
112
vec tovec() const { return vec(x, y, z); }
114
int &operator[](int i) { return v[i]; }
115
int operator[](int i) const { return v[i]; }
117
bool operator==(const ivec &v) const { return x==v.x && y==v.y && z==v.z; }
118
bool operator!=(const ivec &v) const { return x!=v.x || y!=v.y || z!=v.z; }
119
ivec &mul(int n) { x *= n; y *= n; z *= n; return *this; }
120
ivec &div(int n) { x /= n; y /= n; z /= n; return *this; }
121
ivec &add(int n) { x += n; y += n; z += n; return *this; }
122
ivec &sub(int n) { x -= n; y -= n; z -= n; return *this; }
123
ivec &add(const ivec &v) { x += v.x; y += v.y; z += v.z; return *this; }
124
ivec &sub(const ivec &v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
125
ivec &mask(int n) { x &= n; y &= n; z &= n; return *this; }
126
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; }
127
int dot(const ivec &o) const { return x*o.x + y*o.y + z*o.z; }
130
static inline bool htcmp(const ivec &x, const ivec &y)
135
static inline uint hthash(const ivec &k)
144
struct { uchar x, y, z; };
149
bvec(uchar x, uchar y, uchar z) : x(x), y(y), z(z) {}
150
bvec(const vec &v) : x((uchar)((v.x+1)*255/2)), y((uchar)((v.y+1)*255/2)), z((uchar)((v.z+1)*255/2)) {}
152
uchar &operator[](int i) { return v[i]; }
153
uchar operator[](int i) const { return v[i]; }
155
bool operator==(const bvec &v) const { return x==v.x && y==v.y && z==v.z; }
156
bool operator!=(const bvec &v) const { return x!=v.x || y!=v.y || z!=v.z; }
158
bool iszero() const { return x==0 && y==0 && z==0; }
160
vec tovec() const { return vec(x*(2.0f/255.0f)-1.0f, y*(2.0f/255.0f)-1.0f, z*(2.0f/255.0f)-1.0f); }
167
float operator[](int i) const { return v[i]; }
168
float &operator[](int i) { return v[i]; }
170
#define ROTVEC(A, B) \
172
float a = A, b = B; \
177
void rotate_around_x(float angle)
179
float c = cosf(angle), s = sinf(angle);
185
void rotate_around_y(float angle)
187
float c = cosf(angle), s = sinf(angle);
193
void rotate_around_z(float angle)
195
float c = cosf(angle), s = sinf(angle);
203
#define MULMAT(row, col) \
204
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];
206
template<class XT, class YT>
207
void mul(const XT x[16], const YT y[16])
209
MULMAT(0, 0); MULMAT(1, 0); MULMAT(2, 0); MULMAT(3, 0);
210
MULMAT(0, 4); MULMAT(1, 4); MULMAT(2, 4); MULMAT(3, 4);
211
MULMAT(0, 8); MULMAT(1, 8); MULMAT(2, 8); MULMAT(3, 8);
212
MULMAT(0, 12); MULMAT(1, 12); MULMAT(2, 12); MULMAT(3, 12);
217
void mul(const glmatrixf &x, const glmatrixf &y)
224
static const float m[16] =
231
memcpy(v, m, sizeof(v));
234
void translate(float x, float y, float z)
241
void translate(const vec &o)
243
translate(o.x, o.y, o.z);
246
void scale(float x, float y, float z)
248
v[0] *= x; v[1] *= x; v[2] *= x; v[3] *= x;
249
v[4] *= y; v[5] *= y; v[6] *= y; v[7] *= y;
250
v[8] *= z; v[9] *= z; v[10] *= z; v[11] *= z;
255
loopi(2) loopj(4) v[i + j*4] = 0.5f*(v[i + j*4] + v[3 + j*4]);
258
void invertnormal(vec &dir) const
261
dir.x = n.x*v[0] + n.y*v[1] + n.z*v[2];
262
dir.y = n.x*v[4] + n.y*v[5] + n.z*v[6];
263
dir.z = n.x*v[8] + n.y*v[9] + n.z*v[10];
266
void invertvertex(vec &pos) const
272
pos.x = p.x*v[0] + p.y*v[1] + p.z*v[2];
273
pos.y = p.x*v[4] + p.y*v[5] + p.z*v[6];
274
pos.z = p.x*v[8] + p.y*v[9] + p.z*v[10];
277
float transformx(const vec &p) const
279
return p.x*v[0] + p.y*v[4] + p.z*v[8] + v[12];
282
float transformy(const vec &p) const
284
return p.x*v[1] + p.y*v[5] + p.z*v[9] + v[13];
287
float transformz(const vec &p) const
289
return p.x*v[2] + p.y*v[6] + p.z*v[10] + v[14];
292
float transformw(const vec &p) const
294
return p.x*v[3] + p.y*v[7] + p.z*v[11] + v[15];
297
void transform(const vec &in, vec4 &out) const
299
out.x = transformx(in);
300
out.y = transformy(in);
301
out.z = transformz(in);
302
out.w = transformw(in);
305
vec gettranslation() const
307
return vec(v[12], v[13], v[14]);
310
float determinant() const;
311
void adjoint(const glmatrixf &m);
312
bool invert(const glmatrixf &m, float mindet = 1.0e-10f);