summaryrefslogtreecommitdiff
path: root/public/mathlib/halton.h
diff options
context:
space:
mode:
authorFluorescentCIAAfricanAmerican <[email protected]>2020-04-22 12:56:21 -0400
committerFluorescentCIAAfricanAmerican <[email protected]>2020-04-22 12:56:21 -0400
commit3bf9df6b2785fa6d951086978a3e66f49427166a (patch)
tree2c0f1f0c63c4832882bc93814ebd2c2b1c6224e5 /public/mathlib/halton.h
downloadarchived-source-engine-2018-hl2-src-master.tar.xz
archived-source-engine-2018-hl2-src-master.zip
Diffstat (limited to 'public/mathlib/halton.h')
-rw-r--r--public/mathlib/halton.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/public/mathlib/halton.h b/public/mathlib/halton.h
new file mode 100644
index 0000000..44df68f
--- /dev/null
+++ b/public/mathlib/halton.h
@@ -0,0 +1,71 @@
+//========= Copyright Valve Corporation, All rights reserved. ============//
+// $Id$
+
+// halton.h - classes, etc for generating numbers using the Halton pseudo-random sequence. See
+// http://halton-sequences.wikiverse.org/.
+//
+// what this function is useful for is any sort of sampling/integration problem where
+// you want to solve it by random sampling. Each call the NextValue() generates
+// a random number between 0 and 1, in an unclumped manner, so that the space can be more
+// or less evenly sampled with a minimum number of samples.
+//
+// It is NOT useful for generating random numbers dynamically, since the outputs aren't
+// particularly random.
+//
+// To generate multidimensional sample values (points in a plane, etc), use two
+// HaltonSequenceGenerator_t's, with different (primes) bases.
+
+#ifndef HALTON_H
+#define HALTON_H
+
+#include <tier0/platform.h>
+#include <mathlib/vector.h>
+
+class HaltonSequenceGenerator_t
+{
+ int seed;
+ int base;
+ float fbase; //< base as a float
+
+public:
+ HaltonSequenceGenerator_t(int base); //< base MUST be prime, >=2
+
+ float GetElement(int element);
+
+ inline float NextValue(void)
+ {
+ return GetElement(seed++);
+ }
+
+};
+
+
+class DirectionalSampler_t //< pseudo-random sphere sampling
+{
+ HaltonSequenceGenerator_t zdot;
+ HaltonSequenceGenerator_t vrot;
+public:
+ DirectionalSampler_t(void)
+ : zdot(2),vrot(3)
+ {
+ }
+
+ Vector NextValue(void)
+ {
+ float zvalue=zdot.NextValue();
+ zvalue=2*zvalue-1.0; // map from 0..1 to -1..1
+ float phi=acos(zvalue);
+ // now, generate a random rotation angle for x/y
+ float theta=2.0*M_PI*vrot.NextValue();
+ float sin_p=sin(phi);
+ return Vector(cos(theta)*sin_p,
+ sin(theta)*sin_p,
+ zvalue);
+
+ }
+};
+
+
+
+
+#endif // halton_h