-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.h
113 lines (108 loc) · 4.65 KB
/
transform.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
#pragma once
#include "redner.h"
#include "vector.h"
#include "matrix.h"
template <typename T>
DEVICE
inline TVector3<T> xfm_point(const TMatrix4x4<T> &xform,
const TVector3<T> &pt) {
auto tpt = TVector4<T>{
xform(0, 0) * pt[0] + xform(0, 1) * pt[1] + xform(0, 2) * pt[2] + xform(0, 3),
xform(1, 0) * pt[0] + xform(1, 1) * pt[1] + xform(1, 2) * pt[2] + xform(1, 3),
xform(2, 0) * pt[0] + xform(2, 1) * pt[1] + xform(2, 2) * pt[2] + xform(2, 3),
xform(3, 0) * pt[0] + xform(3, 1) * pt[1] + xform(3, 2) * pt[2] + xform(3, 3)};
auto inv_w = 1.f / tpt[3];
return TVector3<T>{tpt[0], tpt[1], tpt[2]} * inv_w;
}
template <typename T>
DEVICE
inline auto xfm_vector(const TMatrix4x4<T> &xform,
const TVector3<T> &vec) {
return TVector3<T>{
xform(0, 0) * vec[0] + xform(0, 1) * vec[1] + xform(0, 2) * vec[2],
xform(1, 0) * vec[0] + xform(1, 1) * vec[1] + xform(1, 2) * vec[2],
xform(2, 0) * vec[0] + xform(2, 1) * vec[1] + xform(2, 2) * vec[2]};
}
template <typename T>
DEVICE
inline void d_xfm_point(const TMatrix4x4<T> &xform,
const TVector3<T> &pt,
const TVector3<T> &d_out,
TMatrix4x4<T> &d_xform,
TVector3<T> &d_pt) {
auto tpt = TVector4<T>{
xform(0, 0) * pt[0] + xform(0, 1) * pt[1] + xform(0, 2) * pt[2] + xform(0, 3),
xform(1, 0) * pt[0] + xform(1, 1) * pt[1] + xform(1, 2) * pt[2] + xform(1, 3),
xform(2, 0) * pt[0] + xform(2, 1) * pt[1] + xform(2, 2) * pt[2] + xform(2, 3),
xform(3, 0) * pt[0] + xform(3, 1) * pt[1] + xform(3, 2) * pt[2] + xform(3, 3)};
auto inv_w = 1.f / tpt[3];
// out = TVector3<T>{tpt[0], tpt[1], tpt[2]} * inv_w
auto d_tpt03 = d_out * inv_w;
auto d_inv_w = sum(d_out * TVector3<T>{tpt[0], tpt[1], tpt[2]});
// inv_w = 1.f / tpt[3]
auto d_tpt3 = -d_inv_w * inv_w / tpt[3];
auto d_tpt = TVector4<T>{d_tpt03[0], d_tpt03[1], d_tpt03[2], d_tpt3};
// auto tpt = TVector4<T>{
// xform(0, 0) * pt[0] + xform(0, 1) * pt[1] + xform(0, 2) * pt[2] + xform(0, 3),
// xform(1, 0) * pt[0] + xform(1, 1) * pt[1] + xform(1, 2) * pt[2] + xform(1, 3),
// xform(2, 0) * pt[0] + xform(2, 1) * pt[1] + xform(2, 2) * pt[2] + xform(2, 3),
// xform(3, 0) * pt[0] + xform(3, 1) * pt[1] + xform(3, 2) * pt[2] + xform(3, 3)};
d_xform(0, 0) += d_tpt[0] * pt[0];
d_xform(0, 1) += d_tpt[0] * pt[1];
d_xform(0, 2) += d_tpt[0] * pt[2];
d_xform(0, 3) += d_tpt[0];
d_xform(1, 0) += d_tpt[1] * pt[0];
d_xform(1, 1) += d_tpt[1] * pt[1];
d_xform(1, 2) += d_tpt[1] * pt[2];
d_xform(1, 3) += d_tpt[1];
d_xform(2, 0) += d_tpt[2] * pt[0];
d_xform(2, 1) += d_tpt[2] * pt[1];
d_xform(2, 2) += d_tpt[2] * pt[2];
d_xform(2, 3) += d_tpt[2];
d_xform(3, 0) += d_tpt[3] * pt[0];
d_xform(3, 1) += d_tpt[3] * pt[1];
d_xform(3, 2) += d_tpt[3] * pt[2];
d_xform(3, 3) += d_tpt[3];
d_pt[0] += d_tpt[0] * xform(0, 0) +
d_tpt[1] * xform(1, 0) +
d_tpt[2] * xform(2, 0) +
d_tpt[3] * xform(3, 0);
d_pt[1] += d_tpt[0] * xform(0, 1) +
d_tpt[1] * xform(1, 1) +
d_tpt[2] * xform(2, 1) +
d_tpt[3] * xform(3, 1);
d_pt[2] += d_tpt[0] * xform(0, 2) +
d_tpt[1] * xform(1, 2) +
d_tpt[2] * xform(2, 2) +
d_tpt[3] * xform(3, 2);
}
template <typename T>
DEVICE
inline void d_xfm_vector(const TMatrix4x4<T> &xform,
const TVector3<T> &vec,
const TVector3<T> &d_out,
TMatrix4x4<T> &d_xform,
TVector3<T> &d_vec) {
// out =
// xform(0, 0) * vec[0] + xform(0, 1) * vec[1] + xform(0, 2) * vec[2]
// xform(1, 0) * vec[0] + xform(1, 1) * vec[1] + xform(1, 2) * vec[2]
// xform(2, 0) * vec[0] + xform(2, 1) * vec[1] + xform(2, 2) * vec[2]
d_xform(0, 0) += d_out[0] * vec[0];
d_xform(0, 1) += d_out[0] * vec[1];
d_xform(0, 2) += d_out[0] * vec[2];
d_xform(1, 0) += d_out[1] * vec[0];
d_xform(1, 1) += d_out[1] * vec[1];
d_xform(1, 2) += d_out[1] * vec[2];
d_xform(2, 0) += d_out[2] * vec[0];
d_xform(2, 1) += d_out[2] * vec[1];
d_xform(2, 2) += d_out[2] * vec[2];
d_vec[0] += d_out[0] * xform(0, 0) +
d_out[1] * xform(1, 0) +
d_out[2] * xform(2, 0);
d_vec[1] += d_out[0] * xform(0, 1) +
d_out[1] * xform(1, 1) +
d_out[2] * xform(2, 1);
d_vec[2] += d_out[0] * xform(0, 2) +
d_out[1] * xform(1, 2) +
d_out[2] * xform(2, 2);
}