Util#
Overview#
The namespace polyhedralGravity::util contains utility
for operations on iterable Containers, Constants and syntactic
sugar for using the third party dependency thrust.
Documentation#
- 
namespace util#
- Typedefs - Functions - 
template<typename Container, typename BinOp>
 Container applyBinaryFunction(const Container &lhs, const Container &rhs, BinOp binOp)#
- Applies a binary function to elements of two containers piece by piece. The objects must be iterable and should have the same size! - Template Parameters:
- Container – an iterable object like an array or vector 
- BinOp – a binary function to apply 
 
- Parameters:
- lhs – the first container 
- rhs – the second container 
- binOp – a binary function like +, -, *, / 
 
- Returns:
- a container containing the result 
 
 - 
template<typename Container, typename Scalar, typename BinOp>
 Container applyBinaryFunction(const Container &lhs, const Scalar &scalar, BinOp binOp)#
- Applies a binary function to elements of one container piece by piece. The objects must be iterable. The resulting container consists of the containers’ object after the application of the binary function with the scalar as a parameter. - Template Parameters:
- Container – an iterable object like an array or vector 
- Scalar – a scalar to use on each element 
- BinOp – a binary function to apply 
 
- Parameters:
- lhs – the first container 
- scalar – a scalar to use on each element 
- binOp – a binary function like +, -, *, / 
 
- Returns:
- a container containing the result 
 
 - 
template<typename Container>
 Container operator-(const Container &lhs, const Container &rhs)#
- Applies the Operation Minus to two Containers piece by piece. - {1, 2, 3} - {1, 1, 1} = {0, 1, 2} - Template Parameters:
- Container – an iterable object like an array or vector 
- Parameters:
- lhs – minuend 
- rhs – subtrahend 
 
- Returns:
- the difference 
 
 - 
template<typename Container>
 Container operator+(const Container &lhs, const Container &rhs)#
- Applies the Operation Plus to two Containers piece by piece. - {1, 2, 3} + {1, 1, 1} = {2, 3, 4} - Template Parameters:
- Container – an iterable object like an array or vector 
- Parameters:
- lhs – addend 
- rhs – addend 
 
- Returns:
- the sum 
 
 - 
template<typename Container>
 Container operator*(const Container &lhs, const Container &rhs)#
- Applies the Operation * to two Containers piece by piece. - {1, 2, 3} * {2, 2, 2} = {2, 4, 6} - Template Parameters:
- Container – an iterable object like an array or vector 
- Parameters:
- lhs – multiplicand 
- rhs – multiplicand 
 
- Returns:
- the product 
 
 - 
template<typename Container>
 Container operator/(const Container &lhs, const Container &rhs)#
- Applies the Operation / to two Containers piece by piece. - {1, 2, 3} / {1, 2, 3} = {1, 1, 1} - Template Parameters:
- Container – an iterable object like an array or vector 
- Parameters:
- lhs – multiplicand 
- rhs – multiplicand 
 
- Returns:
- the product 
 
 - 
template<typename Container, typename Scalar>
 Container operator+(const Container &lhs, const Scalar &scalar)#
- Applies the Operation + to a Container and a Scalar. - {1, 2, 3} + 2 = {3, 4, 5} - Template Parameters:
- Container – an iterable object like an array or vector 
- Scalar – a scalar to use on each element 
 
- Parameters:
- lhs – addend 
- scalar – addend 
 
- Returns:
- a Container 
 
 - 
template<typename Container, typename Scalar>
 Container operator-(const Container &lhs, const Scalar &scalar)#
- Applies the Operation to a Container and a Scalar. - {1, 2, 3} - 2 = {-1, 0, 1} - Template Parameters:
- Container – an iterable object like an array or vector 
- Scalar – a scalar to use on each element 
 
- Parameters:
- lhs – minuend 
- scalar – subtrahend 
 
- Returns:
- a Container 
 
 - 
template<typename Container, typename Scalar>
 Container operator*(const Container &lhs, const Scalar &scalar)#
- Applies the Operation to a Container and a Scalar. - {1, 2, 3} * 2 = {2, 4, 6} - Template Parameters:
- Container – an iterable object like an array or vector 
- Scalar – a scalar to use on each element 
 
