imunano33  0.1.1
simplevectors.hpp File Reference
#include <algorithm>
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <string>
#include <type_traits>
#include <vector>
Include dependency graph for simplevectors.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  svector::Vector< D, T >
 A base vector representation. More...
 
class  svector::Vector2D
 A simple 2D vector representation. More...
 
class  svector::Vector3D
 A simple 3D vector representation. More...
 

Typedefs

typedef Vector< 2 > svector::Vec2_
 An alias to Vector<2>.
 
typedef Vector< 3 > svector::Vec3_
 An alias to Vector<3>.
 

Enumerations

enum  svector::AngleDir { svector::ALPHA , svector::BETA , svector::GAMMA }
 Angle enumerator. More...
 

Functions

template<std::size_t D, typename T >
Vector< D, T > svector::makeVector (std::array< T, D > array)
 Creates a vector from an std::array. More...
 
template<std::size_t D, typename T >
Vector< D, T > svector::makeVector (std::vector< T > vector)
 Creates a vector from a std::vetor. More...
 
template<std::size_t D, typename T >
Vector< D, T > svector::makeVector (const std::initializer_list< T > args)
 Creates a vector from an initializer list. More...
 
double svector::x (const Vector2D &v)
 Gets the x-component of a 2D vector. More...
 
void svector::x (Vector2D &v, const double xValue)
 Sets the x-component of a 2D vector. More...
 
double svector::x (const Vector3D &v)
 Gets the x-component of a 3D vector. More...
 
void svector::x (Vector3D &v, const double xValue)
 Sets the x-component of a 3D vector. More...
 
double svector::y (const Vector2D &v)
 Gets the y-component of a 2D vector. More...
 
void svector::y (Vector2D &v, const double yValue)
 Sets the y-component of a 2D vector. More...
 
double svector::y (const Vector3D &v)
 Gets the y-component of a 3D vector. More...
 
void svector::y (Vector3D &v, const double yValue)
 Sets the y-component of a 3D vector. More...
 
double svector::z (const Vector3D &v)
 Gets the z-component of a 3D vector. More...
 
void svector::z (Vector3D &v, const double zValue)
 Sets the z-component of a 3D vector. More...
 
template<typename T , std::size_t D>
svector::dot (const Vector< D, T > &lhs, const Vector< D, T > &rhs)
 Calculates the dot product of two vectors. More...
 
template<typename T , std::size_t D>
svector::magn (const Vector< D, T > &v)
 Gets the magnitude of the vector. More...
 
template<typename T , std::size_t D>
Vector< D, T > svector::normalize (const Vector< D, T > &v)
 Normalizes a vector. More...
 
template<typename T , std::size_t D>
bool svector::isZero (const Vector< D, T > &v)
 Determines whether a vector is a zero vector. More...
 
double svector::angle (const Vector2D &v)
 Gets the angle of a 2D vector in radians. More...
 
Vector2D svector::rotate (const Vector2D &v, const double ang)
 Rotates a 2D vector by a certain angle. More...
 
Vector3D svector::cross (const Vector3D &lhs, const Vector3D &rhs)
 Cross product of two vectors. More...
 
double svector::alpha (const Vector3D &v)
 Gets α angle. More...
 
double svector::beta (const Vector3D &v)
 Gets β angle. More...
 
double svector::gamma (const Vector3D &v)
 Gets γ angle. More...
 
Vector3D svector::rotateAlpha (const Vector3D &v, const double &ang)
 Rotates around x-axis. More...
 
Vector3D svector::rotateBeta (const Vector3D &v, const double &ang)
 Rotates around y-axis. More...
 
Vector3D svector::rotateGamma (const Vector3D &v, const double &ang)
 Rotates around z-axis. More...
 
template<typename T , std::size_t D>
Vector< D, T > svector::operator+ (const Vector< D, T > &lhs, const Vector< D, T > &rhs)
 Vector addition. More...
 
template<typename T , std::size_t D>
Vector< D, T > svector::operator- (const Vector< D, T > &lhs, const Vector< D, T > &rhs)
 Vector subtraction. More...
 
