Vector3-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Vector3-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Dongmin Kim
5 > Purpose: 3-D vector class.
6 > Created Time: 2017/02/22
7 > Copyright (c) 2018, Dongmin Kim
8 *************************************************************************/
9 #ifndef CUBBYFLOW_VECTOR3_IMPL_H
10 #define CUBBYFLOW_VECTOR3_IMPL_H
11 
12 #include <Core/Math/MathUtils.h>
13 
14 #include <cassert>
15 
16 namespace CubbyFlow
17 {
18  // Constructors
19  template <typename T>
20  template <typename U>
21  Vector<T, 3>::Vector(const std::initializer_list<U>& list)
22  {
23  Set(list);
24  }
25 
26  template <typename T>
28  {
29  x = s;
30  y = s;
31  z = s;
32  }
33 
34  template <typename T>
35  void Vector<T, 3>::Set(T newX, T newY, T newZ)
36  {
37  x = newX;
38  y = newY;
39  z = newZ;
40  }
41 
42  template <typename T>
43  void Vector<T, 3>::Set(const Vector2<T>& pt, T newZ)
44  {
45  x = pt.x;
46  y = pt.y;
47  z = newZ;
48  }
49 
50  template <typename T>
51  template <typename U>
52  void Vector<T, 3>::Set(const std::initializer_list<U>& list)
53  {
54  assert(list.size() >= 3);
55 
56  auto inputElem = list.begin();
57  x = static_cast<T>(*inputElem);
58  y = static_cast<T>(*(++inputElem));
59  z = static_cast<T>(*(++inputElem));
60  }
61 
62  template <typename T>
63  void Vector<T, 3>::Set(const Vector& v)
64  {
65  x = v.x;
66  y = v.y;
67  z = v.z;
68  }
69 
70  template <typename T>
72  {
73  x = 0;
74  y = 0;
75  z = 0;
76  }
77 
78  template <typename T>
80  {
81  T length = Length();
82  x /= length;
83  y /= length;
84  z /= length;
85  }
86 
87  template <typename T>
89  {
90  return Vector<T, 3>(x + v, y + v, z + v);
91  }
92 
93  template <typename T>
95  {
96  return Vector<T, 3>(x + v.x, y + v.y, z + v.z);
97  }
98 
99  template <typename T>
101  {
102  return Vector<T, 3>(x - v, y - v, z - v);
103  }
104 
105  template <typename T>
107  {
108  return Vector<T, 3>(x - v.x, y - v.y, z - v.z);
109  }
110 
111  template <typename T>
113  {
114  return Vector<T, 3>(x * v, y * v, z * v);
115  }
116 
117  template <typename T>
119  {
120  return Vector<T, 3>(x * v.x, y * v.y, z * v.z);
121  }
122 
123  template <typename T>
125  {
126  return Vector<T, 3>(x / v, y / v, z / v);
127  }
128 
129  template <typename T>
131  {
132  return Vector<T, 3>(x / v.x, y / v.y, z / v.z);
133  }
134 
135  template <typename T>
136  T Vector<T, 3>::Dot(const Vector& v) const
137  {
138  return x * v.x + y * v.y + z * v.z;
139  }
140 
141  template <typename T>
143  {
144  return Vector<T, 3>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
145  }
146 
147  template <typename T>
149  {
150  return Vector<T, 3>(v - x, v - y, v - z);
151  }
152 
153  template <typename T>
155  {
156  return Vector<T, 3>(v.x - x, v.y - y, v.z - z);
157  }
158 
159  template <typename T>
161  {
162  return Vector<T, 3>(v / x, v / y, v / z);
163  }
164 
165  template <typename T>
167  {
168  return Vector<T, 3>(v.x / x, v.y / y, v.z / z);
169  }
170 
171  template <typename T>
173  {
174  return Vector<T, 3>(v.y * z - v.z * y, v.z * x - v.x * z, v.x * y - v.y * x);
175  }
176 
177  template <typename T>
179  {
180  x += v;
181  y += v;
182  z += v;
183  }
184 
185  template <typename T>
186  void Vector<T, 3>::IAdd(const Vector& v)
187  {
188  x += v.x;
189  y += v.y;
190  z += v.z;
191  }
192 
193  template <typename T>
195  {
196  x -= v;
197  y -= v;
198  z -= v;
199  }
200 
201  template <typename T>
202  void Vector<T, 3>::ISub(const Vector& v)
203  {
204  x -= v.x;
205  y -= v.y;
206  z -= v.z;
207  }
208 
209  template <typename T>
211  {
212  x *= v;
213  y *= v;
214  z *= v;
215  }
216 
217  template <typename T>
218  void Vector<T, 3>::IMul(const Vector& v)
219  {
220  x *= v.x;
221  y *= v.y;
222  z *= v.z;
223  }
224 
225  template <typename T>
227  {
228  x /= v;
229  y /= v;
230  z /= v;
231  }
232 
233  template <typename T>
234  void Vector<T, 3>::IDiv(const Vector& v)
235  {
236  x /= v.x;
237  y /= v.y;
238  z /= v.z;
239  }
240 
241  template <typename T>
242  const T& Vector<T, 3>::At(size_t i) const
243  {
244  assert(i < 3);
245  return (&x)[i];
246  }
247 
248  template <typename T>
249  T& Vector<T, 3>::At(size_t i)
250  {
251  assert(i < 3);
252  return (&x)[i];
253  }
254 
255  template <typename T>
257  {
258  return x + y + z;
259  }
260 
261  template <typename T>
263  {
264  return Sum() / 3;
265  }
266 
267  template <typename T>
269  {
270  return std::min({ x, y, z });
271  }
272 
273  template <typename T>
275  {
276  return std::max({ x, y, z });
277  }
278 
279  template <typename T>
281  {
282  return CubbyFlow::AbsMin(CubbyFlow::AbsMin(x, y), z);
283  }
284 
285  template <typename T>
287  {
288  return CubbyFlow::AbsMax(CubbyFlow::AbsMax(x, y), z);
289  }
290 
291  template <typename T>
293  {
294  return (std::fabs(x) > std::fabs(y))
295  ? ((std::fabs(x) > std::fabs(z)) ? 0 : 2)
296  : ((std::fabs(y) > std::fabs(z)) ? 1 : 2);
297  }
298 
299  template <typename T>
301  {
302  return (std::fabs(x) < std::fabs(y))
303  ? ((std::fabs(x) < std::fabs(z)) ? 0 : 2)
304  : ((std::fabs(y) < std::fabs(z)) ? 1 : 2);
305  }
306 
307  template <typename T>
309  {
310  return Vector<T, 3>(x / Length(), y / Length(), z / Length());
311  }
312 
313  template <typename T>
315  {
316  return std::sqrt(x * x + y * y + z * z);
317  }
318 
319  template <typename T>
321  {
322  return x * x + y * y + z * z;
323  }
324 
325  template <typename T>
327  {
328  return Sub(other).Length();
329  }
330 
331  template <typename T>
333  {
334  return Sub(other).LengthSquared();
335  }
336 
337  template <typename T>
339  {
340  // this - 2(this.n)n
341  return Sub(normal.Mul(2 * Dot(normal)));
342  }
343 
344  template <typename T>
346  {
347  // this - (this.n)n
348  return Sub(normal.Mul(Dot(normal)));
349  }
350 
351  template <typename T>
352  std::tuple<Vector<T, 3>, Vector<T, 3>> Vector<T, 3>::Tangential() const
353  {
354  Vector<T, 3> a = ((std::fabs(y) > 0 || std::fabs(z) > 0)
355  ? Vector<T, 3>(1, 0, 0)
356  : Vector<T, 3>(0, 1, 0)).Cross(*this).Normalized();
357  Vector<T, 3> b = Cross(a);
358 
359  return std::make_tuple(a, b);
360  }
361 
362  template <typename T>
363  template <typename U>
365  {
366  return Vector<U, 3>(static_cast<U>(x), static_cast<U>(y), static_cast<U>(z));
367  }
368 
369  template <typename T>
370  bool Vector<T, 3>::IsEqual(const Vector& other) const
371  {
372  return (x == other.x && y == other.y && z == other.z);
373  }
374 
375  template <typename T>
376  bool Vector<T, 3>::IsSimilar(const Vector& other, T epsilon) const
377  {
378  return (std::fabs(x - other.x) < epsilon && std::fabs(y - other.y) < epsilon && std::fabs(z - other.z) < epsilon);
379  }
380 
381  template <typename T>
383  {
384  assert(i < 3);
385  return (&x)[i];
386  }
387 
388  template <typename T>
389  const T& Vector<T, 3>::operator[](size_t i) const
390  {
391  assert(i < 3);
392  return (&x)[i];
393  }
394 
395  template <typename T>
396  template <typename U>
397  Vector<T, 3>& Vector<T, 3>::operator=(const std::initializer_list<U>& list)
398  {
399  Set(list);
400  return (*this);
401  }
402 
403  template <typename T>
405  {
406  Set(v);
407  return (*this);
408  }
409 
410  template <typename T>
412  {
413  IAdd(v);
414  return (*this);
415  }
416 
417  template <typename T>
419  {
420  IAdd(v);
421  return (*this);
422  }
423 
424  template <typename T>
426  {
427  ISub(v);
428  return (*this);
429  }
430 
431  template <typename T>
433  {
434  ISub(v);
435  return (*this);
436  }
437 
438  template <typename T>
440  {
441  IMul(v);
442  return (*this);
443  }
444 
445  template <typename T>
447  {
448  IMul(v);
449  return (*this);
450  }
451 
452  template <typename T>
454  {
455  IDiv(v);
456  return (*this);
457  }
458 
459  template <typename T>
461  {
462  IDiv(v);
463  return (*this);
464  }
465 
466  template <typename T>
467  bool Vector<T, 3>::operator==(const Vector& v) const
468  {
469  return IsEqual(v);
470  }
471 
472  template <typename T>
473  bool Vector<T, 3>::operator!=(const Vector& v) const
474  {
475  return !IsEqual(v);
476  }
477 
478  template <typename T>
480  {
481  return a;
482  }
483 
484  template <typename T>
486  {
487  return Vector<T, 3>(-a.x, -a.y, -a.z);
488  }
489 
490  template <typename T>
492  {
493  return a.Add(b);
494  }
495 
496  template <typename T>
498  {
499  return b.Add(a);
500  }
501 
502  template <typename T>
504  {
505  return a.Add(b);
506  }
507 
508  template <typename T>
510  {
511  return a.Sub(b);
512  }
513 
514  template <typename T>
516  {
517  return b.RSub(a);
518  }
519 
520  template <typename T>
522  {
523  return a.Sub(b);
524  }
525 
526  template <typename T>
528  {
529  return a.Mul(b);
530  }
531 
532  template <typename T>
534  {
535  return b.Mul(a);
536  }
537 
538  template <typename T>
540  {
541  return a.Mul(b);
542  }
543 
544  template <typename T>
546  {
547  return a.Div(b);
548  }
549 
550  template <typename T>
552  {
553  return b.RDiv(a);
554  }
555 
556  template <typename T>
558  {
559  return a.Div(b);
560  }
561 
562  template <typename T>
564  {
565  return Vector<T, 3>(std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z));
566  }
567 
568  template <typename T>
570  {
571  return Vector<T, 3>(std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z));
572  }
573 
574  template <typename T>
575  Vector<T, 3> Clamp(const Vector<T, 3>& v, const Vector<T, 3>& low, const Vector<T, 3>& high)
576  {
577  return Vector<T, 3>(Clamp(v.x, low.x, high.x), Clamp(v.y, low.y, high.y), Clamp(v.z, low.z, high.z));
578  }
579 
580  template <typename T>
582  {
583  return Vector<T, 3>(std::ceil(a.x), std::ceil(a.y), std::ceil(a.z));
584  }
585 
586  template <typename T>
588  {
589  return Vector<T, 3>(std::floor(a.x), std::floor(a.y), std::floor(a.z));
590  }
591 
592  template <typename T>
594  const Vector<T, 3>& v0,
595  const Vector<T, 3>& v1,
596  const Vector<T, 3>& v2,
597  const Vector<T, 3>& v3,
598  T f)
599  {
600  static const T two = static_cast<T>(2);
601  static const T three = static_cast<T>(3);
602 
603  Vector<T, 3> d1 = (v2 - v0) / two;
604  Vector<T, 3> d2 = (v3 - v1) / two;
605  Vector<T, 3> D1 = v2 - v1;
606 
607  if (std::fabs(D1.x) < std::numeric_limits<float>::epsilon() ||
608  Sign(D1.x) != Sign(d1.x) ||
609  Sign(D1.x) != Sign(d2.x))
610  {
611  d1.x = d2.x = 0;
612  }
613 
614  if (std::fabs(D1.y) < std::numeric_limits<float>::epsilon() ||
615  Sign(D1.y) != Sign(d1.y) ||
616  Sign(D1.y) != Sign(d2.y))
617  {
618  d1.y = d2.y = 0;
619  }
620 
621  if (std::fabs(D1.z) < std::numeric_limits<float>::epsilon() ||
622  Sign(D1.z) != Sign(d1.z) ||
623  Sign(D1.z) != Sign(d2.z))
624  {
625  d1.z = d2.z = 0;
626  }
627 
628  Vector<T, 3> a3 = d1 + d2 - two * D1;
629  Vector<T, 3> a2 = three * D1 - two * d1 - d2;
630  Vector<T, 3> a1 = d1;
631  Vector<T, 3> a0 = v1;
632 
633  return a3 * Cubic(f) + a2 * Square(f) + a1 * f + a0;
634  }
635 }
636 
637 #endif
T Max() const
Returns the maximum element.
Definition: Vector-Impl.h:208
3-D vector class.
Definition: Vector3.h:26
Vector Mul(T v) const
Computes this * (v, v, v).
Definition: Vector3-Impl.h:112
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
T z
Z (or the third) component of the vector.
Definition: Vector3.h:38
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
Vector RSub(T v) const
Computes (v, v, v) - this.
Definition: Vector3-Impl.h:148
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 & 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 Add(T v) const
Computes this + (v, v, v).
Definition: Vector3-Impl.h:88
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 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.
T x
X (or the first) component of the vector.
Definition: Vector3.h:29
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
Vector Div(T v) const
Computes this / (v, v, v).
Definition: Vector3-Impl.h:124
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 RDiv(T v) const
Computes (v, v, v) / this.
Definition: Vector3-Impl.h:160
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
Vector Sub(T v) const
Computes this - (v, v, v).
Definition: Vector3-Impl.h:100
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 y
Y (or the second) component of the vector.
Definition: Vector3.h:35
Vector Cross(const Vector &v) const
Computes cross product.
Definition: Vector3-Impl.h:142
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