ConvexHull.js 23 KB

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