NumCpp  2.11.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Cartesian.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <cmath>
31 #include <iostream>
32 
33 #include "NumCpp/NdArray.hpp"
35 #include "NumCpp/Utils/sqr.hpp"
36 #include "NumCpp/Vector/Vec2.hpp"
37 #include "NumCpp/Vector/Vec3.hpp"
38 
39 namespace nc::coordinates
40 {
44  class Cartesian
45  {
46  public:
47  double x{ 0. };
48  double y{ 0. };
49  double z{ 0. };
50 
54  Cartesian() noexcept = default;
55 
63  constexpr Cartesian(double inX, double inY, double inZ = 0.) noexcept :
64  x(inX),
65  y(inY),
66  z(inZ)
67  {
68  }
69 
75  Cartesian(const Vec2& inCartesianVector) :
76  x(inCartesianVector.x),
77  y(inCartesianVector.y)
78  {
79  }
80 
86  Cartesian(const Vec3& inCartesianVector) :
87  x(inCartesianVector.x),
88  y(inCartesianVector.y),
89  z(inCartesianVector.z)
90  {
91  }
92 
93  //============================================================================
98  Cartesian(const NdArray<double>& inCartesianVector)
99  {
100  if (inCartesianVector.size() != 3)
101  {
102  THROW_INVALID_ARGUMENT_ERROR("NdArray input must be of length 3.");
103  }
104 
105  x = inCartesianVector[0];
106  y = inCartesianVector[1];
107  z = inCartesianVector[2];
108  }
109 
115  Cartesian(const Cartesian& other) noexcept = default;
116 
122  Cartesian(Cartesian&& other) noexcept = default;
123 
127  virtual ~Cartesian() = default;
128 
134  Cartesian& operator=(const Cartesian& other) noexcept = default;
135 
141  Cartesian& operator=(Cartesian&& other) noexcept = default;
142 
148  [[nodiscard]] static Cartesian xHat() noexcept
149  {
150  return { 1., 0., 0. };
151  }
152 
158  [[nodiscard]] static Cartesian yHat() noexcept
159  {
160  return { 0., 1., 0. };
161  }
162 
168  [[nodiscard]] static Cartesian zHat() noexcept
169  {
170  return { 0., 0., 1. };
171  }
172 
179  bool operator==(const Cartesian& other) const noexcept
180  {
181  return utils::essentiallyEqual(x, other.x) && utils::essentiallyEqual(y, other.y) &&
182  utils::essentiallyEqual(z, other.z);
183  }
184 
191  bool operator!=(const Cartesian& other) const noexcept
192  {
193  return !(*this == other);
194  }
195  };
196 
203  [[nodiscard]] inline Cartesian operator+(const Cartesian& lhs, const Cartesian& rhs) noexcept
204  {
205  return { lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z };
206  }
207 
214  [[nodiscard]] inline Cartesian operator-(const Cartesian& lhs, const Cartesian& rhs) noexcept
215  {
216  return { lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z };
217  }
218 
225  [[nodiscard]] inline double operator*(const Cartesian& lhs, const Cartesian& rhs) noexcept
226  {
227  return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
228  }
229 
236  [[nodiscard]] inline Cartesian operator*(double scalar, const Cartesian& vec) noexcept
237  {
238  return { vec.x * scalar, vec.y * scalar, vec.z * scalar };
239  }
240 
247  [[nodiscard]] inline Cartesian operator*(const Cartesian& vec, double scalar) noexcept
248  {
249  return scalar * vec;
250  }
251 
258  [[nodiscard]] inline Cartesian operator/(const Cartesian& vec, double denominator) noexcept
259  {
260  return vec * (1.0 / denominator);
261  }
262 
269  inline std::ostream& operator<<(std::ostream& os, const Cartesian& vec)
270  {
271  os << "Cartesian(x=" << vec.x << ", y=" << vec.y << ", z=" << vec.z << ")\n";
272  return os;
273  }
274 
282  [[nodiscard]] inline Cartesian cross(const Cartesian& vec1, const Cartesian& vec2) noexcept
283  {
284  return { vec1.y * vec2.z - vec1.z * vec2.y,
285  -(vec1.x * vec2.z - vec1.z * vec2.x),
286  vec1.x * vec2.y - vec1.y * vec2.x };
287  }
288 
295  [[nodiscard]] inline double norm(const Cartesian& vec) noexcept
296  {
297  return std::hypot(vec.x, vec.y, vec.z);
298  }
299 
306  [[nodiscard]] inline Cartesian normalize(const Cartesian& vec) noexcept
307  {
308  return vec / norm(vec);
309  }
310 
318  [[nodiscard]] inline double angle(const Cartesian& vec1, const Cartesian& vec2) noexcept
319  {
320  return std::acos(normalize(vec1) * normalize(vec2));
321  }
322 } // namespace nc::coordinates
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
size_type size() const noexcept
Definition: NdArrayCore.hpp:4477
Holds a 2D vector.
Definition: Vec2.hpp:49
Holds a 3D vector.
Definition: Vec3.hpp:51
Cartensian coordinates.
Definition: Cartesian.hpp:45
bool operator!=(const Cartesian &other) const noexcept
Non-Equality Operator.
Definition: Cartesian.hpp:191
Cartesian(const Vec3 &inCartesianVector)
Default Constructor.
Definition: Cartesian.hpp:86
bool operator==(const Cartesian &other) const noexcept
Non-Equality Operator.
Definition: Cartesian.hpp:179
Cartesian & operator=(const Cartesian &other) noexcept=default
Copy Assignement Operator.
virtual ~Cartesian()=default
Destructor.
static Cartesian zHat() noexcept
z Unit Vector
Definition: Cartesian.hpp:168
static Cartesian xHat() noexcept
x Unit Vector
Definition: Cartesian.hpp:148
Cartesian(const Cartesian &other) noexcept=default
Copy Constructor.
double z
Definition: Cartesian.hpp:49
Cartesian(const Vec2 &inCartesianVector)
Default Constructor.
Definition: Cartesian.hpp:75
Cartesian(Cartesian &&other) noexcept=default
Move Constructor.
static Cartesian yHat() noexcept
y Unit Vector
Definition: Cartesian.hpp:158
double y
Definition: Cartesian.hpp:48
Cartesian() noexcept=default
Default Constructor.
double x
Definition: Cartesian.hpp:47
Cartesian & operator=(Cartesian &&other) noexcept=default
Move Assignement Operator.
Cartesian(const NdArray< double > &inCartesianVector)
Definition: Cartesian.hpp:98
Definition: Cartesian.hpp:40
double norm(const Cartesian &vec) noexcept
Vector norm.
Definition: Cartesian.hpp:295
Cartesian cross(const Cartesian &vec1, const Cartesian &vec2) noexcept
Vector cross product.
Definition: Cartesian.hpp:282
double angle(const Cartesian &vec1, const Cartesian &vec2) noexcept
angle between the two vectors
Definition: Cartesian.hpp:318
Cartesian operator+(const Cartesian &lhs, const Cartesian &rhs) noexcept
Addition of two cartesian points.
Definition: Cartesian.hpp:203
std::ostream & operator<<(std::ostream &os, const Cartesian &vec)
Stream operator.
Definition: Cartesian.hpp:269
Cartesian normalize(const Cartesian &vec) noexcept
normalize the input vector
Definition: Cartesian.hpp:306
Cartesian operator-(const Cartesian &lhs, const Cartesian &rhs) noexcept
Subtraction of two cartesian points.
Definition: Cartesian.hpp:214
double operator*(const Cartesian &lhs, const Cartesian &rhs) noexcept
Dot product of two cartesian points.
Definition: Cartesian.hpp:225
Cartesian operator/(const Cartesian &vec, double denominator) noexcept
Scalar Division a cartesian point.
Definition: Cartesian.hpp:258
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition: essentiallyEqual.hpp:48
double hypot(dtype inValue1, dtype inValue2) noexcept
Definition: hypot.hpp:56