diff options
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/axis.cc | 26 | ||||
-rw-r--r-- | src/math/axis.h | 28 | ||||
-rw-r--r-- | src/math/color.cc | 12 | ||||
-rw-r--r-- | src/math/color.h | 32 | ||||
-rw-r--r-- | src/math/functions.cc | 2 | ||||
-rw-r--r-- | src/math/functions.h | 2 | ||||
-rw-r--r-- | src/math/matrix4f.cc | 6 | ||||
-rw-r--r-- | src/math/matrix4f.h | 18 | ||||
-rw-r--r-- | src/math/vector2f.cc | 12 | ||||
-rw-r--r-- | src/math/vector2f.h | 60 | ||||
-rw-r--r-- | src/math/vector3f.cc | 46 | ||||
-rw-r--r-- | src/math/vector3f.h | 86 |
12 files changed, 176 insertions, 154 deletions
diff --git a/src/math/axis.cc b/src/math/axis.cc index 31ba26a..402b2f6 100644 --- a/src/math/axis.cc +++ b/src/math/axis.cc @@ -30,7 +30,7 @@ void Axis::clear() void Axis::assign(const Axis & other) { - for (size_t i=0; i < 3; i++) { + for (size_t i = 0; i < 3; i++) { axis_vector[i].assign(other.axis_vector[i]); } } @@ -46,10 +46,10 @@ 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; - + Vector3f left = axis_vector[1] * cosa - axis_vector[0] * sina; + axis_vector[0].assign(forward); axis_vector[1].assign(left); } @@ -59,10 +59,10 @@ 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); } @@ -72,10 +72,10 @@ 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); } @@ -88,19 +88,19 @@ void Axis::rotate(Vector3f const &normal, float angle) { float cosa = cosf(angle); float sina = sinf(angle); - - for (size_t i =0; i < 3; i++) { + + for (size_t i = 0; i < 3; i++) { axis_vector[i] = - axis_vector[i]*cosa + + axis_vector[i] * cosa + normal * dotproduct(normal, axis_vector[i]) * (1 - cosa) + - crossproduct(axis_vector[i], normal)*sina; + crossproduct(axis_vector[i], normal) * sina; } } const 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 5f532c8..0ece016 100644 --- a/src/math/axis.h +++ b/src/math/axis.h @@ -20,53 +20,53 @@ 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 const Vector3f & forward() const { return axis_vector[0]; } - + /// global coordinates of the Y-axis in the local coordinates system inline const Vector3f & left() const { return axis_vector[1]; } - + /// global coordinates of the Z-axis in the local coordinates system 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); const Axis operator*(const Axis &other) const; - + /// 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(const Vector3f & normal, const float angle); - + /// return the transpose of this matrix const Axis transpose() const; - + private: Vector3f axis_vector[3]; }; diff --git a/src/math/color.cc b/src/math/color.cc index ff4cb5d..b807b96 100644 --- a/src/math/color.cc +++ b/src/math/color.cc @@ -75,21 +75,21 @@ const Color & Color::operator=(Color const & other) void Color::clamp() { - for (int i =0; i < 4; i++) + 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++) + for (int i = 0; i < 3; i++) rgba_data[i] /= tmp; } - + if (rgba_data[3] > 1) rgba_data[3] = 1; } @@ -126,7 +126,7 @@ Color const operator*(float scalar, Color const & color) Color & Color::operator*=(const float scalar) { - for (int i=0; i < 3; i++) + for (int i = 0; i < 3; i++) rgba_data[i] *= scalar; return (*this); } diff --git a/src/math/color.h b/src/math/color.h index 762ac94..8996af2 100644 --- a/src/math/color.h +++ b/src/math/color.h @@ -19,12 +19,12 @@ public: /// create the default color, white Color(); /// create a color from float RGBA value - Color(float red, float green, float blue, float alpha=1.0f); + Color(float red, float green, float blue, float alpha = 1.0f); /// create a greyscale color - Color(const float grey, const float=1.0f); + 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,22 +33,22 @@ public: float blue() const; /// alpha channel value float alpha() const; - + /// assignment void assign(Color const & other); /// assignment - void assign(float red, float green, float blue, float alpha=1.0f); + void assign(float red, float green, float blue, float alpha = 1.0f); /// assignment - void assign(float grey, float alpha=1.0f); + 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 /*! WARNING: range is not checked * @param index the index of the element to assign to ( 0 <= index < 4 ) @@ -56,7 +56,7 @@ public: 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,22 +64,22 @@ 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; } - + /// 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 bb4e9bf..c4b832c 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 ed013ab..c301969 100644 --- a/src/math/functions.h +++ b/src/math/functions.h @@ -49,7 +49,7 @@ float degrees180f(float angle); 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) +inline void clamp(float &value, const float min = 0.0f, const float max = 1.0f) { if (value < min) value = min; diff --git a/src/math/matrix4f.cc b/src/math/matrix4f.cc index 371d666..b7953e9 100644 --- a/src/math/matrix4f.cc +++ b/src/math/matrix4f.cc @@ -36,7 +36,7 @@ void Matrix4f::clear() void Matrix4f::unity() { memset(matrix, 0, sizeof(matrix)); - for (int i=0; i <4; i++) + for (int i = 0; i < 4; i++) matrix[i][i] = 1; } @@ -48,7 +48,7 @@ void Matrix4f::assign(const Matrix4f & other) void Matrix4f::assign(const Axis & axis) { memset(matrix, 0, sizeof(matrix)); - for (int i=0; i < 3; i++) { + for (int i = 0; i < 3; i++) { memcpy(&matrix[i][0], axis[i].ptr(), sizeof(float) * 3); } matrix[3][3] = 1; @@ -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 e69ae05..4148870 100644 --- a/src/math/matrix4f.h +++ b/src/math/matrix4f.h @@ -21,33 +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; } - + /// 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 65f1f93..878304f 100644 --- a/src/math/vector2f.cc +++ b/src/math/vector2f.cc @@ -17,7 +17,7 @@ Vector2f::Vector2f() clear(); } -Vector2f::Vector2f(const Vector2f &other) +Vector2f::Vector2f(const Vector2f &other) { assign(other); } @@ -66,17 +66,17 @@ Vector2f &Vector2f::operator-=(const Vector2f &other) float distance(const Vector2f& first, const Vector2f& second) { float r = 0; - for (int i=0; i < 2; i++) - r += (first[i]-second[i])*(first[i]-second[i]); - + for (int i = 0; i < 2; i++) + r += (first[i] - second[i]) * (first[i] - second[i]); + return (sqrtf(r)); } float distancesquared(const Vector2f& first, const Vector2f& second) { float r = 0; - for (int i=0; i < 2; i++) - r += (first[i]-second[i])*(first[i]-second[i]); + for (int i = 0; i < 2; i++) + r += (first[i] - second[i]) * (first[i] - second[i]); return (r); } diff --git a/src/math/vector2f.h b/src/math/vector2f.h index ba60aa1..1a5f4de 100644 --- a/src/math/vector2f.h +++ b/src/math/vector2f.h @@ -19,96 +19,104 @@ class Vector2f public: /// default constructor Vector2f(); - + /// assignment constructor Vector2f(const float x, const float y); - + /// copy constructor Vector2f(const Vector2f &other); - + /// assign (0,0) void clear(); - + /// assignment operator void assign(const float x, const float y); - + /// assignment operator void assign(const Vector2f &other); - + /// assignment operator Vector2f & operator=(const Vector2f &other); - + /// vector subtraction Vector2f & operator-=(const Vector2f &other); - + /// vector sum Vector2f & operator+=(const Vector2f &other); - + /// vector subtraction inline Vector2f operator+(const Vector2f &other) const { - Vector2f v(coord[0] + other.coord[0], coord[1]+other.coord[1]); + Vector2f v(coord[0] + other.coord[0], coord[1] + other.coord[1]); return v; } - + /// vector sum 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); + 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]; } - + /// x coordinate inline float width() const { return coord[0]; } - + /// y coordinate inline float height() const { return coord[1]; } /// x coordinate - inline float x() const { return coord[0]; } - + inline float x() const { + return coord[0]; + } + /// y coordinate - inline float y() const { return coord[1]; } + inline float y() const { + return coord[1]; + } /// mutable reference to the x coordinate - inline float & get_x() { return coord[0]; } + inline float & get_x() { + return coord[0]; + } /// mutable reference to the y coordinate - inline float & get_y() { return coord[1]; } - + inline float & get_y() { + return coord[1]; + } + /// a pointer to the internal data 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])); } diff --git a/src/math/vector3f.cc b/src/math/vector3f.cc index 1e9fd2b..4594665 100644 --- a/src/math/vector3f.cc +++ b/src/math/vector3f.cc @@ -56,35 +56,35 @@ Vector3f & Vector3f::operator=(const Vector3f & other) Vector3f & Vector3f::operator*=(const float scalar) { - for (int i=0; i < 3; i++) + 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++) + 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++) + 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++) + for (int i = 0; i < 3; i++) coord[i] += other[i]; return (*this); } bool Vector3f::operator==(const Vector3f& other) const { - for (int i=0; i < 3; i++) + for (int i = 0; i < 3; i++) if (coord[i] != other.coord[i]) return (false); return (true); @@ -93,17 +93,17 @@ bool Vector3f::operator==(const Vector3f& other) const float Vector3f::lengthsquared() const { float r = 0; - for (int i=0; i < 3; i++) - r += coord[i]*coord[i]; + 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]; - + for (int i = 0; i < 3; i++) + r += coord[i] * coord[i]; + return (sqrtf(r)); } @@ -125,24 +125,24 @@ std::ostream &operator<<(std::ostream & os, const Vector3f & vector) std::istream &operator>>(std::istream & is, Vector3f & vector) { - for (int i=0; i < 3; i++) + for (int i = 0; i < 3; i++) is >> vector[i]; return is; } -const Vector3f crossproduct(const Vector3f & first, Vector3f const &second) +const Vector3f crossproduct(const Vector3f & first, const Vector3f &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 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++) + for (int i = 0; i < 3; i++) r += first[i] * second[i]; return (r); } @@ -150,17 +150,17 @@ float dotproduct(const Vector3f& first, const Vector3f& second) 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]); - + 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]); + for (int i = 0; i < 3; i++) + r += (first[i] - second[i]) * (first[i] - second[i]); return (r); } diff --git a/src/math/vector3f.h b/src/math/vector3f.h index 99a665f..09e6e84 100644 --- a/src/math/vector3f.h +++ b/src/math/vector3f.h @@ -23,81 +23,81 @@ class Vector3f public: /// default constructor, assigns (0,0,0) Vector3f(); - + /// 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 /** @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 ~Vector3f(); - + /* -- Assignment operators -- */ /// assign (0, 0, 0) void clear(); - + /// assignment void assign(const float x, const float y, const float z); - + /// assignment void assign(const Vector3f & other); - + /// assignment operator Vector3f& operator=(const Vector3f &other); - + /// scalar multiplication assignment /** @param scalar multiplication factor */ Vector3f& operator*=(const float scalar); - + /// scalar division assignment /** @param scalar divider */ Vector3f& operator/=(const float scalar); - + /// vector sum assignment Vector3f& operator+=(const Vector3f &other); - + /// vector difference assignment Vector3f& operator-=(const Vector3f &other); - + /* -- Mathematical operators -- */ - + /// 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 /*! range is not checked * @param index the index of the element to assign to ( 0 <= index < 3 ) @@ -105,7 +105,7 @@ public: inline float& operator[](const size_t index) { return coord[index]; } - + /// returns the value of an element of this vector /*! range is not checked * @param index the index of the element to return ( 0 <= index < 3 ) @@ -113,61 +113,75 @@ public: inline float operator[](const size_t index) const { return coord[index]; } - + /// x coordinate - inline float x() const { return coord[0]; } + inline float x() const { + return coord[0]; + } /// y coordinate - inline float y() const { return coord[1]; } + inline float y() const { + return coord[1]; + } /// z coordinate - inline float z() const { return coord[2]; } + inline float z() const { + return coord[2]; + } - /// mutable reference to the x coordinate - inline float & get_x() { return coord[0]; } + /// mutable reference to the x coordinate + inline float & get_x() { + return coord[0]; + } /// mutable reference to the y coordinate - inline float & get_y() { return coord[1]; } + inline float & get_y() { + return coord[1]; + } /// mutable reference to the z coordinate - inline float & get_z() { return coord[2]; } + inline float & get_z() { + return coord[2]; + } /// a pointer to the internal data - inline float *ptr() const { return (float *) coord; } + inline float *ptr() const { + return (float *) coord; + } /// cartesian length float length() const; - + /// cartesian length squared float lengthsquared() const; - + /// divide this Vector by its length /// @see normalized() /// vector must not be (0, 0, 0) void normalize(); - + /* static functions */ - + /// Returns the unity vector on the X-axis 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); } - + /// Returns the unity vector on the Z-axis 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) |