2.10.0
Freundlich's C++ toolkit
Classes | Typedefs | Functions
fcppt.math.vector

Description

A class representing static n-dimensional vectors.

Vector overview

A vector is a one-dimensional statically sized array, useful for representing points and directions. For a general introduction on vectors, see Math Classes. For vector's interoperation with fcppt::math::matrix::object, see Operators.

Vector and dim

A dimension is very similar to a vector, see Motivation. Vectors and dims can be converted into each other, see Converting from and to vectors.

Additionally, there are arithmetic operators that take a vector on the left-hand side and a dim on the right-hand side. These are defined in fcppt/math/vector/dim.hpp.

typedef
float,
3
>
float3_vec;
float3_vec vec2(
10.f,
20.f,
30.f
);
typedef
float,
3
>
float3_dim;
float3_dim const dim(
1.4f,
1.5f,
1.6f
);
// dims can be added to or subtracted from vectors
std::cout << (vec2 - dim) << '\n';

Header files

Header file Description
object_fwd.hpp Contains fcppt::math::vector::object's declaration.
object_decl.hpp Contains fcppt::math::vector::object's definition.
object_impl.hpp Contains the definition of fcppt::math::vector::object's member functions.
object.hpp Includes object_fwd.hpp, object_decl.hpp and object_impl.hpp.
arithmetic.hpp Contains symmetric and scalar arithmetic operators.
comparison.hpp Contains all comparison operators.
input.hpp Contains operator>>.
output.hpp Contains operator<<.
dim.hpp Contains arithmetic operators to interoperate with fcppt::math::dim::object.

Classes

struct  fcppt::math::vector::has_dim< Vector, Dim >
 Checks the dimension of a vector. More...
 
struct  fcppt::math::vector::is_vector< T >
 Checks if a type is a vector type. More...
 
class  fcppt::math::vector::object< T, N, S >
 A class representing static n-dimensional vectors. More...
 

Typedefs

template<typename T , fcppt::math::size_type N>
using fcppt::math::vector::static_ = fcppt::math::vector::object< T, N, fcppt::math::detail::static_storage< T, N > >
 Typedef helper for static vectors. More...
 

Functions

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::optional::object< T > fcppt::math::vector::angle_between (fcppt::math::vector::object< T, N, S1 > const &_from, fcppt::math::vector::object< T, N, S2 > const &_to)
 Calculates the angle between two floating point vectors. More...
 
template<typename Dest , typename T , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::optional::object< Dest > fcppt::math::vector::angle_between_cast (fcppt::math::vector::object< T, N, S1 > const &_from, fcppt::math::vector::object< T, N, S2 > const &_to)
 Calculates the angle between two arbitrary vector types. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_UNARY_TYPE(T, -), N > fcppt::math::vector::operator- (fcppt::math::vector::object< T, N, S > const &_left)
 Negates a vector. More...
 
template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L,+, R), N > fcppt::math::vector::operator+ (fcppt::math::vector::object< L, N, S1 > const &_left, fcppt::math::vector::object< R, N, S2 > const &_right)
 Adds a vector to a vector. More...
 
template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, -, R), N > fcppt::math::vector::operator- (fcppt::math::vector::object< L, N, S1 > const &_left, fcppt::math::vector::object< R, N, S2 > const &_right)
 Subtracts a vector from a vector. More...
 
template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, *, R), N > fcppt::math::vector::operator* (fcppt::math::vector::object< L, N, S1 > const &_left, fcppt::math::vector::object< R, N, S2 > const &_right)
 Multiplies a vector by a vector. More...
 
template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::optional::object< fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L,/, R), N >> fcppt::math::vector::operator/ (fcppt::math::vector::object< L, N, S1 > const &_left, fcppt::math::vector::object< R, N, S2 > const &_right)
 Divides a vector by a vector. More...
 
template<typename L , typename R , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, *, R), N > fcppt::math::vector::operator* (fcppt::math::vector::object< L, N, S > const &_left, R const &_right)
 Multiplies a vector by a scalar on the right. More...
 
template<typename L , typename R , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, *, R), N > fcppt::math::vector::operator* (L const &_left, fcppt::math::vector::object< R, N, S > const &_right)
 Multiplies a vector by a scalar on the left. More...
 
template<typename L , typename R , fcppt::math::size_type N, typename S >
fcppt::optional::object< fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L,/, R), N >> fcppt::math::vector::operator/ (fcppt::math::vector::object< L, N, S > const &_left, R const &_right)
 Divides a vector by a scalar. More...
 
template<fcppt::math::size_type Index, typename Vector >
fcppt::container::to_reference_type< std::remove_reference_t< Vector >> fcppt::math::vector::at (Vector &_value)
 Access an element using a compile-time constant. More...
 
template<typename T , typename S >
fcppt::optional::object< T > fcppt::math::vector::atan2 (fcppt::math::vector::object< T, 2, S > const &_vector)
 Computes atan2 of a vector. More...
 
