diff options
author | Pierre Frisch | 2011-12-22 07:25:50 -0800 |
---|---|---|
committer | Valerio Virgillito | 2012-01-27 11:18:17 -0800 |
commit | b89a7ee8b956c96a1dcee995ea840feddc5d4b27 (patch) | |
tree | 0f3136ab0ecdbbbed6a83576581af0a53124d6f1 /js/helper-classes/3D/math-utils.js | |
parent | 2401f05d1f4b94d45e4568b81fc73e67b969d980 (diff) | |
download | ninja-b89a7ee8b956c96a1dcee995ea840feddc5d4b27.tar.gz |
First commit of Ninja to ninja-internal
Signed-off-by: Valerio Virgillito <rmwh84@motorola.com>
Diffstat (limited to 'js/helper-classes/3D/math-utils.js')
-rw-r--r-- | js/helper-classes/3D/math-utils.js | 1104 |
1 files changed, 1104 insertions, 0 deletions
diff --git a/js/helper-classes/3D/math-utils.js b/js/helper-classes/3D/math-utils.js new file mode 100644 index 00000000..49c77c41 --- /dev/null +++ b/js/helper-classes/3D/math-utils.js | |||
@@ -0,0 +1,1104 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | |||
7 | /////////////////////////////////////////////////////////////////////// | ||
8 | // Class Utils | ||
9 | // Math Utility functions | ||
10 | /////////////////////////////////////////////////////////////////////// | ||
11 | var VecUtils = require("js/helper-classes/3D/vec-utils").VecUtils; | ||
12 | |||
13 | var MathUtilsClass = exports.MathUtilsClass = Object.create(Object.prototype, { | ||
14 | /////////////////////////////////////////////////////////////////////// | ||
15 | // Instance variables | ||
16 | /////////////////////////////////////////////////////////////////////// | ||
17 | // VecUtils: { value: null, writable: true }, | ||
18 | |||
19 | EPSILON: { value: 1.e-5, writable: true }, | ||
20 | |||
21 | // these are used in containment tests | ||
22 | INSIDE: { value: -1, writable: true }, | ||
23 | ON: { value: 0, writable: true }, | ||
24 | OUTSIDE: { value: 1, writable: true }, | ||
25 | |||
26 | PI2: { value: 2*Math.PI, writable: true }, | ||
27 | RAD_TO_DEG: { value: 180/Math.PI, writable: true }, | ||
28 | DEG_TO_RAD: { value: Math.PI/180, writable: true }, | ||
29 | |||
30 | /////////////////////////////////////////////////////////////////////// | ||
31 | // Property accessors | ||
32 | /////////////////////////////////////////////////////////////////////// | ||
33 | |||
34 | /////////////////////////////////////////////////////////////////////// | ||
35 | // Vector Methods | ||
36 | /////////////////////////////////////////////////////////////////////// | ||
37 | |||
38 | vecIntersectPlaneForParam: { | ||
39 | value: function( pt0, vec, plane ) | ||
40 | { | ||
41 | // declare the variable to return - undefined when there is no solution | ||
42 | var param; | ||
43 | |||
44 | var a = plane[0], b = plane[1], c = plane[2], d = plane[3]; | ||
45 | var dx = vec[0], dy = vec[1], dz = vec[2]; | ||
46 | var x0 = pt0[0], y0 = pt0[1], z0 = pt0[2]; | ||
47 | |||
48 | var numerator = -(a*x0 + b*y0 + c*z0 + d); | ||
49 | var denominator = a*dx + b*dy + c*dz; | ||
50 | |||
51 | var rtnPt; | ||
52 | if (this.fpSign(denominator) != 0) | ||
53 | param = numerator / denominator; | ||
54 | |||
55 | return param; | ||
56 | } | ||
57 | }, | ||
58 | |||
59 | vecMag3: { | ||
60 | value: function( vec ) | ||
61 | { | ||
62 | if (vec.length < 3) return; | ||
63 | var mag = vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]; | ||
64 | mag = Math.sqrt( mag ); | ||
65 | return mag; | ||
66 | } | ||
67 | }, | ||
68 | |||
69 | vecMag: { | ||
70 | value: function( dimen, vec ) | ||
71 | { | ||
72 | var sum = 0.0; | ||
73 | for (var i=0; i<dimen; i++) | ||
74 | sum += vec[i]*vec[i]; | ||
75 | return Math.sqrt( sum ); | ||
76 | } | ||
77 | }, | ||
78 | |||
79 | vecSubtract: { | ||
80 | value: function( a, b ) | ||
81 | { | ||
82 | var rtnVec; | ||
83 | var n = a.length; | ||
84 | if (b.length < n) n = b.length; | ||
85 | if (n > 0) | ||
86 | { | ||
87 | rtnVec = Vector.create([0]); | ||
88 | for (var i=0; i<n; i++) | ||
89 | rtnVec[i] = a[i] - b[i]; | ||
90 | } | ||
91 | |||
92 | return rtnVec; | ||
93 | } | ||
94 | }, | ||
95 | |||
96 | vecAdd: { | ||
97 | value: function( a, b ) | ||
98 | { | ||
99 | var rtnVec; | ||
100 | var n = a.length; | ||
101 | if (b.length < n) n = b.length; | ||
102 | if (n > 0) | ||
103 | { | ||
104 | rtnVec = Vector.create([0]); | ||
105 | for (var i=0; i<n; i++) | ||
106 | rtnVec[i] = a[i] + b[i]; | ||
107 | } | ||
108 | |||
109 | return rtnVec; | ||
110 | } | ||
111 | }, | ||
112 | |||
113 | vecDist: { | ||
114 | value: function( a, b ) | ||
115 | { | ||
116 | var sum; | ||
117 | var n = a.length; | ||
118 | if (b.length < n) n = b.length; | ||
119 | if (n > 0) | ||
120 | { | ||
121 | var sum = 0.0; | ||
122 | for (var i=0; i<n; i++) | ||
123 | { | ||
124 | var d = a[i] - b[i]; | ||
125 | sum += d*d; | ||
126 | } | ||
127 | |||
128 | sum = Math.sqrt( sum ); | ||
129 | } | ||
130 | |||
131 | return sum; | ||
132 | } | ||
133 | }, | ||
134 | |||
135 | vecIntersectPlane: { | ||
136 | value: function( pt0, vec, plane ) | ||
137 | { | ||
138 | // declare the return point. May be undefined in ill-conditioned cases. | ||
139 | var rtnPt; | ||
140 | |||
141 | var t = this.vecIntersectPlaneForParam( pt0, vec, plane ); | ||
142 | if (t != undefined) | ||
143 | { | ||
144 | var x0 = pt0[0], y0 = pt0[1], z0 = pt0[2]; | ||
145 | var dx = vec[0], dy = vec[1], dz = vec[2]; | ||
146 | rtnPt = Vector.create( [x0 + t*dx, | ||
147 | y0 + t*dy, | ||
148 | z0 + t*dz] ); | ||
149 | } | ||
150 | |||
151 | return rtnPt; | ||
152 | } | ||
153 | }, | ||
154 | |||
155 | getPointOnPlane: { | ||
156 | value: function( plane ) | ||
157 | { | ||
158 | // abreviate the plane equation | ||
159 | var a = plane[0], b = plane[1], c = plane[2], d = plane[3]; | ||
160 | |||
161 | var x = 0.0, y = 0.0, z = 0.0; | ||
162 | if ( Math.abs(plane[0]) > Math.abs(plane[1]) ) | ||
163 | { | ||
164 | if ( Math.abs(plane[0]) > Math.abs(plane[2]) ) | ||
165 | x = -d/a; | ||
166 | else | ||
167 | z = -d/c; | ||
168 | } | ||
169 | else | ||
170 | { | ||
171 | if (Math.abs(plane[1]) > Math.abs(plane[2]) ) | ||
172 | y = -d/b; | ||
173 | else | ||
174 | z = -d/c; | ||
175 | } | ||
176 | |||
177 | // get the point on the plane | ||
178 | return [x, y, z]; | ||
179 | } | ||
180 | }, | ||
181 | |||
182 | transformPlane: { | ||
183 | value: function( plane, mat ) | ||
184 | { | ||
185 | // we will project a point down one of the coordinate axes to find a point on the plane | ||
186 | // that point and the normal to the plane will be transformed by the matrix, and the 'd' | ||
187 | // component of the plane equation will be reset using the new normal and point. | ||
188 | |||
189 | // find a point on the plane | ||
190 | var ptOnPlane = this.getPointOnPlane(plane); | ||
191 | |||
192 | ptOnPlane[3] = 1.0; // 4 dimen so we can transform it | ||
193 | |||
194 | // transform the point | ||
195 | //ptOnPlane = mat.multiply( ptOnPlane ); | ||
196 | ptOnPlane = glmat4.multiplyVec3(mat, ptOnPlane, []); | ||
197 | plane = this.transformVector( plane, mat ); | ||
198 | plane[3] = -this.dot3(plane, ptOnPlane ); | ||
199 | |||
200 | return plane; | ||
201 | } | ||
202 | }, | ||
203 | |||
204 | transformHomogeneousPoint: { | ||
205 | value: function( srcPt, mat ) | ||
206 | { | ||
207 | var pt = srcPt.slice(0); | ||
208 | this.makeDimension4( pt ); | ||
209 | var x = VecUtils.vecDot(4, pt, [mat[0], mat[4], mat[ 8], mat[12]] ), | ||
210 | y = VecUtils.vecDot(4, pt, [mat[1], mat[5], mat[ 9], mat[13]] ), | ||
211 | z = VecUtils.vecDot(4, pt, [mat[2], mat[6], mat[10], mat[14]] ), | ||
212 | w = VecUtils.vecDot(4, pt, [mat[3], mat[7], mat[11], mat[15]] ); | ||
213 | |||
214 | return [x, y, z, w]; | ||
215 | } | ||
216 | }, | ||
217 | |||
218 | applyHomogeneousCoordinate: { | ||
219 | value: function( hPt ) | ||
220 | { | ||
221 | var w = hPt[3]; | ||
222 | hPt[0] /= w; | ||
223 | hPt[1] /= w; | ||
224 | hPt[2] /= w; | ||
225 | hPt[3] = 1; | ||
226 | |||
227 | return hPt; | ||
228 | } | ||
229 | }, | ||
230 | |||
231 | transformAndDivideHomogeneousPoint: { | ||
232 | value: function( pt, mat ) | ||
233 | { | ||
234 | return this.applyHomogeneousCoordinate( this.transformHomogeneousPoint(pt, mat) ); | ||
235 | } | ||