home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / basic_math.h < prev    next >
C/C++ Source or Header  |  2008-12-22  |  3KB  |  132 lines

  1. #ifndef K3DSDK_BASIC_MATH_H
  2. #define K3DSDK_BASIC_MATH_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2004, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This library is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This library is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this library; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. /** \file
  24.         \brief Basic math functions
  25.         \author Tim Shead (tshead@k-3d.com)
  26. */
  27.  
  28. #include <algorithm>
  29. #include <cmath>
  30.  
  31. namespace k3d
  32. {
  33.  
  34. /// Pi
  35. inline double pi()
  36. {
  37.     return 3.1415926535897932384626433832795;
  38. }
  39.  
  40. /// Pi divided-by two
  41. inline double pi_over_2()
  42. {
  43.     return pi() * 0.5;
  44. }
  45.  
  46. /// Pi times two
  47. inline double pi_times_2()
  48. {
  49.     return pi() * 2.0;
  50. }
  51.  
  52. /// Converts degrees to radians
  53. inline const double radians(const double degrees)
  54. {
  55.     return degrees * 0.01745329252;
  56. }
  57.  
  58. /// Converts radians to degrees
  59. inline const double degrees(const double radians)
  60. {
  61.     return radians * 57.2957795131;
  62. }
  63.  
  64. /// Computes N!
  65. inline double factorial(double N)
  66. {
  67.     double result = 1;
  68.     for(double i = 2; i <= N; ++i)
  69.         result *= i;
  70.  
  71.     return result;
  72. }
  73.  
  74. /// Computes an integer power of a base via successive squaring
  75. template <typename Type>
  76. Type fast_pow(Type base, int exponent)
  77. {
  78.     bool invert = exponent < 0;
  79.     if (invert)
  80.         exponent = -exponent;
  81.  
  82.     // loop invariant: prod * base^exponent
  83.     Type prod = Type(1);
  84.     while (exponent > 0) {
  85.         if (exponent % 2 == 0) {
  86.             base *= base;
  87.             exponent /= 2;
  88.         }
  89.         else {
  90.             prod *= base;
  91.             --exponent;
  92.         }
  93.     }
  94.  
  95.     if (invert)
  96.         prod = Type(1) / prod;
  97.  
  98.     return prod;
  99. }
  100.  
  101. /// Returns the sign of an argument: -1 if negative, 0 if zero, +1 if positive
  102. template<class type> double sign(const type& Arg)
  103. {
  104.     if(Arg > 0.0) return 1.0;
  105.     if(Arg < 0.0) return -1.0;
  106.  
  107.     return 0.0;
  108. }
  109.  
  110. /// Rounds a value to the closest integer
  111. inline double round(const double Value)
  112. {
  113.     return (Value - std::floor(Value)) < 0.5 ? std::floor(Value) : std::ceil(Value);
  114. }
  115.  
  116. /// Clamps a value within the specified range (conforms to SL usage)
  117. template<class type> type clamp(const type& x, const type& minval, const type& maxval)
  118. {
  119.     return std::min(std::max(x, minval), maxval);
  120. }
  121.  
  122. /// Returns the linear interpolation of two values
  123. template<class type> type mix(const type& x, const type& y, const double alpha)
  124. {
  125.     return x * (1 - alpha) + y * (alpha);
  126. }
  127.  
  128. } // namespace k3d
  129.  
  130. #endif // !K3DSDK_BASIC_MATH_H
  131.  
  132.