ConvexHull.js 21 KB

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