fixed copying of library links in install target
[gph-math] / src / vector4.inl
1 /*
2 gph-math - math library for graphics programs
3 Copyright (C) 2016 John Tsiombikas <nuclear@member.fsf.org>
4
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.
9 */
10 inline void Vector4::normalize()
11 {
12         float len = (float)sqrt(x * x + y * y + z * z + w * w);
13         if(len != 0.0f) {
14                 x /= len;
15                 y /= len;
16                 z /= len;
17                 w /= len;
18         }
19 }
20
21 inline float &Vector4::operator[] (int idx)
22 {
23         return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
24 }
25
26 inline const float &Vector4::operator[] (int idx) const
27 {
28         return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
29 }
30
31 inline Vector4 operator -(const Vector4 &v)
32 {
33         return Vector4(-v.x, -v.y, -v.z, -v.w);
34 }
35
36 inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
37 {
38         return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
39 }
40
41 inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
42 {
43         return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
44 }
45
46 inline Vector4 operator *(const Vector4 &a, const Vector4 &b)
47 {
48         return Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
49 }
50
51 inline Vector4 operator /(const Vector4 &a, const Vector4 &b)
52 {
53         return Vector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
54 }
55
56 inline Vector4 operator *(const Vector4 &v, float s)
57 {
58         return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
59 }
60
61 inline Vector4 operator *(float s, const Vector4 &v)
62 {
63         return Vector4(s * v.x, s * v.y, s * v.z, s * v.w);
64 }
65
66 inline Vector4 operator /(const Vector4 &v, float s)
67 {
68         return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
69 }
70
71 inline Vector4 operator /(float s, const Vector4 &v)
72 {
73         return Vector4(s / v.x, s / v.y, s / v.z, s / v.w);
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 &a, const Vector4 &b)
104 {
105         a.x /= b.x;
106         a.y /= b.y;
107         a.z /= b.z;
108         a.w /= b.w;
109         return a;
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 Vector4 &operator /=(Vector4 &v, float s)
122 {
123         v.x /= s;
124         v.y /= s;
125         v.z /= s;
126         v.w /= s;
127         return v;
128 }
129
130 inline bool operator ==(const Vector4 &a, const Vector4 &b)
131 {
132         return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
133 }
134
135 inline bool operator !=(const Vector4 &a, const Vector4 &b)
136 {
137         return !(a == b);
138 }
139
140 inline float dot(const Vector4 &a, const Vector4 &b)
141 {
142         return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
143 }
144
145 inline Vector4 cross(const Vector4 &v1, const Vector4 &v2, const Vector4 &v3)
146 {
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);
154
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);
160
161     return Vector4(x, y, z, w);
162 }
163
164 inline float length(const Vector4 &v)
165 {
166         return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
167 }
168
169 inline float length_sq(const Vector4 &v)
170 {
171         return v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w;
172 }
173
174 inline Vector4 normalize(const Vector4 &v)
175 {
176         float len = length(v);
177         if(len == 0.0f) {
178                 return v;
179         }
180
181         return Vector4(v.x / len, v.y / len, v.z / len, v.w / len);
182 }
183
184 inline Vector4 reflect(const Vector4 &v, const Vector4 &n)
185 {
186         return v - n * dot(n, v) * 2.0;
187 }
188
189 inline Vector4 refract(const Vector4 &v, const Vector4 &n, float ior)
190 {
191         float ndotv = dot(n, v);
192         float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv);
193         if(k < 0.0f) {
194                 return Vector4();
195         }
196         return ior * v - (ior * ndotv + sqrt(k)) * n;
197 }
198
199 inline Vector4 refract(const Vector4 &v, const Vector4 &n, float from_ior, float to_ior)
200 {
201         if(to_ior == 0.0f) to_ior = 1.0f;
202         return refract(v, n, from_ior / to_ior);
203 }
204
205 inline float distance(const Vector4 &a, const Vector4 &b)
206 {
207         return length(a - b);
208 }
209
210 inline float distance_sq(const Vector4 &a, const Vector4 &b)
211 {
212         return length_sq(a - b);
213 }
214
215 inline Vector4 faceforward(const Vector4 &n, const Vector4 &vi, const Vector4 &ng)
216 {
217         return dot(ng, vi) < 0.0f ? n : -n;
218 }
219
220 inline Vector4 major(const Vector4 &v)
221 {
222         int m = major_idx(v);
223         Vector4 res;
224         res[m] = v[m];
225         return res;
226 }
227
228 inline int major_idx(const Vector4 &v)
229 {
230         if(fabs(v.x) >= fabs(v.y) && fabs(v.x) >= fabs(v.z) && fabs(v.x >= v.w)) {
231                 return 0;
232         }
233         if(fabs(v.y) >= fabs(v.z) && fabs(v.y) >= fabs(v.w)) {
234                 return 1;
235         }
236         if(fabs(v.z) >= fabs(v.w)) {
237                 return 2;
238         }
239         return 3;
240 }
241
242 inline Vector4 proj_axis(const Vector4 &v, const Vector4 &axis)
243 {
244         return axis * dot(v, axis);
245 }
246
247
248 inline Vector4 rotate(const Vector4 &v, const Quaternion &q)
249 {
250         return v;       // TODO
251 }
252
253 inline Vector4 rotate(const Vector4 &v, const Vector4 &axis, float angle)
254 {
255         return v;       // TODO
256 }
257
258 inline Vector4 rotate(const Vector4 &v, const Vector4 &euler)
259 {
260         return v;       // TODO
261 }