template<typename T , typename T2 , std::size_t D>
Vector< D, T > svector::operator* (const Vector< D, T > &lhs, const T2 rhs)
 Scalar multiplication. More...
 
template<typename T , typename T2 , std::size_t D>
Vector< D, T > svector::operator/ (const Vector< D, T > &lhs, const T2 rhs)
 Scalar division. More...
 
template<typename T , std::size_t D>
bool svector::operator== (const Vector< D, T > &lhs, const Vector< D, T > &rhs)
 Compares equality of two vectors. More...
 
template<typename T , std::size_t D>
bool svector::operator!= (const Vector< D, T > &lhs, const Vector< D, T > &rhs)
 Compares inequality of two vectors. More...
 

Enumeration Type Documentation

◆ AngleDir

Angle enumerator.

An enum representing the angle to use for a 3D vector.

This is only used in svector::Vector3D::angle() and svector::Vector3D::rotate().

Enumerator
ALPHA 

Angle between positive x-axis and vector.

BETA 

Angle between positive y-axis and vector.

GAMMA 

Angle between positive z-axis and vector.

Function Documentation

◆ alpha()

double svector::alpha ( const Vector3D v)
inline

Gets α angle.

α is the angle between the vector and the x-axis.

Note
This method will result in undefined behavior if the vector is a zero vector (if the magnitude equals zero).
Parameters
vA 3D vector.
Returns
α

◆ angle()

double svector::angle ( const Vector2D v)
inline

Gets the angle of a 2D vector in radians.

The angle will be in the range (-π, π].

Parameters
vA 2D vector.
Returns
angle of the vector.

◆ beta()

double svector::beta ( const Vector3D v)
inline

Gets β angle.

β is the angle between the vector and the y-axis.

Note
This method will result in undefined behavior if the vector is a zero vector (if the magnitude equals zero).
Parameters
vA 3D vector.
Returns
β

◆ cross()

Vector3D svector::cross ( const Vector3D lhs,
const Vector3D rhs 
)
inline

Cross product of two vectors.

Parameters
lhsThe first vector.
rhsThe second vector, crossed with the first vector.
Returns
The cross product of the two vectors.

◆ dot()

template<typename T , std::size_t D>
T svector::dot ( const Vector< D, T > &  lhs,
const Vector< D, T > &  rhs 
)
inline

Calculates the dot product of two vectors.

Note
The dimensions of the two vectors must be the same.
Template Parameters
DThe number of dimensions.
TVector type.
Parameters
lhsFirst vector.
rhsSecond vector.
Returns
The dot product of lhs and rhs.

◆ gamma()

double svector::gamma ( const Vector3D v)
inline

Gets γ angle.

γ is the angle between the vector and the z-axis.

Note
This method will result in undefined behavior if the vector is a zero vector (if the magnitude equals zero).
Parameters
vA 3D vector.
Returns
γ

◆ isZero()

template<typename T , std::size_t D>
bool svector::isZero ( const Vector< D, T > &  v)
inline

Determines whether a vector is a zero vector.

Template Parameters
DThe number of dimensions.
TVector type.
Returns
Whether the given vector is a zero vector.

◆ magn()

template<typename T , std::size_t D>
T svector::magn ( const Vector< D, T > &  v)
inline

Gets the magnitude of the vector.

Template Parameters
DThe number of dimensions.
TVector type.
Parameters
vThe vector to get magnitude of.
Returns
magnitude of vector.

◆ makeVector() [1/3]

template<std::size_t D, typename T >
Vector<D, T> svector::makeVector ( const std::initializer_list< T >  args)

Creates a vector from an initializer list.

The initializer list should represent the components of the vector in each dimension. If the size of the initializer list is greater than the number of dimensions given, then the vector only initializes with the first D elements in the list, where D is the number of dimensions. If the size of the initializer list is less than the number of dimensions given, then the vector fills the rest of the dimensions with the value 0.

