Plasma Engine  2.0
Loading...
Searching...
No Matches
plVec3Template< Type > Class Template Reference

A 3-component vector class. More...

#include <Vec3.h>

Public Types

using ComponentType = Type
 

Public Member Functions

 PL_DECLARE_POD_TYPE ()
 
 plVec3Template ()
 default-constructed vector is uninitialized (for speed)
 
 plVec3Template (Type x, Type y, Type z)
 Initializes the vector with x,y,z.
 
 plVec3Template (Type v)
 Initializes all 3 components with xyz.
 
const plVec2Template< Type > GetAsVec2 () const
 Returns an plVec2Template with x and y from this vector.
 
const plVec4Template< Type > GetAsVec4 (Type w) const
 Returns an plVec4Template with x,y,z from this vector and w set to the parameter.
 
const plVec4Template< Type > GetAsPositionVec4 () const
 Returns an plVec4Template with x,y,z from this vector and w set 1.
 
const plVec4Template< Type > GetAsDirectionVec4 () const
 Returns an plVec4Template with x,y,z from this vector and w set 0.
 
const Type * GetData () const
 Returns the data as an array.
 
Type * GetData ()
 Returns the data as an array.
 
void Set (Type xyz)
 Sets all 3 components to this value.
 
void Set (Type x, Type y, Type z)
 Sets the vector to these values.
 
void SetZero ()
 Sets the vector to all zero.
 
PL_DECLARE_IF_FLOAT_TYPE Type GetLength () const
 Returns the length of the vector.
 
PL_DECLARE_IF_FLOAT_TYPE plResult SetLength (Type fNewLength, Type fEpsilon=plMath::DefaultEpsilon< Type >())
 Tries to rescale the vector to the given length. If the vector is too close to zero, PL_FAILURE is returned and the vector is set to zero.
 
Type GetLengthSquared () const
 Returns the squared length. Faster, since no square-root is taken. Useful, if one only wants to compare the lengths of two vectors.
 
PL_DECLARE_IF_FLOAT_TYPE Type GetLengthAndNormalize ()
 Normalizes this vector and returns its previous length in one operation. More efficient than calling GetLength and then Normalize.
 
PL_DECLARE_IF_FLOAT_TYPE const plVec3Template< Type > GetNormalized () const
 Returns a normalized version of this vector, leaves the vector itself unchanged.
 
PL_DECLARE_IF_FLOAT_TYPE void Normalize ()
 Normalizes this vector.
 
PL_DECLARE_IF_FLOAT_TYPE plResult NormalizeIfNotZero (const plVec3Template< Type > &vFallback=plVec3Template< Type >(1, 0, 0), Type fEpsilon=plMath::SmallEpsilon< Type >())
 Tries to normalize this vector. If the vector is too close to zero, PL_FAILURE is returned and the vector is set to the given fallback value.
 
bool IsZero () const
 Returns, whether this vector is (0, 0, 0).
 
bool IsZero (Type fEpsilon) const
 Returns, whether this vector is (0, 0, 0) within a given epsilon.
 
PL_DECLARE_IF_FLOAT_TYPE bool IsNormalized (Type fEpsilon=plMath::HugeEpsilon< Type >()) const
 Returns, whether the squared length of this vector is between 0.999f and 1.001f.
 
bool IsNaN () const
 Returns true, if any of x, y or z is NaN.
 
bool IsValid () const
 Checks that all components are finite numbers.
 
const plVec3Template< Type > operator- () const
 Returns the negation of this vector.
 
void operator+= (const plVec3Template< Type > &rhs)
 Adds rhs component-wise to this vector.
 
void operator-= (const plVec3Template< Type > &rhs)
 Subtracts rhs component-wise from this vector.
 
void operator*= (const plVec3Template< Type > &rhs)
 Multiplies rhs component-wise to this vector.
 
void operator/= (const plVec3Template< Type > &rhs)
 Divides this vector component-wise by rhs.
 
void operator*= (Type f)
 Multiplies all components of this vector with f.
 
void operator/= (Type f)
 Divides all components of this vector by f.
 
bool IsIdentical (const plVec3Template< Type > &rhs) const
 Equality Check (bitwise)
 
bool IsEqual (const plVec3Template< Type > &rhs, Type fEpsilon) const
 Equality Check with epsilon.
 
plAngle GetAngleBetween (const plVec3Template< Type > &rhs) const
 Returns the positive angle between *this and rhs. Both this and rhs must be normalized.
 
Type Dot (const plVec3Template< Type > &rhs) const
 Returns the Dot-product of the two vectors (commutative, order does not matter)
 
const plVec3Template< Type > CrossRH (const plVec3Template< Type > &rhs) const
 Returns the Cross-product of the two vectors (NOT commutative, order DOES matter)
 
const plVec3Template< Type > CompMin (const plVec3Template< Type > &rhs) const
 Returns the component-wise minimum of *this and rhs.
 
const plVec3Template< Type > CompMax (const plVec3Template< Type > &rhs) const
 Returns the component-wise maximum of *this and rhs.
 
const plVec3Template< Type > CompClamp (const plVec3Template< Type > &vLow, const plVec3Template< Type > &vHigh) const
 Returns the component-wise clamped value of *this between low and high.
 
const plVec3Template< Type > CompMul (const plVec3Template< Type > &rhs) const
 Returns the component-wise multiplication of *this and rhs.
 
