rayzor

annotate src/vmath.h @ 0:2a5340a6eee4

rayzor first commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Apr 2014 08:46:27 +0300
parents
children a826bf0fb169
rev   line source
nuclear@0 1 #ifndef VMATH_H_
nuclear@0 2 #define VMATH_H_
nuclear@0 3
nuclear@0 4 #include <math.h>
nuclear@0 5 #include "vmathmat.h"
nuclear@0 6
nuclear@0 7 class Vector3 {
nuclear@0 8 public:
nuclear@0 9 float x, y, z;
nuclear@0 10
nuclear@0 11 Vector3() : x(0), y(0), z(0) {}
nuclear@0 12 Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
nuclear@0 13
nuclear@0 14 float length_sq() const { return x * x + y * y + z * z; }
nuclear@0 15 float length() const { return sqrt(x * x + y * y + z * z); }
nuclear@0 16
nuclear@0 17 void normalize()
nuclear@0 18 {
nuclear@0 19 float len = length();
nuclear@0 20 if(len != 0.0) {
nuclear@0 21 x /= len;
nuclear@0 22 y /= len;
nuclear@0 23 z /= len;
nuclear@0 24 }
nuclear@0 25 }
nuclear@0 26
nuclear@0 27 float &operator [](int idx) { return idx == 2 ? z : (idx == 1 ? y : x); }
nuclear@0 28 const float &operator [](int idx) const { return idx == 2 ? z : (idx == 1 ? y : x); }
nuclear@0 29 };
nuclear@0 30
nuclear@0 31 inline Vector3 normalize(const Vector3 &v)
nuclear@0 32 {
nuclear@0 33 float len = v.length();
nuclear@0 34 if(len != 0.0) {
nuclear@0 35 return Vector3(v.x / len, v.y / len, v.z / len);
nuclear@0 36 }
nuclear@0 37 return v;
nuclear@0 38 }
nuclear@0 39
nuclear@0 40 inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
nuclear@0 41 {
nuclear@0 42 return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
nuclear@0 43 }
nuclear@0 44
nuclear@0 45 inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
nuclear@0 46 {
nuclear@0 47 return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
nuclear@0 48 }
nuclear@0 49
nuclear@0 50 inline Vector3 operator *(const Vector3 &v, float s)
nuclear@0 51 {
nuclear@0 52 return Vector3(v.x * s, v.y * s, v.z * s);
nuclear@0 53 }
nuclear@0 54
nuclear@0 55 inline Vector3 operator /(const Vector3 &v, float s)
nuclear@0 56 {
nuclear@0 57 return Vector3(v.x / s, v.y / s, v.z / s);
nuclear@0 58 }
nuclear@0 59
nuclear@0 60 inline float dot(const Vector3 &a, const Vector3 &b)
nuclear@0 61 {
nuclear@0 62 return a.x * b.x + a.y * b.y + a.z * b.z;
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 inline Vector3 cross(const Vector3 &a, const Vector3 &b)
nuclear@0 66 {
nuclear@0 67 return Vector3(a.y * b.z - a.z * b.y,
nuclear@0 68 a.z * b.z - a.x * b.z,
nuclear@0 69 a.x * b.y - a.y * b.x);
nuclear@0 70 }
nuclear@0 71
nuclear@0 72 inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v)
nuclear@0 73 {
nuclear@0 74 float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3];
nuclear@0 75 float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3];
nuclear@0 76 float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3];
nuclear@0 77 return Vector3(x, y, z);
nuclear@0 78 }
nuclear@0 79
nuclear@0 80 // ---- Vector4 ----
nuclear@0 81
nuclear@0 82 class Vector4 {
nuclear@0 83 public:
nuclear@0 84 float x, y, z, w;
nuclear@0 85
nuclear@0 86 Vector4() : x(0), y(0), z(0), w(1.0) {}
nuclear@0 87 Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {}
nuclear@0 88 Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {}
nuclear@0 89
nuclear@0 90 float length_sq() const { return x * x + y * y + z * z + w * w; }
nuclear@0 91 float length() const { return sqrt(x * x + y * y + z * z + w * w); }
nuclear@0 92
nuclear@0 93 void normalize()
nuclear@0 94 {
nuclear@0 95 float len = length();
nuclear@0 96 if(len != 0.0) {
nuclear@0 97 x /= len;
nuclear@0 98 y /= len;
nuclear@0 99 z /= len;
nuclear@0 100 w /= len;
nuclear@0 101 }
nuclear@0 102 }
nuclear@0 103
nuclear@0 104 float &operator [](int idx)
nuclear@0 105 {
nuclear@0 106 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
nuclear@0 107 }
nuclear@0 108 const float &operator [](int idx) const
nuclear@0 109 {
nuclear@0 110 return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
nuclear@0 111 }
nuclear@0 112 };
nuclear@0 113
nuclear@0 114 inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
nuclear@0 115 {
nuclear@0 116 return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
nuclear@0 117 }
nuclear@0 118
nuclear@0 119 inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
nuclear@0 120 {
nuclear@0 121 return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
nuclear@0 122 }
nuclear@0 123
nuclear@0 124 inline Vector4 operator *(const Vector4 &v, float s)
nuclear@0 125 {
nuclear@0 126 return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
nuclear@0 127 }
nuclear@0 128
nuclear@0 129 inline Vector4 operator /(const Vector4 &v, float s)
nuclear@0 130 {
nuclear@0 131 return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
nuclear@0 132 }
nuclear@0 133
nuclear@0 134 inline float dot(const Vector4 &a, const Vector4 &b)
nuclear@0 135 {
nuclear@0 136 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
nuclear@0 137 }
nuclear@0 138
nuclear@0 139 inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v)
nuclear@0 140 {
nuclear@0 141 float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3] * v.w;
nuclear@0 142 float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3] * v.w;
nuclear@0 143 float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3] * v.w;
nuclear@0 144 float w = m.m[3][0] * v.x + m.m[3][1] * v.y + m.m[3][2] * v.z + m.m[3][3] * v.w;
nuclear@0 145 return Vector4(x, y, z, w);
nuclear@0 146 }
nuclear@0 147
nuclear@0 148 #endif // VMATH_H_