Math.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCOREMATH_H
  28. #define ROCKETCOREMATH_H
  29. #include "Header.h"
  30. namespace Rocket {
  31. namespace Core {
  32. namespace Math {
  33. // The constant PI.
  34. extern ROCKETCORE_API const float ROCKET_PI;
  35. template < typename Type >
  36. Type Max(Type a, Type b)
  37. {
  38. return (a > b) ? a : b;
  39. }
  40. template< typename Type >
  41. Type Min(Type a, Type b)
  42. {
  43. return (a < b) ? a : b;
  44. }
  45. template < typename Type >
  46. Type ClampLower(Type value, Type min)
  47. {
  48. return (value < min) ? min : value;
  49. }
  50. template < typename Type >
  51. Type ClampUpper(Type value, Type max)
  52. {
  53. return (value > max) ? max: value;
  54. }
  55. template< typename Type >
  56. Type Clamp(Type value, Type min, Type max)
  57. {
  58. return (value < min) ? min : (value > max) ? max : value;
  59. }
  60. /// Evaluates if a number is, or close to, zero.
  61. /// @param[in] value The number to compare to zero.
  62. /// @return True if the number if zero or close to it, false otherwise.
  63. ROCKETCORE_API bool IsZero(float value);
  64. /// Evaluates if two floating-point numbers are equal, or so similar that they could be considered
  65. /// so.
  66. /// @param[in] value_0 The first number to compare.
  67. /// @param[in] value_1 The second number to compare.
  68. /// @return True if the numbers are similar or equal.
  69. ROCKETCORE_API bool AreEqual(float value_0, float value_1);
  70. /// Calculates the absolute value of a number.
  71. /// @param[in] value The number of get the absolute value of.
  72. /// @return The absolute value of the number.
  73. ROCKETCORE_API float AbsoluteValue(float value);
  74. /// Calculates the cosine of an angle.
  75. /// @param[in] angle The angle to calculate the cosine of, in radians.
  76. /// @return The cosine of the angle.
  77. ROCKETCORE_API float Cos(float angle);
  78. /// Calculates the arc-cosine of an value.
  79. /// @param[in] angle The value to calculate the arc-cosine of.
  80. /// @return The angle, in radians.
  81. ROCKETCORE_API float ACos(float value);
  82. /// Calculates the sine of an angle.
  83. /// @param[in] angle The angle to calculate the sine of, in radians.
  84. /// @return The sine of the angle.
  85. ROCKETCORE_API float Sin(float angle);
  86. /// Calculates the arc-sine of an value.
  87. /// @param[in] angle The value to calculate the arc-sine of.
  88. /// @return The angle, in radians.
  89. ROCKETCORE_API float ASin(float angle);
  90. /// Calculates the tangent of an angle.
  91. /// @param[in] angle The angle to calculate the tangent of, in radians.
  92. /// @return The tanget of the angle.
  93. ROCKETCORE_API float Tan(float angle);
  94. /// Calculates the angle of a two-dimensional line.
  95. /// @param[in] y The y-component of the line.
  96. /// @param[in] x The x-component of the line.
  97. /// @return The angle of the line in radians.
  98. ROCKETCORE_API float ATan2(float y, float x);
  99. /// Converts an angle from radians to degrees.
  100. /// @param[in] The angle, in radians.
  101. /// @return The angle converted to degrees.
  102. ROCKETCORE_API float RadiansToDegrees(float angle);
  103. /// Converts an angle from degrees to radians.
  104. /// @param[in] The angle, in degrees.
  105. /// @return The angle converted to radians.
  106. ROCKETCORE_API float DegreesToRadians(float angle);
  107. /// Normalises and angle in radians
  108. /// @param[in] The angle, in randians
  109. /// @return The normalised angle
  110. ROCKETCORE_API float NormaliseAngle(float angle);
  111. /// Calculates the square root of a value.
  112. /// @param[in] value The value to calculate the square root of. This must be above zero.
  113. /// @return The square root of the value.
  114. ROCKETCORE_API float SquareRoot(float value);
  115. /// Rounds a floating-point value to the nearest integer.
  116. /// @param[in] value The value to round.
  117. /// @return The rounded integer.
  118. ROCKETCORE_API int Round(float value);
  119. /// Rounds a floating-point value up to the nearest integer.
  120. /// @param[in] value The value to round.
  121. /// @return The rounded integer.
  122. ROCKETCORE_API int RoundUp(float value);
  123. /// Rounds a floating-point value down to the nearest integer.
  124. /// @param[in] value The value to round.
  125. /// @return The rounded integer.
  126. ROCKETCORE_API int RoundDown(float value);
  127. /// Efficiently truncates a floating-point value into an integer.
  128. /// @param[in] value The value to truncate.
  129. /// @return The truncated value as a signed integer.
  130. ROCKETCORE_API int RealToInteger(float value);
  131. /// Converts a number to the nearest power of two, rounding up if necessary.
  132. /// @param[in] value The value to convert to a power-of-two.
  133. /// @return The smallest power of two that is as least as big as the input value.
  134. ROCKETCORE_API int ToPowerOfTwo(int value);
  135. /// Converts from the ASCII-representation of a hexadecimal digit to decimal.
  136. /// @param[in] hex_digit The hexadecimal digit to convert to decimal.
  137. /// @return The digit in decimal.
  138. ROCKETCORE_API int HexToDecimal(char hex_digit);
  139. /// Generates a random floating-point value between 0 and a user-specified value.
  140. /// @param[in] max_value The limit to random value. The generated value will be guaranteed to be below this limit.
  141. /// @return The random value.
  142. ROCKETCORE_API float RandomReal(float max_value);
  143. /// Generates a random integer value between 0 and a user-specified value.
  144. /// @param[in] max_value The limit to random value. The generated value will be guaranteed to be below this limit.
  145. /// @return The random value.
  146. ROCKETCORE_API int RandomInteger(int max_value);
  147. /// Generates a random boolean value, with equal chance of true or false.
  148. /// @return The random value.
  149. ROCKETCORE_API bool RandomBool();
  150. }
  151. }
  152. }
  153. #endif