Tulip  4.6.0
Better Visualization Through Research
library/tulip-core/include/tulip/cxx/Vector.cxx
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 #include <cstring>
00020 
00021 #define VECTORTLP tlp::Vector<TYPE, SIZE, OTYPE, DTYPE>
00022 
00023 //======================================================
00024 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
00025 //VECTORTLP & VECTORTLP::operator*=(const OTYPE scalaire) {
00026 //    assert( scalaire >= std::numeric_limits<TYPE>::min() && scalaire <= std::numeric_limits<TYPE>::max());
00027 //    return (*this) *= static_cast<TYPE>(scalaire);
00028 //}
00029 //======================================================
00030 TEMPLATEVECTOR
00031 VECTORTLP & VECTORTLP::operator*=(const TYPE scalaire) {
00032   for (unsigned int i=0; i<SIZE; ++i)
00033     (*this)[i]*=scalaire;
00034 
00035   return (*this);
00036 }
00037 //======================================================
00038 TEMPLATEVECTOR
00039 VECTORTLP & VECTORTLP::operator*=(const VECTORTLP &vecto) {
00040   for (unsigned int i=0; i<SIZE; ++i)
00041     (*this)[i]*=vecto[i];
00042 
00043   return (*this);
00044 }
00045 //======================================================
00046 TEMPLATEVECTOR
00047 VECTORTLP & VECTORTLP::operator/=(const TYPE scalaire) {
00048   assert(scalaire!=static_cast<TYPE>(0));
00049 
00050   for (unsigned int i=0; i<SIZE; ++i)
00051     (*this)[i]/=scalaire;
00052 
00053   return (*this);
00054 }
00055 //======================================================
00056 //TEMPLATEVECTOR
00057 //VECTORTLP & VECTORTLP::operator/=(const OTYPE scalaire) {
00058 //  assert( scalaire >= std::numeric_limits<TYPE>::min() && scalaire <= std::numeric_limits<TYPE>::max());
00059 //  return (*this) /= static_cast<TYPE>(scalaire);
00060 //}
00061 //======================================================
00062 TEMPLATEVECTOR
00063 VECTORTLP & VECTORTLP::operator/=(const VECTORTLP &vecto) {
00064   for (unsigned int i=0; i<SIZE; ++i) {
00065     assert(vecto[i]!=static_cast<TYPE>(0));
00066     (*this)[i]/=vecto[i];
00067   }
00068 
00069   return (*this);
00070 }
00071 //======================================================
00072 TEMPLATEVECTOR
00073 VECTORTLP & VECTORTLP::operator+=(const TYPE scalaire) {
00074   for (unsigned int i=0; i<SIZE; ++i)
00075     (*this)[i]+=scalaire;
00076 
00077   return (*this);
00078 }
00079 
00080 //TEMPLATEVECTOR
00081 //VECTORTLP & VECTORTLP::operator+=(const OTYPE scalaire) {
00082 //    assert( scalaire >= std::numeric_limits<TYPE>::min() && scalaire <= std::numeric_limits<TYPE>::max());
00083 //    return (*this) += static_cast<TYPE>(scalaire);
00084 //}
00085 //======================================================
00086 TEMPLATEVECTOR
00087 VECTORTLP & VECTORTLP::operator+=(const VECTORTLP &vecto) {
00088   for (unsigned int i=0; i<SIZE; ++i)
00089     (*this)[i]+=vecto[i];
00090 
00091   return (*this);
00092 }
00093 //======================================================
00094 TEMPLATEVECTOR
00095 VECTORTLP & VECTORTLP::operator-=(const TYPE scalaire) {
00096   for (unsigned int i=0; i<SIZE; ++i)
00097     (*this)[i]-=scalaire;
00098 
00099   return (*this);
00100 }
00101 //TEMPLATEVECTOR
00102 //VECTORTLP & VECTORTLP::operator-=(const OTYPE scalaire) {
00103 //    assert( scalaire >= std::numeric_limits<TYPE>::min() && scalaire <= std::numeric_limits<TYPE>::max());
00104 //    return (*this) -= static_cast<TYPE>(scalaire);
00105 //}
00106 //======================================================
00107 TEMPLATEVECTOR
00108 VECTORTLP & VECTORTLP::operator-=(const VECTORTLP &vecto) {
00109   for (unsigned int i=0; i<SIZE; ++i)
00110     (*this)[i]-=vecto[i];
00111 
00112   return (*this);
00113 }
00114 //======================================================
00115 TEMPLATEVECTOR
00116 VECTORTLP & VECTORTLP::operator^=(const VECTORTLP &v) {
00117   (*this) = (*this) ^ v;
00118   return (*this);
00119 }
00120 //======================================================
00121 TEMPLATEVECTOR
00122 VECTORTLP tlp::operator+(const VECTORTLP &u, const VECTORTLP &v) {
00123   return VECTORTLP(u)+=v;
00124 }
00125 //======================================================
00126 TEMPLATEVECTOR
00127 VECTORTLP tlp::operator+(const VECTORTLP &u, const TYPE scalaire) {
00128   return VECTORTLP(u)+=scalaire;
00129 }
00130 //======================================================
00131 //TEMPLATEVECTOR
00132 //VECTORTLP tlp::operator+(const VECTORTLP &u, const OTYPE scalaire) {
00133 //    return VECTORTLP(u) += static_cast<TYPE>(scalaire);
00134 //}
00135 //======================================================
00136 TEMPLATEVECTOR
00137 VECTORTLP tlp::operator-(const VECTORTLP &u, const VECTORTLP &v) {
00138   return VECTORTLP(u) -= v;
00139 }
00140 //======================================================
00141 TEMPLATEVECTOR
00142 VECTORTLP tlp::operator-(const VECTORTLP &u, const TYPE scalaire) {
00143   return VECTORTLP(u) -= scalaire;
00144 }
00145 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
00146 //VECTORTLP tlp::operator-(const VECTORTLP &u, const OTYPE scalaire) {
00147 //  return VECTORTLP(u) -= static_cast<TYPE>(scalaire);
00148 //}
00149 //======================================================
00150 TEMPLATEVECTOR
00151 VECTORTLP tlp::operator*(const VECTORTLP &u, const VECTORTLP &v) {
00152   return VECTORTLP(u)*=v;
00153 }
00154 //======================================================
00155 TEMPLATEVECTOR
00156 VECTORTLP  tlp::operator*(const VECTORTLP &u, const TYPE scalaire) {
00157   return VECTORTLP(u)*=scalaire;
00158 }
00159 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
00160 //VECTORTLP  tlp::operator*(const VECTORTLP &u, const OTYPE scalaire) {
00161 //  return VECTORTLP(u)*=scalaire;
00162 //}
00163 //======================================================
00164 TEMPLATEVECTOR
00165 VECTORTLP  tlp::operator*(const TYPE scalaire, const VECTORTLP &u ) {
00166   return VECTORTLP(u)*=scalaire;
00167 }
00168 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
00169 //VECTORTLP  tlp::operator*(const OTYPE scalaire, const VECTORTLP &u ) {
00170 //  return VECTORTLP(u)*=scalaire;
00171 //}
00172 //======================================================
00173 TEMPLATEVECTOR
00174 VECTORTLP tlp::operator/(const VECTORTLP &u, const VECTORTLP &v) {
00175   return VECTORTLP(u)/=v;
00176 }
00177 //======================================================
00178 TEMPLATEVECTOR
00179 VECTORTLP tlp::operator/(const VECTORTLP &u, const TYPE scalaire) {
00180   return VECTORTLP(u)/=scalaire;
00181 }
00182 //template <typename TYPE,unsigned int SIZE, typename OTYPE>
00183 //VECTORTLP tlp::operator/(const VECTORTLP &u, const OTYPE scalaire) {
00184 //  return VECTORTLP(u)/=scalaire;
00185 //}
00186 //======================================================
00187 TEMPLATEVECTOR
00188 VECTORTLP tlp::operator^(const VECTORTLP &u, const VECTORTLP &v) {
00189 
00190   switch(SIZE) {
00191   case 3:
00192     return VECTORTLP(
00193              static_cast<TYPE>(
00194                static_cast<OTYPE>(u.y()) * static_cast<OTYPE>(v.z())
00195                -
00196                static_cast<OTYPE>(u.z()) * static_cast<OTYPE>(v.y())
00197              )
00198              ,
00199              static_cast<TYPE>(
00200                static_cast<OTYPE>(u.z()) * static_cast<OTYPE>(v.x())
00201                -
00202                static_cast<OTYPE>(u.x()) * static_cast<OTYPE>(v.z())
00203              )
00204              ,
00205              static_cast<TYPE>(
00206                static_cast<OTYPE>(u.x()) * static_cast<OTYPE>(v.y())
00207                -
00208                static_cast<OTYPE>(u.y()) * static_cast<OTYPE>(v.x())
00209              )
00210            );
00211     break;
00212 
00213   default :
00214     tlp::warning() << "cross product not implemented for dimension :" << SIZE << std::endl;
00215     VECTORTLP result;
00216     return result;
00217     break;
00218   }
00219 }
00220 //======================================================
00221 TEMPLATEVECTOR
00222 VECTORTLP tlp::operator-(const VECTORTLP &u) {
00223   return VECTORTLP(u) *= static_cast<TYPE>(-1);
00224 }
00225 //======================================================
00226 TEMPLATEVECTOR
00227 bool VECTORTLP::operator>(const VECTORTLP &vecto) const {
00228   return vecto < (*this);
00229 }
00230 //======================================================
00231 TEMPLATEVECTOR
00232 bool VECTORTLP::operator<(const VECTORTLP &v) const {
00233   if (std::numeric_limits<TYPE>::is_integer) {
00234     return memcmp(&((*this).array[0]), (void*)&(v.array[0]), SIZE * sizeof(TYPE)) < 0;
00235   }
00236 
00237   for (unsigned int i=0; i<SIZE; ++i) {
00238     OTYPE tmp = static_cast<OTYPE>((*this)[i]) - static_cast<OTYPE>(v[i]);
00239 
00240     if (tmp > sqrt(std::numeric_limits<TYPE>::epsilon()) || tmp < -sqrt(std::numeric_limits<TYPE>::epsilon())) {
00241       if (tmp > 0) return false;
00242 
00243       if (tmp < 0) return true;
00244     }
00245   }
00246 
00247   return false;
00248 }
00249 //======================================================
00250 TEMPLATEVECTOR
00251 bool VECTORTLP::operator!=(const VECTORTLP &vecto) const {
00252   return (! ( (*this) == vecto ));
00253 }
00254 //======================================================
00255 TEMPLATEVECTOR
00256 bool VECTORTLP::operator==(const VECTORTLP &v) const {
00257   if (std::numeric_limits<TYPE>::is_integer) {
00258     return memcmp((void*)&((*this).array[0]), (void*)&(v.array[0]), SIZE * sizeof(TYPE)) == 0;
00259   }
00260 
00261   for (unsigned int i=0; i<SIZE; ++i) {
00262     OTYPE tmp = static_cast<OTYPE>((*this)[i]) - static_cast<OTYPE>(v[i]);
00263 
00264     if (tmp > sqrt(std::numeric_limits<TYPE>::epsilon()) || tmp < -sqrt(std::numeric_limits<TYPE>::epsilon())) {
00265       return false;
00266     }
00267   }
00268 
00269   return true;
00270 }
00271 //======================================================
00272 TEMPLATEVECTOR
00273 TYPE VECTORTLP::dotProduct(const VECTORTLP &v) const {
00274   assert (SIZE>0);
00275   OTYPE tmpO = static_cast<OTYPE>(VECTORTLP::array[0]) * static_cast<OTYPE>(v[0]);
00276 
00277   for (unsigned int i=1; i<SIZE; ++i)
00278     tmpO += static_cast<OTYPE>((*this)[i]) * static_cast<OTYPE>(v[i]);
00279 
00280   return static_cast<TYPE>(tmpO);
00281 }
00282 //======================================================
00283 TEMPLATEVECTOR
00284 VECTORTLP & VECTORTLP::fill(const TYPE scalaire) {
00285   for (unsigned int i=0; i<SIZE; ++i)
00286     (*this)[i]=scalaire;
00287 
00288   return (*this);
00289 }
00290 //======================================================
00291 TEMPLATEVECTOR
00292 TYPE  VECTORTLP::norm() const {
00293   switch(SIZE) {
00294   case 1:
00295     return VECTORTLP::array[0];
00296 
00297   case 2:
00298     return tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>(x())
00299                                 +
00300                                 tlpsqr<TYPE, OTYPE>(y())
00301                                );
00302 
00303   case 3:
00304     return tlpsqrt<TYPE, OTYPE>(tlpsqr<TYPE, OTYPE>(x())
00305                                 +
00306                                 tlpsqr<TYPE, OTYPE>(y())
00307                                 +
00308                                 tlpsqr<TYPE, OTYPE>(z())
00309                                );
00310 
00311   default :
00312     OTYPE tmp = tlpsqr<TYPE, OTYPE>((*this)[0]);
00313 
00314     for (unsigned int i=1; i<SIZE; ++i)
00315       tmp += tlpsqr<TYPE, OTYPE>((*this)[i]);
00316 
00317     return(tlpsqrt<TYPE, OTYPE>(tmp));
00318   }
00319 }
00320 //======================================================
00321 TEMPLATEVECTOR
00322 DTYPE  VECTORTLP::dist(const VECTOR &c) const {
00323   switch(SIZE) {
00324   case 1:
00325     return static_cast<TYPE>(fabs(x()-c.x()));
00326 
00327   case 2:
00328     return tlpsqrt<DTYPE, OTYPE>(tlpsqr<DTYPE, OTYPE>(x()-c.x())
00329                                  +
00330                                  tlpsqr<DTYPE, OTYPE>(y()-c.y()));
00331 
00332   case 3:
00333     return tlpsqrt<DTYPE, OTYPE>(tlpsqr<DTYPE, OTYPE>(x()-c.x())
00334                                  +
00335                                  tlpsqr<DTYPE, OTYPE>(y()-c.y())
00336                                  +
00337                                  tlpsqr<DTYPE, OTYPE>(z()-c.z())
00338                                 );
00339 
00340   default :
00341     OTYPE tmp = tlpsqr<DTYPE, OTYPE>((*this)[0] - c[0]);
00342 
00343     for (unsigned int i=1; i<SIZE; ++i)
00344       tmp += tlpsqr<DTYPE, OTYPE>((*this)[i]-c[i]);
00345 
00346     return(tlpsqrt<DTYPE, OTYPE>(tmp));
00347   }
00348 }
00349 
00350 #undef VECTORTLP
 All Classes Files Functions Variables Enumerations Enumerator Properties