0
0

GeometryUtils.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ( function () {
  2. /**
  3. * Generates 2D-Coordinates in a very fast way.
  4. *
  5. * Based on work by:
  6. * @link http://www.openprocessing.org/sketch/15493
  7. *
  8. * @param center Center of Hilbert curve.
  9. * @param size Total width of Hilbert curve.
  10. * @param iterations Number of subdivisions.
  11. * @param v0 Corner index -X, -Z.
  12. * @param v1 Corner index -X, +Z.
  13. * @param v2 Corner index +X, +Z.
  14. * @param v3 Corner index +X, -Z.
  15. */
  16. function hilbert2D( center = new THREE.Vector3( 0, 0, 0 ), size = 10, iterations = 1, v0 = 0, v1 = 1, v2 = 2, v3 = 3 ) {
  17. const half = size / 2;
  18. const vec_s = [ new THREE.Vector3( center.x - half, center.y, center.z - half ), new THREE.Vector3( center.x - half, center.y, center.z + half ), new THREE.Vector3( center.x + half, center.y, center.z + half ), new THREE.Vector3( center.x + half, center.y, center.z - half ) ];
  19. const vec = [ vec_s[ v0 ], vec_s[ v1 ], vec_s[ v2 ], vec_s[ v3 ] ]; // Recurse iterations
  20. if ( 0 <= -- iterations ) {
  21. const tmp = [];
  22. Array.prototype.push.apply( tmp, hilbert2D( vec[ 0 ], half, iterations, v0, v3, v2, v1 ) );
  23. Array.prototype.push.apply( tmp, hilbert2D( vec[ 1 ], half, iterations, v0, v1, v2, v3 ) );
  24. Array.prototype.push.apply( tmp, hilbert2D( vec[ 2 ], half, iterations, v0, v1, v2, v3 ) );
  25. Array.prototype.push.apply( tmp, hilbert2D( vec[ 3 ], half, iterations, v2, v1, v0, v3 ) ); // Return recursive call
  26. return tmp;
  27. } // Return complete Hilbert Curve.
  28. return vec;
  29. }
  30. /**
  31. * Generates 3D-Coordinates in a very fast way.
  32. *
  33. * Based on work by:
  34. * @link http://www.openprocessing.org/visuals/?visualID=15599
  35. *
  36. * @param center Center of Hilbert curve.
  37. * @param size Total width of Hilbert curve.
  38. * @param iterations Number of subdivisions.
  39. * @param v0 Corner index -X, +Y, -Z.
  40. * @param v1 Corner index -X, +Y, +Z.
  41. * @param v2 Corner index -X, -Y, +Z.
  42. * @param v3 Corner index -X, -Y, -Z.
  43. * @param v4 Corner index +X, -Y, -Z.
  44. * @param v5 Corner index +X, -Y, +Z.
  45. * @param v6 Corner index +X, +Y, +Z.
  46. * @param v7 Corner index +X, +Y, -Z.
  47. */
  48. function hilbert3D( center = new THREE.Vector3( 0, 0, 0 ), size = 10, iterations = 1, v0 = 0, v1 = 1, v2 = 2, v3 = 3, v4 = 4, v5 = 5, v6 = 6, v7 = 7 ) {
  49. // Default Vars
  50. const half = size / 2;
  51. const vec_s = [ new THREE.Vector3( center.x - half, center.y + half, center.z - half ), new THREE.Vector3( center.x - half, center.y + half, center.z + half ), new THREE.Vector3( center.x - half, center.y - half, center.z + half ), new THREE.Vector3( center.x - half, center.y - half, center.z - half ), new THREE.Vector3( center.x + half, center.y - half, center.z - half ), new THREE.Vector3( center.x + half, center.y - half, center.z + half ), new THREE.Vector3( center.x + half, center.y + half, center.z + half ), new THREE.Vector3( center.x + half, center.y + half, center.z - half ) ];
  52. const vec = [ vec_s[ v0 ], vec_s[ v1 ], vec_s[ v2 ], vec_s[ v3 ], vec_s[ v4 ], vec_s[ v5 ], vec_s[ v6 ], vec_s[ v7 ] ]; // Recurse iterations
  53. if ( -- iterations >= 0 ) {
  54. const tmp = [];
  55. Array.prototype.push.apply( tmp, hilbert3D( vec[ 0 ], half, iterations, v0, v3, v4, v7, v6, v5, v2, v1 ) );
  56. Array.prototype.push.apply( tmp, hilbert3D( vec[ 1 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
  57. Array.prototype.push.apply( tmp, hilbert3D( vec[ 2 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
  58. Array.prototype.push.apply( tmp, hilbert3D( vec[ 3 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
  59. Array.prototype.push.apply( tmp, hilbert3D( vec[ 4 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
  60. Array.prototype.push.apply( tmp, hilbert3D( vec[ 5 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
  61. Array.prototype.push.apply( tmp, hilbert3D( vec[ 6 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
  62. Array.prototype.push.apply( tmp, hilbert3D( vec[ 7 ], half, iterations, v6, v5, v2, v1, v0, v3, v4, v7 ) ); // Return recursive call
  63. return tmp;
  64. } // Return complete Hilbert Curve.
  65. return vec;
  66. }
  67. /**
  68. * Generates a Gosper curve (lying in the XY plane)
  69. *
  70. * https://gist.github.com/nitaku/6521802
  71. *
  72. * @param size The size of a single gosper island.
  73. */
  74. function gosper( size = 1 ) {
  75. function fractalize( config ) {
  76. let output;
  77. let input = config.axiom;
  78. for ( let i = 0, il = config.steps; 0 <= il ? i < il : i > il; 0 <= il ? i ++ : i -- ) {
  79. output = '';
  80. for ( let j = 0, jl = input.length; j < jl; j ++ ) {
  81. const char = input[ j ];
  82. if ( char in config.rules ) {
  83. output += config.rules[ char ];
  84. } else {
  85. output += char;
  86. }
  87. }
  88. input = output;
  89. }
  90. return output;
  91. }
  92. function toPoints( config ) {
  93. let currX = 0,
  94. currY = 0;
  95. let angle = 0;
  96. const path = [ 0, 0, 0 ];
  97. const fractal = config.fractal;
  98. for ( let i = 0, l = fractal.length; i < l; i ++ ) {
  99. const char = fractal[ i ];
  100. if ( char === '+' ) {
  101. angle += config.angle;
  102. } else if ( char === '-' ) {
  103. angle -= config.angle;
  104. } else if ( char === 'F' ) {
  105. currX += config.size * Math.cos( angle );
  106. currY += - config.size * Math.sin( angle );
  107. path.push( currX, currY, 0 );
  108. }
  109. }
  110. return path;
  111. } //
  112. const gosper = fractalize( {
  113. axiom: 'A',
  114. steps: 4,
  115. rules: {
  116. A: 'A+BF++BF-FA--FAFA-BF+',
  117. B: '-FA+BFBF++BF+FA--FA-B'
  118. }
  119. } );
  120. const points = toPoints( {
  121. fractal: gosper,
  122. size: size,
  123. angle: Math.PI / 3 // 60 degrees
  124. } );
  125. return points;
  126. }
  127. THREE.GeometryUtils = {};
  128. THREE.GeometryUtils.gosper = gosper;
  129. THREE.GeometryUtils.hilbert2D = hilbert2D;
  130. THREE.GeometryUtils.hilbert3D = hilbert3D;
  131. } )();