diff options
Diffstat (limited to 'src/math')
-rw-r--r-- | src/math/README.txt | 13 | ||||
-rw-r--r-- | src/math/all.h | 9 | ||||
-rw-r--r-- | src/math/const.h | 18 | ||||
-rw-r--r-- | src/math/func.h | 29 | ||||
-rw-r--r-- | src/math/geometry.h | 15 | ||||
-rw-r--r-- | src/math/intpoint.h | 18 | ||||
-rw-r--r-- | src/math/intsize.h | 61 | ||||
-rw-r--r-- | src/math/matrix.h | 24 | ||||
-rw-r--r-- | src/math/point.h | 9 | ||||
-rw-r--r-- | src/math/size.h | 66 | ||||
-rw-r--r-- | src/math/vector.h | 9 |
11 files changed, 76 insertions, 195 deletions
diff --git a/src/math/README.txt b/src/math/README.txt index 1a5ce93..fd34dcb 100644 --- a/src/math/README.txt +++ b/src/math/README.txt @@ -1,3 +1,12 @@ -src/math +/** + * \dir math + * \brief Common mathematical structures and functions + */ -Contains common mathematical structures and functions. +/** + * \namespace Math + * \brief Namespace for (new) math code + * + * This namespace was created to avoid clashing with old code, but now it still serves, + * defining a border between math and non-math-related code. + */
\ No newline at end of file diff --git a/src/math/all.h b/src/math/all.h index 13a9290..4ac9d55 100644 --- a/src/math/all.h +++ b/src/math/all.h @@ -14,14 +14,13 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -/** @defgroup MathAllModule math/all.h - Includes all other math module headers. +/** + * \file math/all.h + * \brief Includes all other math module headers */ #pragma once -/* @{ */ // start of group - #include "const.h" #include "func.h" #include "point.h" @@ -30,5 +29,3 @@ #include "geometry.h" #include "conv.h" - -/* @} */ // end of group diff --git a/src/math/const.h b/src/math/const.h index dd7ab0f..0b6f971 100644 --- a/src/math/const.h +++ b/src/math/const.h @@ -14,28 +14,30 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -/** @defgroup MathConstModule math/const.h - Contains the math constants used in math functions. +/** + * \file math/const.h + * \brief Constants used in math functions */ #pragma once +#include <cmath> + // Math module namespace namespace Math { -/* @{ */ // start of group //! Tolerance level -- minimum accepted float value const float TOLERANCE = 1e-6f; //! Very small number (used in testing/returning some values) -const float VERY_SMALL = 1e-6f; +const float VERY_SMALL_NUM = 1e-6f; //! Very big number (used in testing/returning some values) -const float VERY_BIG = 1e6f; +const float VERY_BIG_NUM = 1e6f; //! Huge number -const float HUGE = 1.0e+38f; +const float HUGE_NUM = 1.0e+38f; //! PI const float PI = 3.14159265358979323846f; @@ -45,6 +47,8 @@ const float DEG_TO_RAD = 0.01745329251994329547f; //! Radians to degrees multiplier const float RAD_TO_DEG = 57.29577951308232286465f; -/* @} */ // end of group +//! Natural logarithm of 2 +const float LOG_2 = log(2.0f); + }; // namespace Math diff --git a/src/math/func.h b/src/math/func.h index 2127d1a..541b084 100644 --- a/src/math/func.h +++ b/src/math/func.h @@ -15,8 +15,9 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -/** @defgroup MathFuncModule math/func.h - Contains common math functions. +/** + * \file math/func.h + * \brief Common math functions */ #pragma once @@ -31,8 +32,6 @@ namespace Math { -/* @{ */ // start of group - //! Compares \a a and \a b within \a tolerance inline bool IsEqual(float a, float b, float tolerance = Math::TOLERANCE) { @@ -127,6 +126,14 @@ inline float Rand() return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); } +//! Returns the next nearest power of two to \a x +inline int NextPowerOfTwo(int x) +{ + double logbase2 = log(static_cast<float>(x)) / Math::LOG_2; + return static_cast<int>(pow(2, ceil(logbase2)) + 0.5); +} + + //! Returns a normalized angle, that is in other words between 0 and 2 * PI inline float NormAngle(float angle) { @@ -180,11 +187,13 @@ inline float Direction(float a, float g) //! Managing the dead zone of a joystick. /** -\verbatimin: -1 0 1 +\verbatim +in: -1 0 1 --|-------|----o----|-------|--> <----> dead -out: -1 0 0 1\endverbatim */ +out: -1 0 0 1 +\endverbatim */ inline float Neutral(float value, float dead) { if ( fabs(value) <= dead ) @@ -218,7 +227,8 @@ inline float Smooth(float actual, float hope, float time) //! Bounces any movement /** -\verbatimout +\verbatim +out | 1+------o-------o--- | o | o o | | bounce @@ -227,7 +237,8 @@ inline float Smooth(float actual, float hope, float time) | o | | -o------|-------+----> progress 0| | 1 - |<---->|middle\endverbatim */ + |<---->|middle +\endverbatim */ inline float Bounce(float progress, float middle = 0.3f, float bounce = 0.4f) { if ( progress < middle ) @@ -242,6 +253,4 @@ inline float Bounce(float progress, float middle = 0.3f, float bounce = 0.4f) } } -/* @} */ // end of group - }; // namespace Math diff --git a/src/math/geometry.h b/src/math/geometry.h index 61d1868..1c5f60f 100644 --- a/src/math/geometry.h +++ b/src/math/geometry.h @@ -15,9 +15,9 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -/** @defgroup MathGeometryModule math/geometry.h - Contains math functions related to 3D geometry calculations, - transformations, etc. +/** + * \file math/geometry.h + * \brief Math functions related to 3D geometry calculations, transformations, etc. */ #pragma once @@ -36,18 +36,15 @@ namespace Math { -/* @{ */ // start of group - - //! Returns py up on the line \a a - \a b inline float MidPoint(const Math::Point &a, const Math::Point &b, float px) { if (IsEqual(a.x, b.x)) { if (a.y < b.y) - return HUGE; + return Math::HUGE_NUM; else - return -HUGE; + return -Math::HUGE_NUM; } return (b.y-a.y) * (px-a.x) / (b.x-a.x) + a.y; } @@ -566,6 +563,4 @@ inline Math::Vector RotateView(Math::Vector center, float angleH, float angleV, return eye+center; } -/* @} */ // end of group - }; // namespace Math diff --git a/src/math/intpoint.h b/src/math/intpoint.h index a760bc2..8e13b19 100644 --- a/src/math/intpoint.h +++ b/src/math/intpoint.h @@ -14,31 +14,29 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -/** @defgroup MathIntPointModule math/intpoint.h - Contains the IntPoint struct. +/** + * \file math/intpoint.h + * \brief IntPoint struct */ #pragma once namespace Math { -/* @{ */ // start of group - /** - * \struct IntPoint 2D Point with integer coords + * \struct IntPoint + * \brief 2D Point with integer coords * * Analog of WinAPI's POINT struct. */ struct IntPoint { //! X coord - long x; + int x; //! Y coord - long y; + int y; - IntPoint(long aX = 0, long aY = 0) : x(aX), y(aY) {} + IntPoint(int aX = 0, int aY = 0) : x(aX), y(aY) {} }; -/* @} */ // end of group - }; // namespace Math diff --git a/src/math/intsize.h b/src/math/intsize.h deleted file mode 100644 index f4b2431..0000000 --- a/src/math/intsize.h +++ /dev/null @@ -1,61 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. - -/** @defgroup MathIntSizeModule math/intsize.h - Contains the IntSize struct. - */ - -#pragma once - -// Math module namespace -namespace Math -{ - -/* @{ */ // start of group - -/** \struct IntSize math/size.h - \brief 2D size with integer dimensions */ -struct IntSize -{ - //! Width - int w; - //! Height - int h; - - //! Constructs a zero size: (0,0) - inline IntSize() - { - LoadZero(); - } - - //! Constructs a size from given dimensions: (w,h) - inline explicit IntSize(int w, int h) - { - this->w = w; - this->h = h; - } - - //! Sets the zero size: (0,0) - inline void LoadZero() - { - w = h = 0; - } -}; // struct Size - - -/* @} */ // end of group - -}; // namespace Math diff --git a/src/math/matrix.h b/src/math/matrix.h index 45a7d75..30e629a 100644 --- a/src/math/matrix.h +++ b/src/math/matrix.h @@ -14,8 +14,9 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -/** @defgroup MathMatrixModule math/matrix.h - Contains the Matrix struct and related functions. +/** + * \file math/matrix.h + * \brief Matrix struct and related functions */ #pragma once @@ -32,8 +33,6 @@ namespace Math { -/* @{ */ // start of group - /** \struct Matrix math/matrix.h \brief 4x4 matrix @@ -42,11 +41,12 @@ namespace Math The internal representation is a 16-value table in column-major order, thus: - \verbatim +\verbatim m[0 ] m[4 ] m[8 ] m[12] m[1 ] m[5 ] m[9 ] m[13] m[2 ] m[6 ] m[10] m[14] -m[3 ] m[7 ] m[11] m[15] \endverbatim +m[3 ] m[7 ] m[11] m[15] +\endverbatim This representation is native to OpenGL; DirectX requires transposing the matrix. @@ -405,11 +405,15 @@ inline Math::Matrix MultiplyMatrices(const Math::Matrix &left, const Math::Matri } //! Calculates the result of multiplying m * v -/** The multiplication is performed thus: -\verbatim [ m.m[0 ] m.m[4 ] m.m[8 ] m.m[12] ] [ v.x ] +/** + The multiplication is performed thus: + +\verbatim +[ m.m[0 ] m.m[4 ] m.m[8 ] m.m[12] ] [ v.x ] [ m.m[1 ] m.m[5 ] m.m[9 ] m.m[13] ] [ v.y ] [ m.m[2 ] m.m[6 ] m.m[10] m.m[14] ] * [ v.z ] -[ m.m[3 ] m.m[7 ] m.m[11] m.m[15] ] [ 1 ] \endverbatim +[ m.m[3 ] m.m[7 ] m.m[11] m.m[15] ] [ 1 ] +\endverbatim The result, a 4x1 vector is then converted to 3x1 by dividing x,y,z coords by the fourth coord (w). */ @@ -434,6 +438,4 @@ inline Math::Vector MatrixVectorMultiply(const Math::Matrix &m, const Math::Vect return Math::Vector(x, y, z); } -/* @} */ // end of group - }; // namespace Math diff --git a/src/math/point.h b/src/math/point.h index ea20db9..ecf896f 100644 --- a/src/math/point.h +++ b/src/math/point.h @@ -14,8 +14,9 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -/** @defgroup MathPointModule math/point.h - Contains the Point struct and related functions. +/** + * \file math/point.h + * \brief Point struct and related functions */ #pragma once @@ -31,8 +32,6 @@ namespace Math { -/* @{ */ // start of group - /** \struct Point math/point.h \brief 2D point @@ -188,6 +187,4 @@ inline float Distance(const Point &a, const Point &b) return sqrtf((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)); } -/* @} */ // end of group - }; // namespace Math diff --git a/src/math/size.h b/src/math/size.h deleted file mode 100644 index 781b9a4..0000000 --- a/src/math/size.h +++ /dev/null @@ -1,66 +0,0 @@ -// * This file is part of the COLOBOT source code -// * Copyright (C) 2012, Polish Portal of Colobot (PPC) -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see http://www.gnu.org/licenses/. - -/** @defgroup MathSizeModule math/size.h - Contains the Size struct. - */ - -#pragma once - -// Math module namespace -namespace Math -{ - -/* @{ */ // start of group - -/** \struct Size math/size.h - \brief 2D size - - Represents a 2D size (w, h). - Is separate from Math::Point to avoid confusion. - - */ -struct Size -{ - //! Width - float w; - //! Height - float h; - - //! Constructs a zero size: (0,0) - inline Size() - { - LoadZero(); - } - - //! Constructs a size from given dimensions: (w,h) - inline explicit Size(float w, float h) - { - this->w = w; - this->h = h; - } - - //! Sets the zero size: (0,0) - inline void LoadZero() - { - w = h = 0.0f; - } -}; // struct Size - - -/* @} */ // end of group - -}; // namespace Math diff --git a/src/math/vector.h b/src/math/vector.h index 147869f..4378e75 100644 --- a/src/math/vector.h +++ b/src/math/vector.h @@ -14,8 +14,9 @@ // * You should have received a copy of the GNU General Public License // * along with this program. If not, see http://www.gnu.org/licenses/. -/** @defgroup MathVectorModule math/vector.h - Contains the Vector struct and related functions. +/** + * \file math/vector.h + * \brief Vector struct and related functions */ #pragma once @@ -31,8 +32,6 @@ namespace Math { -/* @{ */ // start of group - /** \struct Vector math/vector.h \brief 3D (3x1) vector @@ -263,6 +262,4 @@ inline float Distance(const Math::Vector &a, const Math::Vector &b) (a.z-b.z)*(a.z-b.z) ); } -/* @} */ // end of group - }; // namespace Math |