template<typename T1 , typename T2 , fcppt::math::size_type N, typename S1 , typename S2 , typename Function >
fcppt::math::vector::static_< std::result_of_t< Function(T1, T2) >, N > fcppt::math::vector::binary_map (fcppt::math::vector::object< T1, N, S1 > const &_left, fcppt::math::vector::object< T2, N, S2 > const &_right, Function const &_function)
 Maps over two vectors. More...
 
template<typename T , fcppt::math::size_type N>
std::array< fcppt::math::vector::static_< T, N >, fcppt::math::power_of_2< std::size_t > fcppt::math::size_type{ N })> fcppt::math::vector::bit_strings ()
 Generates vectors consisting of zeros and ones. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::optional::object< fcppt::math::vector::static_< T, N >> fcppt::math::vector::ceil_div_signed (fcppt::math::vector::object< T, N, S > const _vector, T const _divisor)
 Division rounded up for signed vectors. More...
 
template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator== (fcppt::math::vector::object< T, N, S1 > const &_v1, fcppt::math::vector::object< T, N, S2 > const &_v2)
 Compares two vectors for equality. More...
 
template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator!= (fcppt::math::vector::object< T, N, S1 > const &_v1, fcppt::math::vector::object< T, N, S2 > const &_v2)
 Compares two vectors for disequality. More...
 
template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator< (fcppt::math::vector::object< T, N, S1 > const &_v1, fcppt::math::vector::object< T, N, S2 > const &_v2)
 Compares two vectors lexicographically. More...
 
template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator> (fcppt::math::vector::object< T, N, S1 > const &_v1, fcppt::math::vector::object< T, N, S2 > const &_v2)
 Compares two vectors lexicographically. More...
 
template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator<= (fcppt::math::vector::object< T, N, S1 > const &_v1, fcppt::math::vector::object< T, N, S2 > const &_v2)
 Compares two vectors lexicographically. More...
 
template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator>= (fcppt::math::vector::object< T, N, S1 > const &_v1, fcppt::math::vector::object< T, N, S2 > const &_v2)
 Compares two vectors lexicographically. More...
 
template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::componentwise_equal (fcppt::math::vector::object< T, N, S1 > const &_v1, fcppt::math::vector::object< T, N, S2 > const &_v2, T const _epsilon)
 Compares two vectors for equality using an epsilon. More...
 
template<typename T , typename S1 , typename S2 >
fcppt::math::vector::static_< T, 3 > fcppt::math::vector::cross (fcppt::math::vector::object< T, 3, S1 > const &l, fcppt::math::vector::object< T, 3, S2 > const &r)
 Calculates the cross product of two three-dimensional vectors. More...
 
template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L,+, R), N > fcppt::math::vector::operator+ (fcppt::math::vector::object< L, N, S1 > const &_left, fcppt::math::dim::object< R, N, S2 > const &_right)
 Adds a dim to a vector. More...
 
template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, -, R), N > fcppt::math::vector::operator- (fcppt::math::vector::object< L, N, S1 > const &_left, fcppt::math::dim::object< R, N, S2 > const &_right)
 Subtracts a dim from a vector. More...
 
template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, *, R), N > fcppt::math::vector::operator* (fcppt::math::vector::object< L, N, S1 > const &_left, fcppt::math::dim::object< R, N, S2 > const &_right)
 Multiplies a vector by a dim. More...
 
template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::optional::object< fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L,/, R), N >> fcppt::math::vector::operator/ (fcppt::math::vector::object< L, N, S1 > const &_left, fcppt::math::dim::object< R, N, S2 > const &_right)
 Divides a vector by a dim. More...
 
template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
std::enable_if_t< std::is_floating_point< T >::value, T > fcppt::math::vector::distance (fcppt::math::vector::object< T, N, S1 > const &_v1, fcppt::math::vector::object< T, N, S2 > const &_v2)
 Computes the distance between two floating-point vectors. More...
 
template<typename Dest , typename T , fcppt::math::size_type N, typename S1 , typename S2 >
std::enable_if_t< !std::is_floating_point< T >::value, Dest > fcppt::math::vector::distance (fcppt::math::vector::object< T, N, S1 > const &_v1, fcppt::math::vector::object< T, N, S2 > const &_v2)
 Computes the distance between two non floating-point vectors. More...
 
template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::dot (fcppt::math::vector::object< T, N, S1 > const &_left, fcppt::math::vector::object< T, N, S2 > const &_right)
 Calculates the dot product of two vectors. More...
 
template<typename Vector >
Vector fcppt::math::vector::fill (fcppt::type_traits::value_type< Vector > const &_value)
 Constructs a static vector with all components set to a given value. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< T, N+1u > fcppt::math::vector::hypersphere_to_cartesian (fcppt::math::vector::object< T, N, S > const &_angles)
 Calculate the n dimensional polar coordinates, also called "hyperspherical coordinates". More...
 
