/* math/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]) { clear(); } Vector3f::Vector3f(const Vector3f &other) : x(coord[0]), y(coord[1]), z(coord[2]) { assign(other); } Vector3f::Vector3f(const float vx, const float vy, const float vz) : x(coord[0]), y(coord[1]), z(coord[2]) { assign(vx, vy, vz); } Vector3f::~Vector3f() { } void Vector3f::clear() { memset(coord, 0, sizeof(coord)); } void Vector3f::assign(const float vx, const float vy, const float vz) { coord[0] = vx; coord[1] = vy; coord[2] = vz; } void Vector3f::assign(Vector3f const & other) { coord[0] = other.coord[0]; coord[1] = other.coord[1]; coord[2] = other.coord[2]; } Vector3f & Vector3f::operator=(const Vector3f & other) { assign(other); 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); } 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 { float r = 0; for (int i=0; i < 3; i++) r += coord[i]*coord[i]; return ((float) r); } float Vector3f::length() const { float r = 0; for (int i=0; i < 3; i++) r += coord[i]*coord[i]; return (sqrtf(r)); } void Vector3f::normalize() { (*this) /= this->length(); } Vector3f operator*(float scalar, const Vector3f& vector) { return vector * scalar; } std::ostream &operator<<(std::ostream & os, Vector3f const & 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; } const Vector3f crossproduct(Vector3f const & first, Vector3f const& second) { float vx = first[1]*second[2] - first[2]*second[1]; float vy = first[2]*second[0] - first[0]*second[2]; float vz = first[0]*second[1] - first[1]*second[0]; return(Vector3f(vx,vy,vz)); } float dotproduct(const Vector3f& first, const Vector3f& second) { float r = 0; for (int i=0; i < 3; i++) r += first[i] * second[i]; return (r); } float distance(const Vector3f& first, const Vector3f& second) { float r = 0; for (int i=0; i < 3; i++) r += (first[i]-second[i])*(first[i]-second[i]); return (sqrtf(r)); } float distancesquared(const Vector3f& first, const Vector3f& second) { float r = 0; for (int i=0; i < 3; i++) r += (first[i]-second[i])*(first[i]-second[i]); return (r); } } // namespace math