-
Notifications
You must be signed in to change notification settings - Fork 1
/
light_transport_matrix.cc
149 lines (115 loc) · 4.07 KB
/
light_transport_matrix.cc
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
#include "light_transport_matrix.h"
#include "scene.h"
#include "shape.h"
#include "aggregate.h"
#include "camera.h"
#include "brdf_point_light.h"
#include "stats.h"
namespace Renzoku {
LightTransportMatrix::LightTransportMatrix(int rows, int cols) {
m.resize(rows, cols);
}
void LightTransportMatrix::fill(Scene *scene,
IVirtualPointLightEvaluator *evaluator,
BrdfPointLights &vpls,
Pixels &pixels) {
Camera *camera = scene->get_camera();
Aggregate *agg = scene->get_aggregate();
Random &rd = *scene->get_random();
Float tmin = scene->get_tmin();
Float max_tmax = scene->get_max_tmax();
Float tick = scene->get_tick();
Lights &lights = *scene->get_lights();
m.resize(pixels.size(), vpls.size());
for (int ridx = 0; ridx < pixels.size(); ++ridx) {
Vec2 pixel = pixels[ridx];
Ray r = camera->shoot_ray(pixel.y(), pixel.x());
HitRecord rec;
if (! agg->hit(r, tmin, max_tmax, tick, rec)) continue;
// skip pixels that hit the light source directly since light source does not reflect light so
// VPLs won't affect the light source.
if (rec.light) continue;
Receiver recv(r, rec);
for (int k = 0; k < vpls.size(); ++k) {
Rgb radiance = evaluator->radiance(scene, recv, vpls[k]);
(this->m)(ridx, k) = radiance;
}
}
}
void LightTransportMatrix::get_column(int i, vector<Float> &c) const {
int rows = m.get_rows();
if (c.size() != rows * 3)
c.resize(rows * 3);
for (int r = 0; r < rows; ++r) {
Rgb v = m(r, i);
c[3 * r ] = v.red();
c[3 * r + 1] = v.green();
c[3 * r + 2] = v.blue();
}
}
void LightTransportMatrix::set_column(int i, const vector<Float> &c) {
int rows = m.get_rows();
for (int r = 0; r < rows; ++r) {
m(r, i) = Rgb(c[3 * r], c[3 * r + 1], c[3 * r + 2]);
}
}
void LightTransportMatrix::generate_alphas_for_clustering(vector<Float> &alphas) {
int rows = m.get_rows();
int cols = m.get_cols();
MatrixC<Float> R(rows, cols);
m.flatten_magnitude(R);
// alpha = (n_R^t * n_R - R^t * R) 1
// = (n_R^t * n_R * 1 - R^t * R * 1)
MatrixC<Float> column_norms(1, cols);
for (int j = 0; j < cols; ++j) {
Float sum = 0.0f;
for (int i = 0; i < rows; ++i) {
sum += R(i, j) * R(i, j);
}
column_norms(0, j) = sqrt(sum);
}
MatrixC<Float> Rt(cols, rows);
R.transpose(Rt);
vector<Float> u(rows);
R.row_sum(u); // u = R * 1
vector<Float> v(cols);
Rt.mul(u, v); // v = Rt * u
vector<Float> c; // n_R * 1
column_norms.row_sum(c);
alphas.resize(cols);
for (int i = 0; i < cols; ++i) {
alphas[i] = column_norms(0, i) * c[0] - v[i];
}
}
Float LightTransportMatrix::get_column_norm(int i) const {
Float magnitude = 0.0f;
for (int r = 0; r < m.get_rows(); ++r) {
Rgb v = m(r, i);
magnitude += v.red() * v.red() + v.green() * v.green() + v.blue() * v.blue();
}
return sqrt(magnitude);
}
void LightTransportMatrix::get_column_norms(vector<Float> &norms) const {
norms.resize(m.get_cols());
for (int c = 0; c < m.get_cols(); ++c) {
Float magnitude = 0.f;
for (int r = 0; r < m.get_rows(); ++r) {
Rgb v = m(r, c);
magnitude += v.red() * v.red() + v.green() * v.green() + v.blue() * v.blue();
}
norms[c] = sqrt(magnitude);
}
}
int LightTransportMatrix::get_cols() const {
return m.get_cols();
}
int LightTransportMatrix::get_rows() const {
return m.get_rows();
}
void LightTransportMatrix::resize(int rows, int cols) {
m.resize(rows, cols);
}
void LightTransportMatrix::save(const char *file) const {
m.save(file);
}
} // end namespace