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