2 gph-math - math library for graphics programs
3 Copyright (C) 2016 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software. Feel free to use, modify, and/or redistribute
6 it under the terms of the MIT/X11 license. See LICENSE for details.
7 If you intend to redistribute parts of the code without the LICENSE file
8 replace this paragraph with the full contents of the LICENSE file.
10 inline void Vector4::normalize()
12 float len = (float)sqrt(x * x + y * y + z * z + w * w);
21 inline float &Vector4::operator[] (int idx)
23 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
26 inline const float &Vector4::operator[] (int idx) const
28 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
31 inline Vector4 operator -(const Vector4 &v)
33 return Vector4(-v.x, -v.y, -v.z, -v.w);
36 inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
38 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
41 inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
43 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
46 inline Vector4 operator *(const Vector4 &a, const Vector4 &b)
48 return Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
51 inline Vector4 operator /(const Vector4 &a, const Vector4 &b)
53 return Vector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
56 inline Vector4 operator *(const Vector4 &v, float s)
58 return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
61 inline Vector4 operator *(float s, const Vector4 &v)
63 return Vector4(s * v.x, s * v.y, s * v.z, s * v.w);
66 inline Vector4 operator /(const Vector4 &v, float s)
68 return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
71 inline Vector4 operator /(float s, const Vector4 &v)
73 return Vector4(s / v.x, s / v.y, s / v.z, s / v.w);
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 &a, const Vector4 &b)
112 inline Vector4 &operator *=(Vector4 &v, float s)
121 inline Vector4 &operator /=(Vector4 &v, float s)
130 inline bool operator ==(const Vector4 &a, const Vector4 &b)
132 return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
135 inline bool operator !=(const Vector4 &a, const Vector4 &b)
140 inline float dot(const Vector4 &a, const Vector4 &b)
142 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
145 inline Vector4 cross(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3)
147 /* Calculate intermediate values. */
148 float a = (v2.x * v3.y) - (v2.y * v3.x);
149 float b = (v2.x * v3.z) - (v2.z * v3.x);
150 float c = (v2.x * v3.w) - (v2.w * v3.x);
151 float d = (v2.y * v3.z) - (v2.z * v3.y);
152 float e = (v2.y * v3.w) - (v2.w * v3.y);
153 float f = (v2.z * v3.w) - (v2.w * v3.z);
155 /* Calculate the result-vector components. */
156 float x = (v1.y * f) - (v1.z * e) + (v1.w * d);
157 float y = - (v1.x * f) + (v1.z * c) - (v1.w * b);
158 float z = (v1.x * e) - (v1.y * c) + (v1.w * a);
159 float w = - (v1.x * d) + (v1.y * b) - (v1.z * a);
161 return Vector4(x, y, z, w);
164 inline float length(const Vector4 &v)
166 return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
169 inline float length_sq(const Vector4 &v)
171 return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
174 inline Vector4 normalize(const Vector4 &v)
176 float len = length(v);
181 return Vector4(v.x / len, v.y / len, v.z / len, v.w / len);
184 inline Vector4 reflect(const Vector4 &v, const Vector4 &n)
186 return v - n * dot(n, v) * 2.0;
189 inline Vector4 refract(const Vector4 &v, const Vector4 &n, float ior)
191 float ndotv = dot(n, v);
192 float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv);
196 return ior * v - (ior * ndotv + sqrt(k)) * n;
199 inline Vector4 refract(const Vector4 &v, const Vector4 &n, float from_ior, float to_ior)
201 if(to_ior == 0.0f) to_ior = 1.0f;
202 return refract(v, n, from_ior / to_ior);
205 inline float distance(const Vector4 &a, const Vector4 &b)
207 return length(a - b);
210 inline float distance_sq(const Vector4 &a, const Vector4 &b)
212 return length_sq(a - b);
215 inline Vector4 faceforward(const Vector4 &n, const Vector4 &vi, const Vector4 &ng)
217 return dot(ng, vi) < 0.0f ? n : -n;
220 inline Vector4 major(const Vector4 &v)
222 int m = major_idx(v);
228 inline int major_idx(const Vector4 &v)
230 if(fabs(v.x) >= fabs(v.y) && fabs(v.x) >= fabs(v.z) && fabs(v.x >= v.w)) {
233 if(fabs(v.y) >= fabs(v.z) && fabs(v.y) >= fabs(v.w)) {
236 if(fabs(v.z) >= fabs(v.w)) {
242 inline Vector4 proj_axis(const Vector4 &v, const Vector4 &axis)
244 return axis * dot(v, axis);
248 inline Vector4 rotate(const Vector4 &v, const Quaternion &q)
253 inline Vector4 rotate(const Vector4 &v, const Vector4 &axis, float angle)
258 inline Vector4 rotate(const Vector4 &v, const Vector4 &euler)