rayzor

view src/vmath.h @ 12:d94a69933a71

lots of stuff, can't remember
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 12 Apr 2014 23:28:24 +0300
parents a826bf0fb169
children be616b58df99
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 &a, const Vector3 &b)
51 {
52 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
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 Vector3 operator /(const Vector3 &v, float s)
61 {
62 return Vector3(v.x / s, v.y / s, v.z / s);
63 }
65 inline float dot(const Vector3 &a, const Vector3 &b)
66 {
67 return a.x * b.x + a.y * b.y + a.z * b.z;
68 }
70 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
71 {
72 return Vector3(a.y * b.z - a.z * b.y,
73 a.z * b.x - a.x * b.z,
74 a.x * b.y - a.y * b.x);
75 }
77 inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v)
78 {
79 float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3];
80 float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3];
81 float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3];
82 return Vector3(x, y, z);
83 }
85 // ---- Vector4 ----
87 class Vector4 {
88 public:
89 float x, y, z, w;
91 Vector4() : x(0), y(0), z(0), w(1.0) {}
92 Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {}
93 Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {}
95 float length_sq() const { return x * x + y * y + z * z + w * w; }
96 float length() const { return sqrt(x * x + y * y + z * z + w * w); }
98 void normalize()
99 {
100 float len = length();
101 if(len != 0.0) {
102 x /= len;
103 y /= len;
104 z /= len;
105 w /= len;
106 }
107 }
109 float &operator [](int idx)
110 {
111 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
112 }
113 const float &operator [](int idx) const
114 {
115 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
116 }
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 &a, const Vector4 &b)
125 {
126 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
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 Vector4 operator /(const Vector4 &v, float s)
135 {
136 return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
137 }
139 inline float dot(const Vector4 &a, const Vector4 &b)
140 {
141 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
142 }
144 inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v)
145 {
146 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;
147 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;
148 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;
149 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;
150 return Vector4(x, y, z, w);
151 }
153 #endif // VMATH_H_