Overte C++ Documentation
RandomAndNoise.h
1 //
2 // RandomAndNoise.h
3 //
4 // Created by Olivier Prat on 05/16/18.
5 // Copyright 2018 High Fidelity, Inc.
6 //
7 // Distributed under the Apache License, Version 2.0.
8 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9 //
10 #ifndef RANDOM_AND_NOISE_H
11 #define RANDOM_AND_NOISE_H
12 
13 #include <glm/vec2.hpp>
14 
15 namespace halton {
16  // Low discrepancy Halton sequence generator
17  template <int B>
18  float evaluate(int index) {
19  float f = 1.0f;
20  float r = 0.0f;
21  float invB = 1.0f / (float)B;
22  index++; // Indices start at 1, not 0
23 
24  while (index > 0) {
25  f = f * invB;
26  r = r + f * (float)(index % B);
27  index = index / B;
28 
29  }
30 
31  return r;
32  }
33 }
34 
35 inline float getRadicalInverseVdC(uint32_t bits) {
36  bits = (bits << 16u) | (bits >> 16u);
37  bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
38  bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
39  bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
40  bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
41  return float(bits) * 2.3283064365386963e-10f; // / 0x100000000\n"
42 }
43 
44 namespace hammersley {
45  // Low discrepancy Hammersley 2D sequence generator
46  inline glm::vec2 evaluate(int k, const int sequenceLength) {
47  return glm::vec2(float(k) / float(sequenceLength), getRadicalInverseVdC(k));
48  }
49 }
50 
51 
52 #endif