MC_v4d.cpp

Go to the documentation of this file.
00001 
00002 #include <MC_matrix.hpp>
00003 #include <MC_v3d.hpp>
00004 #include <MC_string_helper.hpp>
00005 #include <MC_v4d.hpp>
00006 
00007 #include <cmath>
00008 
00009 
00010 
00011 namespace mesh_conv
00012 {
00013 
00014 MC_v4d::MC_v4d()
00015 {v[0]=0;v[1]=0;v[2]=0;v[3]=0;}
00016 
00017 MC_v4d::MC_v4d(const double& x,const double& y,const double& z,const double& w)
00018 {v[0]=x;v[1]=y;v[2]=z;v[3]=w;}
00019 
00020 MC_v4d::~MC_v4d(){}
00021 
00022 const double& MC_v4d::operator()(const int& k_dim) const
00023 {
00024     if(k_dim<0 || k_dim>3)
00025     {std::cout<<"Error in const MC_v4d("<<k_dim<<"), size must be 0,1 or 2"<<std::endl;}
00026     return v[k_dim];
00027 }
00028 double& MC_v4d::operator()(const int& k_dim)
00029 {
00030     if(k_dim<0 || k_dim>3)
00031     {std::cout<<"Error in MC_v4d("<<k_dim<<"), size must be 0,1,2 or 3"<<std::endl;}
00032     return v[k_dim];
00033 }
00034 const double& MC_v4d::operator[](const int& k_dim) const
00035 {
00036     if(k_dim<0 || k_dim>3)
00037     {std::cout<<"Error in const MC_v4d["<<k_dim<<"], size must be 0,1,2 or 3"<<std::endl;}
00038     return v[k_dim];
00039 }
00040 double& MC_v4d::operator[](const int& k_dim)
00041 {
00042     if(k_dim<0 || k_dim>3)
00043     {std::cout<<"Error in MC_v4d["<<k_dim<<"], size must be 0,1,2 or 3"<<std::endl;}
00044     return v[k_dim];
00045 }
00046 MC_v4d::MC_v4d(const MC_v4d& _v)
00047 {v[0]=_v[0];v[1]=_v[1];v[2]=_v[2];v[3]=_v[3];}
00048 
00049 MC_v4d::MC_v4d(const double* _v)
00050 {v[0]=_v[0];v[1]=_v[1];v[2]=_v[2];v[3]=_v[3];}
00051 
00052 MC_v4d MC_v4d::operator-() const
00053 {return MC_v4d(-v[0],-v[1],-v[2],-v[3]);}
00054 
00055 
00056 MC_v4d operator+(const MC_v4d& vec,const double& to_add)
00057 {return MC_v4d(vec[0]+to_add,vec[1]+to_add,vec[2]+to_add,vec[3]+to_add);}
00058 MC_v4d operator-(const MC_v4d& vec,const double& to_sub)
00059 {return MC_v4d(vec[0]-to_sub,vec[1]-to_sub,vec[2]-to_sub,vec[3]-to_sub);}
00060 MC_v4d operator*(const MC_v4d& vec,const double& to_mult)
00061 {return MC_v4d(vec[0]*to_mult,vec[1]*to_mult,vec[2]*to_mult,vec[3]*to_mult);}
00062 MC_v4d operator*(const double& to_mult,const MC_v4d& vec)
00063 {return vec*to_mult;}
00064 
00065 MC_v4d operator/(const MC_v4d& vec,const double& to_subdiv)
00066 {
00067     double epsilon=0.000001;
00068     if(fabs(to_subdiv)<epsilon)
00069     {std::cout<<"Error in MC_v4d("<<vec<<"/"<<to_subdiv<<", divide by zero"<<std::endl;exit(-1);}
00070 
00071     return MC_v4d(vec[0]/to_subdiv,vec[1]/to_subdiv,vec[2]/to_subdiv,vec[3]/to_subdiv);
00072 }
00073 MC_v4d operator+(const MC_v4d& vec,const MC_v4d& to_add)
00074 {return MC_v4d(vec[0]+to_add[0],vec[1]+to_add[1],vec[2]+to_add[2],vec[3]+to_add[3]);}
00075 MC_v4d operator-(const MC_v4d& vec,const MC_v4d& to_sub)
00076 {return MC_v4d(vec[0]-to_sub[0],vec[1]-to_sub[1],vec[2]-to_sub[2],vec[3]-to_sub[3]);}
00077 
00078 MC_v4d& MC_v4d::operator+=(const double& to_add)
00079 {
00080     v[0]+=to_add;v[1]+=to_add;v[2]+=to_add;v[3]+=to_add;
00081     return *this;
00082 }
00083 
00084 MC_v4d& MC_v4d::operator-=(const double& to_sub)
00085 {
00086     v[0]-=to_sub;v[1]-=to_sub;v[2]-=to_sub;v[3]-=to_sub;
00087     return *this;
00088 }
00089 MC_v4d& MC_v4d::operator*=(const double& to_mult)
00090 {
00091     v[0]*=to_mult;v[1]*=to_mult;v[2]*=to_mult;v[3]*=to_mult;
00092     return *this;
00093 }
00094 MC_v4d& MC_v4d::operator/=(const double& to_subdiv)
00095 {
00096     double epsilon=0.000001;
00097     if(fabs(to_subdiv)<epsilon)
00098     {std::cout<<"Error in MC_v4d("<<*this<<"/="<<to_subdiv<<", divide by zero"<<std::endl;exit(-1);}
00099 
00100     v[0]/=to_subdiv;v[1]/=to_subdiv;v[2]/=to_subdiv;v[3]/=to_subdiv;
00101     return *this;
00102 }
00103 MC_v4d& MC_v4d::operator+=(const MC_v4d& to_add){
00104     v[0]+=to_add[0];v[1]+=to_add[1];v[2]+=to_add[2];v[3]+=to_add[3];
00105     return *this;
00106 }
00107 MC_v4d& MC_v4d::operator-=(const MC_v4d& to_sub){
00108     v[0]-=to_sub[0];v[1]-=to_sub[1];v[2]-=to_sub[2];v[3]-=to_sub[3];return *this;
00109 }
00110 
00111 
00112 std::ostream& operator<<(std::ostream& stream,const MC_v4d& v)
00113 {
00114     stream<<"("<<v[0]<<","<<v[1]<<","<<v[2]<<","<<v[3]<<")";
00115     return stream;
00116 }
00117 
00118 MC_v4d::MC_v4d(const std::string& s)
00119 {
00120     std::vector <std::string> v_s=mesh_conv::MC_string_tokenizer::tokenize(s," (),;");
00121 
00122     int number_ok=0;
00123     for(unsigned int k=0;number_ok<3 && k<v_s.size();k++)
00124     {
00125         bool is_converted=false;
00126         double temp_value=MC_string_converter::value_of<double>(v_s[k],&is_converted);
00127         if(is_converted==true)
00128             v[number_ok++]=temp_value;
00129     }
00130 
00131 }
00132 
00133 
00134     MC_v4d& operator>>(std::istream& stream,MC_v4d& vec)
00135     {
00136         int current_position=0;
00137         std::string temp_string;
00138         while(current_position<4 && stream.good())
00139         {
00140             if(stream>>temp_string)
00141             {
00142                 std::vector <std::string> v_token=MC_string_tokenizer::tokenize(temp_string,",;() ");
00143 
00144                 for(unsigned int k=0;current_position<4 && k<v_token.size();++k)
00145                 {
00146                     bool is_converted=false;
00147                     double temp_value=MC_string_converter::value_of<double>(v_token[k],&is_converted);
00148                     if(is_converted==true)
00149                         vec[current_position++]=temp_value;
00150                 }
00151             }
00152         }
00153         return vec;
00154     }
00155 
00156     MC_v4d& MC_v4d::scale(const double& sx,const double& sy,const double& sz,const double& sw)
00157     {v[0]*=sx;v[1]*=sy;v[2]*=sz;v[3]*=sw;return *this;}
00158     MC_v4d MC_v4d::mask(const double& sx,const double& sy,const double& sz,const double& sw) const
00159     {return MC_v4d(v[0]*sx,v[1]*sy,v[2]*sz,v[3]*sw);}
00160     MC_v4d& MC_v4d::scale(const MC_v4d& scaling){v[0]*=scaling[0];v[1]*=scaling[1];v[2]*=scaling[2];v[3]*=scaling[3];return *this;}
00161     MC_v4d MC_v4d::mask(const MC_v4d& scaling) const{return MC_v4d(v[0]*scaling[0],v[1]*scaling[1],v[2]*scaling[2],v[3]*scaling[3]);}
00162     MC_v4d& MC_v4d::scale(const double& scaling){return (*this)*=scaling;}
00163     MC_v4d MC_v4d::scale(const double& scaling) const{return (*this)*scaling;}
00164 
00165 
00166     double MC_v4d::dot(const MC_v4d& vec) const
00167     {return v[0]*vec[0]+v[1]*vec[1]+v[2]*vec[2]+v[3]*vec[3];}
00168     double MC_v4d::norm() const
00169     {return sqrt(dot(*this));}
00170     MC_v4d MC_v4d::normalized() const
00171     {
00172         double epsilon=0.000001;
00173         double n=norm();
00174         if(n<epsilon)
00175         {std::cout<<"Warning in MC_v4d::normalized(), norm is "<<n<<std::endl; return MC_v4d(0,0,1,0);}
00176         return (*this)/n;
00177     }
00178 
00179 
00180 
00181 
00182     MC_v4d& MC_v4d::operator*=(const MC_matrix& M)
00183     {M.internal_product(this); return *this;}
00184 
00185     MC_v4d::MC_v4d(const MC_matrix& M)
00186     {
00187         if( ( M.size_1()==4 && M.size_2()==1) ||
00188             ( M.size_2()==4 && M.size_1()==1) )
00189         {v[0]=M(0);v[1]=M(1);v[2]=M(2);v[3]=M(3);}
00190     }
00191     MC_v4d& MC_v4d::set_v3d(const MC_v3d& vec)
00192     {v[0]=vec[0];v[1]=vec[1];v[2]=vec[2];return *this;}
00193 
00194     const double* MC_v4d::pointer() const
00195     {return &v[0];}
00196 
00197 }

Generated on Sun Apr 18 20:24:48 2010 by  doxygen 1.6.1