Visual Computing Library
Loading...
Searching...
No Matches
Math

This module contains all the mathematical functions and utilities used in the library. More...

Classes

class  vcl::Distribution< Scalar >
 The Distribution class allows to collect a set of values and then compute some statistics like average, variance, standardDeviation, and percentiles. More...
 
class  vcl::Histogram< ScalarType >
 The Histogram class allows to collect a set of values and then compute some statistics like average, variance, standardDeviation, and percentiles. More...
 

Functions

template<typename Scalar >
bool vcl::isDegenerate (Scalar number)
 Checks if a floating point number is degenerate.
 
template<typename Scalar >
bool vcl::epsilonEquals (Scalar n1, Scalar n2, Scalar epsilon=std::numeric_limits< Scalar >::epsilon())
 Checks if two floating point numbers are equal within an epsilon value.
 
template<typename Scalar >
Scalar vcl::toRad (const Scalar &deg)
 Converts an angle in degrees to radians.
 
template<typename Scalar >
Scalar vcl::toDeg (const Scalar &rad)
 Converts an angle in radians to degrees.
 
double vcl::lnOfFactorial (int n)
 Computes and caches the result of the natural logarithm of n!
 
template<Point3Concept PointType>
std::vector< PointType > vcl::sphericalFibonacciPointSet (uint n)
 Returns a vector of n points distributed in a unit sphere.
 
template<typename T >
constexpr auto vcl::min (const T &p1, const T &p2)
 Returns the minimum between the two parameters.
 
template<typename Head , typename... Tail>
requires (sizeof...(tail) > 0)
constexpr auto vcl::min (const Head &head0, const Head &head1, const Tail &... tail)
 Returns the minimum between several parameters.
 
template<typename T >
constexpr auto vcl::max (const T &p1, const T &p2)
 Returns the maximum between the two parameters.
 
template<typename Head , typename... Tail>
requires (sizeof...(tail) > 0)
constexpr auto vcl::max (const Head &head0, const Head &head1, const Tail &... tail)
 Returns the maximum between several parameters.
 
template<PointConcept PointType>
constexpr auto vcl::min (const PointType &p1, const PointType &p2)
 Returns the minimum between two points.
 
template<PointConcept PointType>
constexpr auto vcl::max (const PointType &p1, const PointType &p2)
 Returns the maximum between two points.
 
int vcl::poissonRatioOfUniformsInteger (double L, std::mt19937 &gen)
 This subfunction generates a integer with the poisson distribution using the ratio-of-uniforms rejection method (PRUAt). This approach is STABLE even for large L (e.g. it does not suffer from the overflow limit of the classical Knuth implementation) Execution time does not depend on L, except that it matters whether is within the range where ln(n!) is tabulated.
 
int vcl::poissonRandomNumber (double lambda, std::mt19937 &gen)
 algorithm poisson random number (Knuth): init: Let L ← e^−λ, k ← 0 and p ← 1. do: k ← k + 1. Generate uniform random number u in [0,1] and let p ← p × u. while p > L. return k − 1.
 
template<Point3Concept PointType>
PointType vcl::randomTriangleBarycentricCoordinate (std::mt19937 &gen)
 Generate the barycentric coords of a random point over a triangle, with a uniform distribution over the triangle. It uses the parallelogram folding trick.
 
template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType >
void vcl::setTransformMatrixRotation (MatrixType &matrix, PointType axis, const ScalarType &angleRad)
 Given an 3D axis and an angle expressed in radiants, fills the given matrix with a transform matrix that represents the rotation matrix of the given axis/angle.
 
template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType >
void vcl::setTransformMatrixRotationDeg (MatrixType &matrix, PointType axis, const ScalarType &angleDeg)
 Given an 3D axis and an angle expressed in degrees, fills the given matrix with a transform matrix that represents the rotation matrix of the given axis/angle.
 
template<MatrixConcept MatrixType, Point3Concept PointType>
void vcl::setTransformMatrixRotation (MatrixType &matrix, const PointType &fromVector, const PointType &toVector)
 Given two 3D vectors, fills the given matrix with a transform matrix that represents the rotation matrix from the first vector to the second vector.
 
template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType >
MatrixType vcl::rotationMatrix (const PointType &axis, const ScalarType &angleRad)
 Given an 3D axis and an angle expressed in radiants, returns a transform matrix that represents the rotation matrix of the given axis/angle.
 
template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType >
MatrixType vcl::rotationMatrixDeg (const PointType &axis, const ScalarType &angleDeg)
 Given an 3D axis and an angle expressed in degrees, fills the given matrix with a transform matrix that represents the rotation matrix of the given axis/angle.
 
template<MatrixConcept MatrixType, Point3Concept PointType>
MatrixType vcl::rotationMatrix (const PointType &fromVector, const PointType &toVector)
 Given two 3D vectors, returns a transform matrix that represents the rotation matrix from the first vector to the second vector.
 

Detailed Description

This module contains all the mathematical functions and utilities used in the library.

This module depends on the Concepts module.

