## graphene

### view src/gmath/vector.h @ 4:d30e24132b6e

more gmath
author John Tsiombikas Sat, 25 Jul 2015 07:42:30 +0300 d71b4e899e08 2ce58d5309f0
line source
1 #ifndef GMATH_VEC_H_
2 #define GMATH_VEC_H_
4 #include <math.h>
6 namespace gph {
8 class Vector4;
9 class Matrix4x4;
11 class Vector3 {
12 public:
13 float x, y, z;
15 Vector3() : x(0), y(0), z(0) {}
16 Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
17 Vector3(const Vector4 &v);
19 inline void normalize();
20 inline float &operator[] (int idx);
21 inline const float &operator[] (int idx) const;
22 };
25 class Vector4 {
26 public:
27 float x, y, z, w;
29 Vector4() : x(0), y(0), z(0), w(0) {}
30 Vector4(float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {}
31 Vector4(const Vector3 &v);
33 inline void normalize();
34 inline float &operator[] (int idx);
35 inline const float &operator[] (int idx) const;
36 };
38 // ---- Vector3 functions ----
39 inline Vector3 operator +(const Vector3 &a, const Vector3 &b);
40 inline Vector3 operator -(const Vector3 &a, const Vector3 &b);
41 inline Vector3 operator *(const Vector3 &a, const Vector3 &b);
42 inline Vector3 operator /(const Vector3 &a, const Vector3 &b);
43 inline Vector3 operator *(const Vector3 &v, float s);
44 inline Vector3 operator *(float s, const Vector3 &v);
45 inline Vector3 operator /(const Vector3 &v, float s);
46 inline Vector3 operator /(float s, const Vector3 &v);
47 inline Vector3 &operator +=(Vector3 &a, const Vector3 &b);
48 inline Vector3 &operator -=(Vector3 &a, const Vector3 &b);
49 inline Vector3 &operator *=(Vector3 &a, const Vector3 &b);
50 inline Vector3 &operator /=(Vector3 &a, const Vector3 &b);
51 inline Vector3 &operator *=(Vector3 &v, float s);
52 inline Vector3 &operator /=(Vector3 &v, float s);
54 Vector3 operator *(const Vector3 &v, const Matrix4x4 &m);
55 Vector3 operator *(const Matrix4x4 &m, const Vector3 &v);
57 inline bool operator ==(const Vector3 &a, const Vector3 &b);
58 inline bool operator !=(const Vector3 &a, const Vector3 &b);
60 inline float dot(const Vector3 &a, const Vector3 &b);
61 inline Vector3 cross(const Vector3 &a, const Vector3 &b);
62 inline float length(const Vector3 &v);
63 inline float length_sq(const Vector3 &v);
64 inline Vector3 normalize(const Vector3 &v);
66 inline Vector3 reflect(const Vector3 &v, const Vector3 &n);
67 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior);
68 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior);
70 inline Vector3 distance(const Vector3 &a, const Vector3 &b);
71 inline Vector3 distance_sq(const Vector3 &a, const Vector3 &b);
72 inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng);
74 }
76 #endif /* GMATH_VEC_H_ */