ConvexHull.js 23 KB

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