GeometryUtils.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. import {
  6. Vector3
  7. } from "../../../build/three.module.js";
  8. var GeometryUtils = {
  9. /**
  10. * Generates 2D-Coordinates in a very fast way.
  11. *
  12. * @author Dylan Grafmyre
  13. *
  14. * Based on work by:
  15. * @author Thomas Diewald
  16. * @link http://www.openprocessing.org/sketch/15493
  17. *
  18. * @param center Center of Hilbert curve.
  19. * @param size Total width of Hilbert curve.
  20. * @param iterations Number of subdivisions.
  21. * @param v0 Corner index -X, -Z.
  22. * @param v1 Corner index -X, +Z.
  23. * @param v2 Corner index +X, +Z.
  24. * @param v3 Corner index +X, -Z.
  25. */
  26. hilbert2D: function ( center, size, iterations, v0, v1, v2, v3 ) {
  27. // Default Vars
  28. var center = center !== undefined ? center : new Vector3( 0, 0, 0 ),
  29. size = size !== undefined ? size : 10,
  30. half = size / 2,
  31. iterations = iterations !== undefined ? iterations : 1,
  32. v0 = v0 !== undefined ? v0 : 0,
  33. v1 = v1 !== undefined ? v1 : 1,
  34. v2 = v2 !== undefined ? v2 : 2,
  35. v3 = v3 !== undefined ? v3 : 3
  36. ;
  37. var vec_s = [
  38. new Vector3( center.x - half, center.y, center.z - half ),
  39. new Vector3( center.x - half, center.y, center.z + half ),
  40. new Vector3( center.x + half, center.y, center.z + half ),
  41. new Vector3( center.x + half, center.y, center.z - half )
  42. ];
  43. var vec = [
  44. vec_s[ v0 ],
  45. vec_s[ v1 ],
  46. vec_s[ v2 ],
  47. vec_s[ v3 ]
  48. ];
  49. // Recurse iterations
  50. if ( 0 <= -- iterations ) {
  51. var tmp = [];
  52. Array.prototype.push.apply( tmp, GeometryUtils.hilbert2D( vec[ 0 ], half, iterations, v0, v3, v2, v1 ) );
  53. Array.prototype.push.apply( tmp, GeometryUtils.hilbert2D( vec[ 1 ], half, iterations, v0, v1, v2, v3 ) );
  54. Array.prototype.push.apply( tmp, GeometryUtils.hilbert2D( vec[ 2 ], half, iterations, v0, v1, v2, v3 ) );
  55. Array.prototype.push.apply( tmp, GeometryUtils.hilbert2D( vec[ 3 ], half, iterations, v2, v1, v0, v3 ) );
  56. // Return recursive call
  57. return tmp;
  58. }
  59. // Return complete Hilbert Curve.
  60. return vec;
  61. },
  62. /**
  63. * Generates 3D-Coordinates in a very fast way.
  64. *
  65. * @author Dylan Grafmyre
  66. *
  67. * Based on work by:
  68. * @author Thomas Diewald
  69. * @link http://www.openprocessing.org/visuals/?visualID=15599
  70. *
  71. * @param center Center of Hilbert curve.
  72. * @param size Total width of Hilbert curve.
  73. * @param iterations Number of subdivisions.
  74. * @param v0 Corner index -X, +Y, -Z.
  75. * @param v1 Corner index -X, +Y, +Z.
  76. * @param v2 Corner index -X, -Y, +Z.
  77. * @param v3 Corner index -X, -Y, -Z.
  78. * @param v4 Corner index +X, -Y, -Z.
  79. * @param v5 Corner index +X, -Y, +Z.
  80. * @param v6 Corner index +X, +Y, +Z.
  81. * @param v7 Corner index +X, +Y, -Z.
  82. */
  83. hilbert3D: function ( center, size, iterations, v0, v1, v2, v3, v4, v5, v6, v7 ) {
  84. // Default Vars
  85. var center = center !== undefined ? center : new Vector3( 0, 0, 0 ),
  86. size = size !== undefined ? size : 10,
  87. half = size / 2,
  88. iterations = iterations !== undefined ? iterations : 1,
  89. v0 = v0 !== undefined ? v0 : 0,
  90. v1 = v1 !== undefined ? v1 : 1,
  91. v2 = v2 !== undefined ? v2 : 2,
  92. v3 = v3 !== undefined ? v3 : 3,
  93. v4 = v4 !== undefined ? v4 : 4,
  94. v5 = v5 !== undefined ? v5 : 5,
  95. v6 = v6 !== undefined ? v6 : 6,
  96. v7 = v7 !== undefined ? v7 : 7
  97. ;
  98. var vec_s = [
  99. new Vector3( center.x - half, center.y + half, center.z - half ),
  100. new Vector3( center.x - half, center.y + half, center.z + half ),
  101. new Vector3( center.x - half, center.y - half, center.z + half ),
  102. new Vector3( center.x - half, center.y - half, center.z - half ),
  103. new Vector3( center.x + half, center.y - half, center.z - half ),
  104. new Vector3( center.x + half, center.y - half, center.z + half ),
  105. new Vector3( center.x + half, center.y + half, center.z + half ),
  106. new Vector3( center.x + half, center.y + half, center.z - half )
  107. ];
  108. var vec = [
  109. vec_s[ v0 ],
  110. vec_s[ v1 ],
  111. vec_s[ v2 ],
  112. vec_s[ v3 ],
  113. vec_s[ v4 ],
  114. vec_s[ v5 ],
  115. vec_s[ v6 ],
  116. vec_s[ v7 ]
  117. ];
  118. // Recurse iterations
  119. if ( -- iterations >= 0 ) {
  120. var tmp = [];
  121. Array.prototype.push.apply( tmp, GeometryUtils.hilbert3D( vec[ 0 ], half, iterations, v0, v3, v4, v7, v6, v5, v2, v1 ) );
  122. Array.prototype.push.apply( tmp, GeometryUtils.hilbert3D( vec[ 1 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
  123. Array.prototype.push.apply( tmp, GeometryUtils.hilbert3D( vec[ 2 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
  124. Array.prototype.push.apply( tmp, GeometryUtils.hilbert3D( vec[ 3 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
  125. Array.prototype.push.apply( tmp, GeometryUtils.hilbert3D( vec[ 4 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
  126. Array.prototype.push.apply( tmp, GeometryUtils.hilbert3D( vec[ 5 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
  127. Array.prototype.push.apply( tmp, GeometryUtils.hilbert3D( vec[ 6 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
  128. Array.prototype.push.apply( tmp, GeometryUtils.hilbert3D( vec[ 7 ], half, iterations, v6, v5, v2, v1, v0, v3, v4, v7 ) );
  129. // Return recursive call
  130. return tmp;
  131. }
  132. // Return complete Hilbert Curve.
  133. return vec;
  134. }
  135. };
  136. export { GeometryUtils };