-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.cpp
165 lines (152 loc) · 5.49 KB
/
camera.cpp
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
#include "camera.h"
#include "parallel.h"
#include "test_utils.h"
#include "buffer.h"
#include <cmath>
struct primary_ray_sampler {
DEVICE void operator()(int idx) {
auto pixel_x = idx % camera.width;
auto pixel_y = idx / camera.width;
auto sample = samples[idx].xy;
auto screen_pos = Vector2{
(pixel_x + sample[0]) / Real(camera.width),
(pixel_y + sample[1]) / Real(camera.height)
};
rays[idx] = sample_primary(camera, screen_pos);
}
const Camera camera = Camera{};
const CameraSample *samples = nullptr;
Ray *rays = nullptr;
};
void sample_primary_rays(const Camera &camera,
const BufferView<CameraSample> &samples,
BufferView<Ray> rays,
bool use_gpu) {
parallel_for(primary_ray_sampler{camera, samples.begin(), rays.begin()},
samples.size(), use_gpu);
}
void accumulate_camera(const DCameraInst &d_camera_inst,
DCamera &d_camera,
bool use_gpu) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
d_camera.cam_to_world[4 * i + j] +=
d_camera_inst.cam_to_world(i, j);
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
d_camera.world_to_cam[4 * i + j] +=
d_camera_inst.world_to_cam(i, j);
}
}
*(d_camera.fov_factor) += d_camera_inst.fov_factor;
}
void test_sample_primary_rays(bool use_gpu) {
// Let's have a perspective camera with 1x1 pixel,
// with identity to world matrix,
// fov 45 degree
Matrix4x4f c2w = Matrix4x4f::identity();
Matrix4x4f w2c = Matrix4x4f::identity();
Camera camera{1, 1,
&c2w.data[0][0],
&w2c.data[0][0],
1,
1e-2f,
false};
parallel_init();
// Sample from the center of pixel
Buffer<CameraSample> samples(use_gpu, 1);
samples[0].xy = Vector2{0.5f, 0.5f};
Buffer<Ray> rays(use_gpu, 1);
sample_primary_rays(camera, samples.view(0, 1), rays.view(0, 1), use_gpu);
cuda_synchronize();
equal_or_error(__FILE__, __LINE__, rays[0].org, Vector3{0, 0, 0});
equal_or_error(__FILE__, __LINE__, rays[0].dir, Vector3{0, 0, 1});
parallel_cleanup();
}
void test_d_sample_primary_rays() {
Matrix4x4f c2w = Matrix4x4f::identity();
Matrix4x4f w2c = Matrix4x4f::identity();
Camera camera{1, 1,
&c2w.data[0][0],
&w2c.data[0][0],
1,
1e-2f,
false};
DCameraInst d_camera;
DRay d_ray{Vector3{1, 1, 1}, Vector3{1, 1, 1}};
d_sample_primary_ray(camera,
Vector2{0.5, 0.5},
d_ray,
d_camera);
// Compare with central difference
auto finite_delta = Real(1e-6);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
auto delta_camera = camera;
delta_camera.cam_to_world(i, j) += finite_delta;
auto positive_ray =
sample_primary(delta_camera, Vector2{0.5, 0.5});
delta_camera.cam_to_world(i, j) -= 2 * finite_delta;
auto negative_ray =
sample_primary(delta_camera, Vector2{0.5, 0.5});
auto diff = (sum(positive_ray.org - negative_ray.org) +
sum(positive_ray.dir - negative_ray.dir)) /
(2 * finite_delta);
equal_or_error(__FILE__, __LINE__, diff,
d_camera.cam_to_world(i, j));
}
}
auto delta_camera = camera;
delta_camera.fov_factor += finite_delta;
auto positive_ray = sample_primary(delta_camera, Vector2{0.5, 0.5});
delta_camera.fov_factor -= 2 * finite_delta;
auto negative_ray = sample_primary(delta_camera, Vector2{0.5, 0.5});
auto diff = (sum(positive_ray.org - negative_ray.org) +
sum(positive_ray.dir - negative_ray.dir)) /
(2 * finite_delta);
equal_or_error(__FILE__, __LINE__, diff, d_camera.fov_factor);
}
void test_d_camera_to_screen() {
Matrix4x4f c2w = Matrix4x4f::identity();
Matrix4x4f w2c = Matrix4x4f::identity();
Camera camera{1, 1,
&c2w.data[0][0],
&w2c.data[0][0],
1,
1e-2f,
false};
auto pt = Vector3{0.5, 0.5, 1.0};
auto dx = Real(1);
auto dy = Real(1);
auto d_camera = DCameraInst{};
auto d_pt = Vector3{0, 0, 0};
d_camera_to_screen(camera, pt, dx, dy,
d_camera, d_pt);
// Compare with central difference
auto finite_delta = Real(1e-6);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
auto delta_camera = camera;
delta_camera.cam_to_world(i, j) += finite_delta;
auto pxy = camera_to_screen(delta_camera, pt);
delta_camera.cam_to_world(i, j) -= 2 * finite_delta;
auto nxy = camera_to_screen(delta_camera, pt);
auto diff = sum(pxy - nxy) / (2 * finite_delta);
equal_or_error(__FILE__, __LINE__, diff,
d_camera.cam_to_world(i, j));
}
}
auto delta_camera = camera;
delta_camera.fov_factor += finite_delta;
auto pxy = camera_to_screen(delta_camera, pt);
delta_camera.fov_factor -= 2 * finite_delta;
auto nxy = camera_to_screen(delta_camera, pt);
auto diff = sum(pxy - nxy) / (2 * finite_delta);
equal_or_error(__FILE__, __LINE__, diff, d_camera.fov_factor);
}
void test_camera_derivatives() {
test_d_sample_primary_rays();
test_d_camera_to_screen();
}