rev |
line source |
nuclear@1
|
1 #ifndef VMATH_H_
|
nuclear@1
|
2 #define VMATH_H_
|
nuclear@1
|
3
|
nuclear@1
|
4 #include <math.h>
|
nuclear@1
|
5 #include "vmathmat.h"
|
nuclear@1
|
6
|
nuclear@1
|
7 class Vector3 {
|
nuclear@1
|
8 public:
|
nuclear@1
|
9 float x, y, z;
|
nuclear@1
|
10
|
nuclear@1
|
11 Vector3() : x(0), y(0), z(0) {}
|
nuclear@1
|
12 Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
|
nuclear@1
|
13
|
nuclear@1
|
14 float length_sq() const { return x * x + y * y + z * z; }
|
nuclear@1
|
15 float length() const { return sqrt(x * x + y * y + z * z); }
|
nuclear@1
|
16
|
nuclear@1
|
17 void normalize()
|
nuclear@1
|
18 {
|
nuclear@1
|
19 float len = length();
|
nuclear@1
|
20 if(len != 0.0) {
|
nuclear@1
|
21 x /= len;
|
nuclear@1
|
22 y /= len;
|
nuclear@1
|
23 z /= len;
|
nuclear@1
|
24 }
|
nuclear@1
|
25 }
|
nuclear@1
|
26
|
nuclear@1
|
27 float &operator [](int idx) { return idx == 2 ? z : (idx == 1 ? y : x); }
|
nuclear@1
|
28 const float &operator [](int idx) const { return idx == 2 ? z : (idx == 1 ? y : x); }
|
nuclear@1
|
29 };
|
nuclear@1
|
30
|
nuclear@1
|
31 inline Vector3 normalize(const Vector3 &v)
|
nuclear@1
|
32 {
|
nuclear@1
|
33 float len = v.length();
|
nuclear@1
|
34 if(len != 0.0) {
|
nuclear@1
|
35 return Vector3(v.x / len, v.y / len, v.z / len);
|
nuclear@1
|
36 }
|
nuclear@1
|
37 return v;
|
nuclear@1
|
38 }
|
nuclear@1
|
39
|
nuclear@1
|
40 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
|
nuclear@1
|
41 {
|
nuclear@1
|
42 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
|
nuclear@1
|
43 }
|
nuclear@1
|
44
|
nuclear@1
|
45 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
|
nuclear@1
|
46 {
|
nuclear@1
|
47 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
|
nuclear@1
|
48 }
|
nuclear@1
|
49
|
nuclear@1
|
50 inline Vector3 operator *(const Vector3 &v, float s)
|
nuclear@1
|
51 {
|
nuclear@1
|
52 return Vector3(v.x * s, v.y * s, v.z * s);
|
nuclear@1
|
53 }
|
nuclear@1
|
54
|
nuclear@1
|
55 inline Vector3 operator /(const Vector3 &v, float s)
|
nuclear@1
|
56 {
|
nuclear@1
|
57 return Vector3(v.x / s, v.y / s, v.z / s);
|
nuclear@1
|
58 }
|
nuclear@1
|
59
|
nuclear@1
|
60 inline float dot(const Vector3 &a, const Vector3 &b)
|
nuclear@1
|
61 {
|
nuclear@1
|
62 return a.x * b.x + a.y * b.y + a.z * b.z;
|
nuclear@1
|
63 }
|
nuclear@1
|
64
|
nuclear@1
|
65 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
|
nuclear@1
|
66 {
|
nuclear@1
|
67 return Vector3(a.y * b.z - a.z * b.y,
|
nuclear@1
|
68 a.z * b.z - a.x * b.z,
|
nuclear@1
|
69 a.x * b.y - a.y * b.x);
|
nuclear@1
|
70 }
|
nuclear@1
|
71
|
nuclear@1
|
72 inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v)
|
nuclear@1
|
73 {
|
nuclear@1
|
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];
|
nuclear@1
|
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];
|
nuclear@1
|
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];
|
nuclear@1
|
77 return Vector3(x, y, z);
|
nuclear@1
|
78 }
|
nuclear@1
|
79
|
nuclear@1
|
80 // ---- Vector4 ----
|
nuclear@1
|
81
|
nuclear@1
|
82 class Vector4 {
|
nuclear@1
|
83 public:
|
nuclear@1
|
84 float x, y, z, w;
|
nuclear@1
|
85
|
nuclear@1
|
86 Vector4() : x(0), y(0), z(0), w(1.0) {}
|
nuclear@1
|
87 Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {}
|
nuclear@1
|
88 Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {}
|
nuclear@1
|
89
|
nuclear@1
|
90 float length_sq() const { return x * x + y * y + z * z + w * w; }
|
nuclear@1
|
91 float length() const { return sqrt(x * x + y * y + z * z + w * w); }
|
nuclear@1
|
92
|
nuclear@1
|
93 void normalize()
|
nuclear@1
|
94 {
|
nuclear@1
|
95 float len = length();
|
nuclear@1
|
96 if(len != 0.0) {
|
nuclear@1
|
97 x /= len;
|
nuclear@1
|
98 y /= len;
|
nuclear@1
|
99 z /= len;
|
nuclear@1
|
100 w /= len;
|
nuclear@1
|
101 }
|
nuclear@1
|
102 }
|
nuclear@1
|
103
|
nuclear@1
|
104 float &operator [](int idx)
|
nuclear@1
|
105 {
|
nuclear@1
|
106 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
|
nuclear@1
|
107 }
|
nuclear@1
|
108 const float &operator [](int idx) const
|
nuclear@1
|
109 {
|
nuclear@1
|
110 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
|
nuclear@1
|
111 }
|
nuclear@1
|
112 };
|
nuclear@1
|
113
|
nuclear@1
|
114 inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
|
nuclear@1
|
115 {
|
nuclear@1
|
116 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
|
nuclear@1
|
117 }
|
nuclear@1
|
118
|
nuclear@1
|
119 inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
|
nuclear@1
|
120 {
|
nuclear@1
|
121 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
|
nuclear@1
|
122 }
|
nuclear@1
|
123
|
nuclear@1
|
124 inline Vector4 operator *(const Vector4 &v, float s)
|
nuclear@1
|
125 {
|
nuclear@1
|
126 return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
|
nuclear@1
|
127 }
|
nuclear@1
|
128
|
nuclear@1
|
129 inline Vector4 operator /(const Vector4 &v, float s)
|
nuclear@1
|
130 {
|
nuclear@1
|
131 return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
|
nuclear@1
|
132 }
|
nuclear@1
|
133
|
nuclear@1
|
134 inline float dot(const Vector4 &a, const Vector4 &b)
|
nuclear@1
|
135 {
|
nuclear@1
|
136 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
|
nuclear@1
|
137 }
|
nuclear@1
|
138
|
nuclear@1
|
139 inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v)
|
nuclear@1
|
140 {
|
nuclear@1
|
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;
|
nuclear@1
|
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;
|
nuclear@1
|
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;
|
nuclear@1
|
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;
|
nuclear@1
|
145 return Vector4(x, y, z, w);
|
nuclear@1
|
146 }
|
nuclear@1
|
147
|
nuclear@1
|
148 #endif // VMATH_H_
|