You can access all the concepts of VCLib by including #include <vclib/math.h>

Function Documentation

◆ epsilonEquals()

template<typename Scalar >
bool vcl::epsilonEquals ( Scalar  n1,
Scalar  n2,
Scalar  epsilon = std::numeric_limits<Scalar>::epsilon() 
)

Checks if two floating point numbers are equal within an epsilon value.

This function returns true if the absolute difference between the two numbers is less than or equal to the epsilon value.

Parameters
[in]n1the first number to compare
[in]n2the second number to compare
[in]epsilonthe epsilon value to use for the check
Returns
true if the numbers are equal within the epsilon value, false otherwise.

◆ isDegenerate()

template<typename Scalar >
bool vcl::isDegenerate ( Scalar  number)

Checks if a floating point number is degenerate.

A floating point number is degenerate if it is infinite or NaN.

Parameters
[in]numberthe number to check
Returns
true if the number is degenerate, false otherwise.

◆ lnOfFactorial()

double vcl::lnOfFactorial ( int  n)
inline

Computes and caches the result of the natural logarithm of n!

If n > 1024, uses the Stirling approximation.

Parameters
[in]nthe number for which to compute the natural logarithm of the factorial.
Returns
the natural logarithm of n!.

◆ max() [1/3]

template<typename Head , typename... Tail>
requires (sizeof...(tail) > 0)
constexpr auto vcl::max ( const Head head0,
const Head head1,
const Tail &...  tail 
)
constexpr

Returns the maximum between several parameters.

Given a list of parameters, this function returns the maximum between all of them.

Example:

auto m = vcl::max(1, 2, 3, 4, 5); // m = 5
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
constexpr auto max(const T &p1, const T &p2)
Returns the maximum between the two parameters.
Definition min_max.h:83
Parameters
[in]head0The first parameter.
[in]head1The second parameter.
[in]tailThe rest of the parameters.
Returns
The maximum between the parameters.

◆ max() [2/3]

template<PointConcept PointType>
constexpr auto vcl::max ( const PointType &  p1,
const PointType &  p2 
)
constexpr

Returns the maximum between two points.

This function returns a point that contains the maximum value between each component of the two input points.

Parameters
[in]p1The first point.
[in]p2The second point.
Returns
The point containing the maximum value between the two input points.

◆ max() [3/3]

template<typename T >
constexpr auto vcl::max ( const T &  p1,
const T &  p2 
)
constexpr

Returns the maximum between the two parameters.

Parameters
[in]p1The first parameter.
[in]p2The second parameter.
Returns
The maximum between the two parameters.

◆ min() [1/3]

template<typename Head , typename... Tail>
requires (sizeof...(tail) > 0)
constexpr auto vcl::min ( const Head head0,
const Head head1,
const Tail &...  tail 
)
constexpr

Returns the minimum between several parameters.

Given a list of parameters, this function returns the minimum between all of them.

Example:

auto m = vcl::min(1, 2, 3, 4, 5); // m = 1
constexpr auto min(const T &p1, const T &p2)
Returns the minimum between the two parameters.
Definition min_max.h:42
Parameters
[in]head0The first parameter.
[in]head1The second parameter.
[in]tailThe rest of the parameters.
Returns
The minimum between the parameters.

◆ min() [2/3]

template<PointConcept PointType>
constexpr auto vcl::min ( const PointType &  p1,
const PointType &  p2 
)
constexpr

Returns the minimum between two points.

This function returns a point that contains the minimum value between each component of the two input points.

Parameters
[in]p1The first point.
[in]p2The second point.
Returns
The point containing the minimum value between the two input points.

◆ min() [3/3]

template<typename T >
constexpr auto vcl::min ( const T &  p1,
const T &  p2 
)
constexpr

Returns the minimum between the two parameters.

Parameters
[in]p1The first parameter.
[in]p2The second parameter.
Returns
The minimum between the two parameters.

◆ poissonRandomNumber()

int vcl::poissonRandomNumber ( double  lambda,
std::mt19937 &  gen 
)
inline

algorithm poisson random number (Knuth): init: Let L ← e^−λ, k ← 0 and p ← 1. do: k ← k + 1. Generate uniform random number u in [0,1] and let p ← p × u. while p > L. return k − 1.

Parameters
lambda
gen
Returns

◆ poissonRatioOfUniformsInteger()

int vcl::poissonRatioOfUniformsInteger ( double  L,
std::mt19937 &  gen 
)
inline

This subfunction generates a integer with the poisson distribution using the ratio-of-uniforms rejection method (PRUAt). This approach is STABLE even for large L (e.g. it does not suffer from the overflow limit of the classical Knuth implementation) Execution time does not depend on L, except that it matters whether is within the range where ln(n!) is tabulated.

Reference:

E. Stadlober "The ratio of uniforms approach for generating discrete random variates". Journal of Computational and Applied Mathematics, vol. 31, no. 1, 1990, pp. 181-189.

Partially adapted/inspired from some subfunctions of the Agner Fog stocc library ( www.agner.org/random ) Same licensing scheme.

