textpsys

view 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 source
1 #ifndef VEC3_H_
2 #define VEC3_H_
4 #include <math.h>
6 class Vector3 {
7 public:
8 float x, y, z;
10 Vector3() : x(0), y(0), z(0) {}
11 Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
12 };
14 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
15 {
16 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
17 }
19 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
20 {
21 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
22 }
24 inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
25 {
26 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
27 }
29 inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
30 {
31 return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
32 }
34 inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
35 {
36 a.x += b.x;
37 a.y += b.y;
38 a.z += b.z;
39 return a;
40 }
42 inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
43 {
44 a.x -= b.x;
45 a.y -= b.y;
46 a.z -= b.z;
47 return a;
48 }
50 inline Vector3 operator *(const Vector3 &v, float s)
51 {
52 return Vector3(v.x * s, v.y * s, v.z * s);
53 }
55 inline Vector3 operator *(float s, const Vector3 &v)
56 {
57 return Vector3(v.x * s, v.y * s, v.z * s);
58 }
60 inline float dot(const Vector3 &a, const Vector3 &b)
61 {
62 return a.x * b.x + a.y * b.y + a.z * b.z;
63 }
65 inline float length(const Vector3 &v)
66 {
67 return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
68 }
70 inline float length_sq(const Vector3 &v)
71 {
72 return v.x * v.x + v.y * v.y + v.z * v.z;
73 }
75 inline Vector3 normalize(const Vector3 &v)
76 {
77 float len = length(v);
78 if(len == 0.0f) {
79 return v;
80 }
81 return Vector3(v.x / len, v.y / len, v.z / len);
82 }
84 inline float lerp(float a, float b, float t)
85 {
86 return a + (b - a) * t;
87 }
89 inline Vector3 lerp(const Vector3 &a, const Vector3 &b, float t)
90 {
91 return Vector3(lerp(a.x, b.x, t), lerp(a.y, b.y, t), lerp(a.z, b.z, t));
92 }
94 #endif // VEC3_H_