## graphene

### view src/gmath/vector.h @ 6:9fbbc96e6fbe

foo
author John Tsiombikas Fri, 31 Jul 2015 04:59:28 +0300 2ce58d5309f0
line source
1 #ifndef GMATH_VEC_H_
2 #define GMATH_VEC_H_
4 #include <math.h>
5 #include "swizzle.h"
7 namespace gph {
9 #define GPH_SWIZZLE2(T, a, b) inline Vector2 a##b() const;
10 #define GPH_SWIZZLE3(T, a, b, c) inline Vector3 a##b##c() const;
11 #define GPH_SWIZZLE4(T, a, b, c, d) inline Vector4 a##b##c##d() const;
13 class Vector3;
14 class Vector4;
15 class Matrix4x4;
16 class Quaternion;
18 class Vector2 {
19 public:
20 float x, y;
22 Vector2() : x(0), y(0) {}
23 Vector2(float x_, float y_) : x(x_), y(y_) {}
24 Vector2(const Vector3 &v);
26 inline void normalize();
27 inline float &operator[] (int idx);
28 inline const float &operator[] (int idx) const;
30 GPH_VEC2_SWIZZLE
31 };
33 class Vector3 {
34 public:
35 float x, y, z;
37 Vector3() : x(0), y(0), z(0) {}
38 Vector3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
39 Vector3(const Vector4 &v);
41 inline void normalize();
42 inline float &operator[] (int idx);
43 inline const float &operator[] (int idx) const;
45 GPH_VEC3_SWIZZLE
46 };
49 class Vector4 {
50 public:
51 float x, y, z, w;
53 Vector4() : x(0), y(0), z(0), w(0) {}
54 Vector4(float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {}
55 Vector4(const Vector3 &v);
57 inline void normalize();
58 inline float &operator[] (int idx);
59 inline const float &operator[] (int idx) const;
61 GPH_VEC4_SWIZZLE
62 };
64 // ---- Vector3 functions ----
65 inline Vector3 operator +(const Vector3 &a, const Vector3 &b);
66 inline Vector3 operator -(const Vector3 &a, const Vector3 &b);
67 inline Vector3 operator *(const Vector3 &a, const Vector3 &b);
68 inline Vector3 operator /(const Vector3 &a, const Vector3 &b);
69 inline Vector3 operator *(const Vector3 &v, float s);
70 inline Vector3 operator *(float s, const Vector3 &v);
71 inline Vector3 operator /(const Vector3 &v, float s);
72 inline Vector3 operator /(float s, const Vector3 &v);
73 inline Vector3 &operator +=(Vector3 &a, const Vector3 &b);
74 inline Vector3 &operator -=(Vector3 &a, const Vector3 &b);
75 inline Vector3 &operator *=(Vector3 &a, const Vector3 &b);
76 inline Vector3 &operator /=(Vector3 &a, const Vector3 &b);
77 inline Vector3 &operator *=(Vector3 &v, float s);
78 inline Vector3 &operator /=(Vector3 &v, float s);
80 Vector3 operator *(const Vector3 &v, const Matrix4x4 &m);
81 Vector3 operator *(const Matrix4x4 &m, const Vector3 &v);
83 inline bool operator ==(const Vector3 &a, const Vector3 &b);
84 inline bool operator !=(const Vector3 &a, const Vector3 &b);
86 inline float dot(const Vector3 &a, const Vector3 &b);
87 inline Vector3 cross(const Vector3 &a, const Vector3 &b);
88 inline float length(const Vector3 &v);
89 inline float length_sq(const Vector3 &v);
90 inline Vector3 normalize(const Vector3 &v);
92 inline Vector3 reflect(const Vector3 &v, const Vector3 &n);
93 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior);
94 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior);
96 inline Vector3 distance(const Vector3 &a, const Vector3 &b);
97 inline Vector3 distance_sq(const Vector3 &a, const Vector3 &b);
98 inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng);
100 inline Vector3 major(const Vector3 &v);
101 inline int major_idx(const Vector3 &v);
102 inline Vector3 proj_axis(const Vector3 &v, const Vector3 &axis);
104 inline Vector3 rotate(const Vector3 &v, const Quaternion &q);
105 inline Vector3 rotate(const Vector3 &v, const Vector3 &axis, float angle);
106 inline Vector3 rotate(const Vector3 &v, const Vector3 &euler);
108 }
110 #endif /* GMATH_VEC_H_ */