Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Vector3.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_VECTOR3_HPP
8 #define NAZARA_VECTOR3_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 Vector4;
19 
20  template<typename T>
21  class Vector3
22  {
23  public:
24  Vector3() = default;
25  Vector3(T X, T Y, T Z);
26  Vector3(T X, const Vector2<T>& vec);
27  explicit Vector3(T scale);
28  Vector3(const T vec[3]);
29  Vector3(const Vector2<T>& vec, T Z = 0.0);
30  template<typename U> explicit Vector3(const Vector3<U>& vec);
31  Vector3(const Vector3& vec) = default;
32  explicit Vector3(const Vector4<T>& vec);
33  ~Vector3() = default;
34 
35  T AbsDotProduct(const Vector3& vec) const;
36  T AngleBetween(const Vector3& vec) const;
37 
38  Vector3 CrossProduct(const Vector3& vec) const;
39 
40  T Distance(const Vector3& vec) const;
41  float Distancef(const Vector3& vec) const;
42  T DotProduct(const Vector3& vec) const;
43 
44  T GetLength() const;
45  float GetLengthf() const;
46  Vector3 GetNormal(T* length = nullptr) const;
47  T GetSquaredLength() const;
48 
49  Vector3& MakeBackward();
50  Vector3& MakeDown();
51  Vector3& MakeForward();
52  Vector3& MakeLeft();
53  Vector3& MakeRight();
54  Vector3& MakeUnit();
55  Vector3& MakeUnitX();
56  Vector3& MakeUnitY();
57  Vector3& MakeUnitZ();
58  Vector3& MakeUp();
59  Vector3& MakeZero();
60 
61  Vector3& Maximize(const Vector3& vec);
62  Vector3& Minimize(const Vector3& vec);
63 
64  Vector3& Normalize(T* length = nullptr);
65 
66  Vector3& Set(T X, T Y, T Z);
67  Vector3& Set(T X, const Vector2<T>& vec);
68  Vector3& Set(T scale);
69  Vector3& Set(const T vec[3]);
70  Vector3& Set(const Vector2<T>& vec, T Z = 0.0);
71  Vector3& Set(const Vector3<T>& vec);
72  template<typename U> Vector3& Set(const Vector3<U>& vec);
73  Vector3& Set(const Vector4<T>& vec);
74 
75  T SquaredDistance(const Vector3& vec) const;
76 
77  String ToString() const;
78 
79  operator T* ();
80  operator const T* () const;
81 
82  const Vector3& operator+() const;
83  Vector3 operator-() const;
84 
85  Vector3 operator+(const Vector3& vec) const;
86  Vector3 operator-(const Vector3& vec) const;
87  Vector3 operator*(const Vector3& vec) const;
88  Vector3 operator*(T scale) const;
89  Vector3 operator/(const Vector3& vec) const;
90  Vector3 operator/(T scale) const;
91  Vector3& operator=(const Vector3& vec) = default;
92 
93  Vector3& operator+=(const Vector3& vec);
94  Vector3& operator-=(const Vector3& vec);
95  Vector3& operator*=(const Vector3& vec);
96  Vector3& operator*=(T scale);
97  Vector3& operator/=(const Vector3& vec);
98  Vector3& operator/=(T scale);
99 
100  bool operator==(const Vector3& vec) const;
101  bool operator!=(const Vector3& vec) const;
102  bool operator<(const Vector3& vec) const;
103  bool operator<=(const Vector3& vec) const;
104  bool operator>(const Vector3& vec) const;
105  bool operator>=(const Vector3& vec) const;
106 
107  static Vector3 Backward();
108  static Vector3 CrossProduct(const Vector3& vec1, const Vector3& vec2);
109  static T DotProduct(const Vector3& vec1, const Vector3& vec2);
110  static T Distance(const Vector3& vec1, const Vector3& vec2);
111  static float Distancef(const Vector3& vec1, const Vector3& vec2);
112  static Vector3 Down();
113  static Vector3 Forward();
114  static Vector3 Left();
115  static Vector3 Lerp(const Vector3& from, const Vector3& to, T interpolation);
116  static Vector3 Normalize(const Vector3& vec);
117  static Vector3 Right();
118  static T SquaredDistance(const Vector3& vec1, const Vector3& vec2);
119  static Vector3 Unit();
120  static Vector3 UnitX();
121  static Vector3 UnitY();
122  static Vector3 UnitZ();
123  static Vector3 Up();
124  static Vector3 Zero();
125 
126  T x, y, z;
127  };
128 
129  typedef Vector3<double> Vector3d;
130  typedef Vector3<float> Vector3f;
131  typedef Vector3<int> Vector3i;
132  typedef Vector3<unsigned int> Vector3ui;
133  typedef Vector3<Int32> Vector3i32;
134  typedef Vector3<UInt32> Vector3ui32;
135 
136  template<typename T> bool Serialize(SerializationContext& context, const Vector3<T>& vector);
137  template<typename T> bool Unserialize(SerializationContext& context, Vector3<T>* vector);
138 }
139 
140 template<typename T> std::ostream& operator<<(std::ostream& out, const Nz::Vector3<T>& vec);
141 
142 template<typename T> Nz::Vector3<T> operator*(T scale, const Nz::Vector3<T>& vec);
143 template<typename T> Nz::Vector3<T> operator/(T scale, const Nz::Vector3<T>& vec);
144 
145 namespace std
146 {
147  template<class T> struct hash<Nz::Vector3<T>>;
148 }
149 
150 #include <Nazara/Math/Vector3.inl>
151 
152 #endif // NAZARA_VECTOR3_HPP
static Vector3 Right()
Shorthand for the vector (1, 0, 0)
Definition: Vector3.inl:1139
Vector3 GetNormal(T *length=nullptr) const
Gets a copy normalized of the vector.
Definition: Vector3.inl:240
Vector3 & MakeUnitY()
Makes the vector (0, 1, 0)
Definition: Vector3.inl:351
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Vector3 & Minimize(const Vector3 &vec)
Sets this vector&#39;s components to the minimum of its own and other components.
Definition: Vector3.inl:424
float Distancef(const Vector3 &vec) const
Calculates the distance between two vectors.
Definition: Vector3.inl:188
bool operator>(const Vector3 &vec) const
Compares the vector to other one.
Definition: Vector3.inl:952
Vector3 CrossProduct(const Vector3 &vec) const
Calculates the cross (scalar) product with two vectors.
Definition: Vector3.inl:162
Vector3 & operator/=(const Vector3 &vec)
Multiplies the components of other vector to this vector.
Definition: Vector3.inl:835
String ToString() const
Gives a string representation.
Definition: Vector3.inl:612
Vector3 & operator*=(const Vector3 &vec)
Multiplies the components of other vector to this vector.
Definition: Vector3.inl:800
bool Unserialize(SerializationContext &context, bool *value)
Unserializes a boolean.
Definition: Algorithm.inl:279
Vector3 & MakeRight()
Makes the vector (1, 0, 0)
Definition: Vector3.inl:315
T SquaredDistance(const Vector3 &vec) const
Calculates the squared distance between two vectors.
Definition: Vector3.inl:602
Vector3 & MakeBackward()
Makes the vector (0, 0, 1)
Definition: Vector3.inl:267
T AngleBetween(const Vector3 &vec) const
Calculates the angle between two vectors in orthonormal basis.
Definition: Vector3.inl:134
Vector3 & MakeZero()
Makes the vector (0, 0, 0)
Definition: Vector3.inl:387
Vector3 & MakeDown()
Makes the vector (0, -1, 0)
Definition: Vector3.inl:279
Vector3 & MakeLeft()
Makes the vector (-1, 0, 0)
Definition: Vector3.inl:303
STL namespace.
static Vector3 Up()
Shorthand for the vector (0, 1, 0)
Definition: Vector3.inl:1229
bool operator>=(const Vector3 &vec) const
Compares the vector to other one.
Definition: Vector3.inl:964
static Vector3 UnitY()
Shorthand for the vector (0, 1, 0)
Definition: Vector3.inl:1199
bool operator<=(const Vector3 &vec) const
Compares the vector to other one.
Definition: Vector3.inl:932
Vector3 operator*(const Vector3 &vec) const
Multiplies the components of the vector with other vector.
Definition: Vector3.inl:694
T GetLength() const
Calculates the length (magnitude) of the vector.
Definition: Vector3.inl:214
static Vector3 Backward()
Shorthand for the vector (0, 0, 1)
Definition: Vector3.inl:1006
static Vector3 Forward()
Shorthand for the vector (0, 0, -1)
Definition: Vector3.inl:1070
bool operator<(const Vector3 &vec) const
Compares the vector to other one.
Definition: Vector3.inl:912
static Vector3 Unit()
Shorthand for the vector (1, 1, 1)
Definition: Vector3.inl:1169
Vector3 & Normalize(T *length=nullptr)
Normalizes the current vector.
Definition: Vector3.inl:449
bool operator==(const Vector3 &vec) const
Compares the vector to other one.
Definition: Vector3.inl:886
Vector3 & Set(T X, T Y, T Z)
Sets the components of the vector.
Definition: Vector3.inl:475
Vector3 & MakeUnitX()
Makes the vector (1, 0, 0)
Definition: Vector3.inl:339
Vector3 & MakeUp()
Makes the vector (0, 1, 0)
Definition: Vector3.inl:375
T AbsDotProduct(const Vector3 &vec) const
Calculates the absolute dot (scalar) product with two vectors.
Definition: Vector3.inl:116
Vector3 & MakeUnitZ()
Makes the vector (0, 0, 1)
Definition: Vector3.inl:363
Vector3 & Maximize(const Vector3 &vec)
Sets this vector&#39;s components to the maximum of its own and other components.
Definition: Vector3.inl:401
float GetLengthf() const
Calculates the length (magnitude) of the vector.
Definition: Vector3.inl:224
static Vector3 Left()
Shorthand for the vector (-1, 0, 0)
Definition: Vector3.inl:1085
Vector3 & MakeUnit()
Makes the vector (1, 1, 1)
Definition: Vector3.inl:327
T Distance(const Vector3 &vec) const
Calculates the distance between two vectors.
Definition: Vector3.inl:176
bool Serialize(SerializationContext &context, bool value)
Serializes a boolean.
Definition: Algorithm.inl:214
T DotProduct(const Vector3 &vec) const
Calculates the dot (scalar) product with two vectors.
Definition: Vector3.inl:202
static Vector3 UnitZ()
Shorthand for the vector (0, 0, 1)
Definition: Vector3.inl:1214
T GetSquaredLength() const
Calculates the squared length (magnitude) of the vector.
Definition: Vector3.inl:255
Vector3 operator-() const
Negates the components of the vector.
Definition: Vector3.inl:658
static Vector3 Zero()
Shorthand for the vector (0, 0, 0)
Definition: Vector3.inl:1244
static Vector3 Lerp(const Vector3 &from, const Vector3 &to, T interpolation)
Interpolates the vector to other one with a factor of interpolation.
Definition: Vector3.inl:1106
Vector3 operator/(const Vector3 &vec) const
Divides the components of the vector with other vector.
Definition: Vector3.inl:721
static Vector3 Down()
Shorthand for the vector (0, -1, 0)
Definition: Vector3.inl:1055
bool operator!=(const Vector3 &vec) const
Compares the vector to other one.
Definition: Vector3.inl:900
Math class that represents an element of the three dimensional vector space.
Definition: Matrix4.hpp:22
Vector3 & operator-=(const Vector3 &vec)
Substracts the components of other vector to this vector.
Definition: Vector3.inl:784
static Vector3 UnitX()
Shorthand for the vector (1, 0, 0)
Definition: Vector3.inl:1184
Vector3 & operator+=(const Vector3 &vec)
Adds the components of other vector to this vector.
Definition: Vector3.inl:768
Vector3 & MakeForward()
Makes the vector (0, 0, -1)
Definition: Vector3.inl:291
const Vector3 & operator+() const
Helps to represent the sign of the vector.
Definition: Vector3.inl:648