#ifndef _MATH_CLASS_ #define _MATH_CLASS_ #include class vector3 { public: vector3() : x( 0.0f ), y( 0.0f ), z( 0.0f ) {}; vector3( float a_X, float a_Y, float a_Z ) : x( a_X ), y( a_Y ), z( a_Z ) {}; vector3(int a_X, int a_Y, int a_Z) : xint(a_X), yint(a_Y), zint(a_Z) {}; void Set( float a_X, float a_Y, float a_Z ) { x = a_X; y = a_Y; z = a_Z; } void Normalize() { float l = 1.0f / Length(); x *= l; y *= l; z *= l; } float Length() { return (float)sqrt( x * x + y * y + z * z ); } float SqrLength() { return x * x + y * y + z * z; } float Dot( vector3 a_V ) { return x * a_V.x + y * a_V.y + z * a_V.z; } vector3 Cross( vector3 b ) { return vector3( y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x ); } void operator += ( vector3& a_V ) { x += a_V.x; y += a_V.y; z += a_V.z; } void operator += ( vector3* a_V ) { x += a_V->x; y += a_V->y; z += a_V->z; } void operator -= ( vector3& a_V ) { x -= a_V.x; y -= a_V.y; z -= a_V.z; } void operator -= ( vector3* a_V ) { x -= a_V->x; y -= a_V->y; z -= a_V->z; } void operator *= ( float f ) { x *= f; y *= f; z *= f; } void operator *= ( vector3& a_V ) { x *= a_V.x; y *= a_V.y; z *= a_V.z; } void operator *= ( vector3* a_V ) { x *= a_V->x; y *= a_V->y; z *= a_V->z; } vector3 operator- () const { return vector3( -x, -y, -z ); } friend vector3 operator + ( const vector3& v1, const vector3& v2 ) { return vector3( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z ); } friend vector3 operator - ( const vector3& v1, const vector3& v2 ) { return vector3( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z ); } friend vector3 operator + ( const vector3& v1, vector3* v2 ) { return vector3( v1.x + v2->x, v1.y + v2->y, v1.z + v2->z ); } friend vector3 operator - ( const vector3& v1, vector3* v2 ) { return vector3( v1.x - v2->x, v1.y - v2->y, v1.z - v2->z ); } friend vector3 operator * ( const vector3& v, float f ) { return vector3( v.x * f, v.y * f, v.z * f ); } friend vector3 operator * ( const vector3& v1, vector3& v2 ) { return vector3( v1.x * v2.x, v1.y * v2.y, v1.z * v2.z ); } friend vector3 operator * ( float f, const vector3& v ) { return vector3( v.x * f, v.y * f, v.z * f ); } //Added for texture atlas purposes void Barycenter(vector3 v1, vector3 v2, vector3 v3) { x = (v1.x + v2.x + v3.x) / 3; y = (v1.y + v2.y + v3.y) / 3; z = (v1.z + v2.z + v3.z) / 3; } //Triangle Centroid calculation void Centroid(vector3 v1, vector3 v2, vector3 v3) { x = (v1.x/(v1.x + v2.x + v3.x)) + (v1.y/(v1.y + v2.y + v3.y)) + (v1.z/(v1.z + v2.z + v3.z)); y = (v2.x/(v1.x + v2.x + v3.x)) + (v2.y/(v1.y + v2.y + v3.y)) + (v2.z/(v1.z + v2.z + v3.z)); z = (v3.x/(v1.x + v2.x + v3.x)) + (v3.y/(v1.y + v2.y + v3.y)) + (v3.z/(v1.z + v2.z + v3.z)); } //Triangle Area calculation using Heron's Formula double TriangleArea(vector3 v1, vector3 v2, vector3 v3) { float a, b, c, p; double TriArea; a = v1.Length(); b = v2.Length(); c = v3.Length(); p = 0.5 * (a + b + c); TriArea = sqrt(p * (p - a) * (p - b) * (p - c)); return TriArea; } union { struct { float x, y, z; }; struct { float r, g, b; }; struct { float cell[3]; }; struct { int xint, yint, zint;}; }; }; #endif