2
0

ConvexHull.js 23 KB

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