Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Vector4.hpp
1 // Copyright (C) 2017 Rémi Bèges - Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Mathematics module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_VECTOR4_HPP
8 #define NAZARA_VECTOR4_HPP
9 
10 #include <Nazara/Core/String.hpp>
11 #include <functional>
12 
13 namespace Nz
14 {
15  struct SerializationContext;
16 
17  template<typename T> class Vector2;
18  template<typename T> class Vector3;
19 
20  template<typename T>
21  class Vector4
22  {
23  public:
24  Vector4() = default;
25  Vector4(T X, T Y, T Z, T W = 1.0);
26  Vector4(T X, T Y, const Vector2<T>& vec);
27  Vector4(T X, const Vector2<T>& vec, T W);
28  Vector4(T X, const Vector3<T>& vec);
29  explicit Vector4(T scale);
30  Vector4(const T vec[4]);
31  Vector4(const Vector2<T>& vec, T Z = 0.0, T W = 1.0);
32  Vector4(const Vector3<T>& vec, T W = 1.0);
33  template<typename U> explicit Vector4(const Vector4<U>& vec);
34  Vector4(const Vector4& vec) = default;
35  ~Vector4() = default;
36 
37  T AbsDotProduct(const Vector4& vec) const;
38 
39  T DotProduct(const Vector4& vec) const;
40 
41  Vector4 GetNormal(T* length = nullptr) const;
42 
43  Vector4& MakeUnitX();
44  Vector4& MakeUnitY();
45  Vector4& MakeUnitZ();
46  Vector4& MakeZero();
47 
48  Vector4& Maximize(const Vector4& vec);
49  Vector4& Minimize(const Vector4& vec);
50 
51  Vector4& Normalize(T* length = nullptr);
52 
53  Vector4& Set(T X, T Y, T Z, T W = 1.0);
54  Vector4& Set(T X, T Y, const Vector2<T>& vec);
55  Vector4& Set(T X, const Vector2<T>& vec, T W);
56  Vector4& Set(T X, const Vector3<T>& vec);
57  Vector4& Set(T scale);
58  Vector4& Set(const T vec[4]);
59  Vector4& Set(const Vector2<T>& vec, T Z = 0.0, T W = 1.0);
60  Vector4& Set(const Vector3<T>& vec, T W = 1.0);
61  Vector4& Set(const Vector4<T>& vec);
62  template<typename U> Vector4& Set(const Vector4<U>& vec);
63 
64  String ToString() const;
65 
66  operator T* ();
67  operator const T* () const;
68 
69  const Vector4& operator+() const;
70  Vector4 operator-() const;
71 
72  Vector4 operator+(const Vector4& vec) const;
73  Vector4 operator-(const Vector4& vec) const;
74  Vector4 operator*(const Vector4& vec) const;
75  Vector4 operator*(T scale) const;
76  Vector4 operator/(const Vector4& vec) const;
77  Vector4 operator/(T scale) const;
78  Vector4& operator=(const Vector4& other) = default;
79 
80  Vector4& operator+=(const Vector4& vec);
81  Vector4& operator-=(const Vector4& vec);
82  Vector4& operator*=(const Vector4& vec);
83  Vector4& operator*=(T scale);
84  Vector4& operator/=(const Vector4& vec);
85  Vector4& operator/=(T scale);
86 
87  bool operator==(const Vector4& vec) const;
88  bool operator!=(const Vector4& vec) const;
89  bool operator<(const Vector4& vec) const;
90  bool operator<=(const Vector4& vec) const;
91  bool operator>(const Vector4& vec) const;
92  bool operator>=(const Vector4& vec) const;
93 
94  static T DotProduct(const Vector4& vec1, const Vector4& vec2);
95  static Vector4 Lerp(const Vector4& from, const Vector4& to, T interpolation);
96  static Vector4 Normalize(const Vector4& vec);
97  static Vector4 UnitX();
98  static Vector4 UnitY();
99  static Vector4 UnitZ();
100  static Vector4 Zero();
101 
102  T x, y, z, w;
103  };
104 
105  typedef Vector4<double> Vector4d;
106  typedef Vector4<float> Vector4f;
107  typedef Vector4<int> Vector4i;
108  typedef Vector4<unsigned int> Vector4ui;
109  typedef Vector4<Int32> Vector4i32;
110  typedef Vector4<UInt32> Vector4ui32;
111 
112  template<typename T> bool Serialize(SerializationContext& context, const Vector4<T>& vector);
113  template<typename T> bool Unserialize(SerializationContext& context, Vector4<T>* vector);
114 }
115 
116 template<typename T> std::ostream& operator<<(std::ostream& out, const Nz::Vector4<T>& vec);
117 
118 template<typename T> Nz::Vector4<T> operator*(T scale, const Nz::Vector4<T>& vec);
119 template<typename T> Nz::Vector4<T> operator/(T scale, const Nz::Vector4<T>& vec);
120 
121 namespace std
122 {
123  template<class T> struct hash<Nz::Vector4<T>>;
124 }
125 
126 #include <Nazara/Math/Vector4.inl>
127 
128 #endif // NAZARA_VECTOR4_HPP
Vector4 GetNormal(T *length=nullptr) const
Gets a copy normalized of the vector.
Definition: Vector4.inl:185
static Vector4 UnitX()
Shorthand for the vector (1, 0, 0, 1)
Definition: Vector4.inl:962
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Vector4 & operator/=(const Vector4 &vec)
Multiplies the components of other vector to this vector.
Definition: Vector4.inl:756
bool Unserialize(SerializationContext &context, bool *value)
Unserializes a boolean.
Definition: Algorithm.inl:279
static Vector4 UnitY()
Shorthand for the vector (0, 1, 0, 1)
Definition: Vector4.inl:978
STL namespace.
String ToString() const
Gives a string representation.
Definition: Vector4.inl:514
const Vector4 & operator+() const
Helps to represent the sign of the vector.
Definition: Vector4.inl:553
static Vector4 Zero()
Shorthand for the vector (0, 0, 0, 1)
Definition: Vector4.inl:1010
bool operator!=(const Vector4 &vec) const
Compares the vector to other one.
Definition: Vector4.inl:831
bool operator>(const Vector4 &vec) const
Compares the vector to other one.
Definition: Vector4.inl:896
Vector4 & MakeUnitY()
Makes the vector (0, 1, 0, 1)
Definition: Vector4.inl:214
Vector4 & Normalize(T *length=nullptr)
Gives the normalized vector.
Definition: Vector4.inl:309
Vector4 operator/(const Vector4 &vec) const
Divides the components of the vector with other vector.
Definition: Vector4.inl:632
bool operator==(const Vector4 &vec) const
Compares the vector to other one.
Definition: Vector4.inl:815
Vector4 & MakeUnitX()
Makes the vector (1, 0, 0, 1)
Definition: Vector4.inl:201
T DotProduct(const Vector4 &vec) const
Calculates the dot (scalar) product with two vectors.
Definition: Vector4.inl:170
bool operator>=(const Vector4 &vec) const
Compares the vector to other one.
Definition: Vector4.inl:909
bool operator<(const Vector4 &vec) const
Compares the vector to other one.
Definition: Vector4.inl:844
bool Serialize(SerializationContext &context, bool value)
Serializes a boolean.
Definition: Algorithm.inl:214
Math class that represents an element of the three dimensional vector space with the notion of projec...
Definition: Matrix4.hpp:23
Vector4 & MakeUnitZ()
Makes the vector (0, 0, 1, 1)
Definition: Vector4.inl:227
Vector4 operator*(const Vector4 &vec) const
Multiplies the components of the vector with other vector.
Definition: Vector4.inl:603
Vector4 & Minimize(const Vector4 &vec)
Sets this vector&#39;s components to the minimum of its own and other components.
Definition: Vector4.inl:282
static Vector4 Lerp(const Vector4 &from, const Vector4 &to, T interpolation)
Interpolates the vector to other one with a factor of interpolation.
Definition: Vector4.inl:928
bool operator<=(const Vector4 &vec) const
Compares the vector to other one.
Definition: Vector4.inl:870
T AbsDotProduct(const Vector4 &vec) const
Calculates the absolute dot (scalar) product with two vectors.
Definition: Vector4.inl:155
Vector4 & Set(T X, T Y, T Z, T W=1.0)
Sets the components of the vector.
Definition: Vector4.inl:335
Vector4 & operator*=(const Vector4 &vec)
Multiplies the components of other vector to this vector.
Definition: Vector4.inl:717
Vector4 operator-() const
Negates the components of the vector.
Definition: Vector4.inl:564
Vector4 & Maximize(const Vector4 &vec)
Sets this vector&#39;s components to the maximum of its own and other components.
Definition: Vector4.inl:255
Vector4 & operator-=(const Vector4 &vec)
Substracts the components of other vector to this vector.
Definition: Vector4.inl:699
Vector4 & operator+=(const Vector4 &vec)
Adds the components of other vector to this vector.
Definition: Vector4.inl:681
static Vector4 UnitZ()
Shorthand for the vector (0, 0, 1, 1)
Definition: Vector4.inl:994
Vector4 & MakeZero()
Makes the vector (0, 0, 0, 1)
Definition: Vector4.inl:240