Jet  v1.3.3
vector3.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_VECTOR3_H_
8 #define INCLUDE_JET_VECTOR3_H_
9 
10 #include <jet/vector2.h>
11 #include <algorithm> // just make cpplint happy..
12 #include <limits>
13 #include <tuple>
14 
15 namespace jet {
16 
24 template <typename T>
25 class Vector<T, 3> final {
26  public:
27  static_assert(std::is_floating_point<T>::value,
28  "Vector only can be instantiated with floating point types");
29 
31  T x;
32 
34  T y;
35 
37  T z;
38 
39  // MARK: Constructors
40 
42  constexpr Vector() : x(0), y(0), z(0) {}
43 
45  constexpr Vector(T x_, T y_, T z_) : x(x_), y(y_), z(z_) {}
46 
48  constexpr Vector(const Vector2<T>& v, T z_) : x(v.x), y(v.y), z(z_) {}
49 
51  template <typename U>
52  Vector(const std::initializer_list<U>& lst);
53 
55  constexpr Vector(const Vector& v) : x(v.x), y(v.y), z(v.z) {}
56 
57  // MARK: Basic setters
58 
60  void set(T s);
61 
63  void set(T x, T y, T z);
64 
66  void set(const Vector2<T>& pt, T z);
67 
69  template <typename U>
70  void set(const std::initializer_list<U>& lst);
71 
73  void set(const Vector& v);
74 
76  void setZero();
77 
79  void normalize();
80 
81  // MARK: Binary operations: new instance = this (+) v
82 
84  Vector add(T v) const;
85 
87  Vector add(const Vector& v) const;
88 
90  Vector sub(T v) const;
91 
93  Vector sub(const Vector& v) const;
94 
96  Vector mul(T v) const;
97 
99  Vector mul(const Vector& v) const;
101  Vector div(T v) const;
102 
104  Vector div(const Vector& v) const;
105 
107  T dot(const Vector& v) const;
108 
110  Vector cross(const Vector& v) const;
111 
112  // MARK: Binary operations: new instance = v (+) this
113 
115  Vector rsub(T v) const;
116 
118  Vector rsub(const Vector& v) const;
119 
121  Vector rdiv(T v) const;
122 
124  Vector rdiv(const Vector& v) const;
125 
127  Vector rcross(const Vector& v) const;
128 
129  // MARK: Augmented operations: this (+)= v
130 
132  void iadd(T v);
133 
135  void iadd(const Vector& v);
136 
138  void isub(T v);
139 
141  void isub(const Vector& v);
142 
144  void imul(T v);
145 
147  void imul(const Vector& v);
148 
150  void idiv(T v);
151 
153  void idiv(const Vector& v);
154 
155  // MARK: Basic getters
156 
158  const T& at(size_t i) const;
159 
161  T& at(size_t i);
162 
164  T sum() const;
165 
167  T avg() const;
168 
170  T min() const;
171 
173  T max() const;
174 
176  T absmin() const;
177 
179  T absmax() const;
180 
182  size_t dominantAxis() const;
183 
185  size_t subminantAxis() const;
186 
189 
191  T length() const;
192 
194  T lengthSquared() const;
195 
197  T distanceTo(const Vector& other) const;
198 
200  T distanceSquaredTo(const Vector& other) const;
201 
203  Vector reflected(const Vector& normal) const;
204 
206  Vector projected(const Vector& normal) const;
207 
209  std::tuple<Vector, Vector> tangential() const;
210 
212  template <typename U>
214 
216  bool isEqual(const Vector& other) const;
217 
219  bool isSimilar(const Vector& other,
220  T epsilon = std::numeric_limits<T>::epsilon()) const;
221 
222  // MARK: Operators
223 
225  T& operator[](size_t i);
226 
228  const T& operator[](size_t i) const;
229 
231  template <typename U>
232  Vector& operator=(const std::initializer_list<U>& lst);
233 
235  Vector& operator=(const Vector& v);
236 
239 
242 
245 
248 
251 
254 
257 
260 
262  bool operator==(const Vector& v) const;
263 
265  bool operator!=(const Vector& v) const;
266 };
267 
269 template <typename T>
271 
273 template <typename T>
275 
277 template <typename T>
279 
281 template <typename T>
283 
285 template <typename T>
287 
289 template <typename T>
291 
293 template <typename T>
295 
297 template <typename T>
299 
301 template <typename T>
303 
305 template <typename T>
307 
309 template <typename T>
311 
313 template <typename T>
315 
317 template <typename T>
319 
321 template <typename T>
323 
325 template <typename T>
326 Vector3<T> min(const Vector3<T>& a, const Vector3<T>& b);
327 
329 template <typename T>
330 Vector3<T> max(const Vector3<T>& a, const Vector3<T>& b);
331 
333 template <typename T>
334 Vector3<T> clamp(const Vector3<T>& v, const Vector3<T>& low,
335  const Vector3<T>& high);
336 
338 template <typename T>
340 
342 template <typename T>
344 
347 
350 
351 // MARK: Extensions
352 
354 template <>
356  return Vector3F(0.f, 0.f, 0.f);
357 }
358 
360 template <>
362  return Vector3D(0.0, 0.0, 0.0);
363 }
364 
366 template <typename T>
367 struct ScalarType<Vector3<T>> {
368  typedef T value;
369 };
370 
372 template <typename T>
374  const Vector3<T>& v2, const Vector3<T>& v3, T f);
375 
376 } // namespace jet
377 
378 #include "detail/vector3-inl.h"
379 
380 #endif // INCLUDE_JET_VECTOR3_H_
jet::Vector< T, 3 >::castTo
Vector< U, 3 > castTo() const
Returns a vector with different value type.
jet::Vector< T, 3 >::set
void set(T s)
Set all x, y, and z components to s.
jet::Vector< T, 3 >::sub
Vector sub(T v) const
Computes this - (v, v, v).
jet::Vector< T, 3 >::rsub
Vector rsub(const Vector &v) const
Computes (v.x, v.y, v.z) - this.
jet::ceil
Point2< T > ceil(const Point2< T > &a)
Returns element-wise ceiled point.
jet::Vector< T, 3 >::isub
void isub(const Vector &v)
Computes this -= (v.x, v.y, v.z).
jet::Vector< T, 3 >::projected
Vector projected(const Vector &normal) const
Returns the projected vector to the surface with given surface normal.
jet::Vector< T, 3 >::operator=
Vector & operator=(const Vector &v)
Set x and y with other vector pt.
jet::Vector< T, 3 >::iadd
void iadd(const Vector &v)
Computes this += (v.x, v.y, v.z).
jet::Vector< T, 3 >::distanceTo
T distanceTo(const Vector &other) const
Returns the distance to the other vector.
jet::Vector< T, 3 >::operator[]
T & operator[](size_t i)
Returns reference to the i -th element of the vector.
jet::Vector< T, 3 >::rsub
Vector rsub(T v) const
Computes (v, v, v) - this.
jet::Vector< T, 3 >::sub
Vector sub(const Vector &v) const
Computes this - (v.x, v.y, v.z).
jet::max
Point2< T > max(const Point2< T > &a, const Point2< T > &b)
Returns element-wise max point: (max(a.x, b.x), max(a.y, b.y)).
jet::Vector< T, 3 >::operator+=
Vector & operator+=(const Vector &v)
Computes this += (v.x, v.y, v.z)
jet::Vector< T, 3 >::Vector
Vector(const std::initializer_list< U > &lst)
Constructs vector with initializer list.
jet::Vector< T, 3 >::absmin
T absmin() const
Returns the absolute minimum value among x, y, and z.
jet::Vector< T, 3 >::at
const T & at(size_t i) const
Returns const reference to the i -th element of the vector.
jet::Vector< T, 3 >::sum
T sum() const
Returns the sum of all the components (i.e. x + y + z).
jet::Vector< T, 3 >::Vector
constexpr Vector(const Vector &v)
Copy constructor.
Definition: vector3.h:55
jet::Vector< T, 3 >::y
T y
Y (or the second) component of the vector.
Definition: vector3.h:34
jet::Vector< T, 3 >::operator+=
Vector & operator+=(T v)
Computes this += (v, v, v)
jet::Vector< T, 3 >::operator*=
Vector & operator*=(const Vector &v)
Computes this *= (v.x, v.y, v.z)
jet::Vector< T, 3 >::add
Vector add(T v) const
Computes this + (v, v, v).
jet::Vector< T, 3 >::setZero
void setZero()
Set all x, y, and z to zero.
jet::Vector< T, 3 >::at
T & at(size_t i)
Returns reference to the i -th element of the vector.
jet::Vector< T, 3 >::avg
T avg() const
Returns the average of all the components.
jet::Vector< T, 3 >::dot
T dot(const Vector &v) const
Computes dot product.
jet::Vector< T, 3 >::cross
Vector cross(const Vector &v) const
Comptues cross product.
jet::Vector
Generic statically-sized N-D vector class.
Definition: vector.h:31
jet::Vector< T, 3 >::isub
void isub(T v)
Computes this -= (v, v, v).
jet::Vector< T, 3 >::isSimilar
bool isSimilar(const Vector &other, T epsilon=std::numeric_limits< T >::epsilon()) const
Returns true if other is similar to this vector.
jet::Vector< T, 3 >::operator==
bool operator==(const Vector &v) const
Returns true if other is the same as this vector.
jet::Vector< T, 3 >::tangential
std::tuple< Vector, Vector > tangential() const
Returns the tangential vector for this vector.
jet::Vector< T, 3 >::mul
Vector mul(T v) const
Computes this * (v, v, v).
jet::ScalarType< Vector3< T > >::value
T value
Definition: vector3.h:368
jet
Definition: advection_solver2.h:18
jet::Vector< T, 3 >::operator!=
bool operator!=(const Vector &v) const
Returns true if other is the not same as this vector.
jet::Vector< T, 3 >::isEqual
bool isEqual(const Vector &other) const
Returns true if other is the same as this vector.
vector2.h
jet::Vector< T, 3 >::z
T z
Z (or the third) component of the vector.
Definition: vector3.h:37
jet::Vector< T, 3 >::x
T x
X (or the first) component of the vector.
Definition: vector3.h:28
jet::Vector< T, 3 >::operator*=
Vector & operator*=(T v)
Computes this *= (v, v, v)
jet::Vector< T, 3 >::subminantAxis
size_t subminantAxis() const
Returns the index of the subminant axis.
jet::Vector< T, 3 >::dominantAxis
size_t dominantAxis() const
Returns the index of the dominant axis.
jet::Vector< T, 3 >::min
T min() const
Returns the minimum value among x, y, and z.
jet::Vector< T, 3 >::set
void set(const Vector &v)
Set x, y, and z with other vector v.
jet::Vector< T, 3 >::Vector
constexpr Vector()
Constructs default vector (0, 0, 0).
Definition: vector3.h:42
jet::Vector< T, 3 >::operator-=
Vector & operator-=(T v)
Computes this -= (v, v, v)
jet::Vector< T, 2 >
2-D vector class.
Definition: vector2.h:24
jet::clamp
T clamp(T val, T low, T high)
Returns the clamped value.
jet::Vector< T, 3 >::div
Vector div(T v) const
Computes this / (v, v, v).
jet::operator+
Matrix2x2< T > operator+(const Matrix2x2< T > &a, const Matrix2x2< T > &b)
Returns a + b (element-size).
jet::Vector< T, 3 >::operator/=
Vector & operator/=(T v)
Computes this /= (v, v, v)
jet::floor
Point2< T > floor(const Point2< T > &a)
Returns element-wise floored point.
jet::monotonicCatmullRom
T monotonicCatmullRom(const T &f0, const T &f1, const T &f2, const T &f3, T t)
Computes monotonic Catmull-Rom interpolation.
jet::Vector< T, 3 >::operator[]
const T & operator[](size_t i) const
Returns const reference to the i -th element of the vector.
jet::Vector< T, 3 >::set
void set(T x, T y, T z)
Set x, y, and z components with given parameters.
jet::Vector< T, 3 >::idiv
void idiv(T v)
Computes this /= (v, v, v).
jet::Vector< T, 3 >::Vector
constexpr Vector(const Vector2< T > &v, T z_)
Constructs vector with a 2-D vector and a scalar.
Definition: vector3.h:48
jet::Vector< T, 3 >::Vector
constexpr Vector(T x_, T y_, T z_)
Constructs vector with given parameters x_, y_, and z_.
Definition: vector3.h:45
jet::Vector< T, 3 >::operator-=
Vector & operator-=(const Vector &v)
Computes this -= (v.x, v.y, v.z)
jet::Vector< T, 3 >::idiv
void idiv(const Vector &v)
Computes this /= (v.x, v.y, v.z).
jet::Vector< T, 3 >::set
void set(const Vector2< T > &pt, T z)
Set x, y, and z components with given pt.x, pt.y, and z.
jet::Vector< T, 3 >::imul
void imul(const Vector &v)
Computes this *= (v.x, v.y, v.z).
jet::Vector< T, 3 >::iadd
void iadd(T v)
Computes this += (v, v, v).
jet::Vector< T, 3 >::operator/=
Vector & operator/=(const Vector &v)
Computes this /= (v.x, v.y, v.z)
jet::Vector< T, 3 >::normalized
Vector normalized() const
Returns normalized vector.
jet::Vector3D
Vector3< double > Vector3D
Double-type 3D vector.
Definition: vector3.h:349
jet::Vector< T, 3 >::add
Vector add(const Vector &v) const
Computes this + (v.x, v.y, v.z).
jet::Vector< T, 3 >::reflected
Vector reflected(const Vector &normal) const
Returns the reflection vector to the surface with given surface normal.
jet::Vector< T, 3 >
3-D vector class.
Definition: vector3.h:25
jet::Vector< T, 3 >::length
T length() const
Returns the length of the vector.
jet::Vector< T, 3 >::imul
void imul(T v)
Computes this *= (v, v, v).
jet::Vector< T, 3 >::div
Vector div(const Vector &v) const
Computes this / (v.x, v.y, v.z).
jet::operator/
Matrix2x2< T > operator/(const Matrix2x2< T > &a, T b)
Returns a' / b, where every element of matrix a' is a.
jet::min
Point2< T > min(const Point2< T > &a, const Point2< T > &b)
Returns element-wise min point: (min(a.x, b.x), min(a.y, b.y)).
jet::Vector< T, 3 >::distanceSquaredTo
T distanceSquaredTo(const Vector &other) const
Returns the squared distance to the other vector.
jet::Vector< T, 3 >::mul
Vector mul(const Vector &v) const
Computes this * (v.x, v.y, v.z).
jet::Vector< T, 3 >::absmax
T absmax() const
Returns the absolute maximum value among x, y, and z.
jet::operator-
Matrix2x2< T > operator-(const Matrix2x2< T > &a)
Returns a matrix with opposite sign.
jet::Vector< T, 3 >::lengthSquared
T lengthSquared() const
Returns the squared length of the vector.
jet::Vector< T, 3 >::operator=
Vector & operator=(const std::initializer_list< U > &lst)
Set x and y components with given initializer list.
jet::Vector< T, 3 >::normalize
void normalize()
Normalizes this vector.
jet::Vector3F
Vector3< float > Vector3F
Float-type 3D vector.
Definition: vector3.h:346
jet::operator*
Matrix2x2< T > operator*(const Matrix2x2< T > &a, T b)
Returns a * b', where every element of matrix b' is b.
jet::Vector< T, 3 >::rcross
Vector rcross(const Vector &v) const
Computes v cross this.
jet::zero< Vector3D >
constexpr Vector3D zero< Vector3D >()
Returns double-type zero vector.
Definition: vector3.h:361
jet::Vector< T, 3 >::rdiv
Vector rdiv(const Vector &v) const
Computes (v.x, v.y, v.z) / this.
jet::zero< Vector3F >
constexpr Vector3F zero< Vector3F >()
Returns float-type zero vector.
Definition: vector3.h:355
jet::Vector< T, 3 >::max
T max() const
Returns the maximum value among x, y, and z.
jet::Vector< T, 3 >::rdiv
Vector rdiv(T v) const
Computes (v, v, v) / this.
jet::ScalarType
Returns the type of the value itself.
Definition: type_helpers.h:14
jet::Vector< T, 3 >::set
void set(const std::initializer_list< U > &lst)
Set x, y, and z components with given initializer list.