From 18383a5fc596bf9546f14d7393ee66c57720b116 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 12 Oct 2008 11:14:22 +0000 Subject: libmath API cleanup --- src/math/axis.cc | 38 ++++++------ src/math/axis.h | 55 +++++++++++------- src/math/color.cc | 45 +++++++-------- src/math/color.h | 28 ++++----- src/math/functions.cc | 2 +- src/math/functions.h | 12 ++-- src/math/mathlib.h | 5 +- src/math/matrix4f.cc | 4 +- src/math/matrix4f.h | 26 ++++----- src/math/vector2f.cc | 32 ++++------- src/math/vector2f.h | 84 ++++++++++++++++++--------- src/math/vector3f.cc | 68 +++++----------------- src/math/vector3f.h | 156 +++++++++++++++++++++++++++++--------------------- 13 files changed, 289 insertions(+), 266 deletions(-) diff --git a/src/math/axis.cc b/src/math/axis.cc index e6442eb..3c11de1 100644 --- a/src/math/axis.cc +++ b/src/math/axis.cc @@ -16,7 +16,8 @@ Axis::Axis() clear(); } -Axis::Axis(const Axis & other) { +Axis::Axis(const Axis & other) +{ assign(other); } @@ -27,22 +28,25 @@ void Axis::clear() axis_vector[2] = Vector3f(0.0f, 0.0f, 1.0f); } -void Axis::assign(const Axis & other) { +void Axis::assign(const Axis & other) +{ for (size_t i=0; i < 3; i++) { axis_vector[i].assign(other.axis_vector[i]); } } -Axis & Axis::operator=(const Axis & other) { +Axis & Axis::operator=(const Axis & other) +{ assign(other); return *this; } // change heading, rotate around Z-axis (positive is left) -void Axis::change_direction(const float angle) { +void Axis::change_direction(const float angle) +{ float cosa = cosf(angle * M_PI / 180.0f); float sina = sinf(angle * M_PI / 180.0f); - + Vector3f forward = axis_vector[0] * cosa + axis_vector[1] * sina; Vector3f left = axis_vector[1] *cosa - axis_vector[0] * sina; @@ -51,25 +55,27 @@ void Axis::change_direction(const float angle) { } // change pitch, rotate around negative Y-axis (positive is up) -void Axis::change_pitch(const float angle) { +void Axis::change_pitch(const float angle) +{ float cosa = cosf(angle * M_PI / 180.0f); float sina = sinf(angle * M_PI / 180.0f); - + Vector3f forward = axis_vector[0] * cosa + axis_vector[2] * sina; Vector3f up = axis_vector[2] * cosa - axis_vector[0] * sina; - + axis_vector[0].assign(forward); axis_vector[2].assign(up); } // change roll, rotate around forward vector (positive is left) -void Axis::change_roll(const float angle) { +void Axis::change_roll(const float angle) +{ float cosa = cosf(angle * M_PI / 180.0f); float sina = sinf(angle * M_PI / 180.0f); - + Vector3f forward = axis_vector[2] * cosa + axis_vector[1] * sina; Vector3f up = axis_vector[1] * cosa - axis_vector[2] * sina; - + axis_vector[2].assign(forward); axis_vector[1].assign(up); } @@ -82,11 +88,11 @@ void Axis::rotate(Vector3f const &normal, float angle) { float cosa = cosf(angle); float sina = sinf(angle); - + for (size_t i =0; i < 3; i++) { - axis_vector[i] = - axis_vector[i]*cosa + - normal * dotproduct(normal, axis_vector[i]) * (1 - cosa) + + axis_vector[i] = + axis_vector[i]*cosa + + normal * dotproduct(normal, axis_vector[i]) * (1 - cosa) + crossproduct(axis_vector[i], normal)*sina; } } @@ -94,7 +100,7 @@ void Axis::rotate(Vector3f const &normal, float angle) Axis Axis::transpose() const { Axis t; - + for (size_t i = 0; i < 3; i++) for (size_t j = 0; j < 3; j++) t.axis_vector[i][j] = axis_vector[j][i]; diff --git a/src/math/axis.h b/src/math/axis.h index 1396521..38c68de 100644 --- a/src/math/axis.h +++ b/src/math/axis.h @@ -15,57 +15,68 @@ namespace math { /// a local coordinates system -class Axis { +class Axis +{ public: Axis(); Axis(const Axis & other); - + void clear(); - + void assign(const Axis & other); - + /// global coordinates of the X-axis in the local coordinates system - inline Vector3f const & forward() const { return axis_vector[0]; } - + inline const Vector3f & forward() const { + return axis_vector[0]; + } + /// global coordinates of the Y-axis in the local coordinates system - inline Vector3f const & left() const { return axis_vector[1]; } + inline const Vector3f & left() const { + return axis_vector[1]; + } /// global coordinates of the Z-axis in the local coordinates system - inline Vector3f const & up() const { return axis_vector[2]; } - - inline Vector3f & operator[](size_t index) { return axis_vector[index]; } - - inline Vector3f const & operator[](size_t index) const { return axis_vector[index]; } - + inline const Vector3f & up() const { + return axis_vector[2]; + } + + inline Vector3f & operator[](size_t index) { + return axis_vector[index]; + } + + inline const Vector3f & operator[](size_t index) const { + return axis_vector[index]; + } + Axis & operator=(const Axis & other); - + /// change direction, rotate around up vector (positive is left) void change_direction(const float angle); - + /// change pitch, rotate around left vector (positive is up) void change_pitch(const float angle); - + /// change roll, rotate around forward vector (positive is left) void change_roll(const float angle); - + /// rotation about an arbitrary axis - void rotate(Vector3f const &normal, float angle); - + void rotate(const Vector3f & normal, const float angle); + /// return the transpose of this matrix Axis transpose() const; - + private: Vector3f axis_vector[3]; }; /// write an axis to a std::ostream -std::ostream &operator<<(std::ostream & os, Axis const & axis); +std::ostream &operator<<(std::ostream & os, const Axis & axis); /// read an axis from a std::istream std::istream &operator>>(std::istream & is, Axis & axis); /// local-to-global coordinates -Vector3f operator*(Axis const &axis, Vector3f const &vector); +Vector3f operator*(const Axis &axis, const Vector3f &vector); } diff --git a/src/math/color.cc b/src/math/color.cc index 231ec80..ff4cb5d 100644 --- a/src/math/color.cc +++ b/src/math/color.cc @@ -12,37 +12,37 @@ namespace math { Color::Color() : - r(rgba_data[0]), - g(rgba_data[1]), - b(rgba_data[2]), - a(rgba_data[3]) + r(rgba_data[0]), + g(rgba_data[1]), + b(rgba_data[2]), + a(rgba_data[3]) { assign(1.0f, 1.0f); } Color::Color(float red, float green, float blue, float alpha) : - r(rgba_data[0]), - g(rgba_data[1]), - b(rgba_data[2]), - a(rgba_data[3]) + r(rgba_data[0]), + g(rgba_data[1]), + b(rgba_data[2]), + a(rgba_data[3]) { assign(red, green, blue, alpha); } -Color::Color(const float grey, const float alpha) : - r(rgba_data[0]), - g(rgba_data[1]), - b(rgba_data[2]), - a(rgba_data[3]) +Color::Color(const float grey, const float alpha) : + r(rgba_data[0]), + g(rgba_data[1]), + b(rgba_data[2]), + a(rgba_data[3]) { assign(grey, alpha); } -Color::Color(Color const &other) : - r(rgba_data[0]), - g(rgba_data[1]), - b(rgba_data[2]), - a(rgba_data[3]) +Color::Color(Color const &other) : + r(rgba_data[0]), + g(rgba_data[1]), + b(rgba_data[2]), + a(rgba_data[3]) { assign(other); } @@ -58,8 +58,7 @@ void Color::assign(float red, float green, float blue, float alpha) void Color::assign(Color const & other) { - for (int i =0; i < 4; i++) - rgba_data[i] = other.rgba_data[i]; + memcpy(rgba_data, other.rgba_data, sizeof(rgba_data)); } void Color::assign(float grey, float alpha) @@ -79,18 +78,18 @@ void Color::clamp() for (int i =0; i < 4; i++) if (rgba_data[i] < 0) rgba_data[i] = 0; - + float tmp = rgba_data[0]; if (rgba_data[1] > tmp) tmp = rgba_data[1]; if (rgba_data[2] > tmp) tmp = rgba_data[2]; - + if (tmp > 1) { for (int i =0; i < 3; i++) rgba_data[i] /= tmp; } - + if (rgba_data[3] > 1) rgba_data[3] = 1; } diff --git a/src/math/color.h b/src/math/color.h index c938708..762ac94 100644 --- a/src/math/color.h +++ b/src/math/color.h @@ -24,7 +24,7 @@ public: Color(const float grey, const float=1.0f); /// create a copy from an existing color Color(Color const & other); - + /// red channel value float red() const; /// green channel value @@ -33,7 +33,7 @@ public: float blue() const; /// alpha channel value float alpha() const; - + /// assignment void assign(Color const & other); /// assignment @@ -42,21 +42,21 @@ public: void assign(float grey, float alpha=1.0f); /// assignment operator Color const &operator=(Color const & other); - + /// multiply rgb values with scalar value. Color operator*(const float scalar) const; - + /// multiply rgb values with scalar value. Color & operator*=(const float scalar); - - /// assign a value to an element of this color + + /// assign a value to an element of this color /*! WARNING: range is not checked * @param index the index of the element to assign to ( 0 <= index < 4 ) */ inline float& operator[](const size_t index) { return rgba_data[index]; } - + /// returns the value of an element of this color /*! WARNING: range is not checked * @param index the index of the element to return ( 0 <= index < 4 ) @@ -64,19 +64,21 @@ public: inline float operator[](const size_t index) const { return rgba_data[index]; } - - + + /// pointer to the internal data - inline float *ptr() const { return (float *) rgba_data; }; - + inline float *ptr() const { + return (float *) rgba_data; + } + /// clamp color values to the 0-1 range void clamp(); - + float &r; float &g; float &b; float &a; - + private: float rgba_data[4]; diff --git a/src/math/functions.cc b/src/math/functions.cc index c4b832c..bb4e9bf 100644 --- a/src/math/functions.cc +++ b/src/math/functions.cc @@ -68,7 +68,7 @@ float sgnf(float value) return -1; else if (value == 0) return 0; - + return 1; } diff --git a/src/math/functions.h b/src/math/functions.h index be6919d..ed013ab 100644 --- a/src/math/functions.h +++ b/src/math/functions.h @@ -51,18 +51,18 @@ float degrees360f(float angle); /// clamp a float to a specified range inline void clamp(float &value, const float min=0.0f, const float max=1.0f) { - if (value < min) - value = min; - else if (value > max) + if (value < min) + value = min; + else if (value > max) value = max; } /// return the absolute value of a float inline float absf(float f) { - if (f < 0) - return -f; - else + if (f < 0) + return -f; + else return f; } diff --git a/src/math/mathlib.h b/src/math/mathlib.h index 721a1c0..ee1e43e 100644 --- a/src/math/mathlib.h +++ b/src/math/mathlib.h @@ -12,11 +12,12 @@ */ namespace math {} -#include "math/vector3f.h" -#include "math/color.h" #include "math/axis.h" +#include "math/color.h" #include "math/matrix4f.h" #include "math/functions.h" +#include "math/vector2f.h" +#include "math/vector3f.h" #endif // __INCLUDED_MATH_MATHLIB_H__ diff --git a/src/math/matrix4f.cc b/src/math/matrix4f.cc index 094bbd6..371d666 100644 --- a/src/math/matrix4f.cc +++ b/src/math/matrix4f.cc @@ -59,7 +59,7 @@ Matrix4f & Matrix4f::operator=(const Matrix4f &other) assign(other); return(*this); } - + Matrix4f & Matrix4f::operator=(const Axis & axis) { assign(axis); @@ -69,7 +69,7 @@ Matrix4f & Matrix4f::operator=(const Axis & axis) Matrix4f const Matrix4f::transpose() { Matrix4f t; - + for (size_t i = 0; i < 4; i++) for (size_t j = 0; j < 4; j++) t.matrix[j][i] = matrix[i][j]; diff --git a/src/math/matrix4f.h b/src/math/matrix4f.h index 0d7df90..e69ae05 100644 --- a/src/math/matrix4f.h +++ b/src/math/matrix4f.h @@ -7,12 +7,10 @@ #ifndef __INCLUDED_MATH_MATRIX4F_H__ #define __INCLUDED_MATH_MATRIX4F_H__ -// project headers -#include "math/axis.h" - -// C++ headers #include +#include "math/axis.h" + namespace math { @@ -23,31 +21,33 @@ public: Matrix4f(); Matrix4f(const Matrix4f & other); Matrix4f(const Axis & axis); - + /// set all values to zero void clear(); - + /// set the value to a 4x4 unity matrix void unity(); - + /// assignment operator void assign(const Matrix4f & other); - + /// assignment operator inline Matrix4f & operator=(const Matrix4f &other); /// assign the matrix transformation equivalent to the coordinate system void assign(const Axis & axis); - + /// assign the matrix transformation equivalent to the coordinate system Matrix4f & operator=(const Axis & axis); - + /// return a pointer to the internal data - inline float * ptr() const { return (float *) matrix; } - + inline float * ptr() const { + return (float *) matrix; + } + /// return the transpose matrix Matrix4f const transpose(); - + private: float matrix[4][4]; }; diff --git a/src/math/vector2f.cc b/src/math/vector2f.cc index c6f1504..809269e 100644 --- a/src/math/vector2f.cc +++ b/src/math/vector2f.cc @@ -5,22 +5,24 @@ */ #include +#include #include "math/vector2f.h" -namespace math { +namespace math +{ Vector2f::Vector2f() : x(coord[0]), y(coord[1]) { clear(); } -Vector2f::Vector2f(Vector2f const &other) : x(coord[0]), y(coord[1]) +Vector2f::Vector2f(const Vector2f &other) : x(coord[0]), y(coord[1]) { assign(other); } -Vector2f::Vector2f(float const x, float const y) : x(coord[0]), y(coord[1]) +Vector2f::Vector2f(const float x, const float y) : x(coord[0]), y(coord[1]) { assign(x, y); } @@ -30,49 +32,35 @@ void Vector2f::clear() memset(coord, 0, sizeof(coord)); } -void Vector2f::assign(Vector2f const &other) +void Vector2f::assign(const Vector2f &other) { memcpy(coord, other.coord, sizeof(coord)); } -void Vector2f::assign(float const x, float const y) +void Vector2f::assign(const float x, const float y) { coord[0] = x; coord[1] = y; } -Vector2f &Vector2f::operator=(Vector2f const &other) +Vector2f &Vector2f::operator=(const Vector2f &other) { assign(other); return (*this); } -Vector2f &Vector2f::operator+=(Vector2f const &other) +Vector2f &Vector2f::operator+=(const Vector2f &other) { coord[0] += other.coord[0]; coord[1] += other.coord[1]; return (*this); } -Vector2f &Vector2f::operator-=(Vector2f const &other) +Vector2f &Vector2f::operator-=(const Vector2f &other) { coord[0] -= other.coord[0]; coord[1] -= other.coord[1]; return (*this); } -Vector2f Vector2f::operator-(Vector2f const &other) const -{ - Vector2f v(*this); - v -= other; - return v; -} - -Vector2f Vector2f::operator+(Vector2f const &other) const -{ - Vector2f v(*this); - v += other; - return v; -} - } diff --git a/src/math/vector2f.h b/src/math/vector2f.h index 87f61a8..6029b3c 100644 --- a/src/math/vector2f.h +++ b/src/math/vector2f.h @@ -9,62 +9,94 @@ #include -namespace math { +namespace math +{ /// a point or vector in 2D space -class Vector2f { +class Vector2f +{ public: /// default constructor Vector2f(); + /// assignment constructor - Vector2f(float const x, float const y); + Vector2f(const float x, const float y); + /// copy constructor - Vector2f(Vector2f const &other); - + Vector2f(const Vector2f &other); + /// assign (0,0) void clear(); - + /// assignment operator - void assign(float const x, float const y); + void assign(const float x, const float y); + /// assignment operator - void assign(Vector2f const &other); - + void assign(const Vector2f &other); + /// assignment operator - Vector2f & operator=(Vector2f const &other); - + Vector2f & operator=(const Vector2f &other); + /// vector subtraction - Vector2f & operator-=(Vector2f const &other); - + Vector2f & operator-=(const Vector2f &other); + /// vector sum - Vector2f & operator+=(Vector2f const &other); - + Vector2f & operator+=(const Vector2f &other); + /// vector subtraction - Vector2f operator-(Vector2f const &other) const; - + inline Vector2f operator+(const Vector2f &other) const { + Vector2f v(coord[0] + other.coord[0], coord[1]+other.coord[1]); + return v; + } + /// vector sum - Vector2f operator+(Vector2f const &other) const; - - + inline Vector2f operator-(const Vector2f &other) const { + Vector2f v(coord[0] - other.coord[0], coord[1] - other.coord[1]); + return v; + } + + /// scalar product + inline Vector2f operator*(const float scalar) const { + Vector2f v(coord[0]*scalar, coord[1]*scalar); + return v; + } + + /// scalar division + inline Vector2f operator/(const float scalar) const { + Vector2f v(coord[0]/scalar, coord[1]/scalar); + return v; + } + /// array operator inline float& operator[](const size_t index) { return coord[index]; } - + /// array operator inline float operator[](const size_t index) const { return coord[index]; } - + /// a pointer to the internal data - inline float *ptr() const { return (float *) coord; } - + inline float *ptr() const { + return (float *) coord; + } + + inline bool contains(const Vector2f &other) const { + return ((other.coord[0] >= 0) && (other.coord[1] >= 0) && (other.coord[0] <= coord[0]) && (other.coord[1] <= coord[1])); + } + + inline bool contains(float x, float y) const { + return ((x >= 0) && (y >= 0) && (x <= coord[0]) && (y <= coord[1])); + } + /// x coordinate float &x; - + /// y coordinate float &y; - + private: float coord[2]; }; diff --git a/src/math/vector3f.cc b/src/math/vector3f.cc index bf2cb8d..8a6dd5e 100644 --- a/src/math/vector3f.cc +++ b/src/math/vector3f.cc @@ -13,7 +13,7 @@ namespace math { Vector3f::Vector3f() : - x(coord[0]), y(coord[1]), z(coord[2]) + x(coord[0]), y(coord[1]), z(coord[2]) { clear(); } @@ -24,10 +24,10 @@ Vector3f::Vector3f(const Vector3f &other) : assign(other); } -Vector3f::Vector3f(const float vx, const float vy, const float vz) : +Vector3f::Vector3f(const float x, const float y, const float z) : x(coord[0]), y(coord[1]), z(coord[2]) { - assign(vx, vy, vz); + assign(x, y, z); } Vector3f::~Vector3f() @@ -36,26 +36,18 @@ Vector3f::~Vector3f() void Vector3f::clear() { - /* - for (size_t i =0; i < 3; i++) - coord[i] = 0; - */ memset(coord, 0, sizeof(coord)); } -void Vector3f::assign(const float vx, const float vy, const float vz) +void Vector3f::assign(const float x, const float y, const float z) { - coord[0] = vx; - coord[1] = vy; - coord[2] = vz; + coord[0] = x; + coord[1] = y; + coord[2] = z; } -void Vector3f::assign(Vector3f const & other) +void Vector3f::assign(const Vector3f & other) { - /* - for (size_t i =0; i < 3; i++) - coord[i] = other.coord[i]; - */ memcpy(coord, other.coord, sizeof(coord)); } @@ -93,38 +85,6 @@ Vector3f &Vector3f::operator+=(const Vector3f &other) 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++) @@ -146,7 +106,7 @@ float Vector3f::length() const float r = 0; for (int i=0; i < 3; i++) r += coord[i]*coord[i]; - + return (sqrtf(r)); } @@ -155,12 +115,12 @@ void Vector3f::normalize() (*this) /= this->length(); } -Vector3f operator*(float scalar, const Vector3f& vector) +Vector3f operator*(float scalar, const Vector3f &vector) { return vector * scalar; } -std::ostream &operator<<(std::ostream & os, Vector3f const & vector) +std::ostream &operator<<(std::ostream & os, const Vector3f & vector) { os << vector[0] << " " << vector[1] << " " << vector[2]; return os; @@ -173,12 +133,12 @@ std::istream &operator>>(std::istream & is, Vector3f & vector) return is; } -const Vector3f crossproduct(Vector3f const & first, Vector3f const& second) +const Vector3f crossproduct(const Vector3f & 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)); } @@ -195,7 +155,7 @@ 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)); } 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); -- cgit v1.2.3