Deprecate WebKit2 plug-in support.
[WebKit-https.git] / JSTests / slowMicrobenchmarks / v8-raytrace-with-try-catch-high-frequency-throws.js
1 // The ray tracer code in this file is written by Adam Burmister. It
2 // is available in its original form from:
3 //
4 //   http://labs.flog.nz.co/raytracer/
5 //
6 // It has been modified slightly by Google to work as a standalone
7 // benchmark, but the all the computational code remains
8 // untouched. This file also contains a copy of parts of the Prototype
9 // JavaScript framework which is used by the ray tracer.
10
11 // Variable used to hold a number that can be used to verify that
12 // the scene was ray traced correctly.
13 var checkNumber;
14
15
16 // ------------------------------------------------------------------------
17 // ------------------------------------------------------------------------
18
19 // The following is a copy of parts of the Prototype JavaScript library:
20
21 // Prototype JavaScript framework, version 1.5.0
22 // (c) 2005-2007 Sam Stephenson
23 //
24 // Prototype is freely distributable under the terms of an MIT-style license.
25 // For details, see the Prototype web site: http://prototype.conio.net/
26
27 let __exceptionCounter = 0;
28 function randomException() {
29     __exceptionCounter++;
30     if (__exceptionCounter % 35 === 0) {
31         throw new Error("rando");
32     }
33 }
34 noInline(randomException);
35
36 var Class = {
37     create: function() {
38         return function() {
39             try {
40                 this.initialize.apply(this, arguments);
41                 randomException();
42             } catch(e) { }
43         }
44     }
45 };
46
47
48 Object.extend = function(destination, source) {
49     for (var property in source) {
50         try {
51             destination[property] = source[property];
52             randomException();
53         } catch(e) { }
54     }
55     return destination;
56 };
57
58
59 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
61
62 // The rest of this file is the actual ray tracer written by Adam
63 // Burmister. It's a concatenation of the following files:
64 //
65 //   flog/color.js
66 //   flog/light.js
67 //   flog/vector.js
68 //   flog/ray.js
69 //   flog/scene.js
70 //   flog/material/basematerial.js
71 //   flog/material/solid.js
72 //   flog/material/chessboard.js
73 //   flog/shape/baseshape.js
74 //   flog/shape/sphere.js
75 //   flog/shape/plane.js
76 //   flog/intersectioninfo.js
77 //   flog/camera.js
78 //   flog/background.js
79 //   flog/engine.js
80
81
82 /* Fake a Flog.* namespace */
83 if(typeof(Flog) == 'undefined') var Flog = {};
84 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
85
86 Flog.RayTracer.Color = Class.create();
87
88 Flog.RayTracer.Color.prototype = {
89     red : 0.0,
90     green : 0.0,
91     blue : 0.0,
92
93     initialize : function(r, g, b) {
94         try {
95             if(!r) r = 0.0;
96             if(!g) g = 0.0;
97             if(!b) b = 0.0;
98
99             this.red = r;
100             this.green = g;
101             this.blue = b;
102             randomException();
103         } catch(e) { }
104     },
105
106     add : function(c1, c2){
107         try {
108             var result = new Flog.RayTracer.Color(0,0,0);
109
110             result.red = c1.red + c2.red;
111             result.green = c1.green + c2.green;
112             result.blue = c1.blue + c2.blue;
113
114             randomException();
115         } catch(e) { }
116
117         return result;
118     },
119
120     addScalar: function(c1, s){
121         try {
122             var result = new Flog.RayTracer.Color(0,0,0);
123
124             result.red = c1.red + s;
125             result.green = c1.green + s;
126             result.blue = c1.blue + s;
127
128             result.limit();
129
130             randomException();
131         } catch(e) { }
132
133         return result;
134     },
135
136     subtract: function(c1, c2){
137         try {
138             var result = new Flog.RayTracer.Color(0,0,0);
139
140             result.red = c1.red - c2.red;
141             result.green = c1.green - c2.green;
142             result.blue = c1.blue - c2.blue;
143
144             randomException();
145         } catch(e) { }
146
147         return result;
148     },
149
150     multiply : function(c1, c2) {
151         try {
152             var result = new Flog.RayTracer.Color(0,0,0);
153
154             result.red = c1.red * c2.red;
155             result.green = c1.green * c2.green;
156             result.blue = c1.blue * c2.blue;
157
158             randomException();
159         } catch(e) { }
160
161         return result;
162     },
163
164     multiplyScalar : function(c1, f) {
165         try {
166             var result = new Flog.RayTracer.Color(0,0,0);
167
168             result.red = c1.red * f;
169             result.green = c1.green * f;
170             result.blue = c1.blue * f;
171
172             randomException();
173         } catch(e) { }
174
175         return result;
176     },
177
178     divideFactor : function(c1, f) {
179         try {
180             var result = new Flog.RayTracer.Color(0,0,0);
181
182             result.red = c1.red / f;
183             result.green = c1.green / f;
184             result.blue = c1.blue / f;
185
186             randomException();
187         } catch(e) { }
188
189         return result;
190     },
191
192     limit: function(){
193         try { 
194             this.red = (this.red > 0.0) ? ( (this.red > 1.0) ? 1.0 : this.red ) : 0.0;
195             this.green = (this.green > 0.0) ? ( (this.green > 1.0) ? 1.0 : this.green ) : 0.0;
196             this.blue = (this.blue > 0.0) ? ( (this.blue > 1.0) ? 1.0 : this.blue ) : 0.0;
197
198             randomException();
199         } catch(e) { }
200     },
201
202     distance : function(color) {
203         try {
204             var d = Math.abs(this.red - color.red) + Math.abs(this.green - color.green) + Math.abs(this.blue - color.blue);
205             randomException();
206         } catch(e) { }
207         return d;
208     },
209
210     blend: function(c1, c2, w){
211         try {
212             var result = new Flog.RayTracer.Color(0,0,0);
213             result = Flog.RayTracer.Color.prototype.add(
214                     Flog.RayTracer.Color.prototype.multiplyScalar(c1, 1 - w),
215                     Flog.RayTracer.Color.prototype.multiplyScalar(c2, w)
216                     );
217             randomException();
218         } catch(e) { }
219         return result;
220     },
221
222     brightness : function() {
223         try {
224             var r = Math.floor(this.red*255);
225             var g = Math.floor(this.green*255);
226             var b = Math.floor(this.blue*255);
227             randomException();
228         } catch(e) { }
229         return (r * 77 + g * 150 + b * 29) >> 8;
230     },
231
232     toString : function () {
233         try {
234             var r = Math.floor(this.red*255);
235             var g = Math.floor(this.green*255);
236             var b = Math.floor(this.blue*255);
237             randomException();
238         } catch(e) { }
239
240         return "rgb("+ r +","+ g +","+ b +")";
241     }
242 }
243 /* Fake a Flog.* namespace */
244 if(typeof(Flog) == 'undefined') var Flog = {};
245 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
246
247 Flog.RayTracer.Light = Class.create();
248
249 Flog.RayTracer.Light.prototype = {
250     position: null,
251     color: null,
252     intensity: 10.0,
253
254     initialize : function(pos, color, intensity) {
255         try {
256             this.position = pos;
257             this.color = color;
258             this.intensity = (intensity ? intensity : 10.0);
259
260             randomException();
261         } catch(e) { }
262     },
263
264     toString : function () {
265         try {
266             var result = 'Light [' + this.position.x + ',' + this.position.y + ',' + this.position.z + ']';
267             randomException();
268         } catch(e) { }
269         return result;
270     }
271 }
272 /* Fake a Flog.* namespace */
273 if(typeof(Flog) == 'undefined') var Flog = {};
274 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
275
276 Flog.RayTracer.Vector = Class.create();
277
278 Flog.RayTracer.Vector.prototype = {
279     x : 0.0,
280     y : 0.0,
281     z : 0.0,
282
283     initialize : function(x, y, z) {
284         try {
285             this.x = (x ? x : 0);
286             this.y = (y ? y : 0);
287             this.z = (z ? z : 0);
288             randomException();
289         } catch(e) { }
290     },
291
292     copy: function(vector){
293         try {
294             this.x = vector.x;
295             this.y = vector.y;
296             this.z = vector.z;
297             randomException();
298         } catch(e) { }
299     },
300
301     normalize : function() {
302         try {
303             var m = this.magnitude();
304             var result = new Flog.RayTracer.Vector(this.x / m, this.y / m, this.z / m);
305             randomException();
306         } catch(e) { }
307         return result;
308     },
309
310     magnitude : function() {
311         try {
312             return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z));
313         } catch(e)  { }
314     },
315
316     cross : function(w) {
317         try {
318             return new Flog.RayTracer.Vector(
319                     -this.z * w.y + this.y * w.z,
320                     this.z * w.x - this.x * w.z,
321                     -this.y * w.x + this.x * w.y);
322         } catch(e) { }
323     },
324
325     dot : function(w) {
326         try {
327             return this.x * w.x + this.y * w.y + this.z * w.z;
328         } catch(e) { }
329     },
330
331     add : function(v, w) {
332         try {
333             return new Flog.RayTracer.Vector(w.x + v.x, w.y + v.y, w.z + v.z);
334         } catch(e) { }
335     },
336
337     subtract : function(v, w) {
338         try {
339             if(!w || !v) throw 'Vectors must be defined [' + v + ',' + w + ']';
340             return new Flog.RayTracer.Vector(v.x - w.x, v.y - w.y, v.z - w.z);
341         } catch(e) { }
342     },
343
344     multiplyVector : function(v, w) {
345         try {
346             return new Flog.RayTracer.Vector(v.x * w.x, v.y * w.y, v.z * w.z);
347         } catch(e) { }
348     },
349
350     multiplyScalar : function(v, w) {
351         try {
352             return new Flog.RayTracer.Vector(v.x * w, v.y * w, v.z * w);
353         } catch(e) { }
354     },
355
356     toString : function () {
357         try {
358             return 'Vector [' + this.x + ',' + this.y + ',' + this.z + ']';
359         } catch(e) { }
360     }
361 }
362 /* Fake a Flog.* namespace */
363 if(typeof(Flog) == 'undefined') var Flog = {};
364 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
365
366 Flog.RayTracer.Ray = Class.create();
367
368 Flog.RayTracer.Ray.prototype = {
369     position : null,
370     direction : null,
371     initialize : function(pos, dir) {
372         try {
373             this.position = pos;
374             this.direction = dir;
375             randomException();
376         } catch(e) { }
377     },
378
379     toString : function () {
380         try {
381             return 'Ray [' + this.position + ',' + this.direction + ']';
382         } catch(e) { }
383     }
384 }
385 /* Fake a Flog.* namespace */
386 if(typeof(Flog) == 'undefined') var Flog = {};
387 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
388
389 Flog.RayTracer.Scene = Class.create();
390
391 Flog.RayTracer.Scene.prototype = {
392     camera : null,
393     shapes : [],
394     lights : [],
395     background : null,
396
397     initialize : function() {
398         try {
399             this.camera = new Flog.RayTracer.Camera(
400                     new Flog.RayTracer.Vector(0,0,-5),
401                     new Flog.RayTracer.Vector(0,0,1),
402                     new Flog.RayTracer.Vector(0,1,0)
403                     );
404             this.shapes = new Array();
405             this.lights = new Array();
406             this.background = new Flog.RayTracer.Background(new Flog.RayTracer.Color(0,0,0.5), 0.2);
407
408             randomException();
409         } catch(e) { }
410     }
411 }
412 /* Fake a Flog.* namespace */
413 if(typeof(Flog) == 'undefined') var Flog = {};
414 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
415 if(typeof(Flog.RayTracer.Material) == 'undefined') Flog.RayTracer.Material = {};
416
417 Flog.RayTracer.Material.BaseMaterial = Class.create();
418
419 Flog.RayTracer.Material.BaseMaterial.prototype = {
420
421     gloss: 2.0,             // [0...infinity] 0 = matt
422     transparency: 0.0,      // 0=opaque
423     reflection: 0.0,        // [0...infinity] 0 = no reflection
424     refraction: 0.50,
425     hasTexture: false,
426
427     initialize : function() {
428
429     },
430
431     getColor: function(u, v){
432
433     },
434
435     wrapUp: function(t){
436         try {
437             t = t % 2.0;
438             if(t < -1) t += 2.0;
439             if(t >= 1) t -= 2.0;
440             randomException();
441         } catch(e) { }
442         return t;
443     },
444
445     toString : function () {
446         try {
447             return 'Material [gloss=' + this.gloss + ', transparency=' + this.transparency + ', hasTexture=' + this.hasTexture +']';
448         } catch(e) { }
449     }
450 }
451 /* Fake a Flog.* namespace */
452 if(typeof(Flog) == 'undefined') var Flog = {};
453 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
454
455 Flog.RayTracer.Material.Solid = Class.create();
456
457 Flog.RayTracer.Material.Solid.prototype = Object.extend(
458         new Flog.RayTracer.Material.BaseMaterial(), {
459             initialize : function(color, reflection, refraction, transparency, gloss) {
460                 try {
461                     this.color = color;
462                     this.reflection = reflection;
463                     this.transparency = transparency;
464                     this.gloss = gloss;
465                     this.hasTexture = false;
466                     randomException();
467                 } catch(e) { }
468             },
469
470             getColor: function(u, v){
471                 try {
472                     return this.color;
473                 } catch(e) { }
474             },
475
476             toString : function () {
477                 try {
478                     return 'SolidMaterial [gloss=' + this.gloss + ', transparency=' + this.transparency + ', hasTexture=' + this.hasTexture +']';
479                 } catch(e) { }
480             }
481         }
482         );
483 /* Fake a Flog.* namespace */
484 if(typeof(Flog) == 'undefined') var Flog = {};
485 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
486
487 Flog.RayTracer.Material.Chessboard = Class.create();
488
489 Flog.RayTracer.Material.Chessboard.prototype = Object.extend(
490         new Flog.RayTracer.Material.BaseMaterial(), {
491             colorEven: null,
492             colorOdd: null,
493             density: 0.5,
494
495             initialize : function(colorEven, colorOdd, reflection, transparency, gloss, density) {
496                 try {
497                     this.colorEven = colorEven;
498                     this.colorOdd = colorOdd;
499                     this.reflection = reflection;
500                     this.transparency = transparency;
501                     this.gloss = gloss;
502                     this.density = density;
503                     this.hasTexture = true;
504                     randomException();
505                 } catch(e) { }
506             },
507
508             getColor: function(u, v){
509                 try {
510                     var t = this.wrapUp(u * this.density) * this.wrapUp(v * this.density);
511                     randomException();
512                 } catch(e) { }
513
514                 if(t < 0.0)
515                     return this.colorEven;
516                 else
517                     return this.colorOdd;
518             },
519
520             toString : function () {
521                 try {
522                     return 'ChessMaterial [gloss=' + this.gloss + ', transparency=' + this.transparency + ', hasTexture=' + this.hasTexture +']';
523                 } catch(e) { }
524             }
525         }
526 );
527 /* Fake a Flog.* namespace */
528 if(typeof(Flog) == 'undefined') var Flog = {};
529 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
530 if(typeof(Flog.RayTracer.Shape) == 'undefined') Flog.RayTracer.Shape = {};
531
532 Flog.RayTracer.Shape.Sphere = Class.create();
533
534 Flog.RayTracer.Shape.Sphere.prototype = {
535     initialize : function(pos, radius, material) {
536         try {
537             this.radius = radius;
538             this.position = pos;
539             this.material = material;
540
541             randomException();
542         } catch(e) { }
543     },
544
545     intersect: function(ray){
546         try {
547             var info = new Flog.RayTracer.IntersectionInfo();
548             info.shape = this;
549
550             var dst = Flog.RayTracer.Vector.prototype.subtract(ray.position, this.position);
551
552             var B = dst.dot(ray.direction);
553             var C = dst.dot(dst) - (this.radius * this.radius);
554             var D = (B * B) - C;
555
556             if(D > 0){ // intersection!
557                 info.isHit = true;
558                 info.distance = (-B) - Math.sqrt(D);
559                 info.position = Flog.RayTracer.Vector.prototype.add(
560                         ray.position,
561                         Flog.RayTracer.Vector.prototype.multiplyScalar(
562                             ray.direction,
563                             info.distance
564                             )
565                         );
566                 info.normal = Flog.RayTracer.Vector.prototype.subtract(
567                         info.position,
568                         this.position
569                         ).normalize();
570
571                 info.color = this.material.getColor(0,0);
572             } else {
573                 info.isHit = false;
574             }
575
576             randomException();
577         } catch(e) { }
578         return info;
579     },
580
581     toString : function () {
582         try {
583             return 'Sphere [position=' + this.position + ', radius=' + this.radius + ']';
584         } catch(e) { }
585     }
586 }
587 /* Fake a Flog.* namespace */
588 if(typeof(Flog) == 'undefined') var Flog = {};
589 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
590 if(typeof(Flog.RayTracer.Shape) == 'undefined') Flog.RayTracer.Shape = {};
591
592 Flog.RayTracer.Shape.Plane = Class.create();
593
594 Flog.RayTracer.Shape.Plane.prototype = {
595     d: 0.0,
596
597     initialize : function(pos, d, material) {
598         try {
599             this.position = pos;
600             this.d = d;
601             this.material = material;
602             randomException();
603         } catch(e) { }
604     },
605
606     intersect: function(ray){
607         try {
608             var info = new Flog.RayTracer.IntersectionInfo();
609
610             var Vd = this.position.dot(ray.direction);
611             if(Vd == 0) return info; // no intersection
612
613             var t = -(this.position.dot(ray.position) + this.d) / Vd;
614             if(t <= 0) return info;
615
616             info.shape = this;
617             info.isHit = true;
618             info.position = Flog.RayTracer.Vector.prototype.add(
619                     ray.position,
620                     Flog.RayTracer.Vector.prototype.multiplyScalar(
621                         ray.direction,
622                         t
623                         )
624                     );
625             info.normal = this.position;
626             info.distance = t;
627
628             if(this.material.hasTexture){
629                 var vU = new Flog.RayTracer.Vector(this.position.y, this.position.z, -this.position.x);
630                 var vV = vU.cross(this.position);
631                 var u = info.position.dot(vU);
632                 var v = info.position.dot(vV);
633                 info.color = this.material.getColor(u,v);
634             } else {
635                 info.color = this.material.getColor(0,0);
636             }
637
638             randomException();
639         } catch(e) { }
640         return info;
641     },
642
643     toString : function () {
644         try {
645             return 'Plane [' + this.position + ', d=' + this.d + ']';
646         } catch(e) { }
647     }
648 }
649 /* Fake a Flog.* namespace */
650 if(typeof(Flog) == 'undefined') var Flog = {};
651 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
652
653 Flog.RayTracer.IntersectionInfo = Class.create();
654
655 Flog.RayTracer.IntersectionInfo.prototype = {
656     isHit: false,
657     hitCount: 0,
658     shape: null,
659     position: null,
660     normal: null,
661     color: null,
662     distance: null,
663
664     initialize : function() {
665         try {
666             this.color = new Flog.RayTracer.Color(0,0,0);
667             randomException();
668         } catch(e) { }
669     },
670
671     toString : function () {
672         try {
673             return 'Intersection [' + this.position + ']';
674         } catch(e) { }
675     }
676 }
677 /* Fake a Flog.* namespace */
678 if(typeof(Flog) == 'undefined') var Flog = {};
679 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
680
681 Flog.RayTracer.Camera = Class.create();
682
683 Flog.RayTracer.Camera.prototype = {
684     position: null,
685     lookAt: null,
686     equator: null,
687     up: null,
688     screen: null,
689
690     initialize : function(pos, lookAt, up) {
691         try {
692             this.position = pos;
693             this.lookAt = lookAt;
694             this.up = up;
695             this.equator = lookAt.normalize().cross(this.up);
696             this.screen = Flog.RayTracer.Vector.prototype.add(this.position, this.lookAt);
697             randomException();
698         } catch(e) { }
699     },
700
701     getRay: function(vx, vy){
702         try {
703             var pos = Flog.RayTracer.Vector.prototype.subtract(
704                     this.screen,
705                     Flog.RayTracer.Vector.prototype.subtract(
706                         Flog.RayTracer.Vector.prototype.multiplyScalar(this.equator, vx),
707                         Flog.RayTracer.Vector.prototype.multiplyScalar(this.up, vy)
708                         )
709                     );
710             pos.y = pos.y * -1;
711             var dir = Flog.RayTracer.Vector.prototype.subtract(
712                     pos,
713                     this.position
714                     );
715
716             var ray = new Flog.RayTracer.Ray(pos, dir.normalize());
717
718             randomException();
719         } catch(e) { }
720         return ray;
721     },
722
723     toString : function () {
724         try {
725             return 'Ray []';
726         } catch(e) { }
727     }
728 }
729 /* Fake a Flog.* namespace */
730 if(typeof(Flog) == 'undefined') var Flog = {};
731 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
732
733 Flog.RayTracer.Background = Class.create();
734
735 Flog.RayTracer.Background.prototype = {
736     color : null,
737     ambience : 0.0,
738
739     initialize : function(color, ambience) {
740         try {
741             this.color = color;
742             this.ambience = ambience;
743             randomException();
744         } catch(e) { }
745     }
746 }
747 /* Fake a Flog.* namespace */
748 if(typeof(Flog) == 'undefined') var Flog = {};
749 if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {};
750
751 Flog.RayTracer.Engine = Class.create();
752
753 Flog.RayTracer.Engine.prototype = {
754     canvas: null, /* 2d context we can render to */
755
756     initialize: function(options){
757         try {
758             this.options = Object.extend({
759                 canvasHeight: 100,
760                 canvasWidth: 100,
761                 pixelWidth: 2,
762                 pixelHeight: 2,
763                 renderDiffuse: false,
764                 renderShadows: false,
765                 renderHighlights: false,
766                 renderReflections: false,
767                 rayDepth: 2
768             }, options || {});
769
770             this.options.canvasHeight /= this.options.pixelHeight;
771             this.options.canvasWidth /= this.options.pixelWidth;
772
773             randomException();
774         } catch(e) { }
775
776         /* TODO: dynamically include other scripts */
777     },
778
779     setPixel: function(x, y, color){
780         try {
781             var pxW, pxH;
782             pxW = this.options.pixelWidth;
783             pxH = this.options.pixelHeight;
784
785             if (this.canvas) {
786                 this.canvas.fillStyle = color.toString();
787                 this.canvas.fillRect (x * pxW, y * pxH, pxW, pxH);
788             } else {
789                 if (x ===  y) {
790                     checkNumber += color.brightness();
791                 }
792                 // print(x * pxW, y * pxH, pxW, pxH);
793             }
794
795             randomException();
796         } catch(e) { }
797     },
798
799     renderScene: function(scene, canvas){
800         try {
801             checkNumber = 0;
802             /* Get canvas */
803             if (canvas) {
804                 this.canvas = canvas.getContext("2d");
805             } else {
806                 this.canvas = null;
807             }
808
809             var canvasHeight = this.options.canvasHeight;
810             var canvasWidth = this.options.canvasWidth;
811
812             for(var y=0; y < canvasHeight; y++){
813                 for(var x=0; x < canvasWidth; x++){
814                     try {
815                         var yp = y * 1.0 / canvasHeight * 2 - 1;
816                         var xp = x * 1.0 / canvasWidth * 2 - 1;
817
818                         var ray = scene.camera.getRay(xp, yp);
819
820                         var color = this.getPixelColor(ray, scene);
821
822                         this.setPixel(x, y, color);
823
824                         randomException();
825                     } catch(e) { }
826                 }
827             }
828         } catch(e) { }
829         if (checkNumber !== 2321) {
830             throw new Error("Scene rendered incorrectly");
831         }
832     },
833
834     getPixelColor: function(ray, scene){
835         try {
836             var info = this.testIntersection(ray, scene, null);
837             if(info.isHit){
838                 var color = this.rayTrace(info, ray, scene, 0);
839                 return color;
840             }
841             return scene.background.color;
842         } catch(e) { }
843     },
844
845     testIntersection: function(ray, scene, exclude){
846         try {
847             var hits = 0;
848             var best = new Flog.RayTracer.IntersectionInfo();
849             best.distance = 2000;
850
851             for(var i=0; i<scene.shapes.length; i++){
852                 try {
853                     var shape = scene.shapes[i];
854
855                     if(shape != exclude){
856                         var info = shape.intersect(ray);
857                         if(info.isHit && info.distance >= 0 && info.distance < best.distance){
858                             best = info;
859                             hits++;
860                         }
861                     }
862
863                     randomException();
864                 } catch(e) { }
865             }
866             best.hitCount = hits;
867
868             randomException();
869         } catch(e) { }
870         return best;
871     },
872
873     getReflectionRay: function(P,N,V){
874         try {
875             var c1 = -N.dot(V);
876             var R1 = Flog.RayTracer.Vector.prototype.add(
877                     Flog.RayTracer.Vector.prototype.multiplyScalar(N, 2*c1),
878                     V
879                     );
880
881             randomException();
882         } catch(e) { }
883         return new Flog.RayTracer.Ray(P, R1);
884     },
885
886     rayTrace: function(info, ray, scene, depth){
887         // Calc ambient
888         try {
889             var color = Flog.RayTracer.Color.prototype.multiplyScalar(info.color, scene.background.ambience);
890             var oldColor = color;
891             var shininess = Math.pow(10, info.shape.material.gloss + 1);
892
893             for(var i=0; i<scene.lights.length; i++){
894                 try {
895                     var light = scene.lights[i];
896
897                     // Calc diffuse lighting
898                     var v = Flog.RayTracer.Vector.prototype.subtract(
899                             light.position,
900                             info.position
901                             ).normalize();
902
903                     if(this.options.renderDiffuse){
904                         var L = v.dot(info.normal);
905                         if(L > 0.0){
906                             color = Flog.RayTracer.Color.prototype.add(
907                                     color,
908                                     Flog.RayTracer.Color.prototype.multiply(
909                                         info.color,
910                                         Flog.RayTracer.Color.prototype.multiplyScalar(
911                                             light.color,
912                                             L
913                                             )
914                                         )
915                                     );
916                         }
917                     }
918
919                     randomException();
920                 } catch(e) { }
921
922                 try {
923                     // The greater the depth the more accurate the colours, but
924                     // this is exponentially (!) expensive
925                     if(depth <= this.options.rayDepth){
926                         // calculate reflection ray
927                         if(this.options.renderReflections && info.shape.material.reflection > 0)
928                         {
929                             var reflectionRay = this.getReflectionRay(info.position, info.normal, ray.direction);
930                             var refl = this.testIntersection(reflectionRay, scene, info.shape);
931
932                             if (refl.isHit && refl.distance > 0){
933                                 refl.color = this.rayTrace(refl, reflectionRay, scene, depth + 1);
934                             } else {
935                                 refl.color = scene.background.color;
936                             }
937
938                             color = Flog.RayTracer.Color.prototype.blend(
939                                     color,
940                                     refl.color,
941                                     info.shape.material.reflection
942                                     );
943                         }
944
945                         // Refraction
946                         /* TODO */
947                     }
948                     randomException();
949                 }  catch(e) { }
950
951                 /* Render shadows and highlights */
952
953                 var shadowInfo = new Flog.RayTracer.IntersectionInfo();
954
955                 if(this.options.renderShadows){
956                     var shadowRay = new Flog.RayTracer.Ray(info.position, v);
957
958                     shadowInfo = this.testIntersection(shadowRay, scene, info.shape);
959                     if(shadowInfo.isHit && shadowInfo.shape != info.shape /*&& shadowInfo.shape.type != 'PLANE'*/){
960                         var vA = Flog.RayTracer.Color.prototype.multiplyScalar(color, 0.5);
961                         var dB = (0.5 * Math.pow(shadowInfo.shape.material.transparency, 0.5));
962                         color = Flog.RayTracer.Color.prototype.addScalar(vA,dB);
963                     }
964                 }
965
966                 try {
967                     // Phong specular highlights
968                     if(this.options.renderHighlights && !shadowInfo.isHit && info.shape.material.gloss > 0){
969                         var Lv = Flog.RayTracer.Vector.prototype.subtract(
970                                 info.shape.position,
971                                 light.position
972                                 ).normalize();
973
974                         var E = Flog.RayTracer.Vector.prototype.subtract(
975                                 scene.camera.position,
976                                 info.shape.position
977                                 ).normalize();
978
979                         var H = Flog.RayTracer.Vector.prototype.subtract(
980                                 E,
981                                 Lv
982                                 ).normalize();
983
984                         var glossWeight = Math.pow(Math.max(info.normal.dot(H), 0), shininess);
985                         color = Flog.RayTracer.Color.prototype.add(
986                                 Flog.RayTracer.Color.prototype.multiplyScalar(light.color, glossWeight),
987                                 color
988                                 );
989                     }
990                     randomException();
991                 } catch(e) { }
992             }
993             color.limit();
994
995             randomException();
996         } catch(e) { }
997         return color;
998     }
999 };
1000
1001
1002 function renderScene(){
1003     try {
1004         var scene = new Flog.RayTracer.Scene();
1005
1006         scene.camera = new Flog.RayTracer.Camera(
1007                 new Flog.RayTracer.Vector(0, 0, -15),
1008                 new Flog.RayTracer.Vector(-0.2, 0, 5),
1009                 new Flog.RayTracer.Vector(0, 1, 0)
1010                 );
1011
1012         scene.background = new Flog.RayTracer.Background(
1013                 new Flog.RayTracer.Color(0.5, 0.5, 0.5),
1014                 0.4
1015                 );
1016
1017         var sphere = new Flog.RayTracer.Shape.Sphere(
1018                 new Flog.RayTracer.Vector(-1.5, 1.5, 2),
1019                 1.5,
1020                 new Flog.RayTracer.Material.Solid(
1021                     new Flog.RayTracer.Color(0,0.5,0.5),
1022                     0.3,
1023                     0.0,
1024                     0.0,
1025                     2.0
1026                     )
1027                 );
1028
1029         var sphere1 = new Flog.RayTracer.Shape.Sphere(
1030                 new Flog.RayTracer.Vector(1, 0.25, 1),
1031                 0.5,
1032                 new Flog.RayTracer.Material.Solid(
1033                     new Flog.RayTracer.Color(0.9,0.9,0.9),
1034                     0.1,
1035                     0.0,
1036                     0.0,
1037                     1.5
1038                     )
1039                 );
1040
1041         var plane = new Flog.RayTracer.Shape.Plane(
1042                 new Flog.RayTracer.Vector(0.1, 0.9, -0.5).normalize(),
1043                 1.2,
1044                 new Flog.RayTracer.Material.Chessboard(
1045                     new Flog.RayTracer.Color(1,1,1),
1046                     new Flog.RayTracer.Color(0,0,0),
1047                     0.2,
1048                     0.0,
1049                     1.0,
1050                     0.7
1051                     )
1052                 );
1053
1054         scene.shapes.push(plane);
1055         scene.shapes.push(sphere);
1056         scene.shapes.push(sphere1);
1057
1058         var light = new Flog.RayTracer.Light(
1059                 new Flog.RayTracer.Vector(5, 10, -1),
1060                 new Flog.RayTracer.Color(0.8, 0.8, 0.8)
1061                 );
1062
1063         var light1 = new Flog.RayTracer.Light(
1064                 new Flog.RayTracer.Vector(-3, 5, -15),
1065                 new Flog.RayTracer.Color(0.8, 0.8, 0.8),
1066                 100
1067                 );
1068
1069         scene.lights.push(light);
1070         scene.lights.push(light1);
1071
1072         var imageWidth = 100; // $F('imageWidth');
1073         var imageHeight = 100; // $F('imageHeight');
1074         var pixelSize = "5,5".split(','); //  $F('pixelSize').split(',');
1075         var renderDiffuse = true; // $F('renderDiffuse');
1076         var renderShadows = true; // $F('renderShadows');
1077         var renderHighlights = true; // $F('renderHighlights');
1078         var renderReflections = true; // $F('renderReflections');
1079         var rayDepth = 2;//$F('rayDepth');
1080
1081         var raytracer = new Flog.RayTracer.Engine(
1082                 {
1083                     canvasWidth: imageWidth,
1084                     canvasHeight: imageHeight,
1085                     pixelWidth: pixelSize[0],
1086                     pixelHeight: pixelSize[1],
1087                     "renderDiffuse": renderDiffuse,
1088                     "renderHighlights": renderHighlights,
1089                     "renderShadows": renderShadows,
1090                     "renderReflections": renderReflections,
1091                     "rayDepth": rayDepth
1092                 }
1093                 );
1094
1095         raytracer.renderScene(scene, null, 0);
1096         randomException();
1097     } catch(e) { }
1098 }
1099
1100 for (var i = 0; i < 6; ++i)
1101     renderScene();