template<typename Vector , typename Function >
Vector fcppt::math::vector::init (Function const &_function)
 Initializes a vector. More...
 
template<typename T , fcppt::math::size_type N, typename S , typename Ch , typename Traits >
std::basic_istream< Ch, Traits > & fcppt::math::vector::operator>> (std::basic_istream< Ch, Traits > &_stream, fcppt::math::vector::object< T, N, S > &_vector)
 Reads a vector from s, expecting it in the format. More...
 
template<typename T , fcppt::math::size_type N, typename S >
std::enable_if_t< std::is_floating_point< T >::value, T > fcppt::math::vector::length (fcppt::math::vector::object< T, N, S > const &_vec)
 Calculates the length of a vectorThis is the Euclidean distance (or 2 norm) of the vector. There are two variants of this function. One works only for floating point vectors (so you don't have to specify the return type). The other one works for arbitrary vectors and uses a cast internally. More...
 
template<typename Dest , typename T , fcppt::math::size_type N, typename S >
std::enable_if_t< !std::is_floating_point< T >::value, Dest > fcppt::math::vector::length (fcppt::math::vector::object< T, N, S > const &_vec)
 Calculates the length of a vectorThis is the Euclidean distance (or 2 norm) of the vector. There are two variants of this function. One works only for floating point vectors (so you don't have to specify the return type). The other one works for arbitrary vectors and uses a cast internally. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::length_square (fcppt::math::vector::object< T, N, S > const &_vector)
 Calculate the square of the vector's length. More...
 
template<typename T , fcppt::math::size_type N, typename S , typename Function >
fcppt::math::vector::static_< std::result_of_t< Function(T) >, N > fcppt::math::vector::map (fcppt::math::vector::object< T, N, S > const &_value, Function const &_function)
 Maps over the elements of a vector. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::optional::object< fcppt::math::vector::static_< T, N >> fcppt::math::vector::mod (fcppt::math::vector::object< T, N, S > const &_arg, T const _div)
 Calculate vector modulo a scalar. More...
 
template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::optional::object< fcppt::math::vector::static_< T, N >> fcppt::math::vector::mod (fcppt::math::vector::object< T, N, S1 > const &_v0, fcppt::math::vector::object< T, N, S2 > const &_v1)
 Calculate vector modulo a vector. More...
 
template<typename Dest , typename T , fcppt::math::size_type N, typename S >
Dest fcppt::math::vector::narrow_cast (fcppt::math::vector::object< T, N, S > const &_src)
 Shortens a vector to a smaller dimension. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::optional::object< fcppt::math::vector::static_< T, N >> fcppt::math::vector::normalize (fcppt::math::vector::object< T, N, S > const &_vec)
 Normalizes a vector. More...
 
template<typename Vector >
fcppt::math::vector::static_< fcppt::type_traits::value_type< Vector >, Vector::static_size::value > fcppt::math::vector::null ()
 Returns the null vector. More...
 
template<typename T , fcppt::math::size_type N, typename S , typename Ch , typename Traits >
std::basic_ostream< Ch, Traits > & fcppt::math::vector::operator<< (std::basic_ostream< Ch, Traits > &_stream, fcppt::math::vector::object< T, N, S > const &_vector)
 Outputs a vector to a stream. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< T, N > fcppt::math::vector::point_rotate (fcppt::math::vector::object< T, N, S > const &_point, fcppt::math::vector::object< T, N, S > const &_around, T const _rot)
 Rotates a two dimensional point around another point. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< T, N+1 > fcppt::math::vector::push_back (fcppt::math::vector::object< T, N, S > const &_vector, T const &_value)
 Pushes an element to the back of a vector. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::optional::object< fcppt::math::vector::static_< T, N >> fcppt::math::vector::sequence (fcppt::math::vector::object< fcppt::optional::object< T >, N, S > const &_value)
 Sequence for a vector. More...
 
template<typename T , typename S1 , typename S2 >
fcppt::optional::object< T > fcppt::math::vector::signed_angle_between (fcppt::math::vector::object< T, 2, S1 > const &_from, fcppt::math::vector::object< T, 2, S2 > const &_to)
 Calculates the signed angle between two 2D vectors. More...
 
template<typename Dest , typename T , typename S1 , typename S2 >
fcppt::optional::object< Dest > fcppt::math::vector::signed_angle_between_cast (fcppt::math::vector::object< T, 2, S1 > const &_from, fcppt::math::vector::object< T, 2, S2 > const &_to)
 Calculates the signed angle between two arbitrary 2D vector types. More...
 
template<typename Dest , typename Conv , typename T , fcppt::math::size_type N, typename S >
Dest fcppt::math::vector::structure_cast (fcppt::math::vector::object< T, N, S > const &_src)
 Converts a vector into a different vector of the same dimension. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::dim::static_< T, N > fcppt::math::vector::to_dim (fcppt::math::vector::object< T, N, S > const &_src)
 Converts a vector into a corresponding dim. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< std::make_signed_t< T >, N > fcppt::math::vector::to_signed (fcppt::math::vector::object< T, N, S > const &_src)
 Converts a vector into a corresponding signed vector. More...
 
template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< std::make_unsigned_t< T >, N > fcppt::math::vector::to_unsigned (fcppt::math::vector::object< T, N, S > const &_src)
 Converts a vector into a corresponding unsigned vector. More...
 
template<typename Vec >
Vec fcppt::math::vector::unit (typename Vec::size_type const _axis)
 Returns the n-th basis vector of the canonical basis. More...
 

Typedef Documentation

◆ static_

template<typename T , fcppt::math::size_type N>
using fcppt::math::vector::static_ = typedef fcppt::math::vector::object< T, N, fcppt::math::detail::static_storage< T, N > >

Typedef helper for static vectors.

Template Parameters
TThe vector's value_type

See the introduction to fcppt::math::vector::object for more information on static vectors (and on this class).

Function Documentation

◆ angle_between()

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::optional::object< T> fcppt::math::vector::angle_between ( fcppt::math::vector::object< T, N, S1 > const &  _from,
fcppt::math::vector::object< T, N, S2 > const &  _to 
)

Calculates the angle between two floating point vectors.

The function returns nothing if any of the two vectors have length zero.

If you want to calculate the angle between two vectors whose value type is not a floating point type, see fcppt::math::vector::angle_between_cast.

If you want to calculate the signed angle between two 2D vectors, use fcppt::math::vector::signed_angle_between or fcppt::math::vector::signed_angle_between_cast.

See also
fcppt::math::vector::angle_between_cast
fcppt::math::vector::signed_angle_between
fcppt::math::vector::signed_angle_between_cast
Warning
The behaviour is undefined if _from or _to are very close to zero.

◆ angle_between_cast()

template<typename Dest , typename T , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::optional::object< Dest> fcppt::math::vector::angle_between_cast ( fcppt::math::vector::object< T, N, S1 > const &  _from,
fcppt::math::vector::object< T, N, S2 > const &  _to 
)

Calculates the angle between two arbitrary vector types.

Template Parameters
DestThe angle type. Must be a floating point type.

This function is a generalization of fcppt::math::vector::angle_between in that it calculates the angle between two arbitrary vectors, instead of two floating point vectors.

See also
fcppt::math::vector::angle_between
Warning
The behaviour is undefined if _from or _to are very close to zero.

◆ at()

template<fcppt::math::size_type Index, typename Vector >
fcppt::container::to_reference_type< std::remove_reference_t< Vector >> fcppt::math::vector::at ( Vector &  _value)
inline

Access an element using a compile-time constant.

Template Parameters
VectorMust be an fcppt::math::vector::object.

◆ atan2()

template<typename T , typename S >
fcppt::optional::object< T> fcppt::math::vector::atan2 ( fcppt::math::vector::object< T, 2, S > const &  _vector)
inline

Computes atan2 of a vector.

Computes atan2(_vector.y(),_vector.x()). In case x or y is zero, nothing is returned.

The standard C++ functions std::atan2, defined in cmath, have the signature:

float atan2(float y,float x)
double atan2(double y,double x)
long double atan2(long double y,long double x)

These functions can be expressed more intuitively as taking a vector (of a floating-point type), since you don't have to remember to pass in (y,x) instead of (x,y). This is what happens in fcppt's atan2 function.

Template Parameters
TMust be a floating-point type.

◆ binary_map()

template<typename T1 , typename T2 , fcppt::math::size_type N, typename S1 , typename S2 , typename Function >
fcppt::math::vector::static_< std::result_of_t< Function( T1, T2 ) >, N> fcppt::math::vector::binary_map ( fcppt::math::vector::object< T1, N, S1 > const &  _left,
fcppt::math::vector::object< T2, N, S2 > const &  _right,
Function const &  _function 
)
inline

Maps over two vectors.

◆ bit_strings()

template<typename T , fcppt::math::size_type N>
std::array< fcppt::math::vector::static_< T, N >, fcppt::math::power_of_2< std::size_t > fcppt::math::size_type{ N } )> fcppt::math::vector::bit_strings ( )

Generates vectors consisting of zeros and ones.

Template Parameters
TAn integral type

Generates the bit strings of type T in dimension N. Examples:

T = int,
N = 2:
(0,0)
(1,0)
(0,1)
(1,1)
T = int,
N = 3:
(0,0,0)
(1,0,0)
(0,1,0)
(1,1,0)
(0,0,1)
(1,0,1)
(0,1,1)
(1,1,1)

Example:

typedef
int,
3
>
vector3;
// In dimension 3, we have 2^3=8 vectors, so typedef an array of this size
// here:
typedef
vector3,
8
>
binary_vectors;
binary_vectors const vs(
int,
3
>()
);
// Outputs 0,0,0
std::cout << vs[0];
// Outputs 0,0,1
std::cout << vs[1];
// ...

◆ ceil_div_signed()

template<typename T , fcppt::math::size_type N, typename S >
fcppt::optional::object< fcppt::math::vector::static_< T, N >> fcppt::math::vector::ceil_div_signed ( fcppt::math::vector::object< T, N, S > const  _vector,
T const  _divisor 
)

Division rounded up for signed vectors.

Applies fcppt::math::ceil_div_signed to each component. Returns nothing in case _divisor is zero.

◆ componentwise_equal()

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::componentwise_equal ( fcppt::math::vector::object< T, N, S1 > const &  _v1,
fcppt::math::vector::object< T, N, S2 > const &  _v2,
T const  _epsilon 
)
inline

Compares two vectors for equality using an epsilon.

◆ cross()

template<typename T , typename S1 , typename S2 >
fcppt::math::vector::static_< T, 3> fcppt::math::vector::cross ( fcppt::math::vector::object< T, 3, S1 > const &  l,
fcppt::math::vector::object< T, 3, S2 > const &  r 
)

Calculates the cross product of two three-dimensional vectors.

The cross product is defined here:

http://en.wikipedia.org/wiki/Cross_product

◆ distance() [1/2]

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
std::enable_if_t< std::is_floating_point< T >::value, T> fcppt::math::vector::distance ( fcppt::math::vector::object< T, N, S1 > const &  _v1,
fcppt::math::vector::object< T, N, S2 > const &  _v2 
)

Computes the distance between two floating-point vectors.

◆ distance() [2/2]

template<typename Dest , typename T , fcppt::math::size_type N, typename S1 , typename S2 >
std::enable_if_t< !std::is_floating_point< T >::value, Dest> fcppt::math::vector::distance ( fcppt::math::vector::object< T, N, S1 > const &  _v1,
fcppt::math::vector::object< T, N, S2 > const &  _v2 
)

Computes the distance between two non floating-point vectors.

◆ dot()

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
T fcppt::math::vector::dot ( fcppt::math::vector::object< T, N, S1 > const &  _left,
fcppt::math::vector::object< T, N, S2 > const &  _right 
)
inline

Calculates the dot product of two vectors.

The dot product is defined here:

http://en.wikipedia.org/wiki/Dot_product

◆ fill()

template<typename Vector >
Vector fcppt::math::vector::fill ( fcppt::type_traits::value_type< Vector > const &  _value)

Constructs a static vector with all components set to a given value.

Template Parameters
VectorMust be a vector
Parameters
_valueThe value to fill the vector with

◆ hypersphere_to_cartesian()

template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< T, N + 1u> fcppt::math::vector::hypersphere_to_cartesian ( fcppt::math::vector::object< T, N, S > const &  _angles)

Calculate the n dimensional polar coordinates, also called "hyperspherical coordinates".

Template Parameters
TThe vector's value_type
NThe vector's dimension type
SThe vector's storage type
Parameters
_anglesAn N-1 dimensional vector containing the angles in each dimension

The formula is taken from:

http://en.wikipedia.org/wiki/Hypersphere#Hyperspherical_coordinates

Given an (n-1)-dimensional angle vector this returns an n-dimensional unit vector in n-dimensional cartesian coordinates. If you want a scaled version of the result, do it yourself using operator*(T,vector).

Example:

// This code calculates a three-dimensional point from two angles (...of the 3D
// sphere), typically called azimuth an elevation
// The point type
typedef
vector3f;
// The angles type
typedef
vector2f;
vector3f point =
vector2f(
fcppt::math::pi<float>(),
0.0f));