- Parameters:
- lhs – multiplicand 
- scalar – multiplicand 
 
- Returns:
- a Container 
 
 - 
template<typename Container, typename Scalar>
 Container operator/(const Container &lhs, const Scalar &scalar)#
- Applies the Operation / to a Container and a Scalar. - {2, 4, 6} / 2 = {1, 2, 3} - Template Parameters:
- Container – an iterable object like an array or vector 
- Scalar – a scalar to use on each element 
 
- Parameters:
- lhs – the dividend 
- scalar – the divisor 
 
- Returns:
- a Container 
 
 - 
template<typename Container>
 double euclideanNorm(const Container &container)#
- Applies the Euclidean norm/ L2-norm to a Container (e.g., a vector) - Template Parameters:
- Container – must be iterable 
- Parameters:
- container – e.g. a vector 
- Returns:
- a double containing the L2 norm 
 
 - 
template<typename Container>
 Container abs(const Container &container)#
- Computes the absolute value for each value in the given container - Template Parameters:
- Container – an iterable container, containing numerical values 
- Parameters:
- container – the container 
- Returns:
- a container with the modified values 
 
 - 
template<typename T>
 T det(const Matrix<T, 3, 3> &matrix)#
- Computes the determinant with the Sarrus rule for a 3x3 matrix. Notice that for square matrices \(det(A) = det(A^T)\). - Template Parameters:
- T – a numerical value 
- Parameters:
- matrix – the 3x3 matrix 
- Returns:
- the determinant 
 
 - 
template<typename T, size_t M, size_t N>
 Matrix<T, M, N> transpose(const Matrix<T, M, N> &matrix)#
- Computes the transposed of a mxn matrix. - Template Parameters:
- T – the type of the matrix elements 
- M – the row number 
- N – the column number 
 
- Parameters:
- matrix – the matrix to transpose 
- Returns:
- the transposed 
 
 - 
template<typename T>
 std::array<T, 3> cross(const std::array<T, 3> &lhs, const std::array<T, 3> &rhs)#
- Returns the cross-product of two cartesian vectors. - Template Parameters:
- T – a number 
- Parameters:
- lhs – left vector 
- rhs – right vector 
 
- Returns:
- cross product 
 
 - 
template<typename T>
 std::array<T, 3> normal(const std::array<T, 3> &first, const std::array<T, 3> &second)#
- Calculates the normal N as (first * second) / |first * second| with * being the cross product and first, second as cartesian vectors. - Template Parameters:
- T – numerical type 
- Parameters:
- first – first cartesian vector 
- second – second cartesian vector 
 
- Returns:
- the normal (normed) 
 
 - 
template<typename T>
 T dot(const std::array<T, 3> &lhs, const std::array<T, 3> &rhs)#
- Returns the dot product of two cartesian vectors. - Template Parameters:
- T – a number 
- Parameters:
- lhs – left vector 
- rhs – right vector 
 
- Returns:
- dot product 
 
 - 
template<typename T>
 int sgn(T val, double cutoffEpsilon)#
- Implements the signum function with a certain EPSILON to absorb rounding errors. - Template Parameters:
- T – a numerical (floating point) value 
- Parameters:
- val – the value itself 
- cutoffEpsilon – the cut-off radius around zero to return 0 
 
- Returns:
- -1, 0, 1 depending on the sign and the given EPSILON 
 
 - 
template<typename T, size_t M, size_t N>
 std::array<T, M + N> concat(const std::array<T, M> &first, const std::array<T, N> &second)#
- Concatenates two std::array of different sizes to one array. - Template Parameters:
- T – the shared type of the arrays 
- M – the size of the first container 
- N – the size of the second container 
 
- Parameters:
- first – the first array 
- second – the second array 
 
- Returns:
- a new array of size M+N with type T 
 
 - 
template<typename T>
 T surfaceArea(const Matrix<T, 3, 3> &triangle)#
- Calculates the surface area of a triangle consisting of three cartesian vertices. - Template Parameters:
- T – numerical type 
- Returns:
- surface area 
 
 - 
template<typename T>
 bool isCriticalDifference(const T &first, const T &second)#
- Calculates the magnitude between two values and return true if the magnitude between the exponents in greater than 17. - Template Parameters:
- T – numerical type 
- Parameters:
- first – first number 
- second – second number 
 
