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/vec-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/vec-utils.js')
-rw-r--r-- | js/helper-classes/3D/vec-utils.js | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/js/helper-classes/3D/vec-utils.js b/js/helper-classes/3D/vec-utils.js new file mode 100644 index 00000000..ceff4f64 --- /dev/null +++ b/js/helper-classes/3D/vec-utils.js | |||
@@ -0,0 +1,199 @@ | |||
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 | // Vector Utility functions | ||
10 | /////////////////////////////////////////////////////////////////////// | ||
11 | var VecUtils = exports.VecUtils = Object.create(Object.prototype, | ||
12 | { | ||
13 | /////////////////////////////////////////////////////////////////////// | ||
14 | // Instance variables | ||
15 | /////////////////////////////////////////////////////////////////////// | ||
16 | |||
17 | /////////////////////////////////////////////////////////////////////// | ||
18 | // Property accessors | ||
19 | /////////////////////////////////////////////////////////////////////// | ||
20 | |||
21 | /////////////////////////////////////////////////////////////////////// | ||
22 | // Vector Methods | ||
23 | /////////////////////////////////////////////////////////////////////// | ||
24 | vecNormalize: { | ||
25 | value: function(dimen, vec, lenToMake) { | ||
26 | var rtnVec; | ||
27 | try | ||
28 | { | ||
29 | var len = 1.0 | ||
30 | var nArgs = arguments.length; | ||
31 | if (nArgs > 2) | ||
32 | len = lenToMake; | ||
33 | |||
34 | var sum = 0.0; | ||
35 | for (var i=0; i<dimen; i++) | ||
36 | sum += vec[i]*vec[i]; | ||
37 | sum = Math.sqrt( sum ); | ||
38 | if (MathUtils.fpSign(sum) != 0) | ||
39 | { | ||
40 | var scale = len/sum; | ||
41 | rtnVec = Vector.create( [0] ); | ||
42 | for (var i=0; i<dimen; i++) | ||
43 | rtnVec[i] = vec[i]*scale; | ||
44 | } | ||
45 | else | ||
46 | { | ||
47 | rtnVec = Vector.create( [0] ); | ||
48 | for (var i=1; i<dimen; i++) | ||
49 | rtnVec[i] = 0; | ||
50 | } | ||
51 | } | ||
52 | catch(e) | ||
53 | { | ||
54 | throw new Error( "exception in vecNormalize: " + e); | ||
55 | } | ||
56 | |||
57 | return rtnVec; | ||
58 | } | ||
59 | }, | ||
60 | |||
61 | vecAdd: { | ||
62 | value: function( dimen, a, b ) { | ||
63 | |||
64 | var rtnVec; | ||
65 | if ((a.length < dimen) || (b.length < dimen)) | ||
66 | { | ||
67 | throw new Error( "dimension error in VecUtils.vecAdd" ); | ||
68 | } | ||
69 | |||
70 | rtnVec = Vector.create([0]); | ||
71 | for (var i=0; i<dimen; i++) | ||
72 | rtnVec[i] = a[i] + b[i]; | ||
73 | |||
74 | return rtnVec; | ||
75 | } | ||
76 | }, | ||
77 | |||
78 | |||
79 | vecSubtract : { | ||
80 | value: function( dimen, a, b ) { | ||
81 | var rtnVec; | ||
82 | if ((a.length < dimen) || (b.length < dimen)) | ||
83 | { | ||
84 | throw new Error( "dimension error in VecUtils.vecSubtract" ); | ||
85 | } | ||
86 | |||
87 | rtnVec = Vector.create([0]); | ||
88 | for (var i=0; i<dimen; i++) | ||
89 | rtnVec[i] = a[i] - b[i]; | ||
90 | |||
91 | return rtnVec; | ||
92 | } | ||
93 | }, | ||
94 | |||
95 | vecDist : { | ||
96 | value: function( dimen, a, b ) { | ||
97 | var sum; | ||
98 | |||
99 | if ((a.length < dimen) || (b.length < dimen)) | ||
100 | { | ||
101 | throw new Error( "dimension error in VecUtils.vecSubtract" ); | ||
102 | } | ||
103 | |||
104 | var sum = 0.0; | ||
105 | for (var i=0; i<dimen; i++) | ||
106 | { | ||
107 | var d = a[i] - b[i]; | ||
108 | sum += d*d; | ||
109 | } | ||
110 | sum = Math.sqrt( sum ); | ||
111 | |||
112 | return sum; | ||
113 | } | ||
114 | }, | ||
115 | |||
116 | vecDot : { | ||
117 | value: function( dimen, v0, v1 ) { | ||
118 | if ((v0.length < dimen) || (v1.length < dimen)) | ||
119 | { | ||
120 | throw new Error( "dimension error in VecUtils.vecDot" ); | ||
121 | } | ||
122 | |||
123 | var sum = 0.0; | ||
124 | for (var i=0; i<dimen; i++) | ||
125 | sum += v0[i] * v1[i]; | ||
126 | |||
127 | return sum; | ||
128 | } | ||
129 | }, | ||
130 | |||
131 | |||
132 | vecCross: { | ||
133 | value: function( dimen, v0, v1 ) { | ||
134 | if ((v0.length < dimen) || (v1.length < dimen)) | ||
135 | { | ||
136 | throw new Error( "dimension error in VecUtils.vecCross" ); | ||
137 | } | ||
138 | |||
139 | var rtnVal; | ||
140 | if (dimen == 2) | ||
141 | rtnVal = v0[0]*v1[1] - v0[1]*v1[0]; | ||
142 | else if (dimen == 3) | ||
143 | { | ||
144 | rtnVal = Vector.create( [ | ||
145 | v0[1]*v1[2] - v0[2]*v1[1], | ||
146 | -v0[0]*v1[2] + v0[2]*v1[0], | ||
147 | v0[0]*v1[1] - v0[1]*v1[0] | ||
148 | ] ); | ||
149 | } | ||
150 | else | ||
151 | throw new Error( "VecUtils.vecCross supports only 2 or 3 dimensional vectors: " + dimen ); | ||
152 | |||
153 | return rtnVal; | ||
154 | } | ||
155 | }, | ||
156 | |||
157 | |||
158 | vecInterpolate: { | ||
159 | value: function( dimen, a, b, t ) { | ||
160 | if ((a.length < dimen) || (b.length < dimen)) | ||
161 | { | ||
162 | throw new Error( "dimension error in VecUtils.vecSubtract" ); | ||
163 | } | ||
164 | |||
165 | var rtnVec = Vector.create([0]); | ||
166 | for (var i=0; i<dimen; i++) | ||
167 | rtnVec[i] = a[i] + t*(b[i] - a[i]); | ||
168 | |||
169 | return rtnVec; | ||
170 | } | ||
171 | }, | ||
172 | |||
173 | vecMag: { | ||
174 | value: function( dimen, vec ) { | ||
175 | var sum = 0.0; | ||
176 | for (var i=0; i<dimen; i++) | ||
177 | sum += vec[i]*vec[i]; | ||
178 | return Math.sqrt( sum ); | ||
179 | } | ||
180 | }, | ||
181 | |||
182 | vecNegate: { | ||
183 | value: function( dimen, v ) { | ||
184 | for (var i=0; i<dimen; i++) | ||
185 | v[i] = -v[i]; | ||
186 | |||
187 | return v; | ||
188 | } | ||
189 | }, | ||
190 | |||
191 | vecScale: { | ||
192 | value: function(dimen, vec, scale) { | ||
193 | for (var i=0; i<dimen; i++) | ||
194 | vec[i] *= scale; | ||
195 | |||
196 | return vec; | ||
197 | } | ||
198 | } | ||
199 | }); \ No newline at end of file | ||