Note that in two dimensions, you have polar coordinates, and so you need one angle to denote a point on the unit circle. The point then has cartesian coordinates:

(cos(angle),sin(angle))

Just as expected.

In three dimensions, you need two angles, usually called azimuth and inclination to denote a point. The resulting point has the following coordinates:

(cos(inclination),cos(azimuth)*sin(inclination),sin(inclination)*sin(azimuth))

So, if azimuth and inclination are zero, you get the point (1,0,0). In a coordinate system where x points to the right, y to the top and z denotes the "depth" (goes into the screen or out of the screen), you are on the right boundary of the sphere.

The case where inclination is zero can be considered the sphere's "zenith" point, meaning a change in azimuth has no effect on the point's coordinate. This might be counter-intuitive if you're used to a sphere having the zenith point at the top. The formula for the hyperspherical coordinates, however, results in this slight "aberration".

If the inclination is not zero, changing the azimuth towards positive infinity results in a clockwise rotation around the x axis, assuming the viewer looks down the positive x axis.

Changing the inclination towards positive infinity results in a counterclockwise rotation around the z axis, assuming the viewer looks down the positive z axis.

◆ init()

template<typename Vector , typename Function >
Vector fcppt::math::vector::init ( Function const &  _function)
inline

Initializes a vector.