Template Parameters
DThe number of dimensions.
TVector type.
Parameters
argsthe initializer list.
Returns
A vector whose dimensions reflect the elements in the initializer list.

◆ makeVector() [2/3]

template<std::size_t D, typename T >
Vector<D, T> svector::makeVector ( std::array< T, D >  array)

Creates a vector from an std::array.

Template Parameters
DThe number of dimensions.
TVector type.
Parameters
arrayAn array.
Returns
A vector whose dimensions reflect the elements in the array.

◆ makeVector() [3/3]

template<std::size_t D, typename T >
Vector<D, T> svector::makeVector ( std::vector< T >  vector)

Creates a vector from a std::vetor.

If the given std::vector has fewer elements than the specified dimensions, then this function will fill up the first elements of the vector with those in the given std::vector. The rest of the elements would be 0.

If the given std::vector has more elements than the specified dimensions, then the resulting vector would ignore the numbers in those dimensions.

Template Parameters
DThe number of dimensions.
TVector type.
Parameters
vectorA std::vector.
Returns
A vector whose dimensions reflect the elements in the std::vector.

◆ normalize()

template<typename T , std::size_t D>
Vector<D, T> svector::normalize ( const Vector< D, T > &  v)
inline

Normalizes a vector.

Finds the unit vector with the same direction angle as the current vector.

Note
This method will result in undefined behavior if the vector is a zero vector (if the magnitude equals zero).
Template Parameters
DThe number of dimensions.
TVector type.
Parameters
vThe vector to normalize.
Returns
Normalized vector.

◆ operator!=()

template<typename T , std::size_t D>
bool svector::operator!= ( const Vector< D, T > &  lhs,
const Vector< D, T > &  rhs 
)
inline

Compares inequality of two vectors.

Note
This method is only used if SVECTOR_USE_CLASS_OPERATORS is not defined. Otherwise, the operators in svector::Vector are used.
The dimensions of the two vectors must be the same.
Template Parameters
DThe number of dimensions.
TVector type.
Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A boolean representing whether the two vectors do not compare equal.

◆ operator*()

template<typename T , typename T2 , std::size_t D>
Vector<D, T> svector::operator* ( const Vector< D, T > &  lhs,
const T2  rhs 
)
inline

Scalar multiplication.

Performs scalar multiplication and returns a new vector representing the product.

Note
This method is only used if SVECTOR_USE_CLASS_OPERATORS is not defined. Otherwise, the operators in svector::Vector are used.
Template Parameters
DThe number of dimensions.
TVector type.
T2Scalar multiplication type.
Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the scalar product.

◆ operator+()

template<typename T , std::size_t D>
Vector<D, T> svector::operator+ ( const Vector< D, T > &  lhs,
const Vector< D, T > &  rhs 
)
inline

Vector addition.

Performs vector addition and returns a new vector representing the sum of the two vectors.

Note
This method is only used if SVECTOR_USE_CLASS_OPERATORS is not defined. Otherwise, the operators in svector::Vector are used.
The dimensions of the two vectors must be the same.
Template Parameters
DThe number of dimensions.
TVector type.
Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the vector sum.

◆ operator-()

template<typename T , std::size_t D>
Vector<D, T> svector::operator- ( const Vector< D, T > &  lhs,
const Vector< D, T > &  rhs 
)
inline

Vector subtraction.

Performs vector subtraction and returns a new vector representing the difference of the two vectors.

Note
This method is only used if SVECTOR_USE_CLASS_OPERATORS is not defined. Otherwise, the operators in svector::Vector are used.
The dimensions of the two vectors must be the same.
Template Parameters
DThe number of dimensions.
TVector type.
Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the vector sum.

◆ operator/()

template<typename T , typename T2 , std::size_t D>
Vector<D, T> svector::operator/ ( const Vector< D, T > &  lhs,
const T2  rhs 
)
inline

Scalar division.

Performs scalar division and returns a new vector representing the quotient.

Note
This method is only used if SVECTOR_USE_CLASS_OPERATORS is not defined. Otherwise, the operators in svector::Vector are used.
Template Parameters
DThe number of dimensions.
TVector type.
T2Scalar division type.
Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A new vector representing the scalar product.

