QuickHull.js 21 KB

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