QuickHull.js 23 KB

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