-
Notifications
You must be signed in to change notification settings - Fork 13
/
hpc_vector3.hpp
227 lines (202 loc) · 5.58 KB
/
hpc_vector3.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#pragma once
#include <hpc_array_traits.hpp>
#include <hpc_index.hpp>
#include <hpc_macros.hpp>
#include <hpc_math.hpp>
namespace hpc {
struct axis_tag
{
};
using axis_index = hpc::index<axis_tag, int>;
template <typename Scalar>
class vector3
{
public:
using scalar_type = Scalar;
private:
scalar_type raw[3];
public:
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr vector3(Scalar const x, Scalar const y, Scalar const z) noexcept
: raw{x, y, z}
{
}
HPC_ALWAYS_INLINE
vector3() noexcept = default;
HPC_ALWAYS_INLINE
vector3(vector3<scalar_type> const&) noexcept = default;
HPC_ALWAYS_INLINE vector3&
operator=(vector3<scalar_type> const&) noexcept = default;
template <class S2>
HPC_ALWAYS_INLINE explicit vector3(vector3<S2> const& other) noexcept
: vector3(scalar_type(other(0)), scalar_type(other(1)), scalar_type(other(2)))
{
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr scalar_type
operator()(axis_index const i) const noexcept
{
return raw[weaken(i)];
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE scalar_type&
operator()(axis_index const i) noexcept
{
return raw[weaken(i)];
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE static constexpr vector3
zero() noexcept
{
return vector3(0.0, 0.0, 0.0);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE static constexpr vector3
x_axis() noexcept
{
return vector3(1.0, 0.0, 0.0);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE static constexpr vector3
y_axis() noexcept
{
return vector3(0.0, 1.0, 0.0);
}
HPC_ALWAYS_INLINE HPC_HOST_DEVICE static constexpr vector3
z_axis() noexcept
{
return vector3(0.0, 0.0, 1.0);
}
};
template <class T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator+(vector3<T> const left, vector3<T> const right) noexcept
{
return vector3<T>(left(0) + right(0), left(1) + right(1), left(2) + right(2));
}
template <class T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE vector3<T>&
operator+=(vector3<T>& left, vector3<T> const right) noexcept
{
left = left + right;
return left;
}
template <class T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator-(vector3<T> const x) noexcept
{
return vector3<T>(-x(0), -x(1), -x(2));
}
template <class T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator-(vector3<T> const left, vector3<T> const right) noexcept
{
return vector3<T>(left(0) - right(0), left(1) - right(1), left(2) - right(2));
}
template <class T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE vector3<T>&
operator-=(vector3<T>& left, vector3<T> const right) noexcept
{
left = left - right;
return left;
}
template <class L, class R>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
inner_product(vector3<L> const left, vector3<R> const right) noexcept
{
return left(0) * right(0) + left(1) * right(1) + left(2) * right(2);
}
template <class L, class R>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator*(vector3<L> const left, vector3<R> const right) noexcept
{
return inner_product(left, right);
}
template <class L, class R>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator*(vector3<L> const left, R const right) noexcept
{
return vector3<decltype(L() * R())>(left(0) * right, left(1) * right, left(2) * right);
}
template <class L, class R>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE vector3<L>&
operator*=(vector3<L>& left, R const right) noexcept
{
left = left * right;
return left;
}
template <class L, class R>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator*(L const left, vector3<R> const right) noexcept
{
return right * left;
}
template <class L, class R>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
operator/(vector3<L> const left, R const right) noexcept
{
auto const factor = 1.0 / right;
return vector3<decltype(L() / R())>(left(0) * factor, left(1) * factor, left(2) * factor);
}
template <class L, class R>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE vector3<L>&
operator/=(vector3<L>& left, R const right) noexcept
{
left = left / right;
return left;
}
template <class L, class R>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
cross(vector3<L> const left, vector3<R> const right) noexcept
{
return vector3<decltype(L() * R())>(
left(1) * right(2) - left(2) * right(1),
left(2) * right(0) - left(0) * right(2),
left(0) * right(1) - left(1) * right(0));
}
template <class T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
norm_squared(vector3<T> const v) noexcept
{
return (v * v);
}
template <class T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE T
norm(vector3<T> const v) noexcept
{
using std::sqrt;
return sqrt(norm_squared(v));
}
template <class T>
class array_traits<vector3<T>>
{
public:
using value_type = T;
using size_type = axis_index;
HPC_HOST_DEVICE static constexpr size_type
size() noexcept
{
return 3;
}
template <class Iterator>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE static vector3<T>
load(Iterator const it) noexcept
{
return vector3<T>(it[0], it[1], it[2]);
}
template <class Iterator>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE static void
store(Iterator const it, vector3<T> const& value) noexcept
{
it[0] = value(0);
it[1] = value(1);
it[2] = value(2);
}
};
template <typename T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
abs(vector3<T> const v) noexcept
{
return vector3<T>(std::abs(v(0)), std::abs(v(1)), std::abs(v(2)));
}
template <typename T>
HPC_ALWAYS_INLINE HPC_HOST_DEVICE constexpr auto
normalize(vector3<T> const v) noexcept
{
return v / norm(v);
}
} // namespace hpc