const plVec3Template< Type > CompDiv (const plVec3Template< Type > &rhs) const
 Returns the component-wise division of *this and rhs.
 
const plVec3Template< Type > Abs () const
 brief Returns the component-wise absolute of *this.
 
PL_DECLARE_IF_FLOAT_TYPE plResult CalculateNormal (const plVec3Template< Type > &v1, const plVec3Template< Type > &v2, const plVec3Template< Type > &v3)
 Calculates the normal of the triangle defined by the three vertices. Vertices are assumed to be ordered counter-clockwise.
 
PL_DECLARE_IF_FLOAT_TYPE void MakeOrthogonalTo (const plVec3Template< Type > &vNormal)
 Modifies this direction vector to be orthogonal to the given (normalized) direction vector. The result is NOT normalized.
 
PL_DECLARE_IF_FLOAT_TYPE const plVec3Template< Type > GetOrthogonalVector () const
 Returns some arbitrary vector orthogonal to this one. The vector is NOT normalized.
 
PL_DECLARE_IF_FLOAT_TYPE const plVec3Template< Type > GetReflectedVector (const plVec3Template< Type > &vNormal) const
 Returns this vector reflected at vNormal.
 
PL_DECLARE_IF_FLOAT_TYPE const plVec3Template< Type > GetRefractedVector (const plVec3Template< Type > &vNormal, Type fRefIndex1, Type fRefIndex2) const
 Returns this vector, refracted at vNormal, using the refraction index of the current medium and the medium it enters.
 

Static Public Member Functions

static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeNaN ()
 Returns a vector with all components set to Not-a-Number (NaN).
 
static plVec3Template< Type > MakeZero ()
 Returns a vector with all components set to zero.
 
static plVec3Template< Type > MakeAxisX ()
 Returns a vector initialized to the X unit vector (1, 0, 0).
 
static plVec3Template< Type > MakeAxisY ()
 Returns a vector initialized to the Y unit vector (0, 1, 0).
 
static plVec3Template< Type > MakeAxisZ ()
 Returns a vector initialized to the Z unit vector (0, 0, 1).
 
static plVec3Template< Type > Make (Type x, Type y, Type z)
 Returns a vector initialized to x,y,z.
 
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomPointInSphere (plRandom &inout_rng)
 Returns a random point inside a unit sphere (radius 1).
 
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDirection (plRandom &inout_rng)
 Creates a random direction vector. The vector is normalized.
 
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviationX (plRandom &inout_rng, const plAngle &maxDeviation)
 Creates a random vector around the x axis with a maximum deviation angle of maxDeviation. The vector is normalized. The deviation angle must be larger than zero.
 
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviationY (plRandom &inout_rng, const plAngle &maxDeviation)
 Creates a random vector around the y axis with a maximum deviation angle of maxDeviation. The vector is normalized. The deviation angle must be larger than zero.
 
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviationZ (plRandom &inout_rng, const plAngle &maxDeviation)
 Creates a random vector around the z axis with a maximum deviation angle of maxDeviation. The vector is normalized. The deviation angle must be larger than zero.
 
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviation (plRandom &inout_rng, const plAngle &maxDeviation, const plVec3Template< Type > &vNormal)
 Creates a random vector around the given normal with a maximum deviation.
 

Public Attributes

Type x
 
Type y
 
Type z
 

Detailed Description

template<typename Type>
class plVec3Template< Type >

A 3-component vector class.

Member Function Documentation

◆ IsNormalized()

template<typename Type >
PL_IMPLEMENT_IF_FLOAT_TYPE PL_FORCE_INLINE bool plVec3Template< Type >::IsNormalized ( Type fEpsilon = plMath::HugeEpsilon<Type>()) const

Returns, whether the squared length of this vector is between 0.999f and 1.001f.

Note
Normalization, especially with SSE is not very precise. So this function checks whether the (squared) length is between a lower and upper limit.

◆ MakeOrthogonalTo()

template<typename Type >
PL_IMPLEMENT_IF_FLOAT_TYPE void plVec3Template< Type >::MakeOrthogonalTo ( const plVec3Template< Type > & vNormal)

Modifies this direction vector to be orthogonal to the given (normalized) direction vector. The result is NOT normalized.

Note
This function may fail, e.g. create a vector that is zero, if the given normal is parallel to the vector itself. If you need to handle such cases, you should manually check afterwards, whether the result is zero, or cannot be normalized.

◆ MakeRandomDeviation()

template<typename Type >
PL_IMPLEMENT_IF_FLOAT_TYPE plVec3Template< Type > plVec3Template< Type >::MakeRandomDeviation ( plRandom & inout_rng,
const plAngle & maxDeviation,
const plVec3Template< Type > & vNormal )
staticnodiscard

Creates a random vector around the given normal with a maximum deviation.

Note
If you are going to do this many times with the same axis, rather than calling this function, instead manually do what this function does (see inline code) and only compute the quaternion once.

◆ operator*=()

template<typename Type >
PL_FORCE_INLINE void plVec3Template< Type >::operator*= ( const plVec3Template< Type > & rhs)

Multiplies rhs component-wise to this vector.

Test
this is new

◆ operator/=()

template<typename Type >
PL_FORCE_INLINE void plVec3Template< Type >::operator/= ( const plVec3Template< Type > & rhs)

Divides this vector component-wise by rhs.

Test
this is new

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