Debian Codegraph @

closure-library/closure/goog/vec/vec4d.js

raw
1 // Copyright 2013 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15
16 ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
17 // //
18 // Any edits to this file must be applied to vec4f.js by running: //
19 // swap_type.sh vec4d.js > vec4f.js //
20 // //
21 ////////////////////////// NOTE ABOUT EDITING THIS FILE ///////////////////////
22
23
24 /**
25 * @fileoverview Provides functions for operating on 4 element double (64bit)
26 * vectors.
27 *
28 * The last parameter will typically be the output object and an object
29 * can be both an input and output parameter to all methods except where
30 * noted.
31 *
32 * See the README for notes about the design and structure of the API
33 * (especially related to performance).
34 *
35 */
36 goog.provide('goog.vec.vec4d');
37 goog.provide('goog.vec.vec4d.Type');
38
39 /** @suppress {extraRequire} */
40 goog.require('goog.vec');
41
42 /** @typedef {goog.vec.Float64} */ goog.vec.vec4d.Type;
43
44
45 /**
46 * Creates a vec4d with all elements initialized to zero.
47 *
48 * @return {!goog.vec.vec4d.Type} The new vec4d.
49 */
50 goog.vec.vec4d.create = function() {
51 return new Float64Array(4);
52 };
53
54
55 /**
56 * Creates a new vec4d initialized with the value from the given array.
57 *
58 * @param {!Array<number>} vec The source 4 element array.
59 * @return {!goog.vec.vec4d.Type} The new vec4d.
60 */
61 goog.vec.vec4d.createFromArray = function(vec) {
62 var newVec = goog.vec.vec4d.create();
63 goog.vec.vec4d.setFromArray(newVec, vec);
64 return newVec;
65 };
66
67
68 /**
69 * Creates a new vec4d initialized with the supplied values.
70 *
71 * @param {number} v0 The value for element at index 0.
72 * @param {number} v1 The value for element at index 1.
73 * @param {number} v2 The value for element at index 2.
74 * @param {number} v3 The value for element at index 3.
75 * @return {!goog.vec.vec4d.Type} The new vector.
76 */
77 goog.vec.vec4d.createFromValues = function(v0, v1, v2, v3) {
78 var vec = goog.vec.vec4d.create();
79 goog.vec.vec4d.setFromValues(vec, v0, v1, v2, v3);
80 return vec;
81 };
82
83
84 /**
85 * Creates a clone of the given vec4d.
86 *
87 * @param {!goog.vec.vec4d.Type} vec The source vec4d.
88 * @return {!goog.vec.vec4d.Type} The new cloned vec4d.
89 */
90 goog.vec.vec4d.clone = function(vec) {
91 var newVec = goog.vec.vec4d.create();
92 goog.vec.vec4d.setFromVec4d(newVec, vec);
93 return newVec;
94 };
95
96
97 /**
98 * Initializes the vector with the given values.
99 *
100 * @param {!goog.vec.vec4d.Type} vec The vector to receive the values.
101 * @param {number} v0 The value for element at index 0.
102 * @param {number} v1 The value for element at index 1.
103 * @param {number} v2 The value for element at index 2.
104 * @param {number} v3 The value for element at index 3.
105 * @return {!goog.vec.vec4d.Type} Return vec so that operations can be
106 * chained together.
107 */
108 goog.vec.vec4d.setFromValues = function(vec, v0, v1, v2, v3) {
109 vec[0] = v0;
110 vec[1] = v1;
111 vec[2] = v2;
112 vec[3] = v3;
113 return vec;
114 };
115
116
117 /**
118 * Initializes vec4d vec from vec4d src.
119 *
120 * @param {!goog.vec.vec4d.Type} vec The destination vector.
121 * @param {!goog.vec.vec4d.Type} src The source vector.
122 * @return {!goog.vec.vec4d.Type} Return vec so that operations can be
123 * chained together.
124 */
125 goog.vec.vec4d.setFromVec4d = function(vec, src) {
126 vec[0] = src[0];
127 vec[1] = src[1];
128 vec[2] = src[2];
129 vec[3] = src[3];
130 return vec;
131 };
132
133
134 /**
135 * Initializes vec4d vec from vec4f src (typed as a Float32Array to
136 * avoid circular goog.requires).
137 *
138 * @param {!goog.vec.vec4d.Type} vec The destination vector.
139 * @param {Float32Array} src The source vector.
140 * @return {!goog.vec.vec4d.Type} Return vec so that operations can be
141 * chained together.
142 */
143 goog.vec.vec4d.setFromVec4f = function(vec, src) {
144 vec[0] = src[0];
145 vec[1] = src[1];
146 vec[2] = src[2];
147 vec[3] = src[3];
148 return vec;
149 };
150
151
152 /**
153 * Initializes vec4d vec from Array src.
154 *
155 * @param {!goog.vec.vec4d.Type} vec The destination vector.
156 * @param {Array<number>} src The source vector.
157 * @return {!goog.vec.vec4d.Type} Return vec so that operations can be
158 * chained together.
159 */
160 goog.vec.vec4d.setFromArray = function(vec, src) {
161 vec[0] = src[0];
162 vec[1] = src[1];
163 vec[2] = src[2];
164 vec[3] = src[3];
165 return vec;
166 };
167
168
169 /**
170 * Performs a component-wise addition of vec0 and vec1 together storing the
171 * result into resultVec.
172 *
173 * @param {!goog.vec.vec4d.Type} vec0 The first addend.
174 * @param {!goog.vec.vec4d.Type} vec1 The second addend.
175 * @param {!goog.vec.vec4d.Type} resultVec The vector to
176 * receive the result. May be vec0 or vec1.
177 * @return {!goog.vec.vec4d.Type} Return resultVec so that operations can be
178 * chained together.
179 */
180 goog.vec.vec4d.add = function(vec0, vec1, resultVec) {
181 resultVec[0] = vec0[0] + vec1[0];
182 resultVec[1] = vec0[1] + vec1[1];
183 resultVec[2] = vec0[2] + vec1[2];
184 resultVec[3] = vec0[3] + vec1[3];
185 return resultVec;
186 };
187
188
189 /**
190 * Performs a component-wise subtraction of vec1 from vec0 storing the
191 * result into resultVec.
192 *
193 * @param {!goog.vec.vec4d.Type} vec0 The minuend.
194 * @param {!goog.vec.vec4d.Type} vec1 The subtrahend.
195 * @param {!goog.vec.vec4d.Type} resultVec The vector to
196 * receive the result. May be vec0 or vec1.
197 * @return {!goog.vec.vec4d.Type} Return resultVec so that operations can be
198 * chained together.
199 */
200 goog.vec.vec4d.subtract = function(vec0, vec1, resultVec) {
201 resultVec[0] = vec0[0] - vec1[0];
202 resultVec[1] = vec0[1] - vec1[1];
203 resultVec[2] = vec0[2] - vec1[2];
204 resultVec[3] = vec0[3] - vec1[3];
205 return resultVec;
206 };
207
208
209 /**
210 * Negates vec0, storing the result into resultVec.
211 *
212 * @param {!goog.vec.vec4d.Type} vec0 The vector to negate.
213 * @param {!goog.vec.vec4d.Type} resultVec The vector to
214 * receive the result. May be vec0.
215 * @return {!goog.vec.vec4d.Type} Return resultVec so that operations can be
216 * chained together.
217 */
218 goog.vec.vec4d.negate = function(vec0, resultVec) {
219 resultVec[0] = -vec0[0];
220 resultVec[1] = -vec0[1];
221 resultVec[2] = -vec0[2];
222 resultVec[3] = -vec0[3];
223 return resultVec;
224 };
225
226
227 /**
228 * Takes the absolute value of each component of vec0 storing the result in
229 * resultVec.
230 *
231 * @param {!goog.vec.vec4d.Type} vec0 The source vector.
232 * @param {!goog.vec.vec4d.Type} resultVec The vector to receive the result.
233 * May be vec0.
234 * @return {!goog.vec.vec4d.Type} Return resultVec so that operations can be
235 * chained together.
236 */
237 goog.vec.vec4d.abs = function(vec0, resultVec) {
238 resultVec[0] = Math.abs(vec0[0]);
239 resultVec[1] = Math.abs(vec0[1]);
240 resultVec[2] = Math.abs(vec0[2]);
241 resultVec[3] = Math.abs(vec0[3]);
242 return resultVec;
243 };
244
245
246 /**
247 * Multiplies each component of vec0 with scalar storing the product into
248 * resultVec.
249 *
250 * @param {!goog.vec.vec4d.Type} vec0 The source vector.
251 * @param {number} scalar The value to multiply with each component of vec0.
252 * @param {!goog.vec.vec4d.Type} resultVec The vector to
253 * receive the result. May be vec0.
254 * @return {!goog.vec.vec4d.Type} Return resultVec so that operations can be
255 * chained together.
256 */
257 goog.vec.vec4d.scale = function(vec0, scalar, resultVec) {
258 resultVec[0] = vec0[0] * scalar;
259 resultVec[1] = vec0[1] * scalar;
260 resultVec[2] = vec0[2] * scalar;
261 resultVec[3] = vec0[3] * scalar;
262 return resultVec;
263 };
264
265
266 /**
267 * Returns the magnitudeSquared of the given vector.
268 *
269 * @param {!goog.vec.vec4d.Type} vec0 The vector.
270 * @return {number} The magnitude of the vector.
271 */
272 goog.vec.vec4d.magnitudeSquared = function(vec0) {
273 var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
274 return x * x + y * y + z * z + w * w;
275 };
276
277
278 /**
279 * Returns the magnitude of the given vector.
280 *
281 * @param {!goog.vec.vec4d.Type} vec0 The vector.
282 * @return {number} The magnitude of the vector.
283 */
284 goog.vec.vec4d.magnitude = function(vec0) {
285 var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
286 return Math.sqrt(x * x + y * y + z * z + w * w);
287 };
288
289
290 /**
291 * Normalizes the given vector storing the result into resultVec.
292 *
293 * @param {!goog.vec.vec4d.Type} vec0 The vector to normalize.
294 * @param {!goog.vec.vec4d.Type} resultVec The vector to
295 * receive the result. May be vec0.
296 * @return {!goog.vec.vec4d.Type} Return resultVec so that operations can be
297 * chained together.
298 */
299 goog.vec.vec4d.normalize = function(vec0, resultVec) {
300 var x = vec0[0], y = vec0[1], z = vec0[2], w = vec0[3];
301 var ilen = 1 / Math.sqrt(x * x + y * y + z * z + w * w);
302 resultVec[0] = x * ilen;
303 resultVec[1] = y * ilen;
304 resultVec[2] = z * ilen;
305 resultVec[3] = w * ilen;
306 return resultVec;
307 };
308
309
310 /**
311 * Returns the scalar product of vectors v0 and v1.
312 *
313 * @param {!goog.vec.vec4d.Type} v0 The first vector.
314 * @param {!goog.vec.vec4d.Type} v1 The second vector.
315 * @return {number} The scalar product.
316 */
317 goog.vec.vec4d.dot = function(v0, v1) {
318 return v0[0] * v1[0] + v0[1] * v1[1] + v0[2] * v1[2] + v0[3] * v1[3];
319 };
320
321
322 /**
323 * Linearly interpolate from v0 to v1 according to f. The value of f should be
324 * in the range [0..1] otherwise the results are undefined.
325 *
326 * @param {!goog.vec.vec4d.Type} v0 The first vector.
327 * @param {!goog.vec.vec4d.Type} v1 The second vector.
328 * @param {number} f The interpolation factor.
329 * @param {!goog.vec.vec4d.Type} resultVec The vector to receive the
330 * results (may be v0 or v1).
331 * @return {!goog.vec.vec4d.Type} Return resultVec so that operations can be
332 * chained together.
333 */
334 goog.vec.vec4d.lerp = function(v0, v1, f, resultVec) {
335 var x = v0[0], y = v0[1], z = v0[2], w = v0[3];
336 resultVec[0] = (v1[0] - x) * f + x;
337 resultVec[1] = (v1[1] - y) * f + y;
338 resultVec[2] = (v1[2] - z) * f + z;
339 resultVec[3] = (v1[3] - w) * f + w;
340 return resultVec;
341 };
342
343
344 /**
345 * Compares the components of vec0 with the components of another vector or
346 * scalar, storing the larger values in resultVec.
347 *
348 * @param {!goog.vec.vec4d.Type} vec0 The source vector.
349 * @param {!goog.vec.vec4d.Type|number} limit The limit vector or scalar.
350 * @param {!goog.vec.vec4d.Type} resultVec The vector to receive the
351 * results (may be vec0 or limit).
352 * @return {!goog.vec.vec4d.Type} Return resultVec so that operations can be
353 * chained together.
354 */
355 goog.vec.vec4d.max = function(vec0, limit, resultVec) {
356 if (goog.isNumber(limit)) {
357 resultVec[0] = Math.max(vec0[0], limit);
358 resultVec[1] = Math.max(vec0[1], limit);
359 resultVec[2] = Math.max(vec0[2], limit);
360 resultVec[3] = Math.max(vec0[3], limit);
361 } else {
362 resultVec[0] = Math.max(vec0[0], limit[0]);
363 resultVec[1] = Math.max(vec0[1], limit[1]);
364 resultVec[2] = Math.max(vec0[2], limit[2]);
365 resultVec[3] = Math.max(vec0[3], limit[3]);
366 }
367 return resultVec;
368 };
369
370
371 /**
372 * Compares the components of vec0 with the components of another vector or
373 * scalar, storing the smaller values in resultVec.
374 *
375 * @param {!goog.vec.vec4d.Type} vec0 The source vector.
376 * @param {!goog.vec.vec4d.Type|number} limit The limit vector or scalar.
377 * @param {!goog.vec.vec4d.Type} resultVec The vector to receive the
378 * results (may be vec0 or limit).
379 * @return {!goog.vec.vec4d.Type} Return resultVec so that operations can be
380 * chained together.
381 */
382 goog.vec.vec4d.min = function(vec0, limit, resultVec) {
383 if (goog.isNumber(limit)) {
384 resultVec[0] = Math.min(vec0[0], limit);
385 resultVec[1] = Math.min(vec0[1], limit);
386 resultVec[2] = Math.min(vec0[2], limit);
387 resultVec[3] = Math.min(vec0[3], limit);
388 } else {
389 resultVec[0] = Math.min(vec0[0], limit[0]);
390 resultVec[1] = Math.min(vec0[1], limit[1]);
391 resultVec[2] = Math.min(vec0[2], limit[2]);
392 resultVec[3] = Math.min(vec0[3], limit[3]);
393 }
394 return resultVec;
395 };
396
397
398 /**
399 * Returns true if the components of v0 are equal to the components of v1.
400 *
401 * @param {!goog.vec.vec4d.Type} v0 The first vector.
402 * @param {!goog.vec.vec4d.Type} v1 The second vector.
403 * @return {boolean} True if the vectors are equal, false otherwise.
404 */
405 goog.vec.vec4d.equals = function(v0, v1) {
406 return v0.length == v1.length && v0[0] == v1[0] && v0[1] == v1[1] &&
407 v0[2] == v1[2] && v0[3] == v1[3];
408 };