diff options
author | Stijn Buys <ingar@osirion.org> | 2008-01-30 17:30:10 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-01-30 17:30:10 +0000 |
commit | 4fd8d5c71365e58e6dff36fc756d8e2e55204db7 (patch) | |
tree | 851c86f497ce3bfa7050ae5634f50f888d6d80f8 /src/math/vector3f.cc | |
parent | 28180e6b6763e4ce5d65c02e4df5380f11e6d10a (diff) |
math module
Diffstat (limited to 'src/math/vector3f.cc')
-rw-r--r-- | src/math/vector3f.cc | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/src/math/vector3f.cc b/src/math/vector3f.cc new file mode 100644 index 0000000..da3ff12 --- /dev/null +++ b/src/math/vector3f.cc @@ -0,0 +1,161 @@ +/* + 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 <cmath> + +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 |