Vectors - Horizon-NTH/HorizonGUI GitHub Wiki

Introduction

We have developed a collection of classes designed to streamline vector manipulation, inspired by the vector class in GLSL, resulting in more readable and efficient code.
These classes are part of the hgui::kernel namespace, defined in the header file Maths.hpp, and all inherit from the Vector class defined in the header file Vector.hpp.

Notes:

  • If you want to have the same name as the GLSL vectors, check the typedefs.
  • Many of the constructors and functions of the following classes are implemented for multiple template parameters to avoid implicit conversion warning.

Table of Contents


Vector

Overview

The Vector class is a generic class template that represents a mathematical vector with a specified dimension.
It provides essential functionality for working with vectors, including mathematical operations and iteration.

Template Parameters

  • T: Type of the elements in the vector.
  • dimension: The dimension of the vector.

Constructors

  • Vector() noexcept: Default constructor.
  • Vector(const T& initializationValue) noexcept: Constructor that initializes all the vector coordinates with a specified value.
  • Vector(const Vector& vector) noexcept: Copy constructor.
  • Vector(Vector&& vector) noexcept: Move constructor.
  • Vector(const std::valarray<T>& vector) noexcept: Constructor that initializes the vector from a std::valarray.
  • Vector(const std::valarray<T>&& vector) noexcept: Move constructor from a std::valarray.

Assignment Operators

  • Vector& operator=(const Vector& vector) noexcept: Copy assignment operator.
  • Vector& operator=(Vector&& vector) noexcept: Move assignment operator.

Member Functions

  • T length() const: Calculate and return the length (magnitude) of the vector.
  • void normalize(): Normalize the vector to make its length 1 while maintaining direction.
  • void set(const std::initializer_list<T>& newValues) noexcept: Set the values of the vector using an std::initializer_list.
  • const std::valarray<T>& get_data() const noexcept: Retrieves the vector data.
  • void set_data(const std::valarray<T>& valarray) noexcept: Setthe vector data.
  • T& operator[](int indice) noexcept: Access the element at the specified index.
  • const T& operator[](int indice) const noexcept: Access the element at the specified index (const).
  • void operator-() noexcept: Return the negated vector.

Operator

Various operators are overloaded for this class as follows:

  • You can add + or subtract - vectors between them.
  • You can multiply * or divide / a vector with a scalar.
  • You can compare vectors between them with classic operator == and !=.
  • You can stream the vector in an std::ostream.

Iterator Functions

All standard iterator function are implemented allowing you to use your vector in range-base for loop or in some algorithms even if it was not created for and may produce unpredictable results.

Example Usage

#include <hgui/header/Vector.hpp>

// Example Usage of Vector
hgui::kernel::Vector<int, 3> vec;
for (auto& component : vec)
    componant = /* Set some value */;

// Perform operations
auto scaledVector = 3 * vec;
auto newVector = scaledVector - hgui::kernel::Vector<int, 3>(3);

newVector.normalize();

// See the result
std::cout << newVector << std::endl;

Note: In the "Example Usage" section, make sure to replace /* Set some value */ with actual code that initializes your Vector components.


GLSLvec2

Overview

The GLSLvec2 class defines a 2-dimensional vector in the style of GLSL.
This class simplifies operations involving 2D vectors and is compatible with various data types.

Template Parameters

  • T: Type of the elements.

Constructors

  • GLSLvec2(): Default constructor.
  • GLSLvec2(T initializationValue): Constructor that initializes both coordinates with a specified value.
  • GLSLvec2(T x, T y): Constructor with x and y components.
  • GLSLvec2(const GLSLvec2& vector): Copy constructor.
  • GLSLvec2(const Vector<T, 2>& vector): Constructor with a vector.
  • GLSLvec2(const glm::vec<2, T>& vector): Constructor with a glm::vec<2, T>.

Assignment Operators

  • GLSLvec2& operator=(const GLSLvec2& vector): Assignment operator.
  • GLSLvec2& operator=(const glm::vec2& vector): Assignment operator from a glm::vec2.

Operator

This class have the same operators that from its base class.

Public Variables

  • T& x : Represents the x-coordinate of the vector.
  • T& y : Represents the y-coordinate of the vector.

Example Usage

// Create your vector
hgui::kernel::GLSLvec2<float> vec = {1.0f, 2.0f};
float x = vec.x;
float y = vec.y;

// Perform some operations
auto vec2 = vec * 3

// See the results
std::cout << vec2 << std::endl;

GLSLvec3

Overview

The GLSLvec3 class defines a 3-dimensional vector in the style of GLSL.
This class simplifies operations involving 3D vectors and is compatible with various data types.

Template Parameters

  • T: Type of the elements.

Constructors

  • GLSLvec3(): Default constructor.
  • GLSLvec3(T initializationValue): Constructor that initializes both coordinates with a specified value.
  • GLSLvec3(T x, T y, T z): Constructor with x, y and z components.
  • GLSLvec3(const GLSLvec3& vector): Copy constructor.
  • GLSLvec3(const Vector<T, 3>& vector): Constructor with a vector.
  • GLSLvec3(const glm::vec<2, T>& vector, T z = {}): Constructor that initialize x and y component with a glm::vec<2, T> and another facultative z component.
  • GLSLvec3(const glm::vec<3, T>& vector): Constructor with a glm::vec<3, T>.

