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