Vector4 inline definitions, and rearrangement into multiple inl files
[gph-math] / src / vector4.inl
1 inline void Vector4::normalize()
2 {
3         float len = (float)sqrt(x * x + y * y + z * z + w * w);
4         if(len != 0.0f) {
5                 x /= len;
6                 y /= len;
7                 z /= len;
8                 w /= len;
9         }
10 }
11
12 inline float &Vector4::operator[] (int idx)
13 {
14         return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
15 }
16
17 inline const float &Vector4::operator[] (int idx) const
18 {
19         return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
20 }
21
22 inline Vector4 operator -(const Vector4 &v)
23 {
24         return Vector4(-v.x, -v.y, -v.z, -v.w);
25 }
26
27 inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
28 {
29         return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
30 }
31
32 inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
33 {
34         return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
35 }
36
37 inline Vector4 operator *(const Vector4 &a, const Vector4 &b)
38 {
39         return Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
40 }
41
42 inline Vector4 operator /(const Vector4 &a, const Vector4 &b)
43 {
44         return Vector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
45 }
46
47 inline Vector4 operator *(const Vector4 &v, float s)
48 {
49         return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
50 }
51
52 inline Vector4 operator *(float s, const Vector4 &v)
53 {
54         return Vector4(s * v.x, s * v.y, s * v.z, s * v.w);
55 }
56
57 inline Vector4 operator /(const Vector4 &v, float s)
58 {
59         return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
60 }
61
62 inline Vector4 operator /(float s, const Vector4 &v)
63 {
64         return Vector4(s / v.x, s / v.y, s / v.z, s / v.w);
65 }
66
67 inline Vector4 &operator +=(Vector4 &a, const Vector4 &b)
68 {
69         a.x += b.x;
70         a.y += b.y;
71         a.z += b.z;
72         a.w += b.w;
73         return a;
74 }
75
76 inline Vector4 &operator -=(Vector4 &a, const Vector4 &b)
77 {
78         a.x -= b.x;
79         a.y -= b.y;
80         a.z -= b.z;
81         a.w -= b.w;
82         return a;
83 }
84
85 inline Vector4 &operator *=(Vector4 &a, const Vector4 &b)
86 {
87         a.x *= b.x;
88         a.y *= b.y;
89         a.z *= b.z;
90         a.w *= b.w;
91         return a;
92 }
93
94 inline Vector4 &operator /=(Vector4 &a, const Vector4 &b)
95 {
96         a.x /= b.x;
97         a.y /= b.y;
98         a.z /= b.z;
99         a.w /= b.w;
100         return a;
101 }
102
103 inline Vector4 &operator *=(Vector4 &v, float s)
104 {
105         v.x *= s;
106         v.y *= s;
107         v.z *= s;
108         v.w *= s;
109         return v;
110 }
111
112 inline Vector4 &operator /=(Vector4 &v, float s)
113 {
114         v.x /= s;
115         v.y /= s;
116         v.z /= s;
117         v.w /= s;
118         return v;
119 }
120
121 inline bool operator ==(const Vector4 &a, const Vector4 &b)
122 {
123         return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
124 }
125
126 inline bool operator !=(const Vector4 &a, const Vector4 &b)
127 {
128         return !(a == b);
129 }
130
131 inline float dot(const Vector4 &a, const Vector4 &b)
132 {
133         return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
134 }
135
136 inline Vector4 cross(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3)
137 {
138     /* Calculate intermediate values. */
139     float a = (v2.x * v3.y) - (v2.y * v3.x);
140     float b = (v2.x * v3.z) - (v2.z * v3.x);
141     float c = (v2.x * v3.w) - (v2.w * v3.x);
142     float d = (v2.y * v3.z) - (v2.z * v3.y);
143     float e = (v2.y * v3.w) - (v2.w * v3.y);
144     float f = (v2.z * v3.w) - (v2.w * v3.z);
145
146     /* Calculate the result-vector components. */
147     float x =   (v1.y * f) - (v1.z * e) + (v1.w * d);
148     float y = - (v1.x * f) + (v1.z * c) - (v1.w * b);
149     float z =   (v1.x * e) - (v1.y * c) + (v1.w * a);
150     float w = - (v1.x * d) + (v1.y * b) - (v1.z * a);
151
152     return Vector4(x, y, z, w);
153 }
154
155 inline float length(const Vector4 &v)
156 {
157         return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
158 }
159
160 inline float length_sq(const Vector4 &v)
161 {
162         return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
163 }
164
165 inline Vector4 normalize(const Vector4 &v)
166 {
167         float len = length(v);
168         if(len == 0.0f) {
169                 return v;
170         }
171
172         return Vector4(v.x / len, v.y / len, v.z / len, v.w / len);
173 }
174
175 inline Vector4 reflect(const Vector4 &v, const Vector4 &n)
176 {
177         return v - n * dot(n, v) * 2.0;
178 }
179
180 inline Vector4 refract(const Vector4 &v, const Vector4 &n, float ior)
181 {
182         float ndotv = dot(n, v);
183         float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv);
184         if(k < 0.0f) {
185                 return Vector4();
186         }
187         return ior * v - (ior * ndotv + sqrt(k)) * n;
188 }
189
190 inline Vector4 refract(const Vector4 &v, const Vector4 &n, float from_ior, float to_ior)
191 {
192         if(to_ior == 0.0f) to_ior = 1.0f;
193         return refract(v, n, from_ior / to_ior);
194 }
195
196 inline float distance(const Vector4 &a, const Vector4 &b)
197 {
198         return length(a - b);
199 }
200
201 inline float distance_sq(const Vector4 &a, const Vector4 &b)
202 {
203         return length_sq(a - b);
204 }
205
206 inline Vector4 faceforward(const Vector4 &n, const Vector4 &vi, const Vector4 &ng)
207 {
208         return dot(ng, vi) < 0.0f ? n : -n;
209 }
210
211 inline Vector4 major(const Vector4 &v)
212 {
213         int m = major_idx(v);
214         Vector4 res;
215         res[m] = v[m];
216         return res;
217 }
218
219 inline int major_idx(const Vector4 &v)
220 {
221         if(fabs(v.x) >= fabs(v.y) && fabs(v.x) >= fabs(v.z) && fabs(v.x >= v.w)) {
222                 return 0;
223         }
224         if(fabs(v.y) >= fabs(v.z) && fabs(v.y) >= fabs(v.w)) {
225                 return 1;
226         }
227         if(fabs(v.z) >= fabs(v.w)) {
228                 return 2;
229         }
230         return 3;
231 }
232
233 inline Vector4 proj_axis(const Vector4 &v, const Vector4 &axis)
234 {
235         return axis * dot(v, axis);
236 }
237
238
239 inline Vector4 rotate(const Vector4 &v, const Quaternion &q)
240 {
241         return v;       // TODO
242 }
243
244 inline Vector4 rotate(const Vector4 &v, const Vector4 &axis, float angle)
245 {
246         return v;       // TODO
247 }
248
249 inline Vector4 rotate(const Vector4 &v, const Vector4 &euler)
250 {
251         return v;       // TODO
252 }