Random.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /******************************************************************************
  2. Use 'Random' to generate random values.
  3. /******************************************************************************/
  4. struct Randomizer // Randomizer is multi-threaded safe
  5. {
  6. UID seed; // current seed value, this may be freely modified, it affects generated random values
  7. void randomize(); // randomize seed
  8. Randomizer& back (); // perform one step back, which will result in the next random value being the same as the last one
  9. UInt operator()( ); // return random UInt in range 0..0xFFFFFFFF
  10. UInt operator()(UInt n ); // return random UInt in range 0..n-1
  11. Int operator()(Int min, Int max); // return random Int in range min..max
  12. Bool b ( ); // return random Bool (false/true)
  13. ULong l ( ); // return random ULong in range 0..0xFFFFFFFFFFFFFFFF
  14. Flt f ( ); // return random Flt in range 0..1
  15. Flt f (Flt x ); // return random Flt in range 0..x
  16. Flt f (Flt min, Flt max); // return random Flt in range min..max
  17. Vec2 vec2 ( ); // return random Vec2 in range (0,0)..(1,1)
  18. Vec2 vec2 (Flt x ); // return random Vec2 in range (0,0)..(x,x)
  19. Vec2 vec2 (Flt min, Flt max); // return random Vec2 in range (min,min)..(max,max)
  20. Vec vec ( ); // return random Vec in range (0,0,0)..(1,1,1)
  21. Vec vec (Flt x ); // return random Vec in range (0,0,0)..(x,x,x)
  22. Vec vec (Flt min, Flt max); // return random Vec in range (min,min,min)..(max,max,max)
  23. Vec dir(C Vec &dir, Flt a ); // return random direction based on 'dir', and 0..a angle difference, 'dir' must be normalized
  24. Vec dir(C Vec &dir, Flt min, Flt max); // return random direction based on 'dir', and min..max angle difference, 'dir' must be normalized
  25. Vec2 circle1( Bool inside=true); // return random point on/inside circle with radius=1 and Vec2(0,0) position
  26. Vec2 circle ( Flt radius, Bool inside=true); // return random point on/inside circle with 'radius' and Vec2(0,0) position
  27. Vec sphere (C Ball &ball, Flt min, Flt max); // return random point on sphere surface, with min..max vertical angle limit, (min=-1..max, max=min..1)
  28. Vec operator()(C Edge &edge ); // return random point on edge
  29. Vec2 operator()(C Tri2 &tri , Bool inside=true); // return random point on/inside triangle
  30. Vec operator()(C Tri &tri , Bool inside=true); // return random point on/inside triangle
  31. Vec2 operator()(C Quad2 &quad , Bool inside=true); // return random point on/inside quad
  32. Vec operator()(C Quad &quad , Bool inside=true); // return random point on/inside quad
  33. Vec2 operator()(C Rect &rect , Bool inside=true); // return random point on/inside rectangle
  34. Vec operator()(C Box &box , Bool inside=true); // return random point on/inside box
  35. Vec operator()(C OBox &obox , Bool inside=true); // return random point on/inside oriented box
  36. Vec operator()(C Extent &ext , Bool inside=true); // return random point on/inside extent
  37. Vec2 operator()(C Circle &circle , Bool inside=true); // return random point on/inside circle
  38. Vec operator()(C Ball &ball , Bool inside=true); // return random point on/inside ball
  39. Vec operator()(C Capsule &capsule, Bool inside=true); // return random point on/inside capsule
  40. Vec operator()(C Tube &tube , Bool inside=true); // return random point on/inside tube
  41. Vec operator()(C Torus &torus , Bool inside=true); // return random point on/inside torus
  42. Vec operator()(C Cone &cone , Bool inside=true); // return random point on/inside cone
  43. Vec operator()(C Pyramid &pyramid, Bool inside=true); // return random point on/inside pyramid
  44. Vec operator()(C Shape &shape , Bool inside=true); // return random point on/inside shape
  45. Vec operator()(C MeshBase &mshb, C AnimatedSkeleton *anim_skel=null); // return random point on MeshBase surface (if 'anim_skel' is not null then mesh is assumed to be animated by the Skeleton)
  46. Vec operator()(C MeshRender &mshr, C AnimatedSkeleton *anim_skel=null); // return random point on MeshRender surface (if 'anim_skel' is not null then mesh is assumed to be animated by the Skeleton)
  47. Vec operator()(C MeshPart &part, C AnimatedSkeleton *anim_skel=null); // return random point on MeshPart surface (if 'anim_skel' is not null then mesh is assumed to be animated by the Skeleton)
  48. Vec operator()(C Mesh &mesh, C AnimatedSkeleton *anim_skel=null); // return random point on Mesh surface (if 'anim_skel' is not null then mesh is assumed to be animated by the Skeleton)
  49. Str password (Int length, Bool chars, Bool digits, Bool symbols=false); // generate a random password, 'length'=length of the password, 'chars'=if use characters in the password, 'digits'=if use digits in the password, 'symbols'=if use symbols in the password
  50. Str licenseKey(); // generate a random license key in "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" format
  51. // custom probability alignment
  52. UInt align (UInt n , Flt (&func)(Flt x)); // return random UInt in range 0..n-1 with probability aligned based on 'func' function
  53. Int align (Int min, Int max, Flt (&func)(Flt x)); // return random Int in range min..max with probability aligned based on 'func' function
  54. Flt alignF( Flt (&func)(Flt x)); // return random Flt in range 0..1 with probability aligned based on 'func' function
  55. Flt alignF(Flt x , Flt (&func)(Flt x)); // return random Flt in range 0..x with probability aligned based on 'func' function
  56. Flt alignF(Flt min, Flt max, Flt (&func)(Flt x)); // return random Flt in range min..max with probability aligned based on 'func' function
  57. UInt align (UInt n , Flt (&func)(Flt x, Flt y), Flt y); // return random UInt in range 0..n-1 with probability aligned based on 'func' function
  58. Int align (Int min, Int max, Flt (&func)(Flt x, Flt y), Flt y); // return random Int in range min..max with probability aligned based on 'func' function
  59. Flt alignF( Flt (&func)(Flt x, Flt y), Flt y); // return random Flt in range 0..1 with probability aligned based on 'func' function
  60. Flt alignF(Flt x , Flt (&func)(Flt x, Flt y), Flt y); // return random Flt in range 0..x with probability aligned based on 'func' function
  61. Flt alignF(Flt min, Flt max, Flt (&func)(Flt x, Flt y), Flt y); // return random Flt in range min..max with probability aligned based on 'func' function
  62. UInt alignPow (UInt n , Flt pow); // return random UInt in range 0..n-1 with probability aligned to "0" (for pow>1 towards Inf) or to "n-1" (for pow<1 towards 0)
  63. Int alignPow (Int min, Int max, Flt pow); // return random Int in range min..max with probability aligned to "min" (for pow>1 towards Inf) or to "max" (for pow<1 towards 0)
  64. Flt alignPowF( Flt pow); // return random Flt in range 0..1 with probability aligned to "0" (for pow>1 towards Inf) or to "1" (for pow<1 towards 0)
  65. Flt alignPowF(Flt x , Flt pow); // return random Flt in range 0..x with probability aligned to "0" (for pow>1 towards Inf) or to "x" (for pow<1 towards 0)
  66. Flt alignPowF(Flt min, Flt max, Flt pow); // return random Flt in range min..max with probability aligned to "min" (for pow>1 towards Inf) or to "max" (for pow<1 towards 0)
  67. Flt alignTargetNormalF(Flt min, Flt max, Flt target, Flt sharpness); // return random Flt in range min..max with probability aligned to 'target' using normal distribution, 'sharpness'=determines sharpness of alignment (recommended value at least 0.7, for example 1.0, 1.5, or 2.0 are good choices)
  68. Flt normal ( ); // return random Flt with normal distribution in range -13.2163944 .. 13.2163944 (theoretical range is -Inf..Inf however due to floating point precision, the actual range is smaller)
  69. Flt normalSkew(Flt shape); // return random Flt with skew normal distribution
  70. // array element
  71. template<typename TYPE, Int elms> TYPE& elm( TYPE (&array)[elms]) {return array[T( elms )];}
  72. template<typename TYPE, Int elms> C TYPE& elm( C TYPE (&array)[elms]) {return array[T( elms )];}
  73. template<typename TYPE > TYPE& elm( Mems <TYPE > &mems ) {return mems [T(mems.elms())];}
  74. template<typename TYPE > C TYPE& elm(C Mems <TYPE > &mems ) {return mems [T(mems.elms())];}
  75. template<typename TYPE > TYPE& elm( Memc <TYPE > &memc ) {return memc [T(memc.elms())];}
  76. template<typename TYPE > C TYPE& elm(C Memc <TYPE > &memc ) {return memc [T(memc.elms())];}
  77. template<typename TYPE, Int size> TYPE& elm( Memt <TYPE, size> &memt ) {return memt [T(memt.elms())];}
  78. template<typename TYPE, Int size> C TYPE& elm(C Memt <TYPE, size> &memt ) {return memt [T(memt.elms())];}
  79. template<typename TYPE > TYPE& elm( Memb <TYPE > &memb ) {return memb [T(memb.elms())];}
  80. template<typename TYPE > C TYPE& elm(C Memb <TYPE > &memb ) {return memb [T(memb.elms())];}
  81. template<typename TYPE > TYPE& elm( Memx <TYPE > &memx ) {return memx [T(memx.elms())];}
  82. template<typename TYPE > C TYPE& elm(C Memx <TYPE > &memx ) {return memx [T(memx.elms())];}
  83. template<typename TYPE, Int size> TYPE& elm( MemPtr<TYPE, size> &memp ) {return memp [T(memp.elms())];}
  84. template<typename TYPE, Int size> C TYPE& elm(C MemPtr<TYPE, size> &memp ) {return memp [T(memp.elms())];}
  85. Randomizer() {randomize();}
  86. Randomizer(UInt s0, UInt s1, UInt s2, UInt s3);
  87. Randomizer(ULong s0, ULong s1 );
  88. Randomizer(C UID &seed );
  89. #if EE_PRIVATE
  90. void fix();
  91. #endif
  92. }extern
  93. Random; // Main Randomizer
  94. /******************************************************************************/
  95. const Flt NoiseOrganicGain=0.4f; // can be used as 'gain' parameter along with 'NoiseOrganic' transform to get organic noise
  96. // Noise Transform functions which can be passed to noise methods as 'Transform' parameter
  97. inline Flt NoiseMountain(Flt noise) {return Sqr(1-Abs (noise ))* 2+(-1 - 0.171f);}
  98. inline Flt NoiseOrganic (Flt noise) {return Abs (noise ) * 2+(-1 + 0.492f);}
  99. inline Flt NoiseElectric(Flt noise) {return Abs (noise ) *-2-(-1 + 0.492f);} // same as "-NoiseOrganic"
  100. inline Flt NoiseAbsSqr (Flt noise) {return Sqr (noise ) * 2+(-1 + 0.814f);}
  101. inline Flt NoiseCbrt (Flt noise) {return Cbrt(noise*0.5f+0.5f) * 2+(-1 - 0.570f);}
  102. inline Flt NoiseSqrt (Flt noise) {return Sqrt(noise*0.5f+0.5f) * 2+(-1 - 0.398f);}
  103. inline Flt NoiseSqr (Flt noise) {return Sqr (noise*0.5f+0.5f) * 2+(-1 + 0.451f);}
  104. inline Flt NoiseCube (Flt noise) {return Cube(noise*0.5f+0.5f) * 2+(-1 + 0.677f);}
  105. /******************************************************************************/
  106. struct PerlinNoise
  107. {
  108. Flt noise(Dbl x )C;
  109. Flt noise(Dbl x, Dbl y )C;
  110. Flt noise(Dbl x, Dbl y, Dbl z)C;
  111. Flt tiledNoise(Dbl x , Int tile)C;
  112. Flt tiledNoise(Dbl x, Dbl y , C VecI2 &tile)C;
  113. Flt tiledNoise(Dbl x, Dbl y, Dbl z, C VecI &tile)C;
  114. Flt noise1(Dbl x, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  115. Flt noise2(Dbl x, Dbl y, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  116. Flt noise3(Dbl x, Dbl y, Dbl z, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  117. Flt tiledNoise1(Dbl x, Int tile, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  118. Flt tiledNoise2(Dbl x, Dbl y, VecI2 tile, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  119. Flt tiledNoise3(Dbl x, Dbl y, Dbl z, VecI tile, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  120. Flt noise1Bloom(Dbl x, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  121. Flt noise2Bloom(Dbl x, Dbl y, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  122. Flt noise3Bloom(Dbl x, Dbl y, Dbl z, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  123. Flt tiledNoise1Bloom(Dbl x, Int tile, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  124. Flt tiledNoise2Bloom(Dbl x, Dbl y, VecI2 tile, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  125. Flt tiledNoise3Bloom(Dbl x, Dbl y, Dbl z, VecI tile, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  126. Flt mask2(Dbl x, Dbl y, Int octaves, Flt sharpness=0.5f)C;
  127. PerlinNoise(UInt seed=0);
  128. private:
  129. Byte p[512];
  130. };
  131. /******************************************************************************/
  132. struct SimplexNoise // Open Simplex Noise
  133. {
  134. Flt noise(Dbl x )C;
  135. Flt noise(Dbl x, Dbl y )C;
  136. Flt noise(Dbl x, Dbl y, Dbl z )C;
  137. Flt noise(Dbl x, Dbl y, Dbl z, Dbl w)C;
  138. Flt tiledNoise(Dbl x , Int tile)C;
  139. Flt tiledNoise(Dbl x, Dbl y , C VecI2 &tile)C;
  140. Flt tiledNoise(Dbl x, Dbl y, Dbl z , C VecI &tile)C;
  141. Flt tiledNoise(Dbl x, Dbl y, Dbl z, Dbl w, C VecI4 &tile)C;
  142. Flt noise1(Dbl x, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  143. Flt noise2(Dbl x, Dbl y, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  144. Flt noise3(Dbl x, Dbl y, Dbl z, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  145. Flt noise4(Dbl x, Dbl y, Dbl z, Dbl w, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  146. Flt tiledNoise1(Dbl x, Int tile, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  147. Flt tiledNoise2(Dbl x, Dbl y, VecI2 tile, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  148. Flt tiledNoise3(Dbl x, Dbl y, Dbl z, VecI tile, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  149. Flt tiledNoise4(Dbl x, Dbl y, Dbl z, Dbl w, VecI4 tile, Int octaves, Flt gain=0.5f, Flt Transform(Flt noise)=null)C;
  150. Flt noise1Bloom(Dbl x, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  151. Flt noise2Bloom(Dbl x, Dbl y, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  152. Flt noise3Bloom(Dbl x, Dbl y, Dbl z, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  153. Flt noise4Bloom(Dbl x, Dbl y, Dbl z, Dbl w, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  154. Flt tiledNoise1Bloom(Dbl x, Int tile, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  155. Flt tiledNoise2Bloom(Dbl x, Dbl y, VecI2 tile, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  156. Flt tiledNoise3Bloom(Dbl x, Dbl y, Dbl z, VecI tile, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  157. Flt tiledNoise4Bloom(Dbl x, Dbl y, Dbl z, Dbl w, VecI4 tile, Int octaves, Flt bloom=1.0f, Flt sharpness=0.7f)C;
  158. Flt mask2(Dbl x, Dbl y, Int octaves, Flt sharpness=0.5f)C;
  159. SimplexNoise(UInt seed=0);
  160. private:
  161. U16 p[256], permGradIndex3D[256];
  162. #if EE_PRIVATE
  163. Dbl extrapolate2(int xsb, int ysb, Dbl dx, Dbl dy)C;
  164. Dbl extrapolate3(int xsb, int ysb, int zsb, Dbl dx, Dbl dy, Dbl dz)C;
  165. Dbl extrapolate4(int xsb, int ysb, int zsb, int wsb, Dbl dx, Dbl dy, Dbl dz, Dbl dw)C;
  166. #endif
  167. };
  168. /******************************************************************************/
  169. Flt SkewNormalAvg (Flt shape); // calculate average value for Skew Normal distribution
  170. Flt SkewNormalShape(Flt avg ); // calculate shape value for Skew Normal distribution which has an average value of 'avg'
  171. /******************************************************************************/