ConvexGeometry.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @author qiao / https://github.com/qiao
  3. * @fileoverview This is a convex hull generator using the incremental method.
  4. * The complexity is O(n^2) where n is the number of vertices.
  5. * O(nlogn) algorithms do exist, but they are much more complicated.
  6. *
  7. * Benchmark:
  8. *
  9. * Platform: CPU: P7350 @2.00GHz Engine: V8
  10. *
  11. * Num Vertices Time(ms)
  12. *
  13. * 10 1
  14. * 20 3
  15. * 30 19
  16. * 40 48
  17. * 50 107
  18. */
  19. THREE.ConvexGeometry = function( vertices ) {
  20. THREE.Geometry.call( this );
  21. var faces = [ [ 0, 1, 2 ], [ 0, 2, 1 ] ];
  22. for ( var i = 3; i < vertices.length; i ++ ) {
  23. addPoint( i );
  24. }
  25. function addPoint( vertexId ) {
  26. var vertex = vertices[ vertexId ].clone();
  27. var mag = vertex.length();
  28. vertex.x += mag * randomOffset();
  29. vertex.y += mag * randomOffset();
  30. vertex.z += mag * randomOffset();
  31. var hole = [];
  32. for ( var f = 0; f < faces.length; ) {
  33. var face = faces[ f ];
  34. // for each face, if the vertex can see it,
  35. // then we try to add the face's edges into the hole.
  36. if ( visible( face, vertex ) ) {
  37. for ( var e = 0; e < 3; e ++ ) {
  38. var edge = [ face[ e ], face[ ( e + 1 ) % 3 ] ];
  39. var boundary = true;
  40. // remove duplicated edges.
  41. for ( var h = 0; h < hole.length; h ++ ) {
  42. if ( equalEdge( hole[ h ], edge ) ) {
  43. hole[ h ] = hole[ hole.length - 1 ];
  44. hole.pop();
  45. boundary = false;
  46. break;
  47. }
  48. }
  49. if ( boundary ) {
  50. hole.push( edge );
  51. }
  52. }
  53. // remove faces[ f ]
  54. faces[ f ] = faces[ faces.length - 1 ];
  55. faces.pop();
  56. } else {
  57. // not visible
  58. f ++;
  59. }
  60. }
  61. // construct the new faces formed by the edges of the hole and the vertex
  62. for ( var h = 0; h < hole.length; h ++ ) {
  63. faces.push( [
  64. hole[ h ][ 0 ],
  65. hole[ h ][ 1 ],
  66. vertexId
  67. ] );
  68. }
  69. }
  70. /**
  71. * Whether the face is visible from the vertex
  72. */
  73. function visible( face, vertex ) {
  74. var va = vertices[ face[ 0 ] ];
  75. var vb = vertices[ face[ 1 ] ];
  76. var vc = vertices[ face[ 2 ] ];
  77. var n = normal( va, vb, vc );
  78. // distance from face to origin
  79. var dist = n.dot( va );
  80. return n.dot( vertex ) >= dist;
  81. }
  82. /**
  83. * Face normal
  84. */
  85. function normal( va, vb, vc ) {
  86. var cb = new THREE.Vector3();
  87. var ab = new THREE.Vector3();
  88. cb.subVectors( vc, vb );
  89. ab.subVectors( va, vb );
  90. cb.cross( ab );
  91. cb.normalize();
  92. return cb;
  93. }
  94. /**
  95. * Detect whether two edges are equal.
  96. * Note that when constructing the convex hull, two same edges can only
  97. * be of the negative direction.
  98. */
  99. function equalEdge( ea, eb ) {
  100. return ea[ 0 ] === eb[ 1 ] && ea[ 1 ] === eb[ 0 ];
  101. }
  102. /**
  103. * Create a random offset between -1e-6 and 1e-6.
  104. */
  105. function randomOffset() {
  106. return ( Math.random() - 0.5 ) * 2 * 1e-6;
  107. }
  108. // Push vertices into `this.vertices`, skipping those inside the hull
  109. var id = 0;
  110. var newId = new Array( vertices.length ); // map from old vertex id to new id
  111. for ( var i = 0; i < faces.length; i ++ ) {
  112. var face = faces[ i ];
  113. for ( var j = 0; j < 3; j ++ ) {
  114. if ( newId[ face[ j ] ] === undefined ) {
  115. newId[ face[ j ] ] = id ++;
  116. this.vertices.push( vertices[ face[ j ] ] );
  117. }
  118. face[ j ] = newId[ face[ j ] ];
  119. }
  120. }
  121. // Convert faces into instances of THREE.Face3
  122. for ( var i = 0; i < faces.length; i ++ ) {
  123. this.faces.push( new THREE.Face3(
  124. faces[ i ][ 0 ],
  125. faces[ i ][ 1 ],
  126. faces[ i ][ 2 ]
  127. ) );
  128. }
  129. this.computeFaceNormals();
  130. // Compute flat vertex normals
  131. for ( var i = 0; i < this.faces.length; i ++ ) {
  132. var face = this.faces[ i ];
  133. var normal = face.normal;
  134. face.vertexNormals[ 0 ] = normal.clone();
  135. face.vertexNormals[ 1 ] = normal.clone();
  136. face.vertexNormals[ 2 ] = normal.clone();
  137. }
  138. };
  139. THREE.ConvexGeometry.prototype = Object.create( THREE.Geometry.prototype );
  140. THREE.ConvexGeometry.prototype.constructor = THREE.ConvexGeometry;