flippy
a c++20 package for dynamically triangulated membrane simulations.
Loading...
Searching...
No Matches
fp::vec3< Real > Class Template Reference

Internal implementation of a 3D vector. More...

#include <vec3.hpp>

Collaboration diagram for fp::vec3< Real >:

Public Member Functions

void add (vec3< Real > const &v)
 In place addition method.
 
void subtract (vec3< Real > const &v)
 In place subtraction method.
 
void scale (Real s)
 Scale the vector by a real number s.
 
Real dot (vec3< Real > const &v) const
 Calculate dot product with another vector.
 
constexpr std::size_t size () const
 Always returns 3.
 
vec3< Real > cross (vec3< Real > const &other) const
 Calculate cross product with another vector.
 
Real norm () const
 Returns the norm of the vector.
 
Real norm_square () const
 Returns the square of the norm of the vector.
 
vec3< Real > const & normalize ()
 Normalize the vector in place. And return a reference to the new normalized vector.
 
bool operator== (vec3< Real > const &other) const =default
 default equality operator.
 
template<typename Index >
requires std::is_integral_v<Index>
Real & operator[] (Index idx)
 element access operator.
 
template<typename Index >
requires std::is_integral_v<Index>
const Real & operator[] (Index idx) const
 element access operator for constant environments.
 

Static Public Member Functions

static vec3< Real > cross (vec3< Real > const &a, vec3< Real > const &b)
 Calculate cross product between two vectors.
 

Public Attributes

Real x
 The x component of the vector.
 
Real y
 The y component of the vector.
 
Real z
 The z component of the vector.
 

Friends

std::ostream & operator<< (std::ostream &os, const vec3< Real > &obj)
 Streaming operator for easy printing of the vector.
 
vec3< Real > operator+ (vec3< Real > lhs, vec3< Real > const &rhs)
 Overloaded operator defined in terms of vec2::add.
 
void operator+= (vec3< Real > &lhs, vec3< Real > const &rhs)
 Overloaded operator defined in terms of vec3::add.
 
vec3< Real > operator- (vec3< Real > lhs, vec3< Real > const &rhs)
 Overloaded operator defined in terms of vec3::subtract.
 
void operator-= (vec3< Real > &lhs, vec3< Real > const &rhs)
 Overloaded operator defined in terms of vec3::subtract.
 
vec3< Real > operator* (Real const &lhs, vec3< Real > rhs)
 Overloaded operator defined in terms of vec3::scale.
 
vec3< Real > operator* (vec3< Real > lhs, Real const &rhs)
 Overloaded operator defined in terms of vec3::scale.
 
void operator/= (vec3< Real > &lhs, Real const &rhs)
 Overloaded operator defined in terms of vec3::scale.
 
vec3< Real > operator/ (vec3< Real > lhs, Real const &rhs)
 Overloaded operator defined in terms of vec3::scale.
 
vec3< Real > operator- (vec3< Real > v)
 Unary minus operator.
 

Detailed Description

template<floating_point_number Real>
class fp::vec3< Real >

Internal implementation of a 3D vector.

!!! vec3 does not throw !!! This means that if you ask vec3 to divide a vector by 0 or more realistically if you normalize a zero length vector vec3 will not check for the division by zero and will return a nan result! Since vec3 is used everywhere in flippy, including in very expensive calculations, I decided to omit the security check for the sake of speed.

To keep the external dependencies low, flippy implements it's own 3D vector class with basic functionality like dot product and cross product

Example:

++
fp::vec3<double> v1{1,0,0};
fp::vec3<double> v2{0,0,1};
assert(v1.dot(v2)==0);
assert(v1.cross(v2).norm()==1);
assert(((v1-v2)==fp::vec3<double>{1.,0.,-1.}));
Internal implementation of a 3D vector.
Definition vec3.hpp:43
Template Parameters
Realtype that will be used for all floating point numbers inside this class/struct. Any data type that satisfies the floating_point_number concept is allowed, for example, float.

Member Function Documentation

◆ add()

template<floating_point_number Real>
void fp::vec3< Real >::add ( vec3< Real > const & v)
inline

In place addition method.

Example:

++
fp::vec3<double> v1{1,0,0};
fp::vec3<double> v2{0,0,1};
v1.add(v2); // v1 will contain {1, 0, 1}
void add(vec3< Real > const &v)
In place addition method.
Definition vec3.hpp:60
Parameters
vadd this vector elementwise to the vector that is calling the add method.

