1 inline void Vector4::normalize()
3 float len = (float)sqrt(x * x + y * y + z * z + w * w);
12 inline float &Vector4::operator[] (int idx)
14 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
17 inline const float &Vector4::operator[] (int idx) const
19 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
22 inline Vector4 operator -(const Vector4 &v)
24 return Vector4(-v.x, -v.y, -v.z, -v.w);
27 inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
29 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
32 inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
34 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
37 inline Vector4 operator *(const Vector4 &a, const Vector4 &b)
39 return Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
42 inline Vector4 operator /(const Vector4 &a, const Vector4 &b)
44 return Vector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
47 inline Vector4 operator *(const Vector4 &v, float s)
49 return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
52 inline Vector4 operator *(float s, const Vector4 &v)
54 return Vector4(s * v.x, s * v.y, s * v.z, s * v.w);
57 inline Vector4 operator /(const Vector4 &v, float s)
59 return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
62 inline Vector4 operator /(float s, const Vector4 &v)
64 return Vector4(s / v.x, s / v.y, s / v.z, s / v.w);
67 inline Vector4 &operator +=(Vector4 &a, const Vector4 &b)
76 inline Vector4 &operator -=(Vector4 &a, const Vector4 &b)
85 inline Vector4 &operator *=(Vector4 &a, const Vector4 &b)
94 inline Vector4 &operator /=(Vector4 &a, const Vector4 &b)
103 inline Vector4 &operator *=(Vector4 &v, float s)
112 inline Vector4 &operator /=(Vector4 &v, float s)
121 inline bool operator ==(const Vector4 &a, const Vector4 &b)
123 return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
126 inline bool operator !=(const Vector4 &a, const Vector4 &b)
131 inline float dot(const Vector4 &a, const Vector4 &b)
133 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
136 inline Vector4 cross(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3)
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);
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);
152 return Vector4(x, y, z, w);
155 inline float length(const Vector4 &v)
157 return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
160 inline float length_sq(const Vector4 &v)
162 return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
165 inline Vector4 normalize(const Vector4 &v)
167 float len = length(v);
172 return Vector4(v.x / len, v.y / len, v.z / len, v.w / len);
175 inline Vector4 reflect(const Vector4 &v, const Vector4 &n)
177 return v - n * dot(n, v) * 2.0;
180 inline Vector4 refract(const Vector4 &v, const Vector4 &n, float ior)
182 float ndotv = dot(n, v);
183 float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv);
187 return ior * v - (ior * ndotv + sqrt(k)) * n;
190 inline Vector4 refract(const Vector4 &v, const Vector4 &n, float from_ior, float to_ior)
192 if(to_ior == 0.0f) to_ior = 1.0f;
193 return refract(v, n, from_ior / to_ior);
196 inline float distance(const Vector4 &a, const Vector4 &b)
198 return length(a - b);
201 inline float distance_sq(const Vector4 &a, const Vector4 &b)
203 return length_sq(a - b);
206 inline Vector4 faceforward(const Vector4 &n, const Vector4 &vi, const Vector4 &ng)
208 return dot(ng, vi) < 0.0f ? n : -n;
211 inline Vector4 major(const Vector4 &v)
213 int m = major_idx(v);
219 inline int major_idx(const Vector4 &v)
221 if(fabs(v.x) >= fabs(v.y) && fabs(v.x) >= fabs(v.z) && fabs(v.x >= v.w)) {
224 if(fabs(v.y) >= fabs(v.z) && fabs(v.y) >= fabs(v.w)) {
227 if(fabs(v.z) >= fabs(v.w)) {
233 inline Vector4 proj_axis(const Vector4 &v, const Vector4 &axis)
235 return axis * dot(v, axis);
239 inline Vector4 rotate(const Vector4 &v, const Quaternion &q)
244 inline Vector4 rotate(const Vector4 &v, const Vector4 &axis, float angle)
249 inline Vector4 rotate(const Vector4 &v, const Vector4 &euler)