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