-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
501 lines (429 loc) · 12 KB
/
vector.h
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#pragma once
#include "redner.h"
#include <cmath>
#include <iostream>
template <typename T>
struct TVector2 {
DEVICE TVector2() {}
template <typename T2>
DEVICE
TVector2(T2 x, T2 y) : x(T(x)), y(T(y)) {}
template <typename T2>
DEVICE
TVector2(const TVector2<T2> &v) : x(T(v.x)), y(T(v.y)) {}
DEVICE T& operator[](int i) {
return *(&x + i);
}
DEVICE T operator[](int i) const {
return *(&x + i);
}
T x, y;
};
template <typename T>
struct TVector3 {
DEVICE TVector3() {}
template <typename T2>
DEVICE
TVector3(T2 x, T2 y, T2 z) : x(T(x)), y(T(y)), z(T(z)) {}
template <typename T2>
DEVICE
TVector3(const TVector3<T2> &v) : x(T(v.x)), y(T(v.y)), z(T(v.z)) {}
DEVICE T& operator[](int i) {
return *(&x + i);
}
DEVICE T operator[](int i) const {
return *(&x + i);
}
T x, y, z;
};
template <typename T>
struct TVector4 {
DEVICE TVector4() {}
template <typename T2>
DEVICE
TVector4(T2 x, T2 y, T2 z, T2 w) : x(T(x)), y(T(y)), z(T(z)), w(T(w)) {}
template <typename T2>
DEVICE
TVector4(const TVector4<T2> &v) : x(T(v.x)), y(T(v.y)), z(T(v.z)), w(T(v.w)) {}
DEVICE T& operator[](int i) {
return *(&x + i);
}
DEVICE T operator[](int i) const {
return *(&x + i);
}
T x, y, z, w;
};
using Vector2f = TVector2<float>;
using Vector2d = TVector2<double>;
using Vector2i = TVector2<int>;
using Vector2 = TVector2<Real>;
using Vector3i = TVector3<int>;
using Vector3f = TVector3<float>;
using Vector3d = TVector3<double>;
using Vector3 = TVector3<Real>;
using Vector4f = TVector4<float>;
using Vector4d = TVector4<double>;
using Vector4 = TVector4<Real>;
template <typename T0, typename T1>
DEVICE
inline auto operator+(const TVector2<T0> &v0,
const TVector2<T1> &v1) {
return TVector2<decltype(v0[0]+v1[0])>{
v0[0] + v1[0], v0[1] + v1[1]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator+(const T0 &v0,
const TVector3<T1> &v1) {
return TVector3<decltype(v0 + v1[0])>{
v0 + v1[0], v0 + v1[1], v0 + v1[2]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator+(const TVector3<T0> &v0,
const T1 &v1) {
return TVector3<decltype(v0[0] + v1)>{
v0[0] + v1, v0[1] + v1, v0[2] + v1};
}
template <typename T0, typename T1>
DEVICE
inline auto operator+(const TVector3<T0> &v0,
const TVector3<T1> &v1) {
return TVector3<decltype(v0[0] + v1[0])>{
v0[0] + v1[0], v0[1] + v1[1], v0[2] + v1[2]};
}
template <typename T0, typename T1>
DEVICE
inline auto& operator+=(TVector2<T0> &v0,
const TVector2<T1> &v1) {
v0[0] += v1[0];
v0[1] += v1[1];
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto& operator+=(TVector3<T0> &v0,
const TVector3<T1> &v1) {
v0[0] += v1[0];
v0[1] += v1[1];
v0[2] += v1[2];
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto& operator+=(TVector3<T0> &v0,
const T1 &v1) {
v0[0] += v1;
v0[1] += v1;
v0[2] += v1;
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto operator-(const T0 &v0,
const TVector3<T1> &v1) {
return TVector3<decltype(v0 - v1[0])>{
v0 - v1[0], v0 - v1[1], v0 - v1[2]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator-(const TVector2<T0> &v0,
const TVector2<T1> &v1) {
return TVector2<decltype(v0[0] - v1[0])>{
v0[0] - v1[0], v0[1] - v1[1]};
}
template <typename T>
DEVICE
inline auto operator-(const TVector3<T> &v) {
return TVector3<T>{-v[0], -v[1], -v[2]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator-(const TVector3<T0> &v0,
const TVector3<T1> &v1) {
return TVector3<decltype(v0[0] - v1[0])>{
v0[0] - v1[0], v0[1] - v1[1], v0[2] - v1[2]};
}
template <typename T0, typename T1>
DEVICE
inline auto& operator-=(TVector3<T0> &v0,
const TVector3<T1> &v1) {
v0[0] -= v1[0];
v0[1] -= v1[1];
v0[2] -= v1[2];
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto operator*(const TVector2<T0> &v0,
const T1 &s) {
return TVector2<decltype(v0[0] * s)>{
v0[0] * s, v0[1] * s};
}
template <typename T0, typename T1>
DEVICE
inline auto operator*(const T0 &s,
const TVector2<T1> &v0) {
return TVector2<decltype(s * v0[0])>{
s * v0[0], s * v0[1]};
}
template <typename T0, typename T1>
DEVICE
inline TVector2<T0> operator*=(TVector2<T0> &v0,
const T1 &s) {
v0[0] *= s;
v0[1] *= s;
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto operator*(const TVector2<T0> &v0,
const TVector2<T1> &v1) {
return TVector2<decltype(v0[0] * v1[0])>{
v0[0] * v1[0], v0[1] * v1[1]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator*(const TVector3<T0> &v0,
const T1 &s) {
return TVector3<decltype(v0[0] * s)>{
v0[0] * s, v0[1] * s, v0[2] * s};
}
template <typename T0, typename T1>
DEVICE
inline auto operator*(const T0 &s,
const TVector3<T1> &v0) {
return TVector3<decltype(s * v0[0])>{
s * v0[0], s * v0[1], s * v0[2]};
}
template <typename T0, typename T1>
DEVICE
inline TVector3<T0> operator*=(TVector3<T0> &v0,
const T1 &s) {
v0[0] *= s;
v0[1] *= s;
v0[2] *= s;
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto operator*(const TVector3<T0> &v0,
const TVector3<T1> &v1) {
return TVector3<decltype(v0[0] * v1[0])>{
v0[0] * v1[0], v0[1] * v1[1], v0[2] * v1[2]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator/(const TVector2<T0> &v0,
const T1 &s) {
auto inv_s = 1.f / s;
return v0 * inv_s;
}
template <typename T0, typename T1>
DEVICE
inline auto operator/(const TVector3<T0> &v0,
const T1 &s) {
auto inv_s = 1.f / s;
return v0 * inv_s;
}
template <typename T>
DEVICE
inline TVector2<T> get_normal(const TVector2<T> &v) {
return TVector2<T>{v.y, -v.x};
}
template <typename T>
DEVICE
inline T length_squared(const TVector2<T> &v0) {
return square(v0[0]) + square(v0[1]);
}
template <typename T>
DEVICE
inline TVector3<T> d_length_squared(const TVector2<T> &v0, const T &d_l_sq) {
//l_sq = square(v0[0]) + square(v0[1])
return 2 * d_l_sq * v0;
}
template <typename T>
DEVICE
inline T length(const TVector2<T> &v0) {
return sqrt(length_squared(v0));
}
template <typename T>
DEVICE
inline T length_squared(const TVector3<T> &v0) {
return square(v0[0]) + square(v0[1]) + square(v0[2]);
}
template <typename T>
DEVICE
inline TVector3<T> d_length_squared(const TVector3<T> &v0, const T &d_l_sq) {
//l_sq = square(v0[0]) + square(v0[1]) + square(v0[2])
return 2 * d_l_sq * v0;
}
template <typename T>
DEVICE
inline T length(const TVector3<T> &v0) {
return sqrt(length_squared(v0));
}
template <typename T>
DEVICE
inline TVector3<T> d_length(const TVector3<T> &v0, const T &d_l) {
auto l_sq = length_squared(v0);
auto l = sqrt(l_sq);
auto d_l_sq = 0.5f * d_l / l;
return d_length_squared(v0, d_l_sq);
}
template <typename T0, typename T1>
DEVICE
inline auto distance_squared(const TVector3<T0> &v0,
const TVector3<T1> &v1) {
return length_squared(v1 - v0);
}
template <typename T0, typename T1>
DEVICE
inline auto distance(const TVector3<T0> &v0,
const TVector3<T1> &v1) {
return length(v1 - v0);
}
template <typename T0, typename T1>
DEVICE
inline auto distance(const TVector2<T0> &v0,
const TVector2<T1> &v1) {
return length(v1 - v0);
}
template <typename T>
DEVICE
inline TVector2<T> normalize(const TVector2<T> &v0) {
return v0 / length(v0);
}
template <typename T>
DEVICE
inline TVector3<T> normalize(const TVector3<T> &v0) {
return v0 / length(v0);
}
template <typename T>
DEVICE
inline TVector3<T> d_normalize(const TVector3<T> &v0, const TVector3<T> &d_n) {
auto l = length(v0);
auto n = v0 / l;
auto d_v0 = d_n / l;
auto d_l = -(d_n[0] * n[0] + d_n[1] * n[1] + d_n[2] * n[2]) / l;
return d_v0 + d_length(v0, d_l);
}
template <typename T0, typename T1>
DEVICE
inline auto dot(const TVector3<T0> &v0, const TVector3<T1> &v1) {
return v0[0] * v1[0] +
v0[1] * v1[1] +
v0[2] * v1[2];
}
template <typename T0, typename T1>
DEVICE
inline auto cross(const TVector3<T0> &v0, const TVector3<T1> &v1) {
return TVector3<decltype(v0[1] * v1[2] - v0[2] * v1[1])>{
v0[1] * v1[2] - v0[2] * v1[1],
v0[2] * v1[0] - v0[0] * v1[2],
v0[0] * v1[1] - v0[1] * v1[0]};
}
template <typename T>
DEVICE
inline void d_cross(const TVector3<T> &v0, const TVector3<T> &v1, const TVector3<T> &d_output,
TVector3<T> &d_v0, TVector3<T> &d_v1) {
d_v0 += cross(v1, d_output);
d_v1 += cross(d_output, v0);
}
template <typename T>
DEVICE
inline auto luminance(const TVector3<T> &v) {
return 0.212671f * v[0] +
0.715160f * v[1] +
0.072169f * v[2];
}
template <typename T>
DEVICE
inline auto sum(const TVector2<T> &v) {
return v[0] + v[1];
}
template <typename T>
DEVICE
inline auto sum(const TVector3<T> &v) {
return v[0] + v[1] + v[2];
}
template <typename T>
DEVICE
void coordinate_system(const TVector3<T> &n, TVector3<T> &x, TVector3<T> &y) {
if (n[2] < -1.f + 1e-6f) {
x = TVector3<T>{T(0), T(-1), T(0)};
y = TVector3<T>{T(-1), T(0), T(0)};
} else {
auto a = 1.f / (1.f + n[2]);
auto b = -n[0] * n[1] * a;
x = TVector3<T>{1.f - square(n[0]) * a, b, -n[0]};
y = TVector3<T>{b, 1.f - square(n[1]) * a, -n[1]};
}
}
template <typename T>
DEVICE
void d_coordinate_system(const TVector3<T> &n, const TVector3<T> &d_x, const TVector3<T> &d_y,
TVector3<T> &d_n) {
if (n[2] < -1.f + 1e-6f) {
//x = TVector3<T>{T(0), T(-1), T(0)};
//y = TVector3<T>{T(-1), T(0), T(0)};
// don't need to do anything
} else {
auto a = 1.f / (1.f + n[2]);
// auto b = -n[0] * n[1] * a;
// x = TVector3<T>{1.f - square(n[0]) * a, b, -n[0]}
d_n[0] -= 2.f * n[0] * d_x[0] * a;
auto d_a = -square(n[0]) * d_x[0];
auto d_b = d_x[1];
d_n[0] -= d_x[2];
// y = TVector3<T>{b, 1.f - square(n[1]) * a, -n[1]}
d_b += d_y[0];
d_n[1] -= 2.f * d_y[1] * n[1] * a;
d_a -= d_y[1] * square(n[1]);
d_n[1] -= d_y[2];
// b = -n[0] * n[1] * a
d_n[0] -= d_b * n[1] * a;
d_n[1] -= d_b * n[0] * a;
d_a -= d_b * n[0] * n[1];
// a = 1 / (1 + n[2])
d_n[2] -= d_a * a / (1 + n[2]);
}
}
DEVICE
inline bool isfinite(const Vector2 &v) {
return std::isfinite(v.x) &&
std::isfinite(v.y);
}
DEVICE
inline bool isfinite(const Vector3 &v) {
return std::isfinite(v.x) &&
std::isfinite(v.y) &&
std::isfinite(v.z);
}
DEVICE
inline bool is_zero(const Vector3 &v) {
return v.x == 0 && v.y == 0 && v.z == 0;
}
template <typename T>
DEVICE
inline TVector2<T> atomic_add(TVector2<T> &target, const TVector2<T> &source) {
atomic_add(target[0], source[0]);
atomic_add(target[1], source[1]);
return target;
}
template <typename T>
DEVICE
inline TVector3<T> atomic_add(TVector3<T> &target, const TVector3<T> &source) {
atomic_add(target[0], source[0]);
atomic_add(target[1], source[1]);
atomic_add(target[2], source[2]);
return target;
}
template <typename T>
inline std::ostream& operator<<(std::ostream &os, const TVector2<T> &v) {
return os << "(" << v[0] << ", " << v[1] << ")";
}
template <typename T>
inline std::ostream& operator<<(std::ostream &os, const TVector3<T> &v) {
return os << "(" << v[0] << ", " << v[1] << ", " << v[2] << ")";
}