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