QuickHull.js 22 KB

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