HighMap library (C++)
Loading...
Searching...
No Matches
hmap::Vec3< T > Struct Template Reference

Vec3 class for basic manipulation of 3D vectors. More...

#include <algebra.hpp>

Public Member Functions

 Vec3 ()
 Default constructor initializing the vector to (0, 0, 0).
 
 Vec3 (T x, T y, T z)
 Parameterized constructor initializing the vector to given values.
 
 Vec3 (const std::vector< T > &vec)
 Constructs a Vec3 object from a std::vector.
 
bool operator== (const Vec3 &other_vec) const
 Equality operator.
 
bool operator!= (const Vec3 &other_vec) const
 Inequality operator.
 
Vec3operator/= (const T value)
 Division-assignment operator.
 
Vec3 operator/ (const Vec3 &other_vec) const
 Division operator.
 
Vec3 operator* (const Vec3 &other_vec) const
 Multiplication operator.
 
Vec3 operator+ (const Vec3 &other_vec) const
 Addition operator.
 
Vec3 operator- (const Vec3 &other_vec) const
 Subtraction operator.
 
Vec3 operator* (T scalar) const
 Scalar multiplication (Vec3 * scalar).
 
magnitude () const
 Calculate the magnitude (length) of the vector.
 
void normalize ()
 Normalize the vector to have a magnitude of 1.
 
sum () const
 Calculate the sum of the vector components.
 

Public Attributes

x
 
y
 
z
 The x, y, and z components of the vector.
 

Friends

Vec3 operator* (T scalar, const Vec3 &vec)
 Scalar multiplication (scalar * Vec3).
 
Vec3 cross (const Vec3 v1, const Vec3 v2)
 Friend function to calculate the cross product of two vectors.
 
float dot (const Vec3 v1, const Vec3 v2)
 Friend function to calculate the dot product of two vectors.
 

Detailed Description

template<typename T>
struct hmap::Vec3< T >

Vec3 class for basic manipulation of 3D vectors.

This class provides basic operations for 3D vectors, such as addition, subtraction, multiplication, division, and dot product calculation. The class supports various data types through templating.

Template Parameters
TData type for the vector components (e.g., int, float, double).

Constructor & Destructor Documentation

◆ Vec3() [1/3]

template<typename T >
hmap::Vec3< T >::Vec3 ( )
inline

Default constructor initializing the vector to (0, 0, 0).

Initializes the x, y, and z components to zero.

◆ Vec3() [2/3]

template<typename T >
hmap::Vec3< T >::Vec3 ( x,
y,
z 
)
inline

Parameterized constructor initializing the vector to given values.

Parameters
xThe x component of the vector.
yThe y component of the vector.
zThe z component of the vector.

◆ Vec3() [3/3]

template<typename T >
hmap::Vec3< T >::Vec3 ( const std::vector< T > &  vec)
inline

Constructs a Vec3 object from a std::vector.

This constructor takes a vector containing exactly three elements and assigns the first element to x, the second element to y, and the third to z.

Template Parameters
TThe type of elements in the vector (e.g., float, int, double).
Parameters
vecA const reference to a vector of size 3, where the first element corresponds to x, the second to y, and the third to z.
Exceptions
std::invalid_argumentIf the vector does not contain exactly three elements.

Member Function Documentation

◆ operator==()

template<typename T >
bool hmap::Vec3< T >::operator== ( const Vec3< T > &  other_vec) const
inline

Equality operator.

Compares two vectors for equality.

Parameters
other_vecThe vector to compare with.
Returns
true if the vectors are equal, false otherwise.

◆ operator!=()

template<typename T >
bool hmap::Vec3< T >::operator!= ( const Vec3< T > &  other_vec) const
inline

Inequality operator.

Compares two vectors for inequality.

Parameters
other_vecThe vector to compare with.
Returns
true if the vectors are not equal, false otherwise.

◆ operator/=()