Calls _function for every index of the vector.

Template Parameters
VectorMust be a vector
FunctionMust be a function of type Vector::value_type (Vector::size_type)

◆ length() [1/2]

template<typename T , fcppt::math::size_type N, typename S >
std::enable_if_t< std::is_floating_point< T >::value, T> fcppt::math::vector::length ( fcppt::math::vector::object< T, N, S > const &  _vec)
inline

Calculates the length of a vectorThis is the Euclidean distance (or 2 norm) of the vector. There are two variants of this function. One works only for floating point vectors (so you don't have to specify the return type). The other one works for arbitrary vectors and uses a cast internally.

Note
If you want the length squared, use fcppt::math::vector::length_square
See also
fcppt::math::vector::length_square

Example:

typedef
vector3f;
typedef
vector3i;
vector3f vf(1.0f,2.0f,3.0f);
vector3f vi(1,2,3);
// No need to specify the return type here, it'll be float.
float vf_length =
vf);
// It's not so clear here. So we specify "float" explicitly
float vi_length =
fcppt::math::vector::length<float>(
vi);
// Hopefully, this prints "true"
std::cout << (std::abs(vf_length - vi_length) < 0.001f);

◆ length() [2/2]

template<typename Dest , typename T , fcppt::math::size_type N, typename S >
std::enable_if_t< !std::is_floating_point< T >::value, Dest> fcppt::math::vector::length ( fcppt::math::vector::object< T, N, S > const &  _vec)
inline

