ConvexHull.js 22 KB

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