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

template<typename T, size_t M, size_t N>
using Matrix = std::array<std::array<T, N>, M>#

Alias for two-dimensional array with size M and N. M is the major size.

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 consist of the containers’ object after the application of the binary function with the scalar as parameter.

Template Parameters:
  • Container – a 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)#
template<typename Container>
Container operator+(const Container &lhs, const Container &rhs)#
template<typename Container>
Container operator*(const Container &lhs, const Container &rhs)#
template<typename Container>
Container operator/(const Container &lhs, const Container &rhs)#
template<typename Container, typename Scalar>
Container operator+(const Container &lhs, const Scalar &scalar)#
template<typename Container, typename Scalar>
Container operator*(const Container &lhs, const Scalar &scalar)#
template<typename Container, typename Scalar>
Container operator/(const Container &lhs, const Scalar &scalar)#
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:

an 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 – a 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 an 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 be 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)#
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.

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 true if the absolute difference between lhs and rhs is less than or equal to the relative error factored by the larger of the magnitude of lhs and 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 ...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 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::almostEqualRelative function.

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::almostEqualUlps function.

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.

template<typename T>
struct is_stdarray : public std::false_type#
template<typename T, std::size_t N> array< T, N > > : public std::true_type
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