forked from BachiLi/diffvg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
817 lines (709 loc) · 21.4 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
#pragma once
#include "diffvg.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) -> TVector2<decltype(v0[0] + v1[0])> {
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 TVector2<T1> &v1) -> TVector2<decltype(v0 + v1[0])> {
return TVector2<decltype(v0 + v1[0])>{v0 + v1[0], v0 + v1[1]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator+(const T0 &v0,
const TVector3<T1> &v1) -> TVector3<decltype(v0[0] + 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 T1 &v1) -> TVector2<decltype(v0[0] + v1)> {
return TVector2<decltype(v0[0] + v1)>{
v0[0] + v1, v0[1] + v1};
}
template <typename T0, typename T1>
DEVICE
inline auto operator+(const TVector3<T0> &v0,
const T1 &v1) -> TVector3<decltype(v0[0] + v1[0])> {
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) -> TVector3<decltype(v0[0] + v1[0])> {
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 TVector4<T0> &v0,
const TVector4<T1> &v1) -> TVector4<decltype(v0[0] + v1[0])> {
return TVector4<decltype(v0[0] + v1[0])>{
v0[0] + v1[0], v0[1] + v1[1], v0[2] + v1[2], v0[3] + v1[3]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator+=(TVector2<T0> &v0,
const TVector2<T1> &v1) -> TVector2<T0>& {
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) -> TVector3<T0>& {
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) -> TVector3<T0>& {
v0[0] += v1;
v0[1] += v1;
v0[2] += v1;
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto operator+=(TVector4<T0> &v0,
const TVector4<T1> &v1) -> TVector4<T0>& {
v0[0] += v1[0];
v0[1] += v1[1];
v0[2] += v1[2];
v0[3] += v1[3];
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto operator+=(TVector4<T0> &v0,
const T1 &v1) -> TVector4<T0>& {
v0[0] += v1;
v0[1] += v1;
v0[2] += v1;
v0[3] += v1;
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto operator-(const T0 &v0,
const TVector2<T1> &v1) -> TVector2<decltype(v0 - v1[0])> {
return TVector2<decltype(v0 - v1[0])>{v0 - v1[0], v0 - v1[1]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator-(const T0 &v0,
const TVector3<T1> &v1) -> TVector2<decltype(v0 - v1[0])> {
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 T1 &v1) -> TVector2<decltype(v0[0] - v1)> {
return TVector2<decltype(v0[0] - v1)>{v0[0] - v1, v0[1] - v1};
}
template <typename T0, typename T1>
DEVICE
inline auto operator-(const TVector3<T0> &v0,
const T1 &v1) -> TVector3<decltype(v0[0] - 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 TVector2<T0> &v0,
const TVector2<T1> &v1) -> TVector2<decltype(v0[0] - v1[0])> {
return TVector2<decltype(v0[0] - v1[0])>{
v0[0] - v1[0], v0[1] - v1[1]};
}
template <typename T>
DEVICE
inline auto operator-(const TVector2<T> &v) -> TVector2<T> {
return TVector2<T>{-v[0], -v[1]};
}
template <typename T>
DEVICE
inline auto operator-(const TVector3<T> &v) -> TVector3<T> {
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) -> TVector3<decltype(v0[0] - v1[0])> {
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 TVector4<T0> &v0,
const TVector4<T1> &v1) -> TVector4<decltype(v0[0] - v1[0])> {
return TVector4<decltype(v0[0] - v1[0])>{
v0[0] - v1[0], v0[1] - v1[1], v0[2] - v1[2], v0[3] - v1[3]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator-=(TVector2<T0> &v0,
const TVector2<T1> &v1) -> TVector2<T0>& {
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) -> TVector3<T0>& {
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 TVector2<T1> &v1) -> TVector2<decltype(v0[0] * v1[0])> {
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 TVector2<T0> &v0,
const T1 &s) -> TVector2<decltype(v0[0] * 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) -> TVector2<decltype(s * v0[0])> {
return TVector2<decltype(s * v0[0])>{s * v0[0], s * v0[1]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator*=(TVector2<T0> &v0,
const T1 &s) -> TVector2<T0>& {
v0[0] *= s;
v0[1] *= s;
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto operator*(const TVector3<T0> &v0,
const T1 &s) -> TVector3<decltype(v0[0] * 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) -> TVector3<decltype(s * v0[0])> {
return TVector3<decltype(s * v0[0])>{
s * v0[0], s * v0[1], s * v0[2]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator*=(TVector3<T0> &v0,
const T1 &s) -> TVector3<T0>& {
v0[0] *= s;
v0[1] *= s;
v0[2] *= s;
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto operator*=(TVector4<T0> &v0,
const T1 &s) -> TVector4<T0>& {
v0[0] *= s;
v0[1] *= s;
v0[2] *= s;
v0[3] *= s;
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto operator*(const TVector3<T0> &v0,
const TVector3<T1> &v1) -> TVector3<decltype(v0[0] * v1[0])> {
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 TVector4<T0> &v0,
const T1 &s) -> TVector4<decltype(v0[0] * s)> {
return TVector4<decltype(v0[0] * s)>{
v0[0] * s, v0[1] * s, v0[2] * s, v0[3] * s};
}
template <typename T0, typename T1>
DEVICE
inline auto operator*(const T0 &s,
const TVector4<T1> &v0) -> TVector4<decltype(s * v0[0])> {
return TVector4<decltype(s * v0[0])>{
s * v0[0], s * v0[1], s * v0[2], s * v0[3]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator*(const TVector4<T0> &v0,
const TVector4<T1> &v1) -> TVector4<decltype(v0[0] * v1[0])> {
return TVector4<decltype(v0[0] * v1[0])>{
v0[0] * v1[0], v0[1] * v1[1], v0[2] * v1[2], v0[3] * v1[3]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator/(const TVector2<T0> &v0,
const T1 &s) -> TVector2<decltype(v0[0] / 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) -> TVector3<decltype(v0[0] / s)> {
auto inv_s = 1.f / s;
return v0 * inv_s;
}
template <typename T0, typename T1>
DEVICE
inline auto operator/(const TVector4<T0> &v0,
const T1 &s) -> TVector4<decltype(v0[0] / s)> {
auto inv_s = 1.f / s;
return v0 * inv_s;
}
template <typename T0, typename T1>
DEVICE
inline auto operator/(const T0 &s,
const TVector3<T1> &v1) -> TVector3<decltype(s / v1[0])> {
return TVector3<decltype(s / v1[0])>{
s / v1[0], s / v1[2], s / v1[2]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator/(const TVector3<T0> &v0,
const TVector3<T1> &v1) -> TVector3<decltype(v0[0] / v1[0])> {
return TVector3<decltype(v0[0] / v1[0])>{
v0[0] / v1[0], v0[1] / v1[2], v0[2] / v1[2]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator/(const TVector2<T0> &v0,
const TVector2<T1> &v1) -> TVector2<decltype(v0[0] / v1[0])> {
return TVector2<decltype(v0[0] / v1[0])>{
v0[0] / v1[0], v0[1] / v1[1]};
}
template <typename T0, typename T1>
DEVICE
inline auto operator/=(TVector3<T0> &v0,
const T1 &s) -> TVector3<T0>& {
auto inv_s = 1.f / s;
v0[0] *= inv_s;
v0[1] *= inv_s;
v0[2] *= inv_s;
return v0;
}
template <typename T0, typename T1>
DEVICE
inline auto operator/=(TVector4<T0> &v0,
const T1 &s) -> TVector4<T0>& {
auto inv_s = 1.f / s;
v0[0] *= inv_s;
v0[1] *= inv_s;
v0[2] *= inv_s;
v0[3] *= inv_s;
return v0;
}
template <typename T0, typename T1>
DEVICE
inline bool operator==(const TVector2<T0> &v0,
const TVector2<T1> &v1) {
return v0.x == v1.x && v0.y == v1.y;
}
template <typename T0, typename T1>
DEVICE
inline bool operator==(const TVector3<T0> &v0,
const TVector3<T1> &v1) {
return v0.x == v1.x && v0.y == v1.y && v0.z == v1.z;
}
template <typename T0, typename T1>
DEVICE
inline bool operator!=(const TVector3<T0> &v0,
const TVector3<T1> &v1) {
return v0.x != v1.x || v0.y != v1.y || v0.z != v1.z;
}
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 TVector2<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 TVector2<T> d_length(const TVector2<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, T(d_l_sq));
}
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 TVector2<T0> &v0,
const TVector2<T1> &v1) -> decltype(length_squared(v1 - v0)) {
return length_squared(v1 - v0);
}
template <typename T0, typename T1>
DEVICE
inline auto distance_squared(const TVector3<T0> &v0,
const TVector3<T1> &v1) -> decltype(length_squared(v1 - v0)) {
return length_squared(v1 - v0);
}
template <typename T0, typename T1>
DEVICE
inline auto distance(const TVector2<T0> &v0,
const TVector2<T1> &v1) -> decltype(length(v1 - v0)) {
return length(v1 - v0);
}
template <typename T>
DEVICE
inline void d_distance(const TVector2<T> &v0,
const TVector2<T> &v1,
const T &d_output,
TVector2<T> &d_v0,
TVector2<T> &d_v1) {
auto d_v1_v0 = d_length(v1 - v0, d_output);
d_v0 -= d_v1_v0;
d_v1 += d_v1_v0;
}
template <typename T0, typename T1>
DEVICE
inline auto distance(const TVector3<T0> &v0,
const TVector3<T1> &v1) -> decltype(length(v1 - v0)) {
return length(v1 - v0);
}
template <typename T>
DEVICE
inline void d_distance(const TVector3<T> &v0,
const TVector3<T> &v1,
const T &d_output,
TVector3<T> &d_v0,
TVector3<T> &d_v1) {
auto d_v1_v0 = d_length(v1 - v0, d_output);
d_v0 -= d_v1_v0;
d_v1 += d_v1_v0;
}
template <typename T>
DEVICE
inline TVector2<T> normalize(const TVector2<T> &v0) {
return v0 / length(v0);
}
template <typename T>
DEVICE
inline TVector2<T> d_normalize(const TVector2<T> &v0, const TVector2<T> &d_n) {
auto l = length(v0);
auto n = v0 / l;
auto d_v0 = d_n / l;
auto d_l = -dot(d_n, n) / l;
// l = length(v0)
d_v0 += d_length(v0, d_l);
return d_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 = -dot(d_n, n) / l;
// l = length(v0)
d_v0 += d_length(v0, d_l);
return d_v0;
}
template <typename T0, typename T1>
DEVICE
inline auto dot(const TVector2<T0> &v0, const TVector2<T1> &v1) -> decltype(v0[0] * v1[0]) {
return v0[0] * v1[0] +
v0[1] * v1[1];
}
template <typename T0, typename T1>
DEVICE
inline auto dot(const TVector3<T0> &v0, const TVector3<T1> &v1) -> decltype(v0[0] * v1[0]) {
return v0[0] * v1[0] +
v0[1] * v1[1] +
v0[2] * v1[2];
}
template <typename T0, typename T1>
DEVICE
inline auto dot(const TVector4<T0> &v0, const TVector4<T1> &v1) -> decltype(v0[0] * v1[0]) {
return v0[0] * v1[0] +
v0[1] * v1[1] +
v0[2] * v1[2] +
v0[3] * v1[3];
}
template <typename T0, typename T1>
DEVICE
inline auto cross(const TVector3<T0> &v0, const TVector3<T1> &v1) -> TVector3<decltype(v0[1] * v1[2] - v0[2] * v1[1])> {
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 T luminance(const TVector3<T> &v) {
return 0.212671f * v[0] +
0.715160f * v[1] +
0.072169f * v[2];
}
template <typename T>
DEVICE
inline T sum(const T &v) {
return v;
}
template <typename T>
DEVICE
inline T sum(const TVector2<T> &v) {
return v[0] + v[1];
}
template <typename T>
DEVICE
inline T sum(const TVector3<T> &v) {
return v[0] + v[1] + v[2];
}
template <typename T>
DEVICE
inline T sum(const TVector4<T> &v) {
return v[0] + v[1] + v[2] + v[3];
}
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 isfinite(v.x) &&
isfinite(v.y);
}
DEVICE
inline bool isfinite(const Vector3 &v) {
return isfinite(v.x) &&
isfinite(v.y) &&
isfinite(v.z);
}
DEVICE
inline bool isfinite(const Vector4 &v) {
return isfinite(v.x) &&
isfinite(v.y) &&
isfinite(v.z) &&
isfinite(v.w);
}
DEVICE
inline bool is_zero(const Vector3 &v) {
return v.x == 0 && v.y == 0 && v.z == 0;
}
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] << ")";
}
template <typename T>
inline std::ostream& operator<<(std::ostream &os, const TVector4<T> &v) {
return os << "(" << v[0] << ", " << v[1] << ", " << v[2] << ", " << v[3] << ")";
}
DEVICE
inline
float det(const Vector2f &a, const Vector2f &b) {
return a.x*b.y-b.x*a.y;
}
DEVICE
inline
Vector2f quadratic_closest_pt_approx(const Vector2f &b0,
const Vector2f &b1,
const Vector2f &b2,
float *t_ = nullptr) {
// From http://w3.impa.br/~diego/publications/NehHop08.pdf
float a=det(b0,b2), b=2*det(b1,b0), d=2*det(b2,b1);
float f=b*d-a*a;
Vector2f d21=b2-b1, d10=b1-b0, d20=b2-b0;
Vector2f gf=2*(b*d21+d*d10+a*d20);
gf=Vector2f(gf.y,-gf.x);
Vector2f pp=-f*gf/dot(gf,gf);
Vector2f d0p=b0-pp;
float ap=det(d0p,d20), bp=2*det(d10,d0p);
float t=clamp((ap+bp)/(2*a+b+d),0.f,1.f);
float tt = 1 - t;
if (t_ != nullptr) {
*t_ = t;
}
return (tt*tt)*b0 + (2*tt*t)*b1 + (t*t)*b2;
}
DEVICE
inline
Vector2f quadratic_closest_pt_approx(const Vector2f &b0,
const Vector2f &b1,
const Vector2f &b2,
const Vector2f &pt,
float *t = nullptr) {
// Approximate closest point to a quadratic curve
return quadratic_closest_pt_approx(b0 - pt, b1 - pt, b2 - pt, t) + pt;
}