Back to index

salome-med  6.5.0
VectorUtils.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #ifndef __VECTORUTILS_HXX__
00021 #define __VECTORUTILS_HXX__
00022 
00023 #include <sstream>
00024 #include <numeric>
00025 #include <string>
00026 #include <cmath>
00027 #include <map>
00028 
00029 namespace INTERP_KERNEL
00030 {
00032   const double VOL_PREC = 1.0e-6;
00033   
00035   const double DEFAULT_REL_TOL = 1.0e-6;
00036   
00038   const double DEFAULT_ABS_TOL = 5.0e-12;
00039 
00044   template<int SPACEDIM>
00045   inline double getDistanceBtw2Pts(const double *a, const double *b)
00046   {
00047     double ret2=0.;
00048     for(int i=0;i<SPACEDIM;i++)
00049       ret2+=(a[i]-b[i])*(a[i]-b[i]);
00050     return sqrt(ret2);
00051   }
00052 
00053   // -------------------------------------------------------------------
00054   // Math operations for vectors represented by double[3] - arrays  
00055   // -------------------------------------------------------------------
00056   
00064   inline void copyVector3(const double* src, double* dest)
00065   {
00066     for(int i = 0 ; i < 3 ; ++i)
00067       dest[i] = src[i];
00068   }
00069   
00076   inline const std::string vToStr(const double* pt)
00077   {
00078     std::stringstream ss(std::ios::out);
00079     ss << "[" << pt[0] << ", " << pt[1] << ", " << pt[2] << "]";
00080     return ss.str();
00081   }
00082 
00089   inline void add(const double* v, double* res)
00090   {
00091     res[0] += v[0];
00092     res[1] += v[1];
00093     res[2] += v[2];
00094   }
00095 
00103   inline void cross(const double* v1, const double* v2,double* res)
00104   {
00105     res[0] = v1[1]*v2[2] - v1[2]*v2[1];
00106     res[1] = v1[2]*v2[0] - v1[0]*v2[2];
00107     res[2] = v1[0]*v2[1] - v1[1]*v2[0];
00108   }
00109 
00117   inline double dot(const double* v1, const double* v2)
00118   {
00119     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
00120   }
00121 
00128   inline double norm(const double* v)
00129   {
00130     return sqrt(dot(v,v));
00131   }
00132 
00142   inline bool epsilonEqual(const double x, const double y, const double errTol = DEFAULT_ABS_TOL)
00143   {
00144     return y < x ? x - y < errTol : y - x < errTol;
00145     //    return std::fabs(x - y) < errTol;
00146   }
00147 
00159   inline bool epsilonEqualRelative(const double x, const double y, const double relTol = DEFAULT_REL_TOL, const double absTol = DEFAULT_ABS_TOL)
00160   {
00161     // necessary for comparing values close to zero
00162     // in order to avoid division by very small numbers
00163     if(std::fabs(x - y) < absTol)
00164       {
00165         return true;
00166       }
00167 
00168     const double relError = std::fabs((x - y) / std::max(std::fabs(x), std::fabs(y)));
00169 
00170     return relError < relTol;
00171   }
00172 
00173 }
00174 
00175 #endif