graphene

view src/gmath/vector.inl @ 5:2ce58d5309f0

swizzling
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Jul 2015 17:19:27 +0300
parents d30e24132b6e
children 9fbbc96e6fbe
line source
1 #include <math.h>
3 namespace gph {
5 #undef GPH_SWIZZLE2
6 #undef GPH_SWIZZLE3
7 #undef GPH_SWIZZLE4
8 #define GPH_SWIZZLE2(T, a, b) inline Vector2 T::a##b() const { return Vector2(a, b); }
9 #define GPH_SWIZZLE3(T, a, b, c) inline Vector3 T::a##b##c() const { return Vector3(a, b, c); }
10 #define GPH_SWIZZLE4(T, a, b, c, d) inline Vector4 T::a##b##c##d() const { return Vector4(a, b, c, d); }
12 // ---- Vector3 ----
14 inline void Vector3::normalize()
15 {
16 float len = (float)sqrt(x * x + y * y + z * z);
17 if(len != 0.0f) {
18 x /= len;
19 y /= len;
20 z /= len;
21 }
22 }
24 inline float &Vector3::operator[] (int idx)
25 {
26 return idx == 0 ? x : (idx == 1 ? y : z);
27 }
29 inline const float &Vector3::operator[] (int idx) const
30 {
31 return idx == 0 ? x : (idx == 1 ? y : z);
32 }
34 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
35 {
36 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
37 }
39 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
40 {
41 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
42 }
44 inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
45 {
46 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
47 }
49 inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
50 {
51 return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
52 }
54 inline Vector3 operator *(const Vector3 &v, float s)
55 {
56 return Vector3(v.x * s, v.y * s, v.z * s);
57 }
59 inline Vector3 operator *(float s, const Vector3 &v)
60 {
61 return Vector3(s * v.x, s * v.y, s * v.z);
62 }
64 inline Vector3 operator /(const Vector3 &v, float s)
65 {
66 return Vector3(v.x / s, v.y / s, v.z / s);
67 }
69 inline Vector3 operator /(float s, const Vector3 &v)
70 {
71 return Vector3(s / v.x, s / v.y, s / v.z);
72 }
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 }
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 }
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 }
98 inline Vector3 &operator /=(Vector3 &a, const Vector3 &b)
99 {
100 a.x /= b.x;
101 a.y /= b.y;
102 a.z /= b.z;
103 return a;
104 }
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 }
114 inline Vector3 &operator /=(Vector3 &v, float s)
115 {
116 v.x /= s;
117 v.y /= s;
118 v.z /= s;
119 return v;
120 }
122 inline bool operator ==(const Vector3 &a, const Vector3 &b)
123 {
124 return a.x == b.x && a.y == b.y && a.z == b.z;
125 }
127 inline bool operator !=(const Vector3 &a, const Vector3 &b)
128 {
129 return !(a == b);
130 }
132 inline float dot(const Vector3 &a, const Vector3 &b)
133 {
134 return a.x * b.x + a.y * b.y + a.z * b.z;
135 }
137 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
138 {
139 return Vector3(a.y * b.z - a.z * b.y,
140 a.z * b.x - a.x * b.z,
141 a.x * b.y - a.y * b.x);
142 }
144 inline float length(const Vector3 &v)
145 {
146 return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
147 }
149 inline float length_sq(const Vector3 &v)
150 {
151 return v.x * v.x + v.y * v.y + v.z * v.z;
152 }
154 inline Vector3 normalize(const Vector3 &v)
155 {
156 float len = length(v);
157 if(len == 0.0f) {
158 return v;
159 }
161 return Vector3(v.x / len, v.y / len, v.z / len);
162 }
164 inline Vector3 reflect(const Vector3 &v, const Vector3 &n)
165 {
166 return v - n * dot(n, v) * 2.0;
167 }
169 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior)
170 {
171 float ndotv = dot(n, v);
172 float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv);
173 if(k < 0.0f) {
174 return Vector3();
175 }
176 return ior * v - (ior * ndotv + sqrt(k)) * n;
177 }
179 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior)
180 {
181 if(to_ior == 0.0f) to_ior = 1.0f;
182 return refract(v, n, from_ior / to_ior);
183 }
185 inline Vector3 distance(const Vector3 &a, const Vector3 &b)
186 {
187 return length(a - b);
188 }
190 inline Vector3 distance_sq(const Vector3 &a, const Vector3 &b)
191 {
192 return length_sq(a - b);
193 }
195 inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng)
196 {
197 return dot(ng, i) < 0.0f ? n : -n;
198 }
200 GPH_VEC3_SWIZZLE
202 // ---- Vector4 ----
205 inline void Vector4::normalize()
206 {
207 float len = (float)sqrt(x * x + y * y + z * z + w * w);
208 if(len != 0.0f) {
209 x /= len;
210 y /= len;
211 z /= len;
212 w /= len;
213 }
214 }
216 inline float &Vector4::operator[] (int idx)
217 {
218 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
219 }
221 inline const float &Vector4::operator[] (int idx) const
222 {
223 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
224 }
226 GPH_VEC4_SWIZZLE
228 // ---- Vector2 ----
230 inline void Vector2::normalize()
231 {
232 float len = (float)sqrt(x * x + y * y);
233 if(len != 0.0f) {
234 x /= len;
235 y /= len;
236 }
237 }
239 inline float &Vector2::operator[] (int idx)
240 {
241 return idx == 0 ? x : y;
242 }
244 inline const float &Vector2::operator[] (int idx) const
245 {
246 return idx == 0 ? x : y;
247 }
249 GPH_VEC2_SWIZZLE
251 } // namespace gph