diff options
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/Makefile.am | 6 | ||||
-rw-r--r-- | src/math/axis.cc | 59 | ||||
-rw-r--r-- | src/math/axis.h | 27 | ||||
-rw-r--r-- | src/math/color.h | 7 | ||||
-rw-r--r-- | src/math/mathlib.h | 7 | ||||
-rw-r--r-- | src/math/matrix4f.cc | 79 | ||||
-rw-r--r-- | src/math/matrix4f.h | 58 | ||||
-rw-r--r-- | src/math/vector3f.cc | 2 | ||||
-rw-r--r-- | src/math/vector3f.h | 4 |
9 files changed, 224 insertions, 25 deletions
diff --git a/src/math/Makefile.am b/src/math/Makefile.am index 465f9b0..7ea7d66 100644 --- a/src/math/Makefile.am +++ b/src/math/Makefile.am @@ -1,9 +1,11 @@ METASOURCES = AUTO -libmath_la_SOURCES = axis.cc color.cc functions.cc plane3f.cc vector3f.cc +libmath_la_SOURCES = axis.cc color.cc functions.cc matrix4f.cc plane3f.cc \ + vector3f.cc libmath_la_LDFLAGS = -avoid-version -no-undefined -lm noinst_LTLIBRARIES = libmath.la -noinst_HEADERS = axis.h color.h functions.h mathlib.h plane3f.h vector3f.h +noinst_HEADERS = axis.h color.h functions.h mathlib.h matrix4f.h plane3f.h \ + vector3f.h INCLUDES = -I$(top_srcdir)/src diff --git a/src/math/axis.cc b/src/math/axis.cc index 7bd5262..cbca99b 100644 --- a/src/math/axis.cc +++ b/src/math/axis.cc @@ -6,6 +6,7 @@ // project headers #include "math/axis.h" +#include "math/mathlib.h" namespace math { @@ -37,18 +38,56 @@ Axis & Axis::operator=(const Axis & other) { return *this; } -// alter heading, rotate around Z-axis (positive is left) -void Axis::direction(const float angle) { - for (size_t i=0; i < 3; i++) { - //axis_vector[i].rotate(axis_vector[2], angle); - } +// change heading, rotate around Z-axis (positive is left) +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; + + axis_vector[0].assign(forward); + axis_vector[1].assign(left); } -// alter heading, rotate around negative Y-axis (positive is up) -void Axis::pitch(const float pitch) { - for (size_t i=0; i < 3; i++) { - //axis_vector[i].rotate(axis_vector[1], -angle); - } +// change pitch, rotate around negative Y-axis (positive is up) +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) { + 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); +} + +// write an axis to a std::ostream +std::ostream &operator<<(std::ostream & os, Axis const & axis) +{ + os << axis.forward() << " " << axis.left() << " " << axis.up(); + return os; +} + +// read an axis from a std::istream +std::istream &operator>>(std::istream & is, Axis & axis) +{ + is >> axis[0]; + is >> axis[1]; + is >> axis[2]; + return is; } } diff --git a/src/math/axis.h b/src/math/axis.h index 73474ef..7e81d9a 100644 --- a/src/math/axis.h +++ b/src/math/axis.h @@ -25,28 +25,39 @@ public: void assign(const Axis & other); /// global coordinates of the X-axis in the local coordinates system - inline Vector3f const & forward() { return axis_vector[0]; } + inline Vector3f const & forward() const { return axis_vector[0]; } /// global coordinates of the Y-axis in the local coordinates system - inline Vector3f const & left() { return axis_vector[1]; } + inline Vector3f const & left() const { return axis_vector[1]; } /// global coordinates of the Z-axis in the local coordinates system - inline Vector3f const & up() { return axis_vector[2]; } + inline Vector3f const & up() const { return axis_vector[2]; } - inline Vector3f const operator[](size_t index) { return axis_vector[index]; } + inline Vector3f & operator[](size_t index) { return axis_vector[index]; } + + inline Vector3f const & operator[](size_t index) const { return axis_vector[index]; } Axis & operator=(const Axis & other); - /// alter heading, rotate around Z-axis (positive is left) - void direction(const float angle); + /// 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); - /// alter heading, rotate around negative Y-axis (positive is up) - void pitch(const float angle); + /// change roll, rotate around forward vector (positive is left) + void change_roll(const float angle); private: Vector3f axis_vector[3]; }; +/// write an axis to a std::ostream +std::ostream &operator<<(std::ostream & os, Axis const & axis); + +/// read an axis from a std::istream +std::istream &operator>>(std::istream & is, Axis & axis); + } #endif // __INCLUDED_MATH_AXIS_H__ diff --git a/src/math/color.h b/src/math/color.h index 7fa0d68..0dd5fc8 100644 --- a/src/math/color.h +++ b/src/math/color.h @@ -46,7 +46,10 @@ public: /// multiply rgb values with scalar value. Color operator*(const float scalar) const; - /// clam color values to the 0-1 range + /// 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; @@ -54,6 +57,8 @@ public: float &b; float &a; + +private: float rgba_data[4]; }; diff --git a/src/math/mathlib.h b/src/math/mathlib.h index b76c562..77ccde5 100644 --- a/src/math/mathlib.h +++ b/src/math/mathlib.h @@ -4,8 +4,8 @@ the terms of the GNU General Public License version 2 */ -#ifndef __INCLUDED_MATH_H__ -#define __INCLUDED_MATH_H__ +#ifndef __INCLUDED_MATHLIB_H__ +#define __INCLUDED_MATHLIB_H__ /// this namespace contains mathematical classes and functions /** This is an independent library @@ -14,8 +14,9 @@ namespace math {} #include "math/vector3f.h" #include "math/plane3f.h" +#include "math/matrix4f.h" #include "math/color.h" #include "math/functions.h" -#endif // __INCLUDED_MATH_H__ +#endif // __INCLUDED_MATHLIB_H__ diff --git a/src/math/matrix4f.cc b/src/math/matrix4f.cc new file mode 100644 index 0000000..d73b12e --- /dev/null +++ b/src/math/matrix4f.cc @@ -0,0 +1,79 @@ +/* + math/matrix4f.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/matrix4f.h" + +// C++ headers +#include <cmath> + +namespace math +{ + +Matrix4f::Matrix4f() +{ + clear(); +} + +Matrix4f::Matrix4f(const Matrix4f & other) +{ + assign(other); +} + +Matrix4f::Matrix4f(const Axis & axis) +{ + assign(axis); +} + +void Matrix4f::clear() +{ + memset(matrix, 0, sizeof(matrix)); +} + +void Matrix4f::unity() +{ + memset(matrix, 0, sizeof(matrix)); + for (int i=0; i <4; i++) + matrix[i][i] = 1; +} + +void Matrix4f::assign(const Matrix4f & other) +{ + memcpy(matrix, other.matrix, sizeof(matrix)); +} + +void Matrix4f::assign(const Axis & axis) +{ + memset(matrix, 0, sizeof(matrix)); + for (int i=0; i < 3; i++) { + memcpy(&matrix[i][0], axis[i].ptr(), sizeof(float) * 3); + } + matrix[3][3] = 1; +} + +Matrix4f & Matrix4f::operator=(const Matrix4f &other) +{ + assign(other); + return(*this); +} + +Matrix4f & Matrix4f::operator=(const Axis & axis) +{ + assign(axis); + return(*this); +} + +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]; + return t; +} + +} diff --git a/src/math/matrix4f.h b/src/math/matrix4f.h new file mode 100644 index 0000000..0d7df90 --- /dev/null +++ b/src/math/matrix4f.h @@ -0,0 +1,58 @@ +/* + math/matrix4f.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_MATH_MATRIX4F_H__ +#define __INCLUDED_MATH_MATRIX4F_H__ + +// project headers +#include "math/axis.h" + +// C++ headers +#include <iostream> + +namespace math +{ + +/// a transformation matrix +class Matrix4f +{ +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]; +}; + +} + +#endif // __INCLUDED_MATH_MATRIX4F_H__ + diff --git a/src/math/vector3f.cc b/src/math/vector3f.cc index 3d8e5ac..51d0385 100644 --- a/src/math/vector3f.cc +++ b/src/math/vector3f.cc @@ -1,5 +1,5 @@ /* - common/vector3f.cc + 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 */ diff --git a/src/math/vector3f.h b/src/math/vector3f.h index e94f4c7..ddb9e38 100644 --- a/src/math/vector3f.h +++ b/src/math/vector3f.h @@ -113,6 +113,9 @@ public: /// WARNING: vector must not be (0, 0, 0) void normalize(); + /// a pointer to the internal data + inline float *ptr() const { return (float *) coord; } + /* static functions */ /// Returns the unity vector on the X-axis @@ -136,6 +139,7 @@ public: float &y; float &z; +private: float coord[3]; }; |