MeshSurfaceSampler.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {
  2. Triangle
  3. } from "../../../build/three.module.js";
  4. /**
  5. * Utility class for sampling weighted random points on the surface of a mesh.
  6. *
  7. * Building the sampler is a one-time O(n) operation. Once built, any number of
  8. * random samples may be selected in O(logn) time. Memory usage is O(n).
  9. *
  10. * References:
  11. * - http://www.joesfer.com/?p=84
  12. * - https://stackoverflow.com/a/4322940/1314762
  13. */
  14. var MeshSurfaceSampler = ( function () {
  15. var _face = new Triangle();
  16. function MeshSurfaceSampler( mesh ) {
  17. var geometry = mesh.geometry;
  18. if ( ! geometry.isBufferGeometry || geometry.attributes.position.itemSize !== 3 ) {
  19. throw new Error( 'THREE.MeshSurfaceSampler: Requires BufferGeometry triangle mesh.' );
  20. }
  21. if ( geometry.index ) {
  22. console.warn( 'THREE.MeshSurfaceSampler: Converting geometry to non-indexed BufferGeometry.' );
  23. geometry = geometry.toNonIndexed();
  24. }
  25. this.geometry = geometry;
  26. this.randomFunction = Math.random;
  27. this.positionAttribute = this.geometry.getAttribute( 'position' );
  28. this.weightAttribute = null;
  29. this.distribution = null;
  30. }
  31. MeshSurfaceSampler.prototype = {
  32. constructor: MeshSurfaceSampler,
  33. setWeightAttribute: function ( name ) {
  34. this.weightAttribute = name ? this.geometry.getAttribute( name ) : null;
  35. return this;
  36. },
  37. build: function () {
  38. var positionAttribute = this.positionAttribute;
  39. var weightAttribute = this.weightAttribute;
  40. var faceWeights = new Float32Array( positionAttribute.count / 3 );
  41. // Accumulate weights for each mesh face.
  42. for ( var i = 0; i < positionAttribute.count; i += 3 ) {
  43. var faceWeight = 1;
  44. if ( weightAttribute ) {
  45. faceWeight = weightAttribute.getX( i )
  46. + weightAttribute.getX( i + 1 )
  47. + weightAttribute.getX( i + 2 );
  48. }
  49. _face.a.fromBufferAttribute( positionAttribute, i );
  50. _face.b.fromBufferAttribute( positionAttribute, i + 1 );
  51. _face.c.fromBufferAttribute( positionAttribute, i + 2 );
  52. faceWeight *= _face.getArea();
  53. faceWeights[ i / 3 ] = faceWeight;
  54. }
  55. // Store cumulative total face weights in an array, where weight index
  56. // corresponds to face index.
  57. this.distribution = new Float32Array( positionAttribute.count / 3 );
  58. var cumulativeTotal = 0;
  59. for ( var i = 0; i < faceWeights.length; i ++ ) {
  60. cumulativeTotal += faceWeights[ i ];
  61. this.distribution[ i ] = cumulativeTotal;
  62. }
  63. return this;
  64. },
  65. setRandomGenerator: function ( randomFunction ) {
  66. this.randomFunction = randomFunction;
  67. return this;
  68. },
  69. sample: function ( targetPosition, targetNormal ) {
  70. var cumulativeTotal = this.distribution[ this.distribution.length - 1 ];
  71. var faceIndex = this.binarySearch( this.randomFunction() * cumulativeTotal );
  72. return this.sampleFace( faceIndex, targetPosition, targetNormal );
  73. },
  74. binarySearch: function ( x ) {
  75. var dist = this.distribution;
  76. var start = 0;
  77. var end = dist.length - 1;
  78. var index = - 1;
  79. while ( start <= end ) {
  80. var mid = Math.ceil( ( start + end ) / 2 );
  81. if ( mid === 0 || dist[ mid - 1 ] <= x && dist[ mid ] > x ) {
  82. index = mid;
  83. break;
  84. } else if ( x < dist[ mid ] ) {
  85. end = mid - 1;
  86. } else {
  87. start = mid + 1;
  88. }
  89. }
  90. return index;
  91. },
  92. sampleFace: function ( faceIndex, targetPosition, targetNormal ) {
  93. var u = this.randomFunction();
  94. var v = this.randomFunction();
  95. if ( u + v > 1 ) {
  96. u = 1 - u;
  97. v = 1 - v;
  98. }
  99. _face.a.fromBufferAttribute( this.positionAttribute, faceIndex * 3 );
  100. _face.b.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 1 );
  101. _face.c.fromBufferAttribute( this.positionAttribute, faceIndex * 3 + 2 );
  102. targetPosition
  103. .set( 0, 0, 0 )
  104. .addScaledVector( _face.a, u )
  105. .addScaledVector( _face.b, v )
  106. .addScaledVector( _face.c, 1 - ( u + v ) );
  107. _face.getNormal( targetNormal );
  108. return this;
  109. }
  110. };
  111. return MeshSurfaceSampler;
  112. } )();
  113. export { MeshSurfaceSampler };