GameKit  0.0.1a
C++ gamedev tools
Vector3.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Vector3.hpp
5  *
6  * Description:
7  *
8  * Created: 29/06/2018 06:57:12
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_VECTOR3_HPP_
15 #define GK_VECTOR3_HPP_
16 
17 #include <stdexcept>
18 #include <utility>
19 
20 #include "gk/core/IntTypes.hpp"
21 #include "gk/core/Vector2.hpp"
22 
23 namespace gk {
24 
25 template<typename T>
26 class Vector3 {
27  public:
28  Vector3() = default;
29  Vector3(T _x, T _y, T _z) : x(_x), y(_y), z(_z) {}
30 
31  template<typename U>
32  Vector3(const Vector2<U> &vector2, U _z) : x(vector2.x), y(vector2.y), z(_z) {}
33 
34  template<typename U>
35  Vector3(const Vector3<U> &vector3) : x(vector3.x), y(vector3.y), z(vector3.z) {}
36 
37  Vector3 operator+(const Vector3<T> &vector3) const { return Vector3{x + vector3.x, y + vector3.y, z + vector3.z}; }
38  Vector3 operator-(const Vector3<T> &vector3) const { return Vector3{x - vector3.x, y - vector3.y, z - vector3.z}; }
39  Vector3 operator-() const { return Vector3{-x, -y, -z}; }
40  Vector3 operator*(T n) const { return Vector3{x * n, y * n, z * n}; }
41 
42  Vector3 operator/(T n) const {
43  if(n != 0) {
44  return Vector3{x / n, y / n, z / n};
45  } else {
46  throw std::overflow_error("Divide by zero exception");
47  }
48  }
49 
50  Vector3 &operator=(T n) { x = n; y = n; z = n; return *this; }
51  Vector3 &operator+=(const Vector3 &vector3) { *this = operator+(vector3); return *this; }
52  Vector3 &operator-=(const Vector3 &vector3) { *this = operator-(vector3); return *this; }
53  Vector3 &operator*=(T n) { *this = operator*(n); return *this; }
54  Vector3 &operator/=(T n) { *this = operator/(n); return *this; }
55 
56  bool operator==(const Vector3<T> &vector3) const { return x == vector3.x && y == vector3.y && z == vector3.z; }
57  bool operator!=(const Vector3<T> &vector3) const { return !operator==(vector3); }
58 
59  // Needed if Vector3 is used as a key in a std::map
60  bool operator<(const Vector3<T> &vector3) const { return x < vector3.x && y <= vector3.y && z <= vector3.z; }
61  bool operator>(const Vector3<T> &vector3) const { return x > vector3.x && y >= vector3.y && z >= vector3.z; }
62 
63  Vector2<T> xy() const { return Vector2<T>(x, y); }
64 
65  T x;
66  T y;
67  T z;
68 };
69 
70 template<typename T>
71 Vector3<T> operator*(T n, Vector3<T> &vector3) { return vector3.operator*(n); }
72 
77 
78 } // namespace gk
79 
80 namespace std {
81  template<typename T>
82  struct hash<gk::Vector3<T>> {
83  size_t operator()(const gk::Vector3<T> &vector3) const {
84  std::hash<T> hash;
85  auto h1 = hash(vector3.x);
86  auto h2 = hash(vector3.y);
87  auto h3 = hash(vector3.z);
88 
89  return std::hash<T>{}(h1 ^ (h2 << h3) ^ h3);
90  }
91  };
92 }
93 
94 #endif // GK_VECTOR3_HPP_
Vector3 & operator/=(T n)
Definition: Vector3.hpp:54
Vector3 operator+(const Vector3< T > &vector3) const
Definition: Vector3.hpp:37
Vector2< T > xy() const
Definition: Vector3.hpp:63
Definition: Vector3.hpp:80
bool operator>(const Vector3< T > &vector3) const
Definition: Vector3.hpp:61
Vector3 & operator=(T n)
Definition: Vector3.hpp:50
bool operator==(const Vector3< T > &vector3) const
Definition: Vector3.hpp:56
Vector3(T _x, T _y, T _z)
Definition: Vector3.hpp:29
size_t operator()(const gk::Vector3< T > &vector3) const
Definition: Vector3.hpp:83
Vector3 operator-() const
Definition: Vector3.hpp:39
Vector3(const Vector2< U > &vector2, U _z)
Definition: Vector3.hpp:32
Vector3 operator/(T n) const
Definition: Vector3.hpp:42
bool operator!=(const Vector3< T > &vector3) const
Definition: Vector3.hpp:57
Vector3 & operator-=(const Vector3 &vector3)
Definition: Vector3.hpp:52
Vector3 & operator+=(const Vector3 &vector3)
Definition: Vector3.hpp:51
Vector3()=default
Vector3 & operator*=(T n)
Definition: Vector3.hpp:53
Vector3(const Vector3< U > &vector3)
Definition: Vector3.hpp:35
Vector3 operator*(T n) const
Definition: Vector3.hpp:40
Vector3 operator-(const Vector3< T > &vector3) const
Definition: Vector3.hpp:38