diff options
-rw-r--r-- | src/math/Makefile.am | 9 | ||||
-rw-r--r-- | src/math/color.cc | 84 | ||||
-rw-r--r-- | src/math/color.h | 51 | ||||
-rw-r--r-- | src/math/functions.cc | 64 | ||||
-rw-r--r-- | src/math/functions.h | 52 | ||||
-rw-r--r-- | src/math/mathlib.h | 20 | ||||
-rw-r--r-- | src/math/plane.cc | 28 | ||||
-rw-r--r-- | src/math/plane.h | 33 | ||||
-rw-r--r-- | src/math/vector3f.cc | 161 | ||||
-rw-r--r-- | src/math/vector3f.h | 148 |
10 files changed, 650 insertions, 0 deletions
diff --git a/src/math/Makefile.am b/src/math/Makefile.am new file mode 100644 index 0000000..78a52fc --- /dev/null +++ b/src/math/Makefile.am @@ -0,0 +1,9 @@ +METASOURCES = AUTO + +libmath_la_SOURCES = color.cc functions.cc plane.cc vector3f.cc +libmath_la_LDFLAGS = -lm -avoid-version -no-undefined + +noinst_LTLIBRARIES = libmath.la +noinst_HEADERS = color.h functions.h mathlib.h plane.h vector3f.h + +INCLUDES = -I$(top_srcdir)/src diff --git a/src/math/color.cc b/src/math/color.cc new file mode 100644 index 0000000..e41a5aa --- /dev/null +++ b/src/math/color.cc @@ -0,0 +1,84 @@ +/* + math/color.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/color.h" + +namespace math { + +Color::Color() { + _r = _g = _b = 0.0f; + _a = 1.0f; +} + +Color::Color(const float red, const float green , const float blue , const float alpha) { + _r = red; + _g = green; + _b = blue; + _a = alpha; +} + +Color::Color(const float grey, const float alpha) { + _r = _g = _b = grey; + _a = alpha; +} + +Color::Color(const Color &other) { + this->operator=(other); +} + +const Color & Color::operator=(const Color &other) { + this->_r = other._r; + this->_g = other._g; + this->_b = other._b; + this->_a = other._a; + return (*this); +} + +void Color::normalize() { + float tmp = _r; + + if (_g > tmp) + tmp = _g; + if ( _b > tmp) + tmp = _b; + + if (tmp > 1) { + _r /= tmp; + _g /= tmp; + _b /= tmp; + } +} + +float Color::red() const { + return _r; +} + +float Color::green() const { + return _g; +} + +float Color::blue() const { + return _b; +} + +float Color::alpha() const { + return _a; +} + +Color Color::operator*(float scalar) const { + return Color(red()*scalar, green()*scalar, blue()*scalar, alpha()); +} + +Color operator*(float scalar, const Color& color) { + return color * scalar; +} +std::ostream &operator<<(std::ostream &os, const Color &c) { + os << c.red() << " " << c.green() << " " << c.blue() << " " << c.alpha(); + return os; +} + +} // namespace math diff --git a/src/math/color.h b/src/math/color.h new file mode 100644 index 0000000..2c69b9b --- /dev/null +++ b/src/math/color.h @@ -0,0 +1,51 @@ +/* + common/color.h + 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_COLOR_H__ +#define __INCLUDED_MATH_COLOR_H__ + +#include <iostream> + +namespace math { + +/// a class representing an RGBA color value +class Color { + public: + Color(); + Color(const float, float const, const float, const float=1.0f); + Color(const float, const float=1.0f); + Color(const Color &); + + float red() const; + float green() const; + float blue() const; + float alpha() const; + + const Color &operator=(const Color &); + + Color operator*(const float scalar) const; + + // Some default colors + static const Color Black() { return Color(0.0f); }; + static const Color White() { return Color(1.0f); }; + static const Color Red() { return Color(1.0f,0.0f,0.0f); }; + static const Color Green() { return Color(0.0f,1.0f,0.0f); }; + static const Color Blue() { return Color(0.0f, 0.0f, 1.0f); }; + static const Color Yellow() { return Color(1.0f, 1.0f, 0.0f); }; + + private: + void normalize(); + float _r, _g, _b, _a; +}; + +std::ostream &operator<<(std::ostream &os, const Color &c); + +Color operator*(const float scalar, const Color& color); + +} // namespace math + +#endif // __INCLUDED_MATH_COLOR_H__ + diff --git a/src/math/functions.cc b/src/math/functions.cc new file mode 100644 index 0000000..0e394c1 --- /dev/null +++ b/src/math/functions.cc @@ -0,0 +1,64 @@ +/* + common/functions.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/functions.h" + +namespace math { + +float min(float a, float b) { + return (a < b ? a : b); +} + +float max(float a, float b) { + return (a > b ? a : b); +} + +int min(int a, int b) { + return (a < b ? a : b); +} + +int max(int a, int b) { + return (a > b ? a : b); +} + +float randomf(const float max) { + return ((float) rand() / (float) RAND_MAX) * max; +} + +unsigned randomi(const unsigned int max) { + return ((unsigned int)(rand() % max)); +} + +float degrees180f(float angle) { + float r = angle; + while (r <= -180.0f) + r += 360.0f; + while (r >= 180.0f) + r -= 360.0f; + return r; +} + +float degrees360f(float angle) { + float r = angle; + while (r < 0.0f) + r += 360.0f; + while (r >= 360.0f) + r -= 360.0f; + return r; +} + +float sgnf(float value) +{ + if (value < 0 ) + return -1; + else if (value == 0 ) + return 0; + + return 1; +} + +} // namespace math diff --git a/src/math/functions.h b/src/math/functions.h new file mode 100644 index 0000000..767af2d --- /dev/null +++ b/src/math/functions.h @@ -0,0 +1,52 @@ +/* + math/functions.h + 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_FUNCTIONS_H__ +#define __INCLUDED_MATH_FUNCTIONS_H__ + +// C++ headers +#include <cstdlib> +#include <cmath> + +namespace math { + +/// return the smallest of two float values +float min(float a, float b); + +/// return the smallest of two integers +int min(int a, int b); + +/// return the largest of two float values +float max(float a, float b); + +/// return the largest of two integers +int max(int a, int b); + +/// returns a random float +/** The value returned will be in the interval [0-max] + * @param max the maximum value returned + **/ +float randomf(const float max = 1); + +/// returns a random integer +/** The value returned will be in the interval [0-(max-1)] + * @param max the maximum value returned + **/ +unsigned int randomi(const unsigned int max); + +/// return the sign of a float value +float sgnf(float value); + +/// return an angle in the ]-180,180] range +float degrees180f(float angle); + +/// return an angle in the [0,360[ range +float degrees360f(float angle); + +} // namespace math + +#endif // __INCLUDED_MATH_FUNCTIONS_H__ + diff --git a/src/math/mathlib.h b/src/math/mathlib.h new file mode 100644 index 0000000..2e1c245 --- /dev/null +++ b/src/math/mathlib.h @@ -0,0 +1,20 @@ +/* + math/math.h + 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_H__ +#define __INCLUDED_MATH_H__ + +/// this namespace contains mathematical classes and functions +/** This is an independent library + */ +namespace math {} + +#include "math/vector3f.h" +#include "math/color.h" +#include "math/functions.h" + +#endif // __INCLUDED_MATH_H__ + diff --git a/src/math/plane.cc b/src/math/plane.cc new file mode 100644 index 0000000..21172a4 --- /dev/null +++ b/src/math/plane.cc @@ -0,0 +1,28 @@ +/* + common/plane.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/plane.h" + +// C++ headers +#include <cmath> + +namespace math { + +Plane::Plane(Vector3f const &a, Vector3f const &b, Vector3f const &c) +{ + p0 = a; + // FIXME calculate plane normal +} + +Plane::Plane(Vector3f const &p, Vector3f const &n) +{ + p0 = p; + pn = n; +} + +} // namespace math + diff --git a/src/math/plane.h b/src/math/plane.h new file mode 100644 index 0000000..39207b9 --- /dev/null +++ b/src/math/plane.h @@ -0,0 +1,33 @@ +/* + common/plane.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_PLANE_H__ +#define __INCLUDED_MATH_PLANE_H__ + +// project headers +#include "math/vector3f.h" + +// C++ headers +#include <iostream> + +namespace math { + +/// a plane in 3D space +class Plane { +public: + Plane(Vector3f const &a, Vector3f const &b, Vector3f const &c); + Plane(Vector3f const &p, Vector3f const &n); + +private: + // Point on the plane + Vector3f p0; + // Plane normal + Vector3f pn; +}; // class Plane + +} // namespace math + +#endif // __INCLUDED_MATH_PLANE_H__ 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 diff --git a/src/math/vector3f.h b/src/math/vector3f.h new file mode 100644 index 0000000..8bc62ff --- /dev/null +++ b/src/math/vector3f.h @@ -0,0 +1,148 @@ +/* + 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 +*/ + +#ifndef __INCLUDED_MATH_VECTOR3F_H__ +#define __INCLUDED_MATH_VECTOR3F_H__ + +// project headers +#include "math/vector3f.h" + +// C++ headers +#include <iostream> + +namespace math { + +/// a point or vector in 3D-space +/** An instance of this class represents a point in 3D-space or a 3D-vector + * and forms the basic building block for all spatial calculations. + */ +class Vector3f { + +public: + /// Default constructor, creates a Vector3f (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 xv the x-coordinate of the location + * @param yv the y-coordinate of the location + * @param zv the z-coordinate of the location + */ + Vector3f(const float xv, const float yv, const float zv); + + /// Destructor + ~Vector3f(); + + /* -- Assignment operators -- */ + /// assignment operator + Vector3f& operator=(const Vector3f &other); + + /// multiplicate each element of the vector with a given value + /** @param scalar multiplication factor + */ + Vector3f& operator*=(const float scalar); + + /// divide each element of the vector by a given value + /** @param scalar divider + */ + Vector3f& operator/=(const float scalar); + + /// perform an element-wise subtraction + Vector3f& operator-=(const Vector3f &other); + + /// perform ann element-wise addition + 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 sumn of two vectors + Vector3f operator+(const Vector3f &other) const; + + /// return the vector cross-product + float operator*(const Vector3f &other) const; + + /// comparison operator + bool operator==(const Vector3f &other) const; + + /// assign a value to an element of this vector + /*! WARNING: range is not checked + * @param index the index of the element to assign to ( 0 <= index < 3 ) + */ + inline float& operator[](const unsigned int index) { + return coord[index]; + } + + /// returns the value of an element of this vector + /*! WARNING: range is not checked + * @param index the index of the element to return ( 0 <= index < 3 ) + */ + inline float operator[](const unsigned int index) const { + return coord[index]; + } + + float &x; + float &y; + float &z; + + /// Return the cartesian length of this vector + float length() const; + + /// Return the cartesian length squared (to speed up calculations) + float lengthsquared() const; + + /// Divide this Vector by it's length + /// @see normalized() + /// WARNING: 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); } + + /// Return the cartesian length of a vector + 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()); } + + float coord[3]; +}; + +/// Write a Vector3f to a std::ostream +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); + +} // namespace Math + +#endif // __INCLUDED_MATH_VECTOR3F_H__ |