-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmodel_display.php
More file actions
454 lines (385 loc) · 15.2 KB
/
Copy pathmodel_display.php
File metadata and controls
454 lines (385 loc) · 15.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
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
<?php
/* M O D E L _ D I S P L A Y . P H P
* BRL-CAD
*
* Copyright (c) 1995-2013 United States Government as represented by
* the U.S. Army Research Laboratory.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this file; see the file named COPYING for more
* information.
*/
/** @file geometry_viewer/model_display.php
*
*/
include 'accounts/auth.php';
include 'variables.php';
include 'config.php';
?>
<!doctype html>
<html lang="en">
<head>
<title><?php echo $title; ?></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,
user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel=stylesheet href="css/base.css"/>
<link rel=stylesheet href="css/bootstrap.css"/>
<link href="css/pace/themes/pace-theme-minimal.css" rel="stylesheet" />
</head>
<body>
<script src="js/pace.js"></script>
<script src="js/three.min.js"></script>
<script src="js/Detector.js"></script>
<script src="js/Stats.js"></script>
<script src="js/loaders/OBJLoader.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/THREEx.FullScreen.js"></script>
<script src="js/THREEx.WindowResize.js"></script>
<script src="js/KeyboardState.js"></script>
<script src="js/jquery-1.10.2.min.js"></script>
<div id="top-bar" class="effect8">
<div>
<a href="upload.php"><img id="top-bar-logo" src="images/BRL-CAD_gear_logo_256.png"></a>
</div>
<div id="logo-text">BRL-CAD</div>
<div id="slogan">Geometry Viewer</div>
<div class="vertical-line"> </div>
<div id="login-message" style="text-align: right; padding-right: 10px;">
<h5>You are logged in as: <?php echo $username; ?> | <a href="accounts/logout.php">Logout</a></h5>
</div>
<div id="db-file-name">
<?php
$dbFileName=$_GET['dbFileName'];
echo "File uploaded: $dbFileName" ?>
</div>
</div>
<div id="ThreeJS"></div>
<script>
/** Standard global variables. */
var container, scene, camera, renderer, controls, stats;
var clock = new THREE.Clock();
/**
* Creating object of KeyboardState() to implement
* keyboard shorcut keys to see different views of model.
*/
var keyboard = new KeyboardState();
/**
* Global obj model array, used to apply wireframe/shade
* to models.
*/
var objGlobalObject = [];
/** custom global variables */
var entitiesInScene = new Array();
var objEntitiesIndex, dbFileName;
var objPath = <?php echo json_encode($objPath); ?>;
/**
* A N I M A T E
*/
function animate()
{
requestAnimationFrame(animate);
render();
update();
}
/**
* U P D A T E
*/
function update()
{
keyboard.update();
/** Keyboard options to see different views of model. */
if (keyboard.down("T")) {
camera.position.set(0, 100, 0);
}
if (keyboard.down("R")) {
camera.position.set(0, 0, 100);
}
if (keyboard.down("B")) {
camera.position.set(0, -100, 0);
}
if (keyboard.down("L")) {
camera.position.set(100, 0, 0);
}
/**
* If "W" is pressed, wireframe applied to all models
* in the scene.
*/
if (keyboard.down("W")) {
var OBJMaterial = new THREE.MeshBasicMaterial({color: 0x000000, wireframe: true});
for (var i = 0; i < objGlobalObject.length; i++) {
objGlobalObject[i].traverse (function (child) {
if (child instanceof THREE.Mesh) {
child.material = OBJMaterial;
}
});
}
}
/**
* If "S" is pressed, shade applied to all models
* in the scene.
*/
if (keyboard.down("S")) {
var OBJMaterial = new THREE.MeshPhongMaterial({color: Math.random() * 0xffffff});
for (var i = 0; i < objGlobalObject.length; i++) {
objGlobalObject[i].traverse (function (child){
if (child instanceof THREE.Mesh) {
child.material = OBJMaterial;
}
});
}
}
controls.update();
stats.update();
}
/**
* R E N D E R
*/
function render()
{
renderer.render(scene, camera);
}
/**
* M U L T I P L E _ O B J _ L O A D E R
*
* Loads multiple OBJ files. Takes array as argument.
*/
function multiple_obj_loader(objFile)
{
/** Material of OBJ model. */
var OBJMaterial = new THREE.MeshPhongMaterial({color: 0x8888ff});
var loader = new THREE.OBJLoader();
loader.load(objFile[k], function (object){
object.traverse (function (child){
if (child instanceof THREE.Mesh) {
child.material = OBJMaterial;
}
});
object.position.y = 0.1;
scene.add(object);
});
}
/**
* L O A D _ O B J _ F I L E S
*
* Calls multiple_obj_loader in a loop to load multiple
* obj files.
*/
function load_obj_files(objF)
{
for (k = 0; k < objF.length; k++) {
multiple_obj_loader(objF);
}
}
/**
* S I N G L E _ O B J _ L O A D E R
*
* Load an obj file by taking its name as argument.
*/
function single_obj_loader(objFile)
{
var entityName = objFile;
/**
* Applying material to OBJ model. By default,
* wireframe will be applied.
*/
var OBJMaterial = new THREE.MeshPhongMaterial({color: 0x000000, wireframe: true});
var loader = new THREE.OBJLoader();
loader.load(objPath+"/"+objFile, function (object){
objGlobalObject.push(object);
object.traverse (function (child){
if (child instanceof THREE.Mesh) {
child.material = OBJMaterial;
}
});
object.position.y = 0.1;
/**
* By default, models appear tilted(one side
* raised). So rotating model to make them
* appear horizontally.
*/
object.rotation.z = 90 * Math.PI/180;
object.rotation.x = -90 * Math.PI/180;
scene.add(object);
objEntitiesIndex = scene.children.length;
entitiesInScene[objEntitiesIndex] = entityName;
});
}
/**
* A D D _ E N T I T Y
*
* Adds an entity to ThreeJS scene.
*/
function add_entity(entity)
{
/**
* Sending AJAX request to create_obj.php to create
* obj file for a model(to fulfil user's "view" button
* click request). If sent request served successfully,
* receiving name of obj file in 'data' variable in
* response from create_obj.php and passing it
* to single_obj)loader() to load it to ThreeJS scene.
*/
$.post('create_obj.php', {db: dbFileName, en: entity}, function(data){
if (data == entity+".obj"){
single_obj_loader(data);
}
});
}
/**
* D E L E T E _ E N T I T Y
*
* Delete an entity from ThreeJS scene.
*/
function delete_entity(entity)
{
var rmElementIndex = entitiesInScene.indexOf(entity+".obj");
if (rmElementIndex != '-1') {
entitiesInScene.splice(rmElementIndex, 1);
rmElement = scene.children[rmElementIndex - 1];
scene.remove(rmElement);
} else {
alert("Invalid operation.");
}
}
/**
* I N I T
*
* Initializes ThreeJS scene.
*/
function init()
{
/** Scene */
scene = new THREE.Scene();
/**
* Set the view size in pixels (custom or according
* to window size).
*/
var screenWidth = window.innerWidth;
var screenHeight = window.innerHeight;
/** Camera attributes. */
var viewAngle = 45;
var aspect = screenWidth / screenHeight;
var near = 0.1, far = 20000000;
/** Set up camera. */
camera = new THREE.PerspectiveCamera(viewAngle, aspect, near, far);
/** Add camera to the scene. */
scene.add(camera);
/**
* Set camera position (default position is
* (0, 0, 0)) and set the angle towards the scene
* origin.
*/
camera.position.set(1000, 1000, 1000);
camera.lookAt(scene.position);
/**
* Create and start the renderer;
* choose antialias setting.
*/
if (Detector.webgl) {
renderer = new THREE.WebGLRenderer({antialias:true});
} else {
renderer = new THREE.CanvasRenderer();
}
renderer.setSize(screenWidth, screenHeight);
/**
* Attach div element to variable to contain
* the renderer.
*/
container = document.getElementById('ThreeJS');
/** Attach renderer to the container div. */
container.appendChild(renderer.domElement);
/** Automatically resize renderer. */
THREEx.WindowResize(renderer, camera);
/** Toggle full-screen on given key press. */
THREEx.FullScreen.bindKey({ charCode: 'm'.charCodeAt(0) });
/** Controls */
/**
* Move mouse and: left click to rotate,
* middle click to zoom,
* right click to pan.
*/
controls = new THREE.OrbitControls(camera, renderer.domElement);
/** Stats */
/**
* Displays current and past frames per second
* attained by scene.
*/
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.bottom = '0px';
stats.domElement.style.zIndex = 100;
container.appendChild(stats.domElement);
/** Light */
/** Adding lights to scene. */
var light = new THREE.PointLight(0xffffff);
light.position = camera.position;
scene.add(light);
var ambientLight = new THREE.AmbientLight(0x111111);
scene.add(ambientLight);
/** Geometry */
/**
* Create a set of coordinate axes to help orient user
* specify length in pixels in each direction.
*/
var axes = new THREE.AxisHelper(10000);
scene.add(axes);
/** Grid */
var grid = new THREE.GridHelper(300, 10);
scene.add(grid);
/**
* Receive data from URL using GET method.
* TODO: Should use POST method.
*/
<?php
$entitiesString = $_GET['entitiesString'];
$dbFileName = $_GET['dbFileName'];
?>
var entitiesString = <?php echo json_encode($entitiesString); ?>;
dbFileName = <?php echo json_encode($dbFileName); ?>;
var entitiesList = new Array();
entitiesList = entitiesString.split(" ");
var totalEntities = entitiesList.length;
/**
* Creates left side bar to display list of
* entities, each entity having corresponding pair of
* "view" and "delete" button.
*
* TODO: Clean it.
*/
document.write("<div id=\"leftSideBar\" class=\"effect6\"><table><th>Entities:</th>");
for (var h = 0; h < totalEntities-1; h++) {
if (entitiesList[h] == "_GLOBAL") {
h = h + 1;
document.write("<tr><td>"+entitiesList[h]+"</td><td><a id=\"view\" href=# onclick=\"add_entity(this.name)\" name=\""+entitiesList[h]+"\" value=\""+entitiesList[h]+"\">View</td></a>"+" <td><a id=\"hide\" href=# onclick=\"delete_entity(this.name)\" name=\""+entitiesList[h]+"\" value=\""+entitiesList[h]+"\">Hide</a></td></tr>");
} else {
document.write("<tr><td>"+entitiesList[h]+"</td><td><a id=\"view\" href=# onclick=\"add_entity(this.name)\" name=\""+entitiesList[h]+"\" value=\""+entitiesList[h]+"\">View</td></a>"+" <td><a id=\"hide\" href=# onclick=\"delete_entity(this.name)\" name=\""+entitiesList[h]+"\" value=\""+entitiesList[h]+"\">Hide</a></td></tr>");
}
}
document.write("</table></div>");
}
/** Initialization */
init();
/** Animation loop */
animate();
</script>
</body>
</html>
<?php
/*
* Local Variables:
* mode: PHP
* tab-width: 8
* End:
* ex: shiftwidth=4 tabstop=8
*/
?>