PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Math.h * 00003 * 00004 * Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLMATH_MATH_H__ 00024 #define __PLMATH_MATH_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include <PLCore/PLCore.h> 00032 #include "PLMath/PLMath.h" 00033 00034 00035 //[-------------------------------------------------------] 00036 //[ Namespace ] 00037 //[-------------------------------------------------------] 00038 namespace PLMath { 00039 00040 00041 //[-------------------------------------------------------] 00042 //[ Classes ] 00043 //[-------------------------------------------------------] 00044 /** 00045 * @brief 00046 * Static class with some useful math constants and functions 00047 */ 00048 class Math { 00049 00050 00051 //[-------------------------------------------------------] 00052 //[ Public static data ] 00053 //[-------------------------------------------------------] 00054 public: 00055 static PLMATH_API const double Pi; /**< 3.14159265358979323846 */ 00056 static PLMATH_API const double Pi2; /**< 6.28318530717958623200 (Pi*2) */ 00057 static PLMATH_API const double PiHalf; /**< 1.57079632679489655800 (Pi/2) */ 00058 static PLMATH_API const double DegToRad; /**< 0.01745329251994329576 (Pi/180) */ 00059 static PLMATH_API const double RadToDeg; /**< 57.2957795130823208768 (180/Pi) */ 00060 static PLMATH_API const double Epsilon; /**< 1.192092896e-07F */ 00061 static PLMATH_API const double Log2Const; /**< 0.693147180559945309417 */ 00062 static PLMATH_API const int RandMax; /**< Maximum random value (0x7fff) */ 00063 00064 00065 //[-------------------------------------------------------] 00066 //[ Public functions ] 00067 //[-------------------------------------------------------] 00068 public: 00069 //[-------------------------------------------------------] 00070 //[ Floating point ] 00071 //[-------------------------------------------------------] 00072 /** 00073 * @brief 00074 * Checks whether the given float-precision floating-point value is not a number (NAN) 00075 * 00076 * @return 00077 * 'true' if the value is not a number, else 'false' 00078 */ 00079 inline static bool IsNotANumber(float fValue); 00080 00081 /** 00082 * @brief 00083 * Checks whether the given double-precision floating-point value is not a number (NAN) 00084 * 00085 * @return 00086 * 'true' if the value is not a number, else 'false' 00087 */ 00088 inline static bool IsNotANumber(double dValue); 00089 00090 /** 00091 * @brief 00092 * Checks whether the given float-precision floating-point value is finite 00093 * 00094 * @return 00095 * 'true' if the value is finite, (INF < fValue < +INF) else 'false' (infinite or not a number) 00096 */ 00097 inline static bool IsFinite(float fValue); 00098 00099 /** 00100 * @brief 00101 * Checks whether the given double-precision floating-point value is finite 00102 * 00103 * @return 00104 * 'true' if the value is finite, (INF < dValue < +INF) else 'false' (infinite or not a number) 00105 */ 00106 inline static bool IsFinite(double dValue); 00107 00108 //[-------------------------------------------------------] 00109 //[ Nearest power of two ] 00110 //[-------------------------------------------------------] 00111 /** 00112 * @brief 00113 * Check's whether the given the number is a power of 2 00114 * 00115 * @param[in] nNumber 00116 * Number to check 00117 * 00118 * @return 00119 * 'true' if the number is a power of 2, else 'false' 00120 */ 00121 inline static bool IsPowerOfTwo(int nNumber); 00122 00123 /** 00124 * @brief 00125 * Returns the nearest power of 2 00126 * 00127 * @param[in] nNumber 00128 * Number to check 00129 * @param[in] bLower 00130 * 'true' take the lower nearest number, else 'false' takes the higher one 00131 * 00132 * @return 00133 * The nearest power of 2, if it couldn't be found 'nNumber' 00134 */ 00135 inline static int GetNearestPowerOfTwo(int nNumber, bool bLower = true); 00136 00137 //[-------------------------------------------------------] 00138 //[ Random ] 00139 //[-------------------------------------------------------] 00140 /** 00141 * @brief 00142 * Returns a positive random number 00143 * 00144 * @return 00145 * A positive random number 00146 */ 00147 inline static PLCore::uint32 GetRand(); 00148 00149 /** 00150 * @brief 00151 * Returns a positive or negative random number 00152 * 00153 * @return 00154 * A positive or negative random number 00155 */ 00156 inline static int GetRandNeg(); 00157 00158 /** 00159 * @brief 00160 * Returns a positive float random number between 0..1 00161 * 00162 * @return 00163 * A positive float random number between 0..1 00164 */ 00165 inline static float GetRandFloat(); 00166 00167 /** 00168 * @brief 00169 * Returns a positive or negative float random number between -1..1 00170 * 00171 * @return 00172 * A positive or negative float random number between -1..1 00173 */ 00174 inline static float GetRandNegFloat(); 00175 00176 /** 00177 * @brief 00178 * Returns a float random number between the given minimum/maximum 00179 * 00180 * @param[in] fMin 00181 * Minimum (inclusive), must be <= maximum 00182 * @param[in] fMax 00183 * Maximum (inclusive), must be >= minimum 00184 * 00185 * @return 00186 * A float random number between the given minimum/maximum 00187 */ 00188 inline static float GetRandMinMaxFloat(float fMin, float fMax); 00189 00190 //[-------------------------------------------------------] 00191 //[ Misc ] 00192 //[-------------------------------------------------------] 00193 /** 00194 * @brief 00195 * Returns a whether to given numbers are equal or not 00196 * 00197 * @param[in] f1 00198 * First number 00199 * @param[in] f2 00200 * Second number 00201 * @param[in] fEpsilon 00202 * Error tolerance 00203 * 00204 * @return 00205 * 'true' if the given numbers are equal, else 'false' 00206 */ 00207 inline static bool AreEqual(float f1, float f2, float fEpsilon = Math::Epsilon); 00208 00209 /** 00210 * @brief 00211 * Returns a whether to given numbers are equal or not 00212 * 00213 * @param[in] d1 00214 * First number 00215 * @param[in] d2 00216 * Second number 00217 * @param[in] dEpsilon 00218 * Error tolerance 00219 * 00220 * @return 00221 * 'true' if the given numbers are equal, else 'false' 00222 */ 00223 inline static bool AreEqual(double d1, double d2, double dEpsilon = Math::Epsilon); 00224 00225 /** 00226 * @brief 00227 * Returns the minimum of the 2 given values 00228 * 00229 * @param[in] a 00230 * First value 00231 * @param[in] b 00232 * Second value 00233 * 00234 * @return 00235 * The minimum of the 2 given values 00236 */ 00237 inline static float Min(float a, float b); 00238 inline static int Min(int a, int b); 00239 inline static PLCore::uint32 Min(PLCore::uint32 a, PLCore::uint32 b); 00240 00241 /** 00242 * @brief 00243 * Returns the maximum of the 2 given values 00244 * 00245 * @param[in] a 00246 * First value 00247 * @param[in] b 00248 * Second value 00249 * 00250 * @return 00251 * The maximum of the 2 given values 00252 */ 00253 inline static float Max(float a, float b); 00254 inline static int Max(int a, int b); 00255 inline static PLCore::uint32 Max(PLCore::uint32 a, PLCore::uint32 b); 00256 00257 /** 00258 * @brief 00259 * Returns the minimum of the 4 given values 00260 * 00261 * @param[in] a 00262 * First value 00263 * @param[in] b 00264 * Second value 00265 * @param[in] c 00266 * Third value 00267 * @param[in] d 00268 * Fourth value 00269 * 00270 * @return 00271 * The minimum of the 4 given values 00272 */ 00273 inline static float Min(float a, float b, float c, float d); 00274 inline static int Min(int a, int b, int c, int d); 00275 inline static PLCore::uint32 Min(PLCore::uint32 a, PLCore::uint32 b, PLCore::uint32 c, PLCore::uint32 d); 00276 00277 /** 00278 * @brief 00279 * Returns the maximum of the 4 given values 00280 * 00281 * @param[in] a 00282 * First value 00283 * @param[in] b 00284 * Second value 00285 * @param[in] c 00286 * Third value 00287 * @param[in] d 00288 * Fourth value 00289 * 00290 * @return 00291 * The maximum of the 4 given values 00292 */ 00293 inline static float Max(float a, float b, float c, float d); 00294 inline static int Max(int a, int b, int c, int d); 00295 inline static PLCore::uint32 Max(PLCore::uint32 a, PLCore::uint32 b, PLCore::uint32 c, PLCore::uint32 d); 00296 00297 /** 00298 * @brief 00299 * Returns the sign of the given value 00300 * 00301 * @param[in] x 00302 * Value to check 00303 * 00304 * @return 00305 * '-1' if the given value is negative, else '1' 00306 */ 00307 inline static float Sign(float x); 00308 00309 /** 00310 * @brief 00311 * Ensures that the given value is within the given interval [fMin, fMax] by clamping the value 00312 * 00313 * @param[in] fValue 00314 * Value to check 00315 * @param[in] fMin 00316 * Minimum of the interval, must be < fMax! 00317 * @param[in] fMax 00318 * Maximum of the interval, must be > fMin! 00319 * 00320 * @return 00321 * The value within the interval [fMin, fMax] 00322 */ 00323 inline static float ClampToInterval(float fValue, float fMin, float fMax); 00324 00325 /** 00326 * @brief 00327 * Ensures that the given value is within the given interval [fMin, fMax] by wrapping the value 00328 * 00329 * @param[in] fValue 00330 * Value to check 00331 * @param[in] fMin 00332 * Minimum of the interval, must be < fMax! 00333 * @param[in] fMax 00334 * Maximum of the interval, must be > fMin! 00335 * 00336 * @return 00337 * The value within the interval [fMin, fMax] 00338 */ 00339 inline static float WrapToInterval(float fValue, float fMin, float fMax); 00340 00341 /** 00342 * @brief 00343 * Returns the absolute value of the given value 00344 * 00345 * @param[in] x 00346 * Value to return the absolute value from 00347 * 00348 * @return 00349 * Absolute value of the given value 00350 */ 00351 inline static int Abs(int x); 00352 00353 /** 00354 * @brief 00355 * Returns the absolute value of the given value 00356 * 00357 * @param[in] x 00358 * Value to return the absolute value from 00359 * 00360 * @return 00361 * Absolute value of the given value 00362 */ 00363 inline static float Abs(float x); 00364 00365 /** 00366 * @brief 00367 * Returns the absolute value of the given value 00368 * 00369 * @param[in] x 00370 * Value to return the absolute value from 00371 * 00372 * @return 00373 * Absolute value of the given value 00374 */ 00375 inline static double Abs(double x); 00376 00377 /** 00378 * @brief 00379 * Swaps two given values 00380 * 00381 * @param[in, out] a 00382 * First value 00383 * @param[in, out] b 00384 * Second value 00385 */ 00386 inline static void Swap(float &a, float &b); 00387 00388 /** 00389 * @brief 00390 * Clamps the value between 0.0 and 1.0 00391 * 00392 * @param[in] fValue 00393 * Value to clamp 00394 * 00395 * @return 00396 * Clamped value 00397 */ 00398 inline static float Saturate(float fValue); 00399 00400 /** 00401 * @brief 00402 * Returns the floor of the given value 00403 * 00404 * @param[in] fValue 00405 * Value to return the floor from 00406 * 00407 * @return 00408 * The floor of the given value 00409 * 00410 * @remarks 00411 * This function returns a floating-point value representing the largest integer value that 00412 * is less than or equal to the given value. 00413 */ 00414 inline static float Floor(float fValue); 00415 00416 /** 00417 * @brief 00418 * Returns the ceil of the given value 00419 * 00420 * @param[in] fValue 00421 * Value to return the ceil from 00422 * 00423 * @return 00424 * The ceil of the given value 00425 * 00426 * @remarks 00427 * This function returns a floating-point value representing the smallest integer value 00428 * that is greater than or equal to the given value. 00429 */ 00430 inline static float Ceil(float fValue); 00431 00432 /** 00433 * @brief 00434 * Returns the rounded value of the given value 00435 * 00436 * @param[in] fValue 00437 * Value to return the rounded value from 00438 * @param[in] nPrecision 00439 * Number of digits after the comma 00440 * 00441 * @return 00442 * The rounded value of the given value 00443 */ 00444 inline static float Round(float fValue, PLCore::uint32 nPrecision = 0); 00445 00446 /** 00447 * @brief 00448 * Returns the sine of a given angle 00449 * 00450 * @param[in] fAngle 00451 * Angle in radians 00452 * 00453 * @return 00454 * Sine of the given angle 00455 * 00456 * @note 00457 * - If the given angle is greater than or equal to 263, or less 00458 * than or equal to 263, a loss of significance in the result occurs 00459 * - Sine of 0 is 0 00460 */ 00461 inline static float Sin(float fAngle); 00462 00463 /** 00464 * @brief 00465 * Returns the sine of a given angle 00466 * 00467 * @param[in] dAngle 00468 * Angle in radians 00469 * 00470 * @return 00471 * Sine of the given angle 00472 * 00473 * @see 00474 * - Sin(float) above 00475 */ 00476 inline static double Sin(double dAngle); 00477 00478 /** 00479 * @brief 00480 * Returns the arcsine of a given value 00481 * 00482 * @param[in] fValue 00483 * Value between 1 and 1 whose arcsine is to be calculated 00484 * 00485 * @return 00486 * Arcsine of the given value 00487 * 00488 * @remarks 00489 * This function returns the arcsine of the given value in the range p/2 to p/2 radians. 00490 * If the given value is less than 1 or greater than 1, ASin returns an indefinite 00491 * (same as a quiet NaN). 00492 */ 00493 inline static float ASin(float fValue); 00494 00495 /** 00496 * @brief 00497 * Returns the arcsine of a given value 00498 * 00499 * @param[in] dValue 00500 * Value between 1 and 1 whose arcsine is to be calculated 00501 * 00502 * @return 00503 * Arcsine of the given value 00504 * 00505 * @see 00506 * - ASin(float) above 00507 */ 00508 inline static double ASin(double dValue); 00509 00510 /** 00511 * @brief 00512 * Returns the cosine (cos) of a given angle 00513 * 00514 * @param[in] fAngle 00515 * Angle in radians 00516 * 00517 * @return 00518 * Cosine of the given angle 00519 * 00520 * @note 00521 * - If the given angle is greater than or equal to 263, or less 00522 * than or equal to 263, a loss of significance in the result occurs 00523 * - Cosine of 0 is 1 00524 */ 00525 inline static float Cos(float fAngle); 00526 00527 /** 00528 * @brief 00529 * Returns the cosine (cos) of a given angle 00530 * 00531 * @param[in] dAngle 00532 * Angle in radians 00533 * 00534 * @return 00535 * Cosine of the given angle 00536 * 00537 * @see 00538 * - Cos(float) above 00539 */ 00540 inline static double Cos(double dAngle); 00541 00542 /** 00543 * @brief 00544 * Returns the arccosine of a given value 00545 * 00546 * @param[in] fValue 00547 * Value between 1 and 1 whose arccosine is to be calculated 00548 * 00549 * @return 00550 * Arccosine of the given value 00551 * 00552 * @remarks 00553 * This function returns the arccosine of the given value in the range p/2 to p/2 radians. 00554 * If the given value is less than 1 or greater than 1, ACos returns an indefinite 00555 * (same as a quiet NaN). 00556 */ 00557 inline static float ACos(float fValue); 00558 00559 /** 00560 * @brief 00561 * Returns the arccosine of a given value 00562 * 00563 * @param[in] dValue 00564 * Value between 1 and 1 whose arccosine is to be calculated 00565 * 00566 * @return 00567 * Arccosine of the given value 00568 * 00569 * @see 00570 * - ACos(float) above 00571 */ 00572 inline static double ACos(double dValue); 00573 00574 /** 00575 * @brief 00576 * Returns the tangent (tan) of a given angle 00577 * 00578 * @param[in] fAngle 00579 * Angle in radians 00580 * 00581 * @return 00582 * Tangent of the given angle 00583 * 00584 * @note 00585 * - If the given angle is greater than or equal to 263, or less 00586 * than or equal to 263, a loss of significance in the result occurs 00587 * - Tangent of 0 is 0 00588 */ 00589 inline static float Tan(float fAngle); 00590 00591 /** 00592 * @brief 00593 * Returns the tangent (tan) of a given angle 00594 * 00595 * @param[in] dAngle 00596 * Angle in radians 00597 * 00598 * @return 00599 * Tangent of the given angle 00600 * 00601 * @see 00602 * - Tan(float) above 00603 */ 00604 inline static double Tan(double dAngle); 00605 00606 /** 00607 * @brief 00608 * Returns the arctangent of a given value 00609 * 00610 * @param[in] fValue 00611 * Value between 1 and 1 whose arccosine is to be calculated 00612 * 00613 * @return 00614 * Arccosine of the given value 00615 */ 00616 inline static float ATan(float fValue); 00617 00618 /** 00619 * @brief 00620 * Returns the arctangent of a given value 00621 * 00622 * @param[in] dValue 00623 * Value between 1 and 1 whose arccosine is to be calculated 00624 * 00625 * @return 00626 * Arccosine of the given value 00627 */ 00628 inline static double ATan(double dValue); 00629 00630 /** 00631 * @brief 00632 * Returns the arctangent of of y/x 00633 * 00634 * @param[in] fX 00635 * Any number 00636 * @param[in] fY 00637 * Any number 00638 * 00639 * @return 00640 * Arccosine of y/x 00641 * 00642 * @remarks 00643 * Returns a value in the range p to p radians, using the signs of both parameters 00644 * to determine the quadrant of the return value 00645 * 00646 * @note 00647 * - If fX is 0, this function returns 0 00648 * - If both parameters are 0, the function returns 0 00649 */ 00650 inline static float ATan2(float fX, float fY); 00651 00652 /** 00653 * @brief 00654 * Returns the arctangent of of y/x 00655 * 00656 * @param[in] dX 00657 * Any number 00658 * @param[in] dY 00659 * Any number 00660 * 00661 * @return 00662 * Arccosine of y/x 00663 * 00664 * @see 00665 * - ATan2(float, float) above 00666 */ 00667 inline static double ATan2(double dX, double dY); 00668 00669 /** 00670 * @brief 00671 * Returns the square-root of a given value 00672 * 00673 * @param[in] fValue 00674 * Nonnegative floating-point value, if negative, this function returns an indefinite 00675 * (same as a quiet NaN) 00676 * 00677 * @return 00678 * The square-root of the given value, indefinite on error 00679 */ 00680 inline static float Sqrt(float fValue); 00681 00682 /** 00683 * @brief 00684 * Returns the square-root of a given value 00685 * 00686 * @param[in] dValue 00687 * Nonnegative floating-point value, if negative, this function returns an indefinite 00688 * (same as a quiet NaN) 00689 * 00690 * @return 00691 * The square-root of the given value, indefinite on error 00692 */ 00693 inline static double Sqrt(double dValue); 00694 00695 /** 00696 * @brief 00697 * Returns the inverse square-root of a given value 00698 * 00699 * @param[in] fValue 00700 * Nonnegative floating-point value, if negative, this function returns an indefinite 00701 * (same as a quiet NaN) 00702 * 00703 * @return 00704 * The inverse square-root of the given value, indefinite on error 00705 * 00706 * @remarks 00707 * This method is slightly faster then writing "1/Sqrt(<value>)", but may return invalid results. 00708 * So, be careful when using this method and only use it when you're sure that the results are 00709 * correct in your use case. 00710 */ 00711 inline static float FastInvSqrt(float fValue); 00712 00713 /** 00714 * @brief 00715 * Returns x raised to the power of y 00716 * 00717 * @param[in] x 00718 * Base 00719 * @param[in] y 00720 * Exponent 00721 * 00722 * @return 00723 * x raised to the power of y 00724 */ 00725 inline static float Pow(float x, float y); 00726 00727 /** 00728 * @brief 00729 * Returns x raised to the power of y 00730 * 00731 * @param[in] x 00732 * Base 00733 * @param[in] y 00734 * Exponent 00735 * 00736 * @return 00737 * x raised to the power of y 00738 */ 00739 inline static double Pow(double x, double y); 00740 00741 /** 00742 * @brief 00743 * Returns x raised to the power of y 00744 * 00745 * @param[in] x 00746 * Base 00747 * @param[in] y 00748 * Exponent 00749 * 00750 * @return 00751 * x raised to the power of y 00752 */ 00753 inline static double Pow(int x, int y); 00754 00755 /** 00756 * @brief 00757 * Returns the natural logarithm of a given number 00758 * 00759 * @param[in] x 00760 * Given number 00761 * 00762 * @return 00763 * The natural logarithm of the given number 00764 */ 00765 inline static float Log(float x); 00766 00767 /** 00768 * @brief 00769 * Returns the natural logarithm of a given number 00770 * 00771 * @param[in] x 00772 * Given number 00773 * 00774 * @return 00775 * The natural logarithm of the given number 00776 */ 00777 inline static double Log(double x); 00778 00779 /** 00780 * @brief 00781 * Returns the base 2 logarithm for a given number, as long as it is not negative, or zero 00782 * 00783 * @param[in] x 00784 * Given number 00785 * 00786 * @return 00787 * The base 2 logarithm for the given number, as long as it is not negative, or zero 00788 * 00789 * @remarks 00790 * Log(x)/Log(2) 00791 */ 00792 inline static float Log2(float x); 00793 00794 /** 00795 * @brief 00796 * Returns the base 2 logarithm for a given number, as long as it is not negative, or zero 00797 * 00798 * @param[in] x 00799 * Given number 00800 * 00801 * @return 00802 * The base 2 logarithm for the given number, as long as it is not negative, or zero 00803 * 00804 * @remarks 00805 * Log(x)/Log(2) 00806 */ 00807 inline static double Log2(double x); 00808 00809 /** 00810 * @brief 00811 * Returns the e number raised to the power x 00812 * 00813 * @param[in] x 00814 * Given number 00815 * 00816 * @return 00817 * The e number raised to the power x 00818 */ 00819 inline static float Exp(float x); 00820 00821 /** 00822 * @brief 00823 * Returns the e number raised to the power x 00824 * 00825 * @param[in] x 00826 * Given number 00827 * 00828 * @return 00829 * The e number raised to the power x 00830 */ 00831 inline static double Exp(double x); 00832 00833 00834 }; 00835 00836 00837 //[-------------------------------------------------------] 00838 //[ Namespace ] 00839 //[-------------------------------------------------------] 00840 } // PLMath 00841 00842 00843 //[-------------------------------------------------------] 00844 //[ Implementation ] 00845 //[-------------------------------------------------------] 00846 #include "PLMath/Math.inl" 00847 00848 00849 #endif // __PLMATH_MATH_H__
|