ConvexHull.js 23 KB

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