## graphene

### annotate src/gmath/vector.inl @ 6:9fbbc96e6fbe

foo
author John Tsiombikas Fri, 31 Jul 2015 04:59:28 +0300 2ce58d5309f0
rev   line source
nuclear@4 1 #include <math.h>
nuclear@4 2
nuclear@4 3 namespace gph {
nuclear@4 4
nuclear@5 5 #undef GPH_SWIZZLE2
nuclear@5 6 #undef GPH_SWIZZLE3
nuclear@5 7 #undef GPH_SWIZZLE4
nuclear@5 8 #define GPH_SWIZZLE2(T, a, b) inline Vector2 T::a##b() const { return Vector2(a, b); }
nuclear@5 9 #define GPH_SWIZZLE3(T, a, b, c) inline Vector3 T::a##b##c() const { return Vector3(a, b, c); }
nuclear@5 10 #define GPH_SWIZZLE4(T, a, b, c, d) inline Vector4 T::a##b##c##d() const { return Vector4(a, b, c, d); }
nuclear@5 11
nuclear@4 12 // ---- Vector3 ----
nuclear@4 13
nuclear@4 14 inline void Vector3::normalize()
nuclear@4 15 {
nuclear@4 16 float len = (float)sqrt(x * x + y * y + z * z);
nuclear@4 17 if(len != 0.0f) {
nuclear@4 18 x /= len;
nuclear@4 19 y /= len;
nuclear@4 20 z /= len;
nuclear@4 21 }
nuclear@4 22 }
nuclear@4 23
nuclear@4 24 inline float &Vector3::operator[] (int idx)
nuclear@4 25 {
nuclear@4 26 return idx == 0 ? x : (idx == 1 ? y : z);
nuclear@4 27 }
nuclear@4 28
nuclear@4 29 inline const float &Vector3::operator[] (int idx) const
nuclear@4 30 {
nuclear@4 31 return idx == 0 ? x : (idx == 1 ? y : z);
nuclear@4 32 }
nuclear@4 33
nuclear@4 34 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
nuclear@4 35 {
nuclear@4 36 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
nuclear@4 37 }
nuclear@4 38
nuclear@4 39 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
nuclear@4 40 {
nuclear@4 41 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
nuclear@4 42 }
nuclear@4 43
nuclear@4 44 inline Vector3 operator *(const Vector3 &a, const Vector3 &b)
nuclear@4 45 {
nuclear@4 46 return Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
nuclear@4 47 }
nuclear@4 48
nuclear@4 49 inline Vector3 operator /(const Vector3 &a, const Vector3 &b)
nuclear@4 50 {
nuclear@4 51 return Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
nuclear@4 52 }
nuclear@4 53
nuclear@4 54 inline Vector3 operator *(const Vector3 &v, float s)
nuclear@4 55 {
nuclear@4 56 return Vector3(v.x * s, v.y * s, v.z * s);
nuclear@4 57 }
nuclear@4 58
nuclear@4 59 inline Vector3 operator *(float s, const Vector3 &v)
nuclear@4 60 {
nuclear@4 61 return Vector3(s * v.x, s * v.y, s * v.z);
nuclear@4 62 }
nuclear@4 63
nuclear@4 64 inline Vector3 operator /(const Vector3 &v, float s)
nuclear@4 65 {
nuclear@4 66 return Vector3(v.x / s, v.y / s, v.z / s);
nuclear@4 67 }
nuclear@4 68
nuclear@4 69 inline Vector3 operator /(float s, const Vector3 &v)
nuclear@4 70 {
nuclear@4 71 return Vector3(s / v.x, s / v.y, s / v.z);
nuclear@4 72 }
nuclear@4 73
nuclear@4 74 inline Vector3 &operator +=(Vector3 &a, const Vector3 &b)
nuclear@4 75 {
nuclear@4 76 a.x += b.x;
nuclear@4 77 a.y += b.y;
nuclear@4 78 a.z += b.z;
nuclear@4 79 return a;
nuclear@4 80 }
nuclear@4 81
nuclear@4 82 inline Vector3 &operator -=(Vector3 &a, const Vector3 &b)
nuclear@4 83 {
nuclear@4 84 a.x -= b.x;
nuclear@4 85 a.y -= b.y;
nuclear@4 86 a.z -= b.z;
nuclear@4 87 return a;
nuclear@4 88 }
nuclear@4 89
nuclear@4 90 inline Vector3 &operator *=(Vector3 &a, const Vector3 &b)
nuclear@4 91 {
nuclear@4 92 a.x *= b.x;
nuclear@4 93 a.y *= b.y;
nuclear@4 94 a.z *= b.z;
nuclear@4 95 return a;
nuclear@4 96 }
nuclear@4 97
nuclear@4 98 inline Vector3 &operator /=(Vector3 &a, const Vector3 &b)
nuclear@4 99 {
nuclear@4 100 a.x /= b.x;
nuclear@4 101 a.y /= b.y;
nuclear@4 102 a.z /= b.z;
nuclear@4 103 return a;
nuclear@4 104 }
nuclear@4 105
nuclear@4 106 inline Vector3 &operator *=(Vector3 &v, float s)
nuclear@4 107 {
nuclear@4 108 v.x *= s;
nuclear@4 109 v.y *= s;
nuclear@4 110 v.z *= s;
nuclear@4 111 return v;
nuclear@4 112 }
nuclear@4 113
nuclear@4 114 inline Vector3 &operator /=(Vector3 &v, float s)
nuclear@4 115 {
nuclear@4 116 v.x /= s;
nuclear@4 117 v.y /= s;
nuclear@4 118 v.z /= s;
nuclear@4 119 return v;
nuclear@4 120 }
nuclear@4 121
nuclear@4 122 inline bool operator ==(const Vector3 &a, const Vector3 &b)
nuclear@4 123 {
nuclear@4 124 return a.x == b.x && a.y == b.y && a.z == b.z;
nuclear@4 125 }
nuclear@4 126
nuclear@4 127 inline bool operator !=(const Vector3 &a, const Vector3 &b)
nuclear@4 128 {
nuclear@4 129 return !(a == b);
nuclear@4 130 }
nuclear@4 131
nuclear@4 132 inline float dot(const Vector3 &a, const Vector3 &b)
nuclear@4 133 {
nuclear@4 134 return a.x * b.x + a.y * b.y + a.z * b.z;
nuclear@4 135 }
nuclear@4 136
nuclear@4 137 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
nuclear@4 138 {
nuclear@4 139 return Vector3(a.y * b.z - a.z * b.y,
nuclear@4 140 a.z * b.x - a.x * b.z,
nuclear@4 141 a.x * b.y - a.y * b.x);
nuclear@4 142 }
nuclear@4 143
nuclear@4 144 inline float length(const Vector3 &v)
nuclear@4 145 {
nuclear@4 146 return (float)sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
nuclear@4 147 }
nuclear@4 148
nuclear@4 149 inline float length_sq(const Vector3 &v)
nuclear@4 150 {
nuclear@4 151 return v.x * v.x + v.y * v.y + v.z * v.z;
nuclear@4 152 }
nuclear@4 153
nuclear@4 154 inline Vector3 normalize(const Vector3 &v)
nuclear@4 155 {
nuclear@4 156 float len = length(v);
nuclear@4 157 if(len == 0.0f) {
nuclear@4 158 return v;
nuclear@4 159 }
nuclear@4 160
nuclear@4 161 return Vector3(v.x / len, v.y / len, v.z / len);
nuclear@4 162 }
nuclear@4 163
nuclear@4 164 inline Vector3 reflect(const Vector3 &v, const Vector3 &n)
nuclear@4 165 {
nuclear@4 166 return v - n * dot(n, v) * 2.0;
nuclear@4 167 }
nuclear@4 168
nuclear@4 169 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float ior)
nuclear@4 170 {
nuclear@4 171 float ndotv = dot(n, v);
nuclear@4 172 float k = 1.0f - ior * ior * (1.0f - ndotv * ndotv);
nuclear@4 173 if(k < 0.0f) {
nuclear@4 174 return Vector3();
nuclear@4 175 }
nuclear@4 176 return ior * v - (ior * ndotv + sqrt(k)) * n;
nuclear@4 177 }
nuclear@4 178
nuclear@4 179 inline Vector3 refract(const Vector3 &v, const Vector3 &n, float from_ior, float to_ior)
nuclear@4 180 {
nuclear@4 181 if(to_ior == 0.0f) to_ior = 1.0f;
nuclear@4 182 return refract(v, n, from_ior / to_ior);
nuclear@4 183 }
nuclear@4 184
nuclear@4 185 inline Vector3 distance(const Vector3 &a, const Vector3 &b)
nuclear@4 186 {
nuclear@4 187 return length(a - b);
nuclear@4 188 }
nuclear@4 189
nuclear@4 190 inline Vector3 distance_sq(const Vector3 &a, const Vector3 &b)
nuclear@4 191 {
nuclear@4 192 return length_sq(a - b);
nuclear@4 193 }
nuclear@4 194
nuclear@4 195 inline Vector3 faceforward(const Vector3 &n, const Vector3 &vi, const Vector3 &ng)
nuclear@4 196 {
nuclear@4 197 return dot(ng, i) < 0.0f ? n : -n;
nuclear@4 198 }
nuclear@4 199
nuclear@6 200 inline Vector3 major(const Vector3 &v)
nuclear@6 201 {
nuclear@6 202 int m = major_idx(v);
nuclear@6 203 Vector3 res;
nuclear@6 204 res[m] = v[m];
nuclear@6 205 return res;
nuclear@6 206 }
nuclear@6 207
nuclear@6 208 inline int major_idx(const Vector3 &v)
nuclear@6 209 {
nuclear@6 210 return fabs(v.x) >= fabs(v.y) && fabs(v.x) > fabs(v.z) ? 0 :
nuclear@6 211 (fabs(v.y) >= fabs(v.z) ? 1 : 2);
nuclear@6 212 }
nuclear@6 213
nuclear@6 214 inline Vector3 proj_axis(const Vector3 &v, const Vector3 &axis)
nuclear@6 215 {
nuclear@6 216 return axis * dot(v, axis);
nuclear@6 217 }
nuclear@6 218
nuclear@6 219
nuclear@6 220 inline Vector3 rotate(const Vector3 &v, const Quaternion &q)
nuclear@6 221 {
nuclear@6 222 return v; // TODO
nuclear@6 223 }
nuclear@6 224
nuclear@6 225 inline Vector3 rotate(const Vector3 &v, const Vector3 &axis, float angle)
nuclear@6 226 {
nuclear@6 227 return v; // TODO
nuclear@6 228 }
nuclear@6 229
nuclear@6 230 inline Vector3 rotate(const Vector3 &v, const Vector3 &euler)
nuclear@6 231 {
nuclear@6 232 return v; // TODO
nuclear@6 233 }
nuclear@6 234
nuclear@6 235
nuclear@5 236 GPH_VEC3_SWIZZLE
nuclear@5 237
nuclear@4 238 // ---- Vector4 ----
nuclear@4 239
nuclear@4 240
nuclear@4 241 inline void Vector4::normalize()
nuclear@4 242 {
nuclear@4 243 float len = (float)sqrt(x * x + y * y + z * z + w * w);
nuclear@4 244 if(len != 0.0f) {
nuclear@4 245 x /= len;
nuclear@4 246 y /= len;
nuclear@4 247 z /= len;
nuclear@4 248 w /= len;
nuclear@4 249 }
nuclear@4 250 }
nuclear@4 251
nuclear@4 252 inline float &Vector4::operator[] (int idx)
nuclear@4 253 {
nuclear@4 254 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
nuclear@4 255 }
nuclear@4 256
nuclear@4 257 inline const float &Vector4::operator[] (int idx) const
nuclear@4 258 {
nuclear@4 259 return idx == 0 ? x : (idx == 1 ? y : (idx == 2 ? z : w));
nuclear@4 260 }
nuclear@4 261
nuclear@5 262 GPH_VEC4_SWIZZLE
nuclear@5 263
nuclear@5 264 // ---- Vector2 ----
nuclear@5 265
nuclear@5 266 inline void Vector2::normalize()
nuclear@5 267 {
nuclear@5 268 float len = (float)sqrt(x * x + y * y);
nuclear@5 269 if(len != 0.0f) {
nuclear@5 270 x /= len;
nuclear@5 271 y /= len;
nuclear@5 272 }
nuclear@5 273 }
nuclear@5 274
nuclear@5 275 inline float &Vector2::operator[] (int idx)
nuclear@5 276 {
nuclear@5 277 return idx == 0 ? x : y;
nuclear@5 278 }
nuclear@5 279
nuclear@5 280 inline const float &Vector2::operator[] (int idx) const
nuclear@5 281 {
nuclear@5 282 return idx == 0 ? x : y;
nuclear@5 283 }
nuclear@5 284
nuclear@5 285 GPH_VEC2_SWIZZLE
nuclear@5 286
nuclear@4 287 } // namespace gph