Calculates the length of a vectorThis is the Euclidean distance (or 2 norm) of the vector. There are two variants of this function. One works only for floating point vectors (so you don't have to specify the return type). The other one works for arbitrary vectors and uses a cast internally.

Note
If you want the length squared, use fcppt::math::vector::length_square
See also
fcppt::math::vector::length_square

Example:

typedef
vector3f;
typedef
vector3i;
vector3f vf(1.0f,2.0f,3.0f);
vector3f vi(1,2,3);
// No need to specify the return type here, it'll be float.
float vf_length =
vf);
// It's not so clear here. So we specify "float" explicitly
float vi_length =
fcppt::math::vector::length<float>(
vi);
// Hopefully, this prints "true"
std::cout << (std::abs(vf_length - vi_length) < 0.001f);

◆ length_square()

template<typename T , fcppt::math::size_type N, typename S >
T fcppt::math::vector::length_square ( fcppt::math::vector::object< T, N, S > const &  _vector)
inline

Calculate the square of the vector's length.

Parameters
_vectorThe vector to calculate the length for.
Note
If you just want the length, use fcppt::math::vector::length
See also
fcppt::math::vector::length

◆ map()

template<typename T , fcppt::math::size_type N, typename S , typename Function >
fcppt::math::vector::static_< std::result_of_t< Function( T ) >, N> fcppt::math::vector::map ( fcppt::math::vector::object< T, N, S > const &  _value,
Function const &  _function 
)
inline

Maps over the elements of a vector.

◆ mod() [1/2]

template<typename T , fcppt::math::size_type N, typename S >
fcppt::optional::object< fcppt::math::vector::static_< T, N >> fcppt::math::vector::mod ( fcppt::math::vector::object< T, N, S > const &  _arg,
T const  _div 
)

Calculate vector modulo a scalar.

◆ mod() [2/2]

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::optional::object< fcppt::math::vector::static_< T, N >> fcppt::math::vector::mod ( fcppt::math::vector::object< T, N, S1 > const &  _v0,
fcppt::math::vector::object< T, N, S2 > const &  _v1 
)

Calculate vector modulo a vector.

◆ narrow_cast()

template<typename Dest , typename T , fcppt::math::size_type N, typename S >
Dest fcppt::math::vector::narrow_cast ( fcppt::math::vector::object< T, N, S > const &  _src)
inline

Shortens a vector to a smaller dimension.

Template Parameters
DestMust be a vector with a smaller dimension
Parameters
_srcThe vector to shorten (narrow)

The inverse operation is fcppt::math::vector::construct.

See also
fcppt::math::vector::construct

◆ normalize()

template<typename T , fcppt::math::size_type N, typename S >
fcppt::optional::object< fcppt::math::vector::static_< T, N >> fcppt::math::vector::normalize ( fcppt::math::vector::object< T, N, S > const &  _vec)
inline

Normalizes a vector.

Returns nothing in case the length of _vec is zero.

Template Parameters
TMust be a floating-point type.

◆ null()

template<typename Vector >
fcppt::math::vector::static_< fcppt::type_traits::value_type< Vector >, Vector::static_size::value> fcppt::math::vector::null ( )

Returns the null vector.

◆ operator!=()

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator!= ( fcppt::math::vector::object< T, N, S1 > const &  _v1,
fcppt::math::vector::object< T, N, S2 > const &  _v2 
)
inline

Compares two vectors for disequality.

◆ operator*() [1/4]