- Returns:
- true if the difference is too huge, so that floating point absorption will happen 
 
 - 
template<class ...Ts>
 overloaded(Ts...) -> overloaded<Ts...>#
- This function template provides deduction guides for the overloaded struct. It deduces the template arguments for overloaded based on its constructor arguments thus saving you from explicitly mentioning them while instantiation. https://en.cppreference.com/w/cpp/utility/variant/visit - Template Parameters:
- Ts – A template parameter pack representing all types that will be passed to the overloaded struct. 
- Parameters:
- Ts – Variable numbers of parameters to pass to the overloaded struct. 
- Returns:
- This doesn’t return a value, but rather assists in the creation of an overloaded object. The type of the object will be overloaded<Ts…>. 
 
 - 
template<typename ...Ts>
 auto operator+(const std::tuple<Ts...> &t1, const std::tuple<Ts...> &t2)#
- Adds the contents of two tuples of the same size and types with the operator +. - Template Parameters:
- Ts – types of the tuples 
- Parameters:
- t1 – first tuple 
- t2 – second tuple 
 
- Returns:
- a new tuple with the added values 
 
 - 
template<typename T, size_t N>
 std::ostream &operator<<(std::ostream &os, const std::array<T, N> &array)#
- Operator << for an array of any size. - Template Parameters:
- T – type of the array, must have an << operator overload 
- N – size of the array 
 
- Parameters:
- os – the ostream 
- array – the array itself 
 
- Returns:
- ostream 
 
 - 
template<typename T>
 std::ostream &operator<<(std::ostream &os, const std::set<T> &set)#
- Operator << for a set. - Template Parameters:
- T – type of the set, must have an << operator overload 
- Parameters:
- os – the ostream 
- set – the set itself 
 
- Returns:
- ostream 
 
 - 
template<typename FloatType>
 bool almostEqualUlps(FloatType lhs, FloatType rhs, int ulpDistance)#
- Function for comparing closeness of two floating point numbers using ULP (Units in the Last Place) method. - See also - https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ - Note - The ULP distance between 3.0 and std::nextafter(3.0, INFINITY) would be 1, the ULP distance of 3.0 and std::nextafter(std::nextafter(3.0, INFINITY), INFINITY) would be 2, etc. - Template Parameters:
- FloatType – must be either double or float (ensured by static assertion) 
- Parameters:
- lhs – The left hand side floating point number to compare. 
- rhs – The right hand side floating point number to compare. 
- ulpDistance – The maximum acceptable ULP distance between the two floating points for which they would be considered near each other. This is optional and by default, it will be - MAX_ULP_DISTANCE.
 
- Returns:
- true if the ULP distance between lhs and rhs is less than or equal to the provided ulpDistance value, otherwise, false. Returns true if both numbers are exactly the same. Returns false if the signs do not match. 
 
 - template bool almostEqualUlps< float > (float lhs, float rhs, int ulpDistance)
 - template bool almostEqualUlps< double > (double lhs, double rhs, int ulpDistance)
 - 
template<typename FloatType>
 bool almostEqualRelative(FloatType lhs, FloatType rhs, double epsilon)#
- Function to check if two floating point numbers are relatively equal to each other within a given error range or tolerance. - See also - https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ - Template Parameters:
- FloatType – must be either double or float (ensured by static assertion) 
- Parameters:
- lhs – The first floating-point number to be compared. 
- rhs – The second floating-point number to be compared. 
- epsilon – The tolerance for comparison. Two numbers that are less than epsilon apart are considered equal. The default value is - EPSILON_ALMOST_EQUAL.
 
- Returns:
- boolean value - Returns - trueif the absolute difference between- lhsand- rhsis less than or equal to the relative error factored by the larger of the magnitude of- lhsand- rhs. Otherwise,- false.
 
 - template bool almostEqualRelative< float > (float lhs, float rhs, double epsilon)
 - template bool almostEqualRelative< double > (double lhs, double rhs, double epsilon)
 - 
template<typename ...Args>
 inline bool ends_with(const std::string &str, const std::string &suffix, const Args&... suffixes)#
- Returns true if a string ends with the given suffix or any of the given suffices. - Parameters:
- str – the string whose suffix to check 
- suffix – the suffix 
- suffixes – more suffixes arguments. 
 
