Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
Vector3.hpp
Go to the documentation of this file.
1#ifndef PHYTH_VECTOR3_HPP
2#define PHYTH_VECTOR3_HPP
3
5#include "Vector.hpp"
6#include "QuantityFuncs.hpp"
7
8#include <ostream>
9
10namespace Phyth {
11 template<typename T>
27 template<typename T>
28 struct Vector<3, T> {
29 static_assert(is_quantity_v<T>, "Vector only supports Quantity types");
30
37
39 constexpr Vector() : x(0), y(0), z(0) {
40 }
41
49 constexpr Vector(T x_, T y_, T z_) : x(x_), y(y_), z(z_) {
50 }
51
63 static constexpr Vector FromPolar(T r, const Scalar theta_rad) {
64 return Vector(
67 T(0)
68 );
69 }
70
84 static constexpr Vector FromSpherical(T r, const Scalar theta_rad, const Scalar phi_rad) {
85 return Vector(
89 );
90 }
91
103 [[nodiscard]] auto ToPolar() const {
104 Vector r = Length();
105 auto theta = Utils::atan2(y / r, x / r);
106 return std::pair<decltype(r), Scalar>{r, theta};
107 }
108
117 [[nodiscard]] auto ToSpherical() const {
118 auto r = Length();
119 auto theta = Utils::acos(z / r);
120 auto phi = Utils::atan2(y / r, x / r);
121 return std::tuple<decltype(r), Scalar, Scalar>{r, theta, phi};
122 }
123
133 constexpr T &operator[](const size_t i) {
134 switch (i) {
135 case 0: return x;
136 case 1: return y;
137 default: return z;
138 }
139 }
140
147 constexpr const T &operator[](const size_t i) const {
148 switch (i) {
149 case 0: return x;
150 case 1: return y;
151 default: return z;
152 }
153 }
154
162 template<typename U>
163 constexpr auto operator+(const Vector3<U> &other) const {
164 return Vector3<decltype(x + other.x)>(
165 x + other.x,
166 y + other.y,
167 z + other.z
168 );
169 }
170
178 template<typename U>
179 constexpr auto operator-(const Vector3<U> &other) const {
180 return Vector3<decltype(x - other.x)>(
181 x - other.x,
182 y - other.y,
183 z - other.z
184 );
185 }
186
192 constexpr auto operator-() const {
193 return Vector(-x, -y, -z);
194 }
195
206 template<typename U, typename = std::enable_if_t<!std::is_same_v<std::decay_t<U>, Vector>> >
207 constexpr auto operator*(U scalar) const {
209 x * scalar,
210 y * scalar,
211 z * scalar
212 );
213 }
214
222 template<typename U>
223 constexpr auto operator/(U scalar) const {
224 return Vector3<decltype(x / scalar)>(
225 x / scalar,
226 y / scalar,
227 z / scalar
228 );
229 }
230
238 template<typename U>
239 constexpr Vector &operator+=(const Vector3<U> &other) {
240 x += other.x;
241 y += other.y;
242 z += other.z;
243 return *this;
244 }
245
253 template<typename U>
254 constexpr Vector &operator-=(const Vector3<U> &other) {
255 x -= other.x;
256 y -= other.y;
257 z -= other.z;
258 return *this;
259 }
260
268 template<typename U>
269 constexpr Vector &operator*=(U scalar) {
270 x *= scalar;
271 y *= scalar;
272 z *= scalar;
273 return *this;
274 }
275
283 template<typename U>
284 constexpr Vector &operator/=(U scalar) {
285 x /= scalar;
286 y /= scalar;
287 z /= scalar;
288 return *this;
289 }
290
298 template<typename U>
299 constexpr bool operator==(const Vector3<U> &other) const {
300 return x == other.x && y == other.y && z == other.z;
301 }
302
310 template<typename U>
311 constexpr bool operator!=(const Vector3<U> &other) const {
312 return !(*this == other);
313 }
314
327 template<typename U>
328 constexpr auto Dot(const Vector3<U> &other) const -> decltype(x * other.x) {
329 return x * other.x + y * other.y + z * other.z;
330 }
331
344 template<typename U>
345 constexpr auto Cross(const Vector3<U> &other) const {
346 return Vector3<decltype(x * other.x)>(
347 y * other.z - z * other.y,
348 z * other.x - x * other.z,
349 x * other.y - y * other.x
350 );
351 }
352
358 [[nodiscard]] auto Length() const {
359 return Utils::sqrt(x * x + y * y + z * z);
360 }
361
370 [[nodiscard]] auto LengthSquared() const {
371 return x * x + y * y + z * z;
372 }
373
383 auto len = Length();
384 return Vector3<decltype(x / len)>(
385 x / len,
386 y / len,
387 z / len
388 );
389 }
390
403 template<typename U>
404 [[nodiscard]] auto Project(const Vector3<U> &other) const {
405 return Dot(other) / other.LengthSquared() * other;
406 }
407
418 template<typename UnitT>
420 return {x.template As<UnitT>(), y.template As<UnitT>(), z.template As<UnitT>()};
421 }
422
431 friend std::ostream &operator<<(std::ostream &os, const Vector &v) {
432 os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
433 return os;
434 }
435 };
436
440 template<typename T>
441 inline constexpr auto is_vector3_v = is_vector_of_n_v<3, T>;;
442
458 template<typename T, typename U,
459 typename = std::enable_if_t<!is_vector3_v<T>> >
460 constexpr auto operator*(T scalar, const Vector3<U> &v) {
461 return Vector3<decltype(scalar * v.x)>(
462 scalar * v.x,
463 scalar * v.y,
464 scalar * v.z
465 );
466 }
467}
468
469#endif // PHYTH_VECTOR3_HPP
Scalar cos(const Scalar q)
Cosine of a dimensionless quantity.
Scalar atan2(const Scalar y, const Scalar x)
Two-argument arc tangent (atan2)
auto sqrt(Quantity< UnitT > q)
Square root of a quantity.
Scalar acos(const Scalar q)
Arc cosine (inverse cosine)
Scalar sin(const Scalar q)
Sine of a dimensionless quantity.
constexpr auto operator*(Quantity< U1 > a, Quantity< U2 > b)
Multiply two quantities.
Definition Quantity.hpp:524
constexpr bool is_unit_v
Definition Unit.hpp:79
constexpr auto is_vector3_v
Convenience variable template for is_vector3.
Definition Vector3.hpp:441
3D vector with Quantity components
Definition Vector3.hpp:28
constexpr T & operator[](const size_t i)
Index-based access (non-const)
Definition Vector3.hpp:133
constexpr bool operator==(const Vector3< U > &other) const
Equality comparison.
Definition Vector3.hpp:299
constexpr Vector & operator*=(U scalar)
Scalar multiplication assignment.
Definition Vector3.hpp:269
constexpr auto operator*(U scalar) const
Scalar multiplication (vector * scalar)
Definition Vector3.hpp:207
auto LengthSquared() const
Squared Euclidean length.
Definition Vector3.hpp:370
static constexpr Vector FromSpherical(T r, const Scalar theta_rad, const Scalar phi_rad)
Construct a 3D spherical vector.
Definition Vector3.hpp:84
constexpr Vector & operator-=(const Vector3< U > &other)
Subtraction assignment.
Definition Vector3.hpp:254
constexpr Vector(T x_, T y_, T z_)
Construct a vector with specified components.
Definition Vector3.hpp:49
constexpr auto operator/(U scalar) const
Scalar division (vector / scalar)
Definition Vector3.hpp:223
constexpr auto Dot(const Vector3< U > &other) const -> decltype(x *other.x)
Dot product.
Definition Vector3.hpp:328
constexpr Vector & operator+=(const Vector3< U > &other)
Addition assignment.
Definition Vector3.hpp:239
constexpr bool operator!=(const Vector3< U > &other) const
Inequality comparison.
Definition Vector3.hpp:311
constexpr Vector()
Construct a zero vector (0, 0, 0)
Definition Vector3.hpp:39
Vector3< Quantity< UnitT > > As() const
Convert all components to a different unit of the same dimension.
Definition Vector3.hpp:419
static constexpr Vector FromPolar(T r, const Scalar theta_rad)
Construct a 2D polar vector (z = 0)
Definition Vector3.hpp:63
auto Length() const
Euclidean length (magnitude)
Definition Vector3.hpp:358
friend std::ostream & operator<<(std::ostream &os, const Vector &v)
Stream insertion operator for Vector3.
Definition Vector3.hpp:431
Vector3< Scalar > Normalized() const
Normalized vector (unit vector in the same direction)
Definition Vector3.hpp:382
constexpr auto Cross(const Vector3< U > &other) const
Cross product.
Definition Vector3.hpp:345
constexpr const T & operator[](const size_t i) const
Index-based access (const)
Definition Vector3.hpp:147
constexpr auto operator-(const Vector3< U > &other) const
Vector subtraction.
Definition Vector3.hpp:179
auto ToSpherical() const
Convert to spherical coordinates.
Definition Vector3.hpp:117
constexpr Vector & operator/=(U scalar)
Scalar division assignment.
Definition Vector3.hpp:284
auto Project(const Vector3< U > &other) const
Project this vector onto another vector.
Definition Vector3.hpp:404
constexpr auto operator-() const
Unary negation.
Definition Vector3.hpp:192
constexpr auto operator+(const Vector3< U > &other) const
Vector addition.
Definition Vector3.hpp:163
auto ToPolar() const
Convert to 2D polar coordinates (z component is ignored)
Definition Vector3.hpp:103
constexpr Vector()=default
auto Length() const
Definition Vector.hpp:135
constexpr auto Dot(const Vector< N, U > &other) const
Definition Vector.hpp:151