template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, *, R), N> fcppt::math::vector::operator* ( fcppt::math::vector::object< L, N, S1 > const &  _left,
fcppt::math::dim::object< R, N, S2 > const &  _right 
)
inline

Multiplies a vector by a dim.

◆ operator*() [2/4]

template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, *, R), N> fcppt::math::vector::operator* ( fcppt::math::vector::object< L, N, S1 > const &  _left,
fcppt::math::vector::object< R, N, S2 > const &  _right 
)
inline

Multiplies a vector by a vector.

◆ operator*() [3/4]

template<typename L , typename R , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, *, R), N> fcppt::math::vector::operator* ( fcppt::math::vector::object< L, N, S > const &  _left,
R const &  _right 
)
inline

Multiplies a vector by a scalar on the right.

◆ operator*() [4/4]

template<typename L , typename R , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, *, R), N> fcppt::math::vector::operator* ( L const &  _left,
fcppt::math::vector::object< R, N, S > const &  _right 
)
inline

Multiplies a vector by a scalar on the left.

◆ operator+() [1/2]

template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, +, R), N> fcppt::math::vector::operator+ ( fcppt::math::vector::object< L, N, S1 > const &  _left,
fcppt::math::dim::object< R, N, S2 > const &  _right 
)
inline

Adds a dim to a vector.

◆ operator+() [2/2]

template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, +, R), N> fcppt::math::vector::operator+ ( fcppt::math::vector::object< L, N, S1 > const &  _left,
fcppt::math::vector::object< R, N, S2 > const &  _right 
)
inline

Adds a vector to a vector.

◆ operator-() [1/3]

template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_UNARY_TYPE(T, -), N> fcppt::math::vector::operator- ( fcppt::math::vector::object< T, N, S > const &  _left)
inline

Negates a vector.

◆ operator-() [2/3]

template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, -, R), N> fcppt::math::vector::operator- ( fcppt::math::vector::object< L, N, S1 > const &  _left,
fcppt::math::dim::object< R, N, S2 > const &  _right 
)
inline

Subtracts a dim from a vector.

◆ operator-() [3/3]

template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, -, R), N> fcppt::math::vector::operator- ( fcppt::math::vector::object< L, N, S1 > const &  _left,
fcppt::math::vector::object< R, N, S2 > const &  _right 
)
inline

Subtracts a vector from a vector.

◆ operator/() [1/3]

template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::optional::object< fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, /, R), N >> fcppt::math::vector::operator/ ( fcppt::math::vector::object< L, N, S1 > const &  _left,
fcppt::math::dim::object< R, N, S2 > const &  _right 
)
inline

Divides a vector by a dim.

◆ operator/() [2/3]

template<typename L , typename R , fcppt::math::size_type N, typename S1 , typename S2 >
fcppt::optional::object< fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, /, R), N >> fcppt::math::vector::operator/ ( fcppt::math::vector::object< L, N, S1 > const &  _left,
fcppt::math::vector::object< R, N, S2 > const &  _right 
)
inline

Divides a vector by a vector.

◆ operator/() [3/3]

template<typename L , typename R , fcppt::math::size_type N, typename S >
fcppt::optional::object< fcppt::math::vector::static_< FCPPT_MATH_DETAIL_BINARY_TYPE(L, /, R), N >> fcppt::math::vector::operator/ ( fcppt::math::vector::object< L, N, S > const &  _left,
R const &  _right 
)
inline

Divides a vector by a scalar.

◆ operator<()

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator< ( fcppt::math::vector::object< T, N, S1 > const &  _v1,
fcppt::math::vector::object< T, N, S2 > const &  _v2 
)
inline

Compares two vectors lexicographically.

◆ operator<<()

template<typename T , fcppt::math::size_type N, typename S , typename Ch , typename Traits >
std::basic_ostream< Ch, Traits>& fcppt::math::vector::operator<< ( std::basic_ostream< Ch, Traits > &  _stream,
fcppt::math::vector::object< T, N, S > const &  _vector 
)
inline

Outputs a vector to a stream.

Uses the output format

(a_1,a_2,...)

where a_i are the vector's components.

◆ operator<=()

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator<= ( fcppt::math::vector::object< T, N, S1 > const &  _v1,
fcppt::math::vector::object< T, N, S2 > const &  _v2 
)
inline

Compares two vectors lexicographically.

◆ operator==()

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator== ( fcppt::math::vector::object< T, N, S1 > const &  _v1,
fcppt::math::vector::object< T, N, S2 > const &  _v2 
)
inline

Compares two vectors for equality.

◆ operator>()

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator> ( fcppt::math::vector::object< T, N, S1 > const &  _v1,
fcppt::math::vector::object< T, N, S2 > const &  _v2 
)
inline

Compares two vectors lexicographically.

◆ operator>=()

