Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.6 KiB

  1. // $Id$
  2. // halton.h - classes, etc for generating numbers using the Halton pseudo-random sequence. See
  3. // http://halton-sequences.wikiverse.org/.
  4. //
  5. // what this function is useful for is any sort of sampling/integration problem where
  6. // you want to solve it by random sampling. Each call the NextValue() generates
  7. // a random number between 0 and 1, in an unclumped manner, so that the space can be more
  8. // or less evenly sampled with a minimum number of samples.
  9. //
  10. // It is NOT useful for generating random numbers dynamically, since the outputs aren't
  11. // particularly random.
  12. //
  13. // To generate multidimensional sample values (points in a plane, etc), use two
  14. // HaltonSequenceGenerator_t's, with different (primes) bases.
  15. #ifndef HALTON_H
  16. #define HALTON_H
  17. #include <tier0/platform.h>
  18. #include <mathlib/vector.h>
  19. class HaltonSequenceGenerator_t
  20. {
  21. int seed;
  22. int base;
  23. float fbase; //< base as a float
  24. public:
  25. HaltonSequenceGenerator_t(int base); //< base MUST be prime, >=2
  26. float GetElement(int element);
  27. inline float NextValue(void)
  28. {
  29. return GetElement(seed++);
  30. }
  31. };
  32. class DirectionalSampler_t //< pseudo-random sphere sampling
  33. {
  34. HaltonSequenceGenerator_t zdot;
  35. HaltonSequenceGenerator_t vrot;
  36. public:
  37. DirectionalSampler_t(void)
  38. : zdot(2),vrot(3)
  39. {
  40. }
  41. Vector NextValue(void)
  42. {
  43. float zvalue=zdot.NextValue();
  44. zvalue=2*zvalue-1.0; // map from 0..1 to -1..1
  45. float phi=acos(zvalue);
  46. // now, generate a random rotation angle for x/y
  47. float theta=2.0*M_PI*vrot.NextValue();
  48. float sin_p=sin(phi);
  49. return Vector(cos(theta)*sin_p,
  50. sin(theta)*sin_p,
  51. zvalue);
  52. }
  53. };
  54. #endif // halton_h