yasuo katano / QuaternionMath

Fork of QuaternionMath by Chris Pepper

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Vector3.h Source File

Vector3.h

00001 #ifndef __AHRSMATHDSP_VECTOR3_
00002 #define __AHRSMATHDSP_VECTOR3_
00003 
00004 //#include "arm_math.h"
00005 #include "math.h"
00006 #define PI 3.1415926535
00007 
00008 
00009 class Vector3 {
00010 public:
00011     Vector3(){
00012         x = y = z = 0;
00013     }
00014     
00015     Vector3(float x, float y, float z){
00016         this->x = x;
00017         this->y = y;
00018         this->z = z;
00019     }
00020     
00021     void set(float x, float y, float z){
00022         this->x = x;
00023         this->y = y;
00024         this->z = z;     
00025     }
00026     
00027     float length_squared(){
00028         return x*x + y*y + z*z;
00029     }
00030     
00031     float length(){
00032         return sqrt(length_squared());    
00033     }
00034     
00035     void normalise(){
00036         float len = length();
00037         x = x/len;
00038         y = y/len;
00039         z = z/len;
00040     }
00041     
00042     float dot_product(const Vector3 &v){
00043         return x*v.x + y*v.y + z*v.z;
00044     }
00045     
00046     Vector3 cross_product(const Vector3 &v){
00047         Vector3 temp(y*v.z - z*v.y, z*v.x - x*v.z, x*v.y - y*v.x);
00048         return temp;
00049     }
00050     
00051     //operator overides
00052     void operator ()(const float x, const float y, const float z) {
00053         this->x = x;
00054         this->y = y;
00055         this->z = z;
00056     }
00057 
00058     bool operator == (const Vector3 &v) {
00059         return (x==v.x && y==v.y && z==v.z);
00060     }
00061 
00062     bool operator != (const Vector3 &v) {
00063         return (x!=v.x || y!=v.y || z!=v.z);
00064     }
00065 
00066     const Vector3 &operator = (const Vector3 &v) {   
00067         x = v.x;
00068         y = v.y;
00069         z = v.z;     
00070         return *this;
00071     }
00072     
00073     const Vector3 operator - (void) const {
00074         return Vector3(-x,-y,-z);
00075     }
00076  
00077     const Vector3 operator + (const Vector3 &v) const {
00078         return Vector3(x+v.x, y+v.y, z+v.z);
00079     }  
00080 
00081     const Vector3 operator - (const Vector3 &v) const {
00082         return Vector3(x-v.x, y-v.y, z-v.z);
00083     }
00084     
00085     const Vector3 operator * (const float num) const {
00086         Vector3 temp;
00087         temp.x = x * num;
00088         temp.y = y * num;  
00089         temp.z = z * num;  
00090         return temp;
00091     }
00092 
00093     const Vector3 operator / (const float num) const {
00094         Vector3 temp;
00095         temp.x = x / num;
00096         temp.y = y / num;  
00097         temp.z = z / num;  
00098         return temp;
00099     }
00100     
00101     float operator * (const Vector3 &v) const {
00102         return x*v.x + y*v.y + z*v.z;
00103     }
00104     
00105     float x, y, z;
00106        
00107 };
00108 
00109 #endif