Vector2-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Vector2-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Dongmin Kim
5 > Purpose: 2-D vector class.
6 > Created Time: 2017/02/21
7 > Copyright (c) 2018, Dongmin Kim
8 *************************************************************************/
9 #ifndef CUBBYFLOW_VECTOR2_IMPL_H
10 #define CUBBYFLOW_VECTOR2_IMPL_H
11 
12 #include <Core/Math/MathUtils.h>
13 
14 #include <cassert>
15 #include <limits>
16 
17 namespace CubbyFlow
18 {
19  // Constructors
20  template <typename T>
21  template <typename U>
22  Vector<T, 2>::Vector(const std::initializer_list<U>& list)
23  {
24  Set(list);
25  }
26 
27  template <typename T>
29  {
30  x = s;
31  y = s;
32  }
33 
34  template <typename T>
35  void Vector<T, 2>::Set(T newX, T newY)
36  {
37  x = newX;
38  y = newY;
39  }
40 
41  template <typename T>
42  template <typename U>
43  void Vector<T, 2>::Set(const std::initializer_list<U>& list)
44  {
45  assert(list.size() >= 2);
46 
47  auto inputElem = list.begin();
48  x = static_cast<T>(*inputElem);
49  y = static_cast<T>(*(++inputElem));
50  }
51 
52  template <typename T>
53  void Vector<T, 2>::Set(const Vector& v)
54  {
55  x = v.x;
56  y = v.y;
57  }
58 
59  template <typename T>
61  {
62  x = 0;
63  y = 0;
64  }
65 
66  template <typename T>
68  {
69  T length = Length();
70  x /= length;
71  y /= length;
72  }
73 
74  template <typename T>
76  {
77  return Vector(x + v, y + v);
78  }
79 
80  template <typename T>
82  {
83  return Vector(x + v.x, y + v.y);
84  }
85 
86 
87  template <typename T>
89  {
90  return Vector(x - v, y - v);
91  }
92 
93  template <typename T>
95  {
96  return Vector(x - v.x, y - v.y);
97  }
98 
99  template <typename T>
101  {
102  return Vector(x * v, y * v);
103  }
104 
105  template <typename T>
107  {
108  return Vector(x * v.x, y * v.y);
109  }
110 
111  template <typename T>
113  {
114  return Vector(x / v, y / v);
115  }
116 
117  template <typename T>
119  {
120  return Vector(x / v.x, y / v.y);
121  }
122 
123  template <typename T>
124  T Vector<T, 2>::Dot(const Vector& v) const
125  {
126  return x * v.x + y * v.y;
127  }
128 
129  template <typename T>
130  T Vector<T, 2>::Cross(const Vector& v) const
131  {
132  return x * v.y - v.x * y;
133  }
134 
135  template <typename T>
137  {
138  return Vector(v - x, v - y);
139  }
140 
141  template <typename T>
143  {
144  return Vector(v.x - x, v.y - y);
145  }
146 
147  template <typename T>
149  {
150  return Vector(v / x, v / y);
151  }
152 
153  template <typename T>
155  {
156  return Vector(v.x / x, v.y / y);
157  }
158 
159  template <typename T>
160  T Vector<T, 2>::RCross(const Vector& v) const
161  {
162  return v.x * y - x * v.y;
163  }
164 
165  template <typename T>
167  {
168  x += v;
169  y += v;
170  }
171 
172  template <typename T>
173  void Vector<T, 2>::IAdd(const Vector& v)
174  {
175  x += v.x;
176  y += v.y;
177  }
178 
179  template <typename T>
181  {
182  x -= v;
183  y -= v;
184  }
185 
186  template <typename T>
187  void Vector<T, 2>::ISub(const Vector& v)
188  {
189  x -= v.x;
190  y -= v.y;
191  }
192 
193  template <typename T>
195  {
196  x *= v;
197  y *= v;
198  }
199 
200  template <typename T>
201  void Vector<T, 2>::IMul(const Vector& v)
202  {
203  x *= v.x;
204  y *= v.y;
205  }
206 
207  template <typename T>
209  {
210  x /= v;
211  y /= v;
212  }
213 
214  template <typename T>
215  void Vector<T, 2>::IDiv(const Vector& v)
216  {
217  x /= v.x;
218  y /= v.y;
219  }
220 
221  template <typename T>
222  const T& Vector<T, 2>::At(size_t i) const
223  {
224  assert(i < 2);
225  return (&x)[i];
226  }
227 
228  template <typename T>
229  T& Vector<T, 2>::At(size_t i)
230  {
231  assert(i < 2);
232  return (&x)[i];
233  }
234 
235  template <typename T>
237  {
238  return x + y;
239  }
240 
241  template <typename T>
243  {
244  return (x + y) / 2;
245  }
246 
247  template <typename T>
249  {
250  return std::min(x, y);
251  }
252 
253  template <typename T>
255  {
256  return std::max(x, y);
257  }
258 
259  template <typename T>
261  {
262  return CubbyFlow::AbsMin(x, y);
263  }
264 
265  template <typename T>
267  {
268  return CubbyFlow::AbsMax(x, y);
269  }
270 
271  template <typename T>
273  {
274  return (std::fabs(x) > std::fabs(y)) ? 0 : 1;
275  }
276 
277  template <typename T>
279  {
280  return (std::fabs(x) < std::fabs(y)) ? 0 : 1;
281  }
282 
283  template <typename T>
285  {
286  return Vector(x / Length(), y / Length());
287  }
288 
289  template <typename T>
291  {
292  return std::sqrt(x * x + y * y);
293  }
294 
295  template <typename T>
297  {
298  return x * x + y * y;
299  }
300 
301  template <typename T>
303  {
304  return Sub(other).Length();
305  }
306 
307  template <typename T>
309  {
310  return Sub(other).LengthSquared();
311  }
312 
313  template <typename T>
315  {
316  // this - 2(this.n)n
317  return Sub(normal.Mul(2 * Dot(normal)));
318  }
319 
320  template <typename T>
322  {
323  // this - (this.n)n
324  return Sub(normal.Mul(Dot(normal)));
325  }
326 
327  template <typename T>
329  {
330  // Rotate 90 degrees
331  return Vector<T, 2>(-y, x);
332  }
333 
334  template <typename T>
335  template <typename U>
337  {
338  return Vector<U, 2>(static_cast<U>(x), static_cast<U>(y));
339  }
340 
341  template <typename T>
342  bool Vector<T, 2>::IsEqual(const Vector& other) const
343  {
344  return (x == other.x && y == other.y);
345  }
346 
347  template <typename T>
348  bool Vector<T, 2>::IsSimilar(const Vector& other, T epsilon) const
349  {
350  return (std::fabs(x - other.x) < epsilon) && (std::fabs(y - other.y) < epsilon);
351  }
352 
353  template <typename T>
355  {
356  assert(i < 2);
357  return (&x)[i];
358  }
359 
360  template <typename T>
361  const T& Vector<T, 2>::operator[](size_t i) const
362  {
363  return At(i);
364  }
365 
366  template <typename T>
367  template <typename U>
368  Vector<T, 2>& Vector<T, 2>::operator=(const std::initializer_list<U>& list)
369  {
370  Set(list);
371  return (*this);
372  }
373 
374  template <typename T>
376  {
377  Set(v);
378  return (*this);
379  }
380 
381  template <typename T>
383  {
384  IAdd(v);
385  return (*this);
386  }
387 
388  template <typename T>
390  {
391  IAdd(v);
392  return (*this);
393  }
394 
395  template <typename T>
397  {
398  ISub(v);
399  return (*this);
400  }
401 
402  template <typename T>
404  {
405  ISub(v);
406  return (*this);
407  }
408 
409  template <typename T>
411  {
412  IMul(v);
413  return (*this);
414  }
415 
416  template <typename T>
418  {
419  IMul(v);
420  return (*this);
421  }
422 
423  template <typename T>
425  {
426  IDiv(v);
427  return (*this);
428  }
429 
430  template <typename T>
432  {
433  IDiv(v);
434  return (*this);
435  }
436 
437  template <typename T>
438  bool Vector<T, 2>::operator==(const Vector& v) const
439  {
440  return IsEqual(v);
441  }
442 
443  template <typename T>
444  bool Vector<T, 2>::operator!=(const Vector& v) const
445  {
446  return !IsEqual(v);
447  }
448 
449  template <typename T>
451  {
452  return a;
453  }
454 
455  template <typename T>
457  {
458  return Vector<T, 2>(-a.x, -a.y);
459  }
460 
461  template <typename T>
463  {
464  return a.Add(b);
465  }
466 
467  template <typename T>
469  {
470  return b.Add(a);
471  }
472 
473  template <typename T>
475  {
476  return a.Add(b);
477  }
478 
479  template <typename T>
481  {
482  return a.Sub(b);
483  }
484 
485 
486  template <typename T>
488  {
489  return b.RSub(a);
490  }
491 
492  template <typename T>
494  {
495  return a.Sub(b);
496  }
497 
498  template <typename T>
500  {
501  return a.Mul(b);
502  }
503 
504  template <typename T>
506  {
507  return b.Mul(a);
508  }
509 
510  template <typename T>
512  {
513  return a.Mul(b);
514  }
515 
516  template <typename T>
518  {
519  return a.Div(b);
520  }
521 
522  template <typename T>
524  {
525  return b.RDiv(a);
526  }
527 
528  template <typename T>
530  {
531  return a.Div(b);
532  }
533 
534  template <typename T>
536  {
537  return Vector<T, 2>(std::min(a.x, b.x), std::min(a.y, b.y));
538  }
539 
540  template <typename T>
542  {
543  return Vector<T, 2>(std::max(a.x, b.x), std::max(a.y, b.y));
544  }
545 
546  template <typename T>
547  Vector<T, 2> Clamp(const Vector<T, 2>& v, const Vector<T, 2>& low, const Vector<T, 2>& high)
548  {
549  return Vector<T, 2>(Clamp(v.x, low.x, high.x), Clamp(v.y, low.y, high.y));
550  }
551 
552  template <typename T>
554  {
555  return Vector<T, 2>(std::ceil(a.x), std::ceil(a.y));
556  }
557 
558  template <typename T>
560  {
561  return Vector<T, 2>(std::floor(a.x), std::floor(a.y));
562  }
563 
564  template <typename T>
566  const Vector<T, 2>& v0,
567  const Vector<T, 2>& v1,
568  const Vector<T, 2>& v2,
569  const Vector<T, 2>& v3,
570  T f)
571  {
572  static const T two = static_cast<T>(2);
573  static const T three = static_cast<T>(3);
574 
575  Vector<T, 2> d1 = (v2 - v0) / two;
576  Vector<T, 2> d2 = (v3 - v1) / two;
577  Vector<T, 2> D1 = v2 - v1;
578 
579  if (std::fabs(D1.x) < std::numeric_limits<T>::epsilon() ||
580  Sign(D1.x) != Sign(d1.x) ||
581  Sign(D1.x) != Sign(d2.x))
582  {
583  d1.x = d2.x = 0;
584  }
585 
586  if (std::fabs(D1.y) < std::numeric_limits<T>::epsilon() ||
587  Sign(D1.y) != Sign(d1.y) ||
588  Sign(D1.y) != Sign(d2.y))
589  {
590  d1.y = d2.y = 0;
591  }
592 
593  Vector<T, 2> a3 = d1 + d2 - two * D1;
594  Vector<T, 2> a2 = three * D1 - two * d1 - d2;
595  Vector<T, 2> a1 = d1;
596  Vector<T, 2> a0 = v1;
597 
598  return a3 * Cubic(f) + a2 * Square(f) + a1 * f + a0;
599  }
600 }
601 
602 #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
T AbsMin(T x, T y)
Returns the absolute minimum value among the two inputs.
Definition: MathUtils-Impl.h:39
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
T Square(T x)
Returns the square of x.
Definition: MathUtils-Impl.h:111
Vector Sub(T v) const
Computes this - (v, v).
Definition: Vector2-Impl.h:88
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
Vector Mul(T v) const
Computes this * (v, v).
Definition: Vector2-Impl.h:100
bool IsEqual(const E &other) const
Returns true if other is the same as this vector.
Definition: Vector-Impl.h:320
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.
Vector Div(T v) const
Computes this / (v, v).
Definition: Vector2-Impl.h:112
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
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
Vector RSub(T v) const
Computes (v, v) - this.
Definition: Vector2-Impl.h:136
Vector RDiv(T v) const
Computes (v, v) / this.
Definition: Vector2-Impl.h:148
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
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
T Cubic(T x)
Returns the cubic of x.
Definition: MathUtils-Impl.h:117
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
T AbsMax(T x, T y)
Returns the absolute maximum value among the two inputs.
Definition: MathUtils-Impl.h:45
void ISub(const T &s)
Computes this -= (s, s, ... , s).
Definition: Vector-Impl.h:466
T Sign(T x)
Returns the sign of the value.
Definition: MathUtils-Impl.h:26
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
Vector Add(T v) const
Computes this + (v, v).
Definition: Vector2-Impl.h:75
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