MathUtils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author WestLangley / http://github.com/WestLangley
  5. * @author thezwap
  6. */
  7. const _lut = [];
  8. for ( let i = 0; i < 256; i ++ ) {
  9. _lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 );
  10. }
  11. const MathUtils = {
  12. DEG2RAD: Math.PI / 180,
  13. RAD2DEG: 180 / Math.PI,
  14. generateUUID: function () {
  15. // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
  16. const d0 = Math.random() * 0xffffffff | 0;
  17. const d1 = Math.random() * 0xffffffff | 0;
  18. const d2 = Math.random() * 0xffffffff | 0;
  19. const d3 = Math.random() * 0xffffffff | 0;
  20. const uuid = _lut[ d0 & 0xff ] + _lut[ d0 >> 8 & 0xff ] + _lut[ d0 >> 16 & 0xff ] + _lut[ d0 >> 24 & 0xff ] + '-' +
  21. _lut[ d1 & 0xff ] + _lut[ d1 >> 8 & 0xff ] + '-' + _lut[ d1 >> 16 & 0x0f | 0x40 ] + _lut[ d1 >> 24 & 0xff ] + '-' +
  22. _lut[ d2 & 0x3f | 0x80 ] + _lut[ d2 >> 8 & 0xff ] + '-' + _lut[ d2 >> 16 & 0xff ] + _lut[ d2 >> 24 & 0xff ] +
  23. _lut[ d3 & 0xff ] + _lut[ d3 >> 8 & 0xff ] + _lut[ d3 >> 16 & 0xff ] + _lut[ d3 >> 24 & 0xff ];
  24. // .toUpperCase() here flattens concatenated strings to save heap memory space.
  25. return uuid.toUpperCase();
  26. },
  27. clamp: function ( value, min, max ) {
  28. return Math.max( min, Math.min( max, value ) );
  29. },
  30. // compute euclidian modulo of m % n
  31. // https://en.wikipedia.org/wiki/Modulo_operation
  32. euclideanModulo: function ( n, m ) {
  33. return ( ( n % m ) + m ) % m;
  34. },
  35. // Linear mapping from range <a1, a2> to range <b1, b2>
  36. mapLinear: function ( x, a1, a2, b1, b2 ) {
  37. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  38. },
  39. // https://en.wikipedia.org/wiki/Linear_interpolation
  40. lerp: function ( x, y, t ) {
  41. return ( 1 - t ) * x + t * y;
  42. },
  43. // http://en.wikipedia.org/wiki/Smoothstep
  44. smoothstep: function ( x, min, max ) {
  45. if ( x <= min ) return 0;
  46. if ( x >= max ) return 1;
  47. x = ( x - min ) / ( max - min );
  48. return x * x * ( 3 - 2 * x );
  49. },
  50. smootherstep: function ( x, min, max ) {
  51. if ( x <= min ) return 0;
  52. if ( x >= max ) return 1;
  53. x = ( x - min ) / ( max - min );
  54. return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
  55. },
  56. // Random integer from <low, high> interval
  57. randInt: function ( low, high ) {
  58. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  59. },
  60. // Random float from <low, high> interval
  61. randFloat: function ( low, high ) {
  62. return low + Math.random() * ( high - low );
  63. },
  64. // Random float from <-range/2, range/2> interval
  65. randFloatSpread: function ( range ) {
  66. return range * ( 0.5 - Math.random() );
  67. },
  68. // Deterministic pseudo-random float in the interval [ 0, 1 ]
  69. seededRandom: function () {
  70. var seed = 1234567;
  71. return function seededRandom( s ) {
  72. if ( s !== undefined ) seed = s % 2147483647;
  73. // Park-Miller algorithm
  74. seed = seed * 16807 % 2147483647;
  75. return ( seed - 1 ) / 2147483646;
  76. };
  77. }(),
  78. degToRad: function ( degrees ) {
  79. return degrees * MathUtils.DEG2RAD;
  80. },
  81. radToDeg: function ( radians ) {
  82. return radians * MathUtils.RAD2DEG;
  83. },
  84. isPowerOfTwo: function ( value ) {
  85. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  86. },
  87. ceilPowerOfTwo: function ( value ) {
  88. return Math.pow( 2, Math.ceil( Math.log( value ) / Math.LN2 ) );
  89. },
  90. floorPowerOfTwo: function ( value ) {
  91. return Math.pow( 2, Math.floor( Math.log( value ) / Math.LN2 ) );
  92. },
  93. setQuaternionFromProperEuler: function ( q, a, b, c, order ) {
  94. // Intrinsic Proper Euler Angles - see https://en.wikipedia.org/wiki/Euler_angles
  95. // rotations are applied to the axes in the order specified by 'order'
  96. // rotation by angle 'a' is applied first, then by angle 'b', then by angle 'c'
  97. // angles are in radians
  98. const cos = Math.cos;
  99. const sin = Math.sin;
  100. const c2 = cos( b / 2 );
  101. const s2 = sin( b / 2 );
  102. const c13 = cos( ( a + c ) / 2 );
  103. const s13 = sin( ( a + c ) / 2 );
  104. const c1_3 = cos( ( a - c ) / 2 );
  105. const s1_3 = sin( ( a - c ) / 2 );
  106. const c3_1 = cos( ( c - a ) / 2 );
  107. const s3_1 = sin( ( c - a ) / 2 );
  108. switch ( order ) {
  109. case 'XYX':
  110. q.set( c2 * s13, s2 * c1_3, s2 * s1_3, c2 * c13 );
  111. break;
  112. case 'YZY':
  113. q.set( s2 * s1_3, c2 * s13, s2 * c1_3, c2 * c13 );
  114. break;
  115. case 'ZXZ':
  116. q.set( s2 * c1_3, s2 * s1_3, c2 * s13, c2 * c13 );
  117. break;
  118. case 'XZX':
  119. q.set( c2 * s13, s2 * s3_1, s2 * c3_1, c2 * c13 );
  120. break;
  121. case 'YXY':
  122. q.set( s2 * c3_1, c2 * s13, s2 * s3_1, c2 * c13 );
  123. break;
  124. case 'ZYZ':
  125. q.set( s2 * s3_1, s2 * c3_1, c2 * s13, c2 * c13 );
  126. break;
  127. default:
  128. console.warn( 'THREE.MathUtils: .setQuaternionFromProperEuler() encountered an unknown order: ' + order );
  129. }
  130. }
  131. };
  132. export { MathUtils };