◆ operator==()

template<typename T , std::size_t D>
bool svector::operator== ( const Vector< D, T > &  lhs,
const Vector< D, T > &  rhs 
)
inline

Compares equality of two vectors.

Note
This method is only used if SVECTOR_USE_CLASS_OPERATORS is not defined. Otherwise, the operators in svector::Vector are used.
The dimensions of the two vectors must be the same.
Template Parameters
DThe number of dimensions.
TVector type.
Parameters
lhsThe first vector.
rhsThe second vector.
Returns
A boolean representing whether the two vectors compare equal.

◆ rotate()

Vector2D svector::rotate ( const Vector2D v,
const double  ang 
)
inline

Rotates a 2D vector by a certain angle.

The angle should be given in radians. The vector rotates counterclockwise when the angle is positive and clockwise when the angle is negative.

Parameters
vA 2D vector.
angthe angle to rotate the vector, in radians.
Returns
a new, rotated vector.

◆ rotateAlpha()

Vector3D svector::rotateAlpha ( const Vector3D v,
const double &  ang 
)
inline

Rotates around x-axis.

Uses the basic gimbal-like 3D rotation matrices for rotation.

Parameters
vA 3D vector.
angThe angle to rotate the vector, in radians.
Returns
A new, rotated vector.

◆ rotateBeta()

Vector3D svector::rotateBeta ( const Vector3D v,
const double &  ang 
)
inline

Rotates around y-axis.

Uses the basic gimbal-like 3D rotation matrices for rotation.

Parameters
vA 3D vector.
angThe angle to rotate the vector, in radians.
Returns
A new, rotated vector.

◆ rotateGamma()

Vector3D svector::rotateGamma ( const Vector3D v,
const double &  ang 
)
inline

Rotates around z-axis.

Uses the basic gimbal-like 3D rotation matrices for rotation.

Parameters
vA 3D vector.
angThe angle to rotate the vector, in radians.
Returns
A new, rotated vector.

◆ x() [1/4]

double svector::x ( const Vector2D v)
inline

Gets the x-component of a 2D vector.

Parameters
vA 2D Vector.
Returns
x-component of the vector.

◆ x() [2/4]

double svector::x ( const Vector3D v)
inline

Gets the x-component of a 3D vector.

Parameters
vA 3D Vector.
Returns
x-component of the vector.

◆ x() [3/4]

void svector::x ( Vector2D v,
const double  xValue 
)
inline

Sets the x-component of a 2D vector.

Parameters
vA 2D Vector.
xValueThe x-value to set to the vector.

◆ x() [4/4]

void svector::x ( Vector3D v,
const double  xValue 
)
inline

Sets the x-component of a 3D vector.

Parameters
vA 3D Vector.
xValueThe x-value to set to the vector.

◆ y() [1/4]

double svector::y ( const Vector2D v)
inline

Gets the y-component of a 2D vector.

Parameters
vA 2D Vector.
Returns
y-component of the vector.

◆ y() [2/4]

double svector::y ( const Vector3D v)
inline

Gets the y-component of a 3D vector.

Parameters
vA 3D Vector.
Returns
y-component of the vector.

◆ y() [3/4]

void svector::y ( Vector2D v,
const double  yValue 
)
inline

Sets the y-component of a 2D vector.

Parameters
vA 2D Vector.
yValueThe y-value to set to the vector.

◆ y() [4/4]

void svector::y ( Vector3D v,
const double  yValue 
)
inline

Sets the y-component of a 3D vector.

Parameters
vA 3D Vector.
yValueThe y value to set to the vector.

◆ z() [1/2]

double svector::z ( const Vector3D v)
inline

Gets the z-component of a 3D vector.

Parameters
vA 3D Vector.
Returns
z-component of the vector.

◆ z() [2/2]

void svector::z ( Vector3D v,
const double  zValue 
)
inline

Sets the z-component of a 3D vector.

Parameters
vA 3D Vector.
zValueThe z value to set to the vector.