template<typename T >
Vec3 & hmap::Vec3< T >::operator/= ( const T  value)
inline

Division-assignment operator.

Divides all components of the vector by a scalar value and assigns the result.

Parameters
valueThe scalar value to divide by.
Returns
A reference to the current vector after division.

◆ operator/()

template<typename T >
Vec3 hmap::Vec3< T >::operator/ ( const Vec3< T > &  other_vec) const
inline

Division operator.

Divides each component of the vector by the corresponding component of another vector.

Parameters
other_vecThe vector to divide by.
Returns
A new vector that is the result of the division.

◆ operator*() [1/2]

template<typename T >
Vec3 hmap::Vec3< T >::operator* ( const Vec3< T > &  other_vec) const
inline

Multiplication operator.

Multiplies each component of the vector by the corresponding component of another vector.

Parameters
other_vecThe vector to multiply by.
Returns
A new vector that is the result of the multiplication.

◆ operator+()

template<typename T >
Vec3 hmap::Vec3< T >::operator+ ( const Vec3< T > &  other_vec) const
inline

Addition operator.

Adds each component of the vector to the corresponding component of another vector.

Parameters
other_vecThe vector to add.
Returns
A new vector that is the result of the addition.

◆ operator-()

template<typename T >
Vec3 hmap::Vec3< T >::operator- ( const Vec3< T > &  other_vec) const
inline

Subtraction operator.

Subtracts each component of another vector from the corresponding component of this vector.

Parameters
other_vecThe vector to subtract.
Returns
A new vector that is the result of the subtraction.

◆ operator*() [2/2]

template<typename T >
Vec3 hmap::Vec3< T >::operator* ( scalar) const
inline

Scalar multiplication (Vec3 * scalar).

Multiplies each component of the vector by a scalar value.

Parameters
scalarThe scalar value to multiply with.
Returns
Vec3 A new vector with each component multiplied by the scalar.

◆ magnitude()

template<typename T >
T hmap::Vec3< T >::magnitude ( ) const
inline

Calculate the magnitude (length) of the vector.

Returns
The magnitude of the vector.

◆ normalize()

template<typename T >
void hmap::Vec3< T >::normalize ( )
inline

Normalize the vector to have a magnitude of 1.

This method modifies the vector in place. If the vector has zero length, the components remain unchanged to avoid division by zero.

◆ sum()

template<typename T >
T hmap::Vec3< T >::sum ( ) const
inline

Calculate the sum of the vector components.

Returns
The sum of the vector components.

Friends And Related Symbol Documentation

◆ operator*

template<typename T >
Vec3 operator* ( scalar,
const Vec3< T > &  vec 
)
friend

Scalar multiplication (scalar * Vec3).

Multiplies each component of the vector by a scalar value. This function allows expressions where the scalar is on the left side of the multiplication operator.

Parameters
scalarThe scalar value to multiply with.
vecThe vector to multiply.
Returns
Vec3 A new vector with each component multiplied by the scalar.

◆ cross

template<typename T >
Vec3 cross ( const Vec3< T >  v1,
const Vec3< T >  v2 
)
friend

Friend function to calculate the cross product of two vectors.

The cross product results in a vector that is perpendicular to the plane formed by the two input vectors.

Parameters
v1The first vector.
v2The second vector.
Returns
A new vector that is the result of the cross product.

◆ dot

template<typename T >
float dot ( const Vec3< T >  v1,
const Vec3< T >  v2 
)
friend

Friend function to calculate the dot product of two vectors.

The dot product is the sum of the products of the corresponding components of the vectors.

Parameters
v1The first vector.
v2The second vector.
Returns
The dot product of the two vectors.

Member Data Documentation

◆ x

template<typename T >
T hmap::Vec3< T >::x

◆ y

template<typename T >
T hmap::Vec3< T >::y

◆ z

template<typename T >
T hmap::Vec3< T >::z

The x, y, and z components of the vector.


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