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/RDGE/src/core/script/math/vec4.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/RDGE/src/core/script/math/vec4.js')
-rw-r--r-- | js/helper-classes/RDGE/src/core/script/math/vec4.js | 258 |
1 files changed, 258 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/math/vec4.js b/js/helper-classes/RDGE/src/core/script/math/vec4.js new file mode 100644 index 00000000..3a0de375 --- /dev/null +++ b/js/helper-classes/RDGE/src/core/script/math/vec4.js | |||
@@ -0,0 +1,258 @@ | |||
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 | * vec4 = {} | ||
9 | * This library contains functions for operating on 4D vectors. Any JS array | ||
10 | * containing at least 4 numeric components can represent a 4D vector. | ||
11 | * | ||
12 | * For example, all of these are valid vec4 declarations: | ||
13 | * var a = [0, 0, 0, 1]; | ||
14 | * var b = vec3.identity(); | ||
15 | * var c = vec3.zero(); | ||
16 | * | ||
17 | */ | ||
18 | vec4 = {}; | ||
19 | |||
20 | /** | ||
21 | * vec4.string | ||
22 | */ | ||
23 | vec4.string = function(v) { | ||
24 | return "{ " + v[0] + ", " + v[1] + ", " + v[2] + ", " + v[3] + " }"; | ||
25 | } | ||
26 | |||
27 | /** | ||
28 | * vec4.verify | ||
29 | */ | ||
30 | vec4.verify = function(v) { | ||
31 | if (v == undefined || v.length == undefined || v.length < 4) { | ||
32 | return false; | ||
33 | } | ||
34 | |||
35 | if (typeof (v[0]) != "number" || typeof (v[1]) != "number" || typeof (v[2]) != "number" || typeof (v[3]) != "number") { | ||
36 | return false; | ||
37 | } | ||
38 | |||
39 | return true; | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * vec4.inplace_copy | ||
44 | */ | ||
45 | vec4.inplace_copy = function(dst, src) { | ||
46 | dst[0] = src[0]; | ||
47 | dst[1] = src[1]; | ||
48 | dst[2] = src[2]; | ||
49 | dst[3] = src[3]; | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * vec4.copy | ||
54 | */ | ||
55 | vec4.copy = function(v) { | ||
56 | if( v.length == undefined ) { | ||
57 | return [ v, v, v, v ]; | ||
58 | } | ||
59 | |||
60 | if( v.length == 3 ) { | ||
61 | return [ v[0], v[1], v[2], 1.0 ]; | ||
62 | } | ||
63 | |||
64 | return [ v[0], v[1], v[2], v[3] ]; | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * vec4.zero | ||
69 | */ | ||
70 | vec4.zero = function() { | ||
71 | return [ 0.0, 0.0, 0.0, 0.0 ]; | ||
72 | } | ||
73 | |||
74 | /** | ||
75 | * vec4.identity | ||
76 | */ | ||
77 | vec4.identity = function() { | ||
78 | return [ 0.0, 0.0, 0.0, 1.0 ]; | ||
79 | } | ||
80 | |||
81 | /** | ||
82 | * vec4.up | ||
83 | */ | ||
84 | vec4.up = function() { | ||
85 | return [ 0.0, 1.0, 0.0, 0.0 ]; | ||
86 | } | ||
87 | |||
88 | /** | ||
89 | * vec4.forward | ||
90 | */ | ||
91 | vec4.forward = function() { | ||
92 | return [ 0.0, 0.0, 1.0, 0.0 ]; | ||
93 | } | ||
94 | |||
95 | /** | ||
96 | * vec4.right | ||
97 | */ | ||
98 | vec4.right = function() { | ||
99 | return [ 1.0, 0.0, 0.0, 0.0 ]; | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * vec4.random | ||
104 | */ | ||
105 | vec4.random = function(min, max) { | ||
106 | return [ min[0] + (max[0] - min[0]) * Math.random(), | ||
107 | min[1] + (max[1] - min[1]) * Math.random(), | ||
108 | min[2] + (max[2] - min[2]) * Math.random(), | ||
109 | min[3] + (max[3] - min[3]) * Math.random()]; | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * vec4.add | ||
114 | */ | ||
115 | vec4.add = function(a, b) { | ||
116 | return [ a[0] + b[0], | ||
117 | a[1] + b[1], | ||
118 | a[2] + b[2], | ||
119 | a[3] + b[3] ]; | ||
120 | } | ||
121 | |||
122 | /** | ||
123 | * vec4.sub | ||
124 | */ | ||
125 | vec4.sub = function(a, b) { | ||
126 | return [ a[0] - b[0], | ||
127 | a[1] - b[1], | ||
128 | a[2] - b[2], | ||
129 | a[3] - b[3] ]; | ||
130 | } | ||
131 | |||
132 | /** | ||
133 | * vec4.mul | ||
134 | */ | ||
135 | vec4.mul = function(a, b) { | ||
136 | return [ a[0] * b[0], | ||
137 | a[1] * b[1], | ||
138 | a[2] * b[2], | ||
139 | a[3] * b[3] ]; | ||
140 | } | ||
141 | |||
142 | /** | ||
143 | * vec4.addMul | ||
144 | */ | ||
145 | vec4.addMul = function(a, b, s) { | ||
146 | if (s.length != undefined && s.length >= 4) { | ||
147 | return [a[0] + b[0] * s[0], a[1] + b[1] * s[1], a[2] + b[2] * s[2], a[3] + b[3] * s[3]]; | ||
148 | } else { | ||
149 | return [a[0] + b[0] * s, a[1] + b[1] * s, a[2] + b[2] * s, a[3] + b[3] * s]; | ||
150 | } | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * vec4.scale | ||
155 | */ | ||
156 | vec4.scale = function(v, s) { | ||
157 | if (s.length != undefined && s.length >= 4) { | ||
158 | return [v[0] * s[0], v[1] * s[1], v[2] * s[2], v[3] * s[3]]; | ||
159 | } else { | ||
160 | return [v[0] * s, v[1] * s, v[2] * s, v[3] * s]; | ||
161 | } | ||
162 | } | ||
163 | |||
164 | /** | ||
165 | * vec4.negate | ||
166 | */ | ||
167 | vec4.negate = function(v) { | ||
168 | return [ -v[0], -v[1], -v[2], -v[3] ]; | ||
169 | } | ||
170 | |||
171 | /** | ||
172 | * vec4.dot | ||
173 | */ | ||
174 | vec4.dot = function(a, b) { | ||
175 | return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; | ||
176 | } | ||
177 | |||
178 | /** | ||
179 | * vec4.normalize | ||
180 | */ | ||
181 | vec4.normalize = function(v) { | ||
182 | var l = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]); | ||
183 | if (Math.abs(1.0 - l) > 0.0001) { | ||
184 | var ool = 1.0 / l; | ||
185 | return [v[0] * ool, v[1] * ool, v[2] * ool, v[3] * ool]; | ||
186 | } | ||
187 | return v; | ||
188 | } | ||
189 | |||
190 | /** | ||
191 | * vec4.lengthSq | ||
192 | */ | ||
193 | vec4.lengthSq = function( v ) { | ||
194 | return v[0] * v[0] + | ||
195 | v[1] * v[1] + | ||
196 | v[2] * v[2] + | ||
197 | v[3] * v[3]; | ||
198 | } | ||
199 | |||
200 | /** | ||
201 | * vec4.length | ||
202 | */ | ||
203 | vec4.length = function( v ) { | ||
204 | return Math.sqrt( v[0] * v[0] + | ||
205 | v[1] * v[1] + | ||
206 | v[2] * v[2] + | ||
207 | v[3] * v[3] ); | ||
208 | } | ||
209 | |||
210 | /** | ||
211 | * vec4.abs | ||
212 | */ | ||
213 | vec4.abs = function(v) { | ||
214 | return [Math.abs(v[0]), Math.abs(v[1]), Math.abs(v[2]), Math.abs(v[3])]; | ||
215 | } | ||
216 | |||
217 | /** | ||
218 | * vec4.min | ||
219 | */ | ||
220 | vec4.min = function(a, b) { | ||
221 | return [Math.min(a[0], b[0]), Math.min(a[1], b[1]), Math.min(a[2], b[2]), Math.min(a[3], b[3])]; | ||