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

BLI_rand.hh « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 069a81bb84b24871b27d7ae97b30521f87679ef6 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup bli
 */

#pragma once

#include "BLI_math.h"
#include "BLI_math_vec_types.hh"
#include "BLI_span.hh"
#include "BLI_utildefines.h"

namespace blender {

class RandomNumberGenerator {
 private:
  uint64_t x_;

 public:
  RandomNumberGenerator(uint32_t seed = 0)
  {
    this->seed(seed);
  }

  /**
   * Set the seed for future random numbers.
   */
  void seed(uint32_t seed)
  {
    constexpr uint64_t lowseed = 0x330E;
    x_ = (static_cast<uint64_t>(seed) << 16) | lowseed;
  }

  /**
   * Set a randomized hash of the value as seed.
   */
  void seed_random(uint32_t seed);

  uint32_t get_uint32()
  {
    this->step();
    return static_cast<uint32_t>(x_ >> 17);
  }

  int32_t get_int32()
  {
    this->step();
    return static_cast<int32_t>(x_ >> 17);
  }

  /**
   * \return Random value (0..N), but never N.
   */
  int32_t get_int32(int32_t max_exclusive)
  {
    BLI_assert(max_exclusive > 0);
    return this->get_int32() % max_exclusive;
  }

  /**
   * \return Random value (0..1), but never 1.0.
   */
  double get_double()
  {
    return (double)this->get_int32() / 0x80000000;
  }

  /**
   * \return Random value (0..1), but never 1.0.
   */
  float get_float()
  {
    return (float)this->get_int32() / 0x80000000;
  }

  template<typename T> void shuffle(MutableSpan<T> values)
  {
    /* Cannot shuffle arrays of this size yet. */
    BLI_assert(values.size() <= INT32_MAX);

    for (int i = values.size() - 1; i >= 2; i--) {
      int j = this->get_int32(i);
      if (i != j) {
        std::swap(values[i], values[j]);
      }
    }
  }

  /**
   * Compute uniformly distributed barycentric coordinates.
   */
  float3 get_barycentric_coordinates()
  {
    float rand1 = this->get_float();
    float rand2 = this->get_float();

    if (rand1 + rand2 > 1.0f) {
      rand1 = 1.0f - rand1;
      rand2 = 1.0f - rand2;
    }

    return float3(rand1, rand2, 1.0f - rand1 - rand2);
  }

  float2 get_unit_float2();
  float3 get_unit_float3();
  /**
   * Generate a random point inside the given triangle.
   */
  float2 get_triangle_sample(float2 v1, float2 v2, float2 v3);
  float3 get_triangle_sample_3d(float3 v1, float3 v2, float3 v3);
  void get_bytes(MutableSpan<char> r_bytes);

  /**
   * Simulate getting \a n random values.
   */
  void skip(int64_t n)
  {
    while (n--) {
      this->step();
    }
  }

 private:
  void step()
  {
    constexpr uint64_t multiplier = 0x5DEECE66Dll;
    constexpr uint64_t addend = 0xB;
    constexpr uint64_t mask = 0x0000FFFFFFFFFFFFll;

    x_ = (multiplier * x_ + addend) & mask;
  }
};

}  // namespace blender