GeometryUtils.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. */
  5. THREE.GeometryUtils = {
  6. // Merge two geometries or geometry and geometry from object (using object's transform)
  7. merge: function ( geometry1, geometry2, materialIndexOffset ) {
  8. console.warn( 'THREE.GeometryUtils: .merge() has been moved to Geometry. Use geometry.merge( geometry2, matrix, materialIndexOffset ) instead.' );
  9. var matrix;
  10. if ( geometry2 instanceof THREE.Mesh ) {
  11. geometry2.matrixAutoUpdate && geometry2.updateMatrix();
  12. matrix = geometry2.matrix;
  13. geometry2 = geometry2.geometry;
  14. }
  15. geometry1.merge( geometry2, matrix, materialIndexOffset );
  16. },
  17. // Get random point in triangle (via barycentric coordinates)
  18. // (uniform distribution)
  19. // http://www.cgafaq.info/wiki/Random_Point_In_Triangle
  20. randomPointInTriangle: function () {
  21. var vector = new THREE.Vector3();
  22. return function ( vectorA, vectorB, vectorC ) {
  23. var point = new THREE.Vector3();
  24. var a = Math.random();
  25. var b = Math.random();
  26. if ( ( a + b ) > 1 ) {
  27. a = 1 - a;
  28. b = 1 - b;
  29. }
  30. var c = 1 - a - b;
  31. point.copy( vectorA );
  32. point.multiplyScalar( a );
  33. vector.copy( vectorB );
  34. vector.multiplyScalar( b );
  35. point.add( vector );
  36. vector.copy( vectorC );
  37. vector.multiplyScalar( c );
  38. point.add( vector );
  39. return point;
  40. };
  41. }(),
  42. // Get random point in face (triangle)
  43. // (uniform distribution)
  44. randomPointInFace: function ( face, geometry ) {
  45. var vA, vB, vC;
  46. vA = geometry.vertices[ face.a ];
  47. vB = geometry.vertices[ face.b ];
  48. vC = geometry.vertices[ face.c ];
  49. return THREE.GeometryUtils.randomPointInTriangle( vA, vB, vC );
  50. },
  51. // Get uniformly distributed random points in mesh
  52. // - create array with cumulative sums of face areas
  53. // - pick random number from 0 to total area
  54. // - find corresponding place in area array by binary search
  55. // - get random point in face
  56. randomPointsInGeometry: function ( geometry, n ) {
  57. var face, i,
  58. faces = geometry.faces,
  59. vertices = geometry.vertices,
  60. il = faces.length,
  61. totalArea = 0,
  62. cumulativeAreas = [],
  63. vA, vB, vC;
  64. // precompute face areas
  65. for ( i = 0; i < il; i ++ ) {
  66. face = faces[ i ];
  67. vA = vertices[ face.a ];
  68. vB = vertices[ face.b ];
  69. vC = vertices[ face.c ];
  70. face._area = THREE.GeometryUtils.triangleArea( vA, vB, vC );
  71. totalArea += face._area;
  72. cumulativeAreas[ i ] = totalArea;
  73. }
  74. // binary search cumulative areas array
  75. function binarySearchIndices( value ) {
  76. function binarySearch( start, end ) {
  77. // return closest larger index
  78. // if exact number is not found
  79. if ( end < start )
  80. return start;
  81. var mid = start + Math.floor( ( end - start ) / 2 );
  82. if ( cumulativeAreas[ mid ] > value ) {
  83. return binarySearch( start, mid - 1 );
  84. } else if ( cumulativeAreas[ mid ] < value ) {
  85. return binarySearch( mid + 1, end );
  86. } else {
  87. return mid;
  88. }
  89. }
  90. var result = binarySearch( 0, cumulativeAreas.length - 1 );
  91. return result;
  92. }
  93. // pick random face weighted by face area
  94. var r, index,
  95. result = [];
  96. var stats = {};
  97. for ( i = 0; i < n; i ++ ) {
  98. r = Math.random() * totalArea;
  99. index = binarySearchIndices( r );
  100. result[ i ] = THREE.GeometryUtils.randomPointInFace( faces[ index ], geometry );
  101. if ( ! stats[ index ] ) {
  102. stats[ index ] = 1;
  103. } else {
  104. stats[ index ] += 1;
  105. }
  106. }
  107. return result;
  108. },
  109. randomPointsInBufferGeometry: function ( geometry, n ) {
  110. var i,
  111. vertices = geometry.attributes.position.array,
  112. totalArea = 0,
  113. cumulativeAreas = [],
  114. vA, vB, vC;
  115. // precompute face areas
  116. vA = new THREE.Vector3();
  117. vB = new THREE.Vector3();
  118. vC = new THREE.Vector3();
  119. // geometry._areas = [];
  120. var il = vertices.length / 9;
  121. for ( i = 0; i < il; i ++ ) {
  122. vA.set( vertices[ i * 9 + 0 ], vertices[ i * 9 + 1 ], vertices[ i * 9 + 2 ] );
  123. vB.set( vertices[ i * 9 + 3 ], vertices[ i * 9 + 4 ], vertices[ i * 9 + 5 ] );
  124. vC.set( vertices[ i * 9 + 6 ], vertices[ i * 9 + 7 ], vertices[ i * 9 + 8 ] );
  125. area = THREE.GeometryUtils.triangleArea( vA, vB, vC );
  126. totalArea += area;
  127. cumulativeAreas.push( totalArea );
  128. }
  129. // binary search cumulative areas array
  130. function binarySearchIndices( value ) {
  131. function binarySearch( start, end ) {
  132. // return closest larger index
  133. // if exact number is not found
  134. if ( end < start )
  135. return start;
  136. var mid = start + Math.floor( ( end - start ) / 2 );
  137. if ( cumulativeAreas[ mid ] > value ) {
  138. return binarySearch( start, mid - 1 );
  139. } else if ( cumulativeAreas[ mid ] < value ) {
  140. return binarySearch( mid + 1, end );
  141. } else {
  142. return mid;
  143. }
  144. }
  145. var result = binarySearch( 0, cumulativeAreas.length - 1 );
  146. return result;
  147. }
  148. // pick random face weighted by face area
  149. var r, index,
  150. result = [];
  151. for ( i = 0; i < n; i ++ ) {
  152. r = Math.random() * totalArea;
  153. index = binarySearchIndices( r );
  154. // result[ i ] = THREE.GeometryUtils.randomPointInFace( faces[ index ], geometry, true );
  155. vA.set( vertices[ index * 9 + 0 ], vertices[ index * 9 + 1 ], vertices[ index * 9 + 2 ] );
  156. vB.set( vertices[ index * 9 + 3 ], vertices[ index * 9 + 4 ], vertices[ index * 9 + 5 ] );
  157. vC.set( vertices[ index * 9 + 6 ], vertices[ index * 9 + 7 ], vertices[ index * 9 + 8 ] );
  158. result[ i ] = THREE.GeometryUtils.randomPointInTriangle( vA, vB, vC );
  159. }
  160. return result;
  161. },
  162. // Get triangle area (half of parallelogram)
  163. // http://mathworld.wolfram.com/TriangleArea.html
  164. triangleArea: function () {
  165. var vector1 = new THREE.Vector3();
  166. var vector2 = new THREE.Vector3();
  167. return function ( vectorA, vectorB, vectorC ) {
  168. vector1.subVectors( vectorB, vectorA );
  169. vector2.subVectors( vectorC, vectorA );
  170. vector1.cross( vector2 );
  171. return 0.5 * vector1.length();
  172. };
  173. }(),
  174. center: function ( geometry ) {
  175. console.warn( 'THREE.GeometryUtils: .center() has been moved to Geometry. Use geometry.center() instead.' );
  176. return geometry.center();
  177. }
  178. };