Vector4.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Vector4.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 4-D vector class.
6 > Created Time: 2017/02/24
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_VECTOR4_H
10 #define CUBBYFLOW_VECTOR4_H
11 
12 #include <Core/Vector/Vector3.h>
13 
14 #include <limits>
15 
16 namespace CubbyFlow
17 {
25  template <typename T>
26  class Vector<T, 4> final
27  {
28  public:
29  static_assert(std::is_floating_point<T>::value, "Vector only can be instantiated with floating point types");
30 
32  T x;
33 
35  T y;
36 
38  T z;
39 
41  T w;
42 
43  // MARK: Constructors
45  constexpr Vector() : x(0), y(0), z(0), w(0)
46  {
47  // Do nothing
48  }
49 
51  constexpr Vector(T _x, T _y, T _z, T _w) : x(_x), y(_y), z(_z), w(_w)
52  {
53  // Do nothing
54  }
55 
57  constexpr Vector(const Vector<T, 3>& v, T _w) : x(v.x), y(v.y), z(v.z), w(_w)
58  {
59  // Do nothing
60  }
61 
63  template <typename U>
64  Vector(const std::initializer_list<U>& list);
65 
67  constexpr Vector(const Vector& v) : x(v.x), y(v.y), z(v.z), w(v.w)
68  {
69  // Do nothing
70  }
71 
72  // MARK: Basic setters
74  void Set(T s);
75 
77  void Set(T x, T y, T z, T w);
78 
80  void Set(const Vector<T, 3>& pt, T z);
81 
83  template <typename U>
84  void Set(const std::initializer_list<U>& list);
85 
87  void Set(const Vector& v);
88 
90  void SetZero();
91 
93  void Normalize();
94 
95  // MARK: Binary operations: new instance = this (+) v
97  Vector Add(T v) const;
98 
100  Vector Add(const Vector& v) const;
101 
103  Vector Sub(T v) const;
104 
106  Vector Sub(const Vector& v) const;
107 
109  Vector Mul(T v) const;
110 
112  Vector Mul(const Vector& v) const;
113 
115  Vector Div(T v) const;
116 
118  Vector Div(const Vector& v) const;
119 
121  T Dot(const Vector& v) const;
122 
123  // MARK: Binary operations: new instance = v (+) this
125  Vector RSub(T v) const;
126 
128  Vector RSub(const Vector& v) const;
129 
131  Vector RDiv(T v) const;
132 
134  Vector RDiv(const Vector& v) const;
135 
136  // MARK: Augmented operations: this (+)= v
138  void IAdd(T v);
139 
141  void IAdd(const Vector& v);
142 
144  void ISub(T v);
145 
147  void ISub(const Vector& v);
148 
150  void IMul(T v);
151 
153  void IMul(const Vector& v);
154 
156  void IDiv(T v);
157 
159  void IDiv(const Vector& v);
160 
161  // MARK: Basic getters
163  const T& At(size_t i) const;
164 
166  T& At(size_t i);
167 
169  T Sum() const;
170 
172  T Avg() const;
173 
175  T Min() const;
176 
178  T Max() const;
179 
181  T AbsMin() const;
182 
184  T AbsMax() const;
185 
187  size_t DominantAxis() const;
188 
190  size_t SubdominantAxis() const;
191 
193  Vector Normalized() const;
194 
196  T Length() const;
197 
199  T LengthSquared() const;
200 
202  T DistanceTo(const Vector& other) const;
203 
205  T DistanceSquaredTo(const Vector& other) const;
206 
208  template <typename U>
209  Vector<U, 4> CastTo() const;
210 
212  bool IsEqual(const Vector& other) const;
213 
215  bool IsSimilar(const Vector& other, T epsilon = std::numeric_limits<T>::epsilon()) const;
216 
217  // MARK: Operators
219  T& operator[](size_t i);
220 
222  const T& operator[](size_t i) const;
223 
225  template <typename U>
226  Vector& operator=(const std::initializer_list<U>& list);
227 
229  Vector& operator=(const Vector& v);
230 
232  Vector& operator+=(T v);
233 
235  Vector& operator+=(const Vector& v);
236 
238  Vector& operator-=(T v);
239 
241  Vector& operator-=(const Vector& v);
242 
244  Vector& operator*=(T v);
245 
247  Vector& operator*=(const Vector& v);
248 
250  Vector& operator/=(T v);
251 
253  Vector& operator/=(const Vector& v);
254 
256  bool operator==(const Vector& v) const;
257 
259  bool operator!=(const Vector& v) const;
260  };
261 
263  template <typename T> using Vector4 = Vector<T, 4>;
264 
266  template <typename T>
268 
270  template <typename T>
272 
274  template <typename T>
275  Vector<T, 4> operator+(T a, const Vector<T, 4>& b);
276 
278  template <typename T>
279  Vector<T, 4> operator+(const Vector<T, 4>& a, const Vector<T, 4>& b);
280 
282  template <typename T>
283  Vector<T, 4> operator-(const Vector<T, 4>& a, T b);
284 
286  template <typename T>
287  Vector<T, 4> operator-(T a, const Vector<T, 4>& b);
288 
290  template <typename T>
291  Vector<T, 4> operator-(const Vector<T, 4>& a, const Vector<T, 4>& b);
292 
294  template <typename T>
295  Vector<T, 4> operator*(const Vector<T, 4>& a, T b);
296 
298  template <typename T>
299  Vector<T, 4> operator*(T a, const Vector<T, 4>& b);
300 
302  template <typename T>
303  Vector<T, 4> operator*(const Vector<T, 4>& a, const Vector<T, 4>& b);
304 
306  template <typename T>
307  Vector<T, 4> operator/(const Vector<T, 4>& a, T b);
308 
310  template <typename T>
311  Vector<T, 4> operator/(T a, const Vector<T, 4>& b);
312 
314  template <typename T>
315  Vector<T, 4> operator/(const Vector<T, 4>& a, const Vector<T, 4>& b);
316 
318  template <typename T>
319  Vector<T, 4> Min(const Vector<T, 4>& a, const Vector<T, 4>& b);
320 
322  template <typename T>
323  Vector<T, 4> Max(const Vector<T, 4>& a, const Vector<T, 4>& b);
324 
326  template <typename T>
327  Vector<T, 4> Clamp(const Vector<T, 4>& v, const Vector<T, 4>& low, const Vector<T, 4>& high);
328 
330  template <typename T>
331  Vector<T, 4> Ceil(const Vector<T, 4>& a);
332 
334  template <typename T>
335  Vector<T, 4> Floor(const Vector<T, 4>& a);
336 
339 
342 
343  // MARK: Extensions
345  template <>
347  {
348  return Vector4F(0.f, 0.f, 0.f, 0.f);
349  }
350 
352  template <>
354  {
355  return Vector4D(0.0, 0.0, 0.0, 0.0);
356  }
357 
359  template <typename T>
360  struct ScalarType<Vector<T, 4>>
361  {
362  typedef T value;
363  };
364 
366  template <typename T>
368  const Vector<T, 4>& v0,
369  const Vector<T, 4>& v1,
370  const Vector<T, 4>& v2,
371  const Vector<T, 4>& v3,
372  T f);
373 }
374 
376 
377 #endif
T Max() const
Returns the maximum element.
Definition: Vector-Impl.h:208
3-D vector class.
Definition: Vector3.h:26
VectorScalarRDiv< T, Vector > RDiv(const T &s) const
Computes (s, s, ... , s) / this.
Definition: Vector-Impl.h:440
size_t DominantAxis() const
Returns the index of the dominant axis.
Definition: Vector-Impl.h:247
T DistanceTo(const E &other) const
Returns the distance to the other vector.
Definition: Vector-Impl.h:289
Vector4< float > Vector4F
Float-type 4D vector.
Definition: Vector4.h:338
constexpr Vector(const Vector< T, 3 > &v, T _w)
Constructs vector with a 3-D vector (x, y, and z) and a scalar (w).
Definition: Vector4.h:57
Point< T, 2 > Ceil(const Point< T, 2 > &a)
Returns element-wise ceiled point.
Definition: Point2-Impl.h:492
const T & operator[](size_t i) const
Returns the const reference to the i -th element.
Definition: Vector-Impl.h:519
Matrix< T, 2, 2 > operator+(const Matrix< T, 2, 2 > &a, const Matrix< T, 2, 2 > &b)
Returns a + b (element-size).
Definition: Matrix2x2-Impl.h:660
T y
Y (or the second) component of the vector.
Definition: Vector4.h:35
size_t SubdominantAxis() const
Returns the index of the subdominant axis.
Definition: Vector-Impl.h:258
VectorTypeCast< U, Vector< T, N >, T > CastTo() const
Returns a vector with different value type.
Definition: Vector-Impl.h:313
Vector & operator=(const std::initializer_list< U > &list)
Set vector instance with initializer list.
VectorSub< T, Vector, E > Sub(const E &v) const
Computes this - v.
constexpr Vector(const Vector &v)
Copy constructor.
Definition: Vector4.h:67
Generic statically-sized N-D vector class.
Definition: Vector.h:33
Vector()
Constructs a vector with zeros.
Definition: Vector-Impl.h:17
bool IsEqual(const E &other) const
Returns true if other is the same as this vector.
Definition: Vector-Impl.h:320
T w
W (or the fourth) component of the vector.
Definition: Vector4.h:41
Returns the type of the value itself.
Definition: TypeHelpers.h:16
T LengthSquared() const
Returns the squared length of the vector.
Definition: Vector-Impl.h:282
T At(size_t i) const
Returns const reference to the i -th element of the vector.
Definition: Vector-Impl.h:164
Point< T, 2 > Floor(const Point< T, 2 > &a)
Returns element-wise floored point.
Definition: Point2-Impl.h:498
VectorDiv< T, Vector, E > Div(const E &v) const
Computes this / v.
bool operator==(const E &v) const
Returns true if other is the same as this vector.
Definition: Vector-Impl.h:608
Matrix< T, 2, 2 > operator/(const Matrix< T, 2, 2 > &a, T b)
Definition: Matrix2x2-Impl.h:720
Definition: pybind11Utils.h:24
T z
Z (or the third) component of the vector.
Definition: Vector4.h:38
void IAdd(const T &s)
Computes this += (s, s, ... , s).
Definition: Vector-Impl.h:453
T Min() const
Returns the minimum element.
Definition: Vector-Impl.h:195
T Clamp(T val, T low, T high)
Returns the clamped value.
Definition: MathUtils-Impl.h:123
Vector & operator-=(const T &s)
Computes this -= (s, s, ... , s)
Definition: Vector-Impl.h:562
void Set(const T &s)
Sets all elements to s.
Definition: Vector-Impl.h:55
T Avg() const
Returns the average of all the elements.
Definition: Vector-Impl.h:189
T Dot(const E &v) const
Computes dot product.
Definition: Vector-Impl.h:412
void IMul(const T &s)
Computes this *= (s, s, ... , s).
Definition: Vector-Impl.h:479
VectorScalarDiv< T, Vector > Normalized() const
Returns normalized vector.
Definition: Vector-Impl.h:269
constexpr Vector4D Zero< Vector4D >()
Returns double-type zero vector.
Definition: Vector4.h:353
Matrix< T, 2, 2 > operator-(const Matrix< T, 2, 2 > &a)
Returns a matrix with opposite sign.
Definition: Matrix2x2-Impl.h:654
4-D vector class.
Definition: Vector4.h:26
Vector & operator*=(const T &s)
Computes this *= (s, s, ... , s)
Definition: Vector-Impl.h:577
Point< T, 2 > Max(const Point< T, 2 > &a, const Point< T, 2 > &b)
Returns element-wise max point: (max(a.x, b.x), max(a.y, b.y)).
Definition: Point2-Impl.h:480
Vector & operator/=(const T &s)
Computes this /= (s, s, ... , s)
Definition: Vector-Impl.h:592
Vector & operator+=(const T &s)
Computes this += (s, s, ... , s)
Definition: Vector-Impl.h:547
VectorMul< T, Vector, E > Mul(const E &v) const
Computes this * v.
T AbsMax() const
Returns the absolute maximum element.
Definition: Vector-Impl.h:234
T DistanceSquaredTo(const E &other) const
Returns the squared distance to the other vector.
Definition: Vector-Impl.h:296
T Sum() const
Returns the sum of all the elements.
Definition: Vector-Impl.h:176
Vector4< double > Vector4D
Double-type 4D vector.
Definition: Vector4.h:341
bool IsSimilar(const E &other, T epsilon=std::numeric_limits< T >::epsilon()) const
Returns true if other is similar to this vector.
Definition: Vector-Impl.h:340
T AbsMin() const
Returns the absolute minimum element.
Definition: Vector-Impl.h:221
Vector< T, 3 > operator*(const Quaternion< T > &q, const Vector< T, 3 > &v)
Returns quaternion q * vector v.
Definition: Quaternion-Impl.h:481
T x
X (or the first) component of the vector.
Definition: Vector4.h:29
T MonotonicCatmullRom(const T &f0, const T &f1, const T &f2, const T &f3, T f)
Computes monotonic Catmull-Rom interpolation.
Definition: MathUtils-Impl.h:228
constexpr Vector()
Constructs default vector (0, 0, 0, 0).
Definition: Vector4.h:45
void ISub(const T &s)
Computes this -= (s, s, ... , s).
Definition: Vector-Impl.h:466
T value
Definition: Vector4.h:362
constexpr Vector4F Zero< Vector4F >()
Returns float-type zero vector.
Definition: Vector4.h:346
VectorAdd< T, Vector, E > Add(const E &v) const
Computes this + v.
bool operator!=(const E &v) const
Returns true if other is the not same as this vector.
Definition: Vector-Impl.h:615
void SetZero()
Sets all elements to zero.
Definition: Vector-Impl.h:98
constexpr Vector(T _x, T _y, T _z, T _w)
Constructs vector with given parameters _x, _y, _z, and _w.
Definition: Vector4.h:51
Point< T, 2 > Min(const Point< T, 2 > &a, const Point< T, 2 > &b)
Returns element-wise min point: (min(a.x, b.x), min(a.y, b.y)).
Definition: Point2-Impl.h:474
VectorScalarRSub< T, Vector > RSub(const T &s) const
Computes (s, s, ... , s) - this.
Definition: Vector-Impl.h:427
T Length() const
Returns the length of the vector.
Definition: Vector-Impl.h:276
void Normalize()
Normalizes this vector.
Definition: Vector-Impl.h:104
void IDiv(const T &s)
Computes this /= (s, s, ... , s).
Definition: Vector-Impl.h:492