graphene

view src/gmath/vec.h @ 2:fb032d88839f

moved gmath under src
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Jul 2015 05:08:22 +0300
parents gmath/vec.h@8ab44b19895e
children
line source
1 #ifndef GMATH_VEC_H_
2 #define GMATH_VEC_H_
4 #include <math.h>
6 namespace gph {
8 class Vector3 {
9 public:
10 float x, y, z;
12 Vector3() : x(0), y(0), z(0) {}
13 Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
15 inline void normalize();
16 };
18 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
19 {
20 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
21 }
23 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
24 {
25 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
26 }
28 inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
29 {
30 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
31 }
33 inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
34 {
35 return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
36 }
38 inline Vector3 operator *(const Vector3 &v, float s)
39 {
40 return Vector3(v.x * s, v.y * s, v.z * s);
41 }
43 inline Vector3 operator *(float s, const Vector3 &v)
44 {
45 return Vector3(s * v.x, s * v.y, s * v.z);
46 }
48 inline Vector3 operator /(const Vector3 &v, float s)
49 {
50 return Vector3(v.x / s, v.y / s, v.z / s);
51 }
53 inline Vector3 operator /(float s, const Vector3 &v)
54 {
55 return Vector3(s / v.x, s / v.y, s / v.z);
56 }
58 inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
59 {
60 a.x += b.x;
61 a.y += b.y;
62 a.z += b.z;
63 return a;
64 }
66 inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
67 {
68 a.x -= b.x;
69 a.y -= b.y;
70 a.z -= b.z;
71 return a;
72 }
74 inline Vector3 &operator *=(Vector3 &a, const Vector3 &b)
75 {
76 a.x *= b.x;
77 a.y *= b.y;
78 a.z *= b.z;
79 return a;
80 }
82 inline Vector3 &operator /=(Vector3 &a, const Vector3 &b)
83 {
84 a.x /= b.x;
85 a.y /= b.y;
86 a.z /= b.z;
87 return a;
88 }
90 inline Vector3 &operator *=(Vector3 &v, float s)
91 {
92 v.x *= s;
93 v.y *= s;
94 v.z *= s;
95 return v;
96 }
98 inline Vector3 &operator /=(Vector3 &v, float s)
99 {
100 v.x /= s;
101 v.y /= s;
102 v.z /= s;
103 return v;
104 }
106 inline float dot(const Vector3 &a, const Vector3 &b)
107 {
108 return a.x * b.x + a.y * b.y + a.z * b.z;
109 }
111 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
112 {
113 return Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
114 }
116 inline float length(const Vector3 &v)
117 {
118 return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
119 }
121 inline float length_sq(const Vector3 &v)
122 {
123 return v.x * v.x + v.y * v.y + v.z * v.z;
124 }
126 inline Vector3 normalize(const Vector3 &v)
127 {
128 float len = length(v);
129 if(len == 0.0f) {
130 return v;
131 }
133 return Vector3(v.x / len, v.y / len, v.z / len);
134 }
136 inline void Vector3::normalize()
137 {
138 float len = length(*this);
139 if(len != 0.0f) {
140 x /= len;
141 y /= len;
142 z /= len;
143 }
144 }
147 }
149 #endif /* GMATH_VEC_H_ */