2
0

ConvexHull.js 23 KB

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