graphene
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/gmath/vec.h Sat Jul 25 05:08:22 2015 +0300 1.3 @@ -0,0 +1,149 @@ 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 Vector3 { 1.12 +public: 1.13 + float x, y, z; 1.14 + 1.15 + Vector3() : x(0), y(0), z(0) {} 1.16 + Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {} 1.17 + 1.18 + inline void normalize(); 1.19 +}; 1.20 + 1.21 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b) 1.22 +{ 1.23 + return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); 1.24 +} 1.25 + 1.26 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b) 1.27 +{ 1.28 + return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); 1.29 +} 1.30 + 1.31 +inline Vector3 operator *(const Vector3 &a, const Vector3 &b) 1.32 +{ 1.33 + return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); 1.34 +} 1.35 + 1.36 +inline Vector3 operator /(const Vector3 &a, const Vector3 &b) 1.37 +{ 1.38 + return Vector3(a.x / b.x, a.y / b.y, a.z / b.z); 1.39 +} 1.40 + 1.41 +inline Vector3 operator *(const Vector3 &v, float s) 1.42 +{ 1.43 + return Vector3(v.x * s, v.y * s, v.z * s); 1.44 +} 1.45 + 1.46 +inline Vector3 operator *(float s, const Vector3 &v) 1.47 +{ 1.48 + return Vector3(s * v.x, s * v.y, s * v.z); 1.49 +} 1.50 + 1.51 +inline Vector3 operator /(const Vector3 &v, float s) 1.52 +{ 1.53 + return Vector3(v.x / s, v.y / s, v.z / s); 1.54 +} 1.55 + 1.56 +inline Vector3 operator /(float s, const Vector3 &v) 1.57 +{ 1.58 + return Vector3(s / v.x, s / v.y, s / v.z); 1.59 +} 1.60 + 1.61 +inline Vector3 &operator +=(Vector3 &a, const Vector3 &b) 1.62 +{ 1.63 + a.x += b.x; 1.64 + a.y += b.y; 1.65 + a.z += b.z; 1.66 + return a; 1.67 +} 1.68 + 1.69 +inline Vector3 &operator -=(Vector3 &a, const Vector3 &b) 1.70 +{ 1.71 + a.x -= b.x; 1.72 + a.y -= b.y; 1.73 + a.z -= b.z; 1.74 + return a; 1.75 +} 1.76 + 1.77 +inline Vector3 &operator *=(Vector3 &a, const Vector3 &b) 1.78 +{ 1.79 + a.x *= b.x; 1.80 + a.y *= b.y; 1.81 + a.z *= b.z; 1.82 + return a; 1.83 +} 1.84 + 1.85 +inline Vector3 &operator /=(Vector3 &a, const Vector3 &b) 1.86 +{ 1.87 + a.x /= b.x; 1.88 + a.y /= b.y; 1.89 + a.z /= b.z; 1.90 + return a; 1.91 +} 1.92 + 1.93 +inline Vector3 &operator *=(Vector3 &v, float s) 1.94 +{ 1.95 + v.x *= s; 1.96 + v.y *= s; 1.97 + v.z *= s; 1.98 + return v; 1.99 +} 1.100 + 1.101 +inline Vector3 &operator /=(Vector3 &v, float s) 1.102 +{ 1.103 + v.x /= s; 1.104 + v.y /= s; 1.105 + v.z /= s; 1.106 + return v; 1.107 +} 1.108 + 1.109 +inline float dot(const Vector3 &a, const Vector3 &b) 1.110 +{ 1.111 + return a.x * b.x + a.y * b.y + a.z * b.z; 1.112 +} 1.113 + 1.114 +inline Vector3 cross(const Vector3 &a, const Vector3 &b) 1.115 +{ 1.116 + 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); 1.117 +} 1.118 + 1.119 +inline float length(const Vector3 &v) 1.120 +{ 1.121 + return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z); 1.122 +} 1.123 + 1.124 +inline float length_sq(const Vector3 &v) 1.125 +{ 1.126 + return v.x * v.x + v.y * v.y + v.z * v.z; 1.127 +} 1.128 + 1.129 +inline Vector3 normalize(const Vector3 &v) 1.130 +{ 1.131 + float len = length(v); 1.132 + if(len == 0.0f) { 1.133 + return v; 1.134 + } 1.135 + 1.136 + return Vector3(v.x / len, v.y / len, v.z / len); 1.137 +} 1.138 + 1.139 +inline void Vector3::normalize() 1.140 +{ 1.141 + float len = length(*this); 1.142 + if(len != 0.0f) { 1.143 + x /= len; 1.144 + y /= len; 1.145 + z /= len; 1.146 + } 1.147 +} 1.148 + 1.149 + 1.150 +} 1.151 + 1.152 +#endif /* GMATH_VEC_H_ */