Robot

Dependencies:   mbed QEI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Vector.cpp Source File

Vector.cpp

00001 //
00002 // Created by m007 on 26/06/2020.
00003 //
00004 
00005 #include "Ro/Vector.h"
00006 template<class T>
00007 Vector<T>::Vector() {
00008     objectCount++;
00009     this->dimension = 3;
00010     n = new T [this->dimension];
00011     for (int i = 0; i <this->dimension ; i++){
00012         n[i] = 0;
00013     }
00014 }
00015 
00016 template<class T>
00017 Vector<T>::Vector(int dimension) {
00018     objectCount++;
00019     this->dimension = dimension;
00020     n = new T [this->dimension];
00021     for (int i = 0; i <this->dimension ; i++){
00022         n[i] = 0;
00023     }
00024 }
00025 
00026 template<class T>
00027 Vector<T>::~Vector(){
00028     delete [] n;
00029 }
00030 
00031 template<class T>
00032 int Vector<T>::getDimension(){
00033     return dimension;
00034 }
00035 
00036 template<class T>
00037 void Vector<T>::PrintResult(){
00038     for (int x = 0; x<this->dimension; x++) {
00039         printf(" %f ", n[x]);
00040     }
00041     printf("\n");
00042 }
00043 
00044 
00045 template<class T>
00046 Vector<T> Vector<T>::crossProduct(Vector<T> const& VectorB){
00047     Vector<T> result(3);
00048     if(this->dimension == 3) {
00049         result(0) = (n[1] * VectorB.n[2] - n[2] * VectorB.n[1]);
00050         result(1) = -(n[0] * VectorB.n[2] - n[2] * VectorB.n[0]);
00051         result(2) = (n[0] * VectorB.n[1] - n[1] * VectorB.n[0]);
00052         for (int x = 0; x < 3; x++) {
00053             if (( result(x) > -0.000001) && (result(x)) < 0.000001) {
00054                 result(x) = 0;
00055             }
00056         }
00057     }
00058     return result;
00059 }
00060 
00061 
00062 
00063 
00064 template<class T>
00065 Vector<T>&  Vector<T>::operator=(const Vector &other ){
00066     if(this->dimension==other.dimension){
00067         for(int i=0;i<this->dimension;i++){
00068             this->n[i] = other.n[i];
00069         }
00070     }else{
00071         printf("ERROR: Vector A = Vector B. B has more dimensions than A");
00072     }
00073     return (*this);
00074 }
00075 
00076 
00077 template<class T>
00078 Vector<T>& Vector<T>::operator=(T param[]){
00079     for(int i=0;i<this->dimension;i++){
00080         this->n[i]=param[i];
00081     }
00082     return (*this);
00083 
00084 }
00085 template<class T>
00086 Vector<T>  Vector<T>::operator+(Vector<T> other){
00087     Vector<T> result(this->dimension);
00088     if(this->dimension == other.dimension) {
00089         for (int i = 0; i < this->dimension; i++) {
00090             result.n[i] = other.n[i] + this->n[i];
00091         }
00092     }else{
00093         printf("ERROR: Vector A = Vector B. B has more dimensions than A");
00094     }
00095     return result;
00096 }
00097 
00098 template<class T>
00099 Vector<T>  Vector<T>::operator-(Vector<T>& other){
00100     Vector<T> result(this->dimension);
00101     if(this->dimension== other.dimension) {
00102         for (int i = 0; i < this->dimension; i++) {
00103             result.n[i] = other.n[i] - this->n[i];
00104         }
00105     }else{
00106         printf("ERROR: Vector A = Vector B. B has more dimensions than A");
00107     }
00108     return result;
00109 }
00110 
00111 template<class T>
00112 T Vector<T>::operator*(Vector<T>& other) {
00113     T ans=0;
00114     if(this->dimension == other.dimension) {
00115         for (int i = 0; i < this->dimension; i++) {
00116             ans = ans + (other.n[i] * this->n[i]);
00117         }
00118     }else{
00119         printf("ERROR: Vector A = Vector B. B has more dimensions than A");
00120     }
00121     return ans;
00122 }
00123 
00124 template<class T>
00125 Vector<T>  Vector<T>::operator*(T factor){
00126     Vector<T> result(this->dimension);
00127     for (int i = 0; i < this->dimension; i++) {
00128         result.n[i] = factor * this->n[i];
00129     }
00130     return result;
00131 }
00132 
00133 template<class T>
00134 T& Vector<T>::operator()(int dim){
00135     return n[dim];
00136 }
00137 
00138 template<class T>
00139 int Vector<T>::objectCount = 0;
00140 
00141 
00142 Vector<float> zeros(int num){
00143     Vector<float> result(num);
00144     return result;
00145 }
00146 //template<class T>
00147 Vector<float> crossProduct(Vector<float> VectorA,Vector<float> VectorB){
00148     Vector<float> result(3);
00149 
00150     //if(VectorA->dimension == 3) {
00151     result(0) = (VectorA(1) * VectorB(2) - VectorA(2) * VectorB(1));
00152     result(1) = -(VectorA(0) * VectorB(2) - VectorA(2) * VectorB(0));
00153     result(2) = (VectorA(0) * VectorB(1) - VectorA(1) * VectorB(0));
00154     for (int x = 0; x < 3; x++) {
00155         if (( result(x) > -0.000001) && (result(x)) < 0.000001) {
00156             result(x) = 0;
00157         }
00158     }
00159     return result;
00160 }
00161 
00162 template class Vector<float>;
00163 template class Vector<int>;