Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / demos / webkit / resources / J3DIMath-unit.html
1 <!DOCTYPE html>
2 <!--
3 /*
4 ** Copyright (c) 2014 The Khronos Group Inc.
5 **
6 ** Permission is hereby granted, free of charge, to any person obtaining a
7 ** copy of this software and/or associated documentation files (the
8 ** "Materials"), to deal in the Materials without restriction, including
9 ** without limitation the rights to use, copy, modify, merge, publish,
10 ** distribute, sublicense, and/or sell copies of the Materials, and to
11 ** permit persons to whom the Materials are furnished to do so, subject to
12 ** the following conditions:
13 **
14 ** The above copyright notice and this permission notice shall be included
15 ** in all copies or substantial portions of the Materials.
16 **
17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
24 */
25  -->
26 <html>
27 <head>
28 <title>J3DIMath unit test</title>
29 <script src="J3DIMath.js"></script>
30 <script>
31 var log = function(msg) {
32     var p = document.createElement('p');
33     if (msg.indexOf('FAIL') == 0) {
34         p.style.color = '#900';
35     } else if (msg.indexOf('PASS') == 0) {
36         p.style.color = '#090';
37     }
38     p.textContent = msg;
39     document.body.appendChild(p);
40     console.log(msg);
41 };
42
43 var check = function(result, msg) {
44     if (result) {
45         log('PASS ' + msg);
46     } else {
47         log('FAIL ' + msg);
48     }
49 };
50
51 var testMultiplication = function() {
52     var A = new J3DIMatrix4();
53     var B = new J3DIMatrix4();
54     A.$matrix.m21 = 3; // 2nd column, 1st row
55     B.$matrix.m12 = 2; // 1st column, 2nd row
56     A.multiply(B); // multiply from the right
57     // [ 1 3 0 0 ]   [ 1 0 0 0 ]   [ 7 3 0 0 ]
58     // | 0 1 0 0 | x | 2 1 0 0 | = | 2 1 0 0 |
59     // | 0 0 1 0 |   | 0 0 1 0 |   | 0 0 1 0 |
60     // [ 0 0 0 1 ]   [ 0 0 0 1 ]   [ 0 0 0 1 ]
61     check(A.$matrix.m11 == 7 && A.$matrix.m12 == 2 && A.$matrix.m21 == 3 && A.$matrix.m22 == 1,
62           'Matrix multiplication order is as specified');
63 };
64
65 var testFloatArrayIO = function() {
66     // Column major order
67     var array = [ 7, 2, 0, 0,
68                   3, 1, 0, 0,
69                   0, 0, 1, 0,
70                   0, 0, 0, 1 ];
71     var A = new J3DIMatrix4(array);
72     check(A.$matrix.m11 == 7 && A.$matrix.m12 == 2 && A.$matrix.m21 == 3 && A.$matrix.m22 == 1,
73           'Matrix can be constructed from column-major array');
74     var outArray = A.getAsArray();
75     var match = true;
76     for (var i = 0; i < array.length; ++i) {
77         match = match && array[i] == outArray[i];
78     }
79     check(match, 'Matrix array out matches array in');
80 };
81
82 var testCrossProduct = function() {
83     var a = new J3DIVector3(1, 5, 11);
84     var b = new J3DIVector3(3, 7, 13);
85     a.cross(b);
86     var arr = a.getAsArray();
87     check(arr[0] == 5 * 13 - 11 * 7, 'Vector cross product x is correct');
88     check(arr[1] == 11 * 3 - 1 * 13, 'Vector cross product y is correct');
89     check(arr[2] == 1 * 7 - 5 * 3, 'Vector cross product z is correct');
90 };
91
92 var testLinearAlgebra = function(useCSSMatrix) {
93     J3DIHasCSSMatrix = useCSSMatrix;
94     log('Using CSSMatrix: ' + J3DIHasCSSMatrix);
95
96     testMultiplication();
97     testFloatArrayIO();
98     testCrossProduct();
99 };
100
101 var test = function() {
102     testLinearAlgebra(false);
103     testLinearAlgebra(true);
104 };
105 </script>
106 </head>
107 <body onload="test()">
108 </body>
109 </html>