1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #ifndef __VECTORUTILS_HXX__
20 #define __VECTORUTILS_HXX__
28 /// Precision used for tests of 3D part of INTERP_KERNEL
29 #define VOL_PREC 1.0e-6
31 /// Default relative tolerance in epsilonEqualRelative
32 #define DEFAULT_REL_TOL 1.0e-6
34 /// Default absolute tolerance in epsilonEqual and epsilonEqualRelative
35 #define DEFAULT_ABS_TOL 5.0e-12
37 namespace INTERP_KERNEL
40 * @param a first point. Should point on a array of size at least equal to SPACEDIM.
41 * @param b second point. Should point on a array of size at least equal to SPACEDIM.
43 template<int SPACEDIM>
44 inline double getDistanceBtw2Pts(const double *a, const double *b)
47 for(int i=0;i<SPACEDIM;i++)
48 ret2+=(a[i]-b[i])*(a[i]-b[i]);
52 // -------------------------------------------------------------------
53 // Math operations for vectors represented by double[3] - arrays
54 // -------------------------------------------------------------------
57 * Copies a double[3] vector from src to dest
59 * @param src source vector
60 * @param dest destination vector
63 inline void copyVector3(const double* src, double* dest)
65 for(int i = 0 ; i < 3 ; ++i)
70 * Creates a string representation of a double[3] vector
72 * @param pt a 3-vector
73 * @return a string of the form [x, y, z]
75 inline const std::string vToStr(const double* pt)
77 std::stringstream ss(std::ios::out);
78 ss << "[" << pt[0] << ", " << pt[1] << ", " << pt[2] << "]";
83 * Calculates the cross product of two double[3] - vectors.
87 * @param res vector in which to store the result v1 x v2. It should not be one of v1 and v2.
89 inline void cross(const double* v1, const double* v2,double* res)
91 res[0] = v1[1]*v2[2] - v1[2]*v2[1];
92 res[1] = v1[2]*v2[0] - v1[0]*v2[2];
93 res[2] = v1[0]*v2[1] - v1[1]*v2[0];
97 * Calculates the dot product of two double[3] - vectors
100 * @param v2 vector v2
101 * @return dot (scalar) product v1.v2
103 inline double dot(const double* v1, const double* v2)
105 return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
109 * Calculates norm of a double[3] vector
111 * @param v a vector v
112 * @return euclidean norm of v
114 inline double norm(const double* v)
116 return sqrt(dot(v,v));
120 * Compares doubles using an absolute tolerance
121 * This is suitable mainly for comparisons with 0.0
123 * @param x first value
124 * @param y second value
125 * @param errTol maximum allowed absolute difference that is to be treated as equality
126 * @return true if |x - y| < errTol, false otherwise
128 inline bool epsilonEqual(const double x, const double y, const double errTol = DEFAULT_ABS_TOL)
130 return y < x ? x - y < errTol : y - x < errTol;
131 // return std::fabs(x - y) < errTol;
135 * Compares doubles using a relative tolerance
136 * This is suitable mainly for comparing larger values to each other. Before performing the relative test,
137 * an absolute test is performed to guard from problems when comparing to 0.0
139 * @param x first value
140 * @param y second value
141 * @param relTol maximum allowed relative difference that is to be treated as equality
142 * @param absTol maximum allowed absolute difference that is to be treated as equality
143 * @return true if |x - y| <= absTol or |x - y|/max(|x|,|y|) <= relTol, false otherwise
145 inline bool epsilonEqualRelative(const double x, const double y, const double relTol = DEFAULT_REL_TOL, const double absTol = DEFAULT_ABS_TOL)
147 // necessary for comparing values close to zero
148 // in order to avoid division by very small numbers
149 if(std::fabs(x - y) < absTol)
154 const double relError = std::fabs((x - y) / std::max(std::fabs(x), std::fabs(y)));
156 return relError < relTol;