Jet  v1.3.3
vector4.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_VECTOR4_H_
8 #define INCLUDE_JET_VECTOR4_H_
9 
10 #include <jet/vector3.h>
11 
12 #include <algorithm> // just make cpplint happy..
13 #include <limits>
14 
15 namespace jet {
16 
24 template <typename T>
25 class Vector<T, 4> final {
26  public:
27  static_assert(std::is_floating_point<T>::value,
28  "Vector only can be instantiated with floating point types");
29 
31  T x;
32 
34  T y;
35 
37  T z;
38 
40  T w;
41 
42  // MARK: Constructors
43 
45  constexpr Vector() : x(0), y(0), z(0), w(0) {}
46 
48  constexpr Vector(T x_, T y_, T z_, T w_) : x(x_), y(y_), z(z_), w(w_) {}
49 
51  constexpr Vector(const Vector3<T>& v, T w_)
52  : x(v.x), y(v.y), z(v.z), w(w_) {}
53 
55  template <typename U>
56  Vector(const std::initializer_list<U>& lst);
57 
59  constexpr Vector(const Vector& v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
60 
61  // MARK: Basic setters
62 
64  void set(T s);
65 
67  void set(T x, T y, T z, T w);
68 
70  void set(const Vector<T, 3>& pt, T z);
71 
73  template <typename U>
74  void set(const std::initializer_list<U>& lst);
75 
77  void set(const Vector& v);
78 
80  void setZero();
81 
83  void normalize();
84 
85  // MARK: Binary operations: new instance = this (+) v
86 
88  Vector add(T v) const;
89 
91  Vector add(const Vector& v) const;
92 
94  Vector sub(T v) const;
95 
97  Vector sub(const Vector& v) const;
98 
100  Vector mul(T v) const;
101 
103  Vector mul(const Vector& v) const;
105  Vector div(T v) const;
106 
108  Vector div(const Vector& v) const;
109 
111  T dot(const Vector& v) const;
112 
113  // MARK: Binary operations: new instance = v (+) this
114 
116  Vector rsub(T v) const;
117 
119  Vector rsub(const Vector& v) const;
120 
122  Vector rdiv(T v) const;
123 
125  Vector rdiv(const Vector& v) const;
126 
127  // MARK: Augmented operations: this (+)= v
128 
130  void iadd(T v);
131 
133  void iadd(const Vector& v);
134 
136  void isub(T v);
137 
139  void isub(const Vector& v);
140 
142  void imul(T v);
143 
145  void imul(const Vector& v);
146 
148  void idiv(T v);
149 
151  void idiv(const Vector& v);
152 
153  // MARK: Basic getters
154 
156  const T& at(size_t i) const;
157 
159  T& at(size_t i);
160 
162  T sum() const;
163 
165  T avg() const;
166 
168  T min() const;
169 
171  T max() const;
172 
174  T absmin() const;
175 
177  T absmax() const;
178 
180  size_t dominantAxis() const;
181 
183  size_t subminantAxis() const;
184 
187 
189  T length() const;
190 
192  T lengthSquared() const;
193 
195  T distanceTo(const Vector& other) const;
196 
198  T distanceSquaredTo(const Vector& other) const;
199 
201  template <typename U>
203 
205  bool isEqual(const Vector& other) const;
206 
208  bool isSimilar(const Vector& other,
209  T epsilon = std::numeric_limits<T>::epsilon()) const;
210 
211  // MARK: Operators
212 
214  T& operator[](size_t i);
215 
217  const T& operator[](size_t i) const;
218 
220  template <typename U>
221  Vector& operator=(const std::initializer_list<U>& lst);
222 
224  Vector& operator=(const Vector& v);
225 
228 
231 
234 
237 
240 
243 
246 
249 
251  bool operator==(const Vector& v) const;
252 
254  bool operator!=(const Vector& v) const;
255 };
256 
258 template <typename T>
260 
262 template <typename T>
264 
266 template <typename T>
268 
270 template <typename T>
272 
274 template <typename T>
276 
278 template <typename T>
280 
282 template <typename T>
284 
286 template <typename T>
288 
290 template <typename T>
292 
294 template <typename T>
296 
298 template <typename T>
300 
302 template <typename T>
304 
306 template <typename T>
308 
310 template <typename T>
312 
314 template <typename T>
316 
318 template <typename T>
319 Vector4<T> min(const Vector4<T>& a, const Vector4<T>& b);
320 
322 template <typename T>
323 Vector4<T> max(const Vector4<T>& a, const Vector4<T>& b);
324 
326 template <typename T>
327 Vector4<T> clamp(const Vector4<T>& v, const Vector4<T>& low,
328  const Vector4<T>& high);
329 
331 template <typename T>
333 
335 template <typename T>
337 
340 
343 
344 // MARK: Extensions
345 
347 template <>
349  return Vector4F(0.f, 0.f, 0.f, 0.f);
350 }
351 
353 template <>
355  return Vector4D(0.0, 0.0, 0.0, 0.0);
356 }
357 
359 template <typename T>
360 struct ScalarType<Vector4<T>> {
361  typedef T value;
362 };
363 
365 template <typename T>
367  const Vector4<T>& v2, const Vector4<T>& v3, T f);
368 
369 } // namespace jet
370 
371 #include "detail/vector4-inl.h"
372 
373 #endif // INCLUDE_JET_VECTOR4_H_
jet::Vector< T, 4 >::isSimilar
bool isSimilar(const Vector &other, T epsilon=std::numeric_limits< T >::epsilon()) const
Returns true if other is similar to this vector.
jet::Vector< T, 4 >::normalized
Vector normalized() const
Returns normalized vector.
jet::ceil
Point2< T > ceil(const Point2< T > &a)
Returns element-wise ceiled point.
jet::Vector< T, 4 >::sub
Vector sub(const Vector &v) const
Computes this - (v.x, v.y, v.z, v.w).
jet::Vector< T, 4 >::sum
T sum() const
Returns the sum of all the components (i.e. x + y + z + w).
jet::Vector< T, 4 >::distanceSquaredTo
T distanceSquaredTo(const Vector &other) const
Returns the squared distance to the other vector.
jet::Vector< T, 4 >::Vector
constexpr Vector()
Constructs default vector (0, 0, 0, 0).
Definition: vector4.h:45
jet::Vector< T, 4 >::dot
T dot(const Vector &v) const
Computes dot product.
jet::Vector< T, 4 >::set
void set(const std::initializer_list< U > &lst)
Set x, y, z, and w components with given initializer list.
jet::Vector< T, 4 >::length
T length() const
Returns the length of the vector.
jet::Vector< T, 4 >::absmin
T absmin() const
Returns the absolute minimum value among x, y, z, and w.
jet::Vector< T, 4 >::y
T y
Y (or the second) component of the vector.
Definition: vector4.h:34
jet::Vector< T, 4 >::mul
Vector mul(const Vector &v) const
Computes this * (v.x, v.y, v.z, v.w).
jet::max
Point2< T > max(const Point2< T > &a, const Point2< T > &b)
Returns element-wise max point: (max(a.x, b.x), max(a.y, b.y)).
jet::Vector4F
Vector4< float > Vector4F
Float-type 4D vector.
Definition: vector4.h:339
jet::Vector< T, 4 >::Vector
Vector(const std::initializer_list< U > &lst)
Constructs vector with initializer list.
jet::Vector< T, 4 >::idiv
void idiv(const Vector &v)
Computes this /= (v.x, v.y, v.z, v.w).
jet::Vector< T, 4 >::add
Vector add(const Vector &v) const
Computes this + (v.x, v.y, v.z, v.w).
jet::Vector< T, 4 >::absmax
T absmax() const
Returns the absolute maximum value among x, y, z, and w.
jet::Vector
Generic statically-sized N-D vector class.
Definition: vector.h:31
jet::Vector< T, 4 >::set
void set(T s)
Set both x, y, z, and w components to s.
jet::Vector< T, 4 >::subminantAxis
size_t subminantAxis() const
Returns the index of the subminant axis.
jet::Vector< T, 4 >::castTo
Vector< U, 4 > castTo() const
Returns a vector with different value type.
jet::Vector< T, 4 >::operator+=
Vector & operator+=(const Vector &v)
Computes this += (v.x, v.y, v.z, v.w)
jet::Vector< T, 4 >
4-D vector class.
Definition: vector4.h:25
jet::Vector< T, 4 >::rsub
Vector rsub(T v) const
Computes (v, v, v, v) - this.
jet::Vector< T, 4 >::isEqual
bool isEqual(const Vector &other) const
Returns true if other is the same as this vector.
jet
Definition: advection_solver2.h:18
jet::Vector< T, 4 >::operator/=
Vector & operator/=(const Vector &v)
Computes this /= (v.x, v.y, v.z, v.w)
jet::Vector< T, 4 >::operator[]
const T & operator[](size_t i) const
Returns const reference to the i -th element of the vector.
jet::Vector< T, 4 >::isub
void isub(T v)
Computes this -= (v, v, v, v).
jet::Vector< T, 4 >::imul
void imul(const Vector &v)
Computes this *= (v.x, v.y, v.z, v.w).
jet::Vector< T, 4 >::lengthSquared
T lengthSquared() const
Returns the squared length of the vector.
jet::Vector< T, 4 >::operator-=
Vector & operator-=(T v)
Computes this -= (v, v, v, v)
jet::Vector< T, 4 >::dominantAxis
size_t dominantAxis() const
Returns the index of the dominant axis.
jet::Vector< T, 4 >::idiv
void idiv(T v)
Computes this /= (v, v, v, v).
jet::clamp
T clamp(T val, T low, T high)
Returns the clamped value.
jet::Vector< T, 4 >::operator+=
Vector & operator+=(T v)
Computes this += (v, v, v, v)
jet::Vector< T, 4 >::set
void set(T x, T y, T z, T w)
Set x, y, z, and w components with given parameters.
jet::Vector< T, 4 >::Vector
constexpr Vector(const Vector &v)
Copy constructor.
Definition: vector4.h:59
jet::Vector< T, 4 >::operator!=
bool operator!=(const Vector &v) const
Returns true if other is the not same as this vector.
jet::ScalarType< Vector4< T > >::value
T value
Definition: vector4.h:361
jet::Vector< T, 4 >::w
T w
W (or the fourth) component of the vector.
Definition: vector4.h:40
jet::operator+
Matrix2x2< T > operator+(const Matrix2x2< T > &a, const Matrix2x2< T > &b)
Returns a + b (element-size).
jet::Vector< T, 4 >::rdiv
Vector rdiv(const Vector &v) const
Computes (v.x, v.y, v.z, v.w) / this.
jet::floor
Point2< T > floor(const Point2< T > &a)
Returns element-wise floored point.
jet::monotonicCatmullRom
T monotonicCatmullRom(const T &f0, const T &f1, const T &f2, const T &f3, T t)
Computes monotonic Catmull-Rom interpolation.
jet::Vector< T, 4 >::operator[]
T & operator[](size_t i)
Returns reference to the i -th element of the vector.
jet::Vector< T, 4 >::add
Vector add(T v) const
Computes this + (v, v, v, v).
jet::zero< Vector4D >
constexpr Vector4D zero< Vector4D >()
Returns double-type zero vector.
Definition: vector4.h:354
jet::Vector< T, 4 >::div
Vector div(const Vector &v) const
Computes this / (v.x, v.y, v.z, v.w).
jet::Vector4D
Vector4< double > Vector4D
Double-type 4D vector.
Definition: vector4.h:342
jet::Vector< T, 4 >::sub
Vector sub(T v) const
Computes this - (v, v, v, v).
jet::Vector< T, 4 >::avg
T avg() const
Returns the average of all the components.
vector3.h
jet::Vector< T, 4 >::rdiv
Vector rdiv(T v) const
Computes (v, v, v, v) / this.
jet::Vector< T, 4 >::set
void set(const Vector &v)
Set x, y, z, and w with other vector v.
jet::Vector< T, 4 >::at
T & at(size_t i)
Returns reference to the i -th element of the vector.
jet::Vector< T, 4 >::x
T x
X (or the first) component of the vector.
Definition: vector4.h:28
jet::Vector< T, 4 >::setZero
void setZero()
Set all x, y, z, and w to zero.
jet::zero< Vector4F >
constexpr Vector4F zero< Vector4F >()
Returns float-type zero vector.
Definition: vector4.h:348
jet::Vector< T, 4 >::Vector
constexpr Vector(T x_, T y_, T z_, T w_)
Constructs vector with given parameters x_, y_, z_, and w_.
Definition: vector4.h:48
jet::Vector< T, 4 >::at
const T & at(size_t i) const
Returns const reference to the i -th element of the vector.
jet::Vector< T, 4 >::max
T max() const
Returns the maximum value among x, y, z, and w.
jet::Vector< T, 4 >::z
T z
Z (or the third) component of the vector.
Definition: vector4.h:37
jet::Vector< T, 4 >::operator=
Vector & operator=(const Vector &v)
Set x and y with other vector v.
jet::Vector< T, 4 >::iadd
void iadd(const Vector &v)
Computes this += (v.x, v.y, v.z, v.w).
jet::Vector< T, 4 >::Vector
constexpr Vector(const Vector3< T > &v, T w_)
Constructs vector with a 3-D vector (x, y, and z) and a scalar (w).
Definition: vector4.h:51
jet::Vector< T, 3 >
3-D vector class.
Definition: vector3.h:25
jet::Vector< T, 4 >::min
T min() const
Returns the minimum value among x, y, z, and w.
jet::Vector< T, 4 >::isub
void isub(const Vector &v)
Computes this -= (v.x, v.y, v.z, v.w).
jet::Vector< T, 4 >::set
void set(const Vector< T, 3 > &pt, T z)
Set x, y, z, and w components with pt.x, pt.y, z, and w.
jet::Vector< T, 4 >::operator/=
Vector & operator/=(T v)
Computes this /= (v, v, v, v)
jet::operator/
Matrix2x2< T > operator/(const Matrix2x2< T > &a, T b)
Returns a' / b, where every element of matrix a' is a.
jet::min
Point2< T > min(const Point2< T > &a, const Point2< T > &b)
Returns element-wise min point: (min(a.x, b.x), min(a.y, b.y)).
jet::Vector< T, 4 >::operator*=
Vector & operator*=(const Vector &v)
Computes this *= (v.x, v.y, v.z, v.w)
jet::Vector< T, 4 >::div
Vector div(T v) const
Computes this / (v, v, v, v).
jet::Vector< T, 4 >::operator-=
Vector & operator-=(const Vector &v)
Computes this -= (v.x, v.y, v.z, v.w)
jet::Vector< T, 4 >::iadd
void iadd(T v)
Computes this += (v, v, v, v).
jet::Vector< T, 4 >::operator==
bool operator==(const Vector &v) const
Returns true if other is the same as this vector.
jet::operator-
Matrix2x2< T > operator-(const Matrix2x2< T > &a)
Returns a matrix with opposite sign.
jet::Vector< T, 4 >::distanceTo
T distanceTo(const Vector &other) const
Returns the distance to the other vector.
jet::Vector< T, 4 >::imul
void imul(T v)
Computes this *= (v, v, v, v).
jet::operator*
Matrix2x2< T > operator*(const Matrix2x2< T > &a, T b)
Returns a * b', where every element of matrix b' is b.
jet::Vector< T, 4 >::mul
Vector mul(T v) const
Computes this * (v, v, v, v).
jet::Vector< T, 4 >::operator=
Vector & operator=(const std::initializer_list< U > &lst)
Set x and y components with given initializer list.
jet::Vector< T, 4 >::rsub
Vector rsub(const Vector &v) const
Computes (v.x, v.y, v.z, v.w) - this.
jet::Vector< T, 4 >::operator*=
Vector & operator*=(T v)
Computes this *= (v, v, v, v)
jet::Vector< T, 4 >::normalize
void normalize()
Normalizes this vector.
jet::ScalarType
Returns the type of the value itself.
Definition: type_helpers.h:14