Leonetienne/Eule
Homemade math library, mainly targetted towards computer graphics
Vector4.h
Go to the documentation of this file.
1 #pragma once
2 #include <cstdlib>
3 #include <iomanip>
4 #include <ostream>
5 #include <sstream>
6 #include "Matrix4x4.h"
7 
8 namespace Eule
9 {
10  template <typename T> class Vector2;
11  template <typename T> class Vector3;
12 
16  template <typename T>
17  class Vector4
18  {
19  public:
20  Vector4() : x{ 0 }, y{ 0 }, z{ 0 }, w{ 0 } {}
21  Vector4(T _x, T _y, T _z, T _w) : x{ _x }, y{ _y }, z{ _z }, w{ _w } {}
22  Vector4(const Vector4<T>& other) = default;
23  Vector4(Vector4<T>&& other) noexcept = default;
24 
26  double SqrMagnitude() const;
27 
29  double Magnitude() const;
30 
32  [[nodiscard]] Vector4<double> Normalize() const;
33 
35  void NormalizeSelf();
36 
38  [[nodiscard]] Vector4<T> VectorScale(const Vector4<T>& scalar) const;
39 
41  void LerpSelf(const Vector4<T>& other, double t);
42 
44  [[nodiscard]] Vector4<double> Lerp(const Vector4<T>& other, double t) const;
45 
47  [[nodiscard]] bool Similar(const Vector4<T>& other, double epsilon = 0.00001) const;
48 
50  [[nodiscard]] Vector4<int> ToInt() const;
51 
53  [[nodiscard]] Vector4<double> ToDouble() const;
54 
55  T& operator[](std::size_t idx);
56  const T& operator[](std::size_t idx) const;
57 
58  Vector4<T> operator+(const Vector4<T>& other) const;
59  void operator+=(const Vector4<T>& other);
60  Vector4<T> operator-(const Vector4<T>& other) const;
61  void operator-=(const Vector4<T>& other);
62  Vector4<T> operator*(const T scale) const;
63  void operator*=(const T scale);
64  Vector4<T> operator/(const T scale) const;
65  void operator/=(const T scale);
66  Vector4<T> operator*(const Matrix4x4& mat) const;
67  void operator*=(const Matrix4x4& mat);
68  Vector4<T> operator-() const;
69 
70  operator Vector2<T>() const;
71  operator Vector3<T>() const;
72 
73  void operator=(const Vector4<T>& other);
74  void operator=(Vector4<T>&& other) noexcept;
75 
76  bool operator==(const Vector4<T>& other) const;
77  bool operator!=(const Vector4<T>& other) const;
78 
79  friend std::ostream& operator << (std::ostream& os, const Vector4<T>& v)
80  {
81  return os << "[x: " << v.x << " y: " << v.y << " z: " << v.z << " w: " << v.w << "]";
82  }
83  friend std::wostream& operator << (std::wostream& os, const Vector4<T>& v)
84  {
85  return os << L"[x: " << v.x << L" y: " << v.y << L" z: " << v.z << L" w: " << v.w << L"]";
86  }
87 
88  T x;
89  T y;
90  T z;
91  T w;
92 
93  // Some handy predefines
94  static const Vector4<double> up;
95  static const Vector4<double> down;
96  static const Vector4<double> right;
97  static const Vector4<double> left;
98  static const Vector4<double> forward;
99  static const Vector4<double> backward;
100  static const Vector4<double> future;
101  static const Vector4<double> past;
102  static const Vector4<double> one;
103  static const Vector4<double> zero;
104  };
105 
108 }
Eule::Vector4::ToInt
Vector4< int > ToInt() const
Will convert this vector to a Vector4i.
Definition: Vector4.cpp:173
Eule::Vector4::Vector4
Vector4(T _x, T _y, T _z, T _w)
Definition: Vector4.h:21
Eule::Vector4::operator+=
void operator+=(const Vector4< T > &other)
Definition: Vector4.cpp:376
Eule::Vector4::operator/
Vector4< T > operator/(const T scale) const
Definition: Vector4.cpp:603
Eule::Vector4::operator*
Vector4< T > operator*(const T scale) const
Definition: Vector4.cpp:512
Eule::Vector4::NormalizeSelf
void NormalizeSelf()
Will normalize this vector.
Definition: Vector4.cpp:148
Eule::Vector4::Vector4
Vector4()
Definition: Vector4.h:20
Eule::Vector4::backward
static const Vector4< double > backward
Definition: Vector4.h:99
Eule::Vector4::x
T x
Definition: Vector4.h:88
Eule::Vector4::operator=
void operator=(const Vector4< T > &other)
Conversion method.
Definition: Vector4.cpp:730
Eule::Vector4::operator-=
void operator-=(const Vector4< T > &other)
Definition: Vector4.cpp:466
Eule::Vector4::Normalize
Vector4< double > Normalize() const
Will return the normalization of this vector.
Definition: Vector4.cpp:92
Eule::Vector4::z
T z
Definition: Vector4.h:90
Eule::Vector4::operator*=
void operator*=(const T scale)
Definition: Vector4.cpp:557
Eule::Vector4d
Vector4< double > Vector4d
Definition: Vector4.h:107
Eule::Vector4::VectorScale
Vector4< T > VectorScale(const Vector4< T > &scalar) const
Will scale self.n by scalar.n.
Definition: Vector4.cpp:79
Eule::Vector4::operator-
Vector4< T > operator-() const
Definition: Vector4.cpp:719
Eule::Vector4::Lerp
Vector4< double > Lerp(const Vector4< T > &other, double t) const
Will return a lerp result between this and another vector.
Definition: Vector4.cpp:287
Eule::Vector4::one
static const Vector4< double > one
Definition: Vector4.h:102
Eule::Vector4::LerpSelf
void LerpSelf(const Vector4< T > &other, double t)
Will lerp itself towards other by t.
Definition: Vector4.cpp:267
Matrix4x4.h
Eule::Vector4::forward
static const Vector4< double > forward
Definition: Vector4.h:98
Eule::Vector4::operator/=
void operator/=(const T scale)
Definition: Vector4.cpp:647
Eule::Vector4::w
T w
Definition: Vector4.h:91
Eule::Vector4::down
static const Vector4< double > down
Definition: Vector4.h:95
Eule::Vector4::operator+
Vector4< T > operator+(const Vector4< T > &other) const
Definition: Vector4.cpp:331
Eule::Vector4::operator[]
T & operator[](std::size_t idx)
Definition: Vector4.cpp:185
Eule::Vector4::ToDouble
Vector4< double > ToDouble() const
Will convert this vector to a Vector4d.
Definition: Vector4.cpp:179
Eule::Vector4i
Vector4< int > Vector4i
Definition: Vector4.h:106
Eule::Vector4::operator!=
bool operator!=(const Vector4< T > &other) const
Definition: Vector4.cpp:767
Eule::Vector4::Similar
bool Similar(const Vector4< T > &other, double epsilon=0.00001) const
Will compare if two vectors are similar to a certain epsilon value.
Definition: Vector4.cpp:162
Eule::Vector4::Magnitude
double Magnitude() const
Will compute the magnitude.
Definition: Vector4.cpp:38
Eule::Vector4::future
static const Vector4< double > future
Definition: Vector4.h:100
Eule::Vector4::operator==
bool operator==(const Vector4< T > &other) const
Definition: Vector4.cpp:659
Eule::Vector4::zero
static const Vector4< double > zero
Definition: Vector4.h:103
Eule::Vector4::SqrMagnitude
double SqrMagnitude() const
Will compute the square magnitude.
Definition: Vector4.cpp:31
Eule::Vector4::up
static const Vector4< double > up
Definition: Vector4.h:94
Eule::Vector4::past
static const Vector4< double > past
Definition: Vector4.h:101
Eule::Vector4::operator<<
friend std::ostream & operator<<(std::ostream &os, const Vector4< T > &v)
Definition: Vector4.h:79
Eule::Vector4::y
T y
Definition: Vector4.h:89
Eule::Vector4::left
static const Vector4< double > left
Definition: Vector4.h:97
Eule
Definition: Collider.h:4
Eule::Vector4::right
static const Vector4< double > right
Definition: Vector4.h:96
Eule::Vector4
Representation of a 4d vector.
Definition: Vector2.h:8