HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_BRJ.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_BRJ.h (UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_BRJ__
12 #define __UT_BRJ__
13 
14 #include "UT_API.h"
15 #include <SYS/SYS_Types.h>
16 #include "UT_Vector2.h"
17 #include "UT_Vector3.h"
18 
19 /// UT_BRJ - Binary Random Jittered Sampling
20 ///
21 /// Generate a sequence of well distributed random numbers
22 ///
23 /// The generator only supports 1-3 dimensional random numbers.
24 template <size_t DIM>
26 {
27 public:
28  template <typename T>
30  {
31  public:
33  using value_type = T;
34  static constexpr size_t tuple_size = DIM;
35 
36  T operator[](size_t i) const { return myValue[i]; }
37  T &operator[](size_t i) { return myValue[i]; }
38 
39  T *begin() { return myValue; }
40  T *end() { return myValue+DIM; }
41  const T *begin() const { return myValue; }
42  const T *end() const { return myValue+DIM; }
43 
44  private:
45  T myValue[DIM];
46  };
47 
50 
51  /// Default constructor doesn't initialize any data - you need to call
52  /// setSeed() before using.
53  UT_BRJ() = default;
54 
55  /// Initialize using a seed and an optional count
56  UT_BRJ(uint seed, uint count = 0)
57  {
58  setSeed(seed, count);
59  }
60 
61  /// Set the seed (and possibly the sequence number)
62  void setSeed(uint seed, uint count = 0);
63 
64  /// Reset the sequence of numbers (possibly with an offset)
65  SYS_FORCE_INLINE void reset(uint count=0) { myCount = count; }
66 
67  /// Get the float value and advance the state
68  SYS_FORCE_INLINE fvec fx() { return convertFloat(ux()); }
69  /// Convenience operator to get the float value and advance the state
70  SYS_FORCE_INLINE fvec operator()() { return convertFloat(ux()); }
71 
72  /// Stateless computation of the random number
73  static uvec ux(uint seed, uint index);
74 
75 private:
76  /// Return the current integer value, automatically advancing the state
77  /// These values are only meaningful when representing floats
78  uvec ux();
79 
80  static SYS_FORCE_INLINE fvec convertFloat(const uvec &uv)
81  {
82  fvec fv;
84  for (size_t dim = 0; dim < DIM; ++dim)
85  {
86  u.uval = uv[dim];
87  fv[dim] = u.fval - 1;
88  }
89  return fv;
90  }
91 
92  uvec myS0;
93  uint mySeed;
94  uint mySalt;
95  uint myCount;
96 };
97 
98 SYS_FORCE_INLINE float
99 UTbrjF(uint seed, int index)
100 {
102  u.uval = UT_BRJ<1>::ux(seed, index)[0];
103  return u.fval - 1;
104 }
105 
107 UTbrjV2(uint seed, int index)
108 {
109  auto uv = UT_BRJ<2>::ux(seed, index);
110  SYS_FPRealUnionF u[2];
111  u[0].uval = uv[0];
112  u[1].uval = uv[1];
113  return UT_Vector2F(u[0].fval-1, u[1].fval-1);
114 }
115 
117 UTbrjV3(uint seed, int index)
118 {
119  auto uv = UT_BRJ<3>::ux(seed, index);
120  SYS_FPRealUnionF u[3];
121  u[0].uval = uv[0];
122  u[1].uval = uv[1];
123  u[2].uval = uv[2];
124  return UT_Vector3F(u[0].fval-1, u[1].fval-1, u[2].fval-1);
125 }
126 
127 #endif
T operator[](size_t i) const
Definition: UT_BRJ.h:36
GA_API const UT_StringHolder uv
GLuint index
Definition: glew.h:1814
SYS_FORCE_INLINE float UTbrjF(uint seed, int index)
Definition: UT_BRJ.h:99
#define UT_API
Definition: UT_API.h:13
const T * begin() const
Definition: UT_BRJ.h:41
SYS_FORCE_INLINE void reset(uint count=0)
Reset the sequence of numbers (possibly with an offset)
Definition: UT_BRJ.h:65
SYS_FORCE_INLINE fvec fx()
Get the float value and advance the state.
Definition: UT_BRJ.h:68
UT_BRJ(uint seed, uint count=0)
Initialize using a seed and an optional count.
Definition: UT_BRJ.h:56
SYS_FORCE_INLINE UT_Vector3F UTbrjV3(uint seed, int index)
Definition: UT_BRJ.h:117
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
const T * end() const
Definition: UT_BRJ.h:42
Definition: UT_BRJ.h:25
UT_Vector3T< fpreal32 > UT_Vector3F
T & operator[](size_t i)
Definition: UT_BRJ.h:37
UT_Vector2T< fpreal32 > UT_Vector2F
SYS_FORCE_INLINE fvec operator()()
Convenience operator to get the float value and advance the state.
Definition: UT_BRJ.h:70
GLuint GLuint GLsizei count
Definition: glew.h:1253
SYS_FORCE_INLINE UT_Vector2F UTbrjV2(uint seed, int index)
Definition: UT_BRJ.h:107
unsigned int uint
Definition: SYS_Types.h:45