17T sgn(
const T& v) {
return (T(0) < v) - (v < T(0)); }
20T sign(
const T&v) {
return (v >= T(0)) ? T(1) : T(-1); }
23T sign(
const T& a,
const T& b) {
return (b < 0) ? -std::abs(a) : std::abs(a); }
26T step(
const T& a,
const T& b) {
return (b < a) ? T(0) : T(1); }
29T plus(
const T& v) {
return v >= T(0) ? v : T(0); }
32T clamp(
const T& v,
const T& a,
const T& b) {
return v > b ? b : (v < a ? a : v); }
35T saturate(
const T& v) {
return v > T(1) ? T(1) : (v < T(0) ? T(0) : v); }
38T lerp(
const T& a,
const T& b, T t) {
return (1 - t) * a + t * b; }
41T map(
const T& v,
const T& a,
const T& b,
const T& c,
const T& d) {
return c + (d - c) * ((v - a) / (b - a)); }
49 static const int ncof = 28;
50 static const double cof[28] = {
51 -1.3026537197817094, 6.4196979235649026e-1,
52 1.9476473204185836e-2,-9.561514786808631e-3,-9.46595344482036e-4,
53 3.66839497852761e-4,4.2523324806907e-5,-2.0278578112534e-5,
54 -1.624290004647e-6,1.303655835580e-6,1.5626441722e-8,-8.5238095915e-8,
55 6.529054439e-9,5.059343495e-9,-9.91364156e-10,-2.27365122e-10,
56 9.6467911e-11, 2.394038e-12,-6.886027e-12,8.94487e-13, 3.13092e-13,
57 -1.12708e-13,3.81e-16,7.106e-15,-1.523e-15,-9.4e-17,1.21e-16,-2.8e-17
61 double t, ty, tmp, d = 0.0, dd = 0.0;
66 for (j = ncof - 1; j > 0; j--)
69 d = ty * d - dd + cof[j];
72 return static_cast<T
>(t*exp(-z * z + 0.5*(cof[0] + ty * d) - dd));
82 return T(1.0) - detail::erfccheb(x);
84 return detail::erfccheb(-x) - T(1.0);
92 return detail::erfccheb(x);
94 return 2.0 - detail::erfccheb(-x);
101 return exp(x*x)* erfccheb(x);
108 double x, err, t, pp;
109 if (p >= 2.0)
return -100.;
110 if (p <= 0.0)
return 100.;
111 pp = (p < 1.0) ? p : 2. - p;
112 t = sqrt(-2.*log(pp / 2.));
113 x = -0.70711*((2.30753 + t * 0.27061) / (1. + t * (0.99229 + t * 0.04481)) - t);
114 for (
int j = 0; j < 2; j++) {
116 x += err / (1.12837916709551257*exp(-(x*x)) - x * err);
118 return (p < 1.0 ? x : -x);
125 return erfc_inv(1. - p);
133 return (T)(0.5*erfc(-x*sqrt(0.5)));
140 static T a[] = {(T)-3.969683028665376e+01,(T) 2.209460984245205e+02,(T) -2.759285104469687e+02,(T) 1.383577518672690e+02,(T) -3.066479806614716e+01,(T) 2.506628277459239e+00};
141 static T b[] = {(T)-5.447609879822406e+01,(T) 1.615858368580409e+02,(T) -1.556989798598866e+02,(T) 6.680131188771972e+01,(T) -1.328068155288572e+01};
142 static T c[] = {(T)-7.784894002430293e-03,(T)-3.223964580411365e-01,(T)-2.400758277161838e+00,(T)-2.549732539343734e+00,(T) 4.374664141464968e+00,(T) 2.938163982698783e+00};
143 static T d[] = {(T)7.784695709041462e-03,(T)3.224671290700398e-01,(T)2.445134137142996e+00,(T)3.754408661907416e+00};
146 return -std::numeric_limits<T>::infinity();
148 return std::numeric_limits<T>::infinity();
150 if (p < (T)0.02425) {
151 T q = sqrt(-(T)2.0*log(p));
152 x = (((((c(1)*q+c(2))*q+c(3))*q+c(4))*q+c(5))*q+c(6)) /
153 ((((d(1)*q+d(2))*q+d(3))*q+d(4))*q+1);
155 else if (p <= (T)0.97575) {
158 x = (((((a(1)*r+a(2))*r+a(3))*r+a(4))*r+a(5))*r+a(6))*q /
159 (((((b(1)*r+b(2))*r+b(3))*r+b(4))*r+b(5))*r+1);
162 T q = sqrt(-(T)2.0*log(1-p));
163 x = -(((((c(1)*q+c(2))*q+c(3))*q+c(4))*q+c(5))*q+c(6)) /
164 ((((d(1)*q+d(2))*q+d(3))*q+d(4))*q+1);
169extern CGV_API
double compute_unit_ball_volume(
unsigned n);
171extern CGV_API
double compute_ball_volume(
unsigned n,
double R);
173extern CGV_API
double compute_unit_sphere_area(
unsigned n);
175extern CGV_API
double compute_sphere_area(
unsigned n,
double R);
186T maximum(
const T& a,
const T&b)
188 return a > b ? a : b;
193T minimum(
const T &a,
const T&b)
195 return a < b ? a : b;
200T maximum(
const T& a,
const T&b,
const T& c)
204 return a > c ? a : c;
208 return b > c ? b : c;
214T minimum(
const T& a,
const T&b,
const T& c)
218 return a < c ? a : c;
222 return b < c ? b : c;
230 return static_cast<T
>(deg *
static_cast<T
>(cgv::math::constants::pi / 180.0));
237 return static_cast<T
>(rad *
static_cast<T
>(180.0 * cgv::math::constants::inv_pi));
241extern CGV_API
double gamma_ln(
const double xx);
244extern CGV_API
double fac(
const int n);
247extern CGV_API
double fac_ln(
const int n);
250extern CGV_API
double beta(
const double z,
const double w);
253extern CGV_API
double nchoosek(
const int n,
const int k);
258#include <cgv/config/lib_end.h>