Version: 6.3.1

src/INTERP_KERNEL/VectorUtils.hxx

Go to the documentation of this file.
00001 // Copyright (C) 2007-2011  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 
00090   inline void cross(const double* v1, const double* v2,double* res)
00091   {
00092     res[0] = v1[1]*v2[2] - v1[2]*v2[1];
00093     res[1] = v1[2]*v2[0] - v1[0]*v2[2];
00094     res[2] = v1[0]*v2[1] - v1[1]*v2[0];
00095   }
00096 
00104   inline double dot(const double* v1, const double* v2)
00105   {
00106     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
00107   }
00108 
00115   inline double norm(const double* v)
00116   {
00117     return sqrt(dot(v,v));
00118   }
00119 
00129   inline bool epsilonEqual(const double x, const double y, const double errTol = DEFAULT_ABS_TOL)
00130   {
00131     return y < x ? x - y < errTol : y - x < errTol;
00132     //    return std::fabs(x - y) < errTol;
00133   }
00134 
00146   inline bool epsilonEqualRelative(const double x, const double y, const double relTol = DEFAULT_REL_TOL, const double absTol = DEFAULT_ABS_TOL)
00147   {
00148     // necessary for comparing values close to zero
00149     // in order to avoid division by very small numbers
00150     if(std::fabs(x - y) < absTol)
00151       {
00152         return true;
00153       }
00154 
00155     const double relError = std::fabs((x - y) / std::max(std::fabs(x), std::fabs(y)));
00156 
00157     return relError < relTol;
00158   }
00159 
00160 }
00161 
00162 #endif
Copyright © 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE
Copyright © 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS