-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths_SamsungGear360.m
226 lines (177 loc) · 6.34 KB
/
s_SamsungGear360.m
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
%% s_SamsungGear360
% Simulate (roughly) the Samsung Gear 360 rig. We don't know that much
% about it's internals, but we know it has two fisheye lens back to back
% that are spaced roughly 50 mm. The fisheye lenses have a FOV of around
% 180 degrees. The sensor size i 1/2.3-inch, and the resolution is roughly
% 2048x2048 (this is a bit unclear, but each image from each fish-eye lens
% should be square.)
%
% TL, Scien Stanford, 2017
%
%% Initialize
ieInit;
if ~piDockerExists, piDockerConfig; end
%% Set parameters
% Rendering parameters
gcloudFlag = 0;
sceneName = 'whiteRoom';
filmResolution = [128 128];
pixelSamples = 128;
bounces = 4;
% Save parameters
workingDir = fullfile(piRootPath,'local');
if(~exist(workingDir,'dir'))
mkdir(workingDir);
end
%% Setup
% Setup gcloud
if(gcloudFlag)
gCloud = gCloud('dockerImage','gcr.io/primal-surfer-140120/pbrt-v3-spectral-gcloud',...
'cloudBucket','gs://primal-surfer-140120.appspot.com');
gCloud.renderDepth = true;
gCloud.clusterName = 'trisha';
gCloud.maxInstances = 20;
gCloud.init();
end
% Setup save directory. We name the save directory as follows to help avoid
% overwriting previous renders (which an potentially be a very
% computationally costly mistake!)
saveDir = fullfile(workingDir, ...
sprintf('%s_%i_%i_%i_%i',...
sceneName,...
filmResolution(1),...
filmResolution(2),...
pixelSamples,...
bounces));
if(~exist(saveDir,'dir'))
mkdir(saveDir);
end
%% Select and load scene.
[pbrtFile,rigOrigin] = selectBitterliScene(sceneName);
%% Read the file
recipe = piRead(pbrtFile,'version',3);
%% Set camera locations
% These values were determined by looking at the output above. We also know
% the cameras are spaced roughly 45 mm apart.
% Move the origin slightly in the -Y direction so we get more objects in
% our image
rigOrigin = rigOrigin + [-1 0 0];
spacing = 45e-3;
forwardAll = [0 0 -1;
0 0 1];
originAll = [rigOrigin; rigOrigin] + spacing./2.*forwardAll;
upAll = [0 1 0;
0 1 0];
targetAll = originAll + forwardAll;% LookAt needs a "to" parameter
% Plot the rig
camI = [1; 2];
plotRig(originAll-[rigOrigin; rigOrigin],...
targetAll - [rigOrigin; rigOrigin],...
upAll,camI);
%% Change the camera parameters
recipe.set('camera','realistic');
% Focus at roughly meter away.
recipe.set('focusdistance',1.5);
% Both lenses are fisheye lens
lensFile = fullfile(piRootPath,'data','lens','fisheye.87deg.6.0mm.dat');
recipe.set('filmdiagonal',16); % Facebook (1")
% Attach the lens
recipe.set('lensfile',lensFile);
% Set the aperture to be the largest possible.
% PBRT-v3-spectral will automatically scale it down to the largest
% possible aperture for the chosen lens.
recipe.set('aperturediameter',10); % mm
%% Set render quality
recipe.set('filmresolution',filmResolution);
recipe.set('pixelsamples',pixelSamples);
recipe.set('maxdepth',bounces);
%% Loop through each camera in the rig and render.
% For gCloud
rigInfo = cell(size(originAll,1),5);
allRecipes = cell(size(originAll,1),1);
for ii = 1:size(originAll,1)
% Follow Facebook's naming conventions for the cameras
oiName = sprintf('cam%i',ii);
% Set camera lookAt
origin = originAll(ii,:);
target = targetAll(ii,:);
up = upAll(ii,:);
recipe.set('from',origin);
recipe.set('to',target);
recipe.set('up',up);
recipe.set('outputFile',fullfile(workingDir,strcat(oiName,'.pbrt')));
piWrite(recipe);
if(gcloudFlag)
allRecipes{ii} = copy(recipe);
% Save rig info in a large cell matrix. We will save these in the
% optical image after we download the rendered data from gCloud.
rigInfo{ii,1} = oiName;
rigInfo{ii,2} = origin;
rigInfo{ii,3} = target;
rigInfo{ii,4} = up;
rigInfo{ii,5} = rigOrigin;
else
% Otherwise render normally.
[oi, result] = piRender(recipe);
vcAddObject(oi);
oiWindow;
% Save the OI along with location information
oiFilename = fullfile(saveDir,oiName);
save(oiFilename,'oi','origin','target','up','rigOrigin');
clear oi
% Delete the .dat file if it exists (to avoid running out of local
% storage space. Since the oi has already been saved, the .dat file is
% redundant at this point.
[p,n,e] = fileparts(recipe.outputFile);
datFile = fullfile(p,'renderings',strcat(n,'.dat'));
if(exist(datFile,'file'))
delete(datFile);
end
datFileDepth = fullfile(p,'renderings',strcat(n,'_depth.dat'));
if(exist(datFileDepth,'file'))
delete(datFileDepth)
end
end
end
%% Render in gCloud (if applicable)
if(gcloudFlag)
% Upload all recipes to gCloud
% Note: We have to do the upload here because we want to wait until all
% pbrt files have been written out before we start uploading. This way
% all the data needed to render all pbrt files is ready in the working
% folder.(gCloud.upload only zips the working directory up once!)
for ii = 1:length(allRecipes)
gCloud.upload(allRecipes{ii});
end
gCloud.render();
% Save the gCloud object in case MATLAB closes
save(fullfile(workingDir,'gCloudBackup.mat'),'gCloud');
% Pause for user input (wait until gCloud job is done)
x = 'N';
while(~strcmp(x,'Y'))
x = input('Did the gCloud render finish yet? (Y/N)','s');
end
objects = gCloud.download();
for ii = 1:length(objects)
oi = objects{ii};
% "Fix" name. (OI name now includes date, but we want to use the
% "camX" name when saving)
oiName = oiGet(oi,'name');
C = strsplit(oiName,'-');
oiName = C{1};
oiFilename = fullfile(saveDir,strcat(oiName,'.mat'));
% Load up rig info
% Match "camX" name with the ones recorded in the rigInfo cell matrix.
for jj = 1:size(rigInfo,1)
if(strcmp(oiName,rigInfo{jj,1}))
origin = rigInfo{jj,2};
target = rigInfo{jj,3};
up = rigInfo{jj,4};
rigOrigin = rigInfo{jj,5};
break;
end
end
save(oiFilename,'oi','origin','target','up','rigOrigin');
fprintf('Saved oi at %s \n',oiFilename);
end
end