[GEM-cvs] Gem/src/Base GemVector.cpp,NONE,1.1 GemVector.h,NONE,1.1
Brought to you by:
zmoelnig
From: IOhannes m z. <zmo...@us...> - 2005-02-07 13:20:59
|
Update of /cvsroot/pd-gem/Gem/src/Base In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12434 Added Files: GemVector.cpp GemVector.h Log Message: a class for 3D-vectors (formerly found in Manips/camera) --- NEW FILE: GemVector.cpp --- //////////////////////////////////////////////////////// // // GEM - Graphics Environment for Multimedia // // zmo...@ie... // // Implementation file // // // For information on usage and redistribution, and for a DISCLAIMER OF ALL // WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution. // ///////////////////////////////////////////////////////// #include "GemVector.h" #include <math.h> #ifdef __ppc__ # include "Base/GemFuncUtil.h" # undef sqrt # define sqrt fast_sqrtf #endif // default constructor CVector3::CVector3() : x(0), y(0), z(0) {} // This is our constructor that allows us to initialize our data upon creating an instance CVector3::CVector3(float X, float Y, float Z) : x(X), y(Y), z(Z) {} // Here we overload the + operator so we can add vectors together CVector3 CVector3::operator+(CVector3 vVector) { // Return the added vectors result. return CVector3(vVector.x + x, vVector.y + y, vVector.z + z); } // Here we overload the - operator so we can subtract vectors CVector3 CVector3::operator-(CVector3 vVector) { // Return the subtracted vectors result return CVector3(x - vVector.x, y - vVector.y, z - vVector.z); } // Here we overload the - operator so we can negate a vector CVector3 CVector3::operator-() { // Return the subtracted vectors result return CVector3(-x, -y, -z); } // Here we overload the * operator so we can multiply by scalars CVector3 CVector3::operator*(float num) { // Return the scaled vector return CVector3(x * num, y * num, z * num); } // Here we overload the * operator so we can dot-multiply // note: i chose dot-multiplication because this can be done consistently for vectors of any length // but i don't know whether this is "the right way" to do float CVector3::operator*(CVector3 vVector) { // Return the scaled vector return (x*vVector.x+y*vVector.y+z*vVector.z); } // cross-multiplication CVector3 CVector3::cross(CVector3 vVector) { CVector3 vCross; // The vector to hold the cross product vCross.x = ((y * vVector.z) - (z * vVector.y)); // Get the X value vCross.y = ((z * vVector.x) - (x * vVector.z)); // Get the Y value vCross.z = ((x * vVector.y) - (y * vVector.x)); // Get the Z value return vCross; // Return the cross product } // Here we overload the / operator so we can divide by a scalar CVector3 CVector3::operator/(float num) { // Return the scale vector return CVector3(x / num, y / num, z / num); } // here we calculate the absolute-value of the vector float CVector3::abs() { return sqrt(x*x+y*y+z*z); } // here we calculate the square of the absolute-value of the vector float CVector3::abs2() { return (x*x+y*y+z*z); } // here we normalize the vector CVector3 CVector3::normalize() { CVector3 vNormal; float InvMagnitude = 1.f / abs(); // Get the magnitude vNormal.x = x*InvMagnitude; // Divide the vector's X by the magnitude vNormal.y = y*InvMagnitude; // Divide the vector's Y by the magnitude vNormal.z = z*InvMagnitude; // Divide the vector's Z by the magnitude return vNormal; // Return the normal } // compares to vectores and resturns true is they are equal (within a certain threshold) // An epsilon that works fairly well is 0.000001. bool CVector3::equals(CVector3 vVector, float epsilon){ return (fabsf(x - vVector.x) < epsilon && fabsf(y - vVector.y) < epsilon && fabsf(z - vVector.z) < epsilon); } --- NEW FILE: GemVector.h --- /*----------------------------------------------------------------- LOG GEM - Graphics Environment for Multimedia vector-classes zmo...@ie..., ti...@ma... For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution. -----------------------------------------------------------------*/ #ifndef INCLUDE_GEMVECTOR_H_ #define INCLUDE_GEMVECTOR_H_ #include "Base/GemExportDef.h" // This is our 2D point class. This will be used to store the UV coordinates. class GEM_EXTERN CVector2 { public: float x, y; }; // This is our basic 3D point/vector class class GEM_EXTERN CVector3 { public: // the elements of a vector: float x, y, z; // A default constructor CVector3(); // This is our constructor that allows us to initialize our data upon creating an instance CVector3(float X, float Y, float Z); // Here we overload the + operator so we can add vectors together CVector3 operator+(CVector3 vVector); // Here we overload the - operator so we can subtract vectors CVector3 operator-(CVector3 vVector); // Here we overload the - operator so we can negate the vector CVector3 operator-(); // Here we overload the * operator so we can multiply by scalars CVector3 operator*(float num); // Here we overload the * operator so we can dot-multiply float operator*(CVector3 vVector); // cross-multiplication CVector3 cross(CVector3 vVector); // Here we overload the / operator so we can divide by a scalar CVector3 operator/(float num); // here we calculate the absolute-value of the vector float abs(); // here we calculate the square of the absolute-value of the vector float abs2(); // here we normalize the vector CVector3 normalize(); // here we compare 2 vectors on approx. equality bool equals(CVector3 vVector, float epsilon); }; #endif /* INCLUDE_GEMVECTOR_H_ */ |