ConvexHull.js 22 KB

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