# HG changeset patch # User John Tsiombikas # Date 1437365305 -10800 # Node ID 8ab44b19895e21b575a45a1b5eefd2d26e1ae96d graphene!!! diff -r 000000000000 -r 8ab44b19895e .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Mon Jul 20 07:08:25 2015 +0300 @@ -0,0 +1,3 @@ +\.o$ +\.d$ +\.swp$ diff -r 000000000000 -r 8ab44b19895e gmath/vec.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gmath/vec.h Mon Jul 20 07:08:25 2015 +0300 @@ -0,0 +1,146 @@ +#ifndef GMATH_VEC_H_ +#define GMATH_VEC_H_ + +#include + +namespace gmath { + +class Vec3 { +public: + float x, y, z; + + Vec3() : x(0), y(0), z(0) {} + Vec3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {} + + void normalize() + { + float len = length(*this); + if(len != 0.0f) { + x /= len; + y /= len; + z /= len; + } + } +}; + +inline Vec3 operator +(const Vec3 &a, const Vec3 &b) +{ + return Vec3(a.x + b.x, a.y + b.y, a.z + b.z); +} + +inline Vec3 operator -(const Vec3 &a, const Vec3 &b) +{ + return Vec3(a.x - b.x, a.y - b.y, a.z - b.z); +} + +inline Vec3 operator *(const Vec3 &a, const Vec3 &b) +{ + return Vec3(a.x * b.x, a.y * b.y, a.z * b.z); +} + +inline Vec3 operator /(const Vec3 &a, const Vec3 &b) +{ + return Vec3(a.x / b.x, a.y / b.y, a.z / b.z); +} + +inline Vec3 operator *(const Vec3 &v, float s) +{ + return Vec3(v.x * s, v.y * s, v.z * s); +} + +inline Vec3 operator *(float s, const Vec3 &v) +{ + return Vec3(s * v.x, s * v.y, s * v.z); +} + +inline Vec3 operator /(const Vec3 &v, float s) +{ + return Vec3(v.x / s, v.y / s, v.z / s); +} + +inline Vec3 operator /(float s, const Vec3 &v) +{ + return Vec3(s / v.x, s / v.y, s / v.z); +} + +inline Vec3 &operator +=(Vec3 &a, const Vec3 &b) +{ + a.x += b.x; + a.y += b.y; + a.z += b.z; + return *this; +} + +inline Vec3 &operator -=(Vec3 &a, const Vec3 &b) +{ + a.x -= b.x; + a.y -= b.y; + a.z -= b.z; + return *this; +} + +inline Vec3 &operator *=(Vec3 &a, const Vec3 &b) +{ + a.x *= b.x; + a.y *= b.y; + a.z *= b.z; + return *this; +} + +inline Vec3 &operator /=(Vec3 &a, const Vec3 &b) +{ + a.x /= b.x; + a.y /= b.y; + a.z /= b.z; + return *this; +} + +inline Vec3 &operator *=(Vec3 &v, float s) +{ + v.x *= s; + v.y *= s; + v.z *= s; + return *this; +} + +inline Vec3 &operator /=(Vec3 &v, float s) +{ + v.x /= s; + v.y /= s; + v.z /= s; + return *this; +} + +inline float dot(const Vec3 &a, const Vec3 &b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} + +inline Vec3 cross(const Vec3 &a, const Vec3 &b) +{ + 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); +} + +inline float length(const Vec3 &v) +{ + return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z); +} + +inline float length_sq(const Vec3 &v) +{ + return v.x * v.x + v.y * v.y + v.z * v.z; +} + +inline Vec3 normalize(const Vec3 &v) +{ + float len = length(v); + if(len == 0.0f) { + return v; + } + + return Vec3(v.x / len, v.y / len, v.z / len); +} + +} + +#endif /* GMATH_VEC_H_ */