2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 WebInspector.Geometry = {};
36 WebInspector.Geometry._Eps = 1e-5;
44 WebInspector.Geometry.Vector = function(x, y, z)
51 WebInspector.Geometry.Vector.prototype = {
57 return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
62 var length = this.length();
63 if (length <= WebInspector.Geometry._Eps)
74 * @param {number} alpha
75 * @param {number} beta
76 * @param {number} gamma
78 WebInspector.Geometry.EulerAngles = function(alpha, beta, gamma)
86 * @param {!CSSMatrix} rotationMatrix
87 * @return {!WebInspector.Geometry.EulerAngles}
89 WebInspector.Geometry.EulerAngles.fromRotationMatrix = function(rotationMatrix)
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));
98 * @param {!WebInspector.Geometry.Vector} u
99 * @param {!WebInspector.Geometry.Vector} v
102 WebInspector.Geometry.scalarProduct = function(u, v)
104 return u.x * v.x + u.y * v.y + u.z * v.z;
108 * @param {!WebInspector.Geometry.Vector} u
109 * @param {!WebInspector.Geometry.Vector} v
110 * @return {!WebInspector.Geometry.Vector}
112 WebInspector.Geometry.crossProduct = function(u, v)
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);
121 * @param {!WebInspector.Geometry.Vector} u
122 * @param {!WebInspector.Geometry.Vector} v
125 WebInspector.Geometry.calculateAngle = function(u, v)
127 var uLength = u.length();
128 var vLength = v.length();
129 if (uLength <= WebInspector.Geometry._Eps || vLength <= WebInspector.Geometry._Eps)
131 var cos = WebInspector.Geometry.scalarProduct(u, v) / uLength / vLength;
132 if (Math.abs(cos) > 1)
134 return WebInspector.Geometry.radToDeg(Math.acos(cos));
138 * @param {number} rad
141 WebInspector.Geometry.radToDeg = function(rad)
143 return rad * 180 / Math.PI;