diff options
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/color.cc | 50 | ||||
-rw-r--r-- | src/math/color.h | 64 | ||||
-rw-r--r-- | src/math/functions.cc | 35 | ||||
-rw-r--r-- | src/math/functions.h | 7 | ||||
-rw-r--r-- | src/math/mathlib.h | 4 | ||||
-rw-r--r-- | src/math/vector3f.cc | 170 | ||||
-rw-r--r-- | src/math/vector3f.h | 16 |
7 files changed, 189 insertions, 157 deletions
diff --git a/src/math/color.cc b/src/math/color.cc index e41a5aa..b0c3f65 100644 --- a/src/math/color.cc +++ b/src/math/color.cc @@ -1,36 +1,42 @@ /* 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 + 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 { +namespace math +{ -Color::Color() { +Color::Color() +{ _r = _g = _b = 0.0f; _a = 1.0f; } -Color::Color(const float red, const float green , const float blue , const float alpha) { +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) { +Color::Color(const float grey, const float alpha) +{ _r = _g = _b = grey; _a = alpha; } -Color::Color(const Color &other) { +Color::Color(const Color &other) +{ this->operator=(other); } -const Color & Color::operator=(const Color &other) { +const Color & Color::operator=(const Color &other) +{ this->_r = other._r; this->_g = other._g; this->_b = other._b; @@ -38,45 +44,53 @@ const Color & Color::operator=(const Color &other) { return (*this); } -void Color::normalize() { +void Color::normalize() +{ float tmp = _r; if (_g > tmp) tmp = _g; - if ( _b > tmp) + if (_b > tmp) tmp = _b; if (tmp > 1) { _r /= tmp; _g /= tmp; _b /= tmp; - } + } } -float Color::red() const { +float Color::red() const +{ return _r; } -float Color::green() const { +float Color::green() const +{ return _g; } -float Color::blue() const { +float Color::blue() const +{ return _b; } -float Color::alpha() const { +float Color::alpha() const +{ return _a; } -Color Color::operator*(float scalar) const { +Color Color::operator*(float scalar) const +{ return Color(red()*scalar, green()*scalar, blue()*scalar, alpha()); } -Color operator*(float scalar, const Color& color) { +Color operator*(float scalar, const Color& color) +{ return color * scalar; } -std::ostream &operator<<(std::ostream &os, const Color &c) { +std::ostream &operator<<(std::ostream &os, const Color &c) +{ os << c.red() << " " << c.green() << " " << c.blue() << " " << c.alpha(); return os; } diff --git a/src/math/color.h b/src/math/color.h index 2c69b9b..33efcfc 100644 --- a/src/math/color.h +++ b/src/math/color.h @@ -1,44 +1,46 @@ /* 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 + 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> +#include <iostream> -namespace math { +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; +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); diff --git a/src/math/functions.cc b/src/math/functions.cc index 0e394c1..53ce41e 100644 --- a/src/math/functions.cc +++ b/src/math/functions.cc @@ -1,39 +1,47 @@ /* 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 + 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 { +namespace math +{ -float min(float a, float b) { +float min(float a, float b) +{ return (a < b ? a : b); } -float max(float a, float b) { +float max(float a, float b) +{ return (a > b ? a : b); } -int min(int a, int b) { +int min(int a, int b) +{ return (a < b ? a : b); } -int max(int a, int b) { +int max(int a, int b) +{ return (a > b ? a : b); } -float randomf(const float max) { +float randomf(const float max) +{ return ((float) rand() / (float) RAND_MAX) * max; } -unsigned randomi(const unsigned int max) { +unsigned randomi(const unsigned int max) +{ return ((unsigned int)(rand() % max)); } -float degrees180f(float angle) { +float degrees180f(float angle) +{ float r = angle; while (r <= -180.0f) r += 360.0f; @@ -42,7 +50,8 @@ float degrees180f(float angle) { return r; } -float degrees360f(float angle) { +float degrees360f(float angle) +{ float r = angle; while (r < 0.0f) r += 360.0f; @@ -53,9 +62,9 @@ float degrees360f(float angle) { float sgnf(float value) { - if (value < 0 ) + if (value < 0) return -1; - else if (value == 0 ) + else if (value == 0) return 0; return 1; diff --git a/src/math/functions.h b/src/math/functions.h index 767af2d..df86124 100644 --- a/src/math/functions.h +++ b/src/math/functions.h @@ -1,7 +1,7 @@ /* 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 + 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__ @@ -11,7 +11,8 @@ #include <cstdlib> #include <cmath> -namespace math { +namespace math +{ /// return the smallest of two float values float min(float a, float b); diff --git a/src/math/mathlib.h b/src/math/mathlib.h index 2e1c245..ca5b7f4 100644 --- a/src/math/mathlib.h +++ b/src/math/mathlib.h @@ -1,7 +1,7 @@ /* 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 + 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__ diff --git a/src/math/vector3f.cc b/src/math/vector3f.cc index da3ff12..277795b 100644 --- a/src/math/vector3f.cc +++ b/src/math/vector3f.cc @@ -1,7 +1,7 @@ /* 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 + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 */ // project headers @@ -10,151 +10,155 @@ // C++ headers #include <cmath> -namespace math { +namespace math +{ -Vector3f::Vector3f() : - x(coord[0]), y(coord[1]), z(coord[2]) +Vector3f::Vector3f() : + x(coord[0]), y(coord[1]), z(coord[2]) { - for (int i=0; i < 3; i++) - coord[i] = 0; + for (int i=0; i < 3; i++) + coord[i] = 0; } - -Vector3f::Vector3f(const Vector3f &other) : - x(coord[0]), y(coord[1]), z(coord[2]) +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]) +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() { } - -Vector3f & Vector3f::operator=(const Vector3f & other) +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) +Vector3f & Vector3f::operator*=(const float scalar) { - for (int i=0; i < 3; i++) - coord[i] *= scalar; - return (*this); + 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 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 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 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 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); +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 { +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::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]; +float Vector3f::length() const +{ + double r = 0; + for (int i=0; i < 3; i++) + r += coord[i]*coord[i]; - return ((float) sqrt(r)); + return ((float) sqrt(r)); } -void Vector3f::normalize() { - (*this) /= this->length(); +void Vector3f::normalize() +{ + (*this) /= this->length(); } -std::ostream &operator<<(std::ostream & os, const Vector3f & vector) { +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) { +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) { +Vector3f operator*(float scalar, const Vector3f& vector) +{ return vector * scalar; } diff --git a/src/math/vector3f.h b/src/math/vector3f.h index 8bc62ff..cc318b0 100644 --- a/src/math/vector3f.h +++ b/src/math/vector3f.h @@ -1,7 +1,7 @@ /* 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 + 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__ @@ -13,13 +13,15 @@ // C++ headers #include <iostream> -namespace math { +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 { +class Vector3f +{ public: /// Default constructor, creates a Vector3f (0,0,0) @@ -96,14 +98,14 @@ public: 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; |