NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
vec3.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoN authors
3
4#pragma once
5
6#include <Kokkos_Core.hpp> // IWYU pragma: keep
7
11
12
13namespace NeoN
14{
15
16
22class Vec3
23{
24public:
25
26 KOKKOS_INLINE_FUNCTION
28 {
29 cmpts_[0] = 0.0;
30 cmpts_[1] = 0.0;
31 cmpts_[2] = 0.0;
32 }
33
34 KOKKOS_INLINE_FUNCTION
36 {
37 cmpts_[0] = x;
38 cmpts_[1] = y;
39 cmpts_[2] = z;
40 }
41
42 KOKKOS_INLINE_FUNCTION
43 explicit Vec3(const scalar constValue)
44 {
45 cmpts_[0] = constValue;
46 cmpts_[1] = constValue;
47 cmpts_[2] = constValue;
48 }
49
55 scalar* data() { return cmpts_; }
56
62 const scalar* data() const { return cmpts_; }
63
69 constexpr size_t size() const { return 3; }
70
71 KOKKOS_INLINE_FUNCTION
72 scalar& operator[](const size_t i) { return cmpts_[i]; }
73
74 KOKKOS_INLINE_FUNCTION
75 scalar operator[](const size_t i) const { return cmpts_[i]; }
76
77 KOKKOS_INLINE_FUNCTION
78 scalar& operator()(const size_t i) { return cmpts_[i]; }
79
80 KOKKOS_INLINE_FUNCTION
81 scalar operator()(const size_t i) const { return cmpts_[i]; }
82
83 KOKKOS_INLINE_FUNCTION
84 bool operator==(const Vec3& rhs) const
85 {
86 return cmpts_[0] == rhs(0) && cmpts_[1] == rhs(1) && cmpts_[2] == rhs(2);
87 }
88
89 KOKKOS_INLINE_FUNCTION
90 Vec3 operator+(const Vec3& rhs) const
91 {
92 return Vec3(cmpts_[0] + rhs(0), cmpts_[1] + rhs(1), cmpts_[2] + rhs(2));
93 }
94
95 KOKKOS_INLINE_FUNCTION
96 Vec3& operator+=(const Vec3& rhs)
97 {
98 cmpts_[0] += rhs(0);
99 cmpts_[1] += rhs(1);
100 cmpts_[2] += rhs(2);
101 return *this;
102 }
103
104 KOKKOS_INLINE_FUNCTION
105 Vec3 operator-(const Vec3& rhs) const
106 {
107 return Vec3(cmpts_[0] - rhs(0), cmpts_[1] - rhs(1), cmpts_[2] - rhs(2));
108 }
109
110 KOKKOS_INLINE_FUNCTION
111 Vec3& operator-=(const Vec3& rhs)
112 {
113 cmpts_[0] -= rhs(0);
114 cmpts_[1] -= rhs(1);
115 cmpts_[2] -= rhs(2);
116 return *this;
117 }
118
119 KOKKOS_INLINE_FUNCTION
120 Vec3 operator*(const scalar& rhs) const
121 {
122 return Vec3(cmpts_[0] * rhs, cmpts_[1] * rhs, cmpts_[2] * rhs);
123 }
124
125
126 KOKKOS_INLINE_FUNCTION
127 Vec3 operator*(const label& rhs) const
128 {
129 return Vec3(cmpts_[0] * rhs, cmpts_[1] * rhs, cmpts_[2] * rhs);
130 }
131
132
133 KOKKOS_INLINE_FUNCTION
134 Vec3& operator*=(const scalar& rhs)
135 {
136 cmpts_[0] *= rhs;
137 cmpts_[1] *= rhs;
138 cmpts_[2] *= rhs;
139 return *this;
140 }
141
142private:
143
144 scalar cmpts_[3];
145};
146
147
148KOKKOS_INLINE_FUNCTION
149Vec3 operator*(const scalar& sclr, Vec3 rhs)
150{
151 rhs *= sclr;
152 return rhs;
153}
154
155KOKKOS_INLINE_FUNCTION
156scalar operator&(const Vec3& lhs, Vec3 rhs)
157{
158 return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
159}
160
161KOKKOS_INLINE_FUNCTION
162scalar mag(const Vec3& vec) { return sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]); }
163
164std::ostream& operator<<(std::ostream& out, const Vec3& vec);
165
166
167template<>
168KOKKOS_INLINE_FUNCTION Vec3 one<Vec3>()
169{
170 return Vec3(1.0, 1.0, 1.0);
171}
172
173template<>
174KOKKOS_INLINE_FUNCTION Vec3 zero<Vec3>()
175{
176 return Vec3(0.0, 0.0, 0.0);
177}
178
179} // namespace NeoN
A class for the representation of a 3D Vec3.
Definition vec3.hpp:23
scalar * data()
Returns pointer to the data of the vector.
Definition vec3.hpp:55
KOKKOS_INLINE_FUNCTION Vec3 operator-(const Vec3 &rhs) const
Definition vec3.hpp:105
KOKKOS_INLINE_FUNCTION Vec3 operator+(const Vec3 &rhs) const
Definition vec3.hpp:90
KOKKOS_INLINE_FUNCTION scalar & operator()(const size_t i)
Definition vec3.hpp:78
KOKKOS_INLINE_FUNCTION Vec3 & operator-=(const Vec3 &rhs)
Definition vec3.hpp:111
KOKKOS_INLINE_FUNCTION Vec3 & operator+=(const Vec3 &rhs)
Definition vec3.hpp:96
constexpr size_t size() const
Returns the size of the vector.
Definition vec3.hpp:69
KOKKOS_INLINE_FUNCTION Vec3(const scalar constValue)
Definition vec3.hpp:43
KOKKOS_INLINE_FUNCTION Vec3 & operator*=(const scalar &rhs)
Definition vec3.hpp:134
KOKKOS_INLINE_FUNCTION Vec3()
Definition vec3.hpp:27
KOKKOS_INLINE_FUNCTION Vec3 operator*(const scalar &rhs) const
Definition vec3.hpp:120
const scalar * data() const
Returns pointer to the data of the vector.
Definition vec3.hpp:62
KOKKOS_INLINE_FUNCTION bool operator==(const Vec3 &rhs) const
Definition vec3.hpp:84
KOKKOS_INLINE_FUNCTION Vec3(scalar x, scalar y, scalar z)
Definition vec3.hpp:35
KOKKOS_INLINE_FUNCTION scalar & operator[](const size_t i)
Definition vec3.hpp:72
KOKKOS_INLINE_FUNCTION scalar operator[](const size_t i) const
Definition vec3.hpp:75
KOKKOS_INLINE_FUNCTION scalar operator()(const size_t i) const
Definition vec3.hpp:81
KOKKOS_INLINE_FUNCTION Vec3 operator*(const label &rhs) const
Definition vec3.hpp:127
KOKKOS_INLINE_FUNCTION scalar operator&(const Vec3 &lhs, Vec3 rhs)
Definition vec3.hpp:156
KOKKOS_INLINE_FUNCTION Vec3 zero< Vec3 >()
Definition vec3.hpp:174
KOKKOS_INLINE_FUNCTION Vec3 one< Vec3 >()
Definition vec3.hpp:168
KOKKOS_INLINE_FUNCTION scalar mag(const scalar &s)
Definition scalar.hpp:20
std::ostream & operator<<(std::ostream &out, const Vec3 &vec)
KOKKOS_INLINE_FUNCTION Vec3 operator*(const scalar &sclr, Vec3 rhs)
Definition vec3.hpp:149
float scalar
Definition scalar.hpp:14
int32_t label
Definition label.hpp:24