Welcome to mirror list, hosted at ThFree Co, Russian Federation.

random.h « random « carve « include « carve « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 634063cb90c6b0bf9e0ff6ae4bb4bcfae10fd4ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cassert>
#include <cmath>
#include <vector>

namespace boost {
#if __cplusplus > 199711L
#  include <random>
typedef std::mt19937 mt19937;
#else
#  include <stdlib.h>
struct mt19937 {
  int operator()() {
    return rand();
  }

  int max() {
    return RAND_MAX;
  }
};
#endif

template<typename T>
struct uniform_on_sphere {
  typedef std::vector<T> result_type;

  uniform_on_sphere(int dimension) {
    assert(dimension == 3);
  }

  std::vector<T>
  operator()(float u1, float u2) {
    T z = 1.0 - 2.0*u1;
    T r = std::sqrt(std::max(0.0, 1.0 - z*z));
    T phi = 2.0*M_PI*u2;
    T x = r*std::cos(phi);
    T y = r*std::sin(phi);
    std::vector<T> result;
    result.push_back(x);
    result.push_back(y);
    result.push_back(z);
    return result;
  }
};

template<typename RNG, typename DISTR>
struct variate_generator {

  variate_generator(RNG rng, DISTR distr)
    : rng_(rng), distr_(distr) {}

  typename DISTR::result_type
  operator()() {
    float rng_max_inv = 1.0 / rng_.max();
    return distr_(rng_() * rng_max_inv, rng_() * rng_max_inv);
  }

  RNG rng_;
  DISTR distr_;
};

}