template<typename T , fcppt::math::size_type N, typename S1 , typename S2 >
bool fcppt::math::vector::operator>= ( fcppt::math::vector::object< T, N, S1 > const &  _v1,
fcppt::math::vector::object< T, N, S2 > const &  _v2 
)
inline

Compares two vectors lexicographically.

◆ operator>>()

template<typename T , fcppt::math::size_type N, typename S , typename Ch , typename Traits >
std::basic_istream< Ch, Traits>& fcppt::math::vector::operator>> ( std::basic_istream< Ch, Traits > &  _stream,
fcppt::math::vector::object< T, N, S > &  _vector 
)
inline

Reads a vector from s, expecting it in the format.

(a_1,a_2,...)

where a_i are the components

◆ point_rotate()

template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< T, N> fcppt::math::vector::point_rotate ( fcppt::math::vector::object< T, N, S > const &  _point,
fcppt::math::vector::object< T, N, S > const &  _around,
T const  _rot 
)

Rotates a two dimensional point around another point.

Parameters
_pointThe point to rotate
_aroundThe point to rotate around
_rotThe rotation angle

◆ push_back()

template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< T, N + 1> fcppt::math::vector::push_back ( fcppt::math::vector::object< T, N, S > const &  _vector,
T const &  _value 
)

Pushes an element to the back of a vector.

◆ sequence()

template<typename T , fcppt::math::size_type N, typename S >
fcppt::optional::object< fcppt::math::vector::static_< T, N >> fcppt::math::vector::sequence ( fcppt::math::vector::object< fcppt::optional::object< T >, N, S > const &  _value)
inline

Sequence for a vector.

Uses fcppt::optional::sequence.

◆ signed_angle_between()

template<typename T , typename S1 , typename S2 >
fcppt::optional::object< T> fcppt::math::vector::signed_angle_between ( fcppt::math::vector::object< T, 2, S1 > const &  _from,
fcppt::math::vector::object< T, 2, S2 > const &  _to 
)

Calculates the signed angle between two 2D vectors.

Uses fcppt::math::vector::atan2 on the difference between the two vectors. In case this fails, nothing is returned.

If you want to calculate the angle between two vectors whose value type is not a floating-point type, see fcppt::math::vector::signed_angle_between_cast.

If you want to calculate the (unsigned) angle between two vectors of higher dimensions, use fcppt::math::vector::angle_between or fcppt::math::vector::angle_between_cast.

See also
fcppt::math::vector::signed_angle_between_cast
fcppt::math::vector::angle_between
fcppt::math::vector::angle_between_cast
Template Parameters
TMust be a floating-point type.

◆ signed_angle_between_cast()

template<typename Dest , typename T , typename S1 , typename S2 >
fcppt::optional::object< Dest> fcppt::math::vector::signed_angle_between_cast ( fcppt::math::vector::object< T, 2, S1 > const &  _from,
fcppt::math::vector::object< T, 2, S2 > const &  _to 
)

Calculates the signed angle between two arbitrary 2D vector types.

This function is a generalization of fcppt::math::vector::signed_angle_between since it calculates the angle between two arbitrary vectors, instead of two floating-point vectors. In case of an error, nothing is returned.

See also
fcppt::math::vector::signed_angle_between
Template Parameters
DestThe angle type. Must be a floating-point type.

◆ structure_cast()

template<typename Dest , typename Conv , typename T , fcppt::math::size_type N, typename S >
Dest fcppt::math::vector::structure_cast ( fcppt::math::vector::object< T, N, S > const &  _src)
inline

Converts a vector into a different vector of the same dimension.

Template Parameters
DestThe destination vector type (not its value type!)
Convthe converter to use for each element
NThe source vector's dimension
TThe source vector's value_type
SThe source vector's storage type
Parameters
_srcThe vector to cast

See the introduction to fcppt::math::vector::object for more information on this function (and vector in general).

◆ to_dim()

template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::dim::static_< T, N> fcppt::math::vector::to_dim ( fcppt::math::vector::object< T, N, S > const &  _src)
inline

Converts a vector into a corresponding dim.

◆ to_signed()

template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< std::make_signed_t< T >, N> fcppt::math::vector::to_signed ( fcppt::math::vector::object< T, N, S > const &  _src)
inline

Converts a vector into a corresponding signed vector.

◆ to_unsigned()

template<typename T , fcppt::math::size_type N, typename S >
fcppt::math::vector::static_< std::make_unsigned_t< T >, N> fcppt::math::vector::to_unsigned ( fcppt::math::vector::object< T, N, S > const &  _src)
inline

Converts a vector into a corresponding unsigned vector.

◆ unit()

template<typename Vec >
Vec fcppt::math::vector::unit ( typename Vec::size_type const  _axis)

Returns the n-th basis vector of the canonical basis.

Template Parameters
VecThe destination vector type.
Parameters
_axisThe basis axis

Creates a vector of type Vec with all components set to 0 expect for component _axis which is set to 1:

\[ e_i = (0,\ldots,0,1,0,\ldots,0) \]