graphene
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/gmath/vector.h Sat Jul 25 05:52:39 2015 +0300 1.3 @@ -0,0 +1,194 @@ 1.4 +#ifndef GMATH_VEC_H_ 1.5 +#define GMATH_VEC_H_ 1.6 + 1.7 +#include <math.h> 1.8 + 1.9 +namespace gph { 1.10 + 1.11 +class Vector4; 1.12 + 1.13 +class Vector3 { 1.14 +public: 1.15 + float x, y, z; 1.16 + 1.17 + Vector3() : x(0), y(0), z(0) {} 1.18 + Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {} 1.19 + Vector3(const Vector4 &v); 1.20 + 1.21 + inline void normalize() 1.22 + { 1.23 + float len = (float)sqrt(x * x + y * y + z * z); 1.24 + if(len != 0.0f) { 1.25 + x /= len; 1.26 + y /= len; 1.27 + z /= len; 1.28 + } 1.29 + } 1.30 + 1.31 + inline float &operator[] (int idx) 1.32 + { 1.33 + return idx == 0 ? x : (idx == 1 ? y : z); 1.34 + } 1.35 + 1.36 + inline const float &operator[] (int idx) const 1.37 + { 1.38 + return idx == 0 ? x : (idx == 1 ? y : z); 1.39 + } 1.40 +}; 1.41 + 1.42 + 1.43 +class Vector4 { 1.44 +public: 1.45 + float x, y, z, w; 1.46 + 1.47 + Vector4() : x(0), y(0), z(0), w(0) {} 1.48 + Vector4(float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {} 1.49 + Vector4(const Vector3 &v); 1.50 + 1.51 + inline void normalize() 1.52 + { 1.53 + float len = (float)sqrt(x * x + y * y + z * z + w * w); 1.54 + if(len != 0.0f) { 1.55 + x /= len; 1.56 + y /= len; 1.57 + z /= len; 1.58 + w /= len; 1.59 + } 1.60 + } 1.61 + 1.62 + inline float &operator[] (int idx) 1.63 + { 1.64 + return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w)); 1.65 + } 1.66 + 1.67 + inline const float &operator[] (int idx) const 1.68 + { 1.69 + return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w)); 1.70 + } 1.71 +}; 1.72 + 1.73 +// ---- Vector3 functions ---- 1.74 + 1.75 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b) 1.76 +{ 1.77 + return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); 1.78 +} 1.79 + 1.80 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b) 1.81 +{ 1.82 + return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); 1.83 +} 1.84 + 1.85 +inline Vector3 operator *(const Vector3 &a, const Vector3 &b) 1.86 +{ 1.87 + return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); 1.88 +} 1.89 + 1.90 +inline Vector3 operator /(const Vector3 &a, const Vector3 &b) 1.91 +{ 1.92 + return Vector3(a.x / b.x, a.y / b.y, a.z / b.z); 1.93 +} 1.94 + 1.95 +inline Vector3 operator *(const Vector3 &v, float s) 1.96 +{ 1.97 + return Vector3(v.x * s, v.y * s, v.z * s); 1.98 +} 1.99 + 1.100 +inline Vector3 operator *(float s, const Vector3 &v) 1.101 +{ 1.102 + return Vector3(s * v.x, s * v.y, s * v.z); 1.103 +} 1.104 + 1.105 +inline Vector3 operator /(const Vector3 &v, float s) 1.106 +{ 1.107 + return Vector3(v.x / s, v.y / s, v.z / s); 1.108 +} 1.109 + 1.110 +inline Vector3 operator /(float s, const Vector3 &v) 1.111 +{ 1.112 + return Vector3(s / v.x, s / v.y, s / v.z); 1.113 +} 1.114 + 1.115 +inline Vector3 &operator +=(Vector3 &a, const Vector3 &b) 1.116 +{ 1.117 + a.x += b.x; 1.118 + a.y += b.y; 1.119 + a.z += b.z; 1.120 + return a; 1.121 +} 1.122 + 1.123 +inline Vector3 &operator -=(Vector3 &a, const Vector3 &b) 1.124 +{ 1.125 + a.x -= b.x; 1.126 + a.y -= b.y; 1.127 + a.z -= b.z; 1.128 + return a; 1.129 +} 1.130 + 1.131 +inline Vector3 &operator *=(Vector3 &a, const Vector3 &b) 1.132 +{ 1.133 + a.x *= b.x; 1.134 + a.y *= b.y; 1.135 + a.z *= b.z; 1.136 + return a; 1.137 +} 1.138 + 1.139 +inline Vector3 &operator /=(Vector3 &a, const Vector3 &b) 1.140 +{ 1.141 + a.x /= b.x; 1.142 + a.y /= b.y; 1.143 + a.z /= b.z; 1.144 + return a; 1.145 +} 1.146 + 1.147 +inline Vector3 &operator *=(Vector3 &v, float s) 1.148 +{ 1.149 + v.x *= s; 1.150 + v.y *= s; 1.151 + v.z *= s; 1.152 + return v; 1.153 +} 1.154 + 1.155 +inline Vector3 &operator /=(Vector3 &v, float s) 1.156 +{ 1.157 + v.x /= s; 1.158 + v.y /= s; 1.159 + v.z /= s; 1.160 + return v; 1.161 +} 1.162 + 1.163 +inline float dot(const Vector3 &a, const Vector3 &b) 1.164 +{ 1.165 + return a.x * b.x + a.y * b.y + a.z * b.z; 1.166 +} 1.167 + 1.168 +inline Vector3 cross(const Vector3 &a, const Vector3 &b) 1.169 +{ 1.170 + return Vector3(a.y * b.z - a.z * b.y, 1.171 + a.z * b.x - a.x * b.z, 1.172 + a.x * b.y - a.y * b.x); 1.173 +} 1.174 + 1.175 +inline float length(const Vector3 &v) 1.176 +{ 1.177 + return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 1.178 +} 1.179 + 1.180 +inline float length_sq(const Vector3 &v) 1.181 +{ 1.182 + return v.x * v.x + v.y * v.y + v.z * v.z; 1.183 +} 1.184 + 1.185 +inline Vector3 normalize(const Vector3 &v) 1.186 +{ 1.187 + float len = length(v); 1.188 + if(len == 0.0f) { 1.189 + return v; 1.190 + } 1.191 + 1.192 + return Vector3(v.x / len, v.y / len, v.z / len); 1.193 +} 1.194 + 1.195 +} 1.196 + 1.197 +#endif /* GMATH_VEC_H_ */