0
0

ConvexHull.js 22 KB

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