This repository has been archived by the owner on Apr 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsh.hpp
241 lines (202 loc) · 4.77 KB
/
sh.hpp
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
// class SH: table of spherical harmonics coefficients,
// used to approximate a spherical function
#pragma once
#include <functional>
// #include "math/vecmath.hpp"
// #include "math/matmath.hpp"
// #include "image.hpp"
// typedef std::function<float(vec3f)> sphfunc;
const float PI = acos(-1);
template <int n>
struct matrix
{
// row major
float a[n][n] = {{0}};
};
template <int n>
matrix<n> operator* (const matrix<n>&, const matrix<n>&);
template <int n>
class SH;
template <int n>
class SymmSH
{
public:
// coefficients f(l,m=0)
float a[n] = {0};
SymmSH(){}
// from spherical function symmetric around z-axis (about theta)
SymmSH(std::function<float(float)>, int nsample = 100000);
// rotated to be centered around arbitrary axis z'
// SH<n> rotated(vec3f z);
};
struct TensorEntry
{
short a,b,c;
float val;
};
template <int n>
class SH
{
static const int lmax;
// static const float*** load_triple_product_tensor();
static int SHIndex(int l, int m) {
return l*l+l+m;
}
public:
static float Gamma[n*n][n*n][n*n];
static TensorEntry SparseGamma[];
static TensorEntry SquareSparseGamma[];
static int placeholder;
float a[n*n] = {0};
static const SH unit();
float& at(int l, int m) {
return a[SHIndex(l,m)];
}
float const& at(int l, int m) const {
return a[SHIndex(l,m)];
}
SH(){}
SH(SymmSH<n>);
// projection using sampling
// SH(sphfunc, int nsample = 1000000);
// reconstruction
// float eval(vec3f) const;
// rotation
// SH rotated(vec3f);
// suppress ringing
SH windowed();
SH squared();
// phi-theta visualization
// Image visualized(int yres = 200);
// projection of production as transformation M
matrix<n*n> prodMatrix() const;
float magnitude() const;
};
template <int n>
SymmSH<n> log(const SymmSH<n>& a);
template <int n>
float dot(const SH<n>&, const SH<n>&);
template <int n>
SH<n> log(const SH<n>&);
template <int n>
SH<n> exp(const SH<n>&);
template <int n>
SH<n> exp_OL(const SH<n>&);
template <int n>
SH<n> exp_HYB(SH<n>);
template <int n>
SH<n> exp_PS(const SH<n>&, int maxk=30);
// To use these functions, include shexp.hpp
template <int n>
SymmSH<n> operator+(const SymmSH<n>& a, const SymmSH<n>& b);
template <int n>
SymmSH<n> operator-(const SymmSH<n>& a, const SymmSH<n>& b);
template <int n>
SymmSH<n> operator*(float k, const SymmSH<n>& b);
template <int n>
SH<n> operator+(const SH<n>& a, const SH<n>& b);
template <int n>
SH<n> operator-(const SH<n>& a, const SH<n>& b);
template <int n>
SH<n> operator*(float k, const SH<n>& b);
template <int n>
SH<n> operator*(const SH<n>& a, const SH<n>& b);
template <int n>
SH<n> operator*(const matrix<n*n>& a, const SH<n>& b);
// ============== implementation starts ==============
// #include "mtsampler.hpp"
// void SHEvaluate(const vec3f &w, int lmax, float *out);
// void SHRotate(const Color *c_in, Color *c_out, const mat4f &m, int lmax);
inline int SHIndex(int l, int m) {
return l*l+l+m;
}
// #include "shproject.hpp"
// #include "shrotate.hpp"
// #include "shproduct.hpp"
// #include "shlog.hpp"
template <int n>
const int SH<n>::lmax = n-1;
template <int n>
const SH<n> SH<n>::unit()
{
SH<n> a;
a.a[0] = std::sqrt(4*PI);
return a;
}
// convert symmetric to general
template <int n>
SH<n>::SH(SymmSH<n> b)
{
for (int i=0; i<n; ++i)
at(i,0) = b.a[i];
}
// windowing
template <int n>
SH<n> SH<n>::windowed()
{
SH<n> a = *this;
for (int l=0; l<n; ++l) {
float alpha = cos(PI/2*(l/(2.0f*n)));
for (int m = -l; m <= l; ++m)
a.at(l,m) *= alpha;
}
return a;
}
template <int n>
SH<n> operator+(const SH<n>& a, const SH<n>& b)
{
SH<n> c;
for (int l=0; l<n; ++l)
for (int m=-l; m<=l; ++m)
c.at(l,m) = a.at(l,m) + b.at(l,m);
return c;
}
template <int n>
SH<n> operator-(const SH<n>& a, const SH<n>& b)
{
SH<n> c;
for (int l=0; l<n; ++l)
for (int m=-l; m<=l; ++m)
c.at(l,m) = a.at(l,m) - b.at(l,m);
return c;
}
template <int n>
SH<n> operator*(float k, const SH<n>& b)
{
SH<n> c = b;
for (int i=0; i<n*n; ++i)
c.a[i] *= k;
return c;
}
template <int n>
float SH<n>::magnitude() const
{
float t = 0;
for (int i=0; i<n*n; ++i)
t += a[i] * a[i];
return std::sqrt(t);
}
template <int n>
SymmSH<n> operator+(const SymmSH<n>& a, const SymmSH<n>& b)
{
SymmSH<n> c;
for (int l=0; l<n; ++l)
c.a[l] = a.a[l] + b.a[l];
return c;
}
template <int n>
SymmSH<n> operator-(const SymmSH<n>& a, const SymmSH<n>& b)
{
SymmSH<n> c;
for (int l=0; l<n; ++l)
c.a[l] = a.a[l] - b.a[l];
return c;
}
template <int n>
SymmSH<n> operator*(float k, const SymmSH<n>& b)
{
SymmSH<n> c = b;
for (int i=0; i<n; ++i)
c.a[i] *= k;
return c;
}