Point2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Point2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 2-D point class.
6 > Created Time: 2017/02/01
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_POINT2_H
10 #define CUBBYFLOW_POINT2_H
11 
12 #include <Core/Point/Point.h>
13 #include <Core/Utils/Constants.h>
14 
15 namespace CubbyFlow
16 {
24  template <typename T>
25  class Point<T, 2> final
26  {
27  public:
28  static_assert(std::is_arithmetic<T>::value, "Point only can be instantiated with arithmetic types");
29 
31  T x;
32 
34  T y;
35 
36  // MARK: Constructors
38  constexpr Point() : x(0), y(0)
39  {
40  // Do nothing
41  }
42 
44  constexpr Point(T _x, T _y) : x(_x), y(_y)
45  {
46  // Do nothing
47  }
48 
50  template <typename U>
51  Point(const std::initializer_list<U>& list);
52 
54  constexpr Point(const Point& pt) : x(pt.x), y(pt.y)
55  {
56  // Do nothing
57  }
58 
59  // MARK: Basic setters
61  void Set(T s);
62 
64  void Set(T x, T y);
65 
67  template <typename U>
68  void Set(const std::initializer_list<U>& list);
69 
71  void Set(const Point& pt);
72 
74  void SetZero();
75 
76  // MARK: Binary operations: new instance = this (+) v
78  Point Add(T v) const;
79 
81  Point Add(const Point& v) const;
82 
84  Point Sub(T v) const;
85 
87  Point Sub(const Point& v) const;
88 
90  Point Mul(T v) const;
91 
93  Point Mul(const Point& v) const;
94 
96  Point Div(T v) const;
97 
99  Point Div(const Point& v) const;
100 
101  // MARK: Binary operations: new instance = v (+) this
103  Point RAdd(T v) const;
104 
106  Point RAdd(const Point& v) const;
107 
109  Point RSub(T v) const;
110 
112  Point RSub(const Point& v) const;
113 
115  Point RMul(T v) const;
116 
118  Point RMul(const Point& v) const;
119 
121  Point RDiv(T v) const;
122 
124  Point RDiv(const Point& v) const;
125 
126  // MARK: Augmented operations: this (+)= v
128  void IAdd(T v);
129 
131  void IAdd(const Point& v);
132 
134  void ISub(T v);
135 
137  void ISub(const Point& v);
138 
140  void IMul(T v);
141 
143  void IMul(const Point& v);
144 
146  void IDiv(T v);
147 
149  void IDiv(const Point& v);
150 
151  // MARK: Basic getters
153  const T& At(size_t i) const;
154 
156  T& At(size_t i);
157 
159  T Sum() const;
160 
162  T Min() const;
163 
165  T Max() const;
166 
168  T AbsMin() const;
169 
171  T AbsMax() const;
172 
174  size_t DominantAxis() const;
175 
177  size_t SubdominantAxis() const;
178 
180  template <typename U>
181  Point<U, 2> CastTo() const;
182 
184  bool IsEqual(const Point& other) const;
185 
186  // MARK: Operators
188  T& operator[](size_t i);
189 
191  const T& operator[](size_t i) const;
192 
194  Point& operator=(const std::initializer_list<T>& list);
195 
197  Point& operator=(const Point& v);
198 
200  Point& operator+=(T v);
201 
203  Point& operator+=(const Point& v);
204 
206  Point& operator-=(T v);
207 
209  Point& operator-=(const Point& v);
210 
212  Point& operator*=(T v);
213 
215  Point& operator*=(const Point& v);
216 
218  Point& operator/=(T v);
219 
221  Point& operator/=(const Point& v);
222 
224  bool operator==(const Point& v) const;
225 
227  bool operator!=(const Point& v) const;
228  };
229 
231  template <typename T>
232  Point<T, 2> operator+(const Point<T, 2>& a);
233 
235  template <typename T>
236  Point<T, 2> operator-(const Point<T, 2>& a);
237 
239  template <typename T>
240  Point<T, 2> operator+(T a, const Point<T, 2>& b);
241 
243  template <typename T>
244  Point<T, 2> operator+(const Point<T, 2>& a, const Point<T, 2>& b);
245 
247  template <typename T>
248  Point<T, 2> operator-(const Point<T, 2>& a, T b);
249 
251  template <typename T>
252  Point<T, 2> operator-(T a, const Point<T, 2>& b);
253 
255  template <typename T>
256  Point<T, 2> operator-(const Point<T, 2>& a, const Point<T, 2>& b);
257 
259  template <typename T>
260  Point<T, 2> operator*(const Point<T, 2>& a, T b);
261 
263  template <typename T>
264  Point<T, 2> operator*(T a, const Point<T, 2>& b);
265 
267  template <typename T>
268  Point<T, 2> operator*(const Point<T, 2>& a, const Point<T, 2>& b);
269 
271  template <typename T>
272  Point<T, 2> operator/(const Point<T, 2>& a, T b);
273 
275  template <typename T>
276  Point<T, 2> operator/(T a, const Point<T, 2>& b);
277 
279  template <typename T>
280  Point<T, 2> operator/(const Point<T, 2>& a, const Point<T, 2>& b);
281 
283  template <typename T>
284  Point<T, 2> Min(const Point<T, 2>& a, const Point<T, 2>& b);
285 
287  template <typename T>
288  Point<T, 2> Max(const Point<T, 2>& a, const Point<T, 2>& b);
289 
291  template <typename T>
292  Point<T, 2> Clamp(const Point<T, 2>& v, const Point<T, 2>& low, const Point<T, 2>& high);
293 
295  template <typename T>
296  Point<T, 2> Ceil(const Point<T, 2>& a);
297 
299  template <typename T>
300  Point<T, 2> Floor(const Point<T, 2>& a);
301 
303  template <typename T> using Point2 = Point<T, 2>;
304 
307 
310 
313 
316 }
317 
318 #include <Core/Point/Point2-Impl.h>
319 
320 #endif
T AbsMin(T x, T y)
Returns the absolute minimum value among the two inputs.
Definition: MathUtils-Impl.h:39
Point< T, 2 > Ceil(const Point< T, 2 > &a)
Returns element-wise ceiled point.
Definition: Point2-Impl.h:492
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
T x
X (or the first) component of the point.
Definition: Point2.h:28
void Set(const std::initializer_list< U > &list)
Set point instance with initializer list.
Definition: Point-Impl.h:57
2-D point class.
Definition: Point2.h:25
T y
Y (or the second) component of the point.
Definition: Point2.h:34
constexpr Point()
Constructs default point (0, 0).
Definition: Point2.h:38
Point< T, 2 > Floor(const Point< T, 2 > &a)
Returns element-wise floored point.
Definition: Point2-Impl.h:498
Generic N-D point class.
Definition: Point.h:24
Matrix< T, 2, 2 > operator/(const Matrix< T, 2, 2 > &a, T b)
Definition: Matrix2x2-Impl.h:720
Definition: pybind11Utils.h:24
T Clamp(T val, T low, T high)
Returns the clamped value.
Definition: MathUtils-Impl.h:123
Point & operator=(const std::initializer_list< U > &list)
Set point instance with initializer list.
Matrix< T, 2, 2 > operator-(const Matrix< T, 2, 2 > &a)
Returns a matrix with opposite sign.
Definition: Matrix2x2-Impl.h:654
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
const T & operator[](size_t i) const
Returns the const reference to the i -th element.
Definition: Point-Impl.h:91
Vector< T, 3 > operator*(const Quaternion< T > &q, const Vector< T, 3 > &v)
Returns quaternion q * vector v.
Definition: Quaternion-Impl.h:481
T AbsMax(T x, T y)
Returns the absolute maximum value among the two inputs.
Definition: MathUtils-Impl.h:45
constexpr Point(const Point &pt)
Copy constructor.
Definition: Point2.h:54
Point()
Constructs a point with zeros.
Definition: Point-Impl.h:17
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
constexpr Point(T _x, T _y)
Constructs point with given parameters _x and _y.
Definition: Point2.h:44