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 #include "UT_UniquePtr.h"
19 
20 /// UT_BRJ - Binary Random Jittered Sampling
21 ///
22 /// Generate a sequence of well distributed random numbers
23 ///
24 /// The generator only supports 1-3 dimensional random numbers.
25 template <size_t DIM, typename T>
27 {
28 public:
30  using value_type = T;
31  static constexpr size_t tuple_size = DIM;
32 
33  inline T operator[](size_t i) const { return myValue[i]; }
34  inline T &operator[](size_t i) { return myValue[i]; }
35 
36  inline T *begin() { return myValue; }
37  inline T *end() { return myValue+DIM; }
38  inline const T *begin() const { return myValue; }
39  inline const T *end() const { return myValue+DIM; }
40 
41 private:
42  T myValue[DIM];
43 };
44 
45 template <size_t DIM>
47 {
48 public:
49  template <typename T> using noiseVector = ut_BRJNoiseVector<DIM, T>;
52 
53  /// Default constructor doesn't initialize any data - you need to call
54  /// setSeed() before using.
55  UT_BRJ() = default;
56 
57  /// Initialize using a seed and an optional count
58  UT_BRJ(uint seed, uint count = 0)
59  {
60  setSeed(seed, count);
61  }
62 
63  /// Set the seed (and possibly the sequence number)
64  void setSeed(uint seed, uint count = 0);
65 
66  /// Reset the sequence of numbers (possibly with an offset)
67  SYS_FORCE_INLINE void reset(uint count=0) { myCount = count; }
68 
69  /// Get the float value and advance the state
70  SYS_FORCE_INLINE fvec fx() { return convertFloat(ux()); }
71  /// Convenience operator to get the float value and advance the state
72  SYS_FORCE_INLINE fvec operator()() { return convertFloat(ux()); }
73 
74  /// Stateless computation of the random number
75  static uvec ux(uint seed, uint index);
76 
77 private:
78  /// Return the current integer value, automatically advancing the state
79  /// These values are only meaningful when representing floats
80  uvec ux();
81 
82  static SYS_FORCE_INLINE fvec convertFloat(const uvec &uv)
83  {
84  fvec fv;
86  for (size_t dim = 0; dim < DIM; ++dim)
87  {
88  u.uval = uv[dim];
89  fv[dim] = u.fval - 1;
90  }
91  return fv;
92  }
93 
94  uvec myS0;
95  uint mySeed;
96  uint mySalt;
97  uint myCount;
98 };
99 
100 SYS_FORCE_INLINE float
101 UTbrjF(uint seed, int index)
102 {
104  u.uval = UT_BRJ<1>::ux(seed, index)[0];
105  return u.fval - 1;
106 }
107 
109 UTbrjV2(uint seed, int index)
110 {
111  auto uv = UT_BRJ<2>::ux(seed, index);
112  SYS_FPRealUnionF u[2];
113  u[0].uval = uv[0];
114  u[1].uval = uv[1];
115  return UT_Vector2F(u[0].fval-1, u[1].fval-1);
116 }
117 
119 UTbrjV3(uint seed, int index)
120 {
121  auto uv = UT_BRJ<3>::ux(seed, index);
122  SYS_FPRealUnionF u[3];
123  u[0].uval = uv[0];
124  u[1].uval = uv[1];
125  u[2].uval = uv[2];
126  return UT_Vector3F(u[0].fval-1, u[1].fval-1, u[2].fval-1);
127 }
128 
129 #endif
GA_API const UT_StringHolder uv
const T * end() const
Definition: UT_BRJ.h:39
SYS_FORCE_INLINE float UTbrjF(uint seed, int index)
Definition: UT_BRJ.h:101
#define UT_API
Definition: UT_API.h:14
SYS_FORCE_INLINE void reset(uint count=0)
Reset the sequence of numbers (possibly with an offset)
Definition: UT_BRJ.h:67
SYS_FORCE_INLINE fvec fx()
Get the float value and advance the state.
Definition: UT_BRJ.h:70
UT_BRJ(uint seed, uint count=0)
Initialize using a seed and an optional count.
Definition: UT_BRJ.h:58
static constexpr size_t tuple_size
Definition: UT_BRJ.h:31
T operator[](size_t i) const
Definition: UT_BRJ.h:33
SYS_FORCE_INLINE UT_Vector3F UTbrjV3(uint seed, int index)
Definition: UT_BRJ.h:119
const T * begin() const
Definition: UT_BRJ.h:38
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
Definition: UT_BRJ.h:46
UT_Vector3T< fpreal32 > UT_Vector3F
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:72
GLuint index
Definition: glcorearb.h:786
SYS_FORCE_INLINE UT_Vector2F UTbrjV2(uint seed, int index)
Definition: UT_BRJ.h:109
T & operator[](size_t i)
Definition: UT_BRJ.h:34
unsigned int uint
Definition: SYS_Types.h:45
GLint GLsizei count
Definition: glcorearb.h:405