Parameters
L
gen
Returns

◆ randomTriangleBarycentricCoordinate()

template<Point3Concept PointType>
PointType vcl::randomTriangleBarycentricCoordinate ( std::mt19937 &  gen)

Generate the barycentric coords of a random point over a triangle, with a uniform distribution over the triangle. It uses the parallelogram folding trick.

Parameters
gen
Returns

◆ rotationMatrix() [1/2]

template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType >
MatrixType vcl::rotationMatrix ( const PointType &  axis,
const ScalarType &  angleRad 
)

Given an 3D axis and an angle expressed in radiants, returns a transform matrix that represents the rotation matrix of the given axis/angle.

The MatrixType must be at least a 3x3 matrix having the setIdentity() member function. If the matrix is a higher than 3x3 (e.g. 4x4), only the 3x3 submatrix will be set, leaving the identity values in the other cells of the matrix.

Parameters
axis
angleRad
Returns

◆ rotationMatrix() [2/2]

template<MatrixConcept MatrixType, Point3Concept PointType>
MatrixType vcl::rotationMatrix ( const PointType &  fromVector,
const PointType &  toVector 
)

Given two 3D vectors, returns a transform matrix that represents the rotation matrix from the first vector to the second vector.

The MatrixType must be at least a 3x3 matrix having the setIdentity() member function. If the matrix is a higher than 3x3 (e.g. 4x4), only the 3x3 submatrix will be set, leaving the identity values in the other cells of the matrix.

Parameters
fromVector
toVector
Returns

◆ rotationMatrixDeg()

template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType >
MatrixType vcl::rotationMatrixDeg ( const PointType &  axis,
const ScalarType &  angleDeg 
)

Given an 3D axis and an angle expressed in degrees, fills the given matrix with a transform matrix that represents the rotation matrix of the given axis/angle.

The MatrixType must be at least a 3x3 matrix having the setIdentity() member function. If the matrix is a higher than 3x3 (e.g. 4x4), only the 3x3 submatrix will be set, leaving the identity values in the other cells of the matrix.

Parameters
axis
angleDeg
Returns

◆ setTransformMatrixRotation() [1/2]

template<MatrixConcept MatrixType, Point3Concept PointType>
void vcl::setTransformMatrixRotation ( MatrixType &  matrix,
const PointType &  fromVector,
const PointType &  toVector 
)

Given two 3D vectors, fills the given matrix with a transform matrix that represents the rotation matrix from the first vector to the second vector.

The given matrix must be at least a 3x3 matrix. If the matrix is a higher than 3x3 (e.g. 4x4), only the 3x3 submatrix will be set, leaving unchanged the other values.

Parameters
matrix
fromVector
toVector

◆ setTransformMatrixRotation() [2/2]

template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType >
void vcl::setTransformMatrixRotation ( MatrixType &  matrix,
PointType  axis,
const ScalarType &  angleRad 
)

Given an 3D axis and an angle expressed in radiants, fills the given matrix with a transform matrix that represents the rotation matrix of the given axis/angle.

The given matrix must be at least a 3x3 matrix. If the matrix is a higher than 3x3 (e.g. 4x4), only the 3x3 submatrix will be set, leaving unchanged the other values.

Parameters
matrix
axis
angleRad

◆ setTransformMatrixRotationDeg()

template<MatrixConcept MatrixType, Point3Concept PointType, typename ScalarType >
void vcl::setTransformMatrixRotationDeg ( MatrixType &  matrix,
PointType  axis,
const ScalarType &  angleDeg 
)

Given an 3D axis and an angle expressed in degrees, fills the given matrix with a transform matrix that represents the rotation matrix of the given axis/angle.

The given matrix must be at least a 3x3 matrix. If the matrix is a higher than 3x3 (e.g. 4x4), only the 3x3 submatrix will be set, leaving unchanged the other values.

Parameters
matrix
axis
angleDeg

◆ sphericalFibonacciPointSet()

template<Point3Concept PointType>
std::vector< PointType > vcl::sphericalFibonacciPointSet ( uint  n)

Returns a vector of n points distributed in a unit sphere.

This function returns a vector of n points that are uniformly distributed on a unit sphere, using the Spherical Fibonacci Point Sets algorithm described in the paper "Spherical Fibonacci Mapping" by Benjamin Keinert, Matthias Innmann, Michael Sanger, and Marc Stamminger (TOG 2015).

Template Parameters
PointTypeThe type of the point to generate. This type must satisfy the Point3Concept concept.
Parameters
[in]nThe number of points to generate.
Returns
A vector of n points distributed in a unit sphere.

◆ toDeg()

template<typename Scalar >
Scalar vcl::toDeg ( const Scalar &  rad)

Converts an angle in radians to degrees.

Parameters
[in]radthe angle in radians
Returns
the angle in degrees.

◆ toRad()

template<typename Scalar >
Scalar vcl::toRad ( const Scalar &  deg)

Converts an angle in degrees to radians.

Parameters
[in]degthe angle in degrees
Returns
the angle in radians.ù