ConvexHull.js 22 KB

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