ConvexHull.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. console.warn( "THREE.ConvexHull: As part of the transition to ES6 Modules, the files in 'examples/js' have been deprecated in r117 (May 2020) and will be deleted in r124 (December 2020). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author Mugen87 / https://github.com/Mugen87
  4. *
  5. * Ported from: https://github.com/maurizzzio/quickhull3d/ by Mauricio Poppe (https://github.com/maurizzzio)
  6. *
  7. */
  8. THREE.ConvexHull = ( function () {
  9. var Visible = 0;
  10. var Deleted = 1;
  11. var v1 = new THREE.Vector3();
  12. function ConvexHull() {
  13. this.tolerance = - 1;
  14. this.faces = []; // the generated faces of the convex hull
  15. this.newFaces = []; // this array holds the faces that are generated within a single iteration
  16. // the vertex lists work as follows:
  17. //
  18. // let 'a' and 'b' be 'Face' instances
  19. // let 'v' be points wrapped as instance of 'Vertex'
  20. //
  21. // [v, v, ..., v, v, v, ...]
  22. // ^ ^
  23. // | |
  24. // a.outside b.outside
  25. //
  26. this.assigned = new VertexList();
  27. this.unassigned = new VertexList();
  28. this.vertices = []; // vertices of the hull (internal representation of given geometry data)
  29. }
  30. Object.assign( ConvexHull.prototype, {
  31. setFromPoints: function ( points ) {
  32. if ( Array.isArray( points ) !== true ) {
  33. console.error( 'THREE.ConvexHull: Points parameter is not an array.' );
  34. }
  35. if ( points.length < 4 ) {
  36. console.error( 'THREE.ConvexHull: The algorithm needs at least four points.' );
  37. }
  38. this.makeEmpty();
  39. for ( var i = 0, l = points.length; i < l; i ++ ) {
  40. this.vertices.push( new VertexNode( points[ i ] ) );
  41. }
  42. this.compute();
  43. return this;
  44. },
  45. setFromObject: function ( object ) {
  46. var points = [];
  47. object.updateMatrixWorld( true );
  48. object.traverse( function ( node ) {
  49. var i, l, point;
  50. var geometry = node.geometry;
  51. if ( geometry !== undefined ) {
  52. if ( geometry.isGeometry ) {
  53. var vertices = geometry.vertices;
  54. for ( i = 0, l = vertices.length; i < l; i ++ ) {
  55. point = vertices[ i ].clone();
  56. point.applyMatrix4( node.matrixWorld );
  57. points.push( point );
  58. }
  59. } else if ( geometry.isBufferGeometry ) {
  60. var attribute = geometry.attributes.position;
  61. if ( attribute !== undefined ) {
  62. for ( i = 0, l = attribute.count; i < l; i ++ ) {
  63. point = new THREE.Vector3();
  64. point.fromBufferAttribute( attribute, i ).applyMatrix4( node.matrixWorld );
  65. points.push( point );
  66. }
  67. }
  68. }
  69. }
  70. } );
  71. return this.setFromPoints( points );
  72. },
  73. containsPoint: function ( point ) {
  74. var faces = this.faces;
  75. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  76. var face = faces[ i ];
  77. // compute signed distance and check on what half space the point lies
  78. if ( face.distanceToPoint( point ) > this.tolerance ) return false;
  79. }
  80. return true;
  81. },
  82. intersectRay: function ( ray, target ) {
  83. // based on "Fast Ray-Convex Polyhedron Intersection" by Eric Haines, GRAPHICS GEMS II
  84. var faces = this.faces;
  85. var tNear = - Infinity;
  86. var tFar = Infinity;
  87. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  88. var face = faces[ i ];
  89. // interpret faces as planes for the further computation
  90. var vN = face.distanceToPoint( ray.origin );
  91. var vD = face.normal.dot( ray.direction );
  92. // if the origin is on the positive side of a plane (so the plane can "see" the origin) and
  93. // the ray is turned away or parallel to the plane, there is no intersection
  94. if ( vN > 0 && vD >= 0 ) return null;
  95. // compute the distance from the ray’s origin to the intersection with the plane
  96. var t = ( vD !== 0 ) ? ( - vN / vD ) : 0;
  97. // only proceed if the distance is positive. a negative distance means the intersection point
  98. // lies "behind" the origin
  99. if ( t <= 0 ) continue;
  100. // now categorized plane as front-facing or back-facing
  101. if ( vD > 0 ) {
  102. // plane faces away from the ray, so this plane is a back-face
  103. tFar = Math.min( t, tFar );
  104. } else {
  105. // front-face
  106. tNear = Math.max( t, tNear );
  107. }
  108. if ( tNear > tFar ) {
  109. // if tNear ever is greater than tFar, the ray must miss the convex hull
  110. return null;
  111. }
  112. }
  113. // evaluate intersection point
  114. // always try tNear first since its the closer intersection point
  115. if ( tNear !== - Infinity ) {
  116. ray.at( tNear, target );
  117. } else {
  118. ray.at( tFar, target );
  119. }
  120. return target;
  121. },
  122. intersectsRay: function ( ray ) {
  123. return this.intersectRay( ray, v1 ) !== null;
  124. },
  125. makeEmpty: function () {
  126. this.faces = [];
  127. this.vertices = [];
  128. return this;
  129. },
  130. // Adds a vertex to the 'assigned' list of vertices and assigns it to the given face
  131. addVertexToFace: function ( vertex, face ) {
  132. vertex.face = face;
  133. if ( face.outside === null ) {
  134. this.assigned.append( vertex );
  135. } else {
  136. this.assigned.insertBefore( face.outside, vertex );
  137. }
  138. face.outside = vertex;
  139. return this;
  140. },
  141. // Removes a vertex from the 'assigned' list of vertices and from the given face
  142. removeVertexFromFace: function ( vertex, face ) {
  143. if ( vertex === face.outside ) {
  144. // fix face.outside link
  145. if ( vertex.next !== null && vertex.next.face === face ) {
  146. // face has at least 2 outside vertices, move the 'outside' reference
  147. face.outside = vertex.next;
  148. } else {
  149. // vertex was the only outside vertex that face had
  150. face.outside = null;
  151. }
  152. }
  153. this.assigned.remove( vertex );
  154. return this;
  155. },
  156. // Removes all the visible vertices that a given face is able to see which are stored in the 'assigned' vertext list
  157. removeAllVerticesFromFace: function ( face ) {
  158. if ( face.outside !== null ) {
  159. // reference to the first and last vertex of this face
  160. var start = face.outside;
  161. var end = face.outside;
  162. while ( end.next !== null && end.next.face === face ) {
  163. end = end.next;
  164. }
  165. this.assigned.removeSubList( start, end );
  166. // fix references
  167. start.prev = end.next = null;
  168. face.outside = null;
  169. return start;
  170. }
  171. },
  172. // Removes all the visible vertices that 'face' is able to see
  173. deleteFaceVertices: function ( face, absorbingFace ) {
  174. var faceVertices = this.removeAllVerticesFromFace( face );
  175. if ( faceVertices !== undefined ) {
  176. if ( absorbingFace === undefined ) {
  177. // mark the vertices to be reassigned to some other face
  178. this.unassigned.appendChain( faceVertices );
  179. } else {
  180. // if there's an absorbing face try to assign as many vertices as possible to it
  181. var vertex = faceVertices;
  182. do {
  183. // we need to buffer the subsequent vertex at this point because the 'vertex.next' reference
  184. // will be changed by upcoming method calls
  185. var nextVertex = vertex.next;
  186. var distance = absorbingFace.distanceToPoint( vertex.point );
  187. // check if 'vertex' is able to see 'absorbingFace'
  188. if ( distance > this.tolerance ) {
  189. this.addVertexToFace( vertex, absorbingFace );
  190. } else {
  191. this.unassigned.append( vertex );
  192. }
  193. // now assign next vertex
  194. vertex = nextVertex;
  195. } while ( vertex !== null );
  196. }
  197. }
  198. return this;
  199. },
  200. // Reassigns as many vertices as possible from the unassigned list to the new faces
  201. resolveUnassignedPoints: function ( newFaces ) {
  202. if ( this.unassigned.isEmpty() === false ) {
  203. var vertex = this.unassigned.first();
  204. do {
  205. // buffer 'next' reference, see .deleteFaceVertices()
  206. var nextVertex = vertex.next;
  207. var maxDistance = this.tolerance;
  208. var maxFace = null;
  209. for ( var i = 0; i < newFaces.length; i ++ ) {
  210. var face = newFaces[ i ];
  211. if ( face.mark === Visible ) {
  212. var distance = face.distanceToPoint( vertex.point );
  213. if ( distance > maxDistance ) {
  214. maxDistance = distance;
  215. maxFace = face;
  216. }
  217. if ( maxDistance > 1000 * this.tolerance ) break;
  218. }
  219. }
  220. // 'maxFace' can be null e.g. if there are identical vertices
  221. if ( maxFace !== null ) {
  222. this.addVertexToFace( vertex, maxFace );
  223. }
  224. vertex = nextVertex;
  225. } while ( vertex !== null );
  226. }
  227. return this;
  228. },
  229. // Computes the extremes of a simplex which will be the initial hull
  230. computeExtremes: function () {
  231. var min = new THREE.Vector3();
  232. var max = new THREE.Vector3();
  233. var minVertices = [];
  234. var maxVertices = [];
  235. var i, l, j;
  236. // initially assume that the first vertex is the min/max
  237. for ( i = 0; i < 3; i ++ ) {
  238. minVertices[ i ] = maxVertices[ i ] = this.vertices[ 0 ];
  239. }
  240. min.copy( this.vertices[ 0 ].point );
  241. max.copy( this.vertices[ 0 ].point );
  242. // compute the min/max vertex on all six directions
  243. for ( i = 0, l = this.vertices.length; i < l; i ++ ) {
  244. var vertex = this.vertices[ i ];
  245. var point = vertex.point;
  246. // update the min coordinates
  247. for ( j = 0; j < 3; j ++ ) {
  248. if ( point.getComponent( j ) < min.getComponent( j ) ) {
  249. min.setComponent( j, point.getComponent( j ) );
  250. minVertices[ j ] = vertex;
  251. }
  252. }
  253. // update the max coordinates
  254. for ( j = 0; j < 3; j ++ ) {
  255. if ( point.getComponent( j ) > max.getComponent( j ) ) {
  256. max.setComponent( j, point.getComponent( j ) );
  257. maxVertices[ j ] = vertex;
  258. }
  259. }
  260. }
  261. // use min/max vectors to compute an optimal epsilon
  262. this.tolerance = 3 * Number.EPSILON * (
  263. Math.max( Math.abs( min.x ), Math.abs( max.x ) ) +
  264. Math.max( Math.abs( min.y ), Math.abs( max.y ) ) +
  265. Math.max( Math.abs( min.z ), Math.abs( max.z ) )
  266. );
  267. return { min: minVertices, max: maxVertices };
  268. },
  269. // Computes the initial simplex assigning to its faces all the points
  270. // that are candidates to form part of the hull
  271. computeInitialHull: function () {
  272. var line3, plane, closestPoint;
  273. return function computeInitialHull() {
  274. if ( line3 === undefined ) {
  275. line3 = new THREE.Line3();
  276. plane = new THREE.Plane();
  277. closestPoint = new THREE.Vector3();
  278. }
  279. var vertex, vertices = this.vertices;
  280. var extremes = this.computeExtremes();
  281. var min = extremes.min;
  282. var max = extremes.max;
  283. var v0, v1, v2, v3;
  284. var i, l, j;
  285. // 1. Find the two vertices 'v0' and 'v1' with the greatest 1d separation
  286. // (max.x - min.x)
  287. // (max.y - min.y)
  288. // (max.z - min.z)
  289. var distance, maxDistance = 0;
  290. var index = 0;
  291. for ( i = 0; i < 3; i ++ ) {
  292. distance = max[ i ].point.getComponent( i ) - min[ i ].point.getComponent( i );
  293. if ( distance > maxDistance ) {
  294. maxDistance = distance;
  295. index = i;
  296. }
  297. }
  298. v0 = min[ index ];
  299. v1 = max[ index ];
  300. // 2. The next vertex 'v2' is the one farthest to the line formed by 'v0' and 'v1'
  301. maxDistance = 0;
  302. line3.set( v0.point, v1.point );
  303. for ( i = 0, l = this.vertices.length; i < l; i ++ ) {
  304. vertex = vertices[ i ];
  305. if ( vertex !== v0 && vertex !== v1 ) {
  306. line3.closestPointToPoint( vertex.point, true, closestPoint );
  307. distance = closestPoint.distanceToSquared( vertex.point );
  308. if ( distance > maxDistance ) {
  309. maxDistance = distance;
  310. v2 = vertex;
  311. }
  312. }
  313. }
  314. // 3. The next vertex 'v3' is the one farthest to the plane 'v0', 'v1', 'v2'
  315. maxDistance = - 1;
  316. plane.setFromCoplanarPoints( v0.point, v1.point, v2.point );
  317. for ( i = 0, l = this.vertices.length; i < l; i ++ ) {
  318. vertex = vertices[ i ];
  319. if ( vertex !== v0 && vertex !== v1 && vertex !== v2 ) {
  320. distance = Math.abs( plane.distanceToPoint( vertex.point ) );
  321. if ( distance > maxDistance ) {
  322. maxDistance = distance;
  323. v3 = vertex;
  324. }
  325. }
  326. }
  327. var faces = [];
  328. if ( plane.distanceToPoint( v3.point ) < 0 ) {
  329. // the face is not able to see the point so 'plane.normal' is pointing outside the tetrahedron
  330. faces.push(
  331. Face.create( v0, v1, v2 ),
  332. Face.create( v3, v1, v0 ),
  333. Face.create( v3, v2, v1 ),
  334. Face.create( v3, v0, v2 )
  335. );
  336. // set the twin edge
  337. for ( i = 0; i < 3; i ++ ) {
  338. j = ( i + 1 ) % 3;
  339. // join face[ i ] i > 0, with the first face
  340. faces[ i + 1 ].getEdge( 2 ).setTwin( faces[ 0 ].getEdge( j ) );
  341. // join face[ i ] with face[ i + 1 ], 1 <= i <= 3
  342. faces[ i + 1 ].getEdge( 1 ).setTwin( faces[ j + 1 ].getEdge( 0 ) );
  343. }
  344. } else {
  345. // the face is able to see the point so 'plane.normal' is pointing inside the tetrahedron
  346. faces.push(
  347. Face.create( v0, v2, v1 ),
  348. Face.create( v3, v0, v1 ),
  349. Face.create( v3, v1, v2 ),
  350. Face.create( v3, v2, v0 )
  351. );
  352. // set the twin edge
  353. for ( i = 0; i < 3; i ++ ) {
  354. j = ( i + 1 ) % 3;
  355. // join face[ i ] i > 0, with the first face
  356. faces[ i + 1 ].getEdge( 2 ).setTwin( faces[ 0 ].getEdge( ( 3 - i ) % 3 ) );
  357. // join face[ i ] with face[ i + 1 ]
  358. faces[ i + 1 ].getEdge( 0 ).setTwin( faces[ j + 1 ].getEdge( 1 ) );
  359. }
  360. }
  361. // the initial hull is the tetrahedron
  362. for ( i = 0; i < 4; i ++ ) {
  363. this.faces.push( faces[ i ] );
  364. }
  365. // initial assignment of vertices to the faces of the tetrahedron
  366. for ( i = 0, l = vertices.length; i < l; i ++ ) {
  367. vertex = vertices[ i ];
  368. if ( vertex !== v0 && vertex !== v1 && vertex !== v2 && vertex !== v3 ) {
  369. maxDistance = this.tolerance;
  370. var maxFace = null;
  371. for ( j = 0; j < 4; j ++ ) {
  372. distance = this.faces[ j ].distanceToPoint( vertex.point );
  373. if ( distance > maxDistance ) {
  374. maxDistance = distance;
  375. maxFace = this.faces[ j ];
  376. }
  377. }
  378. if ( maxFace !== null ) {
  379. this.addVertexToFace( vertex, maxFace );
  380. }
  381. }
  382. }
  383. return this;
  384. };
  385. }(),
  386. // Removes inactive faces
  387. reindexFaces: function () {
  388. var activeFaces = [];
  389. for ( var i = 0; i < this.faces.length; i ++ ) {
  390. var face = this.faces[ i ];
  391. if ( face.mark === Visible ) {
  392. activeFaces.push( face );
  393. }
  394. }
  395. this.faces = activeFaces;
  396. return this;
  397. },
  398. // Finds the next vertex to create faces with the current hull
  399. nextVertexToAdd: function () {
  400. // if the 'assigned' list of vertices is empty, no vertices are left. return with 'undefined'
  401. if ( this.assigned.isEmpty() === false ) {
  402. var eyeVertex, maxDistance = 0;
  403. // grap the first available face and start with the first visible vertex of that face
  404. var eyeFace = this.assigned.first().face;
  405. var vertex = eyeFace.outside;
  406. // now calculate the farthest vertex that face can see
  407. do {
  408. var distance = eyeFace.distanceToPoint( vertex.point );
  409. if ( distance > maxDistance ) {
  410. maxDistance = distance;
  411. eyeVertex = vertex;
  412. }
  413. vertex = vertex.next;
  414. } while ( vertex !== null && vertex.face === eyeFace );
  415. return eyeVertex;
  416. }
  417. },
  418. // Computes a chain of half edges in CCW order called the 'horizon'.
  419. // For an edge to be part of the horizon it must join a face that can see
  420. // 'eyePoint' and a face that cannot see 'eyePoint'.
  421. computeHorizon: function ( eyePoint, crossEdge, face, horizon ) {
  422. // moves face's vertices to the 'unassigned' vertex list
  423. this.deleteFaceVertices( face );
  424. face.mark = Deleted;
  425. var edge;
  426. if ( crossEdge === null ) {
  427. edge = crossEdge = face.getEdge( 0 );
  428. } else {
  429. // start from the next edge since 'crossEdge' was already analyzed
  430. // (actually 'crossEdge.twin' was the edge who called this method recursively)
  431. edge = crossEdge.next;
  432. }
  433. do {
  434. var twinEdge = edge.twin;
  435. var oppositeFace = twinEdge.face;
  436. if ( oppositeFace.mark === Visible ) {
  437. if ( oppositeFace.distanceToPoint( eyePoint ) > this.tolerance ) {
  438. // the opposite face can see the vertex, so proceed with next edge
  439. this.computeHorizon( eyePoint, twinEdge, oppositeFace, horizon );
  440. } else {
  441. // the opposite face can't see the vertex, so this edge is part of the horizon
  442. horizon.push( edge );
  443. }
  444. }
  445. edge = edge.next;
  446. } while ( edge !== crossEdge );
  447. return this;
  448. },
  449. // Creates a face with the vertices 'eyeVertex.point', 'horizonEdge.tail' and 'horizonEdge.head' in CCW order
  450. addAdjoiningFace: function ( eyeVertex, horizonEdge ) {
  451. // all the half edges are created in ccw order thus the face is always pointing outside the hull
  452. var face = Face.create( eyeVertex, horizonEdge.tail(), horizonEdge.head() );
  453. this.faces.push( face );
  454. // join face.getEdge( - 1 ) with the horizon's opposite edge face.getEdge( - 1 ) = face.getEdge( 2 )
  455. face.getEdge( - 1 ).setTwin( horizonEdge.twin );
  456. return face.getEdge( 0 ); // the half edge whose vertex is the eyeVertex
  457. },
  458. // Adds 'horizon.length' faces to the hull, each face will be linked with the
  459. // horizon opposite face and the face on the left/right
  460. addNewFaces: function ( eyeVertex, horizon ) {
  461. this.newFaces = [];
  462. var firstSideEdge = null;
  463. var previousSideEdge = null;
  464. for ( var i = 0; i < horizon.length; i ++ ) {
  465. var horizonEdge = horizon[ i ];
  466. // returns the right side edge
  467. var sideEdge = this.addAdjoiningFace( eyeVertex, horizonEdge );
  468. if ( firstSideEdge === null ) {
  469. firstSideEdge = sideEdge;
  470. } else {
  471. // joins face.getEdge( 1 ) with previousFace.getEdge( 0 )
  472. sideEdge.next.setTwin( previousSideEdge );
  473. }
  474. this.newFaces.push( sideEdge.face );
  475. previousSideEdge = sideEdge;
  476. }
  477. // perform final join of new faces
  478. firstSideEdge.next.setTwin( previousSideEdge );
  479. return this;
  480. },
  481. // Adds a vertex to the hull
  482. addVertexToHull: function ( eyeVertex ) {
  483. var horizon = [];
  484. this.unassigned.clear();
  485. // remove 'eyeVertex' from 'eyeVertex.face' so that it can't be added to the 'unassigned' vertex list
  486. this.removeVertexFromFace( eyeVertex, eyeVertex.face );
  487. this.computeHorizon( eyeVertex.point, null, eyeVertex.face, horizon );
  488. this.addNewFaces( eyeVertex, horizon );
  489. // reassign 'unassigned' vertices to the new faces
  490. this.resolveUnassignedPoints( this.newFaces );
  491. return this;
  492. },
  493. cleanup: function () {
  494. this.assigned.clear();
  495. this.unassigned.clear();
  496. this.newFaces = [];
  497. return this;
  498. },
  499. compute: function () {
  500. var vertex;
  501. this.computeInitialHull();
  502. // add all available vertices gradually to the hull
  503. while ( ( vertex = this.nextVertexToAdd() ) !== undefined ) {
  504. this.addVertexToHull( vertex );
  505. }
  506. this.reindexFaces();
  507. this.cleanup();
  508. return this;
  509. }
  510. } );
  511. //
  512. function Face() {
  513. this.normal = new THREE.Vector3();
  514. this.midpoint = new THREE.Vector3();
  515. this.area = 0;
  516. this.constant = 0; // signed distance from face to the origin
  517. this.outside = null; // reference to a vertex in a vertex list this face can see
  518. this.mark = Visible;
  519. this.edge = null;
  520. }
  521. Object.assign( Face, {
  522. create: function ( a, b, c ) {
  523. var face = new Face();
  524. var e0 = new HalfEdge( a, face );
  525. var e1 = new HalfEdge( b, face );
  526. var e2 = new HalfEdge( c, face );
  527. // join edges
  528. e0.next = e2.prev = e1;
  529. e1.next = e0.prev = e2;
  530. e2.next = e1.prev = e0;
  531. // main half edge reference
  532. face.edge = e0;
  533. return face.compute();
  534. }
  535. } );
  536. Object.assign( Face.prototype, {
  537. getEdge: function ( i ) {
  538. var edge = this.edge;
  539. while ( i > 0 ) {
  540. edge = edge.next;
  541. i --;
  542. }
  543. while ( i < 0 ) {
  544. edge = edge.prev;
  545. i ++;
  546. }
  547. return edge;
  548. },
  549. compute: function () {
  550. var triangle;
  551. return function compute() {
  552. if ( triangle === undefined ) triangle = new THREE.Triangle();
  553. var a = this.edge.tail();
  554. var b = this.edge.head();
  555. var c = this.edge.next.head();
  556. triangle.set( a.point, b.point, c.point );
  557. triangle.getNormal( this.normal );
  558. triangle.getMidpoint( this.midpoint );
  559. this.area = triangle.getArea();
  560. this.constant = this.normal.dot( this.midpoint );
  561. return this;
  562. };
  563. }(),
  564. distanceToPoint: function ( point ) {
  565. return this.normal.dot( point ) - this.constant;
  566. }
  567. } );
  568. // Entity for a Doubly-Connected Edge List (DCEL).
  569. function HalfEdge( vertex, face ) {
  570. this.vertex = vertex;
  571. this.prev = null;
  572. this.next = null;
  573. this.twin = null;
  574. this.face = face;
  575. }
  576. Object.assign( HalfEdge.prototype, {
  577. head: function () {
  578. return this.vertex;
  579. },
  580. tail: function () {
  581. return this.prev ? this.prev.vertex : null;
  582. },
  583. length: function () {
  584. var head = this.head();
  585. var tail = this.tail();
  586. if ( tail !== null ) {
  587. return tail.point.distanceTo( head.point );
  588. }
  589. return - 1;
  590. },
  591. lengthSquared: function () {
  592. var head = this.head();
  593. var tail = this.tail();
  594. if ( tail !== null ) {
  595. return tail.point.distanceToSquared( head.point );
  596. }
  597. return - 1;
  598. },
  599. setTwin: function ( edge ) {
  600. this.twin = edge;
  601. edge.twin = this;
  602. return this;
  603. }
  604. } );
  605. // A vertex as a double linked list node.
  606. function VertexNode( point ) {
  607. this.point = point;
  608. this.prev = null;
  609. this.next = null;
  610. this.face = null; // the face that is able to see this vertex
  611. }
  612. // A double linked list that contains vertex nodes.
  613. function VertexList() {
  614. this.head = null;
  615. this.tail = null;
  616. }
  617. Object.assign( VertexList.prototype, {
  618. first: function () {
  619. return this.head;
  620. },
  621. last: function () {
  622. return this.tail;
  623. },
  624. clear: function () {
  625. this.head = this.tail = null;
  626. return this;
  627. },
  628. // Inserts a vertex before the target vertex
  629. insertBefore: function ( target, vertex ) {
  630. vertex.prev = target.prev;
  631. vertex.next = target;
  632. if ( vertex.prev === null ) {
  633. this.head = vertex;
  634. } else {
  635. vertex.prev.next = vertex;
  636. }
  637. target.prev = vertex;
  638. return this;
  639. },
  640. // Inserts a vertex after the target vertex
  641. insertAfter: function ( target, vertex ) {
  642. vertex.prev = target;
  643. vertex.next = target.next;
  644. if ( vertex.next === null ) {
  645. this.tail = vertex;
  646. } else {
  647. vertex.next.prev = vertex;
  648. }
  649. target.next = vertex;
  650. return this;
  651. },
  652. // Appends a vertex to the end of the linked list
  653. append: function ( vertex ) {
  654. if ( this.head === null ) {
  655. this.head = vertex;
  656. } else {
  657. this.tail.next = vertex;
  658. }
  659. vertex.prev = this.tail;
  660. vertex.next = null; // the tail has no subsequent vertex
  661. this.tail = vertex;
  662. return this;
  663. },
  664. // Appends a chain of vertices where 'vertex' is the head.
  665. appendChain: function ( vertex ) {
  666. if ( this.head === null ) {
  667. this.head = vertex;
  668. } else {
  669. this.tail.next = vertex;
  670. }
  671. vertex.prev = this.tail;
  672. // ensure that the 'tail' reference points to the last vertex of the chain
  673. while ( vertex.next !== null ) {
  674. vertex = vertex.next;
  675. }
  676. this.tail = vertex;
  677. return this;
  678. },
  679. // Removes a vertex from the linked list
  680. remove: function ( vertex ) {
  681. if ( vertex.prev === null ) {
  682. this.head = vertex.next;
  683. } else {
  684. vertex.prev.next = vertex.next;
  685. }
  686. if ( vertex.next === null ) {
  687. this.tail = vertex.prev;
  688. } else {
  689. vertex.next.prev = vertex.prev;
  690. }
  691. return this;
  692. },
  693. // Removes a list of vertices whose 'head' is 'a' and whose 'tail' is b
  694. removeSubList: function ( a, b ) {
  695. if ( a.prev === null ) {
  696. this.head = b.next;
  697. } else {
  698. a.prev.next = b.next;
  699. }
  700. if ( b.next === null ) {
  701. this.tail = a.prev;
  702. } else {
  703. b.next.prev = a.prev;
  704. }
  705. return this;
  706. },
  707. isEmpty: function () {
  708. return this.head === null;
  709. }
  710. } );
  711. return ConvexHull;
  712. } )();