-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateTheeSixty.php
360 lines (291 loc) · 11.9 KB
/
generateTheeSixty.php
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
<?php
// Reading command arguments
$OPTIONS = getopt("f:i:o:s:", ["filename:","input:","output:","status:"]);
$FILENAMEARG = (array_key_exists("f", $OPTIONS) ? $OPTIONS['f'] : $OPTIONS['filename']) ;
$INPUTARG = (array_key_exists("i", $OPTIONS) ? $OPTIONS['i'] : $OPTIONS['input']);
$OUTPUTARG = (array_key_exists("o", $OPTIONS) ? $OPTIONS['o'] : $OPTIONS['output']);
$STATUSARG = (array_key_exists("s", $OPTIONS) ? $OPTIONS['s'] : $OPTIONS['status']);
echo($FILENAMEARG . PHP_EOL);
echo($INPUTARG . PHP_EOL);
echo($OUTPUTARG . PHP_EOL);
echo($STATUSARG . PHP_EOL);
// Creating folders
if(!is_dir($INPUTARG)) {
mkdir($INPUTARG);
}
if(!is_dir($OUTPUTARG)) {
mkdir($OUTPUTARG);
}
$filesInInput = array_slice(scandir($INPUTARG), 2);
$filesToProcess = [
[
"objectName" => $FILENAMEARG,
"objectPath" => $INPUTARG . '/' . $filesInInput[0]
]
];
$statusJson = [];
$fp = fopen($STATUSARG.'/status.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
$metadataJson = [];
// Processing each file
foreach ($filesToProcess as $file) {
$time_start = microtime(true);
// Conversion to stl if its a obj
if(file_exists($file["objectPath"])) {
$file_extension = pathinfo($file["objectPath"], PATHINFO_EXTENSION);
// Checking validity of file for stl2pov
if(file_exists($file["objectPath"]) && strtolower($file_extension) != "stl"){
echo(PHP_EOL."Converting file to stl".PHP_EOL);
$stlPath = str_replace($file_extension, 'stl', $file["objectPath"]);
exec("ctmconv ".$file["objectPath"]." ".$stlPath, $outputConvCTM);
// exec("assimp export ".$file["objectPath"]." ".$stlPath, $outputConv);
if(file_exists($stlPath)){
$file["objectPath"] = str_replace($file_extension, "stl", $file["objectPath"]);
} else {
echo("Error with CtmConv : ");
var_dump($outputConvCTM);
echo(PHP_EOL."Trying with assimp");
exec("assimp export ".$file["objectPath"]." ".$stlPath, $outputConvASS);
if(file_exists($stlPath)){
$file["objectPath"] = str_replace($file_extension, "stl", $file["objectPath"]);
} else {
echo("Error while converting : ");
var_dump($outputConvASS);
array_push($statusJson, [
"file conversion" => [
"status" => "error",
"message" => $outputConvASS
]
]);
$fp = fopen($STATUSARG.'/status.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
$fp = fopen('/app/files/results.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
return 0;
}
}
}
} else {
echo("Error file".$file["objectName"]." not found");
$fp = fopen('/app/files/results.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
return 0;
}
echo($file["objectPath"]);
// Stl simplification under threshold
$treshold = 5;
$filesize = filesize($file["objectPath"]);
if ($filesize <= 2) {
// File is too small to be a threedobject. Abort
$error = "File too small. Size: ". $filesize . " bytes";
echo(PHP_EOL . $error . PHP_EOL);
array_push($statusJson, [
"stl2pov conversion" => [
"status" => "error",
"message" => $error
]
]);
$fp = fopen($STATUSARG . '/status.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
$fp = fopen('/app/files/results.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
return 0;
}
echo(PHP_EOL."old size: ".$filesize.PHP_EOL);
if ($filesize > ($treshold * 1024 * 1024)) {
$path = "tmp/".$file["objectName"]."-simplified.stl";
$percentageDecrease = ($treshold * ((100 * 1024 * 1024)/$filesize))/100;
echo("Percentage to decrease: ".$percentageDecrease.PHP_EOL);
exec("/app/a.out ".$file["objectPath"]." ".$path." ".$percentageDecrease, $outputSimp);
if (file_exists($path)){
rename($path, $file["objectPath"]);
echo("new size: ".filesize($file["objectPath"]).PHP_EOL);
} else {
echo("Error while simplifying file: ");
var_dump($outputSimp);
array_push($statusJson, [
"file simplification" => [
"status" => "error",
"message" => $outputSimp
]
]);
$fp = fopen($STATUSARG.'/status.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
$fp = fopen('/app/files/results.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
return 0;
}
};
// Conversion to .pov with stl2pov
exec('/app/stl2pov '.$file["objectPath"].' > tmp/'.$file["objectName"].'_w.pov', $outputSTLPOV);
if(!file_exists('tmp/'.$file["objectName"].'_w.pov')) {
echo("Error reading the file data content");
// Writting the error on the status file
array_push($statusJson, [
"stl2pov conversion" => [
"status" => "error",
"message" => $outputSTLPOV
]
]);
$fp = fopen($STATUSARG.'/status.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
$fp = fopen('/app/files/results.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
return 0;
} else {
// Editing the status file
array_push($statusJson, [
"stl2pov conversion" => [
"status" => "done",
"progress" => "20%"
]
]);
$fp = fopen($STATUSARG.'/status.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
// Reading the pov file
$fileName = 'tmp/'.$file["objectName"].'_w.pov';
// Preparing the pov file for the render
// the name of the mesh to correspond to the template
$reading = fopen($fileName, 'r');
$writing = fopen($file["objectName"].'tmp', 'w');
$replaced = false;
// Replacing the name of the mesh to correspond to the template
while (!feof($reading)) {
$line = fgets($reading);
if (stristr($line,'mesh {')) {
$line = "#declare m_body= mesh {";
$replaced = true;
}
fputs($writing, $line);
}
fclose($reading); fclose($writing);
if ($replaced)
{
rename($file["objectName"].'tmp', $fileName);
} else {
unlink($file["objectName"].'tmp');
}
copy($fileName, 'tmp/'.$file["objectName"].'_h.pov');
// copy($fileName, $INPUTARG.'/'.$file["objectName"].'.pov');
$fileName2 = 'tmp/'.$file["objectName"].'_h.pov';
// Reading the templates files
$templateV = file_get_contents('template-v.pov', true);
$templateH = file_get_contents('template-h.pov', true);
// Adding the template to the pov files
file_put_contents($fileName, $templateH, FILE_APPEND);
file_put_contents($fileName2, $templateV, FILE_APPEND);
// Creating the tmp folder for the frames
if(!is_dir('tmp/'.$file["objectName"])) {
mkdir('tmp/'.$file["objectName"]);
}
// Generating the frames
$framesTotal = 15;
$Width = 720;
$Height = 720;
$outputTmpFrames = 'tmp/'.$file["objectName"].'/';
$command1 = 'povray "'.'tmp/'.$file["objectName"].'_w.pov'.'" +FN +W'.$Width.' +H'.$Height.' -O'.$outputTmpFrames.' +Q9 +AM1 +A +UA -D +KFI0 +KFF'.$framesTotal.'';
$command2 = 'povray "'.'tmp/'.$file["objectName"].'_h.pov'.'" +FN +W'.$Width.' +H'.$Height.' -O'.$outputTmpFrames.' +Q9 +AM1 +A +UA -D +KFI0 +KFF'.$framesTotal.'';
for ($x = 1; $x <= 2; $x++){
// Executing Povray
exec(($x == 1 ? $command1 : $command2), $outputPOV);
// Checking if the frames are generated.
// We will assume that if frame 0 and 15 exists then the execution was successfull
if(!file_exists($outputTmpFrames.$file["objectName"].($x == 1 ? '_w' : '_h').'00.png') && !file_exists($outputTmpFrames.$file["objectName"].($x == 1 ? '_w' : '_h').'15.png')){
echo("Error when generating the 360");
// Writting the error on the status file
array_push($statusJson, [
"Povray rendering" => [
"status" => "error",
"message" => $outputSTLPOV
]
]);
$fp = fopen($STATUSARG.'/status.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
$fp = fopen('/app/files/results.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
return 0;
} else {
array_push($statusJson, [
"Povray rendering" => [
"status" => "done",
"progress" => ($x == 1 ? '40%' : '60%')
]
]);
$fp = fopen($STATUSARG.'/status.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
$final = imagecreatetruecolor(($framesTotal+1)*$Width, $Height);
$transparent = imagecolorallocatealpha( $final, 0, 0, 0, 127 );
imagefill( $final, 0, 0, $transparent );
// Merging the 16 frames into 1 the 360 image
for ($i = 0; $i < 16; $i++){
$frameToAdd = imagecreatefrompng('tmp/'.$file["objectName"].'/'.$file["objectName"].($x == 1 ? '_w' : '_h').($i < 10 ? '0' : '').$i.'.png');
// If its the first frame then keep the orinial file or else use the last generated file
if($i > 0){
$final = imagecreatefrompng($OUTPUTARG.'/'.$file["objectName"].($x == 1 ? '_w' : '_h').'_360.png');
}
// Copy the new frame inside the previously generated file
imagecopyresampled($final, $frameToAdd, $i*$Width, 0, 0, 0, $Width, $Height, $Width, $Height);
// Turn off alpha blending
imagealphablending($final, false);
// Do desired operations
// Set alpha flag
imagesavealpha($final, true);
imagepng($final, $OUTPUTARG.'/'.$file["objectName"].($x == 1 ? '_w' : '_h').'_360.png', 9);
// Clearing file from memory
imagedestroy($frameToAdd);
imagedestroy($final);
}
// Optipng
exec('optipng -o1 '.$OUTPUTARG.'/'.$file["objectName"].($x == 1 ? '_w' : '_h').'_360.png');
}
}
array_push($statusJson, [
"360 making" => [
"status" => "done",
"progress" => "80%"
]
]);
$fp = fopen($STATUSARG.'/status.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
}
}
// Clearing the tmp folder recursively
$dir = 'tmp';
$it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->isDir()){
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
$time_end = microtime(true);
$execution_time = ($time_end - $time_start)/60;
array_push($statusJson, [
"processing" => [
"status" => "done",
"progress" => "100%",
"execution_time" => $execution_time
]
]);
// Writting the status file
$fp = fopen($STATUSARG.'/status.json', 'w');
fwrite($fp, json_encode($statusJson));
fclose($fp);
return 0;