NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
Cartesian.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <cmath>
31#include <iostream>
32
33#include "NumCpp/NdArray.hpp"
37
39{
44 {
45 public:
46 double x{ 0. };
47 double y{ 0. };
48 double z{ 0. };
49
53 Cartesian() noexcept = default;
54
62 constexpr Cartesian(double inX, double inY, double inZ = 0.) noexcept :
63 x(inX),
64 y(inY),
65 z(inZ)
66 {
67 }
68
74 Cartesian(const Vec2& inCartesianVector) :
75 x(inCartesianVector.x),
76 y(inCartesianVector.y)
77 {
78 }
79
85 Cartesian(const Vec3& inCartesianVector) :
86 x(inCartesianVector.x),
87 y(inCartesianVector.y),
88 z(inCartesianVector.z)
89 {
90 }
91
92 //============================================================================
97 Cartesian(const NdArray<double>& inCartesianVector)
98 {
99 if (inCartesianVector.size() != 3)
100 {
101 THROW_INVALID_ARGUMENT_ERROR("NdArray input must be of length 3.");
102 }
103
104 x = inCartesianVector[0];
105 y = inCartesianVector[1];
106 z = inCartesianVector[2];
107 }
108
114 Cartesian(const Cartesian& other) noexcept = default;
115
121 Cartesian(Cartesian&& other) noexcept = default;
122
126 virtual ~Cartesian() = default;
127
132 [[nodiscard]] NdArray<double> toNdArray() const
133 {
134 NdArray<double> returnArray = { x, y, z };
135 return returnArray.transpose();
136 }
137
142 [[nodiscard]] Vec2 toVec2() const
143 {
144 return { x, y };
145 }
146
151 [[nodiscard]] Vec3 toVec3() const
152 {
153 return { x, y, z };
154 }
155
161 Cartesian& operator=(const Cartesian& other) noexcept = default;
162
168 Cartesian& operator=(Cartesian&& other) noexcept = default;
169
175 [[nodiscard]] static Cartesian xHat() noexcept
176 {
177 return { 1., 0., 0. };
178 }
179
185 [[nodiscard]] static Cartesian yHat() noexcept
186 {
187 return { 0., 1., 0. };
188 }
189
195 [[nodiscard]] static Cartesian zHat() noexcept
196 {
197 return { 0., 0., 1. };
198 }
199
206 bool operator==(const Cartesian& other) const noexcept
207 {
208 return utils::essentiallyEqual(x, other.x) && utils::essentiallyEqual(y, other.y) &&
209 utils::essentiallyEqual(z, other.z);
210 }
211
218 bool operator!=(const Cartesian& other) const noexcept
219 {
220 return !(*this == other);
221 }
222 };
223
230 [[nodiscard]] inline Cartesian operator+(const Cartesian& lhs, const Cartesian& rhs) noexcept
231 {
232 return { lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z };
233 }
234
241 [[nodiscard]] inline Cartesian operator-(const Cartesian& lhs, const Cartesian& rhs) noexcept
242 {
243 return { lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z };
244 }
245
252 [[nodiscard]] inline double operator*(const Cartesian& lhs, const Cartesian& rhs) noexcept
253 {
254 return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
255 }
256
263 [[nodiscard]] inline Cartesian operator*(double scalar, const Cartesian& vec) noexcept
264 {
265 return { vec.x * scalar, vec.y * scalar, vec.z * scalar };
266 }
267
274 [[nodiscard]] inline Cartesian operator*(const Cartesian& vec, double scalar) noexcept
275 {
276 return scalar * vec;
277 }
278
285 [[nodiscard]] inline Cartesian operator/(const Cartesian& vec, double denominator) noexcept
286 {
287 return vec * (1.0 / denominator);
288 }
289
296 inline std::ostream& operator<<(std::ostream& os, const Cartesian& vec)
297 {
298 os << "Cartesian(x=" << vec.x << ", y=" << vec.y << ", z=" << vec.z << ")\n";
299 return os;
300 }
301
309 [[nodiscard]] inline Cartesian cross(const Cartesian& vec1, const Cartesian& vec2) noexcept
310 {
311 return { vec1.y * vec2.z - vec1.z * vec2.y,
312 -(vec1.x * vec2.z - vec1.z * vec2.x),
313 vec1.x * vec2.y - vec1.y * vec2.x };
314 }
315
322 [[nodiscard]] inline double norm(const Cartesian& vec) noexcept
323 {
324 return std::hypot(vec.x, vec.y, vec.z);
325 }
326
333 [[nodiscard]] inline Cartesian normalize(const Cartesian& vec) noexcept
334 {
335 return vec / norm(vec);
336 }
337
345 [[nodiscard]] inline double angle(const Cartesian& vec1, const Cartesian& vec2) noexcept
346 {
347 return std::acos(normalize(vec1) * normalize(vec2));
348 }
349} // namespace nc::coordinates
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
self_type transpose() const
Definition NdArrayCore.hpp:4959
Holds a 2D vector.
Definition Vec2.hpp:49
Holds a 3D vector.
Definition Vec3.hpp:51
Cartensian coordinates.
Definition Cartesian.hpp:44
bool operator!=(const Cartesian &other) const noexcept
Non-Equality Operator.
Definition Cartesian.hpp:218
Cartesian(const Vec3 &inCartesianVector)
Default Constructor.
Definition Cartesian.hpp:85
bool operator==(const Cartesian &other) const noexcept
Non-Equality Operator.
Definition Cartesian.hpp:206
NdArray< double > toNdArray() const
Convert to NdArray.
Definition Cartesian.hpp:132
virtual ~Cartesian()=default
Destructor.
static Cartesian zHat() noexcept
z Unit Vector
Definition Cartesian.hpp:195
Cartesian & operator=(const Cartesian &other) noexcept=default
Copy Assignement Operator.
Vec2 toVec2() const
Convert to Vec2.
Definition Cartesian.hpp:142
static Cartesian xHat() noexcept
x Unit Vector
Definition Cartesian.hpp:175
Cartesian(const Cartesian &other) noexcept=default
Copy Constructor.
Vec3 toVec3() const
Convert to Vec2.
Definition Cartesian.hpp:151
double z
Definition Cartesian.hpp:48
Cartesian(const Vec2 &inCartesianVector)
Default Constructor.
Definition Cartesian.hpp:74
Cartesian(Cartesian &&other) noexcept=default
Move Constructor.
static Cartesian yHat() noexcept
y Unit Vector
Definition Cartesian.hpp:185
double y
Definition Cartesian.hpp:47
Cartesian() noexcept=default
Default Constructor.
double x
Definition Cartesian.hpp:46
Cartesian(const NdArray< double > &inCartesianVector)
Definition Cartesian.hpp:97
Cartesian & operator=(Cartesian &&other) noexcept=default
Move Assignement Operator.
Definition Cartesian.hpp:39
double norm(const Cartesian &vec) noexcept
Vector norm.
Definition Cartesian.hpp:322
Cartesian cross(const Cartesian &vec1, const Cartesian &vec2) noexcept
Vector cross product.
Definition Cartesian.hpp:309
std::ostream & operator<<(std::ostream &os, const Cartesian &vec)
Stream operator.
Definition Cartesian.hpp:296
double angle(const Cartesian &vec1, const Cartesian &vec2) noexcept
angle between the two vectors
Definition Cartesian.hpp:345
Cartesian operator+(const Cartesian &lhs, const Cartesian &rhs) noexcept
Addition of two cartesian points.
Definition Cartesian.hpp:230
Cartesian normalize(const Cartesian &vec) noexcept
normalize the input vector
Definition Cartesian.hpp:333
Cartesian operator-(const Cartesian &lhs, const Cartesian &rhs) noexcept
Subtraction of two cartesian points.
Definition Cartesian.hpp:241
double operator*(const Cartesian &lhs, const Cartesian &rhs) noexcept
Dot product of two cartesian points.
Definition Cartesian.hpp:252
Cartesian operator/(const Cartesian &vec, double denominator) noexcept
Scalar Division a cartesian point.
Definition Cartesian.hpp:285
bool essentiallyEqual(dtype inValue1, dtype inValue2) noexcept
Definition essentiallyEqual.hpp:47