+ | Vector classes

This commit is contained in:
TheRedShip
2024-10-13 18:06:57 +02:00
parent 8784b5ce02
commit 0bd05282cf
10 changed files with 407 additions and 33 deletions

View File

@ -10,8 +10,8 @@
/* */
/* ************************************************************************** */
#ifndef RT_H
# define RT_H
#ifndef RT__HPP
# define RT__HPP
# define WIDTH 1920
# define HEIGHT 1080
@ -21,4 +21,7 @@
# include <iostream>
# include "Vector/Vector.hpp"
# include "Window.hpp"
#endif

38
includes/Vector/Vec2.hpp Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Vec2.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 17:16:11 by TheRed #+# #+# */
/* Updated: 2024/10/13 17:16:11 by TheRed ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RT_VEC2__HPP
#define RT_VEC2__HPP
# include "Vector/Vector.hpp"
namespace RT
{
template<typename T>
class Vec2 : public Vector<Vec2<T>, T, 2>
{
public:
using Vector<Vec2<T>, T, 2>::Vector;
constexpr Vec2(T x, T y)
{
this->data[0] = x;
this->data[1] = y;
}
};
using Vec2f = Vec2<float>;
using Vec2d = Vec2<double>;
using Vec2i = Vec2<int>;
}
#endif

49
includes/Vector/Vec3.hpp Normal file
View File

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Vec3.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 17:17:04 by TheRed #+# #+# */
/* Updated: 2024/10/13 17:17:04 by TheRed ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RT_VEC3__HPP
# define RT_VEC3__HPP
# include "Vector/Vector.hpp"
# include <cmath>
namespace RT
{
template<typename T>
class Vec3 : public Vector<Vec3<T>, T, 3>
{
public:
using Vector<Vec3<T>, T, 3>::Vector;
constexpr Vec3(T x, T y, T z)
{
this->data[0] = x;
this->data[1] = y;
this->data[2] = z;
}
constexpr T Distance(const Vec3& other) const
{
T dx = this->data[0] - other.data[0];
T dy = this->data[1] - other.data[1];
T dz = this->data[2] - other.data[2];
return std::sqrt(dx * dx + dy * dy + dz * dz);
}
};
using Vec3f = Vec3<float>;
using Vec3d = Vec3<double>;
using Vec3i = Vec3<int>;
}
#endif

40
includes/Vector/Vec4.hpp Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Vec4.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 17:18:35 by TheRed #+# #+# */
/* Updated: 2024/10/13 17:18:35 by TheRed ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RT_VEC4__HPP
# define RT_VEC4__HPP
# include "Vector/Vector.hpp"
namespace RT
{
template<typename T>
class Vec4 : public Vector<Vec4<T>, T, 4>
{
public:
using Vector<Vec4<T>, T, 4>::Vector;
constexpr Vec4(T r, T g, T b, T a)
{
this->data[0] = r;
this->data[1] = g;
this->data[2] = b;
this->data[3] = a;
}
};
using Vec4f = Vec4<float>;
using Vec4d = Vec4<double>;
using Vec4i = Vec4<int>;
}
#endif

111
includes/Vector/Vector.hpp Normal file
View File

@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Vector.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 17:13:03 by TheRed #+# #+# */
/* Updated: 2024/10/13 17:13:03 by TheRed ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RT_VECTOR__HPP
#define RT_VECTOR__HPP
#include <iostream>
#include <array>
namespace RT
{
template<typename Derived, typename T, std::size_t N>
class Vector
{
public:
std::array<T, N> data;
constexpr Vector() { data.fill(0); }
constexpr Vector(std::initializer_list<T> values) { std::copy(values.begin(), values.end(), data.begin()); }
constexpr T& operator[](std::size_t i) { return data[i]; }
constexpr const T& operator[](std::size_t i) const { return data[i]; }
constexpr Derived operator+(const Vector& vec) const
{
Derived result;
for (std::size_t i = 0; i < N; ++i)
result[i] = data[i] + vec[i];
return result;
}
constexpr Derived operator-(const Vector& vec) const
{
Derived result;
for (std::size_t i = 0; i < N; ++i)
result[i] = data[i] - vec[i];
return result;
}
constexpr Derived operator*(T scalar) const
{
Derived result;
for (std::size_t i = 0; i < N; ++i)
result[i] = data[i] * scalar;
return result;
}
constexpr Derived operator/(T scalar) const
{
Derived result;
for (std::size_t i = 0; i < N; ++i)
result[i] = data[i] / scalar;
return result;
}
constexpr Derived& operator+=(const Vector& vec)
{
*this = *this + vec;
return static_cast<Derived&>(*this);
}
constexpr Derived& operator-=(const Vector& vec)
{
*this = *this - vec;
return static_cast<Derived&>(*this);
}
constexpr Derived& operator*=(const Vector& vec)
{
*this = *this * vec;
return static_cast<Derived&>(*this);
}
constexpr Derived& operator/=(const Vector& vec)
{
*this = *this / vec;
return static_cast<Derived&>(*this);
}
constexpr Derived& operator*=(T scalar)
{
*this = *this * scalar;
return static_cast<Derived&>(*this);
}
constexpr Derived& operator/=(T scalar)
{
*this = *this / scalar;
return static_cast<Derived&>(*this);
}
void print() const
{
std::cout << "Vec" << N << "(";
for (std::size_t i = 0; i < N; ++i) {
std::cout << data[i];
if (i < N - 1) std::cout << ", ";
}
std::cout << ")" << std::endl;
}
};
}
#include "Vector/Vec2.hpp"
#include "Vector/Vec3.hpp"
#include "Vector/Vec4.hpp"
#endif

45
includes/Window.hpp Normal file
View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Window.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 16:15:41 by TheRed #+# #+# */
/* Updated: 2024/10/13 16:15:41 by TheRed ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef RT_WINDOW__HPP
# define RT_WINDOW__HPP
# include "RT.hpp"
class Window
{
public:
Window(void);
Window(Window const &src);
~Window(void);
Window &operator=(Window const &rhs);
GLFWwindow *getWindow(void) const;
RT::Vec2i getMousePos(void) const;
void display();
void pollEvents();
bool shouldClose();
static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
static void mouseMoveCallback(GLFWwindow *window, double xpos, double ypos);
static void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods);
private:
GLFWwindow *_window;
RT::Vec2i _mousePos;
};
#endif