| 1 |
|
| 2 |
// vmath.h [pengine] |
| 3 |
|
| 4 |
// Copyright 2004-2006 Jasmine Langridge, [email protected] |
| 5 |
// License: GPL version 2 (see included gpl.txt) |
| 6 |
|
| 7 |
#ifndef VMATHH |
| 8 |
#define VMATHH |
| 9 |
|
| 10 |
#include <cstdlib> |
| 11 |
#include <math.h> |
| 12 |
|
| 13 |
|
| 14 |
#define PI 3.1415926535897932384626433832795 |
| 15 |
|
| 16 |
#define RADIANS(a) ((a)*PI/180.0) |
| 17 |
#define DEGREES(a) ((a)*180.0/PI) |
| 18 |
|
| 19 |
#define rand01 ((float)rand() / (float)RAND_MAX) |
| 20 |
#define randm11 (2.0f * (float)rand() / (float)RAND_MAX - 1.0f) |
| 21 |
|
| 22 |
#define MAX(a,b) (((a) >= (b)) ? (a) : (b)) |
| 23 |
#define MIN(a,b) (((a) <= (b)) ? (a) : (b)) |
| 24 |
#define CLAMP_UPPER(v,max) v = MIN(v, max) |
| 25 |
#define CLAMP_LOWER(v,min) v = MAX(v, min) |
| 26 |
#define CLAMP(v,min,max) v = MAX(MIN(v, max), min) |
| 27 |
#define INTERP(a,b,f) ((a) + ((b) - (a)) * (f)) |
| 28 |
|
| 29 |
#define SQUARED(x) ((x)*(x)) |
| 30 |
|
| 31 |
#define PULLTOWARD(val,target,delta) (val = (target) + (val-(target)) / (1.0 + (delta))) |
| 32 |
|
| 33 |
#define DEADZONE(val, zone) (val = \ |
| 34 |
(val > (zone)) ? ((val - (zone)) / (1.0 - (zone))) : \ |
| 35 |
((val < (-zone)) ? ((val + (zone)) / (1.0 - (zone))) : 0.0)) |
| 36 |
|
| 37 |
#define RANGEADJUST(val, premin, premax, postmin, postmax) (val = \ |
| 38 |
((val - (premin)) * ((postmax) - (postmin)) / ((premax) - (premin)) + (postmin))) |
| 39 |
|
| 40 |
#define makevec2f(vec) (vec2f(vec.x,vec.y)) |
| 41 |
#define makevec3f(vec) (vec3f(vec.x,vec.y,vec.z)) |
| 42 |
#define makevec4f(vec) (vec4f(vec.x,vec.y,vec.z,vec.w)) |
| 43 |
|
| 44 |
#define makevec4d(vec) (vec4d(vec.x,vec.y,vec.z,vec.w)) |
| 45 |
|
| 46 |
|
| 47 |
template<class T> |
| 48 |
class vec2 |
| 49 |
{ |
| 50 |
public: |
| 51 |
T x,y; |
| 52 |
|
| 53 |
vec2 () { } |
| 54 |
vec2 (T _x, T _y) { x=_x; y=_y; } |
| 55 |
vec2 (const vec2<T> &vec) { x=vec.x; y=vec.y; } |
| 56 |
|
| 57 |
T &operator [] (int i) { return (&x)[i]; } |
| 58 |
operator const T* () const { return &x; } |
| 59 |
operator T* () { return &x; } |
| 60 |
|
| 61 |
vec2<T> &operator = (const vec2<T> &vec) { x=vec.x; y=vec.y; return *this; } |
| 62 |
vec2<T> &operator += (const vec2<T> &vec) { x+=vec.x; y+=vec.y; return *this; } |
| 63 |
vec2<T> &operator -= (const vec2<T> &vec) { x-=vec.x; y-=vec.y; return *this; } |
| 64 |
vec2<T> &operator *= (T factor) { x*=factor; y*=factor; return *this; } |
| 65 |
vec2<T> &operator /= (const T factor) { x/=factor; y/=factor; return *this; } |
| 66 |
|
| 67 |
vec2<T> operator + (const vec2<T> &vec) const { return vec2<T>(x+vec.x, y+vec.y); } |
| 68 |
vec2<T> operator - (const vec2<T> &vec) const { return vec2<T>(x-vec.x, y-vec.y); } |
| 69 |
|
| 70 |
vec2<T> operator * (T factor) const { return vec2<T>(x*factor, y*factor); } |
| 71 |
vec2<T> operator / (T factor) const { return vec2<T>(x/factor, y/factor); } |
| 72 |
|
| 73 |
T dot (const vec2<T> &vec) const { return (x*vec.x + y*vec.y); } |
| 74 |
T operator * (const vec2<T> &vec) const { return dot(vec); } |
| 75 |
|
| 76 |
T lengthsq () const { return (x*x + y*y); } |
| 77 |
T length () const { return (T)sqrt(lengthsq()); } |
| 78 |
void normalize () { |
| 79 |
T len = length(); |
| 80 |
if (len) { |
| 81 |
x /= len; |
| 82 |
y /= len; |
| 83 |
} else { |
| 84 |
*this = vec2<T>((T)1,(T)0); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
static const vec2<T> zero () { return vec2<T>(0,0); } |
| 89 |
|
| 90 |
static vec2<T> rand() { |
| 91 |
vec2<T> ret; |
| 92 |
do { ret=vec2<T>((T)2 * (T)::rand() / (T)RAND_MAX - (T)1, |
| 93 |
(T)2 * (T)::rand() / (T)RAND_MAX - (T)1); } |
| 94 |
while (ret.lengthsq() > (T)1); |
| 95 |
return ret; |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
template<class T> inline vec2<T> operator - (const vec2<T> &vec) { return vec2<T>(-vec.x,-vec.y); } |
| 100 |
|
| 101 |
|
| 102 |
template<class T> |
| 103 |
class vec3 |
| 104 |
{ |
| 105 |
public: |
| 106 |
T x,y,z; |
| 107 |
|
| 108 |
vec3 () { } |
| 109 |
vec3 (T _x, T _y, T _z) { x=_x; y=_y; z=_z; } |
| 110 |
vec3 (const vec3<T> &vec) { x=vec.x; y=vec.y; z=vec.z; } |
| 111 |
vec3 (const T *vecptr) { x=vecptr[0]; y=vecptr[1]; z=vecptr[2]; } |
| 112 |
|
| 113 |
T &operator [] (int i) { return (&x)[i]; } |
| 114 |
operator const T* () const { return &x; } |
| 115 |
operator T* () { return &x; } |
| 116 |
|
| 117 |
//operator vec2<T> () const { return vec2<T>(x,y); } |
| 118 |
|
| 119 |
vec3<T> operator + (const vec3<T> &vec) const { return vec3<T>(x+vec.x, y+vec.y, z+vec.z); } |
| 120 |
vec3<T> operator - (const vec3<T> &vec) const { return vec3<T>(x-vec.x, y-vec.y, z-vec.z); } |
| 121 |
vec3<T> operator * (T factor) const { return vec3<T>(x*factor, y*factor, z*factor); } |
| 122 |
vec3<T> operator / (T divisor) const { |
| 123 |
float factor = divisor != ((T)0) ? ((T)1) / divisor : ((T)0); |
| 124 |
return vec3<T>(x*factor, y*factor, z*factor); |
| 125 |
} |
| 126 |
|
| 127 |
vec3<T> &operator += (const vec3<T> &vec) { *this = *this + vec; return *this; } |
| 128 |
vec3<T> &operator -= (const vec3<T> &vec) { *this = *this - vec; return *this; } |
| 129 |
vec3<T> &operator *= (const T factor) { *this = *this * factor; return *this; } |
| 130 |
vec3<T> &operator /= (const T factor) { *this = *this / factor; return *this; } |
| 131 |
|
| 132 |
|
| 133 |
T dot (const vec3<T> &vec) const { return (x*vec.x + y*vec.y + z*vec.z); } |
| 134 |
T operator * (const vec3<T> &vec) const { return dot(vec); } |
| 135 |
|
| 136 |
vec3<T> cross (const vec3<T> &vec) const { return vec3<T>(y*vec.z-z*vec.y, z*vec.x-x*vec.z, x*vec.y-y*vec.x); } |
| 137 |
vec3<T> operator ^ (const vec3<T> &vec) const { return cross(vec); } |
| 138 |
|
| 139 |
vec3<T> modulate (const vec3<T> &vec) const { return vec3<T>(x*vec.x, y*vec.y, z*vec.z); } |
| 140 |
|
| 141 |
T lengthsq () const { return (x*x + y*y + z*z); } |
| 142 |
T length () const { return (T)sqrt(lengthsq()); } |
| 143 |
void normalize () { |
| 144 |
T len = length(); |
| 145 |
if (len) { |
| 146 |
len = (T)1 / len; |
| 147 |
x *= len; |
| 148 |
y *= len; |
| 149 |
z *= len; |
| 150 |
} else { |
| 151 |
*this = vec3<T>((T)0,(T)0,(T)1); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
void copyTo(T *dest) const { dest[0] = x; dest[1] = y; dest[2] = z; } |
| 156 |
void copyFrom(const T *src) { x = src[0]; y = src[1]; z = src[2]; } |
| 157 |
|
| 158 |
static vec3<T> zero () { return vec3<T>(0,0,0); } |
| 159 |
|
| 160 |
static vec3<T> rand() { |
| 161 |
vec3<T> ret; |
| 162 |
do { ret=vec3<T>((T)2 * (T)::rand() / (T)RAND_MAX - (T)1, |
| 163 |
(T)2 * (T)::rand() / (T)RAND_MAX - (T)1, |
| 164 |
(T)2 * (T)::rand() / (T)RAND_MAX - (T)1); } |
| 165 |
while (ret.lengthsq() > (T)1); |
| 166 |
return ret; |
| 167 |
} |
| 168 |
}; |
| 169 |
|
| 170 |
template<class T> inline vec3<T> operator - (const vec3<T> &vec) { return vec3<T>(-vec.x,-vec.y,-vec.z); } |
| 171 |
|
| 172 |
template<class T> inline vec3<T> operator * (const T &fac, const vec3<T> &vec) { return vec * fac; } |
| 173 |
|
| 174 |
template<class T> |
| 175 |
class vec4 |
| 176 |
{ |
| 177 |
public: |
| 178 |
T x,y,z,w; |
| 179 |
|
| 180 |
vec4 () { } |
| 181 |
vec4 (T _x, T _y, T _z, T _w) { x=_x; y=_y; z=_z; w=_w; } |
| 182 |
vec4 (const vec3<T> &vec) { x=vec.x; y=vec.y; z=vec.z; } |
| 183 |
vec4 (const vec4<T> &vec) { x=vec.x; y=vec.y; z=vec.z; w=vec.w; } |
| 184 |
vec4 (const vec3<T> &vec, T _w) { x=vec.x; y=vec.y; z=vec.z; w=_w; } |
| 185 |
|
| 186 |
T &operator [] (int i) { return (&x)[i]; } |
| 187 |
operator const T* () const { return &x; } |
| 188 |
operator T* () { return &x; } |
| 189 |
|
| 190 |
//operator vec3<T> () const { return vec3<T>(x,y,z); } |
| 191 |
|
| 192 |
vec4<T> operator + (const vec4<T> &vec) const { return vec4<T>(x+vec.x, y+vec.y, z+vec.z, w+vec.w); } |
| 193 |
vec4<T> operator - (const vec4<T> &vec) const { return vec4<T>(x-vec.x, y-vec.y, z-vec.z, w-vec.w); } |
| 194 |
vec4<T> operator * (T factor) const { return vec4<T>(x*factor, y*factor, z*factor, w*factor); } |
| 195 |
|
| 196 |
vec4<T> &operator += (const vec4<T> &vec) { *this = *this + vec; return *this; } |
| 197 |
vec4<T> &operator -= (const vec4<T> &vec) { *this = *this - vec; return *this; } |
| 198 |
vec4<T> &operator *= (const T factor) { *this = *this * factor; return *this; } |
| 199 |
|
| 200 |
T dot (const vec4<T> &vec) const { return (x*vec.x + y*vec.y + z*vec.z + w*vec.w); } |
| 201 |
T operator * (const vec4<T> &vec) const { return dot(vec); } |
| 202 |
|
| 203 |
T lengthsq () const { return (x*x + y*y + z*z + w*w); } |
| 204 |
T length () const { return sqrt(lengthsq()); } |
| 205 |
void normalize3a () { |
| 206 |
T len = sqrt(x*x + y*y + z*z); |
| 207 |
if (len) { |
| 208 |
len = (T)1 / len; |
| 209 |
x *= len; |
| 210 |
y *= len; |
| 211 |
z *= len; |
| 212 |
w *= len; |
| 213 |
} else { |
| 214 |
*this = vec4<T>((T)0,(T)0,(T)1,(T)0); |
| 215 |
} |
| 216 |
} |
| 217 |
void normalize3b () { |
| 218 |
if (w) { |
| 219 |
T len = (T)1 / w; |
| 220 |
x *= len; |
| 221 |
y *= len; |
| 222 |
z *= len; |
| 223 |
w = (T)1; |
| 224 |
} else { |
| 225 |
*this = vec4<T>((T)0,(T)0,(T)0,(T)1); |
| 226 |
} |
| 227 |
} |
| 228 |
void normalize3c () { |
| 229 |
T len = sqrt(x*x + y*y + z*z); |
| 230 |
if (len) { |
| 231 |
len = (T)1 / len; |
| 232 |
x *= len; |
| 233 |
y *= len; |
| 234 |
z *= len; |
| 235 |
} else { |
| 236 |
x = (T)0; |
| 237 |
y = (T)0; |
| 238 |
z = (T)1; |
| 239 |
} |
| 240 |
} |
| 241 |
void normalize4 () { |
| 242 |
T len = length(); |
| 243 |
if (len) { |
| 244 |
len = (T)1 / len; |
| 245 |
x *= len; |
| 246 |
y *= len; |
| 247 |
z *= len; |
| 248 |
w *= len; |
| 249 |
} else { |
| 250 |
*this = vec4<T>((T)0,(T)0,(T)0,(T)1); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
void copyTo(T *dest) const { dest[0] = x; dest[1] = y; dest[2] = z; dest[3] = w; } |
| 255 |
void copyFrom(const T *src) { x = src[0]; y = src[1]; z = src[2]; w = src[3]; } |
| 256 |
}; |
| 257 |
|
| 258 |
template<class T> |
| 259 |
class ray |
| 260 |
{ |
| 261 |
public: |
| 262 |
vec3<T> start; |
| 263 |
vec3<T> direction; |
| 264 |
|
| 265 |
ray () { } |
| 266 |
ray (const vec3<T> &_start, const vec3<T> &_direction) : start(_start), direction(_direction) { } |
| 267 |
|
| 268 |
void fromTwoPoints(const vec3<T> &_start, const vec3<T> &_pt) { |
| 269 |
start = _start; |
| 270 |
direction = _pt - _start; |
| 271 |
} |
| 272 |
|
| 273 |
vec3<T> getPt(T lambda) const { |
| 274 |
return start + direction * lambda; |
| 275 |
} |
| 276 |
|
| 277 |
bool getSphereIntersection(const vec3<T> ¢er, T radius, T &outLambda) const |
| 278 |
{ |
| 279 |
vec3<T> temp = start - center; |
| 280 |
|
| 281 |
T a = direction.lengthsq(), |
| 282 |
b = 2.0 * (direction * temp), |
| 283 |
c = temp.lengthsq() - radius*radius, |
| 284 |
determinant = b * b - (T)4 * a * c; |
| 285 |
|
| 286 |
if (determinant < (T)0) return false; |
| 287 |
|
| 288 |
T rootdet = sqrt(determinant), |
| 289 |
t = (-b - rootdet) / ((T)2 * a); |
| 290 |
|
| 291 |
outLambda = t; |
| 292 |
return true; |
| 293 |
} |
| 294 |
}; |
| 295 |
|
| 296 |
/// @brief class storing a plane |
| 297 |
/// @details this plane class is of form (x,y,z) . normal + offset = 0 |
| 298 |
/// ie, increasing offset moves plane backwards along its normal |
| 299 |
/// |
| 300 |
template<class T> |
| 301 |
class plane |
| 302 |
{ |
| 303 |
public: |
| 304 |
vec3<T> normal; |
| 305 |
T offset; |
| 306 |
|
| 307 |
plane () { } |
| 308 |
plane (const vec3<T> &_normal, T _offset) : normal(_normal), offset(_offset) { } |
| 309 |
plane (T _a, T _b, T _c, T _d) : normal(_a, _b, _c), offset(_d) { } |
| 310 |
|
| 311 |
void normalize() { |
| 312 |
T len = normal.length(); |
| 313 |
if (len) { |
| 314 |
len = (T)1 / len; |
| 315 |
normal *= len; |
| 316 |
offset *= len; |
| 317 |
} |
| 318 |
} |
| 319 |
}; |
| 320 |
|
| 321 |
/// |
| 322 |
/// @brief class storing a 4x4 matrix |
| 323 |
/// |
| 324 |
template<class T> |
| 325 |
class mat44 |
| 326 |
{ |
| 327 |
public: |
| 328 |
vec4<T> row[4]; |
| 329 |
|
| 330 |
mat44 () { } |
| 331 |
|
| 332 |
T &operator [] (int i) { return row[0][i]; } |
| 333 |
operator T* () { return &row[0].x; } |
| 334 |
operator const T* () const { return &row[0].x; } |
| 335 |
|
| 336 |
mat44<T> &assemble(const vec4<T> &r0, const vec4<T> &r1, const vec4<T> &r2, const vec4<T> &r3) { |
| 337 |
row[0] = r0; row[1] = r1; row[2] = r2; row[3] = r3; |
| 338 |
return *this; |
| 339 |
} |
| 340 |
mat44<T> &assemble(const vec3<T> &r0, const vec3<T> &r1, const vec3<T> &r2) { |
| 341 |
row[0] = vec4<T>(r0.x, r0.y, r0.z, (T)0); |
| 342 |
row[1] = vec4<T>(r1.x, r1.y, r1.z, (T)0); |
| 343 |
row[2] = vec4<T>(r2.x, r2.y, r2.z, (T)0); |
| 344 |
row[3] = vec4<T>((T)0, (T)0, (T)0, (T)1); |
| 345 |
return *this; |
| 346 |
} |
| 347 |
|
| 348 |
mat44<T> concatenate(const mat44<T> &mat) const { |
| 349 |
mat44<T> ret; |
| 350 |
ret.row[0][0] = row[0][0]*mat.row[0][0] + row[0][1]*mat.row[1][0] + row[0][2]*mat.row[2][0] + row[0][3]*mat.row[3][0]; |
| 351 |
ret.row[0][1] = row[0][0]*mat.row[0][1] + row[0][1]*mat.row[1][1] + row[0][2]*mat.row[2][1] + row[0][3]*mat.row[3][1]; |
| 352 |
ret.row[0][2] = row[0][0]*mat.row[0][2] + row[0][1]*mat.row[1][2] + row[0][2]*mat.row[2][2] + row[0][3]*mat.row[3][2]; |
| 353 |
ret.row[0][3] = row[0][0]*mat.row[0][3] + row[0][1]*mat.row[1][3] + row[0][2]*mat.row[2][3] + row[0][3]*mat.row[3][3]; |
| 354 |
ret.row[1][0] = row[1][0]*mat.row[0][0] + row[1][1]*mat.row[1][0] + row[1][2]*mat.row[2][0] + row[1][3]*mat.row[3][0]; |
| 355 |
ret.row[1][1] = row[1][0]*mat.row[0][1] + row[1][1]*mat.row[1][1] + row[1][2]*mat.row[2][1] + row[1][3]*mat.row[3][1]; |
| 356 |
ret.row[1][2] = row[1][0]*mat.row[0][2] + row[1][1]*mat.row[1][2] + row[1][2]*mat.row[2][2] + row[1][3]*mat.row[3][2]; |
| 357 |
ret.row[1][3] = row[1][0]*mat.row[0][3] + row[1][1]*mat.row[1][3] + row[1][2]*mat.row[2][3] + row[1][3]*mat.row[3][3]; |
| 358 |
ret.row[2][0] = row[2][0]*mat.row[0][0] + row[2][1]*mat.row[1][0] + row[2][2]*mat.row[2][0] + row[2][3]*mat.row[3][0]; |
| 359 |
ret.row[2][1] = row[2][0]*mat.row[0][1] + row[2][1]*mat.row[1][1] + row[2][2]*mat.row[2][1] + row[2][3]*mat.row[3][1]; |
| 360 |
ret.row[2][2] = row[2][0]*mat.row[0][2] + row[2][1]*mat.row[1][2] + row[2][2]*mat.row[2][2] + row[2][3]*mat.row[3][2]; |
| 361 |
ret.row[2][3] = row[2][0]*mat.row[0][3] + row[2][1]*mat.row[1][3] + row[2][2]*mat.row[2][3] + row[2][3]*mat.row[3][3]; |
| 362 |
ret.row[3][0] = row[3][0]*mat.row[0][0] + row[3][1]*mat.row[1][0] + row[3][2]*mat.row[2][0] + row[3][3]*mat.row[3][0]; |
| 363 |
ret.row[3][1] = row[3][0]*mat.row[0][1] + row[3][1]*mat.row[1][1] + row[3][2]*mat.row[2][1] + row[3][3]*mat.row[3][1]; |
| 364 |
ret.row[3][2] = row[3][0]*mat.row[0][2] + row[3][1]*mat.row[1][2] + row[3][2]*mat.row[2][2] + row[3][3]*mat.row[3][2]; |
| 365 |
ret.row[3][3] = row[3][0]*mat.row[0][3] + row[3][1]*mat.row[1][3] + row[3][2]*mat.row[2][3] + row[3][3]*mat.row[3][3]; |
| 366 |
return ret; |
| 367 |
} |
| 368 |
|
| 369 |
mat44<T> concatenateReverse(const mat44<T> &mat) const { |
| 370 |
return mat.concatenate(*this); |
| 371 |
} |
| 372 |
|
| 373 |
mat44<T> transpose() const { |
| 374 |
mat44<T> ret; |
| 375 |
ret.row[0] = vec4<T>(row[0].x, row[1].x, row[2].x, row[3].x); |
| 376 |
ret.row[1] = vec4<T>(row[0].y, row[1].y, row[2].y, row[3].y); |
| 377 |
ret.row[2] = vec4<T>(row[0].z, row[1].z, row[2].z, row[3].z); |
| 378 |
ret.row[3] = vec4<T>(row[0].w, row[1].w, row[2].w, row[3].w); |
| 379 |
return ret; |
| 380 |
} |
| 381 |
|
| 382 |
mat44<T> &translate(const vec3<T> &vec) { |
| 383 |
row[0].w += vec.x; |
| 384 |
row[1].w += vec.y; |
| 385 |
row[2].w += vec.z; |
| 386 |
return *this; |
| 387 |
} |
| 388 |
|
| 389 |
vec3<T> transform1(const vec3<T> &vec) const { |
| 390 |
return vec3<T> ( |
| 391 |
row[0].x * vec.x + row[0].y * vec.y + row[0].z * vec.z + row[0].w, |
| 392 |
row[1].x * vec.x + row[1].y * vec.y + row[1].z * vec.z + row[1].w, |
| 393 |
row[2].x * vec.x + row[2].y * vec.y + row[2].z * vec.z + row[2].w |
| 394 |
); |
| 395 |
} |
| 396 |
|
| 397 |
vec4<T> transform1(const vec4<T> &vec) const { |
| 398 |
return vec4<T> ( |
| 399 |
row[0].x * vec.x + row[0].y * vec.y + row[0].z * vec.z + row[0].w * vec.w, |
| 400 |
row[1].x * vec.x + row[1].y * vec.y + row[1].z * vec.z + row[1].w * vec.w, |
| 401 |
row[2].x * vec.x + row[2].y * vec.y + row[2].z * vec.z + row[2].w * vec.w, |
| 402 |
row[3].x * vec.x + row[3].y * vec.y + row[3].z * vec.z + row[3].w * vec.w |
| 403 |
); |
| 404 |
} |
| 405 |
|
| 406 |
vec3<T> transform2(const vec3<T> &vec) const { |
| 407 |
return vec3<T> ( |
| 408 |
row[0].x * vec.x + row[1].x * vec.y + row[2].x * vec.z + row[3].x, |
| 409 |
row[0].y * vec.x + row[1].y * vec.y + row[2].y * vec.z + row[3].y, |
| 410 |
row[0].z * vec.x + row[1].z * vec.y + row[2].z * vec.z + row[3].z |
| 411 |
); |
| 412 |
} |
| 413 |
|
| 414 |
vec4<T> transform2(const vec4<T> &vec) const { |
| 415 |
return vec4<T> ( |
| 416 |
row[0].x * vec.x + row[1].x * vec.y + row[2].x * vec.z + row[3].x * vec.w, |
| 417 |
row[0].y * vec.x + row[1].y * vec.y + row[2].y * vec.z + row[3].y * vec.w, |
| 418 |
row[0].z * vec.x + row[1].z * vec.y + row[2].z * vec.z + row[3].z * vec.w, |
| 419 |
row[0].w * vec.x + row[1].w * vec.y + row[2].w * vec.z + row[3].w * vec.w |
| 420 |
); |
| 421 |
} |
| 422 |
|
| 423 |
vec3<T> transformNormal(const vec3<T> &vec) const { |
| 424 |
return vec3<T> ( |
| 425 |
row[0].x * vec.x + row[0].y * vec.y + row[0].z * vec.z, |
| 426 |
row[1].x * vec.x + row[1].y * vec.y + row[1].z * vec.z, |
| 427 |
row[2].x * vec.x + row[2].y * vec.y + row[2].z * vec.z |
| 428 |
); |
| 429 |
} |
| 430 |
|
| 431 |
static mat44<T> identity() { |
| 432 |
mat44<T> ret; |
| 433 |
ret.row[0] = vec4<T>(1,0,0,0); |
| 434 |
ret.row[1] = vec4<T>(0,1,0,0); |
| 435 |
ret.row[2] = vec4<T>(0,0,1,0); |
| 436 |
ret.row[3] = vec4<T>(0,0,0,1); |
| 437 |
return ret; |
| 438 |
} |
| 439 |
}; |
| 440 |
|
| 441 |
|
| 442 |
/// |
| 443 |
/// @brief class storing a quaternion |
| 444 |
/// |
| 445 |
template <class T> |
| 446 |
class quat { |
| 447 |
public: |
| 448 |
T x,y,z,w; |
| 449 |
|
| 450 |
public: |
| 451 |
quat() { } |
| 452 |
quat(T _x, T _y, T _z, T _w) : x(_x), y(_y), z(_z), w(_w) { } |
| 453 |
|
| 454 |
quat<T> operator + (const quat<T> &q) const { |
| 455 |
return quat<T>(x+q.x, y+q.y, z+q.z, w+q.w); |
| 456 |
} |
| 457 |
quat<T> operator - (const quat<T> &q) const { |
| 458 |
return quat<T>(x-q.x, y-q.y, z-q.z, w-q.w); |
| 459 |
} |
| 460 |
quat<T> operator * (T val) const { |
| 461 |
return quat<T>(x*val, y*val, z*val, w*val); |
| 462 |
} |
| 463 |
quat<T> operator / (T val) const { |
| 464 |
T mult = (T)1 / val; |
| 465 |
return quat<T>(x*mult, y*mult, z*mult, w*mult); |
| 466 |
} |
| 467 |
|
| 468 |
quat<T> operator * (const quat<T> &q) const { |
| 469 |
return quat<T>( |
| 470 |
w*q.x + x*q.w + y*q.z - z*q.y, |
| 471 |
w*q.y - x*q.z + y*q.w + z*q.x, |
| 472 |
w*q.z + x*q.y - y*q.x + z*q.w, |
| 473 |
w*q.w - x*q.x - y*q.y - z*q.z); |
| 474 |
} |
| 475 |
|
| 476 |
quat<T> &operator += (const quat<T> &q) { |
| 477 |
*this = *this + q; |
| 478 |
return *this; |
| 479 |
} |
| 480 |
// operator *= has to assume an order |
| 481 |
// since quaternion mult is not commutative, it's |
| 482 |
// better to force the user to think about it |
| 483 |
|
| 484 |
float dot(const quat<T> &q) { |
| 485 |
return (x * q.x + y * q.y + z * q.z + w * q.w); |
| 486 |
} |
| 487 |
|
| 488 |
void fromAxisAngle(const vec3<T> &axis, T angle) { |
| 489 |
T len = axis.length(); |
| 490 |
if (len > (T)0) { |
| 491 |
T sinA = sin(angle/(T)2) / len; |
| 492 |
x = sinA * axis.x; |
| 493 |
y = sinA * axis.y; |
| 494 |
z = sinA * axis.z; |
| 495 |
w = cos(angle/(T)2); |
| 496 |
} else { |
| 497 |
*this = identity(); |
| 498 |
} |
| 499 |
} |
| 500 |
void fromXAngle(T angle) { |
| 501 |
x = sin(angle/(T)2); |
| 502 |
y = z = (T)0; |
| 503 |
w = cos(angle/(T)2); |
| 504 |
} |
| 505 |
void fromYAngle(T angle) { |
| 506 |
y = sin(angle/(T)2); |
| 507 |
x = z = (T)0; |
| 508 |
w = cos(angle/(T)2); |
| 509 |
} |
| 510 |
void fromZAngle(T angle) { |
| 511 |
z = sin(angle/(T)2); |
| 512 |
x = y = (T)0; |
| 513 |
w = cos(angle/(T)2); |
| 514 |
} |
| 515 |
void fromThreeAxisAngle(const vec3<T> &vec) { |
| 516 |
quat<T> temp; |
| 517 |
fromXAngle(vec.x); |
| 518 |
temp.fromYAngle(vec.y); |
| 519 |
*this = *this * temp; |
| 520 |
temp.fromZAngle(vec.z); |
| 521 |
*this = *this * temp; |
| 522 |
} |
| 523 |
|
| 524 |
void normalize() { |
| 525 |
T len = x*x + y*y + z*z + w*w; |
| 526 |
if (len > (T)0) { |
| 527 |
len = (T)1 / (T)sqrt(len); |
| 528 |
x *= len; |
| 529 |
y *= len; |
| 530 |
z *= len; |
| 531 |
w *= len; |
| 532 |
} else { |
| 533 |
*this = identity(); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
mat44<T> getMatrix() const { |
| 538 |
T norm = x*x + y*y + z*z + w*w, |
| 539 |
s = (norm > (T)0) ? (T)2/norm : (T)0, |
| 540 |
|
| 541 |
xx = x * x * s, |
| 542 |
yy = y * y * s, |
| 543 |
zz = z * z * s, |
| 544 |
xy = x * y * s, |
| 545 |
xz = x * z * s, |
| 546 |
yz = y * z * s, |
| 547 |
wx = w * x * s, |
| 548 |
wy = w * y * s, |
| 549 |
wz = w * z * s; |
| 550 |
|
| 551 |
mat44<T> m; |
| 552 |
m.assemble( |
| 553 |
vec3<T>((T)1 - (yy + zz), xy + wz, xz - wy), |
| 554 |
vec3<T>(xy - wz, (T)1 - (xx + zz), yz + wx), |
| 555 |
vec3<T>(xz + wy, yz - wx, (T)1 - (xx + yy))); |
| 556 |
|
| 557 |
return m; |
| 558 |
/* |
| 559 |
m.el[0] = (T)1 - (yy + zz); |
| 560 |
m.el[4] = xy + wz; |
| 561 |
m.el[8] = xz - wy; |
| 562 |
|
| 563 |
m.el[1] = xy - wz; |
| 564 |
m.el[5] = (T)1 - (xx + zz); |
| 565 |
m.el[9] = yz + wx; |
| 566 |
|
| 567 |
m.el[2] = xz + wy; |
| 568 |
m.el[6] = yz - wx; |
| 569 |
m.el[10] = (T)1 - (xx + yy); |
| 570 |
|
| 571 |
m.el[12] = m.el[13] = m.el[14] = m.el[3] = m.el[7] = m.el[11] = (T)0; |
| 572 |
m.el[15] = (T)1; |
| 573 |
*/ |
| 574 |
} |
| 575 |
|
| 576 |
static quat<T> identity () { return quat<T>(0,0,0,1); } |
| 577 |
|
| 578 |
static quat<T> rand() { |
| 579 |
quat<T> ret; |
| 580 |
float lengthsq; |
| 581 |
do { ret=quat<T>((T)2 * (T)::rand() / (T)RAND_MAX - (T)1, |
| 582 |
(T)2 * (T)::rand() / (T)RAND_MAX - (T)1, |
| 583 |
(T)2 * (T)::rand() / (T)RAND_MAX - (T)1, |
| 584 |
(T)2 * (T)::rand() / (T)RAND_MAX - (T)1); |
| 585 |
lengthsq = ret.x*ret.x + ret.y*ret.y + ret.z*ret.z + ret.w*ret.w; |
| 586 |
} while (lengthsq > (T)1 || lengthsq <= (T)0); |
| 587 |
float mult = (T)1 / (T)sqrt(lengthsq); |
| 588 |
ret.x *= mult; ret.y *= mult; ret.z *= mult; ret.w *= mult; |
| 589 |
return ret; |
| 590 |
} |
| 591 |
}; |
| 592 |
|
| 593 |
template<class T> |
| 594 |
class frustum { |
| 595 |
public: |
| 596 |
|
| 597 |
enum { |
| 598 |
SideRight, |
| 599 |
SideLeft, |
| 600 |
SideTop, |
| 601 |
SideBottom, |
| 602 |
SideNear, |
| 603 |
SideFar, |
| 604 |
SideCount |
| 605 |
}; |
| 606 |
|
| 607 |
plane<T> side[SideCount]; |
| 608 |
|
| 609 |
public: |
| 610 |
frustum() { } |
| 611 |
frustum(const mat44<T> &mat) { construct(mat); } |
| 612 |
|
| 613 |
frustum<T> &construct(const mat44<T> &mat); |
| 614 |
|
| 615 |
bool isAABBOutside(const vec3<T> &mins, const vec3<T> &maxs) const; |
| 616 |
}; |
| 617 |
|
| 618 |
|
| 619 |
typedef signed char int8; |
| 620 |
typedef unsigned char uint8; |
| 621 |
typedef signed short int16; |
| 622 |
typedef unsigned short uint16; |
| 623 |
typedef signed int int32; |
| 624 |
typedef unsigned int uint32; |
| 625 |
|
| 626 |
typedef vec2<float> vec2f; |
| 627 |
typedef vec2<double> vec2d; |
| 628 |
typedef vec2<int> vec2i; |
| 629 |
typedef vec2<unsigned char> vec2ub; |
| 630 |
|
| 631 |
typedef vec3<float> vec3f; |
| 632 |
typedef vec3<double> vec3d; |
| 633 |
typedef vec3<int> vec3i; |
| 634 |
typedef vec3<unsigned char> vec3ub; |
| 635 |
|
| 636 |
typedef vec4<float> vec4f; |
| 637 |
typedef vec4<double> vec4d; |
| 638 |
typedef vec4<int> vec4i; |
| 639 |
typedef vec4<unsigned char> vec4ub; |
| 640 |
|
| 641 |
typedef quat<float> quatf; |
| 642 |
typedef quat<double> quatd; |
| 643 |
|
| 644 |
typedef ray<float> rayf; |
| 645 |
typedef ray<double> rayd; |
| 646 |
|
| 647 |
typedef plane<float> planef; |
| 648 |
typedef plane<double> planed; |
| 649 |
|
| 650 |
typedef mat44<float> mat44f; |
| 651 |
typedef mat44<double> mat44d; |
| 652 |
|
| 653 |
typedef frustum<float> frustumf; |
| 654 |
typedef frustum<double> frustumd; |
| 655 |
|
| 656 |
template<class T> inline T intersect_plane_ray(const plane<T> &pln, const vec3<T> &linept, const vec3<T> &linedir) |
| 657 |
{ |
| 658 |
T denom = pln.normal * linedir; |
| 659 |
if (denom) { |
| 660 |
return - (pln.normal * linept + pln.offset) / denom; |
| 661 |
} else { |
| 662 |
return (T)0; |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
#endif |