#include #include "Vector3.h" namespace sp { template Vector3::Vector3() { x = y = z = T(0); } template Vector3::Vector3(T x, T y, T z) { v[0] = x; v[1] = y; v[2] = z; } template Vector3::Vector3(T x, T y) { v[0] = x; v[1] = y; v[2] = T(0); } template Vector3::Vector3(T s) { x = y = z = s; } template template inline Vector3::Vector3(const Vector3& vec) { x = T(vec.x); y = T(vec.y); z = T(vec.z); } template template inline Vector3::Vector3(const Vector2& vec) { x = T(vec.x); y = T(vec.y); z = T(0); } template inline float Vector3::length() const { return std::sqrt((x * x) + (y * y) + (z * z)); } template Vector2 Vector3::toVec2() { return Vector2(x, y); } template inline Vector3& Vector3::normalize() { float il = 1.0f / length(); x *= il; y *= il; z *= il; return *this; } // --------- // Compare // --------- template inline bool operator==(const Vector3& a, const Vector3& b) { return a.x == b.x && a.y == b.y && a.z == b.z; } template inline bool operator!=(const Vector3& a, const Vector3& b) { return !(a == b); } template inline bool operator<(const Vector3& a, const Vector3& b) { return a.x < b.x && a.y < b.y && a.z < b.z; } template inline bool operator<=(const Vector3& a, const Vector3& b) { return !(a > b); } template inline bool operator>(const Vector3& a, const Vector3& b) { return a.x > b.x && a.y > b.y && a.z > b.z; } template inline bool operator>=(const Vector3& a, const Vector3& b) { return !(a < b); } // ------------ // Arithmetic // ------------ template inline Vector3 operator+(const Vector3& a, const Vector3& b) { return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); } template inline Vector3 operator-(const Vector3& a, const Vector3& b) { return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); } template inline Vector3 operator/(const Vector3& a, const Vector3& b) { return Vector3(a.x / b.x, a.y / b.y, a.z / b.z); } template inline Vector3 operator*(const Vector3& a, const Vector3& b) { return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); } template inline Vector3& operator+=(Vector3& a, const Vector3& b) { a.x += b.x; a.y += b.y; a.z += b.z; return a; } template inline Vector3& operator-=(Vector3& a, const Vector3& b) { a.x -= b.x; a.y -= b.y; a.z -= b.z; return a; } template inline Vector3& operator/=(Vector3& a, const Vector3& b) { a.x /= b.x; a.y /= b.y; a.z /= b.z; return a; }; template inline Vector3& operator*=(Vector3& a, const Vector3& b) { a.x *= b.x; a.y *= b.y; a.z *= b.z; return a; } // ------------------- // Scalar Arithmetic // ------------------- template inline Vector3 operator+(const Vector3& v, T s) { return Vector3(v.x + s, v.y + s, v.z + s); } template inline Vector3 operator+(T s, const Vector3& v) { return v + s; } template inline Vector3 operator-(const Vector3& v, T s) { return Vector3(v.x - s, v.y - s, v.z - s); } template inline Vector3 operator-(T s, const Vector3& v) { return Vector3(s - v.x, s - v.y, s - v.z); } template inline Vector3 operator/(const Vector3& v, T s) { return Vector3(v.x / s, v.y / s, v.z / s); } template inline Vector3 operator/(T s, const Vector3& v) { return Vector3(s / v.x, s / v.y, s / v.z); } template inline Vector3 operator*(const Vector3& v, T s) { return Vector3(v.x * s, v.y * s, v.z * s); } template inline Vector3 operator*(T s, const Vector3& v) { return v * s; } // ---------------- // Scalar compare // ---------------- template inline bool operator<(const Vector3& v, T s) { return v.x < s && v.y < s && v.z < s; } template inline bool operator<(T s, const Vector3& v) { return s < v.x && s < v.y && s < v.z; } template inline bool operator<=(const Vector3& v, T s) { return v.x <= s && v.y <= s && v.z <= s; } template inline bool operator<=(T s, const Vector3& v) { return s <= v.x && s <= v.y && s <= v.z; } template inline bool operator>(const Vector3& v, T s) { return v.x > s && v.y > s && v.z > s; } template inline bool operator>(T s, const Vector3& v) { return s > v.x && s > v.y && s > v.z; } template inline bool operator>=(const Vector3& v, T s) { return v.x >= s && v.y >= s && v.z >= s; } template inline bool operator>=(T s, const Vector3& v) { return s >= v.x && s >= v.y && s >= v.z; } template inline std::ostream& operator<<(std::ostream &s, const Vector3& v) { return s << "(" << v.x << "," << v.y << "," << v.z << ")"; } } // namespace sp