ConvexHull.js 23 KB

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