Visual Computing Library
All Classes Functions Variables Typedefs Enumerations Friends Modules Pages Concepts
base.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_MATH_BASE_H
24#define VCL_MATH_BASE_H
25
26#include <vclib/types.h>
27
28#include <cassert>
29#include <cmath>
30#include <limits>
31
32namespace vcl {
33
44template<typename Scalar>
45bool isDegenerate(Scalar number)
46{
47 return std::isinf(number) || std::isnan(number);
48}
49
65template<typename Scalar>
67 Scalar n1,
68 Scalar n2,
69 Scalar epsilon = std::numeric_limits<Scalar>::epsilon())
70{
71 return (std::abs(n1 - n2) <= epsilon);
72}
73
82template<typename Scalar>
83Scalar toRad(const Scalar& deg)
84{
85 return M_PI * deg / 180.0;
86}
87
96template<typename Scalar>
97Scalar toDeg(const Scalar& rad)
98{
99 return rad * 180.0 / M_PI;
100}
101
114inline double lnOfFactorial(int n)
115{
116 static const int FAK_LEN = 1024;
117
118 // define constants
119 static const double // coefficients in Stirling approximation
120 C0 = 0.918938533204672722, // ln(sqrt(2*pi))
121 C1 = 1. / 12., C3 = -1. / 360.;
122 // C5 = 1./1260., // use r^5 term if FAK_LEN < 50
123 // C7 = -1./1680.; // use r^7 term if FAK_LEN < 20
124 // static variables
125 static double fac_table[FAK_LEN]; // table of ln(n!):
126 // remember if fac_table has been initialized
127 static bool initialized = false;
128
129 if (n < FAK_LEN) {
130 if (n <= 1) {
131 if (n < 0)
132 assert(0); //("Parameter negative in LnFac function");
133 return 0;
134 }
135 if (!initialized) { // first time. Must initialize table
136 // make table of ln(n!)
137 double sum = fac_table[0] = 0.;
138 for (uint i = 1; i < FAK_LEN; i++) {
139 sum += std::log(double(i));
140 fac_table[i] = sum;
141 }
142 initialized = true;
143 }
144 return fac_table[n];
145 }
146 else {
147 // not found in table. use Stirling approximation
148 double n1, r;
149 n1 = n;
150 r = 1. / n1;
151 return (n1 + 0.5) * std::log(n1) - n1 + C0 + r * (C1 + r * r * C3);
152 }
153}
154
155} // namespace vcl
156
157#endif // VCL_MATH_BASE_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
bool epsilonEquals(Scalar n1, Scalar n2, Scalar epsilon=std::numeric_limits< Scalar >::epsilon())
Checks if two floating point numbers are equal within an epsilon value.
Definition base.h:66
Scalar toRad(const Scalar &deg)
Converts an angle in degrees to radians.
Definition base.h:83
bool isDegenerate(Scalar number)
Checks if a floating point number is degenerate.
Definition base.h:45
double lnOfFactorial(int n)
Computes and caches the result of the natural logarithm of n!
Definition base.h:114
Scalar toDeg(const Scalar &rad)
Converts an angle in radians to degrees.
Definition base.h:97