forked from BachiLi/redner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.h
86 lines (75 loc) · 1.98 KB
/
frame.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
#pragma once
#include "redner.h"
#include "vector.h"
template <typename T>
struct TFrame {
DEVICE TFrame() {}
template <typename T2>
DEVICE
TFrame(const TVector3<T2> &x,
const TVector3<T2> &y,
const TVector3<T2> &n)
: x(x), y(y), n(n) {}
template <typename T2>
DEVICE
TFrame(const TVector3<T2> &n) : n(n) {
coordinate_system(n, x, y);
}
DEVICE
inline TVector3<T>& operator[](int i) {
return *(&x + i);
}
DEVICE
inline const TVector3<T>& operator[](int i) const {
return *(&x + i);
}
TVector3<T> x, y, n;
};
using Frame = TFrame<Real>;
template <typename T>
DEVICE
inline TFrame<T> operator+=(TFrame<T> &f0, const TFrame<T> &f1) {
f0.x += f1.x;
f0.y += f1.y;
f0.n += f1.n;
return f0;
}
template <typename T>
DEVICE
inline TFrame<T> operator-(const TFrame<T> &frame) {
return TFrame<T>{-frame.x, -frame.y, -frame.n};
}
template <typename T0, typename T1>
DEVICE
inline auto to_local(const TFrame<T0> &frame,
const TVector3<T1> &v) {
return TVector3<decltype(dot(v, frame[0]))>{
dot(v, frame[0]),
dot(v, frame[1]),
dot(v, frame[2])};
}
template <typename T0, typename T1>
DEVICE
inline auto to_world(const TFrame<T0> &frame,
const TVector3<T1> &v) {
return frame[0] * v[0] +
frame[1] * v[1] +
frame[2] * v[2];
}
template <typename T>
DEVICE
inline void d_to_world(const TFrame<T> &frame,
const TVector3<T> &v,
const TVector3<T> &d_dir,
TFrame<T> &d_frame,
TVector3<T> &d_v) {
// dir[i] = frame[0][i] * v[0] +
// frame[1][i] * v[1] +
// frame[2][i] * v[2]
d_frame[0] += d_dir * v[0];
d_frame[1] += d_dir * v[1];
d_frame[2] += d_dir * v[2];
d_v[0] += sum(d_dir * frame[0]);
d_v[1] += sum(d_dir * frame[1]);
d_v[2] += sum(d_dir * frame[2]);
}