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