4.6.0
Freundlich's C++ toolkit
|
Math utility classes and functions.
The three array-like classes in fcppt.math are:
Vector and dim are one-dimensional statically sized arrays, while matrix is a two-dimensional statically sized array. Although vector and dim are very similar, they do not provide the same operations. Vectors should be used for points or arrows, while dims should be used for sizes. The three classes follow a common base design which will be explained using vectors.
There are two classes that build on top of vector and dim:
Vector, dim and matrix are statically sized arrays. The declaration of vector uses the template parameters typename T
, fcppt::math::size_type N
and typename S
. Here, T
is the element type, N
the number of elements and S
the storage type. The latter requires further explanation: For a normal vector that owns its elements, S
can be std::array<T,N>
.
This is exactly what fcppt::math::vector::static_ does.
If we want a view vector, for example a vector that represents a row of a matrix, we could use S=T*
instead.
Vector and dim are initialized in the same way: An fcppt::math::vector::object<T,N,S>
can be initialized by either:
N
values of type T
, S
. vector
, dim
and matrix
each have a structure_cast
function. Structure casts preserve dimensions but convert the underlying value type. How this is done is specified by a function object.
vector
and dim
have functions push_back
, which increases the dimension by one, and narrow_cast
, which decreases the dimension.
All math classes can be compared using operator==
, for example:
operator==
uses the underlying comparison operator of the value type, which is error-prone for floats. The componentwise_equal
compare using an epsilon:
For vector
, dim
and matrix
, the following comparison operators are defined:
operator==
operator!=
operator<=
operator<
operator>=
operator>
vector
, dim
and matrix
define different arithmetic operators. See their individual documentations for more information.
It is important to note that all of the free operators support that the value types of their arguments differ. Suppose we can divide meters by seconds to get speed.
This can now be used to divide vectors of meters by vectors of seconds to obtain vectors of speeds.
vector
, dim
and box
provide null
functions that return an object consisting of zeroes.
All classes provide an output operator operator<<
, some also provide an input operator operator>>
.
Topics | |
fcppt.math.box | |
A class representing axis-aligned rectangles, boxes, hypercubes, ... | |
fcppt.math.dim | |
A class representing static n-dimensional dimensions. | |
fcppt.math.matrix | |
A class representing static n times m-dimensional matrices. | |
fcppt.math.sphere | |
A class representing circles, spheres, ... | |
fcppt.math.vector | |
A class representing static n-dimensional vectors. | |
Typedefs | |
template<typename Type , Type Dividend, Type Divisor> | |
using | fcppt::math::ceil_div_static |
Calculates a division of integral contants rounded towards infinity. | |
using | fcppt::math::difference_type = std::make_signed_t<fcppt::math::size_type> |
The difference type used for structure sizes. | |
template<fcppt::math::size_type Start, fcppt::math::size_type End> | |
using | fcppt::math::int_range |
A static int range. | |
template<fcppt::math::size_type Count> | |
using | fcppt::math::int_range_count = fcppt::math::int_range<fcppt::literal<fcppt::math::size_type>(0U), Count> |
A static int range starting at 0. | |
using | fcppt::math::size_type = unsigned |
The size type used for structure sizes. | |
template<fcppt::math::size_type N> | |
using | fcppt::math::static_size = std::integral_constant<fcppt::math::size_type, N> |
An mpl integral_c for math::size_type. | |
template<typename Type > | |
using | fcppt::math::to_array_type |
Returns the array type of an object with static storage. | |
Functions | |
template<typename T > | |
fcppt::optional::object< T > | fcppt::math::ceil_div (T const &_dividend, T const &_divisor) |
Calculates dividend / divisor rounded towards infinity. | |
template<typename T > | |
fcppt::optional::object< T > | fcppt::math::ceil_div_signed (T const &_dividend, T const &_divisor) |
Calculates dividend / divisor rounded towards infinity. | |
template<typename T > | |
fcppt::optional::object< T > | fcppt::math::clamp (T const &_value, T const &_vmin, T const &_vmax) |
Clamps a value into a range. | |
template<typename T > | |
T | fcppt::math::deg_to_rad (T const _deg) |
Transforms degrees into radians. | |
template<typename T > | |
T | fcppt::math::diff (T const &_a, T const &_b) |
Calculates the absolute distance between a and b . | |
template<typename L , typename R > | |
fcppt::optional::object< FCPPT_MATH_DETAIL_BINARY_TYPE(L,/, R)> | fcppt::math::div (L const &_dividend, R const &_divisor) |
Calculates dividend / divisor. | |
template<typename Type > | |
Type | fcppt::math::from_array (fcppt::math::to_array_type< Type > &&_value) |
Constructs an object with static storage from an array rvalue. | |
template<typename Type > | |
Type | fcppt::math::from_array (fcppt::math::to_array_type< Type > const &_value) |
Constructs an object with static storage from an array lvalue. | |
template<typename Float , typename Value > | |
Value | fcppt::math::interpolation::linear (Float const &_f, Value const &_v1, Value const &_v2) |
Interpolates between two values linearly. | |
template<typename Float , typename Value > | |
Value | fcppt::math::interpolation::perlin_fifth_degree (Float const &_f, Value const &_v1, Value const &_v2) |
Interpolates between two values using perlin fifth degree. | |
template<typename Float , typename Value > | |
Value | fcppt::math::interpolation::trigonometric (Float const &_f, Value const &_v1, Value const &_v2) |
Interpolates between two values trigonometrically. | |
template<typename Type > | |
Type | fcppt::math::interval_distance (fcppt::tuple::object< Type, Type > _i1, fcppt::tuple::object< Type, Type > _i2) |
Calculates the distance of two intervals. | |
template<typename T > requires (std::is_unsigned_v<T>) | |
constexpr bool | fcppt::math::is_power_of_2 (T const x) noexcept |
Checks if a number is a power of two. | |
template<typename T > | |
bool | fcppt::math::is_zero (T const &_value) |
Compares a value against zero. | |
template<typename T > | |
T | fcppt::math::log2 (T const x) |
Calculates for unsigned types (using a loop) | |
template<typename T > | |
fcppt::optional::object< T > | fcppt::math::mod (T const &_dividend, T const &_divisor) |
Wraps the integral modulo operator and the floating point modulo functions. | |
template<typename T > | |
T | fcppt::math::next_power_of_2 (T const _value) |
Calculates the next power of 2 for an unsigned value. | |
template<typename Result , typename Exponent > requires (std::is_unsigned_v<Exponent>) | |
constexpr Result | fcppt::math::power_of_2 (Exponent const _exponent) |
Calculates two to the power of n. | |
template<typename T > | |
T | fcppt::math::rad_to_deg (T const _rad) |
Transforms radians into degrees. | |
template<typename Type > | |
fcppt::math::to_array_type< Type > | fcppt::math::to_array (Type const &_value) |
Returns the array of an object with static storage. | |
using fcppt::math::ceil_div_static |
Calculates a division of integral contants rounded towards infinity.
Calculates Dividend / Divisor
rounded towards infinity. For example, 5 / 3
would result in 2
. The result type is a std::integral_constant<Type,V>
, where V
is the calculated value.
Type | Must be an unsigned integral type |
Dividend | The dividend |
Divisor | The divisor |
using fcppt::math::difference_type = std::make_signed_t<fcppt::math::size_type> |
The difference type used for structure sizes.
using fcppt::math::int_range |
A static int range.
using fcppt::math::int_range_count = fcppt::math::int_range<fcppt::literal<fcppt::math::size_type>(0U), Count> |
A static int range starting at 0.
using fcppt::math::size_type = unsigned |
The size type used for structure sizes.
using fcppt::math::static_size = std::integral_constant<fcppt::math::size_type, N> |
An mpl integral_c for math::size_type.
using fcppt::math::to_array_type |
Returns the array type of an object with static storage.
fcppt::optional::object< T > fcppt::math::ceil_div | ( | T const & | _dividend, |
T const & | _divisor ) |
Calculates dividend / divisor rounded towards infinity.
In case divisor is 0, nothing is returned. Otherwise, returns (dividend / divisor) if dividend can be divided by divisor, and (dividend / divisor) + 1 if dividend can not be divided by divisor.
T | An unsigned type. |
fcppt::optional::object< T > fcppt::math::ceil_div_signed | ( | T const & | _dividend, |
T const & | _divisor ) |
Calculates dividend / divisor rounded towards infinity.
The same as fcppt::math::ceil_div, except in case where dividend is negative, dividend / divisor is returned.
T | A signed type |
fcppt::optional::object< T > fcppt::math::clamp | ( | T const & | _value, |
T const & | _vmin, | ||
T const & | _vmax ) |
Clamps a value into a range.
|
inline |
Transforms degrees into radians.
T | A floating point type |
|
inline |
Calculates the absolute distance between a
and b
.
T | A numeric type |
For unsigned types, this returns:
For other types, abs(a-b)
is returned.
fcppt::optional::object< FCPPT_MATH_DETAIL_BINARY_TYPE(L,/, R)> fcppt::math::div | ( | L const & | _dividend, |
R const & | _divisor ) |
Calculates dividend / divisor.
In case divisor is 0, nothing is returned.
R | Any type that works with fcppt::math::is_zero. |
|
inline |
Constructs an object with static storage from an array rvalue.
|
inline |
Constructs an object with static storage from an array lvalue.
Type fcppt::math::interval_distance | ( | fcppt::tuple::object< Type, Type > | _i1, |
fcppt::tuple::object< Type, Type > | _i2 ) |
Calculates the distance of two intervals.
Returns the distance (as defined below) of the intervals _i1 and _i2.
Distance can be zero if the intervals touch, or negative if they overlap. If they only partially overlap, the distance is negative the common length where they overlap. If one completely contains the other, the "outer" interval is split in two parts by the "inner" one. In this case, the (again negative) length of the shorter part is returned. Therefore the distance is zero if the inner interval touches the outer one.
Type | Must support < and - |
|
constexprnoexcept |
Checks if a number is a power of two.
T | Must be an unsigned integral type |
If you need to check whether a signed integral type is a power of two, convert it to its unsigned counterpart before checking.
|
inline |
Compares a value against zero.
Uses fcppt::literal to create a zero constant and uses operator==
for comparison.
This function is special because it also allows comparison of floating-point values against zero, which would otherwise trigger a warning.
Value fcppt::math::interpolation::linear | ( | Float const & | _f, |
Value const & | _v1, | ||
Value const & | _v2 ) |
Interpolates between two values linearly.
Float | Must be a floating point type |
Value | Must support scalar multiplication with Float and addition |
|
inline |
Calculates for unsigned types (using a loop)
T | Must be an unsigned integral type |
x
is 0.
|
inline |
Wraps the integral modulo operator and the floating point modulo functions.
It uses std::fmod for floating point types. Otherwise % is used. Returns nothing if _divisor is zero.
T | A floating-point type or an unsigned type. |
T fcppt::math::next_power_of_2 | ( | T const | _value | ) |
Calculates the next power of 2 for an unsigned value.
If _value is a power of two, then _value is returned. Otherwise, the least power of two that is greater than _value is returned.
T | An unsigned type |
Value fcppt::math::interpolation::perlin_fifth_degree | ( | Float const & | _f, |
Value const & | _v1, | ||
Value const & | _v2 ) |
Interpolates between two values using perlin fifth degree.
Float | Must be a floating point type |
Value | Must support scalar multiplication with Float and addition |
|
constexpr |
Calculates two to the power of n.
Returns 2 ^ _exponent
.
Exponent | Must be an unsigned type. |
Result | Must be a type constructible with fcppt::literal. |
|
inline |
Transforms radians into degrees.
T | A floating point type |
|
inline |
Returns the array of an object with static storage.
Value fcppt::math::interpolation::trigonometric | ( | Float const & | _f, |
Value const & | _v1, | ||
Value const & | _v2 ) |
Interpolates between two values trigonometrically.
Float | Must be a floating point type |
Value | Must support scalar multiplication with Float and addition |