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