Vector3.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Vector3.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 3-D vector class.
6 > Created Time: 2017/02/21
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_VECTOR3_H
10 #define CUBBYFLOW_VECTOR3_H
11 
12 #include <Core/Vector/Vector2.h>
13 
14 #include <limits>
15 
16 namespace CubbyFlow
17 {
25  template <typename T>
26  class Vector<T, 3> 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 
40  // MARK: Constructors
42  constexpr Vector() : x(0), y(0), z(0)
43  {
44  // Do nothing
45  }
46 
48  constexpr Vector(T _x, T _y, T _z) : x(_x), y(_y), z(_z)
49  {
50  // Do nothing
51  }
52 
54  constexpr Vector(const Vector2<T>& v, T _z) : x(v.x), y(v.y), z(_z)
55  {
56  // Do nothing
57  }
58 
60  template <typename U>
61  Vector(const std::initializer_list<U>& list);
62 
64  constexpr Vector(const Vector& v) : x(v.x), y(v.y), z(v.z)
65  {
66  // Do nothing
67  }
68 
69  // MARK: Basic setters
71  void Set(T s);
72 
74  void Set(T x, T y, T z);
75 
77  void Set(const Vector2<T>& pt, T z);
78 
80  template <typename U>
81  void Set(const std::initializer_list<U>& list);
82 
84  void Set(const Vector& v);
85 
87  void SetZero();
88 
90  void Normalize();
91 
92  // MARK: Binary operations: new instance = this (+) v
94  Vector Add(T v) const;
95 
97  Vector Add(const Vector& v) const;
98 
100  Vector Sub(T v) const;
101 
103  Vector Sub(const Vector& v) const;
104 
106  Vector Mul(T v) const;
107 
109  Vector Mul(const Vector& v) const;
110 
112  Vector Div(T v) const;
113 
115  Vector Div(const Vector& v) const;
116 
118  T Dot(const Vector& v) const;
119 
121  Vector Cross(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 
137  Vector RCross(const Vector& v) const;
138 
139  // MARK: Augmented operations: this (+)= v
141  void IAdd(T v);
142 
144  void IAdd(const Vector& v);
145 
147  void ISub(T v);
148 
150  void ISub(const Vector& v);
151 
153  void IMul(T v);
154 
156  void IMul(const Vector& v);
157 
159  void IDiv(T v);
160 
162  void IDiv(const Vector& v);
163 
164  // MARK: Basic getters
166  const T& At(size_t i) const;
167 
169  T& At(size_t i);
170 
172  T Sum() const;
173 
175  T Avg() const;
176 
178  T Min() const;
179 
181  T Max() const;
182 
184  T AbsMin() const;
185 
187  T AbsMax() const;
188 
190  size_t DominantAxis() const;
191 
193  size_t SubdominantAxis() const;
194 
196  Vector Normalized() const;
197 
199  T Length() const;
200 
202  T LengthSquared() const;
203 
205  T DistanceTo(const Vector& other) const;
206 
208  T DistanceSquaredTo(const Vector& other) const;
209 
211  Vector Reflected(const Vector& normal) const;
212 
214  Vector Projected(const Vector& normal) const;
215 
217  std::tuple<Vector<T, 3>, Vector<T, 3>> Tangential() const;
218 
220  template <typename U>
221  Vector<U, 3> CastTo() const;
222 
224  bool IsEqual(const Vector& other) const;
225 
227  bool IsSimilar(const Vector& other, T epsilon = std::numeric_limits<T>::epsilon()) const;
228 
229  // MARK: Operators
231  T& operator[](size_t i);
232 
234  const T& operator[](size_t i) const;
235 
237  template <typename U>
238  Vector& operator=(const std::initializer_list<U>& list);
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  Vector& operator*=(T v);
257 
259  Vector& operator*=(const Vector& v);
260 
262  Vector& operator/=(T v);
263 
265  Vector& operator/=(const Vector& v);
266 
268  bool operator==(const Vector& v) const;
269 
271  bool operator!=(const Vector& v) const;
272  };
273 
275  template <typename T> using Vector3 = Vector<T, 3>;
276 
278  template <typename T>
280 
282  template <typename T>
284 
286  template <typename T>
287  Vector<T, 3> operator+(T a, const Vector<T, 3>& b);
288 
290  template <typename T>
291  Vector<T, 3> operator+(const Vector<T, 3>& a, const Vector<T, 3>& b);
292 
294  template <typename T>
295  Vector<T, 3> operator-(const Vector<T, 3>& a, T b);
296 
298  template <typename T>
299  Vector<T, 3> operator-(T a, const Vector<T, 3>& b);
300 
302  template <typename T>
303  Vector<T, 3> operator-(const Vector<T, 3>& a, const Vector<T, 3>& b);
304 
306  template <typename T>
307  Vector<T, 3> operator*(const Vector<T, 3>& a, T b);
308 
310  template <typename T>
311  Vector<T, 3> operator*(T a, const Vector<T, 3>& b);
312 
314  template <typename T>
315  Vector<T, 3> operator*(const Vector<T, 3>& a, const Vector<T, 3>& b);
316 
318  template <typename T>
319  Vector<T, 3> operator/(const Vector<T, 3>& a, T b);
320 
322  template <typename T>
323  Vector<T, 3> operator/(T a, const Vector<T, 3>& b);
324 
326  template <typename T>
327  Vector<T, 3> operator/(const Vector<T, 3>& a, const Vector<T, 3>& b);
328 
330  template <typename T>
331  Vector<T, 3> Min(const Vector<T, 3>& a, const Vector<T, 3>& b);
332 
334  template <typename T>
335  Vector<T, 3> Max(const Vector<T, 3>& a, const Vector<T, 3>& b);
336 
338  template <typename T>
339  Vector<T, 3> Clamp(const Vector<T, 3>& v, const Vector<T, 3>& low, const Vector<T, 3>& high);
340 
342  template <typename T>
343  Vector<T, 3> Ceil(const Vector<T, 3>& a);
344 
346  template <typename T>
347  Vector<T, 3> Floor(const Vector<T, 3>& a);
348 
351 
354 
355  // MARK: Extensions
357  template <>
359  {
360  return Vector3F(0.f, 0.f, 0.f);
361  }
362 
364  template <>
366  {
367  return Vector3D(0.0, 0.0, 0.0);
368  }
369 
371  template <typename T>
372  struct ScalarType<Vector<T, 3>>
373  {
374  typedef T value;
375  };
376 
378  template <typename T>
380  const Vector3<T>& v0,
381  const Vector3<T>& v1,
382  const Vector3<T>& v2,
383  const Vector3<T>& v3,
384  T f);
385 }
386 
388 
389 #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
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
Vector3< float > Vector3F
Float-type 3D vector.
Definition: Vector3.h:350
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
constexpr Vector3D Zero< Vector3D >()
Returns double-type zero vector.
Definition: Vector3.h:365
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 Vector2< T > &v, T _z)
Constructs vector with a 2-D vector and a scalar.
Definition: Vector3.h:54
Generic statically-sized N-D vector class.
Definition: Vector.h:33
T value
Definition: Vector3.h:374
Vector()
Constructs a vector with zeros.
Definition: Vector-Impl.h:17
constexpr Vector3F Zero< Vector3F >()
Returns float-type zero vector.
Definition: Vector3.h:358
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
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
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
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
constexpr Vector(T _x, T _y, T _z)
Constructs vector with given parameters _x, _y, and _z.
Definition: Vector3.h:48
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
constexpr Vector(const Vector &v)
Copy constructor.
Definition: Vector3.h:64
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
constexpr Vector()
Constructs default vector (0, 0, 0).
Definition: Vector3.h:42
Vector3< double > Vector3D
Double-type 3D vector.
Definition: Vector3.h:353
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