textpsys

diff src/vec3.h @ 0:a4ffd9e6984c

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 19 Aug 2015 09:13:48 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/vec3.h	Wed Aug 19 09:13:48 2015 +0300
     1.3 @@ -0,0 +1,94 @@
     1.4 +#ifndef VEC3_H_
     1.5 +#define VEC3_H_
     1.6 +
     1.7 +#include <math.h>
     1.8 +
     1.9 +class Vector3 {
    1.10 +public:
    1.11 +	float x, y, z;
    1.12 +
    1.13 +	Vector3() : x(0), y(0), z(0) {}
    1.14 +	Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
    1.15 +};
    1.16 +
    1.17 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
    1.18 +{
    1.19 +	return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
    1.20 +}
    1.21 +
    1.22 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
    1.23 +{
    1.24 +	return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
    1.25 +}
    1.26 +
    1.27 +inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
    1.28 +{
    1.29 +	return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
    1.30 +}
    1.31 +
    1.32 +inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
    1.33 +{
    1.34 +	return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
    1.35 +}
    1.36 +
    1.37 +inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
    1.38 +{
    1.39 +	a.x += b.x;
    1.40 +	a.y += b.y;
    1.41 +	a.z += b.z;
    1.42 +	return a;
    1.43 +}
    1.44 +
    1.45 +inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
    1.46 +{
    1.47 +	a.x -= b.x;
    1.48 +	a.y -= b.y;
    1.49 +	a.z -= b.z;
    1.50 +	return a;
    1.51 +}
    1.52 +
    1.53 +inline Vector3 operator *(const Vector3 &v, float s)
    1.54 +{
    1.55 +	return Vector3(v.x * s, v.y * s, v.z * s);
    1.56 +}
    1.57 +
    1.58 +inline Vector3 operator *(float s, const Vector3 &v)
    1.59 +{
    1.60 +	return Vector3(v.x * s, v.y * s, v.z * s);
    1.61 +}
    1.62 +
    1.63 +inline float dot(const Vector3 &a, const Vector3 &b)
    1.64 +{
    1.65 +	return a.x * b.x + a.y * b.y + a.z * b.z;
    1.66 +}
    1.67 +
    1.68 +inline float length(const Vector3 &v)
    1.69 +{
    1.70 +	return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
    1.71 +}
    1.72 +
    1.73 +inline float length_sq(const Vector3 &v)
    1.74 +{
    1.75 +	return v.x * v.x + v.y * v.y + v.z * v.z;
    1.76 +}
    1.77 +
    1.78 +inline Vector3 normalize(const Vector3 &v)
    1.79 +{
    1.80 +	float len = length(v);
    1.81 +	if(len == 0.0f) {
    1.82 +		return v;
    1.83 +	}
    1.84 +	return Vector3(v.x / len, v.y / len, v.z / len);
    1.85 +}
    1.86 +
    1.87 +inline float lerp(float a, float b, float t)
    1.88 +{
    1.89 +	return a + (b - a) * t;
    1.90 +}
    1.91 +
    1.92 +inline Vector3 lerp(const Vector3 &a, const Vector3 &b, float t)
    1.93 +{
    1.94 +	return Vector3(lerp(a.x, b.x, t), lerp(a.y, b.y, t), lerp(a.z, b.z, t));
    1.95 +}
    1.96 +
    1.97 +#endif	// VEC3_H_