LCOV - code coverage report
Current view: top level - ugbase/common/math/math_vector_matrix - math_vector_functions_common_impl.hpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 25.9 % 81 21
Test Date: 2025-09-21 23:31:46 Functions: 7.7 % 13 1

            Line data    Source code
       1              : /*
       2              :  * Copyright (c) 2009-2015:  G-CSC, Goethe University Frankfurt
       3              :  * Author: Sebastian Reiter
       4              :  * 
       5              :  * This file is part of UG4.
       6              :  * 
       7              :  * UG4 is free software: you can redistribute it and/or modify it under the
       8              :  * terms of the GNU Lesser General Public License version 3 (as published by the
       9              :  * Free Software Foundation) with the following additional attribution
      10              :  * requirements (according to LGPL/GPL v3 ��7):
      11              :  * 
      12              :  * (1) The following notice must be displayed in the Appropriate Legal Notices
      13              :  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
      14              :  * 
      15              :  * (2) The following notice must be displayed at a prominent place in the
      16              :  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
      17              :  * 
      18              :  * (3) The following bibliography is recommended for citation and must be
      19              :  * preserved in all covered files:
      20              :  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
      21              :  *   parallel geometric multigrid solver on hierarchically distributed grids.
      22              :  *   Computing and visualization in science 16, 4 (2013), 151-164"
      23              :  * "Vogel, A., Reiter, S., Rupp, M., N��gel, A., and Wittum, G. UG4 -- a novel
      24              :  *   flexible software system for simulating pde based models on high performance
      25              :  *   computers. Computing and visualization in science 16, 4 (2013), 165-179"
      26              :  * 
      27              :  * This program is distributed in the hope that it will be useful,
      28              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      29              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      30              :  * GNU Lesser General Public License for more details.
      31              :  */
      32              : 
      33              : ////////////////////////////////////////////////////////////////////////
      34              : ////////////////////////////////////////////////////////////////////////
      35              : //      This file supplies a general implementation of vector_functions.
      36              : //      The methods implemented here work on every vector-type, that supports
      37              : //      the vector-description in lgmath_vector_descriptor.h.
      38              : //      Since this implementation is quite general it is not the fastest.
      39              : //      One should consider implementing specializations for commonly used types
      40              : //      like vector3 etc.
      41              : ////////////////////////////////////////////////////////////////////////
      42              : 
      43              : #ifndef __H__COMMON__VECTOR_FUNCTIONS_COMMON_IMPL__
      44              : #define __H__COMMON__VECTOR_FUNCTIONS_COMMON_IMPL__
      45              : 
      46              : #include <cmath>
      47              : #include <algorithm>
      48              : #include "common/error.h"
      49              : #include "common/assert.h"
      50              : #include "common/static_assert.h"
      51              : 
      52              : namespace ug
      53              : {
      54              : 
      55              : template <typename vector_target_t, typename vector_source_t>
      56              : void VecCopy(vector_target_t& target, const vector_source_t& source,
      57              :                          typename vector_target_t::value_type fill)
      58              : {
      59              :         using std::min;
      60              :         size_t minSize = min(target.size(), source.size());
      61            0 :         for(size_t i = 0; i < minSize; ++i)
      62            0 :                 target[i] = source[i];
      63              : 
      64              :         for(size_t i = minSize; i < target.size(); ++i)
      65              :                 target[i] = fill;
      66              : }
      67              : 
      68              : ///     adds a MathVector<N>s to a second one
      69              : template <typename vector_t>
      70              : inline
      71              : void
      72              : VecAppend(vector_t& vOut, const vector_t& v1)
      73              : {
      74              :         typedef typename vector_t::size_type size_type;
      75            0 :         for(size_type i = 0; i < vOut.size(); ++i)
      76              :         {
      77            0 :                 vOut[i] += v1[i];
      78              :         }
      79              : }
      80              : 
      81              : ///     adds two MathVector<N>s and adds the result to a third one
      82              : template <typename vector_t>
      83              : inline
      84              : void
      85              : VecAppend(vector_t& vOut, const vector_t& v1, const vector_t& v2)
      86              : {
      87              :         typedef typename vector_t::size_type size_type;
      88              :         for(size_type i = 0; i < vOut.size(); ++i)
      89              :         {
      90              :                 vOut[i] += v1[i] + v2[i];
      91              :         }
      92              : }
      93              : 
      94              : ///     adds three Vectors and adds the result to a fourth one
      95              : template <typename vector_t>
      96              : inline
      97              : void
      98              : VecAppend(vector_t& vOut, const vector_t& v1, const vector_t& v2,
      99              :                                                         const vector_t& v3)
     100              : {
     101              :         typedef typename vector_t::size_type size_type;
     102              :         for(size_type i = 0; i < vOut.size(); ++i)
     103              :         {
     104              :                 vOut[i] += v1[i] + v2[i] + v3[i];
     105              :         }
     106              : }
     107              : 
     108              : ///     adds four Vectors and adds the result to a fifth one
     109              : template <typename vector_t>
     110              : inline
     111              : void
     112              : VecAppend(vector_t& vOut, const vector_t& v1, const vector_t& v2,
     113              :                         const vector_t& v3, const vector_t& v4)
     114              : {
     115              :         typedef typename vector_t::size_type size_type;
     116              :         for(size_type i = 0; i < vOut.size(); ++i)
     117              :         {
     118              :                 vOut[i] += v1[i] + v2[i] + v3[i] + v4[i];
     119              :         }
     120              : }
     121              : 
     122              : /// Scales a Vector and adds it to a second vector
     123              : template <typename vector_t>
     124              : inline
     125              : void
     126              : VecScaleAppend(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1)
     127              : {
     128              :         typedef typename vector_t::size_type size_type;
     129       450208 :         for(size_type i = 0; i < vOut.size(); ++i)
     130              :         {
     131       303012 :                 vOut[i] += s1 * v1[i];
     132              :         }
     133              : }
     134              : 
     135              : /// Scales two Vectors, adds the sum to a third vector
     136              : template <typename vector_t>
     137              : inline
     138              : void
     139              : VecScaleAppend(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
     140              :                                                                  typename vector_t::value_type s2, const vector_t& v2)
     141              : {
     142              :         typedef typename vector_t::size_type size_type;
     143          222 :         for(size_type i = 0; i < vOut.size(); ++i)
     144              :         {
     145          165 :                 vOut[i] += s1 * v1[i] + s2 * v2[i];
     146              :         }
     147              : }
     148              : 
     149              : /// Scales three Vectors, adds the sum to a fourth vector
     150              : template <typename vector_t>
     151              : inline
     152              : void
     153              : VecScaleAppend(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
     154              :                                                                  typename vector_t::value_type s2, const vector_t& v2,
     155              :                                                                  typename vector_t::value_type s3, const vector_t& v3)
     156              : {
     157              :         typedef typename vector_t::size_type size_type;
     158            0 :         for(size_type i = 0; i < vOut.size(); ++i)
     159              :         {
     160            0 :                 vOut[i] += s1 * v1[i] + s2 * v2[i] + s3 * v3[i];
     161              :         }
     162              : }
     163              : 
     164              : /// Scales four Vectors, adds the sum to a fifth vector
     165              : template <typename vector_t>
     166              : inline
     167              : void
     168              : VecScaleAppend(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
     169              :                                                                  typename vector_t::value_type s2, const vector_t& v2,
     170              :                                                                  typename vector_t::value_type s3, const vector_t& v3,
     171              :                                                                  typename vector_t::value_type s4, const vector_t& v4)
     172              : {
     173              :         typedef typename vector_t::size_type size_type;
     174            0 :         for(size_type i = 0; i < vOut.size(); ++i)
     175              :         {
     176            0 :                 vOut[i] += s1 * v1[i] + s2 * v2[i] + s3 * v3[i] + s4 * v4[i];
     177              :         }
     178              : }
     179              : 
     180              : 
     181              : ///     adds two MathVector<N>s and stores the result in a third one
     182              : template <typename vector_t>
     183              : inline
     184              : void
     185              : VecAdd(vector_t& vOut, const vector_t& v1, const vector_t& v2)
     186              : {
     187              :         typedef typename vector_t::size_type size_type;
     188            0 :         for(size_type i = 0; i < vOut.size(); ++i)
     189              :         {
     190            0 :                 vOut[i] = v1[i] + v2[i];
     191              :         }
     192              : }
     193              : 
     194              : ///     adds three Vectors and stores the result in a fourth one
     195              : template <typename vector_t>
     196              : inline
     197              : void
     198              : VecAdd(vector_t& vOut, const vector_t& v1, const vector_t& v2,
     199              :                                                         const vector_t& v3)
     200              : {
     201              :         typedef typename vector_t::size_type size_type;
     202            0 :         for(size_type i = 0; i < vOut.size(); ++i)
     203              :         {
     204            0 :                 vOut[i] = v1[i] + v2[i] + v3[i];
     205              :         }
     206              : }
     207              : 
     208              : ///     adds four Vectors and stores the result in a fifth one
     209              : template <typename vector_t>
     210              : inline
     211              : void
     212              : VecAdd(vector_t& vOut, const vector_t& v1, const vector_t& v2,
     213              :                         const vector_t& v3, const vector_t& v4)
     214              : {
     215              :         typedef typename vector_t::size_type size_type;
     216            0 :         for(size_type i = 0; i < vOut.size(); ++i)
     217              :         {
     218            0 :                 vOut[i] = v1[i] + v2[i] + v3[i] + v4[i];
     219              :         }
     220              : }
     221              : 
     222              : ///     subtracts v2 from v1 and stores the result in a vOut
     223              : template <typename vector_t>
     224              : inline
     225              : void
     226              : VecSubtract(vector_t& vOut, const vector_t& v1, const vector_t& v2)
     227              : {
     228              :         typedef typename vector_t::size_type size_type;
     229  16009505668 :         for(size_type i = 0; i < vOut.size(); ++i)
     230              :         {
     231  12243207060 :                 vOut[i] = v1[i] - v2[i];
     232              :         }
     233              : }
     234              : 
     235              : /// component-wise exponentiation of a vector
     236              : template <typename vector_t>
     237              : inline
     238              : void
     239              : VecPow(vector_t& vOut, const vector_t& v1, typename vector_t::value_type s)
     240              : {
     241              :         typedef typename vector_t::size_type size_type;
     242            0 :         for(size_type i = 0; i < vOut.size(); ++i)
     243              :         {
     244            0 :                 vOut[i] = std::pow(v1[i], s);
     245              :         }
     246              : }
     247              : 
     248              : ///     scales a MathVector<N>
     249              : template <typename vector_t>
     250              : inline
     251              : void
     252              : VecScale(vector_t& vOut, const vector_t& v, typename vector_t::value_type s)
     253              : {
     254              :         typedef typename vector_t::size_type size_type;
     255            0 :         for(size_type i = 0; i < vOut.size(); ++i)
     256              :         {
     257            0 :                 vOut[i] = s * v[i];
     258              :         }
     259              : }
     260              : 
     261              : /// Scales two Vectors, adds them and returns the sum in a third vector
     262              : template <typename vector_t>
     263              : inline
     264              : void
     265              : VecScaleAdd(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
     266              :                                                                  typename vector_t::value_type s2, const vector_t& v2)
     267              : {
     268              :         typedef typename vector_t::size_type size_type;
     269          393 :         for(size_type i = 0; i < vOut.size(); ++i)
     270              :         {
     271          288 :                 vOut[i] = s1 * v1[i] + s2 * v2[i];
     272              :         }
     273              : }
     274              : 
     275              : /// Scales three Vectors, adds them and returns the sum in a fourth vector
     276              : template <typename vector_t>
     277              : inline
     278              : void
     279              : VecScaleAdd(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
     280              :                                                                  typename vector_t::value_type s2, const vector_t& v2,
     281              :                                                                  typename vector_t::value_type s3, const vector_t& v3)
     282              : {
     283              :         typedef typename vector_t::size_type size_type;
     284            0 :         for(size_type i = 0; i < vOut.size(); ++i)
     285              :         {
     286            0 :                 vOut[i] = s1 * v1[i] + s2 * v2[i] + s3 * v3[i];
     287              :         }
     288              : }
     289              : 
     290              : /// Scales four Vectors, adds them and returns the sum in a fifth vector
     291              : template <typename vector_t>
     292              : inline
     293              : void
     294              : VecScaleAdd(vector_t& vOut, typename vector_t::value_type s1, const vector_t& v1,
     295              :                                                                  typename vector_t::value_type s2, const vector_t& v2,
     296              :                                                                  typename vector_t::value_type s3, const vector_t& v3,
     297              :                                                                  typename vector_t::value_type s4, const vector_t& v4)
     298              : {
     299              :         typedef typename vector_t::size_type size_type;
     300            0 :         for(size_type i = 0; i < vOut.size(); ++i)
     301              :         {
     302            0 :                 vOut[i] = s1 * v1[i] + s2 * v2[i] + s3 * v3[i] + s4 * v4[i];
     303              :         }
     304              : }
     305              : 
     306              : //      performs linear interpolation between two MathVector<N>s.
     307              : template <typename vector_t>
     308              : inline
     309              : void
     310              : VecInterpolateLinear(vector_t& vOut, const vector_t& v1, const vector_t& v2,
     311              :                                                 typename vector_t::value_type interpAmount)
     312              : {
     313              :         typedef typename vector_t::size_type size_type;
     314            0 :         for(size_type i = 0; i < vOut.size(); ++i)
     315              :         {
     316            0 :                 vOut[i] = (1. - interpAmount) * v1[i] + interpAmount * v2[i];
     317              :         }
     318              : }
     319              : 
     320              : ///     returns the squared length of v. Faster than VecLength.
     321              : template <typename vector_t>
     322              : inline
     323              : typename vector_t::value_type
     324              : VecLengthSq(const vector_t& v)
     325              : {
     326              :         typename vector_t::value_type len = 0;
     327              :         typedef typename vector_t::size_type size_type;
     328              : 
     329   3200002320 :         for(size_type i = 0; i < v.size(); ++i)
     330              :         {
     331   2240001680 :                 len += v[i] * v[i];
     332              :         }
     333              : 
     334              :         return len;
     335              : }
     336              : 
     337              : ///     returns the length of v. Slower than VecLengthSq.
     338              : template <typename vector_t>
     339              : inline
     340              : typename vector_t::value_type
     341              : VecLength(const vector_t& v)
     342              : {
     343              :         return static_cast<typename vector_t::value_type>(
     344          760 :                         std::sqrt(VecLengthSq(v)));
     345              : }
     346              : 
     347              : ///     returns the squared distance of two MathVector<N>s.
     348              : template <typename vector_t>
     349              : inline
     350              : typename vector_t::value_type
     351            0 : VecDistanceSq(const vector_t& v1, const vector_t& v2)
     352              : {
     353              :         vector_t v;
     354              :         VecSubtract(v, v1, v2);
     355            0 :         return VecLengthSq(v);
     356              : }
     357              : 
     358              : ///     returns the squared distance of two MathVector<N>s.
     359              : template <typename TVector, typename TMatrix>
     360              : inline
     361              : typename TVector::value_type
     362            0 : VecDistanceSq(const TVector& v1, const TVector& v2, const TMatrix& M)
     363              : {
     364              :         TVector delta;
     365              :         TVector deltaM;
     366              :         VecSubtract(delta, v1, v2);
     367              :         MatVecMult(deltaM, M, delta);
     368            0 :         return VecDot(deltaM, delta);
     369              : }
     370              : 
     371              : ///     returns the distance of two MathVector<N>s.
     372              : template <typename vector_t>
     373              : inline
     374              : typename vector_t::value_type
     375            0 : VecDistance(const vector_t& v1, const vector_t& v2)
     376              : {
     377              :         return static_cast<typename vector_t::value_type>
     378            0 :                         (std::sqrt(VecDistanceSq(v1, v2)));
     379              : }
     380              : 
     381              : ///     calculates the dot-product of two MathVector<N>s
     382              : template <typename vector_t>
     383              : inline
     384              : typename vector_t::value_type
     385              : VecDot(const vector_t& v1, const vector_t& v2)
     386              : {
     387              :         typename vector_t::value_type dp = 0;
     388              :         typedef typename vector_t::size_type size_type;
     389              : 
     390  16051003776 :         for(size_type i = 0; i < v1.size(); ++i)
     391              :         {
     392  11335439680 :                 dp += v1[i] * v2[i];
     393              :         }
     394              : 
     395              :         return dp;
     396              : }
     397              : 
     398              : template <typename vector_t>
     399              : inline
     400              : typename vector_t::value_type
     401            0 : VecAngle(const vector_t& v1, const vector_t& v2)
     402              : {
     403              :         typedef typename vector_t::value_type value_t;
     404              : 
     405            0 :         value_t l = sqrt(VecLengthSq(v1) * VecLengthSq(v2));
     406            0 :         if(l > 0){
     407            0 :                 number a = VecDot(v1, v2) / l;
     408            0 :                 if(a >= 1)
     409              :                         return 0;
     410            0 :                 else if(a <= -1)
     411              :                         return PI;
     412            0 :                 return acos(a);
     413              :         }
     414              : 
     415              :         return 0;
     416              : }
     417              : 
     418              : template <typename vector_t>
     419              : inline
     420              : typename vector_t::value_type
     421              : VecAngleNorm(const vector_t& v1, const vector_t& v2)
     422              : {
     423              :         number a = VecDot(v1, v2);
     424              :         if(a >= 1)
     425              :                 return 0;
     426              :         else if(a <= -1)
     427              :                 return PI;
     428              :         return acos(a);
     429              : }
     430              : 
     431              : ///     calculates the cross product of two MathVector<N>s of dimension 3. It makes no sense to use VecCross for
     432              : /// MathVector<N>s that have not dimension 3.
     433              : /// todo: Create compile-time check for dimensionality in case of MathVector type.
     434              : template <typename vector_t>
     435              : inline
     436              : void
     437    634770496 : VecCross(vector_t& vOut, const vector_t& v1, const vector_t& v2)
     438              : {
     439    634770496 :         if(&vOut != &v1 && &vOut != &v2)
     440              :         {
     441    634770496 :                 vOut[0] = v1[1] * v2[2] - v2[1] * v1[2];
     442    634770496 :                 vOut[1] = v1[2] * v2[0] - v2[2] * v1[0];
     443    634770496 :                 vOut[2] = v1[0] * v2[1] - v2[0] * v1[1];
     444              :         }
     445              :         else
     446              :         {
     447              :                 vector_t _temp;
     448            0 :                 _temp[0] = v1[1] * v2[2] - v2[1] * v1[2];
     449            0 :                 _temp[1] = v1[2] * v2[0] - v2[2] * v1[0];
     450            0 :                 _temp[2] = v1[0] * v2[1] - v2[0] * v1[1];
     451              :                 vOut = _temp;
     452              :         }
     453    634770496 : }
     454              : 
     455              : /** computes the "generalized vector product" of two vectors
     456              :  *
     457              :  * In 3d, this is a usual cross-product. In 2d, the first component
     458              :  * of the result is the determinant of the 2x2-matrix build of the operands,
     459              :  * and the second component is always 0.
     460              :  *
     461              :  * \tparam dim  dimensionality of the vectors
     462              :  */
     463              : template <size_t dim>
     464            0 : inline void GenVecCross
     465              : (
     466              :         MathVector<dim> & result, ///< = v_1 X v_2
     467              :         const MathVector<dim> & v_1, const MathVector<dim> & v_2
     468              : )
     469              : {
     470              :         /* Cf. the specializations below! */
     471            0 :         UG_THROW ("The generalized vector product is defined only in 2 and 3 dimensions");
     472              : };
     473              : 
     474              : /// specialization of the "generalized vector product" in 2d. \see GenVecCross
     475              : template <>
     476              : inline void GenVecCross<2>
     477              : (
     478              :         MathVector<2> & result,
     479              :         const MathVector<2> & v_1, const MathVector<2> & v_2
     480              : )
     481              : {
     482            0 :         result[0] = v_1[0] * v_2[1] - v_1[1] * v_2[0];
     483            0 :         result[1] = 0;
     484            0 : };
     485              : 
     486              : /// specialization of the "generalized vector product" in 3d. \see GenVecCross
     487              : template <>
     488              : inline void GenVecCross<3>
     489              : (
     490              :         MathVector<3> & result,
     491              :         const MathVector<3> & v_1, const MathVector<3> & v_2
     492              : )
     493              : {
     494            0 :         VecCross (result, v_1, v_2);
     495            0 : };
     496              : 
     497              : ///     scales a MathVector<N> to unit length
     498              : template <typename vector_t>
     499              : inline
     500              : void
     501            0 : VecNormalize(vector_t& vOut, const vector_t& v)
     502              : {
     503              :         typename vector_t::value_type len = VecLength(v);
     504            0 :         if(len > 0)
     505            0 :                 VecScale(vOut, v, (typename vector_t::value_type) 1 / len);
     506              :         else
     507              :                 vOut = v;
     508            0 : }
     509              : 
     510              : ///     Calculates a triangle-normal in 3d.
     511              : template <typename vector_t>
     512              : inline
     513              : void
     514            0 : CalculateTriangleNormalNoNormalize(vector_t& vOut, const vector_t& v1,
     515              :                                                 const vector_t& v2, const vector_t& v3)
     516              : {
     517              :         vector_t e1, e2;
     518              :         VecSubtract(e1, v2, v1);
     519              :         VecSubtract(e2, v3, v1);
     520              : 
     521            0 :         VecCross(vOut, e1, e2);
     522            0 : }
     523              : 
     524              : ///     Calculates a triangle-normal in 3d.
     525              : template <typename vector_t>
     526              : inline
     527              : void
     528              : CalculateTriangleNormal(vector_t& vOut, const vector_t& v1,
     529              :                                                 const vector_t& v2, const vector_t& v3)
     530              : {
     531            0 :         CalculateTriangleNormalNoNormalize(vOut, v1, v2, v3);
     532            0 :         VecNormalize(vOut, vOut);
     533              : }
     534              : 
     535              : /// Set each vector component to scalar
     536              : template <typename vector_t>
     537              : inline
     538              : void
     539              : VecSet(vector_t& vInOut, typename vector_t::value_type s)
     540              : {
     541              :         typedef typename vector_t::size_type size_type;
     542         2020 :         for(size_type i = 0; i < vInOut.size(); ++i)
     543              :         {
     544         1680 :                 vInOut[i] = s;
     545              :         }
     546              : }
     547              : 
     548              : /// Add a scalar to a vector (componentwise)
     549              : template <typename vector_t>
     550              : inline
     551              : void
     552              : VecAdd(vector_t& vOut, const vector_t& v, typename vector_t::value_type s)
     553              : {
     554              :         typedef typename vector_t::size_type size_type;
     555              :         for(size_type i = 0; i < vOut.size(); ++i)
     556              :         {
     557              :                 vOut[i] = v[i] + s;
     558              :         }
     559              : }
     560              : 
     561              : /// Subtract a scalar from a vector (componentwise)
     562              : template <typename vector_t>
     563              : inline
     564              : void
     565              : VecSubtract(vector_t& vOut, const vector_t& v, typename vector_t::value_type s)
     566              : {
     567              :         typedef typename vector_t::size_type size_type;
     568              :         for(size_type i = 0; i < vOut.size(); ++i)
     569              :         {
     570              :                 vOut[i] = v[i] - s;
     571              :         }
     572              : }
     573              : 
     574              : template <typename vector_t>
     575              : inline
     576              : typename vector_t::value_type
     577              : VecTwoNorm(const vector_t& v)
     578              : {
     579              :         return VecLength(v);
     580              : }
     581              : 
     582              : template <typename vector_t>
     583              : inline
     584              : typename vector_t::value_type
     585              : VecTwoNormSq(const vector_t& v)
     586              : {
     587              :         return VecLengthSq(v);
     588              : }
     589              : 
     590              : template <typename vector_t>
     591              : inline
     592              : typename vector_t::value_type
     593              : VecOneNorm(const vector_t& v)
     594              : {
     595              :         typename vector_t::value_type len = 0;
     596              :         typedef typename vector_t::size_type size_type;
     597              : 
     598              :         for(size_type i = 0; i < v.size(); ++i)
     599              :         {
     600              :                 len += std::abs(v[i]);
     601              :         }
     602              : 
     603              :         return len;
     604              : }
     605              : 
     606              : template <typename vector_t>
     607              : inline
     608              : typename vector_t::value_type
     609              : VecPNorm(const vector_t& v, unsigned int p)
     610              : {
     611              :         typename vector_t::value_type len = 0;
     612              :         typedef typename vector_t::size_type size_type;
     613              : 
     614              :         for(size_type i = 0; i < v.size(); ++i)
     615              :         {
     616              :                 len += std::pow(v[i], p);
     617              :         }
     618              : 
     619              :         return std::pow(len, (typename vector_t::value_type) 1/ p);
     620              : }
     621              : 
     622              : template <typename vector_t>
     623              : inline
     624              : typename vector_t::value_type
     625              : VecMaxNorm(const vector_t& v)
     626              : {
     627              :         typename vector_t::value_type m = 0;
     628              :         typedef typename vector_t::size_type size_type;
     629              : 
     630              :         for(size_type i = 0; i < v.size(); ++i)
     631              :         {
     632              :                 m = std::max(m, v[i]);
     633              :         }
     634              : 
     635              :         return m;
     636              : }
     637              : 
     638              : template <typename vector_t>
     639              : inline
     640              : typename vector_t::value_type
     641              : VecInftyNorm(const vector_t& v)
     642              : {
     643              :         return VecMaxNorm(v);
     644              : }
     645              : 
     646              : 
     647              : ///     component-wise product
     648              : template <typename vector_t>
     649              : inline
     650              : void
     651              : VecElemProd(vector_t& vOut, const vector_t& v1, const vector_t& v2)
     652              : {
     653              :         typedef typename vector_t::size_type size_type;
     654              :         for(size_type i = 0; i < vOut.size(); ++i)
     655              :         {
     656              :                 vOut[i] = v1[i] * v2[i];
     657              :         }
     658              : }
     659              : 
     660              : ///     component-wise square root
     661              : template <typename vector_t>
     662              : inline
     663              : void
     664              : VecElemSqrt(vector_t& vOut, const vector_t& v1)
     665              : {
     666              :         typedef typename vector_t::size_type size_type;
     667              :         for(size_type i = 0; i < vOut.size(); ++i)
     668              :         {
     669              :                 vOut[i] = sqrt(v1[i]);
     670              :         }
     671              : }
     672              : 
     673              : ///     component-wise comparison of two vectors (in the absolute values)
     674              : template <typename vector_t>
     675              : inline
     676              : bool
     677              : VecAbsIsLess(const vector_t& v1, const vector_t& v2)
     678              : {
     679            0 :         for(typename vector_t::size_type i = 0; i < v1.size(); ++i)
     680            0 :                 if (std::abs (v1[i]) >= std::abs (v2[i]))
     681              :                         return false;
     682              :         return true;
     683              : }
     684              : 
     685              : ///     component-wise comparison of a vector (in the absolute values) with a given number
     686              : template <typename vector_t>
     687              : inline
     688              : bool
     689              : VecAbsIsLess(const vector_t& v1, const typename vector_t::value_type s)
     690              : {
     691            0 :         for(typename vector_t::size_type i = 0; i < v1.size(); ++i)
     692            0 :                 if (std::abs (v1[i]) >= s)
     693              :                         return false;
     694              :         return true;
     695              : }
     696              : 
     697              : /// checks if the given point is in the bounding box given by two other points
     698              : template <typename vector_t>
     699              : inline
     700              : bool
     701              : VecIsInBB(const vector_t& v, const vector_t& low, const vector_t& high)
     702              : {
     703              :         for(typename vector_t::size_type i = 0; i < v.size(); ++i)
     704              :                 if (v[i] < low[i] || high[i] < v[i])
     705              :                         return false;
     706              :         return true;
     707              : }
     708              : 
     709              : }//     end of namespace
     710              : 
     711              : #endif /* __H__COMMON__MathVector_FUNCTIONS_COMMON_IMPL__ */
     712              : 
        

Generated by: LCOV version 2.0-1