DryPhys 1.0.0
...
Loading...
Searching...
No Matches
vector3d.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 C.R. Drisko. All rights reserved.
2// Licensed under the MIT License. See the LICENSE file in the project root for more information.
3//
4// Name: vector3d.hpp
5// Author: crdrisko
6// Date: 06/12/2024-06:01:10
7// Description: A vector representing a quantity with x, y, and z components
8
9#ifndef DRYPHYS_INCLUDE_DRYPHYS_VECTOR3D_HPP
10#define DRYPHYS_INCLUDE_DRYPHYS_VECTOR3D_HPP
11
12#include <config.h>
13
14#include <cmath>
15
16#include <common-utils/utilities.hpp>
17
18namespace DryPhys
19{
27 class Vector3D : private DryChem::EqualityComparable<Vector3D>
28 {
29 private:
30 real x {};
31 real y {};
32 real z {};
33
34#ifdef phys_four_word_alignment
36 [[maybe_unused]] real pad;
37#endif
38
39 public:
41 DRYPHYS_CONSTEXPR Vector3D() noexcept = default;
42 DRYPHYS_CONSTEXPR Vector3D(const real x, const real y, const real z) noexcept : x {x}, y {y}, z {z} {}
43
45 DRYPHYS_CONSTEXPR friend bool operator==(const Vector3D& lhs_, const Vector3D& rhs_)
46 {
47 return lhs_.x == rhs_.x && lhs_.y == rhs_.y && lhs_.z == rhs_.z;
48 }
49
50 DRYPHYS_CONSTEXPR friend bool operator<(const Vector3D& lhs_, const Vector3D& rhs_)
51 {
52 return lhs_.x < rhs_.x && lhs_.y < rhs_.y && lhs_.z < rhs_.z;
53 }
54
55 DRYPHYS_CONSTEXPR friend bool operator>(const Vector3D& lhs_, const Vector3D& rhs_)
56 {
57 return lhs_.x > rhs_.x && lhs_.y > rhs_.y && lhs_.z > rhs_.z;
58 }
59
60 DRYPHYS_CONSTEXPR friend bool operator<=(const Vector3D& lhs_, const Vector3D& rhs_)
61 {
62 return lhs_.x <= rhs_.x && lhs_.y <= rhs_.y && lhs_.z <= rhs_.z;
63 }
64
65 DRYPHYS_CONSTEXPR friend bool operator>=(const Vector3D& lhs_, const Vector3D& rhs_)
66 {
67 return lhs_.x >= rhs_.x && lhs_.y >= rhs_.y && lhs_.z >= rhs_.z;
68 }
69
71 DRYPHYS_CONSTEXPR real& operator[](unsigned i) { return ((&x)[i]); }
72 DRYPHYS_CONSTEXPR const real& operator[](unsigned i) const { return ((&x)[i]); }
73
75 DRYPHYS_CONSTEXPR void operator+=(const Vector3D& rhs)
76 {
77 x += rhs.x;
78 y += rhs.y;
79 z += rhs.z;
80 }
81
82 DRYPHYS_CONSTEXPR Vector3D operator+(const Vector3D& rhs) const { return Vector3D(x + rhs.x, y + rhs.y, z + rhs.z); }
83
84 DRYPHYS_CONSTEXPR void operator-=(const Vector3D& rhs)
85 {
86 x -= rhs.x;
87 y -= rhs.y;
88 z -= rhs.z;
89 }
90
91 DRYPHYS_CONSTEXPR Vector3D operator-(const Vector3D& rhs) const { return Vector3D(x - rhs.x, y - rhs.y, z - rhs.z); }
92
93 DRYPHYS_CONSTEXPR void operator*=(real rhs)
94 {
95 x *= rhs;
96 y *= rhs;
97 z *= rhs;
98 }
99
100 DRYPHYS_CONSTEXPR Vector3D operator*(real rhs) const { return Vector3D(x * rhs, y * rhs, z * rhs); }
101
102 DRYPHYS_CONSTEXPR void operator/=(real rhs)
103 {
104 rhs = static_cast<real>(1) / rhs;
105
106 x *= rhs;
107 y *= rhs;
108 z *= rhs;
109 }
110
111 DRYPHYS_CONSTEXPR Vector3D operator/(real rhs) const
112 {
113 rhs = static_cast<real>(1) / rhs;
114 return Vector3D(x * rhs, y * rhs, z * rhs);
115 }
116
118 DRYPHYS_CONSTEXPR void operator*=(const Vector3D& rhs)
119 {
120 x *= rhs.x;
121 y *= rhs.y;
122 z *= rhs.z;
123 }
124
126 DRYPHYS_CONSTEXPR Vector3D operator*(const Vector3D& rhs) const { return Vector3D(x * rhs.x, y * rhs.y, z * rhs.z); }
127
131 DRYPHYS_CONSTEXPR real dot(const Vector3D& rhs) const { return x * rhs.x + y * rhs.y + z * rhs.z; }
132
136 DRYPHYS_CONSTEXPR Vector3D cross(const Vector3D& rhs) const
137 {
138 return Vector3D(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
139 }
140
144 DRYPHYS_CONSTEXPR void invert()
145 {
146 x = -x;
147 y = -y;
148 z = -z;
149 }
150
154 DRYPHYS_CONSTEXPR real magnitudeSquared() const { return x * x + y * y + z * z; }
155
159 real magnitude() const { return std::sqrt(x * x + y * y + z * z); }
160
165 {
166 if (real length = magnitude(); length > 0)
167 (*this) *= static_cast<real>(1) / length;
168 }
169 };
170
174 template<std::size_t Index>
175 decltype(auto) get(Vector3D& vec)
176 {
177 static_assert(Index < 3, "Index must be within 0 and 2, inclusive.");
178
179 if constexpr (Index == 0)
180 return vec[0];
181 else if constexpr (Index == 1)
182 return vec[1];
183 else
184 return vec[2];
185 }
186
190 template<std::size_t Index>
191 decltype(auto) get(const Vector3D& vec)
192 {
193 static_assert(Index < 3, "Index must be within 0 and 2, inclusive.");
194
195 if constexpr (Index == 0)
196 return vec[0];
197 else if constexpr (Index == 1)
198 return vec[1];
199 else
200 return vec[2];
201 }
202
206 template<std::size_t Index>
207 decltype(auto) get(Vector3D&& vec)
208 {
209 static_assert(Index < 3, "Index must be within 0 and 2, inclusive.");
210
211 if constexpr (Index == 0)
212 return std::move(vec[0]);
213 else if constexpr (Index == 1)
214 return std::move(vec[1]);
215 else
216 return std::move(vec[2]);
217 }
218} // namespace DryPhys
219
221template<>
222struct std::tuple_size<DryPhys::Vector3D>
223{
224 static constexpr int value = 3;
225};
226
227template<std::size_t Index>
228struct std::tuple_element<Index, DryPhys::Vector3D>
229{
230 using type = DryPhys::real;
231};
232
233#endif
Definition vector3d.hpp:28
DRYPHYS_CONSTEXPR Vector3D operator/(real rhs) const
Definition vector3d.hpp:111
DRYPHYS_CONSTEXPR Vector3D operator*(real rhs) const
Definition vector3d.hpp:100
real z
Definition vector3d.hpp:32
DRYPHYS_CONSTEXPR void operator/=(real rhs)
Definition vector3d.hpp:102
DRYPHYS_CONSTEXPR Vector3D operator+(const Vector3D &rhs) const
Definition vector3d.hpp:82
DRYPHYS_CONSTEXPR const real & operator[](unsigned i) const
Definition vector3d.hpp:72
DRYPHYS_CONSTEXPR real & operator[](unsigned i)
Element access.
Definition vector3d.hpp:71
real y
Definition vector3d.hpp:31
real x
Definition vector3d.hpp:30
DRYPHYS_CONSTEXPR friend bool operator<=(const Vector3D &lhs_, const Vector3D &rhs_)
Definition vector3d.hpp:60
DRYPHYS_CONSTEXPR Vector3D operator-(const Vector3D &rhs) const
Definition vector3d.hpp:91
DRYPHYS_CONSTEXPR void operator+=(const Vector3D &rhs)
Arithmetic Operators.
Definition vector3d.hpp:75
real magnitude() const
Definition vector3d.hpp:159
DRYPHYS_CONSTEXPR friend bool operator>=(const Vector3D &lhs_, const Vector3D &rhs_)
Definition vector3d.hpp:65
DRYPHYS_CONSTEXPR real dot(const Vector3D &rhs) const
Definition vector3d.hpp:131
DRYPHYS_CONSTEXPR real magnitudeSquared() const
Definition vector3d.hpp:154
DRYPHYS_CONSTEXPR Vector3D operator*(const Vector3D &rhs) const
We use operator* for the component product.
Definition vector3d.hpp:126
DRYPHYS_CONSTEXPR void operator*=(real rhs)
Definition vector3d.hpp:93
DRYPHYS_CONSTEXPR friend bool operator>(const Vector3D &lhs_, const Vector3D &rhs_)
Definition vector3d.hpp:55
void normalize()
Definition vector3d.hpp:164
DRYPHYS_CONSTEXPR friend bool operator<(const Vector3D &lhs_, const Vector3D &rhs_)
Definition vector3d.hpp:50
DRYPHYS_CONSTEXPR friend bool operator==(const Vector3D &lhs_, const Vector3D &rhs_)
Comparison operators - only the equality operator is symmetric.
Definition vector3d.hpp:45
DRYPHYS_CONSTEXPR void operator*=(const Vector3D &rhs)
We use operator*= for the component product.
Definition vector3d.hpp:118
DRYPHYS_CONSTEXPR void invert()
Definition vector3d.hpp:144
DRYPHYS_CONSTEXPR void operator-=(const Vector3D &rhs)
Definition vector3d.hpp:84
DRYPHYS_CONSTEXPR Vector3D cross(const Vector3D &rhs) const
Definition vector3d.hpp:136
DRYPHYS_CONSTEXPR Vector3D() noexcept=default
Constructors.
Definition vector3d.hpp:19
decltype(auto) get(Vector3D &vec)
Definition vector3d.hpp:175
DryPhys::real type
Definition vector3d.hpp:230
static constexpr int value
Definition vector3d.hpp:224