hilbert3D.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Hilbert Curve: Generates 2D-Coordinates in a very fast way.
  3. *
  4. * @author Dylan Grafmyre
  5. *
  6. * Based on work by:
  7. * @author Thomas Diewald
  8. * @link http://www.openprocessing.org/visuals/?visualID=15599
  9. *
  10. * Based on `examples/canvas_lines_colors.html`:
  11. * @author OpenShift guest
  12. * @link https://github.com/mrdoob/three.js/blob/8413a860aa95ed29c79cbb7f857c97d7880d260f/examples/canvas_lines_colors.html
  13. * @see Line 149 - 186
  14. *
  15. * @param center Center of Hilbert curve.
  16. * @param size Total width of Hilbert curve.
  17. * @param iterations Number of subdivisions.
  18. * @param v0 Corner index -X, +Y, -Z.
  19. * @param v1 Corner index -X, +Y, +Z.
  20. * @param v2 Corner index -X, -Y, +Z.
  21. * @param v3 Corner index -X, -Y, -Z.
  22. * @param v4 Corner index +X, -Y, -Z.
  23. * @param v5 Corner index +X, -Y, +Z.
  24. * @param v6 Corner index +X, +Y, +Z.
  25. * @param v7 Corner index +X, +Y, -Z.
  26. */
  27. function hilbert3D( center, size, iterations, v0, v1, v2, v3, v4, v5, v6, v7 ) {
  28. // Default Vars
  29. var center = undefined !== center ? center : new THREE.Vector3( 0, 0, 0 ),
  30. size = undefined !== size ? size : 10,
  31. half = size / 2,
  32. iterations = undefined !== iterations ? iterations : 1,
  33. v0 = undefined !== v0 ? v0 : 0,
  34. v1 = undefined !== v1 ? v1 : 1,
  35. v2 = undefined !== v2 ? v2 : 2,
  36. v3 = undefined !== v3 ? v3 : 3,
  37. v4 = undefined !== v4 ? v4 : 4,
  38. v5 = undefined !== v5 ? v5 : 5,
  39. v6 = undefined !== v6 ? v6 : 6,
  40. v7 = undefined !== v7 ? v7 : 7
  41. ;
  42. var vec_s = [
  43. new THREE.Vector3( center.x - half, center.y + half, center.z - half ),
  44. new THREE.Vector3( center.x - half, center.y + half, center.z + half ),
  45. new THREE.Vector3( center.x - half, center.y - half, center.z + half ),
  46. new THREE.Vector3( center.x - half, center.y - half, center.z - half ),
  47. new THREE.Vector3( center.x + half, center.y - half, center.z - half ),
  48. new THREE.Vector3( center.x + half, center.y - half, center.z + half ),
  49. new THREE.Vector3( center.x + half, center.y + half, center.z + half ),
  50. new THREE.Vector3( center.x + half, center.y + half, center.z - half )
  51. ];
  52. var vec = [
  53. vec_s[ v0 ],
  54. vec_s[ v1 ],
  55. vec_s[ v2 ],
  56. vec_s[ v3 ],
  57. vec_s[ v4 ],
  58. vec_s[ v5 ],
  59. vec_s[ v6 ],
  60. vec_s[ v7 ]
  61. ];
  62. // Recurse iterations
  63. if ( -- iterations >= 0 ) {
  64. var tmp = [];
  65. Array.prototype.push.apply( tmp, hilbert3D ( vec[ 0 ], half, iterations, v0, v3, v4, v7, v6, v5, v2, v1 ) );
  66. Array.prototype.push.apply( tmp, hilbert3D ( vec[ 1 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
  67. Array.prototype.push.apply( tmp, hilbert3D ( vec[ 2 ], half, iterations, v0, v7, v6, v1, v2, v5, v4, v3 ) );
  68. Array.prototype.push.apply( tmp, hilbert3D ( vec[ 3 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
  69. Array.prototype.push.apply( tmp, hilbert3D ( vec[ 4 ], half, iterations, v2, v3, v0, v1, v6, v7, v4, v5 ) );
  70. Array.prototype.push.apply( tmp, hilbert3D ( vec[ 5 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
  71. Array.prototype.push.apply( tmp, hilbert3D ( vec[ 6 ], half, iterations, v4, v3, v2, v5, v6, v1, v0, v7 ) );
  72. Array.prototype.push.apply( tmp, hilbert3D ( vec[ 7 ], half, iterations, v6, v5, v2, v1, v0, v3, v4, v7 ) );
  73. // Return recursive call
  74. return tmp;
  75. }
  76. // Return complete Hilbert Curve.
  77. return vec;
  78. }