Assignment Operators

  • GLSLvec3& operator=(const GLSLvec3& vector): Assignment operator.
  • GLSLvec3& operator=(const glm::vec2& vector): Assignment operator from a glm::vec2.
  • GLSLvec3& operator=(const glm::vec3& vector): Assignment operator from a glm::vec3.

Operator

This class have the same operators that from its base class.

Public Variables

  • T& x : Represents the x-coordinate of the vector.
  • T& y : Represents the y-coordinate of the vector.
  • T& z : Represents the z-coordinate of the vector.

Example Usage

// Create your vector
hgui::kernel::GLSLvec3<float> vec = {1.0f, 2.0f, 3.0f};
float x = vec.x;
float y = vec.y;
float z = vec.z;

// Perform some operations
auto vec3 = vec * 3

// See the results
std::cout << vec3 << std::endl;

GLSLvec4

Overview

The GLSLvec4 class defines a 4-dimensional vector in the style of GLSL.
This class simplifies operations involving 4D vectors and is compatible with various data types.

Template Parameters

  • T: Type of the elements.

Constructors

  • GLSLvec4(): Default constructor.
  • GLSLvec4(T initializationValue): Constructor that initializes both coordinates with a specified value.
  • GLSLvec4(T x, T y, T z, T w): Constructor with x, y and z components.
  • GLSLvec4(const GLSLvec4& vector): Copy constructor.
  • GLSLvec4(const Vector<T, 4>& vector): Constructor with a vector.
  • GLSLvec4(const glm::vec<2, T>& vector, T z = {}, T w = {}): Constructor that initialize x and y component with a glm::vec<2, T> and facultative z, w components.
  • GLSLvec4(const glm::vec<3, T>& vector, T w = {}): Constructor that initialize x, y and z component with a glm::vec<3, T> and another facultative w component.
  • GLSLvec4(const glm::vec<4, T>& vector): Constructor with a glm::vec<4, T>.

Assignment Operators

  • GLSLvec4& operator=(const GLSLvec4& vector): Assignment operator.
  • GLSLvec4& operator=(const glm::vec2& vector): Assignment operator from a glm::vec2.
  • GLSLvec4& operator=(const glm::vec3& vector): Assignment operator from a glm::vec3.
  • GLSLvec4& operator=(const glm::vec4& vector): Assignment operator from a glm::vec4.

Operator

This class have the same operators that from its base class.

Public Variables

  • T& x : Represents the x-coordinate of the vector.
  • T& y : Represents the y-coordinate of the vector.
  • T& z : Represents the z-coordinate of the vector.
  • T& w : Represents the w-coordinate of the vector.

Example Usage

// Create your vector
hgui::kernel::GLSLvec4<float> vec = {1.0f, 2.0f, 3.0f, 1.0f};
float x = vec.x;
float y = vec.y;
float z = vec.z;
float w = vec.w;

// Perform some operations
auto vec4 = vec * 3

// See the results
std::cout << vec4 << std::endl;

Color

Overview

The Color class is designed to represent colors, both in RGB and RGBA formats. This class provides a convenient way to work with colors. The range of the colors' component is between 0 and 1.

Template Parameters

  • T: Type of the elements.

Constructors

  • Color() noexcept: Default constructor.
  • explicit Color(T rgb, T a = 1.0f) noexcept: Constructor with the same value for all RGB components and optional alpha.
  • explicit Color(const std::string& hexColor, T a = static_cast<T>(1)) noexcept: Constructor from an hexadecimal value and optional alpha.
  • Color(T r, T g, T b, T a = 1.0f) noexcept: Constructor with separate components and optional alpha.
  • Color(const Color& color) noexcept: Copy constructor.
  • Color(const kernel::Vector<T, 3>& color) noexcept: Constructor with a 3D vector.
  • Color(const kernel::Vector<T, 4>& color) noexcept: Constructor with a 4D vector.
  • explicit Color(const glm::vec<3, T>& color) noexcept: Constructor with a glm::vec<3, T>.
  • explicit Color(const glm::vec<4, T>& color) noexcept: Constructor with a glm::vec<4, T>.

Assignment Operators

  • Color<T>& operator=(const Color<T>& color): Assignment operator.
  • Color<T>& operator=(const glm::vec3& color): Assignment operator from glm::vec2.
  • Color<T>& operator=(const glm::vec4& color): Assignment operator from glm::vec3.

Public Variables

  • T r : Represents the red value of the color.
  • T g : Represents the green value of the color.
  • T b : Represents the blue value of the color.
  • T a : Represents the alpha value of the color.

Example Usage

hgui::kernel::Color<float> color = {1.0f, 0.5f, 0.0f, 1.0f};
float r = color.r;
float g = color.g;
float b = color.b;
float a = color.a;

Additional Functions

  • dot(const Vector<T, dimension>& u, const Vector<T, dimension>& v): Calculate the dot product of two vectors.
  • cross(const Vector<T, 3>& u, const Vector<T, 3>& v): Calculate the cross product of two 3-dimensional vectors.
  • distance(const Vector<T, dimension>& u, const Vector<T, dimension>& v): Calculate the Euclidean distance between two vectors.
⚠️ **GitHub.com Fallback** ⚠️