Jet  v1.3.3
vector2.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_VECTOR2_H_
8 #define INCLUDE_JET_VECTOR2_H_
9 
10 #include <jet/vector.h>
11 #include <algorithm> // just make cpplint happy..
12 #include <limits>
13 
14 namespace jet {
15 
23 template <typename T>
24 class Vector<T, 2> final {
25  public:
26  static_assert(std::is_floating_point<T>::value,
27  "Vector only can be instantiated with floating point types");
28 
30  T x;
31 
33  T y;
34 
35  // MARK: Constructors
36 
38  constexpr Vector() : x(0), y(0) {}
39 
41  constexpr Vector(T x_, T y_) : x(x_), y(y_) {}
42 
44  template <typename U>
45  Vector(const std::initializer_list<U>& lst);
46 
48  constexpr Vector(const Vector& v) : x(v.x), y(v.y) {}
49 
50  // MARK: Basic setters
51 
53  void set(T s);
54 
56  void set(T x, T y);
57 
59  template <typename U>
60  void set(const std::initializer_list<U>& lst);
61 
63  void set(const Vector& pt);
64 
66  void setZero();
67 
69  void normalize();
70 
71  // MARK: Binary operations: new instance = this (+) v
72 
74  Vector add(T v) const;
75 
77  Vector add(const Vector& v) const;
78 
80  Vector sub(T v) const;
81 
83  Vector sub(const Vector& v) const;
84 
86  Vector mul(T v) const;
87 
89  Vector mul(const Vector& v) const;
90 
92  Vector div(T v) const;
93 
95  Vector div(const Vector& v) const;
96 
98  T dot(const Vector& v) const;
99 
101  T cross(const Vector& v) const;
102 
103  // MARK: Binary operations: new instance = v (+) this
104 
106  Vector rsub(T v) const;
107 
109  Vector rsub(const Vector& v) const;
110 
112  Vector rdiv(T v) const;
113 
115  Vector rdiv(const Vector& v) const;
116 
118  T rcross(const Vector& v) const;
119 
120  // MARK: Augmented operations: this (+)= v
121 
123  void iadd(T v);
124 
126  void iadd(const Vector& v);
127 
129  void isub(T v);
130 
132  void isub(const Vector& v);
133 
135  void imul(T v);
136 
138  void imul(const Vector& v);
139 
141  void idiv(T v);
142 
144  void idiv(const Vector& v);
145 
146  // MARK: Basic getters
147 
149  const T& at(size_t i) const;
150 
152  T& at(size_t i);
153 
155  T sum() const;
156 
158  T avg() const;
159 
161  T min() const;
162 
164  T max() const;
165 
167  T absmin() const;
168 
170  T absmax() const;
171 
173  size_t dominantAxis() const;
174 
176  size_t subminantAxis() const;
177 
180 
182  T length() const;
183 
185  T lengthSquared() const;
186 
188  T distanceTo(const Vector& other) const;
189 
191  T distanceSquaredTo(const Vector& other) const;
192 
194  Vector reflected(const Vector& normal) const;
195 
197  Vector projected(const Vector& normal) const;
198 
201 
203  template <typename U>
205 
207  bool isEqual(const Vector& other) const;
208 
210  bool isSimilar(const Vector& other,
211  T epsilon = std::numeric_limits<T>::epsilon()) const;
212 
213  // MARK: Operators
214 
216  T& operator[](size_t i);
217 
219  const T& operator[](size_t i) const;
220 
222  template <typename U>
223  Vector& operator=(const std::initializer_list<U>& lst);
224 
226  Vector& operator=(const Vector& v);
227 
230 
233 
236 
239 
242 
245 
248 
251 
253  bool operator==(const Vector& v) const;
254 
256  bool operator!=(const Vector& v) const;
257 };
258 
260 template <typename T>
262 
264 template <typename T>
266 
268 template <typename T>
270 
272 template <typename T>
274 
276 template <typename T>
278 
280 template <typename T>
282 
284 template <typename T>
286 
288 template <typename T>
290 
292 template <typename T>
294 
296 template <typename T>
298 
300 template <typename T>
302 
304 template <typename T>
306 
308 template <typename T>
310 
312 template <typename T>
314 
316 template <typename T>
317 Vector2<T> min(const Vector2<T>& a, const Vector2<T>& b);
318 
320 template <typename T>
321 Vector2<T> max(const Vector2<T>& a, const Vector2<T>& b);
322 
324 template <typename T>
325 Vector2<T> clamp(const Vector2<T>& v, const Vector2<T>& low,
326  const Vector2<T>& high);
327 
329 template <typename T>
331 
333 template <typename T>
335 
338 
341 
342 // MARK: Extensions
343 
345 template <>
347  return Vector2F(0.f, 0.f);
348 }
349 
351 template <>
353  return Vector2D(0.0, 0.0);
354 }
355 
357 template <typename T>
358 struct ScalarType<Vector2<T>> {
359  typedef T value;
360 };
361 
363 template <typename T>
365  const Vector2<T>& v2, const Vector2<T>& v3, T f);
366 
367 } // namespace jet
368 
369 #include "detail/vector2-inl.h"
370 
371 #endif // INCLUDE_JET_VECTOR2_H_
jet::Vector< T, 2 >::Vector
Vector(const std::initializer_list< U > &lst)
Constructs vector with initializer list.
jet::Vector< T, 2 >::rcross
T rcross(const Vector &v) const
Computes v cross this.
jet::Vector< T, 2 >::operator+=
Vector & operator+=(const Vector &v)
Computes this += (v.x, v.y)
jet::Vector< T, 2 >::distanceSquaredTo
T distanceSquaredTo(const Vector &other) const
Returns the squared distance to the other vector.
jet::ceil
Point2< T > ceil(const Point2< T > &a)
Returns element-wise ceiled point.
jet::Vector< T, 2 >::min
T min() const
Returns the minimum value among x and y.
jet::Vector< T, 2 >::operator!=
bool operator!=(const Vector &v) const
Returns true if other is the not same as this vector.
jet::Vector< T, 2 >::operator/=
Vector & operator/=(T v)
Computes this /= (v, v)
jet::Vector< T, 2 >::add
Vector add(const Vector &v) const
Computes this + (v.x, v.y).
jet::Vector< T, 2 >::absmin
T absmin() const
Returns the absolute minimum value among x and y.
jet::Vector< T, 2 >::x
T x
X (or the first) component of the vector.
Definition: vector2.h:27
jet::Vector< T, 2 >::rsub
Vector rsub(const Vector &v) const
Computes (v.x, v.y) - this.
jet::Vector< T, 2 >::sub
Vector sub(const Vector &v) const
Computes this - (v.x, v.y).
jet::Vector< T, 2 >::operator*=
Vector & operator*=(T v)
Computes this *= (v, v)
jet::Vector< T, 2 >::rsub
Vector rsub(T v) const
Computes (v, v) - this.
jet::Vector< T, 2 >::castTo
Vector< U, 2 > castTo() const
Returns a vector with different value type.
jet::Vector2D
Vector2< double > Vector2D
Double-type 2D vector.
Definition: vector2.h:340
jet::Vector< T, 2 >::set
void set(const std::initializer_list< U > &lst)
Set x and y components with given initializer list.
jet::Vector< T, 2 >::rdiv
Vector rdiv(const Vector &v) const
Computes (v.x, v.y) / this.
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, 2 >::operator[]
T & operator[](size_t i)
Returns reference to the i -th element of the vector.
jet::Vector< T, 2 >::operator==
bool operator==(const Vector &v) const
Returns true if other is the same as this vector.
jet::Vector< T, 2 >::add
Vector add(T v) const
Computes this + (v, v).
jet::Vector< T, 2 >::Vector
constexpr Vector(const Vector &v)
Copy constructor.
Definition: vector2.h:48
jet::Vector< T, 2 >::normalize
void normalize()
Normalizes this vector.
jet::Vector< T, 2 >::tangential
Vector tangential() const
Returns the tangential vector for this vector.
jet::Vector< T, 2 >::Vector
constexpr Vector()
Constructs default vector (0, 0).
Definition: vector2.h:38
jet::Vector< T, 2 >::idiv
void idiv(T v)
Computes this /= (v, v).
jet::Vector< T, 2 >::isub
void isub(const Vector &v)
Computes this -= (v.x, v.y).
jet::Vector< T, 2 >::setZero
void setZero()
Set both x and y to zero.
jet::Vector< T, 2 >::imul
void imul(const Vector &v)
Computes this *= (v.x, v.y).
jet::Vector< T, 2 >::absmax
T absmax() const
Returns the absolute maximum value among x and y.
jet::Vector< T, 2 >::operator-=
Vector & operator-=(const Vector &v)
Computes this -= (v.x, v.y)
jet::Vector< T, 2 >::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, 2 >::at
T & at(size_t i)
Returns reference to the i -th element of the vector.
jet::Vector< T, 2 >::operator+=
Vector & operator+=(T v)
Computes this += (v, v)
jet::Vector
Generic statically-sized N-D vector class.
Definition: vector.h:31
jet::Vector< T, 2 >::operator/=
Vector & operator/=(const Vector &v)
Computes this /= (v.x, v.y)
jet::Vector< T, 2 >::div
Vector div(T v) const
Computes this / (v, v).
jet::Vector< T, 2 >::operator=
Vector & operator=(const std::initializer_list< U > &lst)
Set x and y components with given initializer list.
jet::Vector< T, 2 >::sum
T sum() const
Returns the sum of all the components (i.e. x + y).
jet::Vector< T, 2 >::sub
Vector sub(T v) const
Computes this - (v, v).
jet::Vector< T, 2 >::Vector
constexpr Vector(T x_, T y_)
Constructs vector with given parameters x_ and y_.
Definition: vector2.h:41
jet::Vector< T, 2 >::set
void set(T x, T y)
Set x and y components with given parameters.
jet::Vector< T, 2 >::length
T length() const
Returns the length of the vector.
jet::Vector< T, 2 >::operator=
Vector & operator=(const Vector &v)
Set x and y with other vector pt.
jet::Vector< T, 2 >::operator-=
Vector & operator-=(T v)
Computes this -= (v, v)
jet
Definition: advection_solver2.h:18
jet::Vector< T, 2 >::normalized
Vector normalized() const
Returns normalized vector.
jet::Vector< T, 2 >::reflected
Vector reflected(const Vector &normal) const
Returns the reflection vector to the surface with given surface normal.
jet::Vector< T, 2 >::iadd
void iadd(T v)
Computes this += (v, v).
jet::Vector< T, 2 >::div
Vector div(const Vector &v) const
Computes this / (v.x, v.y).
jet::Vector< T, 2 >::y
T y
Y (or the second) component of the vector.
Definition: vector2.h:33
jet::Vector< T, 2 >::isub
void isub(T v)
Computes this -= (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, 2 >::imul
void imul(T v)
Computes this *= (v, v).
jet::operator+
Matrix2x2< T > operator+(const Matrix2x2< T > &a, const Matrix2x2< T > &b)
Returns a + b (element-size).
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, 2 >::mul
Vector mul(T v) const
Computes this * (v, v).
jet::Vector< T, 2 >::mul
Vector mul(const Vector &v) const
Computes this * (v.x, v.y).
jet::Vector< T, 2 >::lengthSquared
T lengthSquared() const
Returns the squared length of the vector.
jet::Vector< T, 2 >::distanceTo
T distanceTo(const Vector &other) const
Returns the distance to the other vector.
jet::Vector< T, 2 >::operator*=
Vector & operator*=(const Vector &v)
Computes this *= (v.x, v.y)
jet::Vector< T, 2 >::iadd
void iadd(const Vector &v)
Computes this += (v.x, v.y).
jet::Vector< T, 2 >::max
T max() const
Returns the maximum value among x and y.
jet::Vector< T, 2 >::set
void set(const Vector &pt)
Set x and y with other vector pt.
jet::Vector< T, 2 >::set
void set(T s)
Set both x and y components to s.
jet::Vector< T, 2 >::projected
Vector projected(const Vector &normal) const
Returns the projected vector to the surface with given surface normal.
jet::Vector< T, 2 >::idiv
void idiv(const Vector &v)
Computes this /= (v.x, v.y).
jet::Vector< T, 2 >::rdiv
Vector rdiv(T v) const
Computes (v, v) / this.
jet::Vector< T, 2 >::operator[]
const T & operator[](size_t i) const
Returns const reference to the i -th element of the vector.
vector.h
jet::Vector< T, 2 >::dominantAxis
size_t dominantAxis() const
Returns the index of the dominant axis.
jet::Vector2F
Vector2< float > Vector2F
Float-type 2D vector.
Definition: vector2.h:337
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, 2 >::isEqual
bool isEqual(const Vector &other) const
Returns true if other is the same as this vector.
jet::Vector< T, 2 >::at
const T & at(size_t i) const
Returns const reference to the i -th element of the vector.
jet::Vector< T, 2 >::subminantAxis
size_t subminantAxis() const
Returns the index of the subminant axis.
jet::operator-
Matrix2x2< T > operator-(const Matrix2x2< T > &a)
Returns a matrix with opposite sign.
jet::zero< Vector2D >
constexpr Vector2D zero< Vector2D >()
Returns double-type zero vector.
Definition: vector2.h:352
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, 2 >::cross
T cross(const Vector &v) const
Comptues cross product.
jet::zero< Vector2F >
constexpr Vector2F zero< Vector2F >()
Returns float-type zero vector.
Definition: vector2.h:346
jet::Vector< T, 2 >::dot
T dot(const Vector &v) const
Computes dot product.
jet::ScalarType< Vector2< T > >::value
T value
Definition: vector2.h:359
jet::Vector< T, 2 >::avg
T avg() const
Returns the average of all the components.
jet::ScalarType
Returns the type of the value itself.
Definition: type_helpers.h:14