-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.html
executable file
·780 lines (654 loc) · 19.9 KB
/
main.html
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
<!DOCTYPE html>
<!--
main.html
Author: Aven Bross
Simple driving simulation with odometry and laser range sensor.
-->
<html style="overflow: hidden;">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Wildfire Simulation</title>
<style>
canvas {
display: inline;
}
</style>
</head>
<body onload="appMain('can1')"
style="overflow: hidden; margin: 0; padding: 0; background-color: #000000;">
<div id="can1text"></div>
<div id="canvas-container">
<canvas id="can1" width="600" height="600"
style="margin: 0; padding: 0;"></canvas>
</div>
<script id="vshader1" type="x-shader/x-vertex">
// Vertex Shader #1
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
attribute vec4 vertex_attr;
attribute vec4 color_attr;
varying vec4 color_var;
varying vec3 ptobjcoords;
void main() {
// Compute projected vertex position
gl_Position = projectionMatrix * modelViewMatrix * vertex_attr;
// Send color to fragment shader
color_var = color_attr;
// Send object coordinates to fragment shader
ptobjcoords = vertex_attr.xyz / vertex_attr.w;
}
</script>
<script id="fshader1" type="x-shader/x-fragment">
// Fragment Shader #1
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 color_var;
varying vec3 ptobjcoords;
void main() {
// Set color
gl_FragColor = color_var;
}
</script>
<script type="text/javascript" src="include/J3DIMath.js"></script>
<script type="text/javascript" src="include/webgl-ggc.js"></script>
<script type="text/javascript" src="include/mersenne-twister.js"></script>
<script type="text/javascript" src="src/slam.js"></script>
<script type="text/javascript">
// General
var canvas; // Our canvas
var cwidth, cheight; // Canvas width & height
var gl; // WebGL context
var redisplay_needed; // True if redisplay needed
var isClicked = false;
var totaltime = 0;
// Shaders
var prog1; // Shader program object
// Random number generator
var rng = new MersenneTwister();
// Array of all lines in the scene
var lines = []
var lastScan = [];
// Current point
var setPoint = null;
var resolution = 360;
var pause = false;
var first_run = true;
var cartogrobot;
var theoreticalLocation;
var theoreticalOrientation;
var dp_slam;
var control;
var map;
var x_min = 0, x_max = 0, y_min = 0, y_max = 0;
// Robot, made it a class for some reason...
function robot(x, y, angle) {
this.x = x;
this.y = y;
this.angle = angle;
// Scans environment with given density and returns distance vectors
this.scan = function(scanLines){
// The vector of distance scans to be reported
var data = [];
// Setup initial angle and delta
var angle = this.angle;
var delta = 2.0*Math.PI / scanLines;
// Scan in a circle, starting at the robot's current orientation
for(var i=0; i<scanLines; i++){
// Calculate scan line
var scanLine = {p1: { x: this.x, y: this.y },
p2: { x: Math.cos(angle)*100, y: Math.sin(angle)*100 }};
// Tracks the closest intersection
var bestPoint = null;
var bestLine = null;
// Look at all lines and find intersections
for(var j=0; j<lines.length; j++) {
line = lines[j];
var point = intersection(scanLine, line);
if(point != null) {
var polar = {
r: Math.sqrt(Math.pow(this.x-point.x, 2)
+ Math.pow(this.y-point.y, 2)),
angle: Math.atan2(point.y, point.x)
};
if(bestPoint == null || polar.r < bestPoint.r) {
bestPoint = polar;
bestLine = {
p1: { x: this.x, y: this.y },
p2: point
};
}
}
}
// Save closest intersection as the distance scan
if(bestPoint != null) {
data.push(bestPoint.r + sample_normal(0.0, 0.001));
//console.log(bestPoint);
lastScan.push(bestLine);
}
else {
data.push(0.0);
}
// Increment the angle
angle += delta;
// Correct into the range [0,2pi)
if(angle >= 2.0*Math.PI) {
angle -= 2.0*Math.PI;
}
}
//console.log(data.length);
return data;
};
}
// Find the intersection between two line segments if it exists and returns it in polar coordinates
function intersection(line1, line2) {
var x12 = line1.p1.x - line1.p2.x;
var x34 = line2.p1.x - line2.p2.x;
var y12 = line1.p1.y - line1.p2.y;
var y34 = line2.p1.y - line2.p2.y;
var c = x12 * y34 - y12 * x34;
// Intersection
var a = line1.p1.x * line1.p2.y - line1.p1.y * line1.p2.x;
var b = line2.p1.x * line2.p2.y - line2.p1.y * line2.p2.x;
var x = (a * x34 - b * x12) / c;
var y = (a * y34 - b * y12) / c;
//Check the intersection is on the line segments
if((x >= line1.p1.x && x <= line1.p2.x) || (x <= line1.p1.x && x >= line1.p2.x)){
if((y >= line1.p1.y && y <= line1.p2.y) || (y <= line1.p1.y && y >= line1.p2.y)){
if((x >= line2.p1.x && x <= line2.p2.x) || (x <= line2.p1.x && x >= line2.p2.x)){
if((y >= line2.p1.y && y <= line2.p2.y) || (y <= line2.p1.y && y >= line2.p2.y)){
//console.log(x + ", " + y);
// Convert to polar and return
return {x: x, y: y};
}
}
}
}
// If not, return
return null;
}
function recieveData(e) {
var args = e.data.split(",");
theoreticalLocation = { x: parseFloat(args[0]), y: parseFloat(args[1]) };
theoreticalOrientation = parseFloat(args[2]);
}
// WebGL Functions
function myDisplay() {
gl.clearColor(0.7, 0.7, 0.7, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(prog1);
gl.mvMatrix.makeIdentity();
gl.mvMatrix.translate(0., 0., -19.);
pushMvMatrix(gl);
drawGrid(gl);
drawLines(gl, lines, 0., 0., 0., 1.0);
drawLines(gl, lastScan, 0.9, 0., 0., 0.5);
var theoLine = { p1 :{x: theoreticalLocation.x, y: theoreticalLocation.y},
p2 :{x: theoreticalLocation.x + Math.cos(theoreticalOrientation)*2,
y: theoreticalLocation.y + Math.sin(theoreticalOrientation)*2} };
var orieLine = { p1 :{x: cartogrobot.x, y: cartogrobot.y},
p2 :{x: cartogrobot.x + Math.cos(cartogrobot.angle)*2,
y: cartogrobot.y + Math.sin(cartogrobot.angle)*2} };
drawLines(gl, [ theoLine ], 0., 0., 0.7, 1.0);
drawLines(gl, [orieLine], 0., 0.7, 0.1, 1.0);
popMvMatrix(gl);
gl.flush();
}
function myIdle() {
// Get elapsed time
var elapsedtime = getElapsedTime(0.1); // Param: max value to return
totaltime += elapsedtime; // Track total elapsed time
if(totaltime > 1.0) {
if(pause == false) {
update();
}
if(first_run == true) {
pause = true;
first_run = false;
}
totaltime = 0.0;
}
//redisplay_needed = true;
}
function myReshape(w, h) {
// Set up viewport
gl.viewport(0, 0, w, h);
// Set up projection
gl.pMatrix.makeIdentity();
gl.pMatrix.perspective(60., w/h, 0.1, 20.0);
redisplay_needed = true;
}
function scan() {
while(lastScan.length > 0){
lastScan.pop();
}
var data = cartogrobot.scan(resolution);
redisplay_needed = true;
return data;
}
function update() {
if(control.last.equals(cartogrobot)) {
control.update(new location_t(
cartogrobot.x,
cartogrobot.y,
cartogrobot.angle
));
}
else {
control.update(new location_t(
cartogrobot.x + sample_normal(0.0, 0.0001),
cartogrobot.y + sample_normal(0.0, 0.0001),
cartogrobot.angle + sample_normal(0.0, 0.00001)
));
}
dp_slam.update(control, scan());
var x_dist = Math.ceil(11 * cwidth / cheight);
x_min = -1 * x_dist;
x_max = 1 + x_dist;
var sample = dp_slam.sample(x_min, x_max, y_min, y_max);
theoreticalLocation.x = sample.location.x;
theoreticalLocation.y = sample.location.y;
theoreticalOrientation = sample.location.angle;
map = sample.map;
}
function myKeyboard(ch)
{
// Note: Escape, arrows, etc. are handled inconsistently between
// browsers. Alas! So we only deal with printable ASCII.
switch (ch)
{
case ' ':
pause = !pause;
break;
case 'c':
case 'C':
// Clear all lines from the scene
while(lines.length > 0){
lines.pop();
}
redisplay_needed = true;
break;
case 'z':
case 'Z':
lines.pop();
redisplay_needed = true;
break;
case 'r':
case 'R':
cartogrobot.x = 0.0;
cartogrobot.y = 0.0;
cartogrobot.angle = 0.0;
theoreticalPosition = { x: 0.0, y: 0.0 };
theoreticalOrientation = 0.0;
redisplay_needed = true;
break;
case 's':
case 'S':
cartogrobot.x -= Math.cos(cartogrobot.angle)*0.1;
cartogrobot.y -= Math.sin(cartogrobot.angle)*0.1;
redisplay_needed = true;
break;
case 'w':
case 'W':
cartogrobot.x += Math.cos(cartogrobot.angle)*0.1;
cartogrobot.y += Math.sin(cartogrobot.angle)*0.1;
redisplay_needed = true;
break;
case 'a':
case 'A':
cartogrobot.angle += Math.PI/72;
redisplay_needed = true;
break;
case 'd':
case 'D':
cartogrobot.angle -= Math.PI/72;
redisplay_needed = true;
break;
case 'q':
case 'Q':
resolution -= 4;
break;
case 'e':
case 'E':
resolution += 4;
break;
default:
break;
}
}
function myMouseMove(x,y) {
}
function myMouseDown(x,y, right_click) {
var point = { x: x, y: y };
if(setPoint == null) {
setPoint = point;
}
else{
var line = { p1: setPoint, p2: point };
lines.push(line);
setPoint = null;
scan();
}
redisplay_needed = true;
}
function myMouseUp(x,y) {
}
function init() {
// Initialize variables here
cartogrobot = new robot(0.0,0.0,0.0);
var motion = new odometry_motion_model_t(0.00001, 0.00001, 0.0001, 0.00001);
var measurement = new beam_measurement_model_t(2.0, 20.0, 36, 360);
dp_slam = new dp_slam_t(50, motion, measurement);
control = new control_t(
new location_t(0.0, 0.0, 0.0),
new location_t(0.0, 0.0, 0.0)
);
theoreticalLocation = {x: 0.0, y: 0.0};
theoreticalOrientation = 0.0;
y_min = -11;
y_max = 11;
var x_dist = Math.ceil(11 * cwidth / cheight);
x_min = -1 * x_dist;
x_max = 1 + x_dist;
update();
redisplay_needed = true;
// Shaders
prog1 = makeProgramObjectFromIds(gl, 'vshader1', 'fshader1');
}
// Below is kind of a mini-GLUT
//
// Call appMain(canvasId) to start up the app (on document load?)
//
// These global variables should be declared:
// canvas - Our canvas object
// cwidth - Width of canvas (pixels)
// cheight - Height of canvas (pixels)
// gl - WebGL context
// redisplay_needed - Set to true to indicate redisplay needed
//
// These functions will be called as needed, if they exist:
// init
// myDisplay
// myReshape
// myIdle
// myKeyboard
// doFrame
// Should be called repeatedly. Calls myIdle and, if needed, myReshape,
// myDisplay.
function doFrame() {
// Idle
if (myIdle) myIdle();
// Reshape if necessary
if (canvas.width != cwidth || canvas.height != cheight) {
cwidth = canvas.width;
cheight = canvas.height;
if (myReshape) myReshape(cwidth, cheight);
redisplay_needed=true;
}
// Display if necessary
if (redisplay_needed) {
redisplay_needed = false;
if (myDisplay) myDisplay();
}
}
// sizeCanvas
// Called on window resize events
function sizeCanvas() {
var w = window.innerWidth;
var h = window.innerHeight;
canvas.width = w;
cwidth = w;
canvas.height = h;
cheight = h;
myReshape(w, h);
}
// doKeypress
// Called on keypress event
function doKeypress(evt) {
if (evt.charCode && myKeyboard)
myKeyboard(String.fromCharCode(evt.charCode));
}
// doMouseMove
// Called on mousemove event
function doMouseMove(evt) {
var hscale = 22;
var wscale = 22 * cwidth / cheight;
var x = ((evt.clientX-(window.innerWidth-canvas.width)/2)/cwidth)*wscale-wscale/2;
var y = (evt.clientY/cheight)*(-hscale)+hscale/2;
if(myMouseMove) myMouseMove(x, y);
}
// doMouseDown
// Called on click event
function doMouseDown(evt) {
var hscale = 22;
var wscale = 22 * cwidth / cheight;
var x = ((evt.clientX-(window.innerWidth-canvas.width)/2)/cwidth)*wscale-wscale/2;
var y = (evt.clientY/cheight)*(-hscale)+hscale/2;
if(myMouseDown) myMouseDown(x, y);
}
function doMouseUp(evt) {
var hscale = 22;
var wscale = 22 * cwidth / cheight;
var x = ((evt.clientX-(window.innerWidth-canvas.width)/2)/cwidth)*wscale-wscale/2;
var y = (evt.clientY/cheight)*(-hscale)+hscale/2;
if(myMouseUp) myMouseUp(x, y);
}
function stopContext(evt) {
evt.preventDefault();
}
// addEvent
// Used to add an extra event to the resize event without overwriting
var addEvent = function(elem, type, eventHandle) {
if (elem == null || typeof(elem) == 'undefined') return;
if ( elem.addEventListener ) {
elem.addEventListener( type, eventHandle, false );
}
else if ( elem.attachEvent ) {
elem.attachEvent( "on" + type, eventHandle );
}
else {
elem["on"+type]=eventHandle;
}
}
// appMain
// Our "main" function
function appMain(canvasId) {
// Initialize canvas, gl
canvas = getCanvas(canvasId);
gl = getGlContext(canvas);
if (!gl)
return;
// Application-specific initialization
if (init) init();
// Add event listeners
sizeCanvas();
addEvent(window, "resize", sizeCanvas);
document.addEventListener('keypress', doKeypress, false);
document.getElementById("can1").addEventListener('mousemove', doMouseMove, false);
document.getElementById("can1").addEventListener('mousedown', doMouseDown, false);
document.getElementById("can1").addEventListener('contextmenu', stopContext, false);
document.getElementById("can1").addEventListener('mouseup', doMouseUp, false);
// Do our main loop
redisplay_needed = true;
animate(doFrame);
}
// draw set of lines in one call with vbo
function drawLines(ctx, lineArray, r, g, b, a) {
// Get attribute locations
var attriblocs = getAttribLocs(ctx);
if (!attriblocs) {
errOut(arguments.callee.name + ': ' +
'Could not get attribute locations');
return;
}
// Create VBOs
var buffs = new Array(5);
var datas = new Array(5);
var array_size = 4*2*lineArray.length;
for (var i = 0; i < 5; ++i) {
buffs[i] = ctx.createBuffer();
var components = (i == 2 || i == 4) ? 3 : 4;
datas[i] = new Float32Array(components*array_size);
}
var b4 = 0;
var b3 = 0;
for(var i=0; i<lineArray.length; i++) {
var line = lineArray[i];
// vertex coords
datas[0][b4+0] = line.p1.x;
datas[0][b4+1] = line.p1.y;
datas[0][b4+2] = 0.;
datas[0][b4+3] = 1.;
datas[0][b4+4] = line.p2.x;
datas[0][b4+5] = line.p2.y;
datas[0][b4+6] = 0.;
datas[0][b4+7] = 1.;
// color
datas[1][b4+0] = r;
datas[1][b4+1] = g;
datas[1][b4+2] = b;
datas[1][b4+3] = a;
datas[1][b4+4] = r;
datas[1][b4+5] = g;
datas[1][b4+6] = b;
datas[1][b4+7] = a;
// normal
datas[2][b3+0] = 0.;
datas[2][b3+1] = 0.;
datas[2][b3+2] = 1.;
datas[2][b3+3] = 0.;
datas[2][b3+4] = 0.;
datas[2][b3+5] = 1.;
// texture coords
datas[3][b4+0] = line.p1.x;
datas[3][b4+1] = line.p1.y;
datas[3][b4+2] = 0.;
datas[3][b4+3] = 1.;
datas[3][b4+4] = line.p2.x;
datas[3][b4+5] = line.p2.y;
datas[3][b4+6] = 0.;
datas[3][b4+7] = 1.;
// tangent
datas[4][b3+0] = 1.;
datas[4][b3+1] = 0.;
datas[4][b3+2] = 0.;
datas[4][b3+3] = 1.;
datas[4][b3+4] = 0.;
datas[4][b3+5] = 0.;
b4 += 8;
b3 += 6;
}
for (var i in attriblocs) {
if (attriblocs[i] == -1)
continue;
var components = (i == 2 || i == 4) ? 3 : 4;
ctx.bindBuffer(ctx.ARRAY_BUFFER, buffs[i]);
ctx.bufferData(
ctx.ARRAY_BUFFER, datas[i], ctx.STATIC_DRAW);
ctx.vertexAttribPointer(
attriblocs[i], components, ctx.FLOAT, false, 0, 0);
}
// Set up uniforms, enable attributes
sendMatrices(ctx);
for (var i in attriblocs)
if (attriblocs[i] != -1)
ctx.enableVertexAttribArray(attriblocs[i]);
// Draw with VBO
ctx.drawArrays(ctx.LINES, 0, array_size);
// Disable attributes
for (var i in attriblocs)
if (attriblocs[i] != -1)
ctx.disableVertexAttribArray(attriblocs[i]);
// Delete buffer objects
for (i in buffs)
ctx.deleteBuffer(buffs[i]);
}
// Draws the forest in one draw call with VBO
function drawGrid(ctx)
{
// Get attribute locations
var attriblocs = getAttribLocs(ctx);
if (!attriblocs)
{
errOut(arguments.callee.name + ': ' +
'Could not get attribute locations');
return;
}
// Create VBOs
var buffs = new Array(5);
var datas = new Array(5);
var array_size = 6 * Math.ceil(1 + 22 * cwidth / cheight) * (22);
for (var i = 0; i < 5; ++i)
{
buffs[i] = ctx.createBuffer();
var components = (i == 2 || i == 4) ? 3 : 4;
datas[i] = new Float32Array(components * array_size);
}
var b4 = 0;
var b3 = 0;
for(var x = x_min; x < x_max; x++){
for(var y = y_min; y < y_max; y++){
var color = 0.9;
if(map[x][y]) color = 0.5;
for (var i = 0; i < 6; ++i)
{
var t_x = (i == 1 || i == 4 || i == 5) ? 0. : 1.;
var t_y = (i == 2 || i == 3 || i == 5) ? 0. : 1.;
var size = 0.95;
// vertex coords
datas[0][b4+0] = x + (t_x) * size;
datas[0][b4+1] = y + (t_y) * size;
datas[0][b4+2] = 0.;
datas[0][b4+3] = 1.;
// color
datas[1][b4+0] = color;
datas[1][b4+1] = color;
datas[1][b4+2] = color;
datas[1][b4+3] = 0.5;
// normal
datas[2][b3+0] = 0.;
datas[2][b3+1] = 0.;
datas[2][b3+2] = 1.;
// texture coords
datas[3][b4+0] = x;
datas[3][b4+1] = y;
datas[3][b4+2] = 0.;
datas[3][b4+3] = 1.;
// tangent
datas[4][b3+0] = 1.;
datas[4][b3+1] = 0.;
datas[4][b3+2] = 0.;
b4 += 4;
b3 += 3;
}
}
}
for (var i in attriblocs)
{
if (attriblocs[i] == -1)
continue;
var components = (i == 2 || i == 4) ? 3 : 4;
ctx.bindBuffer(ctx.ARRAY_BUFFER, buffs[i]);
ctx.bufferData(
ctx.ARRAY_BUFFER, datas[i], ctx.STATIC_DRAW);
ctx.vertexAttribPointer(
attriblocs[i], components, ctx.FLOAT, false, 0, 0);
}
// Set up uniforms, enable attributes
sendMatrices(ctx);
for (var i in attriblocs)
if (attriblocs[i] != -1)
ctx.enableVertexAttribArray(attriblocs[i]);
// Draw with VBO
ctx.drawArrays(ctx.TRIANGLES, 0, array_size);
// Disable attributes
for (var i in attriblocs)
if (attriblocs[i] != -1)
ctx.disableVertexAttribArray(attriblocs[i]);
// Delete buffer objects
for (i in buffs)
ctx.deleteBuffer(buffs[i]);
}
</script>
</body>
</html>