/
plenoxels.py
1375 lines (1163 loc) · 63 KB
/
plenoxels.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import functools
import math
import random
import matplotlib.pyplot as plt
from matplotlib import use as mpl_use
import numpy as np
import torch
import torch.nn as nn
import pathos.multiprocessing as mp
import torch.multiprocessing as tmp
from timeit import default_timer as timer
from torchvision.utils import save_image
from torchvision import datasets, transforms
from torchviz import make_dot
import os
from memory_profiler import profile
# import logging as log
class Log:
def info(self, msg):
print(msg)
def warning(self, msg):
print(msg)
log = Log()
def white_rgb():
return torch.tensor([1., 1., 1.])
def black_rgb():
return torch.tensor([0., 0., 0.])
class Empty:
EMPTY_RGB = black_rgb
ALL_EMPTY = torch.zeros
# EMPTY_RGB = white_rgb
# ALL_EMPTY = torch.ones
def cube_training_positions():
return torch.tensor([[-4.7487, 44.7487, 20.0000, 1.0000],
[-3.9054, -3.9054, 29.0587, 1.0000],
[-1.4330, 41.4330, 2.5000, 1.0000],
[-1.4330, 41.4330, 37.5000, 1.0000],
[2.5000, 2.5000, -4.7487, 1.0000],
[7.6256, 32.3744, -10.3109, 1.0000],
[13.5946, 13.5946, 53.8074, 1.0000],
[20.0000, 20.0000, -15.0000, 1.0000],
[20.0000, 20.0000, 55.0000, 1.0000],
[26.4054, 26.4054, -13.8074, 1.0000],
[32.3744, 7.6256, -10.3109, 1.0000],
[37.5000, 37.5000, 44.7487, 1.0000],
[43.9054, 43.9054, 10.9413, 1.0000],
[44.7487, -4.7487, 20.0000, 1.0000]])
def table_training_positions():
return torch.tensor([[70.0000, 20.0000, 43.0000, 1.0000],
[69.8043, 24.4190, 43.0000, 1.0000],
[69.2189, 28.8034, 43.0000, 1.0000],
[68.2482, 33.1189, 43.0000, 1.0000],
[66.9000, 37.3318, 43.0000, 1.0000],
[65.1847, 41.4090, 43.0000, 1.0000],
[63.1157, 45.3186, 43.0000, 1.0000],
[60.7093, 49.0301, 43.0000, 1.0000],
[57.9844, 52.5144, 43.0000, 1.0000],
[54.9621, 55.7443, 43.0000, 1.0000],
[51.6662, 58.6943, 43.0000, 1.0000],
[48.1225, 61.3416, 43.0000, 1.0000],
[44.3586, 63.6653, 43.0000, 1.0000],
[40.4042, 65.6472, 43.0000, 1.0000],
[36.2900, 67.2719, 43.0000, 1.0000],
[32.0484, 68.5267, 43.0000, 1.0000],
[27.7124, 69.4016, 43.0000, 1.0000],
[23.3161, 69.8899, 43.0000, 1.0000],
[18.8939, 69.9878, 43.0000, 1.0000],
[14.4803, 69.6944, 43.0000, 1.0000],
[10.1099, 69.0121, 43.0000, 1.0000],
[5.8169, 67.9462, 43.0000, 1.0000],
[1.6349, 66.5051, 43.0000, 1.0000],
[-2.4033, 64.7000, 43.0000, 1.0000],
[-6.2663, 62.5451, 43.0000, 1.0000],
[-9.9236, 60.0572, 43.0000, 1.0000],
[-13.3468, 57.2558, 43.0000, 1.0000],
[-16.5090, 54.1628, 43.0000, 1.0000],
[-19.3854, 50.8024, 43.0000, 1.0000],
[-21.9536, 47.2010, 43.0000, 1.0000],
[-24.1935, 43.3867, 43.0000, 1.0000],
[-26.0874, 39.3894, 43.0000, 1.0000],
[-27.6207, 35.2403, 43.0000, 1.0000],
[-28.7813, 30.9719, 43.0000, 1.0000],
[-29.5601, 26.6177, 43.0000, 1.0000],
[-29.9511, 22.2117, 43.0000, 1.0000],
[-29.9511, 17.7883, 43.0000, 1.0000],
[-29.5601, 13.3823, 43.0000, 1.0000],
[-28.7813, 9.0281, 43.0000, 1.0000],
[-27.6207, 4.7597, 43.0000, 1.0000],
[-26.0874, 0.6106, 43.0000, 1.0000],
[-24.1935, -3.3867, 43.0000, 1.0000],
[-21.9536, -7.2010, 43.0000, 1.0000],
[-19.3854, -10.8024, 43.0000, 1.0000],
[-16.5090, -14.1628, 43.0000, 1.0000],
[-13.3468, -17.2558, 43.0000, 1.0000],
[-9.9236, -20.0572, 43.0000, 1.0000],
[-6.2663, -22.5451, 43.0000, 1.0000],
[-2.4033, -24.7000, 43.0000, 1.0000],
[1.6349, -26.5051, 43.0000, 1.0000],
[5.8169, -27.9462, 43.0000, 1.0000],
[10.1099, -29.0121, 43.0000, 1.0000],
[14.4803, -29.6944, 43.0000, 1.0000],
[18.8939, -29.9878, 43.0000, 1.0000],
[23.3161, -29.8899, 43.0000, 1.0000],
[27.7124, -29.4016, 43.0000, 1.0000],
[32.0484, -28.5267, 43.0000, 1.0000],
[36.2900, -27.2719, 43.0000, 1.0000],
[40.4042, -25.6472, 43.0000, 1.0000],
[44.3586, -23.6653, 43.0000, 1.0000],
[48.1225, -21.3416, 43.0000, 1.0000],
[51.6662, -18.6943, 43.0000, 1.0000],
[54.9621, -15.7443, 43.0000, 1.0000],
[57.9844, -12.5144, 43.0000, 1.0000],
[60.7093, -9.0301, 43.0000, 1.0000],
[63.1157, -5.3186, 43.0000, 1.0000],
[65.1847, -1.4090, 43.0000, 1.0000],
[66.9000, 2.6682, 43.0000, 1.0000],
[68.2482, 6.8811, 43.0000, 1.0000],
[69.2189, 11.1966, 43.0000, 1.0000],
[69.8043, 15.5810, 43.0000, 1.0000],
[70.0000, 20.0000, 43.0000, 1.0000]])
log.info(f"Using backend {plt.get_backend()}")
GRID_X = 40
GRID_Y = 40
GRID_Z = 40
INHOMOGENEOUS_ZERO_VECTOR = torch.tensor([0., 0., 0.])
REGULARISATION_FRACTION = 0.01
TV_REGULARISATION_LAMBDA = 0.001
CAUCHY_REGULARISATION_LAMBDA = 0.00001
LEARNING_RATE = 0.001
NUM_STOCHASTIC_RAYS = 1500
ARBITRARY_SCALE = 1
MASTER_RAY_SAMPLE_POSITIONS_STRUCTURE = []
MASTER_VOXELS_STRUCTURE = []
VOXELS_NOT_USED = 0
OUTPUT_FOLDER = "./output"
class Camera:
def __init__(self, focal_length, center, look_at):
self.center = center
self.look_at = look_at
self.basis = basis_from_depth(look_at, center)
self.focal_length = focal_length
camera_center = center.detach().clone()
transposed_basis = torch.transpose(self.basis, 0, 1)
camera_center[:3] = camera_center[
:3] * -1 # We don't want to multiply the homogenous coordinate component; it needs to remain 1
camera_origin_translation = torch.eye(4, 4)
camera_origin_translation[:, 3] = camera_center
extrinsic_camera_parameters = torch.matmul(torch.inverse(transposed_basis), camera_origin_translation)
intrinsic_camera_parameters = torch.tensor([[focal_length, 0., 0., 0.],
[0., focal_length, 0., 0.],
[0., 0., 1., 0.]])
self.transform = torch.matmul(intrinsic_camera_parameters, extrinsic_camera_parameters)
def to_2D(self, point):
rendered_point = torch.matmul(self.transform, torch.transpose(point, 0, 1))
point_z = rendered_point[2, 0]
return rendered_point / point_z
def viewing_angle(self):
camera_basis_z = self.basis[2][:3]
camera_basis_theta = math.atan(camera_basis_z[1] / camera_basis_z[0]) if (
camera_basis_z[0] != 0) else math.pi / 2
camera_basis_phi = math.atan((camera_basis_z[0] ** 2 + camera_basis_z[1] ** 2) / camera_basis_z[2]) if (
camera_basis_z[2] != 0) else math.pi / 2
return torch.tensor([camera_basis_theta, camera_basis_phi])
def camera_basis_from(camera_depth_z_vector):
depth_vector = camera_depth_z_vector[:3] # We just want the inhomogenous parts of the coordinates
# This calculates the projection of the world z-axis onto the surface defined by the camera direction,
# since we want to derive the coordinate system of the camera to be orthogonal without having
# to calculate it manually.
cartesian_z_vector = torch.tensor([0., 0., 1.])
cartesian_z_projection_lambda = torch.dot(depth_vector, cartesian_z_vector) / torch.dot(
depth_vector, depth_vector)
camera_up_vector = cartesian_z_vector - cartesian_z_projection_lambda * depth_vector
# This special case is for when the camera is directly pointing up or down, then
# there is no way to decide which way to orient its up vector in the X-Y plane.
# We choose to align the up veector with the X-axis in this case.
if (torch.equal(camera_up_vector, INHOMOGENEOUS_ZERO_VECTOR)):
camera_up_vector = torch.tensor([1., 0., 0.])
log.info(f"Up vector is: {camera_up_vector}")
# The camera coordinate system now has the direction of camera and the up direction of the camera.
# We need to find the third vector which needs to be orthogonal to both the previous vectors.
# Taking the cross product of these vectors gives us this third component
camera_x_vector = torch.linalg.cross(depth_vector, camera_up_vector)
inhomogeneous_basis = torch.stack([camera_x_vector, camera_up_vector, depth_vector, torch.tensor([0., 0., 0.])])
homogeneous_basis = torch.hstack((inhomogeneous_basis, torch.tensor([[0.], [0.], [0.], [1.]])))
homogeneous_basis[0] = unit_vector(homogeneous_basis[0])
homogeneous_basis[1] = unit_vector(homogeneous_basis[1])
homogeneous_basis[2] = unit_vector(homogeneous_basis[2])
return homogeneous_basis
def basis_from_depth(look_at, camera_center):
log.info(f"Looking at: {look_at}")
log.info(f"Looking from: {camera_center}")
depth_vector = torch.sub(look_at, camera_center)
depth_vector[3] = 1.
return camera_basis_from(depth_vector)
def unit_vector(camera_basis_vector):
return camera_basis_vector / math.sqrt(
pow(camera_basis_vector[0], 2) +
pow(camera_basis_vector[1], 2) +
pow(camera_basis_vector[2], 2))
def generate_camera_angles(radius, look_at):
camera_positions = []
for phi in np.linspace(0, math.pi, 4):
for theta in np.linspace(0, 2 * math.pi, 5):
phi += math.pi / 4
theta += math.pi / 4
x = radius * math.sin(phi) * math.cos(theta)
y = radius * math.sin(phi) * math.sin(theta)
z = radius * math.cos(phi)
x = 0 if abs(x) < 0.0001 else x
y = 0 if abs(y) < 0.0001 else y
z = 0 if abs(z) < 0.0001 else z
camera_positions.append(torch.tensor([x, y, z, 0]))
positions = (torch.stack(camera_positions).unique(dim=0)) + look_at
log.info(positions)
return positions
HALF_SQRT_3_BY_PI = 0.5 * math.sqrt(3. / math.pi)
HALF_SQRT_15_BY_PI = 0.5 * math.sqrt(15. / math.pi)
QUARTER_SQRT_15_BY_PI = 0.25 * math.sqrt(15. / math.pi)
QUARTER_SQRT_5_BY_PI = 0.25 * math.sqrt(5. / math.pi)
Y_0_0 = 0.5 * math.sqrt(1. / math.pi)
Y_m1_1 = lambda theta, phi: HALF_SQRT_3_BY_PI * math.sin(theta) * math.sin(phi)
Y_0_1 = lambda theta, phi: HALF_SQRT_3_BY_PI * math.cos(theta)
Y_1_1 = lambda theta, phi: HALF_SQRT_3_BY_PI * math.sin(theta) * math.cos(phi)
Y_m2_2 = lambda theta, phi: HALF_SQRT_15_BY_PI * math.sin(theta) * math.cos(phi) * math.sin(
theta) * math.sin(phi)
Y_m1_2 = lambda theta, phi: HALF_SQRT_15_BY_PI * math.sin(theta) * math.sin(phi) * math.cos(theta)
Y_0_2 = lambda theta, phi: QUARTER_SQRT_5_BY_PI * (3 * math.cos(theta) * math.cos(theta) - 1)
Y_1_2 = lambda theta, phi: HALF_SQRT_15_BY_PI * math.sin(theta) * math.cos(phi) * math.cos(theta)
Y_2_2 = lambda theta, phi: QUARTER_SQRT_15_BY_PI * (
pow(math.sin(theta) * math.cos(phi), 2) - pow(math.sin(theta) * math.sin(phi), 2))
def harmonic(C_0_0, C_m1_1, C_0_1, C_1_1, C_m2_2, C_m1_2, C_0_2, C_1_2, C_2_2):
return lambda theta, phi: C_0_0 * Y_0_0 + C_m1_1 * Y_m1_1(theta, phi) + C_0_1 * Y_0_1(theta, phi) + C_1_1 * Y_1_1(
theta, phi) + C_m2_2 * Y_m2_2(theta, phi) + C_m1_2 * Y_m1_2(theta, phi) + C_0_2 * Y_0_2(theta,
phi) + C_1_2 * Y_1_2(
theta, phi) + C_2_2 * Y_2_2(theta, phi)
def rgb_harmonics(rgb_harmonic_coefficients):
red_harmonic = harmonic(*rgb_harmonic_coefficients[:9])
green_harmonic = harmonic(*rgb_harmonic_coefficients[9:18])
blue_harmonic = harmonic(*rgb_harmonic_coefficients[18:])
return (red_harmonic, green_harmonic, blue_harmonic)
class Voxel:
NUM_INTERPOLATING_VOXEL_NEIGHBOURS = 8
DEFAULT_OPACITY = 0.05
VOXEL_PRUNING_OPACITY_THRESHOLD = 0.2
NUM_VOXEL_NEIGHBOURS = 9 * 3 - 1
VOXEL_PRUNING_NEIGHBOUR_OPACITY_THRESHOLDS = torch.full([NUM_VOXEL_NEIGHBOURS], VOXEL_PRUNING_OPACITY_THRESHOLD)
@staticmethod
def default_voxel(requires_grad=True):
return lambda: torch.tensor(Voxel.uniform_harmonic(), requires_grad=requires_grad)
@staticmethod
def random_coloured_voxel(requires_grad=True):
return lambda: torch.tensor(
np.concatenate(([0.7], (np.random.rand(VoxelGrid.VOXEL_DIMENSION - 1) - 0.5))),
requires_grad=requires_grad)
@staticmethod
def uniform_harmonic(density=1.):
return [density] + ([0.5] + [0.] * (VoxelGrid.PER_CHANNEL_DIMENSION - 1)) * 3
@staticmethod
def random_harmonic_coefficient_set():
return ([random.random()] + [0.] * (VoxelGrid.PER_CHANNEL_DIMENSION - 1))
@staticmethod
def uniform_harmonic_random_colour(density=0.04, requires_grad=True):
return lambda: torch.tensor([
density] + Voxel.random_harmonic_coefficient_set() + Voxel.random_harmonic_coefficient_set() + Voxel.random_harmonic_coefficient_set(),
requires_grad=requires_grad)
@staticmethod
def occupied_voxel(density=1., requires_grad=True):
return lambda: torch.tensor(Voxel.uniform_harmonic(density), requires_grad=requires_grad)
@staticmethod
def empty_voxel(requires_grad=True):
return lambda: torch.zeros([VoxelGrid.VOXEL_DIMENSION], requires_grad=requires_grad)
@staticmethod
def like_voxel(prototype_voxel):
return lambda: prototype_voxel.clone()
@staticmethod
def prune(voxel_tensor):
voxel_tensor.requires_grad = False
voxel_tensor.fill_(0.)
voxel_tensor.pruned = True
@staticmethod
def is_pruned(voxel_tensor):
return voxel_tensor.pruned if hasattr(voxel_tensor, "pruned") else False
class Ray:
def __init__(self, num_samples, view_point, ray_sample_positions, voxel_positions, voxels):
self.num_samples = num_samples
self.ray_sample_positions = ray_sample_positions
self.view_point = view_point
self.voxels = voxels
self.voxel_positions = voxel_positions
if num_samples != len(ray_sample_positions):
log.warning(f"WARNING: num_samples = {num_samples}, sample_positions = {ray_sample_positions}")
def at(self, index):
start = index * Voxel.NUM_INTERPOLATING_VOXEL_NEIGHBOURS
end = start + Voxel.NUM_INTERPOLATING_VOXEL_NEIGHBOURS
return self.ray_sample_positions[index], \
self.voxel_positions[start: end], \
self.voxels[start: end]
class VoxelAccess:
def __init__(self, view_points, ray_sample_positions, voxel_pointers, all_voxels, all_voxel_positions):
self.ray_sample_positions = ray_sample_positions
self.view_points = view_points
self.voxel_positions = all_voxel_positions
self.all_voxels = all_voxels
self.voxel_pointers = voxel_pointers
def for_ray(self, ray_index):
ptr = self.voxel_pointers[ray_index]
start, end, num_samples = ptr
return Ray(num_samples, self.view_points[ray_index],
self.ray_sample_positions[int(start / Voxel.NUM_INTERPOLATING_VOXEL_NEIGHBOURS): int(
end / Voxel.NUM_INTERPOLATING_VOXEL_NEIGHBOURS)],
self.voxel_positions[start:end],
self.all_voxels[start:end])
def cube_faces(cube_spec):
x, y, z, dx, dy, dz = cube_spec
face1 = (x, y, z, dx + 1, dy + 1, 1)
face2 = (x, y, z + dz, dx + 1, dy + 1, 1)
face3 = (x, y, z, 1, dy + 1, dz + 1)
face4 = (x + dx, y, z, 1, dy + 1, dz + 1)
face5 = (x, y, z, dx + 1, 1, dz + 1)
face6 = (x, y + dy, z, dx + 1, 1, dz + 1)
return face1, face2, face3, face4, face5, face6
class VoxelGrid:
VOXEL_DIMENSION = 28
PER_CHANNEL_DIMENSION = 9
DEFAULT_SCALE = torch.tensor([1., 1., 1.])
def __init__(self, world_tensor, scale=DEFAULT_SCALE):
self.scale = scale
self.grid_x, self.grid_y, self.grid_z = torch.tensor(world_tensor.shape) * self.scale
self.voxel_grid_x, self.voxel_grid_y, self.voxel_grid_z = world_tensor.shape
self.voxel_grid = world_tensor
def voxel_dimensions(self):
return torch.tensor([self.voxel_grid_x, self.voxel_grid_y, self.voxel_grid_z]).int()
def world_x(self):
return self.grid_x
def world_y(self):
return self.grid_y
def world_z(self):
return self.grid_z
@staticmethod
def build_empty_world(x, y, z, scale=DEFAULT_SCALE):
return VoxelGrid.new(x, y, z, Voxel.empty_voxel(), scale)
@staticmethod
def build_random_world(x, y, z, scale=DEFAULT_SCALE):
return VoxelGrid.new(x, y, z, Voxel.random_coloured_voxel(), scale)
@staticmethod
def build_with_voxel(x, y, z, prototype_voxel, scale=DEFAULT_SCALE):
return VoxelGrid.new(x, y, z, Voxel.like_voxel(prototype_voxel), scale)
@staticmethod
def copy_from(world, scale=DEFAULT_SCALE):
return VoxelGrid(world.voxel_grid.copy(), scale=world.scale)
@classmethod
def from_tensor(cls, world_tensor, scale=DEFAULT_SCALE):
return cls(world_tensor, scale)
@classmethod
def as_parameter(cls, world, model):
world_tensor = world.voxel_grid
voxel_x, voxel_y, voxel_z = world.voxel_dimensions()
new_world = VoxelGrid.build_empty_world(voxel_x, voxel_y, voxel_z, scale=world.scale)
for i, j, k, v in world.all_voxels():
parameter = nn.Parameter(v)
if Voxel.is_pruned(v):
Voxel.prune(parameter)
new_world.set((i, j, k), parameter)
model.register_parameter(f"{(i, j, k)}", parameter)
return new_world
@classmethod
def new(cls, voxel_x, voxel_y, voxel_z, make_voxel, scale=DEFAULT_SCALE):
log.info(f"Initialising world with dimensions ({voxel_x, voxel_y, voxel_z})")
voxel_grid = np.ndarray((voxel_x, voxel_y, voxel_z), dtype=list)
for i in range(voxel_x):
for j in range(voxel_y):
for k in range(voxel_z):
voxel_grid[i, j, k] = make_voxel()
return cls(voxel_grid, scale)
def at(self, world_x, world_y, world_z):
if self.is_outside(world_x, world_y, world_z):
return Voxel.empty_voxel(requires_grad=False)()
else:
voxel_x, voxel_y, voxel_z = self.to_voxel_coordinates(torch.tensor([world_x, world_y, world_z]))
return self.voxel_grid[voxel_x, voxel_y, voxel_z]
# TODO: Remember to test scale_up() again
def scale_up(self):
new_dimensions = self.voxel_dimensions() * 2
new_scale = self.scale / 2
x_scale, y_scale, z_scale = new_scale
log.info(f"New scaled up dimensions={new_dimensions}")
scaled_up_world = VoxelGrid.build_empty_world(new_dimensions[0], new_dimensions[1], new_dimensions[2],
scale=new_scale)
for i, j, k, original_voxel in self.voxels_in_world(
torch.tensor([0, 0, 0, self.grid_x, self.grid_y, self.grid_z])):
x2 = (i * 2 + 1 / x_scale).int()
y2 = (j * 2 + 1 / y_scale).int()
z2 = (k * 2 + 1 / z_scale).int()
scaled_up_world.voxel_grid[i * 2: x2, j * 2: y2, k * 2: z2].fill(original_voxel.detach().clone())
if hasattr(original_voxel, "pruned"):
for x, y, z, v in scaled_up_world.voxel_by_position(
torch.tensor([i * 2, j * 2, k * 2, 1 / x_scale, 1 / y_scale, 1 / z_scale])):
Voxel.prune(v)
return scaled_up_world
def to_voxel_coordinates(self, world_coordinates):
return torch.divide(world_coordinates, self.scale).int()
def set(self, voxel_position, voxel):
voxel_x, voxel_y, voxel_z = voxel_position
if self.is_outside_grid(voxel_x, voxel_y, voxel_z):
log.warning(f"[WARNING]: set() attempted to set a value at {(voxel_position)} outside grid")
return
else:
self.voxel_grid[voxel_x, voxel_y, voxel_z] = voxel
def is_inside_grid(self, voxel_x, voxel_y, voxel_z):
return (0 <= voxel_x < self.voxel_grid_x and
0 <= voxel_y < self.voxel_grid_y and
0 <= voxel_z < self.voxel_grid_z)
def is_outside_grid(self, voxel_x, voxel_y, voxel_z):
return not self.is_inside_grid(voxel_x, voxel_y, voxel_z)
def is_inside(self, world_x, world_y, world_z):
return (0 <= world_x < self.grid_x and
0 <= world_y < self.grid_y and
0 <= world_z < self.grid_z)
def is_outside(self, world_x, world_y, world_z):
return not self.is_inside(world_x, world_y, world_z)
def neighbour_opacities(self, voxel_x, voxel_y, voxel_z):
opacities = []
for i in range(voxel_x - 1, voxel_x + 2):
for j in range(voxel_y - 1, voxel_y + 2):
for k in range(voxel_z - 1, voxel_z + 2):
if voxel_x == i and voxel_y == j and voxel_z == k:
continue
opacities.append(self.voxel_by_position(i, j, k)[0])
# print(f"Opacities are {torch.stack(opacities)}")
return torch.stack(opacities)
def prune(self, voxel_position):
voxel = self.voxel_by_position(*voxel_position)
if (voxel[0] > Voxel.VOXEL_PRUNING_OPACITY_THRESHOLD):
return False
surrounding_opacities = self.neighbour_opacities(*voxel_position)
log.info(f"Scanning neighbours...{surrounding_opacities}")
if (surrounding_opacities.less_equal(
Voxel.VOXEL_PRUNING_NEIGHBOUR_OPACITY_THRESHOLDS).all()):
voxel.requires_grad = False
voxel.mul_(0)
Voxel.prune(voxel)
return True
def channel_opacity(self, distance_density_color_tensors, viewing_angle):
number_of_samples = len(distance_density_color_tensors)
density_distance_products = distance_density_color_tensors[:, 0] * distance_density_color_tensors[:, 1]
summing_matrix = torch.tensor(list(
functools.reduce(lambda acc, n: acc + [[1.] * n + [0.] * (number_of_samples - n)],
range(1, number_of_samples + 1),
[]))).t()
# print(f"Sigma-D={density_distance_products.type()}, summing matrix = {summing_matrix.t().type()}")
transmittances = torch.matmul(density_distance_products.double(), summing_matrix.double())
transmittances = torch.exp(-transmittances)
red_channel, green_channel, blue_channel = [], [], []
for index, distance_density_color_tensor in enumerate(distance_density_color_tensors):
red_harmonic, green_harmonic, blue_harmonic = rgb_harmonics(
distance_density_color_tensor[2:])
r = red_harmonic(viewing_angle[0], viewing_angle[1])
g = green_harmonic(viewing_angle[0], viewing_angle[1])
b = blue_harmonic(viewing_angle[0], viewing_angle[1])
red_channel.append(r)
green_channel.append(g)
blue_channel.append(b)
red_channel, green_channel, blue_channel = torch.stack(red_channel), torch.stack(green_channel), torch.stack(
blue_channel)
base_transmittance_factors = transmittances * (1 - torch.exp(- density_distance_products))
red = (base_transmittance_factors * red_channel).sum()
green = (base_transmittance_factors * green_channel).sum()
blue = (base_transmittance_factors * blue_channel).sum()
color_densities = torch.stack([red, green, blue])
return color_densities
def to_voxel_cube_spec(self, world_cube_spec):
x1, y1, z1, dx, dy, dz = world_cube_spec
x2, y2, z2 = x1 + dx, y1 + dy, z1 + dz
return self.to_voxel_coordinates(torch.stack([x1, y1, z1])), self.to_voxel_coordinates(
torch.stack([x2, y2, z2]))
def voxels_in_world(self, world_cube_spec):
return self.__voxels(self.to_voxel_cube_spec(world_cube_spec))
def voxels_by_position(self, voxel_cube_spec):
x1, y1, z1, dx, dy, dz = voxel_cube_spec
x2, y2, z2 = x1 + dx, y1 + dy, z1 + dz
return self.__voxels([[x1, y1, z2], [x2, y2, z2]])
def __voxels(self, from_to_voxel_positions):
from_voxel, to_voxel = from_to_voxel_positions
voxel_x1, voxel_y1, voxel_z1 = from_voxel
voxel_x2, voxel_y2, voxel_z2 = to_voxel
for i in torch.arange(voxel_x1, voxel_x2):
for j in torch.arange(voxel_y1, voxel_y2):
for k in torch.arange(voxel_z1, voxel_z2):
yield int(i), int(j), int(k), self.voxel_by_position(int(i), int(j), int(k))
def all_voxels(self):
return self.__voxels(torch.tensor([[0., 0., 0.], [self.voxel_grid_x, self.voxel_grid_y, self.voxel_grid_z]]))
def build_solid_cube(self, cube_spec):
for i, j, k, _ in self.voxels_in_world(cube_spec):
self.voxel_grid[i, j, k] = Voxel.occupied_voxel(0.2)()
def build_monochrome_hollow_cube(self, cube_spec):
self.build_hollow_cube_with_randomly_coloured_sides(Voxel.default_voxel(), cube_spec)
def build_hollow_cube_with_randomly_coloured_sides(self, make_voxel, cube_spec):
voxel_1, voxel_2, voxel_3, voxel_4, voxel_5, voxel_6 = make_voxel(), make_voxel(), make_voxel(), make_voxel(), make_voxel(), make_voxel()
face1, face2, face3, face4, face5, face6 = cube_faces(cube_spec)
for i, j, k, _ in self.voxels_in_world(face1):
self.voxel_grid[i, j, k] = voxel_1
for i, j, k, _ in self.voxels_in_world(face2):
self.voxel_grid[i, j, k] = voxel_2
for i, j, k, _ in self.voxels_in_world(face3):
self.voxel_grid[i, j, k] = voxel_3
for i, j, k, _ in self.voxels_in_world(face4):
self.voxel_grid[i, j, k] = voxel_4
for i, j, k, _ in self.voxels_in_world(face5):
self.voxel_grid[i, j, k] = voxel_5
for i, j, k, _ in self.voxels_in_world(face6):
self.voxel_grid[i, j, k] = voxel_6
def density(self, ray_samples_with_positions_distances, viewing_angle):
global MASTER_VOXELS_STRUCTURE
collected_intensities = []
for ray_sample in ray_samples_with_positions_distances:
ray_sample_world_position = ray_sample[:3]
collected_intensities.append(
self.intensities(ray_sample_world_position, self.interpolating_neighbours(ray_sample_world_position)))
return self.channel_opacity(
torch.cat([ray_samples_with_positions_distances[:, 3:], torch.stack(collected_intensities)], 1),
viewing_angle)
def density_split(self, ray_sample_distances, ray, viewing_angle):
collected_intensities = []
for index, distance in enumerate(ray_sample_distances):
ray_sample_world_position, voxel_positions, voxels = ray.at(index)
if len(voxels) == 0:
return Empty.EMPTY_RGB()
collected_intensities.append(self.intensities(ray_sample_world_position, voxels))
return self.channel_opacity(torch.cat([ray_sample_distances, torch.stack(collected_intensities)], 1),
viewing_angle)
def interpolating_neighbours(self, ray_sample_world_position):
x_0, x_1, y_0, y_1, z_0, z_1, _1, _2, _3 = self.interpolating_neighbour_endpoints(ray_sample_world_position)
c_000 = self.voxel_by_position(x_0, y_0, z_0)
c_001 = self.voxel_by_position(x_0, y_0, z_1)
c_010 = self.voxel_by_position(x_0, y_1, z_0)
c_011 = self.voxel_by_position(x_0, y_1, z_1)
c_100 = self.voxel_by_position(x_1, y_0, z_0)
c_101 = self.voxel_by_position(x_1, y_0, z_1)
c_110 = self.voxel_by_position(x_1, y_1, z_0)
c_111 = self.voxel_by_position(x_1, y_1, z_1)
return (c_000, c_001, c_010, c_011, c_100, c_101, c_110, c_111)
def interpolating_neighbour_endpoints(self, ray_sample_world_coords):
# print(f"Scale is {self.scale}")
x, y, z = ray_sample_world_coords
voxel_x, voxel_y, voxel_z = self.to_voxel_coordinates(ray_sample_world_coords)
x_0, x_1 = voxel_x, voxel_x + 1
y_0, y_1 = voxel_y, voxel_y + 1
z_0, z_1 = voxel_z, voxel_z + 1
x_d = (x / self.scale[0] - x_0) / (x_1 - x_0)
y_d = (y / self.scale[1] - y_0) / (y_1 - y_0)
z_d = (z / self.scale[2] - z_0) / (z_1 - z_0)
return x_0, x_1, y_0, y_1, z_0, z_1, x_d, y_d, z_d
def intensities(self, ray_sample_world_position, interpolating_neighbours):
global MASTER_VOXELS_STRUCTURE
_, _, _, _, _, _, x_d, y_d, z_d = self.interpolating_neighbour_endpoints(ray_sample_world_position)
c_000, c_001, c_010, c_011, c_100, c_101, c_110, c_111 = interpolating_neighbours
c_00 = c_000 * (1 - x_d) + c_100 * x_d
c_01 = c_001 * (1 - x_d) + c_101 * x_d
c_10 = c_010 * (1 - x_d) + c_110 * x_d
c_11 = c_011 * (1 - x_d) + c_111 * x_d
c_0 = c_00 * (1 - y_d) + c_10 * y_d
c_1 = c_01 * (1 - y_d) + c_11 * y_d
c = c_0 * (1 - z_d) + c_1 * z_d
MASTER_VOXELS_STRUCTURE += [c_000, c_001, c_010, c_011, c_100, c_101, c_110, c_111]
if (c[0].abs() > 900):
log.warning(f"WARNING: Bad neighbouring tensor at {(ray_sample_world_position)}")
log.warning(f"WARNING: {(x_d, y_d, z_d)}")
log.warning(interpolating_neighbours)
return c
def neighbours(self, world_x, world_y, world_z):
x_0, x_1, y_0, y_1, z_0, z_1, _1, _2, _3 = self.interpolating_neighbour_endpoints(
torch.tensor([world_x, world_y, world_z]))
c_000 = self.voxel_by_position(x_0, y_0, z_0)
c_001 = self.voxel_by_position(x_0, y_0, z_1)
c_010 = self.voxel_by_position(x_0, y_1, z_0)
c_011 = self.voxel_by_position(x_0, y_1, z_1)
c_100 = self.voxel_by_position(x_1, y_0, z_0)
c_101 = self.voxel_by_position(x_1, y_0, z_1)
c_110 = self.voxel_by_position(x_1, y_1, z_0)
c_111 = self.voxel_by_position(x_1, y_1, z_1)
return ([c_000, c_001, c_010, c_011, c_100, c_101, c_110, c_111], torch.tensor([[x_0, y_0, z_0],
[x_0, y_0, z_1],
[x_0, y_1, z_0],
[x_0, y_1, z_1],
[x_1, y_0, z_0],
[x_1, y_0, z_1],
[x_1, y_1, z_0],
[x_1, y_1, z_1]]))
def voxel_by_position(self, voxel_x, voxel_y, voxel_z):
if (voxel_x < 0 or voxel_x >= self.voxel_grid_x or
voxel_y < 0 or voxel_y >= self.voxel_grid_y or
voxel_z < 0 or voxel_z >= self.voxel_grid_z):
return torch.zeros(VoxelGrid.VOXEL_DIMENSION)
return self.voxel_grid[voxel_x, voxel_y, voxel_z]
class ClampingFunctions:
SIGMOID = nn.Sigmoid()
CLAMP = lambda t: torch.clamp(t, min=0, max=1)
DEFAULT = CLAMP
class Renderer:
def __init__(self, world, camera, view_spec, ray_spec):
self.world = world
self.camera = camera
self.ray_length = ray_spec[0]
self.num_ray_samples = ray_spec[1]
self.x_1, self.x_2 = view_spec[0], view_spec[1]
self.y_1, self.y_2 = view_spec[2], view_spec[3]
self.num_view_samples_x = view_spec[4]
self.num_view_samples_y = view_spec[5]
def render_from_ray(self, ray, viewing_angle, clamping_function):
# print(f"Wall clock in render_from_ray() is {timer()}")
ray_sample_positions = ray.ray_sample_positions
unique_ray_samples = ray_sample_positions
view_x, view_y = ray.view_point
if (len(unique_ray_samples) <= 1):
return torch.tensor([view_x, view_y, 0., 0., 0.])
t1 = unique_ray_samples[:-1]
t2 = unique_ray_samples[1:]
consecutive_sample_distances = (t1 - t2).pow(2).sum(1).sqrt()
# Make 1D tensor into 2D tensor
# List of tensors, each entry is distance from i-th sample to the next sample
ray_sample_distances = torch.reshape(consecutive_sample_distances, (-1, 1))
color_densities = self.world.density_split(ray_sample_distances, ray, viewing_angle)
color_tensor = clamping_function(color_densities * ARBITRARY_SCALE)
if (view_x < self.x_1 or view_x > self.x_2
or view_y < self.y_1 or view_y > self.y_2):
log.warning(f"[WARNING]: bad generation: {view_x}, {view_y}")
# print(color_tensor)
return torch.cat([torch.tensor([view_x, view_y]), color_tensor])
def render_from_rays(self, voxel_access, clamping_function=ClampingFunctions.DEFAULT):
X, Y = 0, 1
RED_CHANNEL, GREEN_CHANNEL, BLUE_CHANNEL = 2, 3, 4
camera = self.camera
# composite_colour_tensors = self.render_parallel(voxel_access, camera)
composite_colour_tensors = self.render_serial(voxel_access, camera, clamping_function)
red_channel = composite_colour_tensors[:, [X, Y, RED_CHANNEL]]
green_channel = composite_colour_tensors[:, [X, Y, GREEN_CHANNEL]]
blue_channel = composite_colour_tensors[:, [X, Y, BLUE_CHANNEL]]
log.info("Done volumetric calculations from rays!!")
return (red_channel, green_channel, blue_channel)
def render_serial(self, voxel_access, camera, clamping_function):
viewing_angle = camera.viewing_angle()
num_view_points = len(voxel_access.view_points)
composite_colour_tensors = torch.stack(list(
map(lambda index: self.render_from_ray(voxel_access.for_ray(index), viewing_angle, clamping_function),
range(num_view_points))))
return composite_colour_tensors
def render_from_angle(self, ray):
return self.render_from_ray(ray, self.camera.viewing_angle(), clamping_function=ClampingFunctions.DEFAULT)
def render_parallel(self, voxel_access, camera):
viewing_angle = camera.viewing_angle()
num_view_points = len(voxel_access.view_points)
workers = os.cpu_count()
p = tmp.Pool(workers)
start_copy_rays = timer()
rays = list(map(lambda i: voxel_access.for_ray(i), range(num_view_points)))
end_copy_rays = timer()
log.info(f"Copying rays took {end_copy_rays - start_copy_rays}")
log.info(f"Wall clock is {timer()}")
start_render_rays = timer()
# responses = p.map(lambda ray: self.render_from_ray(ray, viewing_angle), rays)
responses = p.map(self.render_from_angle, rays)
p.close()
p.join()
end_render_rays = timer()
log.info(f"Actual rendering took {end_render_rays - start_render_rays}")
composite_colour_tensors = torch.stack(list(responses))
return composite_colour_tensors
@staticmethod
def initialise_plt(plt):
plt.rcParams['axes.xmargin'] = 0
plt.rcParams['axes.ymargin'] = 0
figure = plt.figure(f"{random.random()}", frameon=False)
plt.rcParams['axes.facecolor'] = 'black'
plt.axis("equal")
plt.style.use("dark_background")
ax = plt.gca()
ax.set_aspect('equal', adjustable='box')
plt.axis("off")
return figure
def build_rays(self, ray_intersection_weights):
camera = self.camera
camera_basis_x = camera.basis[0][:3]
camera_basis_y = camera.basis[1][:3]
camera_basis_z = camera.basis[2][:3]
camera_center_inhomogenous = camera.center[:3]
all_voxel_positions = []
view_points = []
voxel_pointers = []
all_voxels = []
ray_sample_positions = []
view_screen_origin = camera_basis_z * camera.focal_length + camera_center_inhomogenous
counter = 0
for ray_intersection_weight in ray_intersection_weights:
ray_screen_intersection = camera_basis_x * ray_intersection_weight[0] + \
camera_basis_y * ray_intersection_weight[1] + view_screen_origin
unit_ray = unit_vector(ray_screen_intersection - camera_center_inhomogenous)
view_x, view_y = ray_intersection_weight[0], ray_intersection_weight[1]
num_intersecting_voxels = 0
all_voxels_per_ray = []
all_voxel_positions_per_ray = []
ray_sample_positions_per_ray = []
for k in np.linspace(0, self.ray_length, int(self.num_ray_samples)):
ray_endpoint = camera_center_inhomogenous + unit_ray * k
ray_x, ray_y, ray_z = ray_endpoint
if (self.world.is_outside(ray_x, ray_y, ray_z)):
continue
# We are in the box
interpolating_voxels, interpolating_voxel_positions = self.world.neighbours(ray_x, ray_y, ray_z)
num_intersecting_voxels += 1
all_voxels_per_ray += interpolating_voxels
all_voxel_positions_per_ray += interpolating_voxel_positions
ray_sample_positions_per_ray.append(torch.tensor([ray_x, ray_y, ray_z]))
if (num_intersecting_voxels <= 1):
continue
all_voxels += all_voxels_per_ray
all_voxel_positions += all_voxel_positions_per_ray
ray_sample_positions += ray_sample_positions_per_ray
view_points.append((view_x, view_y))
voxel_pointers.append(
(counter, counter + Voxel.NUM_INTERPOLATING_VOXEL_NEIGHBOURS * num_intersecting_voxels,
num_intersecting_voxels))
counter += Voxel.NUM_INTERPOLATING_VOXEL_NEIGHBOURS * num_intersecting_voxels
if (view_x < self.x_1 or view_x > self.x_2
or view_y < self.y_1 or view_y > self.y_2):
log.warning(f"[WARNING]: bad generation: {view_x}, {view_y}")
log.info("Done building candidate rays!!")
return VoxelAccess(view_points, torch.stack(ray_sample_positions), voxel_pointers, all_voxels,
all_voxel_positions)
def render(self, plt, clamping_function=ClampingFunctions.DEFAULT, text=None):
RED_CHANNEL, GREEN_CHANNEL, BLUE_CHANNEL = 0, 1, 2
global VOXELS_NOT_USED
global MASTER_RAY_SAMPLE_POSITIONS_STRUCTURE
red_image = []
green_image = []
blue_image = []
camera = self.camera
camera_basis_x = camera.basis[0][:3]
camera_basis_y = camera.basis[1][:3]
camera_basis_z = camera.basis[2][:3]
viewing_angle = camera.viewing_angle()
camera_center_inhomogenous = camera.center[:3]
Renderer.initialise_plt(plt)
log.info(f"Camera basis={camera.basis}")
view_screen_origin = camera_basis_z * camera.focal_length + camera_center_inhomogenous
log.info(f"View screen origin={view_screen_origin}")
for i in np.linspace(self.x_1, self.x_2, int(self.num_view_samples_x)):
red_column = []
green_column = []
blue_column = []
for j in np.linspace(self.y_1, self.y_2, int(self.num_view_samples_y)):
ray_screen_intersection = camera_basis_x * i + camera_basis_y * j + view_screen_origin
unit_ray = unit_vector(ray_screen_intersection - camera_center_inhomogenous)
# print(f"Camera basis is {camera.basis}, Camera center is {camera_center_inhomogenous}, intersection is {ray_screen_intersection}, Unit ray is [{unit_ray}]")
ray_samples = []
# To remove artifacts, set ray step samples to be higher, like 200
for k in np.linspace(0, self.ray_length, int(self.num_ray_samples)):
ray_endpoint = camera_center_inhomogenous + unit_ray * k
ray_x, ray_y, ray_z = ray_endpoint
if (self.world.is_outside(ray_x, ray_y, ray_z)):
# print(
# f"Skipping [{ray_x},{ray_y},{ray_z}], k={k}, unit ray={unit_ray}, camera is {camera_center_inhomogenous}")
continue
# We are in the box
ray_samples.append([ray_x, ray_y, ray_z])
# print(
# f"Sample at ({[ray_x, ray_y, ray_z]}), voxel value here is {self.world.at(ray_x, ray_y, ray_z)}")
# unique_ray_samples = torch.unique(torch.tensor(ray_samples), dim=0)
unique_ray_samples = torch.tensor(ray_samples)
if (len(unique_ray_samples) <= 1):
red_column.append(torch.tensor(0))
green_column.append(torch.tensor(0))
blue_column.append(torch.tensor(0))
plt.plot(i, j, marker="o", color=Empty.EMPTY_RGB().detach().numpy())
# print("Too few")
continue
MASTER_RAY_SAMPLE_POSITIONS_STRUCTURE += unique_ray_samples
VOXELS_NOT_USED += 8
t1 = unique_ray_samples[:-1]
t2 = unique_ray_samples[1:]
consecutive_sample_distances = (t1 - t2).pow(2).sum(1).sqrt()
# print(consecutive_sample_distances)
# Make 1D tensor into 2D tensor
ray_samples_with_distances = torch.cat([t1, torch.reshape(consecutive_sample_distances, (-1, 1))], 1)
# print(ray_samples_with_distances)
color_densities = self.world.density(ray_samples_with_distances, viewing_angle)
# color_tensor = torch.clamp(color_densities, min=0, max=1)
color_tensor = clamping_function(color_densities * ARBITRARY_SCALE)
# print(color_tensor)
plt.plot(i, j, marker="o", color=color_tensor.detach().numpy())
red_column.append(color_tensor[RED_CHANNEL])
green_column.append(color_tensor[GREEN_CHANNEL])
blue_column.append(color_tensor[BLUE_CHANNEL])
red_image.append(torch.tensor(red_column))
green_image.append(torch.tensor(green_column))
blue_image.append(torch.tensor(blue_column))
# Flip to prevent image being rendered upside down when saved to a file
red_image_tensor = torch.flip(torch.stack(red_image).t(), [0])
green_image_tensor = torch.flip(torch.stack(green_image).t(), [0])
blue_image_tensor = torch.flip(torch.stack(blue_image).t(), [0])
if (text is not None):
plt.text(0.5, 0.5, text, fontsize=14, backgroundcolor="white", alpha=0.5, color="black")
plt.show()
log.info("Done rendering in full!!")
return (red_image_tensor, green_image_tensor, blue_image_tensor)
def plot_from_image(self, image_data, plt, text=None):
Renderer.initialise_plt(plt)
red_render_channel, green_render_channel, blue_render_channel = image_data.detach().numpy()
width, height = red_render_channel.shape
for i in range(width):
for j in range(height):
plt.plot(i, height - 1 - j, marker="o",
color=[red_render_channel[j, i], green_render_channel[j, i], blue_render_channel[j, i]])
if (text is not None):
plt.text(0.5, 0.5, text, fontsize=14, backgroundcolor="white", alpha=0.5, color="black")
plt.show()
def stochastic_samples(num_stochastic_samples, view_spec):
x_1, x_2 = view_spec[0], view_spec[1]
y_1, y_2 = view_spec[2], view_spec[3]
view_length = x_2 - x_1
view_height = y_2 - y_1
# Need to convert the range [Random(0,1), Random(0,1)] into bounds of [[x1, x2], [y1, y2]]
ray_intersection_weights = list(
map(lambda x: torch.mul(torch.rand(2), torch.tensor([view_length, view_height])) + torch.tensor(
[x_1, y_1]), list(range(0, num_stochastic_samples))))
return ray_intersection_weights
def fullscreen_samples(view_spec):
x_1, x_2 = view_spec[0], view_spec[1]
y_1, y_2 = view_spec[2], view_spec[3]
num_view_samples_x = view_spec[4]
num_view_samples_y = view_spec[5]
ray_intersection_weights = []
for i in np.linspace(x_1, x_2, int(num_view_samples_x)):
for j in np.linspace(y_1, y_2, int(num_view_samples_y)):
ray_intersection_weights.append(torch.tensor([i, j]))
log.info(f"Number of weights={len(ray_intersection_weights)}")
return ray_intersection_weights
def camera_to_image(x, y, view_spec):
view_x1, view_x2, view_y1, view_y2, num_rays_x, num_rays_y = view_spec
step_x = (view_x2 - view_x1) / num_rays_x
step_y = (view_y2 - view_y1) / num_rays_y
# (view_y2 - y) implies we are flipping the Y-axis
image_x = int((x - view_x1) / step_x)
image_y = int((view_y2 - y) / step_y)
# In the above calculation, [-1,1] maps to [0, num_rays]. Only +1 maps to num_rays.
# We need to handle that isolated case and decrement by 1 to bring into the range
# of valid indices
image_x = image_x if image_x < num_rays_x else image_x - 1