// vmath.h [pengine] // Copyright 2004-2006 Jasmine Langridge, jas@jareiko.net // License: GPL version 2 (see included gpl.txt) #ifndef VMATHH #define VMATHH #include #include #define PI 3.1415926535897932384626433832795 #define RADIANS(a) ((a)*PI/180.0) #define DEGREES(a) ((a)*180.0/PI) #define rand01 ((float)rand() / (float)RAND_MAX) #define randm11 (2.0f * (float)rand() / (float)RAND_MAX - 1.0f) #define MAX(a,b) (((a) >= (b)) ? (a) : (b)) #define MIN(a,b) (((a) <= (b)) ? (a) : (b)) #define CLAMP_UPPER(v,max) v = MIN(v, max) #define CLAMP_LOWER(v,min) v = MAX(v, min) #define CLAMP(v,min,max) v = MAX(MIN(v, max), min) #define INTERP(a,b,f) ((a) + ((b) - (a)) * (f)) #define SQUARED(x) ((x)*(x)) #define PULLTOWARD(val,target,delta) (val = (target) + (val-(target)) / (1.0 + (delta))) #define DEADZONE(val, zone) (val = \ (val > (zone)) ? ((val - (zone)) / (1.0 - (zone))) : \ ((val < (-zone)) ? ((val + (zone)) / (1.0 - (zone))) : 0.0)) #define RANGEADJUST(val, premin, premax, postmin, postmax) (val = \ ((val - (premin)) * ((postmax) - (postmin)) / ((premax) - (premin)) + (postmin))) #define makevec2f(vec) (vec2f(vec.x,vec.y)) #define makevec3f(vec) (vec3f(vec.x,vec.y,vec.z)) #define makevec4f(vec) (vec4f(vec.x,vec.y,vec.z,vec.w)) #define makevec4d(vec) (vec4d(vec.x,vec.y,vec.z,vec.w)) template class vec2 { public: T x,y; vec2 () { } vec2 (T _x, T _y) { x=_x; y=_y; } vec2 (const vec2 &vec) { x=vec.x; y=vec.y; } T &operator [] (int i) { return (&x)[i]; } operator const T* () const { return &x; } operator T* () { return &x; } vec2 &operator = (const vec2 &vec) { x=vec.x; y=vec.y; return *this; } vec2 &operator += (const vec2 &vec) { x+=vec.x; y+=vec.y; return *this; } vec2 &operator -= (const vec2 &vec) { x-=vec.x; y-=vec.y; return *this; } vec2 &operator *= (T factor) { x*=factor; y*=factor; return *this; } vec2 &operator /= (const T factor) { x/=factor; y/=factor; return *this; } vec2 operator + (const vec2 &vec) const { return vec2(x+vec.x, y+vec.y); } vec2 operator - (const vec2 &vec) const { return vec2(x-vec.x, y-vec.y); } vec2 operator * (T factor) const { return vec2(x*factor, y*factor); } vec2 operator / (T factor) const { return vec2(x/factor, y/factor); } T dot (const vec2 &vec) const { return (x*vec.x + y*vec.y); } T operator * (const vec2 &vec) const { return dot(vec); } T lengthsq () const { return (x*x + y*y); } T length () const { return (T)sqrt(lengthsq()); } void normalize () { T len = length(); if (len) { x /= len; y /= len; } else { *this = vec2((T)1,(T)0); } } static const vec2 zero () { return vec2(0,0); } static vec2 rand() { vec2 ret; do { ret=vec2((T)2 * (T)::rand() / (T)RAND_MAX - (T)1, (T)2 * (T)::rand() / (T)RAND_MAX - (T)1); } while (ret.lengthsq() > (T)1); return ret; } }; template inline vec2 operator - (const vec2 &vec) { return vec2(-vec.x,-vec.y); } template class vec3 { public: T x,y,z; vec3 () { } vec3 (T _x, T _y, T _z) { x=_x; y=_y; z=_z; } vec3 (const vec3 &vec) { x=vec.x; y=vec.y; z=vec.z; } vec3 (const T *vecptr) { x=vecptr[0]; y=vecptr[1]; z=vecptr[2]; } T &operator [] (int i) { return (&x)[i]; } operator const T* () const { return &x; } operator T* () { return &x; } //operator vec2 () const { return vec2(x,y); } vec3 operator + (const vec3 &vec) const { return vec3(x+vec.x, y+vec.y, z+vec.z); } vec3 operator - (const vec3 &vec) const { return vec3(x-vec.x, y-vec.y, z-vec.z); } vec3 operator * (T factor) const { return vec3(x*factor, y*factor, z*factor); } vec3 operator / (T divisor) const { float factor = divisor != ((T)0) ? ((T)1) / divisor : ((T)0); return vec3(x*factor, y*factor, z*factor); } vec3 &operator += (const vec3 &vec) { *this = *this + vec; return *this; } vec3 &operator -= (const vec3 &vec) { *this = *this - vec; return *this; } vec3 &operator *= (const T factor) { *this = *this * factor; return *this; } vec3 &operator /= (const T factor) { *this = *this / factor; return *this; } T dot (const vec3 &vec) const { return (x*vec.x + y*vec.y + z*vec.z); } T operator * (const vec3 &vec) const { return dot(vec); } vec3 cross (const vec3 &vec) const { return vec3(y*vec.z-z*vec.y, z*vec.x-x*vec.z, x*vec.y-y*vec.x); } vec3 operator ^ (const vec3 &vec) const { return cross(vec); } vec3 modulate (const vec3 &vec) const { return vec3(x*vec.x, y*vec.y, z*vec.z); } T lengthsq () const { return (x*x + y*y + z*z); } T length () const { return (T)sqrt(lengthsq()); } void normalize () { T len = length(); if (len) { len = (T)1 / len; x *= len; y *= len; z *= len; } else { *this = vec3((T)0,(T)0,(T)1); } } void copyTo(T *dest) const { dest[0] = x; dest[1] = y; dest[2] = z; } void copyFrom(const T *src) { x = src[0]; y = src[1]; z = src[2]; } static vec3 zero () { return vec3(0,0,0); } static vec3 rand() { vec3 ret; do { ret=vec3((T)2 * (T)::rand() / (T)RAND_MAX - (T)1, (T)2 * (T)::rand() / (T)RAND_MAX - (T)1, (T)2 * (T)::rand() / (T)RAND_MAX - (T)1); } while (ret.lengthsq() > (T)1); return ret; } }; template inline vec3 operator - (const vec3 &vec) { return vec3(-vec.x,-vec.y,-vec.z); } template inline vec3 operator * (const T &fac, const vec3 &vec) { return vec * fac; } template class vec4 { public: T x,y,z,w; vec4 () { } vec4 (T _x, T _y, T _z, T _w) { x=_x; y=_y; z=_z; w=_w; } vec4 (const vec3 &vec) { x=vec.x; y=vec.y; z=vec.z; } vec4 (const vec4 &vec) { x=vec.x; y=vec.y; z=vec.z; w=vec.w; } vec4 (const vec3 &vec, T _w) { x=vec.x; y=vec.y; z=vec.z; w=_w; } T &operator [] (int i) { return (&x)[i]; } operator const T* () const { return &x; } operator T* () { return &x; } //operator vec3 () const { return vec3(x,y,z); } vec4 operator + (const vec4 &vec) const { return vec4(x+vec.x, y+vec.y, z+vec.z, w+vec.w); } vec4 operator - (const vec4 &vec) const { return vec4(x-vec.x, y-vec.y, z-vec.z, w-vec.w); } vec4 operator * (T factor) const { return vec4(x*factor, y*factor, z*factor, w*factor); } vec4 &operator += (const vec4 &vec) { *this = *this + vec; return *this; } vec4 &operator -= (const vec4 &vec) { *this = *this - vec; return *this; } vec4 &operator *= (const T factor) { *this = *this * factor; return *this; } T dot (const vec4 &vec) const { return (x*vec.x + y*vec.y + z*vec.z + w*vec.w); } T operator * (const vec4 &vec) const { return dot(vec); } T lengthsq () const { return (x*x + y*y + z*z + w*w); } T length () const { return sqrt(lengthsq()); } void normalize3a () { T len = sqrt(x*x + y*y + z*z); if (len) { len = (T)1 / len; x *= len; y *= len; z *= len; w *= len; } else { *this = vec4((T)0,(T)0,(T)1,(T)0); } } void normalize3b () { if (w) { T len = (T)1 / w; x *= len; y *= len; z *= len; w = (T)1; } else { *this = vec4((T)0,(T)0,(T)0,(T)1); } } void normalize3c () { T len = sqrt(x*x + y*y + z*z); if (len) { len = (T)1 / len; x *= len; y *= len; z *= len; } else { x = (T)0; y = (T)0; z = (T)1; } } void normalize4 () { T len = length(); if (len) { len = (T)1 / len; x *= len; y *= len; z *= len; w *= len; } else { *this = vec4((T)0,(T)0,(T)0,(T)1); } } void copyTo(T *dest) const { dest[0] = x; dest[1] = y; dest[2] = z; dest[3] = w; } void copyFrom(const T *src) { x = src[0]; y = src[1]; z = src[2]; w = src[3]; } }; template class ray { public: vec3 start; vec3 direction; ray () { } ray (const vec3 &_start, const vec3 &_direction) : start(_start), direction(_direction) { } void fromTwoPoints(const vec3 &_start, const vec3 &_pt) { start = _start; direction = _pt - _start; } vec3 getPt(T lambda) const { return start + direction * lambda; } bool getSphereIntersection(const vec3 ¢er, T radius, T &outLambda) const { vec3 temp = start - center; T a = direction.lengthsq(), b = 2.0 * (direction * temp), c = temp.lengthsq() - radius*radius, determinant = b * b - (T)4 * a * c; if (determinant < (T)0) return false; T rootdet = sqrt(determinant), t = (-b - rootdet) / ((T)2 * a); outLambda = t; return true; } }; /// @brief class storing a plane /// @details this plane class is of form (x,y,z) . normal + offset = 0 /// ie, increasing offset moves plane backwards along its normal /// template class plane { public: vec3 normal; T offset; plane () { } plane (const vec3 &_normal, T _offset) : normal(_normal), offset(_offset) { } plane (T _a, T _b, T _c, T _d) : normal(_a, _b, _c), offset(_d) { } void normalize() { T len = normal.length(); if (len) { len = (T)1 / len; normal *= len; offset *= len; } } }; /// /// @brief class storing a 4x4 matrix /// template class mat44 { public: vec4 row[4]; mat44 () { } T &operator [] (int i) { return row[0][i]; } operator T* () { return &row[0].x; } operator const T* () const { return &row[0].x; } mat44 &assemble(const vec4 &r0, const vec4 &r1, const vec4 &r2, const vec4 &r3) { row[0] = r0; row[1] = r1; row[2] = r2; row[3] = r3; return *this; } mat44 &assemble(const vec3 &r0, const vec3 &r1, const vec3 &r2) { row[0] = vec4(r0.x, r0.y, r0.z, (T)0); row[1] = vec4(r1.x, r1.y, r1.z, (T)0); row[2] = vec4(r2.x, r2.y, r2.z, (T)0); row[3] = vec4((T)0, (T)0, (T)0, (T)1); return *this; } mat44 concatenate(const mat44 &mat) const { mat44 ret; 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]; 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]; 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]; 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]; 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]; 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]; 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]; 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]; 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]; 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]; 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]; 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]; 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]; 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]; 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]; 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]; return ret; } mat44 concatenateReverse(const mat44 &mat) const { return mat.concatenate(*this); } mat44 transpose() const { mat44 ret; ret.row[0] = vec4(row[0].x, row[1].x, row[2].x, row[3].x); ret.row[1] = vec4(row[0].y, row[1].y, row[2].y, row[3].y); ret.row[2] = vec4(row[0].z, row[1].z, row[2].z, row[3].z); ret.row[3] = vec4(row[0].w, row[1].w, row[2].w, row[3].w); return ret; } mat44 &translate(const vec3 &vec) { row[0].w += vec.x; row[1].w += vec.y; row[2].w += vec.z; return *this; } vec3 transform1(const vec3 &vec) const { return vec3 ( row[0].x * vec.x + row[0].y * vec.y + row[0].z * vec.z + row[0].w, row[1].x * vec.x + row[1].y * vec.y + row[1].z * vec.z + row[1].w, row[2].x * vec.x + row[2].y * vec.y + row[2].z * vec.z + row[2].w ); } vec4 transform1(const vec4 &vec) const { return vec4 ( row[0].x * vec.x + row[0].y * vec.y + row[0].z * vec.z + row[0].w * vec.w, row[1].x * vec.x + row[1].y * vec.y + row[1].z * vec.z + row[1].w * vec.w, row[2].x * vec.x + row[2].y * vec.y + row[2].z * vec.z + row[2].w * vec.w, row[3].x * vec.x + row[3].y * vec.y + row[3].z * vec.z + row[3].w * vec.w ); } vec3 transform2(const vec3 &vec) const { return vec3 ( row[0].x * vec.x + row[1].x * vec.y + row[2].x * vec.z + row[3].x, row[0].y * vec.x + row[1].y * vec.y + row[2].y * vec.z + row[3].y, row[0].z * vec.x + row[1].z * vec.y + row[2].z * vec.z + row[3].z ); } vec4 transform2(const vec4 &vec) const { return vec4 ( row[0].x * vec.x + row[1].x * vec.y + row[2].x * vec.z + row[3].x * vec.w, row[0].y * vec.x + row[1].y * vec.y + row[2].y * vec.z + row[3].y * vec.w, row[0].z * vec.x + row[1].z * vec.y + row[2].z * vec.z + row[3].z * vec.w, row[0].w * vec.x + row[1].w * vec.y + row[2].w * vec.z + row[3].w * vec.w ); } vec3 transformNormal(const vec3 &vec) const { return vec3 ( row[0].x * vec.x + row[0].y * vec.y + row[0].z * vec.z, row[1].x * vec.x + row[1].y * vec.y + row[1].z * vec.z, row[2].x * vec.x + row[2].y * vec.y + row[2].z * vec.z ); } static mat44 identity() { mat44 ret; ret.row[0] = vec4(1,0,0,0); ret.row[1] = vec4(0,1,0,0); ret.row[2] = vec4(0,0,1,0); ret.row[3] = vec4(0,0,0,1); return ret; } }; /// /// @brief class storing a quaternion /// template class quat { public: T x,y,z,w; public: quat() { } quat(T _x, T _y, T _z, T _w) : x(_x), y(_y), z(_z), w(_w) { } quat operator + (const quat &q) const { return quat(x+q.x, y+q.y, z+q.z, w+q.w); } quat operator - (const quat &q) const { return quat(x-q.x, y-q.y, z-q.z, w-q.w); } quat operator * (T val) const { return quat(x*val, y*val, z*val, w*val); } quat operator / (T val) const { T mult = (T)1 / val; return quat(x*mult, y*mult, z*mult, w*mult); } quat operator * (const quat &q) const { return quat( w*q.x + x*q.w + y*q.z - z*q.y, w*q.y - x*q.z + y*q.w + z*q.x, w*q.z + x*q.y - y*q.x + z*q.w, w*q.w - x*q.x - y*q.y - z*q.z); } quat &operator += (const quat &q) { *this = *this + q; return *this; } // operator *= has to assume an order // since quaternion mult is not commutative, it's // better to force the user to think about it float dot(const quat &q) { return (x * q.x + y * q.y + z * q.z + w * q.w); } void fromAxisAngle(const vec3 &axis, T angle) { T len = axis.length(); if (len > (T)0) { T sinA = sin(angle/(T)2) / len; x = sinA * axis.x; y = sinA * axis.y; z = sinA * axis.z; w = cos(angle/(T)2); } else { *this = identity(); } } void fromXAngle(T angle) { x = sin(angle/(T)2); y = z = (T)0; w = cos(angle/(T)2); } void fromYAngle(T angle) { y = sin(angle/(T)2); x = z = (T)0; w = cos(angle/(T)2); } void fromZAngle(T angle) { z = sin(angle/(T)2); x = y = (T)0; w = cos(angle/(T)2); } void fromThreeAxisAngle(const vec3 &vec) { quat temp; fromXAngle(vec.x); temp.fromYAngle(vec.y); *this = *this * temp; temp.fromZAngle(vec.z); *this = *this * temp; } void normalize() { T len = x*x + y*y + z*z + w*w; if (len > (T)0) { len = (T)1 / (T)sqrt(len); x *= len; y *= len; z *= len; w *= len; } else { *this = identity(); } } mat44 getMatrix() const { T norm = x*x + y*y + z*z + w*w, s = (norm > (T)0) ? (T)2/norm : (T)0, xx = x * x * s, yy = y * y * s, zz = z * z * s, xy = x * y * s, xz = x * z * s, yz = y * z * s, wx = w * x * s, wy = w * y * s, wz = w * z * s; mat44 m; m.assemble( vec3((T)1 - (yy + zz), xy + wz, xz - wy), vec3(xy - wz, (T)1 - (xx + zz), yz + wx), vec3(xz + wy, yz - wx, (T)1 - (xx + yy))); return m; /* m.el[0] = (T)1 - (yy + zz); m.el[4] = xy + wz; m.el[8] = xz - wy; m.el[1] = xy - wz; m.el[5] = (T)1 - (xx + zz); m.el[9] = yz + wx; m.el[2] = xz + wy; m.el[6] = yz - wx; m.el[10] = (T)1 - (xx + yy); m.el[12] = m.el[13] = m.el[14] = m.el[3] = m.el[7] = m.el[11] = (T)0; m.el[15] = (T)1; */ } static quat identity () { return quat(0,0,0,1); } static quat rand() { quat ret; float lengthsq; do { ret=quat((T)2 * (T)::rand() / (T)RAND_MAX - (T)1, (T)2 * (T)::rand() / (T)RAND_MAX - (T)1, (T)2 * (T)::rand() / (T)RAND_MAX - (T)1, (T)2 * (T)::rand() / (T)RAND_MAX - (T)1); lengthsq = ret.x*ret.x + ret.y*ret.y + ret.z*ret.z + ret.w*ret.w; } while (lengthsq > (T)1 || lengthsq <= (T)0); float mult = (T)1 / (T)sqrt(lengthsq); ret.x *= mult; ret.y *= mult; ret.z *= mult; ret.w *= mult; return ret; } }; template class frustum { public: enum { SideRight, SideLeft, SideTop, SideBottom, SideNear, SideFar, SideCount }; plane side[SideCount]; public: frustum() { } frustum(const mat44 &mat) { construct(mat); } frustum &construct(const mat44 &mat); bool isAABBOutside(const vec3 &mins, const vec3 &maxs) const; }; typedef signed char int8; typedef unsigned char uint8; typedef signed short int16; typedef unsigned short uint16; typedef signed int int32; typedef unsigned int uint32; typedef vec2 vec2f; typedef vec2 vec2d; typedef vec2 vec2i; typedef vec2 vec2ub; typedef vec3 vec3f; typedef vec3 vec3d; typedef vec3 vec3i; typedef vec3 vec3ub; typedef vec4 vec4f; typedef vec4 vec4d; typedef vec4 vec4i; typedef vec4 vec4ub; typedef quat quatf; typedef quat quatd; typedef ray rayf; typedef ray rayd; typedef plane planef; typedef plane planed; typedef mat44 mat44f; typedef mat44 mat44d; typedef frustum frustumf; typedef frustum frustumd; template inline T intersect_plane_ray(const plane &pln, const vec3 &linept, const vec3 &linedir) { T denom = pln.normal * linedir; if (denom) { return - (pln.normal * linept + pln.offset) / denom; } else { return (T)0; } } #endif