GeometryUtils.js 5.5 KB

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