ConvexHull.js 23 KB

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