gl_matrix/
vec4.rs

1//! 4 Dimensional Vector
2 
3use super::common::{Vec4, Mat4, Quat, random_f32, hypot, EPSILON};
4
5/// Creates a new, empty vec3.
6/// 
7/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
8pub fn create() -> Vec4 {
9    let out: Vec4 = [0_f32; 4];
10
11    out
12}
13
14/// Creates a new vec4 initialized with values from an existing vector.
15/// 
16/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
17pub fn clone(a: &Vec4) -> Vec4 {
18    let mut out: Vec4 = [0_f32; 4];
19
20    out[0] = a[0];
21    out[1] = a[1];
22    out[2] = a[2];
23    out[3] = a[3];
24
25    out
26}
27
28/// Creates a new vec4 initialized with the given values.
29/// 
30/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
31pub fn from_values(x: f32, y: f32, z: f32, w: f32) -> Vec4{
32    let mut out: Vec4 = [0_f32; 4];
33
34    out[0] = x;
35    out[1] = y;
36    out[2] = z;
37    out[3] = w;
38
39    out
40}
41
42/// Copy the values from one vec4 to another.
43/// 
44/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
45pub fn copy(out: &mut Vec4, a: &Vec4) -> Vec4 {
46    out[0] = a[0];
47    out[1] = a[1];
48    out[2] = a[2];
49    out[3] = a[3];
50
51    *out
52}
53
54/// Set the components of a vec4 to the given values.
55/// 
56/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
57pub fn set(out: &mut Vec4, x: f32, y: f32, z: f32, w: f32) -> Vec4 {
58    out[0] = x;
59    out[1] = y;
60    out[2] = z;
61    out[3] = w;
62
63    *out
64}
65
66/// Adds two vec4's.
67/// 
68/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
69pub fn add(out: &mut Vec4, a: &Vec4, b: &Vec4) -> Vec4 {
70    out[0] = a[0] + b[0];
71    out[1] = a[1] + b[1];
72    out[2] = a[2] + b[2];
73    out[3] = a[3] + b[3];
74
75    *out
76}
77
78/// Subtracts vector b from vector a.
79/// 
80/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
81pub fn subtract(out: &mut Vec4, a: &Vec4, b: &Vec4) -> Vec4 {
82    out[0] = a[0] - b[0];
83    out[1] = a[1] - b[1];
84    out[2] = a[2] - b[2];
85    out[3] = a[3] - b[3];
86
87    *out
88}
89
90/// Multiplies two vec4's.
91/// 
92/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
93pub fn multiply(out: &mut Vec4, a: &Vec4, b: &Vec4) -> Vec4 {
94    out[0] = a[0] * b[0];
95    out[1] = a[1] * b[1];
96    out[2] = a[2] * b[2];
97    out[3] = a[3] * b[3];
98
99    *out
100}
101
102/// Divides two vec4's.
103/// 
104/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
105pub fn divide(out: &mut Vec4, a: &Vec4, b: &Vec4) -> Vec4 {
106    out[0] = a[0] / b[0];
107    out[1] = a[1] / b[1];
108    out[2] = a[2] / b[2];
109    out[3] = a[3] / b[3];
110
111    *out
112}
113
114/// f32::ceil the components of a vec4.
115/// 
116/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
117pub fn ceil(out: &mut Vec4, a: &Vec4) -> Vec4 {
118    out[0] = f32::ceil(a[0]);
119    out[1] = f32::ceil(a[1]);
120    out[2] = f32::ceil(a[2]);
121    out[3] = f32::ceil(a[3]);
122
123    *out
124}
125
126/// f32::floor the components of a vec4.
127/// 
128/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
129pub fn floor(out: &mut Vec4, a: &Vec4) -> Vec4 {
130    out[0] = f32::floor(a[0]);
131    out[1] = f32::floor(a[1]);
132    out[2] = f32::floor(a[2]);
133    out[3] = f32::floor(a[3]);
134
135    *out
136}
137
138/// Returns the minimum of two vec4's.
139/// 
140/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
141pub fn min(out: &mut Vec4, a: &Vec4, b: &Vec4) -> Vec4 {
142    out[0] = f32::min(a[0], b[0]);
143    out[1] = f32::min(a[1], b[1]);
144    out[2] = f32::min(a[2], b[2]);
145    out[3] = f32::min(a[3], b[3]);
146
147    *out
148}
149
150/// Returns the maximum of two vec4's.
151/// 
152/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
153pub fn max(out: &mut Vec4, a: &Vec4, b: &Vec4) -> Vec4 {
154    out[0] = f32::max(a[0], b[0]);
155    out[1] = f32::max(a[1], b[1]);
156    out[2] = f32::max(a[2], b[2]);
157    out[3] = f32::max(a[3], b[3]);
158
159    *out
160}
161
162/// f32::round the components of a vec4.
163/// 
164/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
165pub fn round(out: &mut Vec4, a: &Vec4) -> Vec4 {
166    out[0] = f32::round(a[0]);
167    out[1] = f32::round(a[1]);
168    out[2] = f32::round(a[2]);
169    out[3] = f32::round(a[3]);
170
171    *out
172}
173
174/// Scales a vec4 by a scalar number.
175/// 
176/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
177pub fn scale(out: &mut Vec4, a: &Vec4, b: f32) -> Vec4 {
178    out[0] = a[0] * b;
179    out[1] = a[1] * b;
180    out[2] = a[2] * b;
181    out[3] = a[3] * b;
182
183    *out
184}
185
186/// Adds two vec4's after scaling the second operand by a scalar value.
187/// 
188/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
189pub fn scale_and_add(out: &mut Vec4, a: &Vec4, b: &Vec4, scale: f32) -> Vec4 {
190    out[0] = a[0] + (b[0] * scale);
191    out[1] = a[1] + (b[1] * scale);
192    out[2] = a[2] + (b[2] * scale);
193    out[3] = a[3] + (b[3] * scale);
194
195    *out
196}
197
198/// Calculates the euclidian distance between two vec4's.
199/// 
200/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
201pub fn distance(a: &Vec4, b: &Vec4) -> f32 {
202    let mut dist: Vec4 = [0_f32; 4];
203   
204    dist[0] = b[0] - a[0]; // x
205    dist[1] = b[1] - a[1]; // y 
206    dist[2] = b[2] - a[2]; // z
207    dist[3] = b[3] - a[3]; // w
208
209    hypot(&dist.to_vec())
210}
211
212/// Calculates the squared euclidian distance between two vec4's.
213/// 
214/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
215pub fn squared_distance(a: &Vec4, b: &Vec4) -> f32 {
216    let x = b[0] - a[0];
217    let y = b[1] - a[1];
218    let z = b[2] - a[2];
219    let w = b[3] - a[3];
220
221    x*x + y*y + z*z + w*w
222}
223
224/// Calculates the length of a vec4.
225/// 
226/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
227pub fn length(a: &Vec4) -> f32 {
228    let mut len: Vec4  = [0_f32; 4];
229    
230    len[0] = a[0]; // x
231    len[1] = a[1]; // y 
232    len[2] = a[2]; // z
233    len[3] = a[3]; // w
234
235    hypot(&len.to_vec())
236}
237
238/// calculates the squared length of a vec4.
239/// 
240/// [glmatrix documentation](http://glmatrix.net/docs/vec4.js.html)
241pub fn squared_length(a: &[f32]) -> f32 {
242    let x = a[0];
243    let y = a[1];
244    let z = a[2];
245    let w = a[3];
246
247    x*x + y*y + z*z + w*w
248}
249
250/// Negates the components of a vec4.
251/// 
252/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
253pub fn negate(out: &mut Vec4, a: &Vec4) -> Vec4 {
254    out[0] = -a[0];
255    out[1] = -a[1];
256    out[2] = -a[2];
257    out[3] = -a[3];
258
259    *out
260}
261
262/// Returns the inverse of the components of a vec4.
263/// 
264/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
265pub fn inverse(out: &mut Vec4, a: &Vec4) -> Vec4 {
266    out[0] = 1.0 / a[0];
267    out[1] = 1.0 / a[1];
268    out[2] = 1.0 / a[2];
269    out[3] = 1.0 / a[3];
270
271    *out
272}
273
274/// Normalize a vec4.
275/// 
276/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
277pub fn normalize(out: &mut Vec4, a: &Vec4) -> Vec4 {
278    let x = a[0];
279    let y = a[1];
280    let z = a[2];
281    let w = a[3];
282
283    let mut len = x*x + y*y + z*z + w*w;
284    if len > 0_f32 {
285        len = 1_f32 / f32::sqrt(len);
286    }
287
288    out[0] = x * len;
289    out[1] = y * len;
290    out[2] = z * len;
291    out[3] = w * len;
292
293    *out
294}
295
296/// Calculates the dot product of two vec4's.
297/// 
298/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
299pub fn dot(a: &Vec4, b: &Vec4) -> f32 { 
300    a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]
301}
302
303/// Returns the cross-product of three vectors in a 4-dimensional space.
304/// 
305/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
306pub fn cross (out: &mut Vec4, u: &Vec4, v: &Vec4, w: &Vec4) -> Vec4 {
307    let a = (v[0] * w[1]) - (v[1] * w[0]);
308    let b = (v[0] * w[2]) - (v[2] * w[0]);
309    let c = (v[0] * w[3]) - (v[3] * w[0]);
310    let d = (v[1] * w[2]) - (v[2] * w[1]);
311    let e = (v[1] * w[3]) - (v[3] * w[1]);
312    let f = (v[2] * w[3]) - (v[3] * w[2]);
313
314    let g = u[0];
315    let h = u[1];
316    let i = u[2];
317    let j = u[3];
318
319    out[0] = (h * f) - (i * e) + (j * d);
320    out[1] = -(g * f) + (i * c) - (j * b);
321    out[2] = (g * e) - (h * c) + (j * a);
322    out[3] = -(g * d) + (h * b) - (i * a);
323
324    *out
325}
326
327/// Performs a linear interpolation between two vec4's.
328/// 
329/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
330pub fn lerp(out: &mut Vec4, a: &Vec4, b: &Vec4, t: f32) -> Vec4 {
331    let ax = a[0];
332    let ay = a[1];
333    let az = a[2];
334    let aw = a[3];
335
336    out[0] = ax + t * (b[0] - ax);
337    out[1] = ay + t * (b[1] - ay);
338    out[2] = az + t * (b[2] - az);
339    out[3] = aw + t * (b[3] - aw);
340
341    *out
342}
343
344/// Generates a random vector with the given scale.
345/// 
346/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
347pub fn random(out: &mut Vec4, scale: Option<f32>) -> Vec4 {
348    let scale = match scale { 
349        Some(scale) => scale, 
350        None => 1_f32, 
351    };
352    // Marsaglia, George. Choosing a Point from the Surface of a
353    // Sphere. Ann. f32:: Statist. 43 (1972), no. 2, 645--646.
354    // http://projecteuclid.org/euclid.aoms/1177692644;
355    let mut v1 = 0_f32;
356    let mut v2 = 0_f32;
357    let mut v3 = 0_f32;
358    let mut v4 = 0_f32;
359
360    let mut s1 = 2_f32;
361    let mut s2 = 2_f32;
362
363    while s1 > 1_f32 {
364        v1 = random_f32() * 2. - 1.;
365        v2 = random_f32() * 2. - 1.;
366        s1 = v1 * v1 + v2 * v2;
367    }
368    while s2 > 1_f32 {
369        v3 = random_f32() * 2. - 1.;
370        v4 = random_f32() * 2. - 1.;
371        s2 = v3 * v3 + v4 * v4;
372    } 
373
374    let d = f32::sqrt((1_f32 - s1) / s2);
375
376    out[0] = scale * v1;
377    out[1] = scale * v2;
378    out[2] = scale * v3 * d;
379    out[3] = scale * v4 * d;
380
381    *out
382}
383
384/// Transforms the vec4 with a mat4.
385/// 
386/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
387pub fn transform_mat4(out: &mut Vec4, a: &Vec4, m: &Mat4) -> Vec4 {
388    let x = a[0];
389    let y = a[1];
390    let z = a[2];
391    let w = a[3];
392
393    out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
394    out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
395    out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
396    out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
397
398    *out
399}
400
401/// Transforms the vec4 with a quat.
402/// 
403/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
404pub fn transform_quat(out: &mut Vec4, a: &Vec4 , q: &Quat) -> Vec4 {
405    let x = a[0];
406    let y = a[1];
407    let z = a[2];
408
409    let qx = q[0];
410    let qy = q[1];
411    let qz = q[2];
412    let qw = q[3];
413
414    // calculate quat * vec
415    let ix = qw * x + qy * z - qz * y;
416    let iy = qw * y + qz * x - qx * z;
417    let iz = qw * z + qx * y - qy * x;
418    let iw = -qx * x - qy * y - qz * z;
419
420    // calculate result * inverse quat
421    out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
422    out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
423    out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
424    out[3] = a[3];
425
426    *out
427}
428
429/// Set the components of a vec4 to zero.
430/// 
431/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
432pub fn zero(out: &mut Vec4) -> Vec4 {
433    out[0] = 0.0;
434    out[1] = 0.0;
435    out[2] = 0.0;
436    out[3] = 0.0;
437
438    *out
439}
440
441/// Returns a string representation of a vector.
442/// 
443/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
444pub fn vec4_string(a: &Vec4) -> String {
445    let a0 = ["vec4(".to_string(), a[0].to_string()].join("");
446    let a1 = a[1].to_string(); 
447    let a2 = a[2].to_string(); 
448    let a3 = [a[3].to_string(), ")".to_string()].join("");
449
450    [a0, a1, a2, a3].join(", ")
451}
452
453/// Returns whether or not the vectors have exactly the same elements in the same position (when compared with ==).
454/// 
455/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
456pub fn exact_equals(a: &Vec4, b: &Vec4) -> bool {
457    a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]
458}
459
460/// Returns whether or not the vectors have approximately the same elements in the same position..
461/// 
462/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
463pub fn equals(a: &Vec4, b: &Vec4) -> bool {
464    let a0 = a[0];
465    let a1 = a[1];
466    let a2 = a[2];
467    let a3 = a[3];
468
469    let b0 = b[0];
470    let b1 = b[1];
471    let b2 = b[2];
472    let b3 = b[3];
473
474    f32::abs(a0 - b0) <= EPSILON * f32::max(1.0, f32::max(f32::abs(a0), f32::abs(b0))) && 
475    f32::abs(a1 - b1) <= EPSILON * f32::max(1.0, f32::max(f32::abs(a1), f32::abs(b1))) && 
476    f32::abs(a2 - b2) <= EPSILON * f32::max(1.0, f32::max(f32::abs(a2), f32::abs(b2))) && 
477    f32::abs(a3 - b3) <= EPSILON * f32::max(1.0, f32::max(f32::abs(a3), f32::abs(b3)))
478}
479
480/// Alias for vec4::subtract.
481/// 
482/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
483pub fn sub(out: &mut Vec4, a: &Vec4, b: &Vec4) -> Vec4 {
484    subtract(out, a, b)
485}
486
487/// Alias for vec4::multiply.
488/// 
489/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
490pub fn mul(out: &mut Vec4, a: &Vec4, b: &Vec4) -> Vec4 {
491    multiply(out, a, b)
492}
493
494/// Alias for vec4::divide.
495/// 
496/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
497pub fn div(out: &mut Vec4, a: &Vec4, b: &Vec4) -> Vec4 {
498    divide(out, a, b)
499}
500
501/// Alias for vec4::distance.
502/// 
503/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
504pub fn dist(a: &Vec4, b: &Vec4) -> f32 {
505    distance(a, b)
506}
507
508/// Alias for vec4::squared_distance.
509/// 
510/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
511pub fn sqr_dist(a: &Vec4, b: &Vec4) -> f32 {
512    squared_distance(a, b)
513}
514
515/// Alias for vec4::length.
516/// 
517/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
518pub fn len(a: &Vec4) -> f32 {
519    length(a)
520}
521
522/// Alias for vec4::squred_length.
523/// 
524/// [glMatrix Documentation](http://glmatrix.net/docs/vec4.js.html)
525pub fn sqr_len(a: &Vec4) -> f32 {
526    squared_length(a)
527}
528
529// /**
530//  * Perform some operation over an array of vec4s.
531//  *
532//  * @param {Array} a the array of vectors to iterate over
533//  * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
534//  * @param {Number} offset Number of elements to skip at the beginning of the array
535//  * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array
536//  * @param {fn} fn fn to call for each vector in the array
537//  * @param {Object} [arg] additional argument to pass to fn
538//  * @returns {Array} a
539//  * @fn
540//  */
541
542// pub fn for_each(a: &[f32], stride: f32, offset: f32, count: f32, f: fn(), arg){
543// }
544// pub const forEach = (fn() {
545//   let vec = create();
546//   return fn(a, stride, offset, count, fn, arg) {
547//     let i, l;
548//     if(!stride) {
549//       stride = 4;
550//     }
551
552//     if(!offset) {
553//       offset = 0;
554//     }
555
556//     if(count) {
557//       l = f32::min((count * stride) + offset, a.length);
558//     } else {
559//       l = a.length;
560//     }
561
562//     for(i = offset; i < l; i += stride) {
563//       vec[0] = a[i]; vec[1] = a[i+1]; vec[2] = a[i+2]; vec[3] = a[i+3];
564//       fn(vec, vec, arg);
565//       a[i] = vec[0]; a[i+1] = vec[1]; a[i+2] = vec[2]; a[i+3] = vec[3];
566//     }
567
568//     return a;
569//   };
570// })();
571
572
573#[cfg(test)]
574mod tests { 
575    use super::*;
576
577    #[test]
578    fn clone_a_vec4() { 
579        let vec4_a: Vec4 = [1., 2., 3., 4.];
580
581        let out = clone(&vec4_a);
582
583        assert_eq!([1., 2., 3., 4.], out);
584    }
585
586    #[test] 
587    fn create_vec4_from_values() { 
588        let out = from_values(1., 2., 3., 4.);
589
590        assert_eq!([1., 2., 3., 4.], out);
591    }
592
593    #[test]
594    fn copy_values_from_a_vec4_it_another() { 
595        let mut out: Vec4 = [0., 0., 0., 0.];
596        let vec4_a: Vec4 = [1., 2., 3., 4.];
597
598        let result = copy(&mut out, &vec4_a);
599
600        assert_eq!([1., 2., 3., 4.], out);
601        assert_eq!(result, out);
602    }
603
604    #[test]
605    fn set_vec4_with_values() { 
606        let mut out: Vec4 = [0., 0., 0., 0.];
607
608        let result = set(&mut out, 1., 2., 3., 4.);
609
610        assert_eq!([1., 2., 3., 4.], out);
611        assert_eq!(result, out);
612    }
613
614    #[test]
615    fn add_two_vec4s() { 
616        let mut out: Vec4 = [0., 0., 0., 0.];
617        let vec4_a: Vec4 = [1., 2., 3., 4.];
618        let vec4_b: Vec4 = [5., 6., 7., 8.];
619
620        let result = add(&mut out, &vec4_a, &vec4_b);
621
622        assert_eq!([6., 8., 10., 12.], out);
623        assert_eq!(result, out);
624    }
625
626    #[test]
627    fn subtract_two_vec4s() { 
628        let mut out: Vec4 = [0., 0., 0., 0.];
629        let vec4_a: Vec4 = [1., 2., 3., 4.];
630        let vec4_b: Vec4 = [5., 6., 7., 8.];
631
632        let result = subtract(&mut out, &vec4_a, &vec4_b);
633
634        assert_eq!([-4., -4., -4., -4.], out);
635        assert_eq!(result, out);
636    }
637
638
639    #[test]
640    fn scale_vec4() { 
641        let mut out: Vec4 = [0., 0., 0., 0.];
642        let vec4_a: Vec4 = [1., 2., 3., 4.];
643
644        let result = scale(&mut out, &vec4_a, 2_f32);
645
646        assert_eq!([2., 4., 6., 8.], out);
647        assert_eq!(result, out);
648    }
649
650    #[test]
651    fn dot_vec4() { 
652        let mut out: Vec4 = [0., 0., 0., 0.];
653        let vec4_a: Vec4 = [1., 2., 3., 4.];
654
655        let result = scale(&mut out, &vec4_a, 2_f32);
656
657        assert_eq!([2., 4., 6., 8.], out);
658        assert_eq!(result, out);
659    }
660
661    #[test]
662    fn lerp_vec4() { 
663        let mut out: Vec4 = [0., 0., 0., 0.];
664        let vec4_a: Vec4 = [1., 2., 3., 4.];
665        let vec4_b: Vec4 = [5., 6., 7., 8.];
666
667        let result = add(&mut out, &vec4_a, &vec4_b);
668
669        assert_eq!([6., 8., 10., 12.], out);
670        assert_eq!(result, out);
671    }
672
673    #[test]
674    fn length_of_vec4() {
675        let vec4_a: Vec4 = [1., 2., 3., 4.];
676
677        let out = length(&vec4_a);
678
679        // they get 5.477225
680        assert_eq!(5.477226, out);
681    }
682
683    #[test]
684    fn len_of_vec4() { 
685        let vec4_a: Vec4 = [1., 2., 3., 4.];
686
687        let out = len(&vec4_a);
688
689        // they get 5.477225
690        assert_eq!(5.477226, out);
691    }
692
693    #[test]
694    fn length_is_equal_to_len() {
695        let vec4_a: Vec4 = [1., 2., 3., 4.];
696
697        let out_a = length(&vec4_a);
698        let out_b = len(&vec4_a);
699
700        assert_eq!(out_a, out_b);
701    }
702
703    #[test]
704    fn squared_length_of_vec4() { 
705        let vec4_a: Vec4 = [1., 2., 3., 4.];
706
707        let out = squared_length(&vec4_a);
708
709        // they get 5.477225
710        assert_eq!(30_f32, out);
711    }
712
713    #[test]
714    fn sqr_len_of_vec4() { 
715        let vec4_a: Vec4 = [1., 2., 3., 4.];
716
717        let out = sqr_len(&vec4_a);
718
719        // they get 5.477225
720        assert_eq!(30_f32, out);
721    }
722
723    #[test]
724    fn squared_length_is_equal_to_sqr_len() { 
725        let vec4_a: Vec4 = [1., 2., 3., 4.];
726
727        let out_a = squared_length(&vec4_a);
728        let out_b = sqr_len(&vec4_a);
729
730        assert_eq!(out_a, out_b);
731    }
732
733    #[test]
734    fn normalize_vec4() { 
735        let mut out: Vec4 = [0., 0., 0., 0.];
736        let vec4_a: Vec4 = [5., 0., 0., 0.];
737
738        let result = normalize(&mut out, &vec4_a);
739
740        assert_eq!([1., 0., 0., 0.], out);
741        assert_eq!(result, out);
742    }
743
744    #[test]
745    fn vec4_are_exact_equal() { 
746        let vec4_a: Vec4 = [0., 1., 2., 3.];
747        let vec4_b: Vec4 = [0., 1., 2., 3.];
748
749        let r0 = exact_equals(&vec4_a, &vec4_b);
750
751        assert!(r0);  
752    }
753
754    #[test]
755    fn vec4_are_not_exact_equal() { 
756        let vec4_a: Vec4 = [0., 1., 2., 3.];
757        let vec4_b: Vec4 = [1., 2., 3., 4.];
758
759        let r0 = exact_equals(&vec4_a, &vec4_b);
760
761        assert!(!r0); 
762    }
763
764    #[test]
765    fn vec4_are_equal() { 
766        let vec4_a: Vec4 = [0., 1., 2., 3.];
767        let vec4_b: Vec4 = [0., 1., 2., 3.];
768
769        let r0 = equals(&vec4_a, &vec4_b);
770
771        assert!(r0);  
772    }
773
774    #[test]
775    fn vec4_are_equal_enough() { 
776        let vec4_a: Vec4 = [0., 1., 2., 3.];
777        let vec4_b: Vec4 = [1_f32*10_f32.powi(-16), 1., 2., 3.];
778
779        let r0 = equals(&vec4_a, &vec4_b);
780
781        assert!(r0);  
782    }
783
784    #[test]
785    fn vec4_are_not_equal() { 
786        let vec4_a: Vec4 = [0., 1., 2., 3.];
787        let vec4_b: Vec4 = [1., 2., 3., 4.];
788
789        let r0 = equals(&vec4_a, &vec4_b);
790
791        assert!(!r0);  
792    }
793}