Debian Codegraph @

external/cubicvr2/math/vec4.h

raw
1 //
2 // vec4.h
3 // CubicVR2
4 //
5 // Created by Charles J. Cliffe on 2013-02-22.
6 // Copyright (c) 2013 Charles J. Cliffe. All rights reserved.
7 //
8
9 #ifndef __CubicVR2__vec4__
10 #define __CubicVR2__vec4__
11
12 #include <iostream>
13 #include "cubic_types.h"
14 #include <cmath>
15 #include <stddef.h>
16
17 namespace CubicVR {
18
19 #define vec4SG(c,x,y) \
20 vec3 COMBINE(get,x)() { return y; } \
21 c & COMBINE(set,x)(vec3 value) { y = value; return *this; }
22
23 struct vec4 {
24
25 __float x, y, z, w;
26
27 public:
28 __float& r() { return x; }
29 __float& g() { return y; }
30 __float& b() { return z; }
31 __float& a() { return w; }
32
33 //access as-array:
34 inline __float& operator [] (size_t i) {
35 __float* as_array = (__float*)this;
36 return (as_array[i]);
37 }
38
39 inline const __float& operator [] (size_t i) const {
40 __float* as_array = (__float*)this;
41 return (as_array[i]);
42 }
43
44 vec4 (__float xi,__float yi,__float zi,__float wi) { x = xi; y = yi; z = zi; w = wi; }
45 vec4 () { x = y = z = w = 0.0f; }
46
47 vec4 operator*(__float v) { return vec4(x*v, y*v, z*v, w*v); }
48 // vec4 operator*(vec4 v) { return vec4::cross(*this,v); }
49 // vec4 operator+(vec4 v) { return vec4::add(*this,v); }
50 // vec4 operator-(vec4 v) { return vec4::subtract(*this,v); }
51
52 static __float length(vec4 a, vec4 b) {
53 __float v[4] = {a[0]-b[0],a[1]-b[1],a[2]-b[2],a[3]-b[3]};
54 return sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
55 };
56
57 static __float length(vec4 v) {
58 return sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2] + v[3] * v[3]);
59 };
60
61 static vec4 normalize(vec4 v) {
62 __float n = sqrtf(vec4::length(v));
63
64 v[0] /= n;
65 v[1] /= n;
66 v[2] /= n;
67 v[3] /= n;
68
69 return v;
70 };
71
72 static __float dot(vec4 v1, vec4 v2) {
73 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2] + v1[3] * v2[3];
74 };
75
76 };
77
78 }
79
80 #endif /* defined(__CubicVR2__vec4__) */