ConvexHull.js 21 KB

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