Leonetienne/Eule
Homemade math library, mainly targetted towards computer graphics
Vector3.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 Vector4;
12 
16  template <typename T>
17  class Vector3
18  {
19  public:
20  Vector3() : x{ 0 }, y{ 0 }, z{ 0 } {}
21  Vector3(T _x, T _y, T _z) : x{ _x }, y{ _y }, z{ _z } {}
22  Vector3(const Vector3<T>& other) = default;
23  Vector3(Vector3<T>&& other) noexcept = default;
24 
26  double DotProduct(const Vector3<T>& other) const;
27 
29  Vector3<double> CrossProduct(const Vector3<T>& other) const;
30 
32  double SqrMagnitude() const;
33 
35  double Magnitude() const;
36 
38  [[nodiscard]] Vector3<double> Normalize() const;
39 
41  void NormalizeSelf();
42 
44  [[nodiscard]] Vector3<T> VectorScale(const Vector3<T>& scalar) const;
45 
47  void LerpSelf(const Vector3<T>& other, double t);
48 
50  [[nodiscard]] Vector3<double> Lerp(const Vector3<T>& other, double t) const;
51 
53  [[nodiscard]] bool Similar(const Vector3<T>& other, double epsilon = 0.00001) const;
54 
56  [[nodiscard]] Vector3<int> ToInt() const;
57 
59  [[nodiscard]] Vector3<double> ToDouble() const;
60 
61  T& operator[](std::size_t idx);
62  const T& operator[](std::size_t idx) const;
63 
64  Vector3<T> operator+(const Vector3<T>& other) const;
65  void operator+=(const Vector3<T>& other);
66  Vector3<T> operator-(const Vector3<T>& other) const;
67  void operator-=(const Vector3<T>& other);
68  Vector3<T> operator*(const T scale) const;
69  void operator*=(const T scale);
70  Vector3<T> operator/(const T scale) const;
71  void operator/=(const T scale);
72  Vector3<T> operator*(const Matrix4x4& mat) const;
73  void operator*=(const Matrix4x4& mat);
74  Vector3<T> operator-() const;
75 
76  operator Vector2<T>() const;
77  operator Vector4<T>() const;
78 
79  void operator=(const Vector3<T>& other);
80  void operator=(Vector3<T>&& other) noexcept;
81 
82  bool operator==(const Vector3<T>& other) const;
83  bool operator!=(const Vector3<T>& other) const;
84 
85  friend std::ostream& operator << (std::ostream& os, const Vector3<T>& v)
86  {
87  return os << "[x: " << v.x << " y: " << v.y << " z: " << v.z << "]";
88  }
89  friend std::wostream& operator << (std::wostream& os, const Vector3<T>& v)
90  {
91  return os << L"[x: " << v.x << L" y: " << v.y << L" z: " << v.z << L"]";
92  }
93 
94  T x;
95  T y;
96  T z;
97 
98  // Some handy predefines
99  static const Vector3<double> up;
100  static const Vector3<double> down;
101  static const Vector3<double> right;
102  static const Vector3<double> left;
103  static const Vector3<double> forward;
104  static const Vector3<double> backward;
105  static const Vector3<double> one;
106  static const Vector3<double> zero;
107  };
108 
110  typedef Vector3<double> Vector3d;
111 }
Eule::Vector3::operator+
Vector3< T > operator+(const Vector3< T > &other) const
Definition: Vector3.cpp:372
Eule::Vector3::right
static const Vector3< double > right
Definition: Vector3.h:101
Eule::Vector3::operator<<
friend std::ostream & operator<<(std::ostream &os, const Vector3< T > &v)
Definition: Vector3.h:85
Eule::Vector3::LerpSelf
void LerpSelf(const Vector3< T > &other, double t)
Will lerp itself towards other by t.
Definition: Vector3.cpp:311
Eule::Vector3::CrossProduct
Vector3< double > CrossProduct(const Vector3< T > &other) const
Will compute the cross product to another Vector3.
Definition: Vector3.cpp:68
Eule::Vector3::left
static const Vector3< double > left
Definition: Vector3.h:102
Eule::Vector3::Similar
bool Similar(const Vector3< T > &other, double epsilon=0.00001) const
Will compare if two vectors are similar to a certain epsilon value.
Definition: Vector3.cpp:213
Eule::Vector3
Representation of a 3d vector.
Definition: Matrix4x4.h:9
Eule::Vector3::Vector3
Vector3(T _x, T _y, T _z)
Definition: Vector3.h:21
Eule::Vector3::operator-
Vector3< T > operator-() const
Definition: Vector3.cpp:806
Eule::Vector3::down
static const Vector3< double > down
Definition: Vector3.h:100
Eule::Vector3::Lerp
Vector3< double > Lerp(const Vector3< T > &other, double t) const
Will return a lerp result between this and another vector.
Definition: Vector3.cpp:330
Eule::Vector3::forward
static const Vector3< double > forward
Definition: Vector3.h:103
Eule::Vector3::one
static const Vector3< double > one
Definition: Vector3.h:105
Eule::Vector3::operator=
void operator=(const Vector3< T > &other)
Conversion method.
Definition: Vector3.cpp:816
Eule::Vector3::ToDouble
Vector3< double > ToDouble() const
Will convert this vector to a Vector3d.
Definition: Vector3.cpp:229
Eule::Vector3::ToInt
Vector3< int > ToInt() const
Will convert this vector to a Vector3i.
Definition: Vector3.cpp:223
Eule::Vector3::operator==
bool operator==(const Vector3< T > &other) const
Definition: Vector3.cpp:855
Eule::Vector3::operator*=
void operator*=(const T scale)
Definition: Vector3.cpp:583
Eule::Vector3::Vector3
Vector3()
Definition: Vector3.h:20
Eule::Vector3d
Vector3< double > Vector3d
Definition: Matrix4x4.h:9
Matrix4x4.h
Eule::Vector3i
Vector3< int > Vector3i
Definition: Vector3.h:109
Eule::Vector3::backward
static const Vector3< double > backward
Definition: Vector3.h:104
Eule::Vector3::Normalize
Vector3< double > Normalize() const
Will return the normalization of this vector.
Definition: Vector3.cpp:147
Eule::Vector3::z
T z
Definition: Vector3.h:96
Eule::Vector3::up
static const Vector3< double > up
Definition: Vector3.h:99
Eule::Vector3::operator[]
T & operator[](std::size_t idx)
Definition: Vector3.cpp:235
Eule::Vector3::x
T x
Definition: Vector3.h:94
Eule::Vector3::DotProduct
double DotProduct(const Vector3< T > &other) const
Will compute the dot product to another Vector3.
Definition: Vector3.cpp:48
Eule::Vector3::operator/
Vector3< T > operator/(const T scale) const
Definition: Vector3.cpp:626
Eule::Vector3::operator-=
void operator-=(const Vector3< T > &other)
Definition: Vector3.cpp:498
Eule::Vector3::SqrMagnitude
double SqrMagnitude() const
Will compute the square magnitude.
Definition: Vector3.cpp:88
Eule::Vector3::operator/=
void operator/=(const T scale)
Definition: Vector3.cpp:667
Eule::Vector3::operator!=
bool operator!=(const Vector3< T > &other) const
Definition: Vector3.cpp:864
Eule::Vector3::operator+=
void operator+=(const Vector3< T > &other)
Definition: Vector3.cpp:414
Eule::Vector3::Magnitude
double Magnitude() const
Will compute the magnitude.
Definition: Vector3.cpp:95
Eule::Vector3::NormalizeSelf
void NormalizeSelf()
Will normalize this vector.
Definition: Vector3.cpp:200
Eule::Vector3::operator*
Vector3< T > operator*(const T scale) const
Definition: Vector3.cpp:541
Eule::Vector3::zero
static const Vector3< double > zero
Definition: Vector3.h:106
Eule
Definition: Collider.h:4
Eule::Vector3::y
T y
Definition: Vector3.h:95
Eule::Vector3::VectorScale
Vector3< T > VectorScale(const Vector3< T > &scalar) const
Will scale self.n by scalar.n.
Definition: Vector3.cpp:135