diff options
author | Stijn Buys <ingar@osirion.org> | 2008-10-12 11:14:22 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-10-12 11:14:22 +0000 |
commit | 18383a5fc596bf9546f14d7393ee66c57720b116 (patch) | |
tree | 5382c3b380a72149eabbc4f75a2c5744b895e48a /src/math/vector3f.h | |
parent | 0d831968949b1119db48530a86c2d1651c6cbfc6 (diff) |
libmath API cleanup
Diffstat (limited to 'src/math/vector3f.h')
-rw-r--r-- | src/math/vector3f.h | 156 |
1 files changed, 90 insertions, 66 deletions
diff --git a/src/math/vector3f.h b/src/math/vector3f.h index 20be5ab..82b818e 100644 --- a/src/math/vector3f.h +++ b/src/math/vector3f.h @@ -21,142 +21,166 @@ class Vector3f { public: - /// Default constructor, creates a Vector3f (0,0,0) + /// default constructor, assigns (0,0,0) Vector3f(); - - /// Copy constructor + + /// copy constructor /** Create a new Vector3f that is a copy from an other * @param other the vector to copy values from */ Vector3f(const Vector3f &other); - - /// Create a Vector3f with given coordinates + + /// create a Vector3f with given coordinates /** @param x the x-coordinate of the location * @param y the y-coordinate of the location * @param z the z-coordinate of the location */ Vector3f(const float x, const float y, const float z); - - /// Destructor + + /// destructor ~Vector3f(); - + /* -- Assignment operators -- */ /// assign (0, 0, 0) void clear(); - + /// assignment void assign(const float x, const float y, const float z); - + /// assignment - void assign(Vector3f const & other); - + void assign(const Vector3f & other); + /// assignment operator Vector3f& operator=(const Vector3f &other); - - /// multiplicate each element of the vector with a given value + + /// scalar multiplication assignment /** @param scalar multiplication factor */ Vector3f& operator*=(const float scalar); - - /// divide each element of the vector by a given value + + /// scalar division assignment /** @param scalar divider */ Vector3f& operator/=(const float scalar); - - /// perform an element-wise subtraction - Vector3f& operator-=(const Vector3f &other); - - /// perform an element-wise addition + + /// vector sum assignment Vector3f& operator+=(const Vector3f &other); - + + /// vector difference assignment + Vector3f& operator-=(const Vector3f &other); + /* -- Mathematical operators -- */ - - /// return this Vector multiplied with scalar - Vector3f operator*(const float scalar) const; - - /// return this vector divided by a scalar - Vector3f operator/(const float scalar) const; - - /// return the element-wise difference between two vectors - Vector3f operator-(const Vector3f &other) const; - - /// return the element-wise sum of two vectors - Vector3f operator+(const Vector3f &other) const; - + + /// scalar multiplication + inline Vector3f operator*(const float scalar) const { + Vector3f v(coord[0] * scalar, coord[1] * scalar, coord[2] * scalar); + return v; + } + + /// scalar division + Vector3f operator/(const float scalar) const { + Vector3f v(coord[0] / scalar, coord[1] / scalar, coord[2] / scalar); + return v; + } + + /// vector sum + inline Vector3f operator+(const Vector3f &other) const { + Vector3f v(coord[0] + other.coord[0], coord[1] + other.coord[1], coord[2] + other.coord[2]); + return v; + } + + /// vector difference + inline Vector3f operator-(const Vector3f &other) const { + Vector3f v(coord[0] - other.coord[0], coord[1] - other.coord[1], coord[2] - other.coord[2]); + return v; + } + /// comparison operator bool operator==(const Vector3f &other) const; - + /// assign a value to an element of this vector - /*! WARNING: range is not checked + /*! range is not checked * @param index the index of the element to assign to ( 0 <= index < 3 ) */ inline float& operator[](const size_t index) { return coord[index]; } - + /// returns the value of an element of this vector - /*! WARNING: range is not checked + /*! range is not checked * @param index the index of the element to return ( 0 <= index < 3 ) */ inline float operator[](const size_t index) const { return coord[index]; } - - /// Return the cartesian length of this vector + + /// cartesian length float length() const; - - /// Return the cartesian length squared (to speed up calculations) + + /// cartesian length squared float lengthsquared() const; - - /// Divide this Vector by it's length + + /// divide this Vector by its length /// @see normalized() - /// WARNING: vector must not be (0, 0, 0) + /// vector must not be (0, 0, 0) void normalize(); - + /// a pointer to the internal data - inline float *ptr() const { return (float *) coord; } - + inline float *ptr() const { + return (float *) coord; + } + /* static functions */ - + /// Returns the unity vector on the X-axis - static inline Vector3f Xaxis() { return Vector3f(1.0f, 0.0f, 0.0f); } - + static inline Vector3f Xaxis() { + return Vector3f(1.0f, 0.0f, 0.0f); + } + /// Returns the unity vector on the Y-axis - static inline Vector3f Yaxis() { return Vector3f(0.0f, 1.0f, 0.0f); } - + static inline Vector3f Yaxis() { + return Vector3f(0.0f, 1.0f, 0.0f); + } + /// Returns the unity vector on the Z-axis - static inline Vector3f Zaxis() { return Vector3f(0.0f, 0.0f, 1.0f); } - - /// Return the cartesian length of a vector - static inline float length(const Vector3f& vector) { return vector.length(); } - + static inline Vector3f Zaxis() { + return Vector3f(0.0f, 0.0f, 1.0f); + } + + /// cartesian lenght + static inline float length(const Vector3f & vector) { + return vector.length(); + } + /// Return a vector divided by it's length /// @see normalize() /// WARNING: vector must not be (0, 0, 0) - static inline Vector3f normalized(const Vector3f& vector) { return (vector / vector.length()); } - + static inline Vector3f normalized(const Vector3f & vector) { + return (vector / vector.length()); + } + float &x; float &y; float &z; - + private: float coord[3]; }; /// Write a Vector3f to a std::ostream -std::ostream &operator<<(std::ostream & os, Vector3f const & vector); +std::ostream &operator<<(std::ostream & os, const Vector3f & vector); /// Read a Vector3d from a std::istream std::istream &operator>>(std::istream & is, Vector3f & vector); /// scalar * Vector3f operators -Vector3f operator*(float scalar, const Vector3f& vector); +Vector3f operator*(float scalar, const Vector3f & vector); /// vector cross product -const Vector3f crossproduct(Vector3f const & first, Vector3f const & second); +const Vector3f crossproduct(const Vector3f & first, const Vector3f & second); /// vector dot product -float dotproduct(const Vector3f& first, const Vector3f& second); +float dotproduct(const Vector3f& first, const Vector3f & second); /// distance between two vectors float distance(const Vector3f& first, const Vector3f& second); |