Point2-Impl.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Point2-Impl.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 2-D point class.
6 > Created Time: 2017/02/03
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_POINT2_IMPL_H
10 #define CUBBYFLOW_POINT2_IMPL_H
11 
12 #include <Core/Math/MathUtils.h>
13 
14 #include <cassert>
15 #include <cmath>
16 
17 namespace CubbyFlow
18 {
19  // Constructors
20  template <typename T>
21  template <typename U>
22  Point<T, 2>::Point(const std::initializer_list<U>& list)
23  {
24  Set(list);
25  }
26 
27  // Basic setters
28  template <typename T>
29  void Point<T, 2>::Set(T s)
30  {
31  x = s;
32  y = s;
33  }
34 
35  template <typename T>
36  void Point<T, 2>::Set(T newX, T newY)
37  {
38  x = newX;
39  y = newY;
40  }
41 
42  template <typename T>
43  template <typename U>
44  void Point<T, 2>::Set(const std::initializer_list<U>& list)
45  {
46  assert(list.size() >= 2);
47 
48  auto inputElem = list.begin();
49  x = static_cast<T>(*inputElem);
50  y = static_cast<T>(*(++inputElem));
51  }
52 
53  template <typename T>
54  void Point<T, 2>::Set(const Point& v)
55  {
56  x = v.x;
57  y = v.y;
58  }
59 
60  template <typename T>
62  {
63  x = y = 0;
64  }
65 
66  // Binary operators: new instance = this (+) v
67  template <typename T>
69  {
70  return Point(x + v, y + v);
71  }
72 
73  template <typename T>
75  {
76  return Point(x + v.x, y + v.y);
77  }
78 
79  template <typename T>
81  {
82  return Point(x - v, y - v);
83  }
84 
85  template <typename T>
87  {
88  return Point(x - v.x, y - v.y);
89  }
90 
91  template <typename T>
93  {
94  return Point(x * v, y * v);
95  }
96 
97  template <typename T>
99  {
100  return Point(x * v.x, y * v.y);
101  }
102 
103  template <typename T>
105  {
106  return Point(x / v, y / v);
107  }
108 
109  template <typename T>
111  {
112  return Point(x / v.x, y / v.y);
113  }
114 
115  // Binary operators: new instance = v (+) this
116  template <typename T>
118  {
119  return Point(v + x, v + y);
120  }
121 
122  template <typename T>
124  {
125  return Point(v.x + x, v.y + y);
126  }
127 
128  template <typename T>
130  {
131  return Point(v - x, v - y);
132  }
133 
134  template <typename T>
136  {
137  return Point(v.x - x, v.y - y);
138  }
139 
140  template <typename T>
142  {
143  return Point(v * x, v * y);
144  }
145 
146  template <typename T>
148  {
149  return Point(v.x * x, v.y * y);
150  }
151 
152  template <typename T>
154  {
155  return Point(v / x, v / y);
156  }
157 
158  template <typename T>
160  {
161  return Point(v.x / x, v.y / y);
162  }
163 
164  // Augmented operators: this (+)= v
165  template <typename T>
167  {
168  x += v;
169  y += v;
170  }
171 
172  template <typename T>
173  void Point<T, 2>::IAdd(const Point& v)
174  {
175  x += v.x;
176  y += v.y;
177  }
178 
179  template <typename T>
181  {
182  x -= v;
183  y -= v;
184  }
185 
186  template <typename T>
187  void Point<T, 2>::ISub(const Point& v)
188  {
189  x -= v.x;
190  y -= v.y;
191  }
192 
193  template <typename T>
195  {
196  x *= v;
197  y *= v;
198  }
199 
200  template <typename T>
201  void Point<T, 2>::IMul(const Point& v)
202  {
203  x *= v.x;
204  y *= v.y;
205  }
206 
207  template <typename T>
209  {
210  x /= v;
211  y /= v;
212  }
213 
214  template <typename T>
215  void Point<T, 2>::IDiv(const Point& v)
216  {
217  x /= v.x;
218  y /= v.y;
219  }
220 
221  // Basic getters
222  template <typename T>
223  const T& Point<T, 2>::At(size_t i) const
224  {
225  assert(i < 2);
226  return (&x)[i];
227  }
228 
229  template <typename T>
230  T& Point<T, 2>::At(size_t i)
231  {
232  assert(i < 2);
233  return (&x)[i];
234  }
235 
236  template <typename T>
238  {
239  return x + y;
240  }
241 
242  template <typename T>
244  {
245  return std::min(x, y);
246  }
247 
248  template <typename T>
250  {
251  return std::max(x, y);
252  }
253 
254  template <typename T>
256  {
257  return CubbyFlow::AbsMin(x, y);
258  }
259 
260  template <typename T>
262  {
263  return CubbyFlow::AbsMax(x, y);
264  }
265 
266  template <typename T>
268  {
269  return (std::fabs(x) > std::fabs(y)) ? 0 : 1;
270  }
271 
272  template <typename T>
274  {
275  return (std::fabs(x) < std::fabs(y)) ? 0 : 1;
276  }
277 
278  template <typename T>
279  template <typename U>
281  {
282  return Point2<U>(static_cast<U>(x), static_cast<U>(y));
283  }
284 
285  template <typename T>
286  bool Point<T, 2>::IsEqual(const Point& other) const
287  {
288  return (x == other.x && y == other.y);
289  }
290 
291  // Operators
292  template <typename T>
294  {
295  assert(i < 2);
296  return (&x)[i];
297  }
298 
299  template <typename T>
300  const T& Point<T, 2>::operator[](size_t i) const
301  {
302  assert(i < 2);
303  return (&x)[i];
304  }
305 
306  template <typename T>
307  Point<T, 2>& Point<T, 2>::operator=(const std::initializer_list<T>& list)
308  {
309  Set(list);
310  return (*this);
311  }
312 
313  template <typename T>
315  {
316  Set(v);
317  return (*this);
318  }
319 
320  template <typename T>
322  {
323  IAdd(v);
324  return (*this);
325  }
326 
327  template <typename T>
329  {
330  IAdd(v);
331  return (*this);
332  }
333 
334  template <typename T>
336  {
337  ISub(v);
338  return (*this);
339  }
340 
341  template <typename T>
343  {
344  ISub(v);
345  return (*this);
346  }
347 
348  template <typename T>
350  {
351  IMul(v);
352  return (*this);
353  }
354 
355  template <typename T>
357  {
358  IMul(v);
359  return (*this);
360  }
361 
362  template <typename T>
364  {
365  IDiv(v);
366  return (*this);
367  }
368 
369  template <typename T>
371  {
372  IDiv(v);
373  return (*this);
374  }
375 
376  template <typename T>
377  bool Point<T, 2>::operator==(const Point& v) const
378  {
379  return IsEqual(v);
380  }
381 
382  template <typename T>
383  bool Point<T, 2>::operator!=(const Point& v) const
384  {
385  return !IsEqual(v);
386  }
387 
388  // Math functions
389  template <typename T>
391  {
392  return a;
393  }
394 
395  template <typename T>
397  {
398  return Point<T, 2>(-a.x, -a.y);
399  }
400 
401  template <typename T>
403  {
404  return a.Add(b);
405  }
406 
407  template <typename T>
409  {
410  return b.RAdd(a);
411  }
412 
413  template <typename T>
415  {
416  return a.Add(b);
417  }
418 
419  template <typename T>
421  {
422  return a.Sub(b);
423  }
424 
425  template <typename T>
427  {
428  return b.RSub(a);
429  }
430 
431  template <typename T>
433  {
434  return a.Sub(b);
435  }
436 
437  template <typename T>
439  {
440  return a.Mul(b);
441  }
442 
443  template <typename T>
445  {
446  return b.RMul(a);
447  }
448 
449  template <typename T>
451  {
452  return a.Mul(b);
453  }
454 
455  template <typename T>
457  {
458  return a.Div(b);
459  }
460 
461  template <typename T>
463  {
464  return b.RDiv(a);
465  }
466 
467  template <typename T>
469  {
470  return a.Div(b);
471  }
472 
473  template <typename T>
475  {
476  return Point<T, 2>(std::min(a.x, b.x), std::min(a.y, b.y));
477  }
478 
479  template <typename T>
481  {
482  return Point<T, 2>(std::max(a.x, b.x), std::max(a.y, b.y));
483  }
484 
485  template <typename T>
486  Point<T, 2> Clamp(const Point<T, 2>& v, const Point<T, 2>& low, const Point<T, 2>& high)
487  {
488  return Point<T, 2>(Clamp(v.x, low.x, high.x), Clamp(v.y, low.y, high.y));
489  }
490 
491  template <typename T>
493  {
494  return Point<T, 2>(std::ceil(a.x), std::ceil(a.y));
495  }
496 
497  template <typename T>
499  {
500  return Point<T, 2>(std::floor(a.x), std::floor(a.y));
501  }
502 }
503 
504 #endif
T AbsMin(T x, T y)
Returns the absolute minimum value among the two inputs.
Definition: MathUtils-Impl.h:39
Point Mul(T v) const
Computes this * (v, v).
Definition: Point2-Impl.h:92
Point Div(T v) const
Computes this / (v, v).
Definition: Point2-Impl.h:104
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
Point Sub(T v) const
Computes this - (v, v).
Definition: Point2-Impl.h:80
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
Point RDiv(T v) const
Computes (v, v) / this.
Definition: Point2-Impl.h:153
Point RMul(T v) const
Computes (v, v) * this.
Definition: Point2-Impl.h:141
T y
Y (or the second) component of the point.
Definition: Point2.h:34
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
Point Add(T v) const
Computes this + (v, v).
Definition: Point2-Impl.h:68
Matrix< T, 2, 2 > operator/(const Matrix< T, 2, 2 > &a, T b)
Definition: Matrix2x2-Impl.h:720
Definition: pybind11Utils.h:24
Point RAdd(T v) const
Computes (v, v) + this.
Definition: Point2-Impl.h:117
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
Point RSub(T v) const
Computes (v, v) - this.
Definition: Point2-Impl.h:129
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
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