Team Fortress 2 Source Code as on 22/4/2020
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.

71 lines
1.7 KiB

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