Math.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_MATH_H
8 #define NUKLEI_MATH_H
9 
10 #include <nuklei/Common.h>
12 
13 #include <iostream>
14 #include <cmath>
15 
16 namespace nuklei {
17 
18 #define NUKLEI_RANGE_CHECK(value, low, high)\
19 {\
20  if (! (low <= value && value <= high))\
21  {\
22  NUKLEI_WARN("Range Error:" << NUKLEI_NVP((low <= value && value <= high))\
23  << "\n" << NUKLEI_NVP(high)\
24  << "\n" << NUKLEI_NVP(value)\
25  << "\n" << NUKLEI_NVP(low))\
26  }\
27 }
28 
29  template<typename T> T ACos(T fValue)
30  {
31  if ( -(T)1.0 < fValue )
32  {
33  if ( fValue < (T)1.0 )
34  return (T)std::acos((double)fValue);
35  else
36  return (T)0.0;
37  }
38  else
39  {
40  return M_PI;
41  }
42  }
43 
44  template<typename T> T FastACos(T fValue)
45  {
46  NUKLEI_FAST_ASSERT(-1-FLOATTOL < fValue && fValue < 1+FLOATTOL);
47  if (fValue >= 1) return 0;
48  else if (fValue >= 0)
49  return nuklei_wmf::Math<coord_t>::FastInvCos0( fValue );
50  else if (fValue > -1)
51  return M_PI - nuklei_wmf::Math<coord_t>::FastInvCos0( -fValue );
52  else return M_PI;
53  }
54 
55  template<typename T> T ASin(T fValue)
56  {
57  if ( -(T)1.0 < fValue )
58  {
59  if ( fValue < (T)1.0 )
60  return (T)std::asin((double)fValue);
61  else
62  return M_PI/2.0;
63  }
64  else
65  {
66  return -M_PI/2.0;
67  }
68  }
69 
70  template<typename T> T FastNegExp(T fValue)
71  {
72  NUKLEI_FAST_ASSERT(0-1e-6 < fValue);
73  if (fValue < 0) fValue = 0;
74  return nuklei_wmf::Math<coord_t>::FastNegExp3( fValue );
75  }
76 
77  inline coord_t
78  trivariateGaussian(const Vector3 &x, const Vector3 &m, const Matrix3 &cov,
79  const weight_t w = 1)
80  {
81  const Vector3 diff = x-m;
82  const Matrix3 inv = cov.Inverse();
83  const Vector3 tmp = inv * diff;
84  double val = diff.Dot(tmp);
85  double ret = std::exp(-.5 * val);
86  ret /= std::pow(2*M_PI, 3.0/2) * std::sqrt(cov.Determinant());
87  return w*ret;
88  }
89 
90  inline coord_t
91  trivariateGaussianDistance(const Vector3 &x, const Vector3 &m,
92  const Matrix3 &cov,
93  const weight_t w = 1)
94  {
95  const Vector3 diff = x-m;
96  const Matrix3 inv = cov.Inverse();
97  const Vector3 tmp = inv * diff;
98  double val = diff.Dot(tmp);
99 
100  double ret = -.5 * val + std::log(w) - 3.0/2 * std::log(2*M_PI) -
101  .5 * std::log(cov.Determinant());
102 
103  return -ret;
104  }
105 
106  // relative float equality test
107  bool rfe(const double a, const double b, double tol = 1e-6);
108 
109  // absolute float equality test
110  inline bool afe(const double a, const double b, double tol = 1e-6)
111  {
112  // warning: !(std::fabs(a-b) <= tol) is not eq to (std::fabs(a-b) > tol) because of nan/inf
113  return (std::fabs(a-b) <= tol);
114  }
115 
116 
117  template<typename T>
118  T angularDistance(T a, T b)
119  {
120  T d = std::fabs( atan2( std::sin(a - b), std::cos(a - b) ) ) / M_PI;
121  if (d > 1)
122  {
123  NUKLEI_FAST_ASSERT(afe(d, 1));
124  d = 1;
125  }
126  return d;
127  }
128 
129  double confluentHypergeometric1F1(const double a, const double b, const double x);
130  double besselI1(const double x);
131 
132 
133 
134 }
135 
136 #endif
137 
Public namespace.
Definition: Color.cpp:9
double weight_t
Type for particle weights.
Definition: Definitions.h:31
double coord_t
Type for point coordinates.
Definition: Definitions.h:25
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.