Orrery
A GPU-accelerated N-body gravitational simulator
Toggle main menu visibility
Loading...
Searching...
No Matches
vec3.hpp
Go to the documentation of this file.
1
#pragma once
2
13
14
#include <cmath>
15
16
#include "
orrery/core/types.hpp
"
17
18
namespace
orrery::core {
19
26
struct
Vec3
{
27
Real x{};
28
Real y{};
29
Real z{};
30
31
constexpr
Vec3
& operator+=(
Vec3
other)
noexcept
{
32
x += other.x;
33
y += other.y;
34
z += other.z;
35
return
*
this
;
36
}
37
38
constexpr
Vec3
& operator-=(
Vec3
other)
noexcept
{
39
x -= other.x;
40
y -= other.y;
41
z -= other.z;
42
return
*
this
;
43
}
44
45
constexpr
Vec3
& operator*=(Real scale)
noexcept
{
46
x *= scale;
47
y *= scale;
48
z *= scale;
49
return
*
this
;
50
}
51
52
constexpr
Vec3
& operator/=(Real divisor)
noexcept
{
53
x /= divisor;
54
y /= divisor;
55
z /= divisor;
56
return
*
this
;
57
}
58
66
[[nodiscard]]
constexpr
bool
operator==
(
const
Vec3
& other)
const
=
default
;
67
};
68
69
[[nodiscard]]
constexpr
Vec3
operator+(
Vec3
a,
Vec3
b)
noexcept
{
70
return
{a.x + b.x, a.y + b.y, a.z + b.z};
71
}
72
73
[[nodiscard]]
constexpr
Vec3 operator-(Vec3 a, Vec3 b)
noexcept
{
74
return
{a.x - b.x, a.y - b.y, a.z - b.z};
75
}
76
77
[[nodiscard]]
constexpr
Vec3
operator-(
Vec3
v)
noexcept
{
78
return
{-v.x, -v.y, -v.z};
79
}
80
81
[[nodiscard]]
constexpr
Vec3
operator*(
Vec3
v, Real scale)
noexcept
{
82
return
{v.x * scale, v.y * scale, v.z * scale};
83
}
84
85
[[nodiscard]]
constexpr
Vec3
operator*(Real scale,
Vec3
v)
noexcept
{
86
return
v * scale;
87
}
88
89
[[nodiscard]]
constexpr
Vec3
operator/(
Vec3
v, Real divisor)
noexcept
{
90
return
{v.x / divisor, v.y / divisor, v.z / divisor};
91
}
92
93
[[nodiscard]]
constexpr
Real dot(
Vec3
a,
Vec3
b)
noexcept
{
94
return
(a.x * b.x) + (a.y * b.y) + (a.z * b.z);
95
}
96
97
[[nodiscard]]
constexpr
Vec3
cross(
Vec3
a,
Vec3
b)
noexcept
{
98
return
{(a.y * b.z) - (a.z * b.y), (a.z * b.x) - (a.x * b.z), (a.x * b.y) - (a.y * b.x)};
99
}
100
108
[[nodiscard]]
constexpr
Real
squared_norm
(
Vec3
v)
noexcept
{
109
return
dot(v, v);
110
}
111
117
[[nodiscard]]
inline
Real
norm
(
Vec3
v)
noexcept
{
118
return
std::sqrt(
squared_norm
(v));
119
}
120
121
}
// namespace orrery::core
orrery::core::Vec3
A vector in three-dimensional Euclidean space.
Definition
vec3.hpp:26
orrery::core::Vec3::operator==
constexpr bool operator==(const Vec3 &other) const =default
Exact component-wise equality.
types.hpp
The scalar and index types that every layer of the project agrees on.
orrery::core::norm
Real norm(Vec3 v) noexcept
The length.
Definition
vec3.hpp:117
orrery::core::squared_norm
constexpr Real squared_norm(Vec3 v) noexcept
The squared length.
Definition
vec3.hpp:108
orrery
core
vec3.hpp
Generated by
1.17.0