Vector2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Vector2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 2-D vector class.
6 > Created Time: 2017/02/19
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_VECTOR2_H
10 #define CUBBYFLOW_VECTOR2_H
11 
12 #include <Core/Utils/Constants.h>
13 #include <Core/Utils/TypeHelpers.h>
14 #include <Core/Vector/Vector.h>
15 
16 namespace CubbyFlow
17 {
25  template <typename T>
26  class Vector<T, 2> 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 
37  // MARK: Constructors
39  constexpr Vector() : x(0), y(0)
40  {
41  // Do nothing
42  }
43 
45  constexpr Vector(T _x, T _y) : x(_x), y(_y)
46  {
47  // Do nothing
48  }
49 
51  template <typename U>
52  Vector(const std::initializer_list<U>& list);
53 
55  constexpr Vector(const Vector& v) : x(v.x), y(v.y)
56  {
57  // Do nothing
58  }
59 
60  // MARK: Basic setters
62  void Set(T s);
63 
65  void Set(T x, T y);
66 
68  template <typename U>
69  void Set(const std::initializer_list<U>& list);
70 
72  void Set(const Vector& pt);
73 
75  void SetZero();
76 
78  void Normalize();
79 
80  // MARK: Binary operations: new instance = this (+) v
82  Vector Add(T v) const;
83 
85  Vector Add(const Vector& v) const;
86 
88  Vector Sub(T v) const;
89 
91  Vector Sub(const Vector& v) const;
92 
94  Vector Mul(T v) const;
95 
97  Vector Mul(const Vector& v) const;
98 
100  Vector Div(T v) const;
101 
103  Vector Div(const Vector& v) const;
104 
106  T Dot(const Vector& v) const;
107 
109  T Cross(const Vector& v) const;
110 
111  // MARK: Binary operations: new instance = v (+) this
113  Vector RSub(T v) const;
114 
116  Vector RSub(const Vector& v) const;
117 
119  Vector RDiv(T v) const;
120 
122  Vector RDiv(const Vector& v) const;
123 
125  T RCross(const Vector& v) const;
126 
127  // MARK: Augmented operations: this (+)= v
129  void IAdd(T v);
130 
132  void IAdd(const Vector& v);
133 
135  void ISub(T v);
136 
138  void ISub(const Vector& v);
139 
141  void IMul(T v);
142 
144  void IMul(const Vector& v);
145 
147  void IDiv(T v);
148 
150  void IDiv(const Vector& v);
151 
152  // MARK: Basic getters
154  const T& At(size_t i) const;
155 
157  T& At(size_t i);
158 
160  T Sum() const;
161 
163  T Avg() const;
164 
166  T Min() const;
167 
169  T Max() const;
170 
172  T AbsMin() const;
173 
175  T AbsMax() const;
176 
178  size_t DominantAxis() const;
179 
181  size_t SubdominantAxis() const;
182 
184  Vector Normalized() const;
185 
187  T Length() const;
188 
190  T LengthSquared() const;
191 
193  T DistanceTo(const Vector& other) const;
194 
196  T DistanceSquaredTo(const Vector& other) const;
197 
199  Vector Reflected(const Vector& normal) const;
200 
202  Vector Projected(const Vector& normal) const;
203 
205  Vector Tangential() const;
206 
208  template <typename U>
209  Vector<U, 2> 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 Vector2 = Vector<T, 2>;
264 
266  template <typename T>
268 
270  template <typename T>
272 
274  template <typename T>
275  Vector<T, 2> operator+(T a, const Vector<T, 2>& b);
276 
278  template <typename T>
279  Vector<T, 2> operator+(const Vector<T, 2>& a, const Vector<T, 2>& b);
280 
282  template <typename T>
283  Vector<T, 2> operator-(const Vector<T, 2>& a, T b);
284 
286  template <typename T>
287  Vector<T, 2> operator-(T a, const Vector<T, 2>& b);
288 
290  template <typename T>
291  Vector<T, 2> operator-(const Vector<T, 2>& a, const Vector<T, 2>& b);
292 
294  template <typename T>
295  Vector<T, 2> operator*(const Vector<T, 2>& a, T b);
296 
298  template <typename T>
299  Vector<T, 2> operator*(T a, const Vector<T, 2>& b);
300 
302  template <typename T>
303  Vector<T, 2> operator*(const Vector<T, 2>& a, const Vector<T, 2>& b);
304 
306  template <typename T>
307  Vector<T, 2> operator/(const Vector<T, 2>& a, T b);
308 
310  template <typename T>
311  Vector<T, 2> operator/(T a, const Vector<T, 2>& b);
312 
314  template <typename T>
315  Vector<T, 2> operator/(const Vector<T, 2>& a, const Vector<T, 2>& b);
316 
318  template <typename T>
319  Vector<T, 2> Min(const Vector<T, 2>& a, const Vector<T, 2>& b);
320 
322  template <typename T>
323  Vector<T, 2> Max(const Vector<T, 2>& a, const Vector<T, 2>& b);
324 
326  template <typename T>
327  Vector<T, 2> Clamp(const Vector<T, 2>& v, const Vector<T, 2>& low, const Vector<T, 2>& high);
328 
330  template <typename T>
331  Vector<T, 2> Ceil(const Vector<T, 2>& a);
332 
334  template <typename T>
335  Vector<T, 2> Floor(const Vector<T, 2>& a);
336 
339 
342 
343  // MARK: Extensions
345  template <>
346  constexpr Vector<float, 2> Zero<Vector<float, 2>>()
347  {
348  return Vector<float, 2>(0.f, 0.f);
349  }
350 
352  template <>
353  constexpr Vector<double, 2> Zero<Vector<double, 2>>()
354  {
355  return Vector<double, 2>(0.0, 0.0);
356  }
357 
359  template <typename T>
360  struct ScalarType<Vector<T, 2>>
361  {
362  typedef T value;
363  };
364 
366  template <typename T>
368  const Vector<T, 2>& v0,
369  const Vector<T, 2>& v1,
370  const Vector<T, 2>& v2,
371  const Vector<T, 2>& v3,
372  T f);
373 }
374 
376 
377 #endif
T Max() const
Returns the maximum element.
Definition: Vector-Impl.h:208
T x
X (or the first) component of the vector.
Definition: Vector2.h:29
constexpr Vector(const Vector &v)
Copy constructor.
Definition: Vector2.h:55
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
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
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.
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
Returns the type of the value itself.
Definition: TypeHelpers.h:16
constexpr Vector()
Constructs default vector (0, 0).
Definition: Vector2.h:39
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
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
T value
Definition: Vector2.h:362
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
Matrix< T, 2, 2 > operator-(const Matrix< T, 2, 2 > &a)
Returns a matrix with opposite sign.
Definition: Matrix2x2-Impl.h:654
T y
Y (or the second) component of the vector.
Definition: Vector2.h:35
Vector & operator*=(const T &s)
Computes this *= (s, s, ... , s)
Definition: Vector-Impl.h:577
constexpr Vector(T _x, T _y)
Constructs vector with given parameters _x and _y.
Definition: Vector2.h:45
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
2-D vector class.
Definition: Vector2.h:26
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 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
void ISub(const T &s)
Computes this -= (s, s, ... , s).
Definition: Vector-Impl.h:466
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
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