graphene

view src/gmath/vector.h @ 3:d71b4e899e08

minimal matrix and vec4
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Jul 2015 05:52:39 +0300
parents src/gmath/vec.h@fb032d88839f
children d30e24132b6e
line source
1 #ifndef GMATH_VEC_H_
2 #define GMATH_VEC_H_
4 #include <math.h>
6 namespace gph {
8 class Vector4;
10 class Vector3 {
11 public:
12 float x, y, z;
14 Vector3() : x(0), y(0), z(0) {}
15 Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
16 Vector3(const Vector4 &v);
18 inline void normalize()
19 {
20 float len = (float)sqrt(x * x + y * y + z * z);
21 if(len != 0.0f) {
22 x /= len;
23 y /= len;
24 z /= len;
25 }
26 }
28 inline float &operator[] (int idx)
29 {
30 return idx == 0 ? x : (idx == 1 ? y : z);
31 }
33 inline const float &operator[] (int idx) const
34 {
35 return idx == 0 ? x : (idx == 1 ? y : z);
36 }
37 };
40 class Vector4 {
41 public:
42 float x, y, z, w;
44 Vector4() : x(0), y(0), z(0), w(0) {}
45 Vector4(float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {}
46 Vector4(const Vector3 &v);
48 inline void normalize()
49 {
50 float len = (float)sqrt(x * x + y * y + z * z + w * w);
51 if(len != 0.0f) {
52 x /= len;
53 y /= len;
54 z /= len;
55 w /= len;
56 }
57 }
59 inline float &operator[] (int idx)
60 {
61 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
62 }
64 inline const float &operator[] (int idx) const
65 {
66 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
67 }
68 };
70 // ---- Vector3 functions ----
72 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
73 {
74 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
75 }
77 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
78 {
79 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
80 }
82 inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
83 {
84 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
85 }
87 inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
88 {
89 return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
90 }
92 inline Vector3 operator *(const Vector3 &v, float s)
93 {
94 return Vector3(v.x * s, v.y * s, v.z * s);
95 }
97 inline Vector3 operator *(float s, const Vector3 &v)
98 {
99 return Vector3(s * v.x, s * v.y, s * v.z);
100 }
102 inline Vector3 operator /(const Vector3 &v, float s)
103 {
104 return Vector3(v.x / s, v.y / s, v.z / s);
105 }
107 inline Vector3 operator /(float s, const Vector3 &v)
108 {
109 return Vector3(s / v.x, s / v.y, s / v.z);
110 }
112 inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
113 {
114 a.x += b.x;
115 a.y += b.y;
116 a.z += b.z;
117 return a;
118 }
120 inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
121 {
122 a.x -= b.x;
123 a.y -= b.y;
124 a.z -= b.z;
125 return a;
126 }
128 inline Vector3 &operator *=(Vector3 &a, const Vector3 &b)
129 {
130 a.x *= b.x;
131 a.y *= b.y;
132 a.z *= b.z;
133 return a;
134 }
136 inline Vector3 &operator /=(Vector3 &a, const Vector3 &b)
137 {
138 a.x /= b.x;
139 a.y /= b.y;
140 a.z /= b.z;
141 return a;
142 }
144 inline Vector3 &operator *=(Vector3 &v, float s)
145 {
146 v.x *= s;
147 v.y *= s;
148 v.z *= s;
149 return v;
150 }
152 inline Vector3 &operator /=(Vector3 &v, float s)
153 {
154 v.x /= s;
155 v.y /= s;
156 v.z /= s;
157 return v;
158 }
160 inline float dot(const Vector3 &a, const Vector3 &b)
161 {
162 return a.x * b.x + a.y * b.y + a.z * b.z;
163 }
165 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
166 {
167 return Vector3(a.y * b.z - a.z * b.y,
168 a.z * b.x - a.x * b.z,
169 a.x * b.y - a.y * b.x);
170 }
172 inline float length(const Vector3 &v)
173 {
174 return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
175 }
177 inline float length_sq(const Vector3 &v)
178 {
179 return v.x * v.x + v.y * v.y + v.z * v.z;
180 }
182 inline Vector3 normalize(const Vector3 &v)
183 {
184 float len = length(v);
185 if(len == 0.0f) {
186 return v;
187 }
189 return Vector3(v.x / len, v.y / len, v.z / len);
190 }
192 }
194 #endif /* GMATH_VEC_H_ */