◆ subtract()

template<floating_point_number Real>
void fp::vec3< Real >::subtract ( vec3< Real > const & v)
inline

In place subtraction method.

Example:

++
fp::vec3<double> v1{2,0,0};
fp::vec3<double> v2{1,0,1};
v1.subtract(v2); // v1 will contain {1, 0, -1}
void subtract(vec3< Real > const &v)
In place subtraction method.
Definition vec3.hpp:77
Parameters
vsubtract this vector elementwise from the vector that is calling the subtract method.

◆ scale()

template<floating_point_number Real>
void fp::vec3< Real >::scale ( Real s)
inline

Scale the vector by a real number s.

This function scales the vector in-place by the provided number s.

Parameters
smultiplicative prefactor.

◆ dot()

template<floating_point_number Real>
Real fp::vec3< Real >::dot ( vec3< Real > const & v) const
inline

Calculate dot product with another vector.

Example:

{c++}
fp::vec3<double> v1{1,0,0};
fp::vec3<double> v2{2,0,1};
double res = v1.dot(v2); // res will contain 2*1 + 0*0 + 0*1=2
Real dot(vec3< Real > const &v) const
Calculate dot product with another vector.
Definition vec3.hpp:106
Parameters
vthe other vec3 vector
Returns
result of the dot product between the original vector and v.
Here is the caller graph for this function:

◆ size()

template<floating_point_number Real>
std::size_t fp::vec3< Real >::size ( ) const
inlinenodiscardconstexpr

Always returns 3.

This function always returns 3 since vec3 can only have three elements. It was implemented for completeness, to make it more easy for vec3 to be used as a drop-in replacement for other vector types.

Returns
Size (number of elements) of vec3.

◆ cross() [1/2]

template<floating_point_number Real>
static vec3< Real > fp::vec3< Real >::cross ( vec3< Real > const & a,
vec3< Real > const & b )
inlinestatic

Calculate cross product between two vectors.

A static method to calculate cross product between two vectors. Example:

{c++}
fp::vec3<double> v1{1,0,0};
fp::vec3<double> v2{0,1,0};
fp::vec3<double> v3 = cross(v1, v2); // v3 will contain {0,0,1}
static vec3< Real > cross(vec3< Real > const &a, vec3< Real > const &b)
Calculate cross product between two vectors.
Definition vec3.hpp:133
Parameters
afirst vector of the cross product
bsecond vector of the cross product
Returns
result of the cross product between the original vector and v.
Here is the caller graph for this function:

◆ cross() [2/2]

template<floating_point_number Real>
vec3< Real > fp::vec3< Real >::cross ( vec3< Real > const & other) const
inline

Calculate cross product with another vector.

Example:

{c++}
fp::vec3<double> v1{1,0,0};
fp::vec3<double> v2{0,1,0};
fp::vec3<double> v3 = v1.cross(v2); // v3 will contain {0,0,1}
Parameters
otherthe other vec3 vector.
Returns
result of the cross product between the original vector and other.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ norm()

template<floating_point_number Real>
Real fp::vec3< Real >::norm ( ) const
inline

Returns the norm of the vector.

Example:

{c++}
double res = v.norm(); // res will contain 1,4142135624... i.e. sqrt(2)
Real norm() const
Returns the norm of the vector.
Definition vec3.hpp:164
Returns
The euclidian norm of the vector.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ norm_square()

template<floating_point_number Real>
Real fp::vec3< Real >::norm_square ( ) const
inline

Returns the square of the norm of the vector.

Example:

{c++}
double res = v.norm_square(); // res will contain 2
Real norm_square() const
Returns the square of the norm of the vector.
Definition vec3.hpp:175
Returns
Square of the euclidian norm of the vector.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ normalize()

template<floating_point_number Real>
vec3< Real > const & fp::vec3< Real >::normalize ( )
inline

Normalize the vector in place. And return a reference to the new normalized vector.

Warning
If you normalize a zero length vector, you effectively demand to divide by zero! this function will not do a security check for you and will just return nan!
Returns
Reference to the normalized vector.
Here is the call graph for this function:

◆ operator==()

template<floating_point_number Real>
bool fp::vec3< Real >::operator== ( vec3< Real > const & other) const
default

default equality operator.

Parameters
othervec3 on the right hand side of the comparison operator.
Returns
true if all elements of the compared vectors are equal and to false otherwise.

◆ operator[]() [1/2]

