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