/* common/vector3f.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ // project headers #include "math/vector3f.h" // C++ headers #include namespace math { Vector3f::Vector3f() : x(coord[0]), y(coord[1]), z(coord[2]) { for (int i=0; i < 3; i++) coord[i] = 0; } Vector3f::Vector3f(const Vector3f &other) : x(coord[0]), y(coord[1]), z(coord[2]) { for (int i=0; i < 3; i++) coord[i] = other.coord[i]; } Vector3f::Vector3f(const float xv, const float yv, const float zv) : x(coord[0]), y(coord[1]), z(coord[2]) { coord[0] = xv; coord[1] = yv; coord[2] = zv; } Vector3f::~Vector3f() { } Vector3f & Vector3f::operator=(const Vector3f & other) { for (int i=0; i < 3; i++) coord[i] = other.coord[i]; return (*this); } Vector3f & Vector3f::operator*=(const float scalar) { for (int i=0; i < 3; i++) coord[i] *= scalar; return (*this); } Vector3f & Vector3f::operator/=(const float scalar) { for (int i=0; i < 3; i++) coord[i] /= scalar; return (*this); } Vector3f &Vector3f::operator-=(const Vector3f & other) { for (int i=0; i < 3; i++) coord[i] -= other[i]; return (*this); } Vector3f &Vector3f::operator+=(const Vector3f &other) { for (int i=0; i < 3; i++) coord[i] += other[i]; return (*this); } Vector3f Vector3f::operator*(const float scalar) const { Vector3f r(*this); for (int i=0; i < 3; i++) r.coord[i] *= scalar; return (r); } Vector3f Vector3f::operator/(const float scalar) const { Vector3f r(*this); for (int i=0; i < 3; i++) r.coord[i] /= scalar; return (r); } Vector3f Vector3f::operator-(const Vector3f& other) const { Vector3f r(*this); for (int i=0; i < 3; i++) r.coord[i] -= other.coord[i]; return (r); } Vector3f Vector3f::operator+(const Vector3f& other) const { Vector3f r(*this); for (int i=0; i < 3; i++) r.coord[i] += other.coord[i]; return (r); } float Vector3f::operator*(const Vector3f& other) const { float r = 0; for (int i=0; i < 3; i++) r += coord[i] * other.coord[i]; return (r); } bool Vector3f::operator==(const Vector3f& other) const { for (int i=0; i < 3; i++) if (coord[i] != other.coord[i]) return (false); return (true); } float Vector3f::lengthsquared() const { double r = 0; for (int i=0; i < 3; i++) r += coord[i]*coord[i]; return ((float) r); } float Vector3f::length() const { double r = 0; for (int i=0; i < 3; i++) r += coord[i]*coord[i]; return ((float) sqrt(r)); } void Vector3f::normalize() { (*this) /= this->length(); } std::ostream &operator<<(std::ostream & os, const Vector3f & vector) { os << vector[0] << " " << vector[1] << " " << vector[2]; return os; } std::istream &operator>>(std::istream & is, Vector3f & vector) { for (int i=0; i < 3; i++) is >> vector[i]; return is; } Vector3f operator*(float scalar, const Vector3f& vector) { return vector * scalar; } } // namespace math