/** * @file vector.hpp * @author Julian Iseringhausen * @date Mon Apr 18 12:26:13 2011 * * @brief VMG::Vector * * */ #ifndef VECTOR_HPP_ #define VECTOR_HPP_ #include #include #include namespace VMG { class Index; class Vector { public: Vector(const Index& index); Vector(vmg_float x, vmg_float y, vmg_float z); Vector(vmg_float val); Vector(vmg_float* arr) {i[0]=arr[0];i[1]=arr[1];i[2]=arr[2];} Vector(); vmg_float& operator[](const int& index) {return i[index];} const vmg_float& operator[](const int& index) const {return i[index];} vmg_float& X() {return i[0];} vmg_float& Y() {return i[1];} vmg_float& Z() {return i[2];} const vmg_float& X() const {return i[0];} const vmg_float& Y() const {return i[1];} const vmg_float& Z() const {return i[2];} const Vector Abs() {return Vector(fabs(i[0]), fabs(i[1]), fabs(i[2]));} vmg_float Length() const {return sqrt(i[0]*i[0]+i[1]*i[1]+i[2]*i[2]);} vmg_float Max() const {return std::max(std::max(i[0],i[1]),i[2]);} vmg_float Min() const {return std::min(std::min(i[0],i[1]),i[2]);} vmg_float Product() const {return i[0]*i[1]*i[2];} vmg_float* vec() {return i;} const vmg_float* vec() const {return i;} Vector& operator=(const Vector& rhs) { i[0] = rhs.X(); i[1] = rhs.Y(); i[2] = rhs.Z(); return *this; } Vector& operator=(const vmg_float& rhs) { i[0] = rhs; i[1] = rhs; i[2] = rhs; return *this; } bool operator==(const Vector& other) const { return (i[0]==other.X() && i[1]==other.Y() && i[2]==other.Z()); } bool operator!=(const Vector& other) const { return !(*this == other); } Vector& operator+=(const Vector& rhs) { i[0] += rhs.X(); i[1] += rhs.Y(); i[2] += rhs.Z(); return *this; } Vector& operator-=(const Vector& rhs) { i[0] -= rhs.X(); i[1] -= rhs.Y(); i[2] -= rhs.Z(); return *this; } Vector& operator*=(const Vector& rhs) { i[0] *= rhs.X(); i[1] *= rhs.Y(); i[2] *= rhs.Z(); return *this; } Vector& operator/=(const Vector& rhs) { i[0] /= rhs.X(); i[1] /= rhs.Y(); i[2] /= rhs.Z(); return *this; } Vector& operator+=(const vmg_float& rhs) { i[0] += rhs; i[1] += rhs; i[2] += rhs; return *this; } Vector& operator-=(const vmg_float& rhs) { i[0] -= rhs; i[1] -= rhs; i[2] -= rhs; return *this; } Vector& operator*=(const vmg_float& rhs) { i[0] *= rhs; i[1] *= rhs; i[2] *= rhs; return *this; } Vector& operator/=(const vmg_float& rhs) { i[0] /= rhs; i[1] /= rhs; i[2] /= rhs; return *this; } const Vector operator+(const Vector& rhs) const { return Vector(*this) += rhs; } const Vector operator-(const Vector& rhs) const { return Vector(*this) -= rhs; } const Vector operator*(const Vector& rhs) const { return Vector(*this) *= rhs; } const Vector operator/(const Vector& rhs) const { return Vector(*this) /= rhs; } const Vector operator+(const vmg_float& rhs) const { return Vector(*this) += rhs; } const Vector operator-(const vmg_float& rhs) const { return Vector(*this) -= rhs; } const Vector operator*(const vmg_float& rhs) const { return Vector(*this) *= rhs; } const Vector operator/(const vmg_float& rhs) const { return Vector(*this) /= rhs; } private: vmg_float i[3]; }; inline const Vector operator+(const vmg_float& lhs, const Vector& rhs) { return Vector(lhs) += rhs; } inline const Vector operator-(const vmg_float& lhs, const Vector& rhs) { return Vector(lhs) -= rhs; } inline const Vector operator*(const vmg_float& lhs, const Vector& rhs) { return Vector(lhs) *= rhs; } inline const Vector operator/(const vmg_float& lhs, const Vector& rhs) { return Vector(lhs) /= rhs; } inline vmg_float Norm_2(const Vector& vec) { return sqrt(vec.X()*vec.X()+vec.Y()*vec.Y()+vec.Z()*vec.Z()); } inline vmg_float InnerProduct(const Vector& vec1, const Vector& vec2) { return vec1.X()*vec2.X()+vec1.Y()*vec2.Y()+vec1.Z()*vec2.Z(); } std::ostream& operator<<(std::ostream& out, const Vector& base); } #endif /* VECTOR_HPP_ */