rayzor
view src/vmath.h @ 0:2a5340a6eee4
rayzor first commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 05 Apr 2014 08:46:27 +0300 |
parents | |
children | a826bf0fb169 |
line source
1 #ifndef VMATH_H_
2 #define VMATH_H_
4 #include <math.h>
5 #include "vmathmat.h"
7 class Vector3 {
8 public:
9 float x, y, z;
11 Vector3() : x(0), y(0), z(0) {}
12 Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
14 float length_sq() const { return x * x + y * y + z * z; }
15 float length() const { return sqrt(x * x + y * y + z * z); }
17 void normalize()
18 {
19 float len = length();
20 if(len != 0.0) {
21 x /= len;
22 y /= len;
23 z /= len;
24 }
25 }
27 float &operator [](int idx) { return idx == 2 ? z : (idx == 1 ? y : x); }
28 const float &operator [](int idx) const { return idx == 2 ? z : (idx == 1 ? y : x); }
29 };
31 inline Vector3 normalize(const Vector3 &v)
32 {
33 float len = v.length();
34 if(len != 0.0) {
35 return Vector3(v.x / len, v.y / len, v.z / len);
36 }
37 return v;
38 }
40 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
41 {
42 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
43 }
45 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
46 {
47 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
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 /(const Vector3 &v, float s)
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 Vector3 cross(const Vector3 &a, const Vector3 &b)
66 {
67 return Vector3(a.y * b.z - a.z * b.y,
68 a.z * b.z - a.x * b.z,
69 a.x * b.y - a.y * b.x);
70 }
72 inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v)
73 {
74 float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3];
75 float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3];
76 float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3];
77 return Vector3(x, y, z);
78 }
80 // ---- Vector4 ----
82 class Vector4 {
83 public:
84 float x, y, z, w;
86 Vector4() : x(0), y(0), z(0), w(1.0) {}
87 Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {}
88 Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {}
90 float length_sq() const { return x * x + y * y + z * z + w * w; }
91 float length() const { return sqrt(x * x + y * y + z * z + w * w); }
93 void normalize()
94 {
95 float len = length();
96 if(len != 0.0) {
97 x /= len;
98 y /= len;
99 z /= len;
100 w /= len;
101 }
102 }
104 float &operator [](int idx)
105 {
106 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
107 }
108 const float &operator [](int idx) const
109 {
110 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
111 }
112 };
114 inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
115 {
116 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
117 }
119 inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
120 {
121 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
122 }
124 inline Vector4 operator *(const Vector4 &v, float s)
125 {
126 return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
127 }
129 inline Vector4 operator /(const Vector4 &v, float s)
130 {
131 return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
132 }
134 inline float dot(const Vector4 &a, const Vector4 &b)
135 {
136 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
137 }
139 inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v)
140 {
141 float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3] * v.w;
142 float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3] * v.w;
143 float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3] * v.w;
144 float w = m.m[3][0] * v.x + m.m[3][1] * v.y + m.m[3][2] * v.z + m.m[3][3] * v.w;
145 return Vector4(x, y, z, w);
146 }
148 #endif // VMATH_H_