-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathp3a_vector2.hpp
250 lines (223 loc) · 6.55 KB
/
p3a_vector2.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#pragma once
#include <type_traits>
#include "p3a_macros.hpp"
#include "p3a_scalar.hpp"
#include "p3a_functions.hpp"
namespace p3a {
template <class T>
class vector2 {
T m_x;
T m_y;
public:
using reference = T&;
using const_reference = T const&;
P3A_ALWAYS_INLINE vector2() = default;
P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
vector2(T const& a, T const& b)
:m_x(a)
,m_y(b)
{}
template <class U>
P3A_HOST_DEVICE P3A_ALWAYS_INLINE explicit constexpr
vector2(vector2<U> const& other)
:vector2(T(other.x()), T(other.y()))
{
}
template <
class U,
class V,
typename std::enable_if<
std::is_constructible_v<T, U const&> &&
std::is_constructible_v<T, V const&>,
bool>::type = false>
P3A_HOST_DEVICE P3A_ALWAYS_INLINE explicit constexpr
vector2(U const& a, V const& b)
:m_x(a)
,m_y(b)
{}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
reference x() { return m_x; }
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
reference y() { return m_y; }
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
const_reference x() const { return m_x; }
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
const_reference y() const { return m_y; }
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto area() const { return m_x * m_y; }
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
T const& operator[](int pos) const
{
if (pos == 0) return m_x;
return m_y;
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
T& operator[](int pos)
{
if (pos == 0) return m_x;
return m_y;
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE static constexpr
vector2 zero()
{
return vector2(T(0), T(0));
}
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE static constexpr
vector2 ones()
{
return vector2(T(1), T(1));
}
template <class U>
P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
vector2& operator=(vector2<U> const& other)
{
m_x = other.x();
m_y = other.y();
return *this;
}
};
template <class T>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
bool operator==(vector2<T> const& a, vector2<T> const& b)
{
return a.x() == b.x() &&
a.y() == b.y();
}
template <class A, class B>
P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
void operator+=(vector2<A>& a, vector2<B> const& b)
{
a.x() += b.x();
a.y() += b.y();
}
template <class A, class B>
P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
void operator-=(vector2<A>& a, vector2<B> const& b)
{
a.x() -= b.x();
a.y() -= b.y();
}
template <class A, class B>
P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
void operator*=(vector2<A>& a, B const& b)
{
a.x() *= b;
a.y() *= b;
}
template <class A, class B>
P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
void operator/=(vector2<A>& a, B const& b)
{
a.x() /= b;
a.y() /= b;
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto operator+(vector2<A> const& a, vector2<B> const& b) {
using C = decltype(a.x() + b.x());
return vector2<C>(a.x() + b.x(), a.y() + b.y());
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto operator-(vector2<A> const& a, vector2<B> const& b) {
using C = decltype(a.x() - b.x());
return vector2<C>(a.x() - b.x(), a.y() - b.y());
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto operator/(vector2<A> const& a, B const& b) {
using C = decltype(a.x() / b);
return vector2<C>(a.x() / b, a.y() / b);
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE inline constexpr
typename std::enable_if<is_scalar<B>, vector2<decltype(A() * B())>>::type
operator*(vector2<A> const& a, B const& b) {
using C = decltype(a.x() * b);
return vector2<C>(a.x() * b, a.y() * b);
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
typename std::enable_if<is_scalar<A>, vector2<decltype(A() * B())>>::type
operator*(A const& a, vector2<B> const& b) {
return b * a;
}
template <class A>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
vector2<A> operator-(vector2<A> const& a) {
return vector2<A>(-a.x(), -a.y());
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto hadamard_product(vector2<A> const& a, vector2<B> const& b) {
using C = decltype(a.x() * b.x());
return vector2<C>(a.x() * b.x(), a.y() * b.y());
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto hadamard_division(vector2<A> const& a, vector2<B> const& b) {
using C = decltype(a.x() / b.x());
return vector2<C>(a.x() / b.x(), a.y() / b.y());
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto hadamard_equality(vector2<A> const& a, vector2<B> const& b) {
using C = decltype(a.x() == b.x());
return vector2<C>(a.x() == b.x(), a.y() == b.y());
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
vector2<B> hadamard_condition(
vector2<A> const& a,
vector2<B> const& b,
vector2<B> const& c) {
return vector2<B>(
condition(a.x(), b.x(), c.x()),
condition(a.y(), b.y(), c.y()));
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto hadamard_disjunction(vector2<A> const& a, vector2<B> const& b) {
using C = decltype(a.x() || b.x());
return vector2<C>(a.x() || b.x(), a.y() || b.y());
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto cross_product(vector2<A> const& a, vector2<B> const& b) {
return a.x() * b.y() - a.y() * b.x();
}
template <class A, class B>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto dot_product(vector2<A> const& a, vector2<B> const& b) {
return a.x() * b.x() + a.y() * b.y();
}
template <class T>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
T magnitude(vector2<T> const& a)
{
return hypot(a.x(), a.y());
}
template <class T>
[[nodiscard]] P3A_HOST_DEVICE P3A_ALWAYS_INLINE constexpr
auto normalize(vector2<T> const& a) {
return a / magnitude(a);
}
template <class T>
[[nodiscard]] P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline
vector2<T> load_vector2(
T const* ptr, int stride, int offset)
{
return vector2<T>(
load(ptr, 0 * stride + offset),
load(ptr, 1 * stride + offset));
}
template <class T>
P3A_ALWAYS_INLINE P3A_HOST_DEVICE inline
void store(
vector2<T> const& value,
T* ptr, int stride, int offset)
{
store(value.x(), ptr, 0 * stride + offset);
store(value.y(), ptr, 1 * stride + offset);
}
}