NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Vec3.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <cmath>
31 #include <initializer_list>
32 #include <iostream>
33 #include <sstream>
34 #include <string>
35 
38 #include "NumCpp/NdArray.hpp"
40 #include "NumCpp/Utils/interp.hpp"
41 
42 //====================================================================================
43 
44 namespace nc
45 {
46  //================================================================================
47  // Class Description:
49  class Vec3
50  {
51  public:
52  //====================================Attributes==============================
53  double x{ 0. };
54  double y{ 0. };
55  double z{ 0. };
56 
57  //============================================================================
58  // Method Description:
61  constexpr Vec3() = default;
62 
63  //============================================================================
64  // Method Description:
71  constexpr Vec3(double inX, double inY, double inZ) noexcept :
72  x(inX),
73  y(inY),
74  z(inZ)
75  {
76  }
77 
78  //============================================================================
79  // Method Description:
84  Vec3(const std::initializer_list<double>& inList)
85  {
86  if (inList.size() != 3)
87  {
88  THROW_INVALID_ARGUMENT_ERROR("input initializer list must have a size = 3");
89  }
90 
91  x = *inList.begin();
92  y = *(inList.begin() + 1);
93  z = *(inList.begin() + 2);
94  }
95 
96  //============================================================================
97  // Method Description:
102  Vec3(const NdArray<double>& ndArray)
103  {
104  if (ndArray.size() != 3)
105  {
106  THROW_INVALID_ARGUMENT_ERROR("input NdArray must have a size = 3");
107  }
108 
109  x = ndArray[0];
110  y = ndArray[1];
111  z = ndArray[2];
112  }
113 
114  //============================================================================
115  // Method Description:
121  double angle(const Vec3& otherVec) const noexcept
122  {
123  double dotProduct = dot(otherVec);
124  dotProduct /= norm();
125  dotProduct /= otherVec.norm();
126 
127  // clamp the value to the acos range just to be safe
128  dotProduct = std::max(std::min(dotProduct, 1.), -1.);
129 
130  return std::acos(dotProduct);
131  }
132 
133  //============================================================================
134  // Method Description:
139  static constexpr Vec3 back() noexcept
140  {
141  return Vec3(0., 0., -1.);
142  }
143 
144  //============================================================================
145  // Method Description:
152  Vec3 clampMagnitude(double maxLength) const noexcept
153  {
154  const double magnitude = norm();
155  if (magnitude <= maxLength)
156  {
157  return *this;
158  }
159 
160  Vec3 returnVec = Vec3(*this).normalize();
161  returnVec *= maxLength;
162  return returnVec;
163  }
164 
165  //============================================================================
166  // Method Description:
172  Vec3 cross(const Vec3& otherVec) const noexcept
173  {
174  const double crossX = y * otherVec.z - z * otherVec.y;
175  const double crossY = -(x * otherVec.z - z * otherVec.x);
176  const double crossZ = x * otherVec.y - y * otherVec.x;
177 
178  return { crossX, crossY, crossZ };
179  }
180 
181  //============================================================================
182  // Method Description:
188  double distance(const Vec3& otherVec) const noexcept
189  {
190  return (Vec3(*this) -= otherVec).norm();
191  }
192 
193  //============================================================================
194  // Method Description:
200  double dot(const Vec3& otherVec) const noexcept
201  {
202  return x * otherVec.x + y * otherVec.y + z * otherVec.z;
203  }
204 
205  //============================================================================
206  // Method Description:
211  static constexpr Vec3 down() noexcept
212  {
213  return Vec3(0., -1., 0.);
214  }
215 
216  //============================================================================
217  // Method Description:
222  static constexpr Vec3 forward() noexcept
223  {
224  return Vec3(0., 0., 1.);
225  }
226 
227  //============================================================================
228  // Method Description:
233  static constexpr Vec3 left() noexcept
234  {
235  return Vec3(-1., 0., 0.);
236  }
237 
238  //============================================================================
239  // Method Description:
246  Vec3 lerp(const Vec3& otherVec, double t) const noexcept
247  {
248  t = std::max(std::min(t, 1.), 0.);
249 
250  Vec3 trajectory = otherVec;
251  trajectory -= *this;
252  const double xInterp = utils::interp(0., trajectory.x, t);
253  const double yInterp = utils::interp(0., trajectory.y, t);
254  const double zInterp = utils::interp(0., trajectory.z, t);
255 
256  return Vec3(*this) += Vec3(xInterp, yInterp, zInterp);
257  }
258 
259  //============================================================================
260  // Method Description:
265  double norm() const noexcept
266  {
267  return hypot(x, y, z);
268  }
269 
270  //============================================================================
271  // Method Description:
276  Vec3 normalize() const noexcept
277  {
278  return Vec3(*this) /= norm();
279  }
280 
281  //============================================================================
282  // Method Description:
288  Vec3 project(const Vec3& otherVec) const noexcept
289  {
290  const double projectedMagnitude = norm() * std::cos(angle(otherVec));
291  return otherVec.normalize() *= projectedMagnitude;
292  }
293 
294  //============================================================================
295  // Method Description:
300  static constexpr Vec3 right() noexcept
301  {
302  return Vec3(1., 0., 0.);
303  }
304 
305  //============================================================================
306  // Method Description:
311  std::string toString() const
312  {
313  std::stringstream stream;
314  stream << "Vec3[" << x << ", " << y << ", " << z << "]";
315  return stream.str();
316  }
317 
318  //============================================================================
319  // Method Description:
325  {
326  NdArray<double> returnArray = { x, y, z };
327  return returnArray.transpose();
328  }
329 
330  //============================================================================
331  // Method Description:
336  static constexpr Vec3 up() noexcept
337  {
338  return Vec3(0., 1., 0.);
339  }
340 
341  //============================================================================
342  // Method Description:
348  bool operator==(const Vec3& rhs) const noexcept
349  {
350  return utils::essentiallyEqual(x, rhs.x) && utils::essentiallyEqual(y, rhs.y) &&
351  utils::essentiallyEqual(z, rhs.z);
352  }
353 
354  //============================================================================
355  // Method Description:
361  bool operator!=(const Vec3& rhs) const noexcept
362  {
363  return !(*this == rhs);
364  }
365 
366  //============================================================================
367  // Method Description:
373  Vec3& operator+=(double scaler) noexcept
374  {
375  x += scaler;
376  y += scaler;
377  z += scaler;
378  return *this;
379  }
380 
381  //============================================================================
382  // Method Description:
388  Vec3& operator+=(const Vec3& rhs) noexcept
389  {
390  x += rhs.x;
391  y += rhs.y;
392  z += rhs.z;
393  return *this;
394  }
395 
396  //============================================================================
397  // Method Description:
403  Vec3& operator-=(double scaler) noexcept
404  {
405  x -= scaler;
406  y -= scaler;
407  z -= scaler;
408  return *this;
409  }
410 
411  //============================================================================
412  // Method Description:
418  Vec3& operator-=(const Vec3& rhs) noexcept
419  {
420  x -= rhs.x;
421  y -= rhs.y;
422  z -= rhs.z;
423  return *this;
424  }
425 
426  //============================================================================
427  // Method Description:
433  Vec3& operator*=(double scaler) noexcept
434  {
435  x *= scaler;
436  y *= scaler;
437  z *= scaler;
438  return *this;
439  }
440 
441  //============================================================================
442  // Method Description:
448  Vec3& operator/=(double scaler) noexcept
449  {
450  x /= scaler;
451  y /= scaler;
452  z /= scaler;
453  return *this;
454  }
455  };
456 
457  //============================================================================
458  // Method Description:
465  inline Vec3 operator+(const Vec3& lhs, double rhs) noexcept
466  {
467  return Vec3(lhs) += rhs;
468  }
469 
470  //============================================================================
471  // Method Description:
478  inline Vec3 operator+(double lhs, const Vec3& rhs) noexcept
479  {
480  return Vec3(rhs) += lhs;
481  }
482 
483  //============================================================================
484  // Method Description:
491  inline Vec3 operator+(const Vec3& lhs, const Vec3& rhs) noexcept
492  {
493  return Vec3(lhs) += rhs;
494  }
495 
496  //============================================================================
497  // Method Description:
502  inline Vec3 operator-(const Vec3& vec) noexcept
503  {
504  return { -vec.x, -vec.y, -vec.z };
505  }
506 
507  //============================================================================
508  // Method Description:
515  inline Vec3 operator-(const Vec3& lhs, double rhs) noexcept
516  {
517  return Vec3(lhs) -= rhs;
518  }
519 
520  //============================================================================
521  // Method Description:
528  inline Vec3 operator-(double lhs, const Vec3& rhs) noexcept
529  {
530  return -Vec3(rhs) += lhs;
531  }
532 
533  //============================================================================
534  // Method Description:
541  inline Vec3 operator-(const Vec3& lhs, const Vec3& rhs) noexcept
542  {
543  return Vec3(lhs) -= rhs;
544  }
545 
546  //============================================================================
547  // Method Description:
554  inline Vec3 operator*(const Vec3& lhs, double rhs) noexcept
555  {
556  return Vec3(lhs) *= rhs;
557  }
558 
559  //============================================================================
560  // Method Description:
567  inline Vec3 operator*(double lhs, const Vec3& rhs) noexcept
568  {
569  return Vec3(rhs) *= lhs;
570  }
571 
572  //============================================================================
573  // Method Description:
581  inline double operator*(const Vec3& lhs, const Vec3& rhs) noexcept
582  {
583  return lhs.dot(rhs);
584  }
585 
586  //============================================================================
587  // Method Description:
594  inline Vec3 operator/(const Vec3& lhs, double rhs) noexcept
595  {
596  return Vec3(lhs) /= rhs;
597  }
598 
599  //============================================================================
600  // Method Description:
607  inline std::ostream& operator<<(std::ostream& stream, const Vec3& vec)
608  {
609  stream << vec.toString() << std::endl;
610  return stream;
611  }
612 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
size_type size() const noexcept
Definition: NdArrayCore.hpp:4105
NdArray< dtype > transpose() const
Definition: NdArrayCore.hpp:4457
Holds a 3D vector.
Definition: Vec3.hpp:50
Vec3 & operator*=(double scaler) noexcept
Definition: Vec3.hpp:433
double z
Definition: Vec3.hpp:55
Vec3 & operator-=(const Vec3 &rhs) noexcept
Definition: Vec3.hpp:418
std::string toString() const
Definition: Vec3.hpp:311
Vec3 & operator+=(const Vec3 &rhs) noexcept
Definition: Vec3.hpp:388
bool operator==(const Vec3 &rhs) const noexcept
Definition: Vec3.hpp:348
double distance(const Vec3 &otherVec) const noexcept
Definition: Vec3.hpp:188
Vec3(const std::initializer_list< double > &inList)
Definition: Vec3.hpp:84
Vec3 & operator/=(double scaler) noexcept
Definition: Vec3.hpp:448
static constexpr Vec3 back() noexcept
Definition: Vec3.hpp:139
NdArray< double > toNdArray() const
Definition: Vec3.hpp:324
Vec3(const NdArray< double > &ndArray)
Definition: Vec3.hpp:102
static constexpr Vec3 down() noexcept
Definition: Vec3.hpp:211
Vec3 clampMagnitude(double maxLength) const noexcept
Definition: Vec3.hpp:152
double angle(const Vec3 &otherVec) const noexcept
Definition: Vec3.hpp:121
Vec3 normalize() const noexcept
Definition: Vec3.hpp:276
constexpr Vec3(double inX, double inY, double inZ) noexcept
Definition: Vec3.hpp:71
double norm() const noexcept
Definition: Vec3.hpp:265
Vec3 & operator-=(double scaler) noexcept
Definition: Vec3.hpp:403
static constexpr Vec3 left() noexcept
Definition: Vec3.hpp:233
double x
Definition: Vec3.hpp:53
double y
Definition: Vec3.hpp:54
Vec3 project(const Vec3 &otherVec) const noexcept
Definition: Vec3.hpp:288
bool operator!=(const Vec3 &rhs) const noexcept
Definition: Vec3.hpp:361
static constexpr Vec3 up() noexcept
Definition: Vec3.hpp:336
Vec3 lerp(const Vec3 &otherVec, double t) const noexcept
Definition: Vec3.hpp:246
static constexpr Vec3 forward() noexcept
Definition: Vec3.hpp:222
double dot(const Vec3 &otherVec) const noexcept
Definition: Vec3.hpp:200
constexpr Vec3()=default
Vec3 cross(const Vec3 &otherVec) const noexcept
Definition: Vec3.hpp:172
static constexpr Vec3 right() noexcept
Definition: Vec3.hpp:300
Vec3 & operator+=(double scaler) noexcept
Definition: Vec3.hpp:373
constexpr double interp(double inValue1, double inValue2, double inPercent) noexcept
Definition: Utils/interp.hpp:43
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:51
Definition: Coordinate.hpp:45
Duration operator-(const DateTime &lhs, const DateTime &rhs) noexcept
Subtraction operator.
Definition: DateTime/DateTime.hpp:551
double hypot(dtype inValue1, dtype inValue2) noexcept
Definition: hypot.hpp:56
NdArray< dtype > max(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: max.hpp:44
NdArrayConstIterator< dtype, PointerType, DifferenceType > operator+(typename NdArrayConstIterator< dtype, PointerType, DifferenceType >::difference_type offset, NdArrayConstIterator< dtype, PointerType, DifferenceType > next) noexcept
Definition: NdArrayIterators.hpp:307
auto cos(dtype inValue) noexcept
Definition: cos.hpp:49
NdArray< dtype > min(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: min.hpp:44
NdArray< dtype > operator*(const NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition: NdArrayOperators.hpp:723
std::ostream & operator<<(std::ostream &os, Duration duration)
Output stream operator for the Duration type.
Definition: Clock.hpp:30
NdArray< dtype > operator/(const NdArray< dtype > &lhs, const NdArray< dtype > &rhs)
Definition: NdArrayOperators.hpp:990