-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
160 lines (132 loc) · 4.38 KB
/
util.py
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
import numpy as np
from camera import get_proj_mat_J
SH_C0 = 0.28209479177387814
SH_C1 = 0.4886025119029199
SH_C2 = [
1.0925484305920792,
-1.0925484305920792,
0.31539156525252005,
-1.0925484305920792,
0.5462742152960396,
]
SH_C3 = [
-0.5900435899266435,
2.890611442640554,
-0.4570457994644658,
0.3731763325901154,
-0.4570457994644658,
1.445305721320277,
-0.5900435899266435,
]
def computeColorFromSH(deg, pos, campos, sh):
# The implementation is loosely based on code for
# "Differentiable Point-Based Radiance Fields for
# Efficient View Synthesis" by Zhang et al. (2022)
dir = pos - campos
dir = dir / np.linalg.norm(dir)
result = SH_C0 * sh[0]
if deg > 0:
x, y, z = dir
result = result - SH_C1 * y * sh[1] + SH_C1 * z * sh[2] - SH_C1 * x * sh[3]
if deg > 1:
xx = x * x
yy = y * y
zz = z * z
xy = x * y
yz = y * z
xz = x * z
result = (
result
+ SH_C2[0] * xy * sh[4]
+ SH_C2[1] * yz * sh[5]
+ SH_C2[2] * (2.0 * zz - xx - yy) * sh[6]
+ SH_C2[3] * xz * sh[7]
+ SH_C2[4] * (xx - yy) * sh[8]
)
if deg > 2:
result = (
result
+ SH_C3[0] * y * (3.0 * xx - yy) * sh[9]
+ SH_C3[1] * xy * z * sh[10]
+ SH_C3[2] * y * (4.0 * zz - xx - yy) * sh[11]
+ SH_C3[3] * z * (2.0 * zz - 3.0 * xx - 3.0 * yy) * sh[12]
+ SH_C3[4] * x * (4.0 * zz - xx - yy) * sh[13]
+ SH_C3[5] * z * (xx - yy) * sh[14]
+ SH_C3[6] * x * (xx - 3.0 * yy) * sh[15]
)
result += 0.5
return np.clip(result, a_min=0, a_max=1)
def ndc2Pix(v, S):
return ((v + 1.0) * S - 1.0) * 0.5
def in_cutoff(p_orig, viewmatrix):
# bring point to screen space
p_view = transformPoint4x3(p_orig, viewmatrix)
if p_view[2] <= 0.2:
return None
return p_view
def transformPoint4x4(p, matrix):
matrix = np.array(matrix).flatten(order="F")
x, y, z = p
transformed = np.array(
[
matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12],
matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13],
matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14],
matrix[3] * x + matrix[7] * y + matrix[11] * z + matrix[15],
]
)
return transformed
def transformPoint4x3(p, matrix):
matrix = np.array(matrix).flatten(order="F")
x, y, z = p
transformed = np.array(
[
matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12],
matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13],
matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14],
]
)
return transformed
def computeCov3D(scale, mod, rot):
S = np.array(
[[scale[0] * mod, 0, 0], [0, scale[1] * mod, 0], [0, 0, scale[2] * mod]]
)
R = rot
M = np.dot(R, S)
cov3D = np.dot(M, M.T)
return cov3D
def computeCov2D(mean, focal_x, focal_y, tan_fovx, tan_fovy, cov3D, viewmatrix):
# The following models the steps outlined by equations 29
# and 31 in "EWA Splatting" (Zwicker et al., 2002).
# Additionally considers aspect / scaling of viewport.
# Transposes used to account for row-/column-major conventions.
t = transformPoint4x3(mean, viewmatrix)
limx = 1.3 * tan_fovx
limy = 1.3 * tan_fovy
txtz = t[0] / t[2]
tytz = t[1] / t[2]
t[0] = min(limx, max(-limx, txtz)) * t[2]
t[1] = min(limy, max(-limy, tytz)) * t[2]
J = np.array(
[
[focal_x / t[2], 0, -(focal_x * t[0]) / (t[2] * t[2])],
[0, focal_y / t[2], -(focal_y * t[1]) / (t[2] * t[2])],
[0, 0, 0],
]
)
W = viewmatrix[:3, :3]
T = np.dot(J, W)
cov = np.dot(T, cov3D)
cov = np.dot(cov, T.T)
# Apply low-pass filter
# Every Gaussia should be at least one pixel wide/high
# Discard 3rd row and column
cov[0, 0] += 0.3
cov[1, 1] += 0.3
return [cov[0, 0], cov[0, 1], cov[1, 1]]
if __name__ == "__main__":
deg = 3
pos = np.array([2, 0, -2])
campos = np.array([0, 0, 5])
sh = np.random.random((16, 3))
computeColorFromSH(deg, pos, campos, sh)