Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / Geometry.js
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 WebInspector.Geometry = {};
32
33 /**
34  * @type {number}
35  */
36 WebInspector.Geometry._Eps = 1e-5;
37
38 /**
39  * @constructor
40  * @param {number} x
41  * @param {number} y
42  * @param {number} z
43  */
44 WebInspector.Geometry.Vector = function(x, y, z)
45 {
46     this.x = x;
47     this.y = y;
48     this.z = z;
49 }
50
51 WebInspector.Geometry.Vector.prototype = {
52     /**
53      * @return {number}
54      */
55     length: function()
56     {
57         return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
58     },
59
60     normalize: function()
61     {
62         var length = this.length();
63         if (length <= WebInspector.Geometry._Eps)
64             return;
65
66         this.x /= length;
67         this.y /= length;
68         this.z /= length;
69     }
70 }
71
72 /**
73  * @constructor
74  * @param {number} alpha
75  * @param {number} beta
76  * @param {number} gamma
77  */
78 WebInspector.Geometry.EulerAngles = function(alpha, beta, gamma)
79 {
80     this.alpha = alpha;
81     this.beta = beta;
82     this.gamma = gamma;
83 }
84
85 /**
86  * @param {!CSSMatrix} rotationMatrix
87  * @return {!WebInspector.Geometry.EulerAngles}
88  */
89 WebInspector.Geometry.EulerAngles.fromRotationMatrix = function(rotationMatrix)
90 {
91     var beta = Math.atan2(rotationMatrix.m23, rotationMatrix.m33);
92     var gamma = Math.atan2(-rotationMatrix.m13, Math.sqrt(rotationMatrix.m11 * rotationMatrix.m11 + rotationMatrix.m12 * rotationMatrix.m12));
93     var alpha = Math.atan2(rotationMatrix.m12, rotationMatrix.m11);
94     return new WebInspector.Geometry.EulerAngles(WebInspector.Geometry.radToDeg(alpha), WebInspector.Geometry.radToDeg(beta), WebInspector.Geometry.radToDeg(gamma));
95 }
96
97 /**
98  * @param {!WebInspector.Geometry.Vector} u
99  * @param {!WebInspector.Geometry.Vector} v
100  * @return {number}
101  */
102 WebInspector.Geometry.scalarProduct = function(u, v)
103 {
104     return u.x * v.x + u.y * v.y + u.z * v.z;
105 }
106
107 /**
108  * @param {!WebInspector.Geometry.Vector} u
109  * @param {!WebInspector.Geometry.Vector} v
110  * @return {!WebInspector.Geometry.Vector}
111  */
112 WebInspector.Geometry.crossProduct = function(u, v)
113 {
114     var x = u.y * v.z - u.z * v.y;
115     var y = u.z * v.x - u.x * v.z;
116     var z = u.x * v.y - u.y * v.x;
117     return new WebInspector.Geometry.Vector(x, y, z);
118 }
119
120 /**
121  * @param {!WebInspector.Geometry.Vector} u
122  * @param {!WebInspector.Geometry.Vector} v
123  * @return {number}
124  */
125 WebInspector.Geometry.calculateAngle = function(u, v)
126 {
127     var uLength = u.length();
128     var vLength = v.length();
129     if (uLength <= WebInspector.Geometry._Eps || vLength <= WebInspector.Geometry._Eps)
130         return 0;
131     var cos = WebInspector.Geometry.scalarProduct(u, v) / uLength / vLength;
132     if (Math.abs(cos) > 1)
133         return 0;
134     return WebInspector.Geometry.radToDeg(Math.acos(cos));
135 }
136
137 /**
138  * @param {number} rad
139  * @return {number}
140  */
141 WebInspector.Geometry.radToDeg = function(rad)
142 {
143     return rad * 180 / Math.PI;
144 }
145