-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathray_utils.py
More file actions
351 lines (282 loc) · 13.2 KB
/
ray_utils.py
File metadata and controls
351 lines (282 loc) · 13.2 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
"""Utility functions for ray computation."""
import math
import numpy as np
from scipy.spatial.transform import Rotation as R
import box_utils
import torch
def apply_batched_transformations(inputs, transformations):
"""Batched transformation of inputs.
Args:
inputs: List of [R, S, 3]
transformations: [R, 4, 4]
Returns:
transformed_inputs: List of [R, S, 3]
"""
# if rotation_only:
# transformations[:, :3, 3] = torch.zeros((3,), dtype=torch.float)
transformed_inputs = []
for x in inputs:
N_samples = x.size()[1]
homog_transformations = transformations.unsqueeze(1) # [R, 1, 4, 4]
homog_transformations = torch.tile(homog_transformations, (1, N_samples, 1, 1)) # [R, S, 4, 4]
homog_component = torch.ones_like(x)[..., 0:1] # [R, S, 1]
homog_x = torch.cat((x, homog_component), axis=-1) # [R, S, 4]
homog_x = homog_x.unsqueeze(2)
transformed_x = torch.matmul(
homog_x,
torch.transpose(homog_transformations, 2, 3)) # [R, S, 1, 4]
transformed_x = transformed_x[..., 0, :3] # [R, S, 3]
transformed_inputs.append(transformed_x)
return transformed_inputs
def get_transformation_from_params(params):
translation, rotation = [0, 0, 0], [0, 0, 0]
if 'translation' in params:
translation = params['translation']
if 'rotation' in params:
rotation = params['rotation']
translation = torch.tensor(translation, dtype=torch.float)
rotmat = torch.tensor(R.from_euler('xyz', rotation, degrees=True).as_matrix(), dtype=torch.float)
return translation, rotmat
def rotate_dirs(dirs, rotmat):
"""
Args:
dirs: [R, 3] float tensor.
rotmat: [3, 3]
"""
if type(dirs) == np.ndarray:
dirs = torch.tensor(dirs).float()
#rotmat = rotmat.unsqueeze(0)
rotmat = torch.broadcast_to(rotmat, (dirs.shape[0], 3, 3)) # [R, 3, 3]
dirs_obj = torch.matmul(dirs.unsqueeze(1), torch.transpose(rotmat, 1, 2)) # [R, 1, 3]
dirs_obj = dirs_obj.squeeze(1) # [R, 3]
return dirs_obj
def transform_dirs(dirs, params, inverse=False):
_, rotmat = get_transformation_from_params(params) # [3,], [3, 3]
if inverse:
rotmat = torch.transpose(rotmat, 0, 1) # [3, 3]
dirs_transformed = rotate_dirs(dirs, rotmat)
return dirs_transformed
def transform_rays(ray_batch, params, use_viewdirs, inverse=False):
"""Transform rays into object coordinate frame given o2w transformation params.
Note: do not assume viewdirs is always the normalized version of rays_d (e.g., in staticcam case).
Args:
ray_batch: [R, M] float tensor. Batch of rays.
params: Dictionary containing transformation parameters:
'translation': List of 3 elements. xyz translation.
'rotation': List of 3 euler angles in xyz.
use_viewdirs: bool. Whether to we are using viewdirs.
inverse: bool. Whether to apply inverse of the transformations provided in 'params'.
Returns:
ray_batch_obj: [R, M] float tensor. The ray batch, in object coordinate frame.
"""
rays_o, rays_d = ray_batch[:, 0:3], ray_batch[:, 3:6]
translation, rotmat = get_transformation_from_params(params) # [3,], [3, 3]
if inverse:
translation = -1 * translation # [3,]
rotmat = torch.transpose(rotmat, 1, 0) # [3, 3]
translation_inverse = -1 * translation
rotmat_inverse = torch.transpose(rotmat, 1, 0)
# Transform the ray origin.
rays_o_obj, _ = box_utils.ray_to_box_coordinate_frame_pairwise(
box_center=translation_inverse,
box_rotation_matrix=rotmat_inverse,
rays_start_point=rays_o,
rays_end_point=rays_d)
# Only apply rotation to rays_d.
rays_d_obj = rotate_dirs(rays_d, rotmat)
ray_batch_obj = update_ray_batch_slice(ray_batch, rays_o_obj, 0, 3)
ray_batch_obj = update_ray_batch_slice(ray_batch_obj, rays_d_obj, 3, 6)
if use_viewdirs:
# Grab viewdirs from the ray batch itself. Because it may be different from rays_d
# (as in the staticcam case).
viewdirs = ray_batch[:, 8:11]
viewdirs_obj = rotate_dirs(viewdirs, rotmat)
ray_batch_obj = update_ray_batch_slice(ray_batch_obj, viewdirs_obj, 8, 11)
return ray_batch_obj
def transform_points_into_world_coordinate_frame(pts, params, check_numerics=False):
translation, rotmat = get_transformation_from_params(params) # [3,], [3, 3]
# pts_flat = pts.view(-1, 3) # [RS, 3]
# num_examples = pts_flat.size()[0] # RS
# translation = translation.unsqueeze(0)
# translation = torch.tile(translation, (num_examples, 1)) # [RS, 3]
# rotmat = rotmat.unsqueeze(0)
# rotmat = torch.tile(rotmat, (num_examples, 1, 1))
# # pts_flat_transformed = torch.matmul(pts_flat[:, None, :], torch.transpose(rotmat, 2, 1)) # [RS, 1, 3]
# pts_flat_transformed = pts_flat[:, None, :] # [RS, 1, 3]
# pts_flat_transformed += translation[:, None, :] # [RS, 1, 3]
# pts_transformed = pts_flat_transformed.view(pts.size()) # [R, S, 3]
chunk = 256
# Check batch transformations works without rotation.
if check_numerics:
transformations = np.eye(4)
transformations[:3, 3] = translation
transformations = torch.tensor(transformations, dtype=torch.float) # [4, 4]
transformations = torch.tile(transformations[None, ...], (pts.size()[0], 1, 1)) # [R, 4, 4]
pts_transformed1 = []
for i in range(0, pts.size()[0], chunk):
pts_transformed1_chunk = apply_batched_transformations(
inputs=[pts[i:i+chunk]], transformations=transformations[i:i+chunk])[0]
pts_transformed1.append(pts_transformed1_chunk)
pts_transformed1 = torch.cat(pts_transformed1, dim=0)
pts_transformed2 = pts + translation[None, None, :]
# Now add rotation
transformations = np.eye(4)
transformations = torch.tensor(transformations, dtype=torch.float)
transformations[:3, :3] = rotmat
transformations[:3, 3] = translation
#transformations = torch.tensor(transformations, dtype=torch.float) # [4, 4]
transformations = torch.tile(transformations[None, ...], (pts.size()[0], 1, 1)) # [R, 4, 4]
pts_transformed = []
for i in range(0, pts.size()[0], chunk):
pts_transformed_chunk = apply_batched_transformations(
inputs=[pts[i:i+chunk]], transformations=transformations[i:i+chunk])[0]
pts_transformed.append(pts_transformed_chunk)
pts_transformed = torch.cat(pts_transformed, dim=0)
return pts_transformed
# def transform_rays(ray_batch, translation, use_viewdirs):
# """Apply transformation to rays.
# Args:
# ray_batch: [R, M] float tensor. All information necessary
# for sampling along a ray, including: ray origin, ray direction, min
# dist, max dist, and unit-magnitude viewing direction.
# translation: [3,] float tensor. The (x, y, z) translation to apply.
# use_viewdirs: Whether to use view directions.
# Returns:
# ray_batch: [R, M] float tensor. Transformed ray batch.
# """
# assert translation.size()[0] == 3, "translation.size()[0] must be 3..."
# # Since we are only supporting translation for now, only ray origins need to be
# # modified. Ray directions do not need to change.
# rays_o = ray_batch[:, 0:3] + translation
# rays_remaining = ray_batch[:, 3:]
# ray_batch = torch.cat((rays_o, rays_remaining), dim=1)
# return ray_batch
def compute_rays_length(rays_d):
"""Compute ray length.
Args:
rays_d: [R, 3] float tensor. Ray directions.
Returns:
rays_length: [R, 1] float tensor. Ray lengths.
"""
rays_length = torch.norm(rays_d, dim=-1, keepdim=True) # [N_rays, 1]
return rays_length
def normalize_rays(rays):
"""Normalize ray directions.
Args:
rays: [R, 3] float tensor. Ray directions.
Returns:
normalized_rays: [R, 3] float tensor. Normalized ray directions.
"""
normalized_rays = rays / compute_rays_length(rays_d=rays)
return normalized_rays
def compute_ray_dirs_and_length(rays_o, rays_dst):
"""Compute ray directions.
Args:
rays_o: [R, 3] float tensor. Ray origins.
rays_dst: [R, 3] float tensor. Ray destinations.
Returns:
rays_d: [R, 3] float tensor. Normalized ray directions.
"""
# The ray directions are the difference between the ray destinations and the
# ray origins.
rays_d = rays_dst - rays_o # [R, 3] # Direction out of light source
# Compute the length of the rays.
rays_length = compute_rays_length(rays_d=rays_d)
# Normalized the ray directions.
rays_d = rays_d / rays_length # [R, 3] # Normalize direction
return rays_d, rays_length
def update_ray_batch_slice(ray_batch, x, start, end):
left = ray_batch[:, :start] # [R, ?]
right = ray_batch[:, end:] # [R, ?]
updated_ray_batch = torch.cat((left, x, right), dim=-1)
return updated_ray_batch
def update_ray_batch_bounds(ray_batch, bounds):
updated_ray_batch = update_ray_batch_slice(ray_batch=ray_batch, x=bounds,
start=6, end=8)
return updated_ray_batch
def create_ray_batch(
rays_o, rays_dst, rays_i, use_viewdirs, rays_near=None, rays_far=None, epsilon=1e-10):
# Compute the ray directions.
rays_d = rays_dst - rays_o # [R,3] # Direction out of light source
rays_length = compute_rays_length(rays_d=rays_d) # [R, 1]
rays_d = rays_d / rays_length # [R, 3] # Normalize direction
viewdirs = rays_d # [R, 3]
# If bounds are not provided, set the beginning and end of ray as sampling bounds.
if rays_near is None:
rays_near = torch.zeros((rays_o.size()[0], 1), dtype=torch.float) + epsilon # [R, 1]
if rays_far is None:
rays_far = rays_length # [R, 1]
ray_batch = torch.cat((rays_o, rays_d, rays_near, rays_far), dim=-1)
if use_viewdirs:
ray_batch = torch.cat((ray_batch, viewdirs), dim=-1)
ray_batch = torch.cat((ray_batch, rays_i), dim=-1)
return ray_batch
def sample_random_lightdirs(num_rays, num_samples, upper_only=False):
"""Randomly sample directions in the unit sphere.
Args:
num_rays: int or tensor shape dimension. Number of rays.
num_samples: int or tensor shape dimension. Number of samples per ray.
upper_only: bool. Whether to sample only on the upper hemisphere.
Returns:
lightdirs: [R, S, 3] float tensor. Random light directions sampled from the unit
sphere for each sampled point.
"""
if upper_only:
min_z = 0
else:
min_z = -1
phi = torch.rand(num_rays, num_samples) * (2 * math.pi) # [R, S]
cos_theta = torch.rand(num_rays, num_samples) * (1 - min_z) + min_z # [R, S]
theta = torch.acos(cos_theta) # [R, S]
x = torch.sin(theta) * torch.cos(phi)
y = torch.sin(theta) * torch.sin(phi)
z = torch.cos(theta)
lightdirs = torch.cat((x[..., None], y[..., None], z[..., None]), dim=-1) # [R, S, 3]
return lightdirs
def get_light_positions(rays_i, img_light_pos):
"""Extracts light positions given scene IDs.
Args:
rays_i: [R, 1] float tensor. Per-ray image IDs.
img_light_pos: [N, 3] float tensor. Per-image light positions.
Returns:
rays_light_pos: [R, 3] float tensor. Per-ray light positions.
"""
#print("img_light_pos shape: ", img_light_pos.shape)
rays_light_pos = img_light_pos[rays_i.long()].squeeze() # [R, 3]
return rays_light_pos
def get_lightdirs(lightdirs_method, num_rays=None, num_samples=None, rays_i=None,
metadata=None, ray_batch=None, use_viewdirs=False, normalize=False):
"""Compute lightdirs.
Args:
lightdirs_method: str. Method to use for computing lightdirs.
num_rays: int or tensor shape dimension. Number of rays.
num_samples: int or tensor shape dimension. Number of samples per ray.
rays_i: [R, 1] float tensor. Ray image IDs.
metadata: [N, 3] float tensor. Metadata about each image. Currently only light
position is provided.
ray_batch: [R, M] float tensor. Ray batch.
use_viewdirs: bool. Whether to use viewdirs.
normalize: bool. Whether to normalize lightdirs.
Returns;
lightdirs: [R, S, 3] float tensor. Light directions for each sample.
"""
if lightdirs_method == 'viewdirs':
raise NotImplementedError
assert use_viewdirs
lightdirs = ray_batch[:, 8:11] # [R, 3]
lightdirs *= 1.5
lightdirs = torch.tile(lightdirs[:, None, :], (1, num_samples, 1))
elif lightdirs_method == 'metadata':
lightdirs = get_light_positions(rays_i, metadata) # [R, 3]
lightdirs = torch.tile(lightdirs[:, None, :], (1, num_samples, 1)) # [R, S, 3]
elif lightdirs_method == 'random':
lightdirs = sample_random_lightdirs(num_rays, num_samples) # [R, S, 3]
elif lightdirs_method == 'random_upper':
lightdirs = sample_random_lightdirs(num_rays, num_samples, upper_only=True) # [R, S, 3]
else:
raise ValueError(f'Invalid lightdirs_method: {lightdirs_method}.')
if normalize:
lightdirs_flat = lightdirs.view(-1, 3) # [RS, 3]
lightdirs_flat = normalize_rays(lightdirs_flat) # [RS, 3]
lightdirs = lightdirs_flat.view(lightdirs.size()) # [R, S, 3]
return lightdirs