- Returns:
- true if the string ends with suffix, otherwise false 
 
 - 
template<typename ...Iterator>
 auto zip(const Iterator&... args)#
- Returns a zip iterator for a pack of Iterators - Template Parameters:
- Iterator – should be a iterator 
- Parameters:
- args – can be any number of iterators 
- Returns:
- a thrust::zip_iterator 
 
 - 
template<typename ...Container>
 auto zipBegin(const Container&... args)#
- Returns a zip iterator for a pack of Containers - Template Parameters:
- Container – should be a container on which one can call std::begin() 
- Parameters:
- args – can be any number of containers 
- Returns:
- a thrust::zip_iterator for the beginning of all containers 
 
 - 
template<typename ...Container>
 auto zipEnd(const Container&... args)#
- Returns a zip iterator for a pack of Containers - Template Parameters:
- Container – should be a container on which one can call std::end() 
- Parameters:
- args – can be any number of containers 
- Returns:
- a thrust::zip_iterator for the end of all containers 
 
 - 
template<typename ...Container>
 auto zipPair(const Container&... args)#
- Returns a zip iterator pair for a pack of Containers - Template Parameters:
- Container – should be a container on which one can call std::begin() and std::end() 
- Parameters:
- args – can be any number of containers 
- Returns:
- a pair of thrust::zip_iterator for the beginning and end of all containers 
 
 - Variables - 
constexpr double PI = 3.1415926535897932384626433832795028841971693993751058209749445923#
- PI with enough precision 
 - 
constexpr double PI2 = 6.2831853071795864769252867665590057683943387987502116419498891846#
- PI multiplied by 2 with enough precision 
 - 
constexpr double PI_2 = 1.5707963267948966192313216916397514420985846996875529104874722961#
- PI divided by 2 with enough precision 
 - 
constexpr double GRAVITATIONAL_CONSTANT = 6.67430e-11#
- The gravitational constant G in \([m^3/(kg*s^2)]\). In Tsoulis et al. (2012) above Equation (4) 
 - 
constexpr double DEFAULT_CONSTANT_DENSITY = 2670.0#
- The assumed constant density rho for a polyhedron after Tsoulis paper in \([kg/m^3]\). In Tsoulis et al. (2012) above Equation (4) 
 - 
constexpr double EPSILON_ALMOST_EQUAL = 1e-10#
- This relative EPSILON is utilized ONLY for testing purposes to compare intermediate values to Tsoulis’ reference implementation Fortran. It is used in the - polyhedralGravity::util::almostEqualRelativefunction.- Note - While in theory no difference at all is observed when compiling this program on Linux using GCC on x86_64, the intermediate values change when the program is compiled in different environments. Hence, this solves the flakiness of the tests when on different plattforms 
 - 
constexpr int MAX_ULP_DISTANCE = 4#
- The maximal allowed ULP distance utilized for FloatingPoint comparisons using the - polyhedralGravity::util::almostEqualUlpsfunction.
 - 
template<typename T>
 struct is_stdarray : public std::false_type#
 - 
template<class ...Ts>
 struct overloaded : public polyhedralGravity::util::Ts
- #include <UtilityContainer.h>A utility struct that creates an overload set out of all the function objects it inherits from. It allows a lambda function to be used with std::visit in a variant. The lambda function needs to be able to handle all types contained in the variant, and this struct allows it to do so by inheriting the function-call operator from each type. https://en.cppreference.com/w/cpp/utility/variant/visit - Template Parameters:
- Ts – A template parameter pack representing all types for which the struct should be able to handle. 
 
 - 
namespace detail#
- Functions - 
template<typename ...Ts, size_t... Is>
 auto tuple_add(const std::tuple<Ts...> &t1, const std::tuple<Ts...> &t2, std::index_sequence<Is...>)#
- Helper method for the tuple operator+, which expands the tuple into a parameter pack. - Template Parameters:
- Ts – types of the tuple 
- Is – indices of the tuple 
 
- Parameters:
- t1 – first tuple 
- t2 – second tuple 
 
- Returns:
- a new tuple with the added values 
 
 
- 
template<typename ...Ts, size_t... Is>
 
- 
template<typename Container, typename BinOp>