template<floating_point_number Real>
template<typename Index >
requires std::is_integral_v<Index>
Real & fp::vec3< Real >::operator[] ( Index idx)
inline

element access operator.

Template Parameters
Indexautomatically deduced type of the index.
Parameters
idxcan only be 0 1 or 2. Any other number will cause the program to exit with an error.
Returns
for a vec3 v: v[1] returns v.x, v[2] returns v.y and v[3] returns v.z.
Note
: The use of the subscription operator might be slower than the direct access of the data member.

◆ operator[]() [2/2]

template<floating_point_number Real>
template<typename Index >
requires std::is_integral_v<Index>
const Real & fp::vec3< Real >::operator[] ( Index idx) const
inline

element access operator for constant environments.

Template Parameters
Indexautomatically deduced type of the index.
Parameters
idxcan only be 0 1 or 2. Any other number will cause the program to exit with an error.
Returns
for a vec3 v: v[1] returns a constant reference to v.x, v[2] returns a constant reference to v.y and v[3] returns a constant reference to v.z.
Note
: The use of the subscription operator might be slower than the direct access of the data member.

Friends And Related Symbol Documentation

◆ operator+

template<floating_point_number Real>
vec3< Real > operator+ ( vec3< Real > lhs,
vec3< Real > const & rhs )
friend

Overloaded operator defined in terms of vec2::add.

Parameters
lhsleft hand side of the + operator
rhsright hand side oif the + operator
Returns
equivalent to a new copy of lhs.add(rhs).

◆ operator+=

template<floating_point_number Real>
void operator+= ( vec3< Real > & lhs,
vec3< Real > const & rhs )
friend

Overloaded operator defined in terms of vec3::add.

Equivalent to lhs.add(rhs).

Parameters
lhsleft hand side of the += operator
rhsright hand side oif the += operator

◆ operator- [1/2]

template<floating_point_number Real>
vec3< Real > operator- ( vec3< Real > lhs,
vec3< Real > const & rhs )
friend

Overloaded operator defined in terms of vec3::subtract.

Parameters
lhsleft hand side of the - operator
rhsright hand side oif the - operator
Returns
equivalent to a new copy of lhs.subtract(rhs).

◆ operator-=

template<floating_point_number Real>
void operator-= ( vec3< Real > & lhs,
vec3< Real > const & rhs )
friend

Overloaded operator defined in terms of vec3::subtract.

Equivalent to lhs.subtract(rhs).

Parameters
lhsleft hand side of the -= operator
rhsright hand side oif the -= operator

◆ operator* [1/2]

template<floating_point_number Real>
vec3< Real > operator* ( Real const & lhs,
vec3< Real > rhs )
friend

Overloaded operator defined in terms of vec3::scale.

Left multiplication by a scalar s*v.

Parameters
lhsleft hand side of the * operator
rhsright hand side oif the * operator
Returns
equivalent to a new copy of rhs.scale(lhs).

◆ operator* [2/2]

template<floating_point_number Real>
vec3< Real > operator* ( vec3< Real > lhs,
Real const & rhs )
friend

Overloaded operator defined in terms of vec3::scale.

Right multiplication by a scalar v*s.

Parameters
lhsleft hand side of the * operator
rhsright hand side oif the * operator
Returns
equivalent to a new copy of lhs.scale(rhs).

◆ operator/=

template<floating_point_number Real>
void operator/= ( vec3< Real > & lhs,
Real const & rhs )
friend

Overloaded operator defined in terms of vec3::scale.

In place division by a scalar v/s, equivalent to lhs.scale(1/rhs).

Parameters
lhsleft hand side of the /= operator
rhsright hand side oif the /= operator
Warning
for performance reasons, this function will not check for zero division!

◆ operator/

template<floating_point_number Real>
vec3< Real > operator/ ( vec3< Real > lhs,
Real const & rhs )
friend

Overloaded operator defined in terms of vec3::scale.

Division by a scalar v/s.

Parameters
lhsleft hand side of the / operator
rhsright hand side oif the / operator
Returns
equivalent to a new copu of lhs.scale(1/rhs).
Warning
for performance reasons, this function will not check for zero division!

◆ operator- [2/2]

template<floating_point_number Real>
vec3< Real > operator- ( vec3< Real > v)
friend

Unary minus operator.

Parameters
voriginal vector.
Returns
A copy of -v the vector v itself stays unaffected.

The documentation for this class was generated from the following file: