ConvexHull.js 22 KB

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