-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVector.h
More file actions
398 lines (367 loc) · 11.5 KB
/
Copy pathVector.h
File metadata and controls
398 lines (367 loc) · 11.5 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#pragma once
#include "Core/ISerializable.h"
#include "glm/vec2.hpp"
#include "glm/vec3.hpp"
#include "glm/vec4.hpp"
#include <cassert>
#include <type_traits>
#include <initializer_list>
#include <tuple>
namespace sh::game
{
template <typename, typename = void>
struct HasX : std::false_type {};
template <typename T>
struct HasX<T, std::void_t<decltype(std::declval<T>().x)>> : std::true_type {};
template <typename, typename = void>
struct HasY : std::false_type {};
template <typename T>
struct HasY<T, std::void_t<decltype(std::declval<T>().y)>> : std::true_type {};
template <typename, typename = void>
struct HasZ : std::false_type {};
template <typename T>
struct HasZ<T, std::void_t<decltype(std::declval<T>().z)>> : std::true_type {};
template <typename, typename = void>
struct HasW : std::false_type {};
template <typename T>
struct HasW<T, std::void_t<decltype(std::declval<T>().w)>> : std::true_type {};
template<std::size_t N>
struct Vec
{
private:
struct Empty {};
public:
union
{
float data[N];
struct
{
std::conditional_t<N >= 1, float, Empty> x;
std::conditional_t<N >= 2, float, Empty> y;
std::conditional_t<N >= 3, float, Empty> z;
std::conditional_t<N >= 4, float, Empty> w;
};
};
Vec()
{
for (std::size_t i = 0; i < N; ++i)
data[i] = 0.f;
}
Vec(const std::initializer_list<float>& list)
{
for (std::size_t i = 0; i < N; ++i)
{
if (i < list.size())
data[i] = *(list.begin() + i);
}
}
Vec(const glm::vec2& vec)
{
assert(N >= 2);
if constexpr (N >= 2)
{
x = vec.x;
y = vec.y;
}
}
Vec(const glm::vec3& vec)
{
if constexpr (N >= 2)
{
x = vec.x;
y = vec.y;
}
if constexpr (N >= 3)
z = vec.z;
}
Vec(const glm::vec4& vec)
{
if constexpr (N >= 2)
{
x = vec.x;
y = vec.y;
}
if constexpr (N >= 3)
z = vec.z;
if constexpr (N >= 4)
w = vec.w;
}
operator glm::vec2() const
{
static_assert(N >= 1);
if constexpr (N >= 2)
return glm::vec2{ x, y };
else if constexpr (N >= 1)
return glm::vec2{ x, 0.f };
}
operator glm::vec3() const
{
static_assert(N >= 1);
if constexpr (N >= 3)
return glm::vec3{ x, y, z };
else if constexpr (N >= 2)
return glm::vec3{ x, y, 0.f };
else if constexpr (N >= 1)
return glm::vec3{ x, 0.f, 0.f };
}
operator glm::vec4() const
{
static_assert(N >= 1);
if constexpr (N >= 4)
return glm::vec4{ x, y, z, w };
else if constexpr (N >= 3)
return glm::vec4{ x, y, z, 0.f };
else if constexpr (N >= 2)
return glm::vec4{ x, y, 0.f, 0.f };
else if constexpr (N >= 1)
return glm::vec4{ x, 0.f, 0.f, 0.f };
}
bool operator==(const Vec<N>& other) const
{
return x == other.x && y == other.y && z == other.z;
}
bool operator!=(const Vec<N>& other) const
{
return !operator==(other);
}
auto operator=(const glm::vec2& other) -> Vec<N>
{
static_assert(N >= 1);
x = other.x;
if constexpr (N >= 2)
y = other.y;
return *this;
}
auto operator=(const glm::vec3& other) -> Vec<N>
{
static_assert(N >= 1);
x = other.x;
if constexpr (N >= 2)
y = other.y;
if constexpr (N >= 3)
z = other.z;
return *this;
}
auto operator=(const glm::vec4& other) -> Vec<N>
{
static_assert(N >= 1);
x = other.x;
if constexpr (N >= 2)
y = other.y;
if constexpr (N >= 3)
z = other.z;
if constexpr (N >= 4)
w = other.w;
return *this;
}
auto operator=(const std::initializer_list<float>& list) -> Vec<N>
{
for (std::size_t i = 0; i < N; ++i)
{
if (i < list.size())
data[i] = *(list.begin() + i);
}
return *this;
}
// 벡터 간 연산
auto operator+(const Vec<N> other) const-> Vec<N>
{
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = this->data[i] + other.data[i];
return result;
}
auto operator-(const Vec<N> other) const-> Vec<N>
{
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = this->data[i] - other.data[i];
return result;
}
auto operator*(const Vec<N> other) const-> Vec<N>
{
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = this->data[i] * other.data[i];
return result;
}
auto operator/(const Vec<N> other) const-> Vec<N>
{
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = this->data[i] / other.data[i];
return result;
}
// 스칼라 연산
auto operator+(float scalar) const-> Vec<N>
{
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = this->data[i] + scalar;
return result;
}
auto operator-(float scalar) const-> Vec<N>
{
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = this->data[i] - scalar;
return result;
}
auto operator*(float scalar) const-> Vec<N>
{
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = this->data[i] * scalar;
return result;
}
auto operator/(float scalar) const-> Vec<N>
{
assert(scalar != 0.f);
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = this->data[i] / scalar;
return result;
}
};
// 스칼라가 왼쪽인 연산
template<std::size_t N>
auto operator+(float scalar, const Vec<N> vec)-> Vec<N>
{
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = scalar + vec.data[i];
return result;
}
template<std::size_t N>
auto operator-(float scalar, const Vec<N> vec)-> Vec<N>
{
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = scalar - vec.data[i];
return result;
}
template<std::size_t N>
auto operator*(float scalar, const Vec<N> vec)-> Vec<N>
{
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = scalar * vec.data[i];
return result;
}
template<std::size_t N>
auto operator/(float scalar, const Vec<N> vec)-> Vec<N>
{
Vec<N> result{};
for (std::size_t i = 0; i < N; ++i)
result.data[i] = scalar / vec.data[i];
return result;
}
using Vec2 = Vec<2>;
using Vec3 = Vec<3>;
using Vec4 = Vec<4>;
template <std::size_t I>
constexpr auto get(Vec2& v) noexcept -> float&
{
if constexpr (I == 0) return v.x;
else return v.y;
}
template <std::size_t I>
constexpr auto get(const Vec2& v) noexcept -> const float&
{
if constexpr (I == 0) return v.x;
else return v.y;
}
template <std::size_t I>
constexpr auto get(Vec2&& v) noexcept -> float&&
{
if constexpr (I == 0) return std::move(v.x);
else return std::move(v.y);
}
template <std::size_t I>
constexpr auto get(Vec3& v) noexcept -> float&
{
if constexpr (I == 0) return v.x;
else if constexpr (I == 1) return v.y;
else return v.z;
}
template <std::size_t I>
constexpr auto get(const Vec3& v) noexcept -> const float&
{
if constexpr (I == 0) return v.x;
else if constexpr (I == 1) return v.y;
else return v.z;
}
template <std::size_t I>
constexpr auto get(Vec3&& v) noexcept -> float&&
{
if constexpr (I == 0) return std::move(v.x);
else if constexpr (I == 1) return std::move(v.y);
else return std::move(v.z);
}
template <std::size_t I>
constexpr auto get(Vec4& v) noexcept -> float&
{
if constexpr (I == 0) return v.x;
else if constexpr (I == 1) return v.y;
else if constexpr (I == 2) return v.z;
else return v.w;
}
template <std::size_t I>
constexpr auto get(const Vec4& v) noexcept -> const float&
{
if constexpr (I == 0) return v.x;
else if constexpr (I == 1) return v.y;
else if constexpr (I == 2) return v.z;
else return v.w;
}
template <std::size_t I>
constexpr auto get(Vec4&& v) noexcept -> float&&
{
if constexpr (I == 0) return std::move(v.x);
else if constexpr (I == 1) return std::move(v.y);
else if constexpr (I == 2) return std::move(v.z);
else return std::move(v.w);
}
}//namespace
namespace sh::core
{
// 직렬화, 역직렬화
template<std::size_t n>
inline void SerializeProperty(core::Json& json, const std::string& key, const game::Vec<n>& vec)
{
for (int i = 0; i < n; ++i)
json[key].push_back(vec.data[i]);
}
template<std::size_t n>
inline auto DeserializeProperty(const core::Json& json, const std::string& key, game::Vec<n>& vec) -> bool
{
if (json.contains(key) && json[key].is_array() && json[key].size() == n)
{
for(std::size_t i = 0; i < n; ++i)
vec.data[i] = json[key][i].get<float>();
return true;
}
return false;
}
}//namespace
namespace std // 구조적 바인딩 지원
{
template<>
struct tuple_size<sh::game::Vec2> : std::integral_constant<std::size_t, 2> {};
template<>
struct tuple_size<sh::game::Vec3> : std::integral_constant<std::size_t, 3> {};
template<>
struct tuple_size<sh::game::Vec4> : std::integral_constant<std::size_t, 4> {};
template<std::size_t I>
struct tuple_element<I, sh::game::Vec2> { using type = float; };
template<std::size_t I>
struct tuple_element<I, const sh::game::Vec2> { using type = float; };
template<std::size_t I>
struct tuple_element<I, sh::game::Vec3> { using type = float; };
template<std::size_t I>
struct tuple_element<I, const sh::game::Vec3> { using type = float; };
template<std::size_t I>
struct tuple_element<I, sh::game::Vec4> { using type = float; };
template<std::size_t I>
struct tuple_element<I, const sh::game::Vec4> { using type = float; };
}//namespace std