#ifndef VEC_H #include "util.h" #include // STRUCTURE struct Vector { inline Vector(void) : x(0), y(0), z(0), w(0) {} inline Vector(float x, float y, float z) : x(x), y(y), z(z), w(0) {} union { float e[4]; struct { float x, y, z, w; }; }; }; // OPERATORS // -v inline Vector operator-(Vector const &v) { Vector result; result.x = -v.x; result.y = -v.y; result.z = -v.z; return result; } // v1 - v2 inline Vector operator-(Vector const &v1, Vector const &v2) { Vector result; result.x = v1.x - v2.x; result.y = v1.y - v2.y; result.z = v1.z - v2.z; return result; } // v1 + v2 inline Vector operator+(Vector const &v1, Vector const &v2) { Vector result; result.x = v1.x + v2.x; result.y = v1.y + v2.y; result.z = v1.z + v2.z; return result; } // v1 += v2 inline Vector &operator+=(Vector &v1, Vector const &v2) { v1 = v1 + v2; return v1; } // f * v inline Vector operator*(float f, Vector const &v) { Vector result; result.x = v.x * f; result.y = v.y * f; result.z = v.z * f; return result; } // v / f inline Vector operator/(Vector const &v, float f) { Vector result; float inverse = 1.0f / f; result.x = v.x * inverse; result.y = v.y * inverse; result.z = v.z * inverse; return result; } // v /= f inline Vector &operator/=(Vector &v, float f) { v = v / f; return v; } // PUBLIC FUNCTIONS inline float VectorLength(Vector &v) { float result = sqrtf(v.x * v.x + v.y * v.y + v.z * v.z); // zero length if (result < EPSILON_E3) { result = 0.0f; } return result; } inline void VectorNormalize(Vector &v) { float length = VectorLength(v); // zero length if (length == 0.0f) { v.x = 0.0f; v.y = 0.0f; v.z = 0.0f; } else { float lengthInv = 1.0f / length; v.x *= lengthInv; v.y *= lengthInv; v.z *= lengthInv; } } inline Vector VectorCross(Vector &v1, Vector &v2) { Vector result; result.x = (v1.y * v2.z) - (v1.z * v2.y); result.y = (v1.z * v2.x) - (v1.x * v2.z); result.z = (v1.x * v2.y) - (v1.y * v2.x); return result; } inline float VectorDot(Vector &v1, Vector &v2) { float result; result = (v1.x * v2.x) + (v1.y * v2.y) + (v1.z * v2.z); return result; } #define VEC_H #endif